OrbitControls.js 25 KB

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