OrbitControls.js 25 KB

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