CameraControls.js 26 KB

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