OrbitControls.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. import {
  2. EventDispatcher,
  3. MOUSE,
  4. Quaternion,
  5. Spherical,
  6. TOUCH,
  7. Vector2,
  8. Vector3
  9. } from 'three';
  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 handleMouseUp( /*event*/ ) {
  356. // no-op
  357. }
  358. function handleMouseWheel( event ) {
  359. if ( event.deltaY < 0 ) {
  360. dollyIn( getZoomScale() );
  361. } else if ( event.deltaY > 0 ) {
  362. dollyOut( getZoomScale() );
  363. }
  364. scope.update();
  365. }
  366. function handleKeyDown( event ) {
  367. let needsUpdate = false;
  368. switch ( event.code ) {
  369. case scope.keys.UP:
  370. pan( 0, scope.keyPanSpeed );
  371. needsUpdate = true;
  372. break;
  373. case scope.keys.BOTTOM:
  374. pan( 0, - scope.keyPanSpeed );
  375. needsUpdate = true;
  376. break;
  377. case scope.keys.LEFT:
  378. pan( scope.keyPanSpeed, 0 );
  379. needsUpdate = true;
  380. break;
  381. case scope.keys.RIGHT:
  382. pan( - scope.keyPanSpeed, 0 );
  383. needsUpdate = true;
  384. break;
  385. }
  386. if ( needsUpdate ) {
  387. // prevent the browser from scrolling on cursor keys
  388. event.preventDefault();
  389. scope.update();
  390. }
  391. }
  392. function handleTouchStartRotate() {
  393. if ( pointers.length === 1 ) {
  394. rotateStart.set( pointers[ 0 ].pageX, pointers[ 0 ].pageY );
  395. } else {
  396. const x = 0.5 * ( pointers[ 0 ].pageX + pointers[ 1 ].pageX );
  397. const y = 0.5 * ( pointers[ 0 ].pageY + pointers[ 1 ].pageY );
  398. rotateStart.set( x, y );
  399. }
  400. }
  401. function handleTouchStartPan() {
  402. if ( pointers.length === 1 ) {
  403. panStart.set( pointers[ 0 ].pageX, pointers[ 0 ].pageY );
  404. } else {
  405. const x = 0.5 * ( pointers[ 0 ].pageX + pointers[ 1 ].pageX );
  406. const y = 0.5 * ( pointers[ 0 ].pageY + pointers[ 1 ].pageY );
  407. panStart.set( x, y );
  408. }
  409. }
  410. function handleTouchStartDolly() {
  411. const dx = pointers[ 0 ].pageX - pointers[ 1 ].pageX;
  412. const dy = pointers[ 0 ].pageY - pointers[ 1 ].pageY;
  413. const distance = Math.sqrt( dx * dx + dy * dy );
  414. dollyStart.set( 0, distance );
  415. }
  416. function handleTouchStartDollyPan() {
  417. if ( scope.enableZoom ) handleTouchStartDolly();
  418. if ( scope.enablePan ) handleTouchStartPan();
  419. }
  420. function handleTouchStartDollyRotate() {
  421. if ( scope.enableZoom ) handleTouchStartDolly();
  422. if ( scope.enableRotate ) handleTouchStartRotate();
  423. }
  424. function handleTouchMoveRotate( event ) {
  425. if ( pointers.length == 1 ) {
  426. rotateEnd.set( event.pageX, event.pageY );
  427. } else {
  428. const position = getSecondPointerPosition( event );
  429. const x = 0.5 * ( event.pageX + position.x );
  430. const y = 0.5 * ( event.pageY + position.y );
  431. rotateEnd.set( x, y );
  432. }
  433. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  434. const element = scope.domElement;
  435. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  436. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  437. rotateStart.copy( rotateEnd );
  438. }
  439. function handleTouchMovePan( event ) {
  440. if ( pointers.length === 1 ) {
  441. panEnd.set( event.pageX, event.pageY );
  442. } else {
  443. const position = getSecondPointerPosition( event );
  444. const x = 0.5 * ( event.pageX + position.x );
  445. const y = 0.5 * ( event.pageY + position.y );
  446. panEnd.set( x, y );
  447. }
  448. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  449. pan( panDelta.x, panDelta.y );
  450. panStart.copy( panEnd );
  451. }
  452. function handleTouchMoveDolly( event ) {
  453. const position = getSecondPointerPosition( event );
  454. const dx = event.pageX - position.x;
  455. const dy = event.pageY - position.y;
  456. const distance = Math.sqrt( dx * dx + dy * dy );
  457. dollyEnd.set( 0, distance );
  458. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  459. dollyOut( dollyDelta.y );
  460. dollyStart.copy( dollyEnd );
  461. }
  462. function handleTouchMoveDollyPan( event ) {
  463. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  464. if ( scope.enablePan ) handleTouchMovePan( event );
  465. }
  466. function handleTouchMoveDollyRotate( event ) {
  467. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  468. if ( scope.enableRotate ) handleTouchMoveRotate( event );
  469. }
  470. function handleTouchEnd( /*event*/ ) {
  471. // no-op
  472. }
  473. //
  474. // event handlers - FSM: listen for events and reset state
  475. //
  476. function onPointerDown( event ) {
  477. if ( scope.enabled === false ) return;
  478. if ( pointers.length === 0 ) {
  479. scope.domElement.setPointerCapture( event.pointerId );
  480. scope.domElement.addEventListener( 'pointermove', onPointerMove );
  481. scope.domElement.addEventListener( 'pointerup', onPointerUp );
  482. }
  483. //
  484. addPointer( event );
  485. if ( event.pointerType === 'touch' ) {
  486. onTouchStart( event );
  487. } else {
  488. onMouseDown( event );
  489. }
  490. }
  491. function onPointerMove( event ) {
  492. if ( scope.enabled === false ) return;
  493. if ( event.pointerType === 'touch' ) {
  494. onTouchMove( event );
  495. } else {
  496. onMouseMove( event );
  497. }
  498. }
  499. function onPointerUp( event ) {
  500. if ( scope.enabled === false ) return;
  501. if ( event.pointerType === 'touch' ) {
  502. onTouchEnd();
  503. } else {
  504. onMouseUp( event );
  505. }
  506. removePointer( event );
  507. //
  508. if ( pointers.length === 0 ) {
  509. scope.domElement.releasePointerCapture( event.pointerId );
  510. scope.domElement.removeEventListener( 'pointermove', onPointerMove );
  511. scope.domElement.removeEventListener( 'pointerup', onPointerUp );
  512. }
  513. }
  514. function onPointerCancel( event ) {
  515. removePointer( event );
  516. }
  517. function onMouseDown( event ) {
  518. let mouseAction;
  519. switch ( event.button ) {
  520. case 0:
  521. mouseAction = scope.mouseButtons.LEFT;
  522. break;
  523. case 1:
  524. mouseAction = scope.mouseButtons.MIDDLE;
  525. break;
  526. case 2:
  527. mouseAction = scope.mouseButtons.RIGHT;
  528. break;
  529. default:
  530. mouseAction = - 1;
  531. }
  532. switch ( mouseAction ) {
  533. case MOUSE.DOLLY:
  534. if ( scope.enableZoom === false ) return;
  535. handleMouseDownDolly( event );
  536. state = STATE.DOLLY;
  537. break;
  538. case MOUSE.ROTATE:
  539. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  540. if ( scope.enablePan === false ) return;
  541. handleMouseDownPan( event );
  542. state = STATE.PAN;
  543. } else {
  544. if ( scope.enableRotate === false ) return;
  545. handleMouseDownRotate( event );
  546. state = STATE.ROTATE;
  547. }
  548. break;
  549. case MOUSE.PAN:
  550. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  551. if ( scope.enableRotate === false ) return;
  552. handleMouseDownRotate( event );
  553. state = STATE.ROTATE;
  554. } else {
  555. if ( scope.enablePan === false ) return;
  556. handleMouseDownPan( event );
  557. state = STATE.PAN;
  558. }
  559. break;
  560. default:
  561. state = STATE.NONE;
  562. }
  563. if ( state !== STATE.NONE ) {
  564. scope.dispatchEvent( _startEvent );
  565. }
  566. }
  567. function onMouseMove( event ) {
  568. if ( scope.enabled === false ) return;
  569. switch ( state ) {
  570. case STATE.ROTATE:
  571. if ( scope.enableRotate === false ) return;
  572. handleMouseMoveRotate( event );
  573. break;
  574. case STATE.DOLLY:
  575. if ( scope.enableZoom === false ) return;
  576. handleMouseMoveDolly( event );
  577. break;
  578. case STATE.PAN:
  579. if ( scope.enablePan === false ) return;
  580. handleMouseMovePan( event );
  581. break;
  582. }
  583. }
  584. function onMouseUp( event ) {
  585. handleMouseUp( event );
  586. scope.dispatchEvent( _endEvent );
  587. state = STATE.NONE;
  588. }
  589. function onMouseWheel( event ) {
  590. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  591. event.preventDefault();
  592. scope.dispatchEvent( _startEvent );
  593. handleMouseWheel( event );
  594. scope.dispatchEvent( _endEvent );
  595. }
  596. function onKeyDown( event ) {
  597. if ( scope.enabled === false || scope.enablePan === false ) return;
  598. handleKeyDown( event );
  599. }
  600. function onTouchStart( event ) {
  601. trackPointer( event );
  602. switch ( pointers.length ) {
  603. case 1:
  604. switch ( scope.touches.ONE ) {
  605. case TOUCH.ROTATE:
  606. if ( scope.enableRotate === false ) return;
  607. handleTouchStartRotate();
  608. state = STATE.TOUCH_ROTATE;
  609. break;
  610. case TOUCH.PAN:
  611. if ( scope.enablePan === false ) return;
  612. handleTouchStartPan();
  613. state = STATE.TOUCH_PAN;
  614. break;
  615. default:
  616. state = STATE.NONE;
  617. }
  618. break;
  619. case 2:
  620. switch ( scope.touches.TWO ) {
  621. case TOUCH.DOLLY_PAN:
  622. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  623. handleTouchStartDollyPan();
  624. state = STATE.TOUCH_DOLLY_PAN;
  625. break;
  626. case TOUCH.DOLLY_ROTATE:
  627. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  628. handleTouchStartDollyRotate();
  629. state = STATE.TOUCH_DOLLY_ROTATE;
  630. break;
  631. default:
  632. state = STATE.NONE;
  633. }
  634. break;
  635. default:
  636. state = STATE.NONE;
  637. }
  638. if ( state !== STATE.NONE ) {
  639. scope.dispatchEvent( _startEvent );
  640. }
  641. }
  642. function onTouchMove( event ) {
  643. trackPointer( event );
  644. switch ( state ) {
  645. case STATE.TOUCH_ROTATE:
  646. if ( scope.enableRotate === false ) return;
  647. handleTouchMoveRotate( event );
  648. scope.update();
  649. break;
  650. case STATE.TOUCH_PAN:
  651. if ( scope.enablePan === false ) return;
  652. handleTouchMovePan( event );
  653. scope.update();
  654. break;
  655. case STATE.TOUCH_DOLLY_PAN:
  656. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  657. handleTouchMoveDollyPan( event );
  658. scope.update();
  659. break;
  660. case STATE.TOUCH_DOLLY_ROTATE:
  661. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  662. handleTouchMoveDollyRotate( event );
  663. scope.update();
  664. break;
  665. default:
  666. state = STATE.NONE;
  667. }
  668. }
  669. function onTouchEnd( event ) {
  670. handleTouchEnd( event );
  671. scope.dispatchEvent( _endEvent );
  672. state = STATE.NONE;
  673. }
  674. function onContextMenu( event ) {
  675. if ( scope.enabled === false ) return;
  676. event.preventDefault();
  677. }
  678. function addPointer( event ) {
  679. pointers.push( event );
  680. }
  681. function removePointer( event ) {
  682. delete pointerPositions[ event.pointerId ];
  683. for ( let i = 0; i < pointers.length; i ++ ) {
  684. if ( pointers[ i ].pointerId == event.pointerId ) {
  685. pointers.splice( i, 1 );
  686. return;
  687. }
  688. }
  689. }
  690. function trackPointer( event ) {
  691. let position = pointerPositions[ event.pointerId ];
  692. if ( position === undefined ) {
  693. position = new Vector2();
  694. pointerPositions[ event.pointerId ] = position;
  695. }
  696. position.set( event.pageX, event.pageY );
  697. }
  698. function getSecondPointerPosition( event ) {
  699. const pointer = ( event.pointerId === pointers[ 0 ].pointerId ) ? pointers[ 1 ] : pointers[ 0 ];
  700. return pointerPositions[ pointer.pointerId ];
  701. }
  702. //
  703. scope.domElement.addEventListener( 'contextmenu', onContextMenu );
  704. scope.domElement.addEventListener( 'pointerdown', onPointerDown );
  705. scope.domElement.addEventListener( 'pointercancel', onPointerCancel );
  706. scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
  707. // force an update at start
  708. this.update();
  709. }
  710. }
  711. // This set of controls performs orbiting, dollying (zooming), and panning.
  712. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  713. // This is very similar to OrbitControls, another set of touch behavior
  714. //
  715. // Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
  716. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  717. // Pan - left mouse, or arrow keys / touch: one-finger move
  718. class MapControls extends OrbitControls {
  719. constructor( object, domElement ) {
  720. super( object, domElement );
  721. this.screenSpacePanning = false; // pan orthogonal to world-space direction camera.up
  722. this.mouseButtons.LEFT = MOUSE.PAN;
  723. this.mouseButtons.RIGHT = MOUSE.ROTATE;
  724. this.touches.ONE = TOUCH.PAN;
  725. this.touches.TWO = TOUCH.DOLLY_ROTATE;
  726. }
  727. }
  728. export { OrbitControls, MapControls };