CameraControls.js 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. import {
  2. EventDispatcher,
  3. MOUSE,
  4. Quaternion,
  5. Spherical,
  6. TOUCH,
  7. Vector2,
  8. Vector3
  9. } from 'three';
  10. var CameraControls = function ( object, domElement ) {
  11. if ( domElement === undefined ) console.warn( 'THREE.CameraControls: The second parameter "domElement" is now mandatory.' );
  12. if ( domElement === document ) console.error( 'THREE.CameraControls: "document" should not be used as the target "domElement". Please use "renderer.domElement" instead.' );
  13. this.object = object;
  14. this.domElement = domElement;
  15. // Set to false to disable this control
  16. this.enabled = true;
  17. // "target" sets the location of focus, where the object orbits around
  18. this.target = new Vector3();
  19. // Set to true to enable trackball behavior
  20. this.trackball = false;
  21. // How far you can dolly in and out ( PerspectiveCamera only )
  22. this.minDistance = 0;
  23. this.maxDistance = Infinity;
  24. // How far you can zoom in and out ( OrthographicCamera only )
  25. this.minZoom = 0;
  26. this.maxZoom = Infinity;
  27. // How far you can orbit vertically, upper and lower limits.
  28. // Range is 0 to Math.PI radians.
  29. this.minPolarAngle = 0; // radians
  30. this.maxPolarAngle = Math.PI; // radians
  31. // How far you can orbit horizontally, upper and lower limits.
  32. // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
  33. this.minAzimuthAngle = - Infinity; // radians
  34. this.maxAzimuthAngle = Infinity; // radians
  35. // Set to true to enable damping (inertia)
  36. // If damping is enabled, you must call controls.update() in your animation loop
  37. this.enableDamping = false;
  38. this.dampingFactor = 0.05;
  39. // This option enables dollying in and out; property named as "zoom" for backwards compatibility
  40. // Set to false to disable zooming
  41. this.enableZoom = true;
  42. this.zoomSpeed = 1.0;
  43. // Set to false to disable rotating
  44. this.enableRotate = true;
  45. this.rotateSpeed = 1.0;
  46. // Set to false to disable panning
  47. this.enablePan = true;
  48. this.panSpeed = 1.0;
  49. this.screenSpacePanning = false; // if true, pan in screen-space
  50. this.keyPanSpeed = 7.0; // pixels moved per arrow key push
  51. // Set to true to automatically rotate around the target
  52. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  53. // auto-rotate is not supported for trackball behavior
  54. this.autoRotate = false;
  55. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  56. // Set to false to disable use of the keys
  57. this.enableKeys = true;
  58. // The four arrow keys
  59. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  60. // Mouse buttons
  61. this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
  62. // Touch fingers
  63. this.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN };
  64. // for reset
  65. this.target0 = this.target.clone();
  66. this.position0 = this.object.position.clone();
  67. this.quaternion0 = this.object.quaternion.clone();
  68. this.zoom0 = this.object.zoom;
  69. //
  70. // public methods
  71. //
  72. this.getPolarAngle = function () {
  73. return spherical.phi;
  74. };
  75. this.getAzimuthalAngle = function () {
  76. return spherical.theta;
  77. };
  78. this.saveState = function () {
  79. scope.target0.copy( scope.target );
  80. scope.position0.copy( scope.object.position );
  81. scope.quaternion0.copy( scope.object.quaternion );
  82. scope.zoom0 = scope.object.zoom;
  83. };
  84. this.reset = function () {
  85. scope.target.copy( scope.target0 );
  86. scope.object.position.copy( scope.position0 );
  87. scope.object.quaternion.copy( scope.quaternion0 );
  88. scope.object.zoom = scope.zoom0;
  89. scope.object.updateProjectionMatrix();
  90. scope.dispatchEvent( changeEvent );
  91. scope.update();
  92. state = STATE.NONE;
  93. };
  94. // this method is exposed, but perhaps it would be better if we can make it private...
  95. this.update = function () {
  96. var offset = new Vector3();
  97. // so camera.up is the orbit axis
  98. var quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
  99. var quatInverse = quat.clone().invert();
  100. var lastPosition = new Vector3();
  101. var lastQuaternion = new Quaternion();
  102. var q = new Quaternion();
  103. var vec = new Vector3();
  104. return function update() {
  105. var position = scope.object.position;
  106. offset.copy( position ).sub( scope.target );
  107. if ( scope.trackball ) {
  108. // rotate around screen-space y-axis
  109. if ( sphericalDelta.theta ) {
  110. vec.set( 0, 1, 0 ).applyQuaternion( scope.object.quaternion );
  111. var factor = ( scope.enableDamping ) ? scope.dampingFactor : 1;
  112. q.setFromAxisAngle( vec, sphericalDelta.theta * factor );
  113. scope.object.quaternion.premultiply( q );
  114. offset.applyQuaternion( q );
  115. }
  116. // rotate around screen-space x-axis
  117. if ( sphericalDelta.phi ) {
  118. vec.set( 1, 0, 0 ).applyQuaternion( scope.object.quaternion );
  119. var factor = ( scope.enableDamping ) ? scope.dampingFactor : 1;
  120. q.setFromAxisAngle( vec, sphericalDelta.phi * factor );
  121. scope.object.quaternion.premultiply( q );
  122. offset.applyQuaternion( q );
  123. }
  124. offset.multiplyScalar( scale );
  125. offset.clampLength( scope.minDistance, scope.maxDistance );
  126. } else {
  127. // rotate offset to "y-axis-is-up" space
  128. offset.applyQuaternion( quat );
  129. if ( scope.autoRotate && state === STATE.NONE ) {
  130. rotateLeft( getAutoRotationAngle() );
  131. }
  132. spherical.setFromVector3( offset );
  133. if ( scope.enableDamping ) {
  134. spherical.theta += sphericalDelta.theta * scope.dampingFactor;
  135. spherical.phi += sphericalDelta.phi * scope.dampingFactor;
  136. } else {
  137. spherical.theta += sphericalDelta.theta;
  138. spherical.phi += sphericalDelta.phi;
  139. }
  140. // restrict theta to be between desired limits
  141. spherical.theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, spherical.theta ) );
  142. // restrict phi to be between desired limits
  143. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  144. spherical.makeSafe();
  145. spherical.radius *= scale;
  146. // restrict radius to be between desired limits
  147. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  148. offset.setFromSpherical( spherical );
  149. // rotate offset back to "camera-up-vector-is-up" space
  150. offset.applyQuaternion( quatInverse );
  151. }
  152. // move target to panned location
  153. if ( scope.enableDamping === true ) {
  154. scope.target.addScaledVector( panOffset, scope.dampingFactor );
  155. } else {
  156. scope.target.add( panOffset );
  157. }
  158. position.copy( scope.target ).add( offset );
  159. if ( scope.trackball === false ) {
  160. scope.object.lookAt( scope.target );
  161. }
  162. if ( scope.enableDamping === true ) {
  163. sphericalDelta.theta *= ( 1 - scope.dampingFactor );
  164. sphericalDelta.phi *= ( 1 - scope.dampingFactor );
  165. panOffset.multiplyScalar( 1 - scope.dampingFactor );
  166. } else {
  167. sphericalDelta.set( 0, 0, 0 );
  168. panOffset.set( 0, 0, 0 );
  169. }
  170. scale = 1;
  171. // update condition is:
  172. // min(camera displacement, camera rotation in radians)^2 > EPS
  173. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  174. if ( zoomChanged ||
  175. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  176. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  177. scope.dispatchEvent( changeEvent );
  178. lastPosition.copy( scope.object.position );
  179. lastQuaternion.copy( scope.object.quaternion );
  180. zoomChanged = false;
  181. return true;
  182. }
  183. return false;
  184. };
  185. }();
  186. this.dispose = function () {
  187. scope.domElement.removeEventListener( 'contextmenu', onContextMenu, false );
  188. scope.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  189. scope.domElement.removeEventListener( 'wheel', onMouseWheel, false );
  190. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  191. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  192. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  193. document.removeEventListener( 'mousemove', onMouseMove, false );
  194. document.removeEventListener( 'mouseup', onMouseUp, false );
  195. scope.domElement.removeEventListener( 'keydown', onKeyDown, false );
  196. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  197. };
  198. //
  199. // internals
  200. //
  201. var scope = this;
  202. var changeEvent = { type: 'change' };
  203. var startEvent = { type: 'start' };
  204. var endEvent = { type: 'end' };
  205. var STATE = {
  206. NONE: - 1,
  207. ROTATE: 0,
  208. DOLLY: 1,
  209. PAN: 2,
  210. TOUCH_ROTATE: 3,
  211. TOUCH_PAN: 4,
  212. TOUCH_DOLLY_PAN: 5,
  213. TOUCH_DOLLY_ROTATE: 6
  214. };
  215. var state = STATE.NONE;
  216. var EPS = 0.000001;
  217. // current position in spherical coordinates
  218. var spherical = new Spherical();
  219. var sphericalDelta = new Spherical();
  220. var scale = 1;
  221. var panOffset = new Vector3();
  222. var zoomChanged = false;
  223. var rotateStart = new Vector2();
  224. var rotateEnd = new Vector2();
  225. var rotateDelta = new Vector2();
  226. var panStart = new Vector2();
  227. var panEnd = new Vector2();
  228. var panDelta = new Vector2();
  229. var dollyStart = new Vector2();
  230. var dollyEnd = new Vector2();
  231. var dollyDelta = new Vector2();
  232. function getAutoRotationAngle() {
  233. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  234. }
  235. function getZoomScale() {
  236. return Math.pow( 0.95, scope.zoomSpeed );
  237. }
  238. function rotateLeft( angle ) {
  239. sphericalDelta.theta -= angle;
  240. }
  241. function rotateUp( angle ) {
  242. sphericalDelta.phi -= angle;
  243. }
  244. var panLeft = function () {
  245. var v = new Vector3();
  246. return function panLeft( distance, objectMatrix ) {
  247. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  248. v.multiplyScalar( - distance );
  249. panOffset.add( v );
  250. };
  251. }();
  252. var panUp = function () {
  253. var v = new Vector3();
  254. return function panUp( distance, objectMatrix ) {
  255. if ( scope.screenSpacePanning === true ) {
  256. v.setFromMatrixColumn( objectMatrix, 1 );
  257. } else {
  258. v.setFromMatrixColumn( objectMatrix, 0 );
  259. v.crossVectors( scope.object.up, v );
  260. }
  261. v.multiplyScalar( distance );
  262. panOffset.add( v );
  263. };
  264. }();
  265. // deltaX and deltaY are in pixels; right and down are positive
  266. var pan = function () {
  267. var offset = new Vector3();
  268. return function pan( deltaX, deltaY ) {
  269. var element = scope.domElement;
  270. if ( scope.object.isPerspectiveCamera ) {
  271. // perspective
  272. var position = scope.object.position;
  273. offset.copy( position ).sub( scope.target );
  274. var targetDistance = offset.length();
  275. // half of the fov is center to top of screen
  276. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  277. // we use only clientHeight here so aspect ratio does not distort speed
  278. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  279. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  280. } else if ( scope.object.isOrthographicCamera ) {
  281. // orthographic
  282. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  283. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  284. } else {
  285. // camera neither orthographic nor perspective
  286. console.warn( 'WARNING: CameraControls.js encountered an unknown camera type - pan disabled.' );
  287. scope.enablePan = false;
  288. }
  289. };
  290. }();
  291. function dollyIn( dollyScale ) {
  292. if ( scope.object.isPerspectiveCamera ) {
  293. scale /= dollyScale;
  294. } else if ( scope.object.isOrthographicCamera ) {
  295. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  296. scope.object.updateProjectionMatrix();
  297. zoomChanged = true;
  298. } else {
  299. console.warn( 'WARNING: CameraControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  300. scope.enableZoom = false;
  301. }
  302. }
  303. function dollyOut( dollyScale ) {
  304. if ( scope.object.isPerspectiveCamera ) {
  305. scale *= dollyScale;
  306. } else if ( scope.object.isOrthographicCamera ) {
  307. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  308. scope.object.updateProjectionMatrix();
  309. zoomChanged = true;
  310. } else {
  311. console.warn( 'WARNING: CameraControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  312. scope.enableZoom = false;
  313. }
  314. }
  315. //
  316. // event callbacks - update the object state
  317. //
  318. function handleMouseDownRotate( event ) {
  319. rotateStart.set( event.clientX, event.clientY );
  320. }
  321. function handleMouseDownDolly( event ) {
  322. dollyStart.set( event.clientX, event.clientY );
  323. }
  324. function handleMouseDownPan( event ) {
  325. panStart.set( event.clientX, event.clientY );
  326. }
  327. function handleMouseMoveRotate( event ) {
  328. rotateEnd.set( event.clientX, event.clientY );
  329. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  330. var element = scope.domElement;
  331. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  332. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  333. rotateStart.copy( rotateEnd );
  334. scope.update();
  335. }
  336. function handleMouseMoveDolly( event ) {
  337. dollyEnd.set( event.clientX, event.clientY );
  338. dollyDelta.subVectors( dollyEnd, dollyStart );
  339. if ( dollyDelta.y > 0 ) {
  340. dollyIn( getZoomScale() );
  341. } else if ( dollyDelta.y < 0 ) {
  342. dollyOut( getZoomScale() );
  343. }
  344. dollyStart.copy( dollyEnd );
  345. scope.update();
  346. }
  347. function handleMouseMovePan( event ) {
  348. panEnd.set( event.clientX, event.clientY );
  349. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  350. pan( panDelta.x, panDelta.y );
  351. panStart.copy( panEnd );
  352. scope.update();
  353. }
  354. function handleMouseUp( /*event*/ ) {
  355. // no-op
  356. }
  357. function handleMouseWheel( event ) {
  358. if ( event.deltaY < 0 ) {
  359. dollyOut( getZoomScale() );
  360. } else if ( event.deltaY > 0 ) {
  361. dollyIn( getZoomScale() );
  362. }
  363. scope.update();
  364. }
  365. function handleKeyDown( event ) {
  366. var needsUpdate = false;
  367. switch ( event.keyCode ) {
  368. case scope.keys.UP:
  369. pan( 0, scope.keyPanSpeed );
  370. needsUpdate = true;
  371. break;
  372. case scope.keys.BOTTOM:
  373. pan( 0, - scope.keyPanSpeed );
  374. needsUpdate = true;
  375. break;
  376. case scope.keys.LEFT:
  377. pan( scope.keyPanSpeed, 0 );
  378. needsUpdate = true;
  379. break;
  380. case scope.keys.RIGHT:
  381. pan( - scope.keyPanSpeed, 0 );
  382. needsUpdate = true;
  383. break;
  384. }
  385. if ( needsUpdate ) {
  386. // prevent the browser from scrolling on cursor keys
  387. event.preventDefault();
  388. scope.update();
  389. }
  390. }
  391. function handleTouchStartRotate( event ) {
  392. if ( event.touches.length == 1 ) {
  393. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  394. } else {
  395. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  396. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  397. rotateStart.set( x, y );
  398. }
  399. }
  400. function handleTouchStartPan( event ) {
  401. if ( event.touches.length == 1 ) {
  402. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  403. } else {
  404. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  405. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  406. panStart.set( x, y );
  407. }
  408. }
  409. function handleTouchStartDolly( event ) {
  410. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  411. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  412. var distance = Math.sqrt( dx * dx + dy * dy );
  413. dollyStart.set( 0, distance );
  414. }
  415. function handleTouchStartDollyPan( event ) {
  416. if ( scope.enableZoom ) handleTouchStartDolly( event );
  417. if ( scope.enablePan ) handleTouchStartPan( event );
  418. }
  419. function handleTouchStartDollyRotate( event ) {
  420. if ( scope.enableZoom ) handleTouchStartDolly( event );
  421. if ( scope.enableRotate ) handleTouchStartRotate( event );
  422. }
  423. function handleTouchMoveRotate( event ) {
  424. if ( event.touches.length == 1 ) {
  425. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  426. } else {
  427. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  428. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  429. rotateEnd.set( x, y );
  430. }
  431. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  432. var element = scope.domElement;
  433. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  434. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  435. rotateStart.copy( rotateEnd );
  436. }
  437. function handleTouchMovePan( event ) {
  438. if ( event.touches.length == 1 ) {
  439. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  440. } else {
  441. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  442. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  443. panEnd.set( x, y );
  444. }
  445. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  446. pan( panDelta.x, panDelta.y );
  447. panStart.copy( panEnd );
  448. }
  449. function handleTouchMoveDolly( event ) {
  450. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  451. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  452. var distance = Math.sqrt( dx * dx + dy * dy );
  453. dollyEnd.set( 0, distance );
  454. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  455. dollyIn( dollyDelta.y );
  456. dollyStart.copy( dollyEnd );
  457. }
  458. function handleTouchMoveDollyPan( event ) {
  459. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  460. if ( scope.enablePan ) handleTouchMovePan( event );
  461. }
  462. function handleTouchMoveDollyRotate( event ) {
  463. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  464. if ( scope.enableRotate ) handleTouchMoveRotate( event );
  465. }
  466. function handleTouchEnd( /*event*/ ) {
  467. // no-op
  468. }
  469. //
  470. // event handlers - FSM: listen for events and reset state
  471. //
  472. function onMouseDown( event ) {
  473. if ( scope.enabled === false ) return;
  474. // Prevent the browser from scrolling.
  475. event.preventDefault();
  476. // Manually set the focus since calling preventDefault above
  477. // prevents the browser from setting it automatically.
  478. scope.domElement.focus ? scope.domElement.focus() : window.focus();
  479. var mouseAction;
  480. switch ( event.button ) {
  481. case 0:
  482. mouseAction = scope.mouseButtons.LEFT;
  483. break;
  484. case 1:
  485. mouseAction = scope.mouseButtons.MIDDLE;
  486. break;
  487. case 2:
  488. mouseAction = scope.mouseButtons.RIGHT;
  489. break;
  490. default:
  491. mouseAction = - 1;
  492. }
  493. switch ( mouseAction ) {
  494. case MOUSE.DOLLY:
  495. if ( scope.enableZoom === false ) return;
  496. handleMouseDownDolly( event );
  497. state = STATE.DOLLY;
  498. break;
  499. case MOUSE.ROTATE:
  500. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  501. if ( scope.enablePan === false ) return;
  502. handleMouseDownPan( event );
  503. state = STATE.PAN;
  504. } else {
  505. if ( scope.enableRotate === false ) return;
  506. handleMouseDownRotate( event );
  507. state = STATE.ROTATE;
  508. }
  509. break;
  510. case MOUSE.PAN:
  511. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  512. if ( scope.enableRotate === false ) return;
  513. handleMouseDownRotate( event );
  514. state = STATE.ROTATE;
  515. } else {
  516. if ( scope.enablePan === false ) return;
  517. handleMouseDownPan( event );
  518. state = STATE.PAN;
  519. }
  520. break;
  521. default:
  522. state = STATE.NONE;
  523. }
  524. if ( state !== STATE.NONE ) {
  525. document.addEventListener( 'mousemove', onMouseMove, false );
  526. document.addEventListener( 'mouseup', onMouseUp, false );
  527. scope.dispatchEvent( startEvent );
  528. }
  529. }
  530. function onMouseMove( event ) {
  531. if ( scope.enabled === false ) return;
  532. event.preventDefault();
  533. switch ( state ) {
  534. case STATE.ROTATE:
  535. if ( scope.enableRotate === false ) return;
  536. handleMouseMoveRotate( event );
  537. break;
  538. case STATE.DOLLY:
  539. if ( scope.enableZoom === false ) return;
  540. handleMouseMoveDolly( event );
  541. break;
  542. case STATE.PAN:
  543. if ( scope.enablePan === false ) return;
  544. handleMouseMovePan( event );
  545. break;
  546. }
  547. }
  548. function onMouseUp( event ) {
  549. if ( scope.enabled === false ) return;
  550. handleMouseUp( event );
  551. document.removeEventListener( 'mousemove', onMouseMove, false );
  552. document.removeEventListener( 'mouseup', onMouseUp, false );
  553. scope.dispatchEvent( endEvent );
  554. state = STATE.NONE;
  555. }
  556. function onMouseWheel( event ) {
  557. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  558. event.preventDefault();
  559. event.stopPropagation();
  560. scope.dispatchEvent( startEvent );
  561. handleMouseWheel( event );
  562. scope.dispatchEvent( endEvent );
  563. }
  564. function onKeyDown( event ) {
  565. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  566. handleKeyDown( event );
  567. }
  568. function onTouchStart( event ) {
  569. if ( scope.enabled === false ) return;
  570. event.preventDefault();
  571. switch ( event.touches.length ) {
  572. case 1:
  573. switch ( scope.touches.ONE ) {
  574. case TOUCH.ROTATE:
  575. if ( scope.enableRotate === false ) return;
  576. handleTouchStartRotate( event );
  577. state = STATE.TOUCH_ROTATE;
  578. break;
  579. case TOUCH.PAN:
  580. if ( scope.enablePan === false ) return;
  581. handleTouchStartPan( event );
  582. state = STATE.TOUCH_PAN;
  583. break;
  584. default:
  585. state = STATE.NONE;
  586. }
  587. break;
  588. case 2:
  589. switch ( scope.touches.TWO ) {
  590. case TOUCH.DOLLY_PAN:
  591. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  592. handleTouchStartDollyPan( event );
  593. state = STATE.TOUCH_DOLLY_PAN;
  594. break;
  595. case TOUCH.DOLLY_ROTATE:
  596. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  597. handleTouchStartDollyRotate( event );
  598. state = STATE.TOUCH_DOLLY_ROTATE;
  599. break;
  600. default:
  601. state = STATE.NONE;
  602. }
  603. break;
  604. default:
  605. state = STATE.NONE;
  606. }
  607. if ( state !== STATE.NONE ) {
  608. scope.dispatchEvent( startEvent );
  609. }
  610. }
  611. function onTouchMove( event ) {
  612. if ( scope.enabled === false ) return;
  613. event.preventDefault();
  614. event.stopPropagation();
  615. switch ( state ) {
  616. case STATE.TOUCH_ROTATE:
  617. if ( scope.enableRotate === false ) return;
  618. handleTouchMoveRotate( event );
  619. scope.update();
  620. break;
  621. case STATE.TOUCH_PAN:
  622. if ( scope.enablePan === false ) return;
  623. handleTouchMovePan( event );
  624. scope.update();
  625. break;
  626. case STATE.TOUCH_DOLLY_PAN:
  627. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  628. handleTouchMoveDollyPan( event );
  629. scope.update();
  630. break;
  631. case STATE.TOUCH_DOLLY_ROTATE:
  632. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  633. handleTouchMoveDollyRotate( event );
  634. scope.update();
  635. break;
  636. default:
  637. state = STATE.NONE;
  638. }
  639. }
  640. function onTouchEnd( event ) {
  641. if ( scope.enabled === false ) return;
  642. handleTouchEnd( event );
  643. scope.dispatchEvent( endEvent );
  644. state = STATE.NONE;
  645. }
  646. function onContextMenu( event ) {
  647. if ( scope.enabled === false ) return;
  648. event.preventDefault();
  649. }
  650. //
  651. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  652. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  653. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  654. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  655. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  656. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  657. scope.domElement.addEventListener( 'keydown', onKeyDown, false );
  658. // make sure element can receive keys.
  659. if ( scope.domElement.tabIndex === - 1 ) {
  660. scope.domElement.tabIndex = 0;
  661. }
  662. // force an update at start
  663. this.object.lookAt( scope.target );
  664. this.update();
  665. this.saveState();
  666. };
  667. CameraControls.prototype = Object.create( EventDispatcher.prototype );
  668. CameraControls.prototype.constructor = CameraControls;
  669. // OrbitControls maintains the "up" direction, camera.up (+Y by default).
  670. //
  671. // Orbit - left mouse / touch: one-finger move
  672. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  673. // Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move
  674. var OrbitControls = function ( object, domElement ) {
  675. CameraControls.call( this, object, domElement );
  676. this.mouseButtons.LEFT = MOUSE.ROTATE;
  677. this.mouseButtons.RIGHT = MOUSE.PAN;
  678. this.touches.ONE = TOUCH.ROTATE;
  679. this.touches.TWO = TOUCH.DOLLY_PAN;
  680. };
  681. OrbitControls.prototype = Object.create( EventDispatcher.prototype );
  682. OrbitControls.prototype.constructor = OrbitControls;
  683. // MapControls maintains the "up" direction, camera.up (+Y by default)
  684. //
  685. // Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
  686. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  687. // Pan - left mouse, or left right + ctrl/meta/shiftKey, or arrow keys / touch: one-finger move
  688. var MapControls = function ( object, domElement ) {
  689. CameraControls.call( this, object, domElement );
  690. this.mouseButtons.LEFT = MOUSE.PAN;
  691. this.mouseButtons.RIGHT = MOUSE.ROTATE;
  692. this.touches.ONE = TOUCH.PAN;
  693. this.touches.TWO = TOUCH.DOLLY_ROTATE;
  694. };
  695. MapControls.prototype = Object.create( EventDispatcher.prototype );
  696. MapControls.prototype.constructor = MapControls;
  697. // TrackballControls allows the camera to rotate over the polls and does not maintain camera.up
  698. //
  699. // Orbit - left mouse / touch: one-finger move
  700. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  701. // Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move
  702. var TrackballControls = function ( object, domElement ) {
  703. CameraControls.call( this, object, domElement );
  704. this.trackball = true;
  705. this.screenSpacePanning = true;
  706. this.autoRotate = false;
  707. this.mouseButtons.LEFT = MOUSE.ROTATE;
  708. this.mouseButtons.RIGHT = MOUSE.PAN;
  709. this.touches.ONE = TOUCH.ROTATE;
  710. this.touches.TWO = TOUCH.DOLLY_PAN;
  711. };
  712. TrackballControls.prototype = Object.create( EventDispatcher.prototype );
  713. TrackballControls.prototype.constructor = TrackballControls;
  714. export { CameraControls, OrbitControls, MapControls, TrackballControls };