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