CameraControls.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. import {
  2. EventDispatcher,
  3. MOUSE,
  4. Quaternion,
  5. Spherical,
  6. TOUCH,
  7. Vector2,
  8. Vector3
  9. } from '../../../../build/three.module.js';
  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 };