2
0

OrbitControls.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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.listenToKeyEvents = function ( domElement ) {
  87. domElement.addEventListener( 'keydown', onKeyDown );
  88. this._domElementKeyEvents = domElement;
  89. };
  90. this.saveState = function () {
  91. scope.target0.copy( scope.target );
  92. scope.position0.copy( scope.object.position );
  93. scope.zoom0 = scope.object.zoom;
  94. };
  95. this.reset = function () {
  96. scope.target.copy( scope.target0 );
  97. scope.object.position.copy( scope.position0 );
  98. scope.object.zoom = scope.zoom0;
  99. scope.object.updateProjectionMatrix();
  100. scope.dispatchEvent( _changeEvent );
  101. scope.update();
  102. state = STATE.NONE;
  103. };
  104. // this method is exposed, but perhaps it would be better if we can make it private...
  105. this.update = function () {
  106. const offset = new Vector3();
  107. // so camera.up is the orbit axis
  108. const quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
  109. const quatInverse = quat.clone().invert();
  110. const lastPosition = new Vector3();
  111. const lastQuaternion = new Quaternion();
  112. const twoPI = 2 * Math.PI;
  113. return function update() {
  114. const position = scope.object.position;
  115. offset.copy( position ).sub( scope.target );
  116. // rotate offset to "y-axis-is-up" space
  117. offset.applyQuaternion( quat );
  118. // angle from z-axis around y-axis
  119. spherical.setFromVector3( offset );
  120. if ( scope.autoRotate && state === STATE.NONE ) {
  121. rotateLeft( getAutoRotationAngle() );
  122. }
  123. if ( scope.enableDamping ) {
  124. spherical.theta += sphericalDelta.theta * scope.dampingFactor;
  125. spherical.phi += sphericalDelta.phi * scope.dampingFactor;
  126. } else {
  127. spherical.theta += sphericalDelta.theta;
  128. spherical.phi += sphericalDelta.phi;
  129. }
  130. // restrict theta to be between desired limits
  131. let min = scope.minAzimuthAngle;
  132. let max = scope.maxAzimuthAngle;
  133. if ( isFinite( min ) && isFinite( max ) ) {
  134. if ( min < - Math.PI ) min += twoPI; else if ( min > Math.PI ) min -= twoPI;
  135. if ( max < - Math.PI ) max += twoPI; else if ( max > Math.PI ) max -= twoPI;
  136. if ( min <= max ) {
  137. spherical.theta = Math.max( min, Math.min( max, spherical.theta ) );
  138. } else {
  139. spherical.theta = ( spherical.theta > ( min + max ) / 2 ) ?
  140. Math.max( min, spherical.theta ) :
  141. Math.min( max, spherical.theta );
  142. }
  143. }
  144. // restrict phi to be between desired limits
  145. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  146. spherical.makeSafe();
  147. spherical.radius *= scale;
  148. // restrict radius to be between desired limits
  149. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  150. // move target to panned location
  151. if ( scope.enableDamping === true ) {
  152. scope.target.addScaledVector( panOffset, scope.dampingFactor );
  153. } else {
  154. scope.target.add( panOffset );
  155. }
  156. offset.setFromSpherical( spherical );
  157. // rotate offset back to "camera-up-vector-is-up" space
  158. offset.applyQuaternion( quatInverse );
  159. position.copy( scope.target ).add( offset );
  160. scope.object.lookAt( scope.target );
  161. if ( scope.enableDamping === true ) {
  162. sphericalDelta.theta *= ( 1 - scope.dampingFactor );
  163. sphericalDelta.phi *= ( 1 - scope.dampingFactor );
  164. panOffset.multiplyScalar( 1 - scope.dampingFactor );
  165. } else {
  166. sphericalDelta.set( 0, 0, 0 );
  167. panOffset.set( 0, 0, 0 );
  168. }
  169. scale = 1;
  170. // update condition is:
  171. // min(camera displacement, camera rotation in radians)^2 > EPS
  172. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  173. if ( zoomChanged ||
  174. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  175. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  176. scope.dispatchEvent( _changeEvent );
  177. lastPosition.copy( scope.object.position );
  178. lastQuaternion.copy( scope.object.quaternion );
  179. zoomChanged = false;
  180. return true;
  181. }
  182. return false;
  183. };
  184. }();
  185. this.dispose = function () {
  186. scope.domElement.removeEventListener( 'contextmenu', onContextMenu );
  187. scope.domElement.removeEventListener( 'pointerdown', onPointerDown );
  188. scope.domElement.removeEventListener( 'pointercancel', onPointerCancel );
  189. scope.domElement.removeEventListener( 'wheel', onMouseWheel );
  190. scope.domElement.removeEventListener( 'pointermove', onPointerMove );
  191. scope.domElement.removeEventListener( 'pointerup', onPointerUp );
  192. if ( scope._domElementKeyEvents !== null ) {
  193. scope._domElementKeyEvents.removeEventListener( 'keydown', onKeyDown );
  194. }
  195. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  196. };
  197. //
  198. // internals
  199. //
  200. const scope = this;
  201. const STATE = {
  202. NONE: - 1,
  203. ROTATE: 0,
  204. DOLLY: 1,
  205. PAN: 2,
  206. TOUCH_ROTATE: 3,
  207. TOUCH_PAN: 4,
  208. TOUCH_DOLLY_PAN: 5,
  209. TOUCH_DOLLY_ROTATE: 6
  210. };
  211. let state = STATE.NONE;
  212. const EPS = 0.000001;
  213. // current position in spherical coordinates
  214. const spherical = new Spherical();
  215. const sphericalDelta = new Spherical();
  216. let scale = 1;
  217. const panOffset = new Vector3();
  218. let zoomChanged = false;
  219. const rotateStart = new Vector2();
  220. const rotateEnd = new Vector2();
  221. const rotateDelta = new Vector2();
  222. const panStart = new Vector2();
  223. const panEnd = new Vector2();
  224. const panDelta = new Vector2();
  225. const dollyStart = new Vector2();
  226. const dollyEnd = new Vector2();
  227. const dollyDelta = new Vector2();
  228. const pointers = [];
  229. const pointerPositions = {};
  230. function getAutoRotationAngle() {
  231. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  232. }
  233. function getZoomScale() {
  234. return Math.pow( 0.95, scope.zoomSpeed );
  235. }
  236. function rotateLeft( angle ) {
  237. sphericalDelta.theta -= angle;
  238. }
  239. function rotateUp( angle ) {
  240. sphericalDelta.phi -= angle;
  241. }
  242. const panLeft = function () {
  243. const v = new Vector3();
  244. return function panLeft( distance, objectMatrix ) {
  245. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  246. v.multiplyScalar( - distance );
  247. panOffset.add( v );
  248. };
  249. }();
  250. const panUp = function () {
  251. const v = new Vector3();
  252. return function panUp( distance, objectMatrix ) {
  253. if ( scope.screenSpacePanning === true ) {
  254. v.setFromMatrixColumn( objectMatrix, 1 );
  255. } else {
  256. v.setFromMatrixColumn( objectMatrix, 0 );
  257. v.crossVectors( scope.object.up, v );
  258. }
  259. v.multiplyScalar( distance );
  260. panOffset.add( v );
  261. };
  262. }();
  263. // deltaX and deltaY are in pixels; right and down are positive
  264. const pan = function () {
  265. const offset = new Vector3();
  266. return function pan( deltaX, deltaY ) {
  267. const element = scope.domElement;
  268. if ( scope.object.isPerspectiveCamera ) {
  269. // perspective
  270. const position = scope.object.position;
  271. offset.copy( position ).sub( scope.target );
  272. let targetDistance = offset.length();
  273. // half of the fov is center to top of screen
  274. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  275. // we use only clientHeight here so aspect ratio does not distort speed
  276. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  277. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  278. } else if ( scope.object.isOrthographicCamera ) {
  279. // orthographic
  280. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  281. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  282. } else {
  283. // camera neither orthographic nor perspective
  284. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  285. scope.enablePan = false;
  286. }
  287. };
  288. }();
  289. function dollyOut( dollyScale ) {
  290. if ( scope.object.isPerspectiveCamera ) {
  291. scale /= dollyScale;
  292. } else if ( scope.object.isOrthographicCamera ) {
  293. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  294. scope.object.updateProjectionMatrix();
  295. zoomChanged = true;
  296. } else {
  297. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  298. scope.enableZoom = false;
  299. }
  300. }
  301. function dollyIn( dollyScale ) {
  302. if ( scope.object.isPerspectiveCamera ) {
  303. scale *= dollyScale;
  304. } else if ( scope.object.isOrthographicCamera ) {
  305. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  306. scope.object.updateProjectionMatrix();
  307. zoomChanged = true;
  308. } else {
  309. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  310. scope.enableZoom = false;
  311. }
  312. }
  313. //
  314. // event callbacks - update the object state
  315. //
  316. function handleMouseDownRotate( event ) {
  317. rotateStart.set( event.clientX, event.clientY );
  318. }
  319. function handleMouseDownDolly( event ) {
  320. dollyStart.set( event.clientX, event.clientY );
  321. }
  322. function handleMouseDownPan( event ) {
  323. panStart.set( event.clientX, event.clientY );
  324. }
  325. function handleMouseMoveRotate( event ) {
  326. rotateEnd.set( event.clientX, event.clientY );
  327. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  328. const element = scope.domElement;
  329. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  330. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  331. rotateStart.copy( rotateEnd );
  332. scope.update();
  333. }
  334. function handleMouseMoveDolly( event ) {
  335. dollyEnd.set( event.clientX, event.clientY );
  336. dollyDelta.subVectors( dollyEnd, dollyStart );
  337. if ( dollyDelta.y > 0 ) {
  338. dollyOut( getZoomScale() );
  339. } else if ( dollyDelta.y < 0 ) {
  340. dollyIn( getZoomScale() );
  341. }
  342. dollyStart.copy( dollyEnd );
  343. scope.update();
  344. }
  345. function handleMouseMovePan( event ) {
  346. panEnd.set( event.clientX, event.clientY );
  347. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  348. pan( panDelta.x, panDelta.y );
  349. panStart.copy( panEnd );
  350. scope.update();
  351. }
  352. function handleMouseUp( /*event*/ ) {
  353. // no-op
  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. function handleTouchEnd( /*event*/ ) {
  468. // no-op
  469. }
  470. //
  471. // event handlers - FSM: listen for events and reset state
  472. //
  473. function onPointerDown( event ) {
  474. if ( scope.enabled === false ) return;
  475. if ( pointers.length === 0 ) {
  476. scope.domElement.setPointerCapture( event.pointerId );
  477. scope.domElement.addEventListener( 'pointermove', onPointerMove );
  478. scope.domElement.addEventListener( 'pointerup', onPointerUp );
  479. }
  480. //
  481. addPointer( event );
  482. if ( event.pointerType === 'touch' ) {
  483. onTouchStart( event );
  484. } else {
  485. onMouseDown( event );
  486. }
  487. }
  488. function onPointerMove( event ) {
  489. if ( scope.enabled === false ) return;
  490. if ( event.pointerType === 'touch' ) {
  491. onTouchMove( event );
  492. } else {
  493. onMouseMove( event );
  494. }
  495. }
  496. function onPointerUp( event ) {
  497. if ( scope.enabled === false ) return;
  498. if ( event.pointerType === 'touch' ) {
  499. onTouchEnd();
  500. } else {
  501. onMouseUp( event );
  502. }
  503. removePointer( event );
  504. //
  505. if ( pointers.length === 0 ) {
  506. scope.domElement.releasePointerCapture( event.pointerId );
  507. scope.domElement.removeEventListener( 'pointermove', onPointerMove );
  508. scope.domElement.removeEventListener( 'pointerup', onPointerUp );
  509. }
  510. }
  511. function onPointerCancel( event ) {
  512. removePointer( event );
  513. }
  514. function onMouseDown( event ) {
  515. let mouseAction;
  516. switch ( event.button ) {
  517. case 0:
  518. mouseAction = scope.mouseButtons.LEFT;
  519. break;
  520. case 1:
  521. mouseAction = scope.mouseButtons.MIDDLE;
  522. break;
  523. case 2:
  524. mouseAction = scope.mouseButtons.RIGHT;
  525. break;
  526. default:
  527. mouseAction = - 1;
  528. }
  529. switch ( mouseAction ) {
  530. case MOUSE.DOLLY:
  531. if ( scope.enableZoom === false ) return;
  532. handleMouseDownDolly( event );
  533. state = STATE.DOLLY;
  534. break;
  535. case MOUSE.ROTATE:
  536. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  537. if ( scope.enablePan === false ) return;
  538. handleMouseDownPan( event );
  539. state = STATE.PAN;
  540. } else {
  541. if ( scope.enableRotate === false ) return;
  542. handleMouseDownRotate( event );
  543. state = STATE.ROTATE;
  544. }
  545. break;
  546. case MOUSE.PAN:
  547. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  548. if ( scope.enableRotate === false ) return;
  549. handleMouseDownRotate( event );
  550. state = STATE.ROTATE;
  551. } else {
  552. if ( scope.enablePan === false ) return;
  553. handleMouseDownPan( event );
  554. state = STATE.PAN;
  555. }
  556. break;
  557. default:
  558. state = STATE.NONE;
  559. }
  560. if ( state !== STATE.NONE ) {
  561. scope.dispatchEvent( _startEvent );
  562. }
  563. }
  564. function onMouseMove( event ) {
  565. if ( scope.enabled === false ) return;
  566. switch ( state ) {
  567. case STATE.ROTATE:
  568. if ( scope.enableRotate === false ) return;
  569. handleMouseMoveRotate( event );
  570. break;
  571. case STATE.DOLLY:
  572. if ( scope.enableZoom === false ) return;
  573. handleMouseMoveDolly( event );
  574. break;
  575. case STATE.PAN:
  576. if ( scope.enablePan === false ) return;
  577. handleMouseMovePan( event );
  578. break;
  579. }
  580. }
  581. function onMouseUp( event ) {
  582. handleMouseUp( event );
  583. scope.dispatchEvent( _endEvent );
  584. state = STATE.NONE;
  585. }
  586. function onMouseWheel( event ) {
  587. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  588. event.preventDefault();
  589. scope.dispatchEvent( _startEvent );
  590. handleMouseWheel( event );
  591. scope.dispatchEvent( _endEvent );
  592. }
  593. function onKeyDown( event ) {
  594. if ( scope.enabled === false || scope.enablePan === false ) return;
  595. handleKeyDown( event );
  596. }
  597. function onTouchStart( event ) {
  598. trackPointer( event );
  599. switch ( pointers.length ) {
  600. case 1:
  601. switch ( scope.touches.ONE ) {
  602. case TOUCH.ROTATE:
  603. if ( scope.enableRotate === false ) return;
  604. handleTouchStartRotate();
  605. state = STATE.TOUCH_ROTATE;
  606. break;
  607. case TOUCH.PAN:
  608. if ( scope.enablePan === false ) return;
  609. handleTouchStartPan();
  610. state = STATE.TOUCH_PAN;
  611. break;
  612. default:
  613. state = STATE.NONE;
  614. }
  615. break;
  616. case 2:
  617. switch ( scope.touches.TWO ) {
  618. case TOUCH.DOLLY_PAN:
  619. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  620. handleTouchStartDollyPan();
  621. state = STATE.TOUCH_DOLLY_PAN;
  622. break;
  623. case TOUCH.DOLLY_ROTATE:
  624. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  625. handleTouchStartDollyRotate();
  626. state = STATE.TOUCH_DOLLY_ROTATE;
  627. break;
  628. default:
  629. state = STATE.NONE;
  630. }
  631. break;
  632. default:
  633. state = STATE.NONE;
  634. }
  635. if ( state !== STATE.NONE ) {
  636. scope.dispatchEvent( _startEvent );
  637. }
  638. }
  639. function onTouchMove( event ) {
  640. trackPointer( event );
  641. switch ( state ) {
  642. case STATE.TOUCH_ROTATE:
  643. if ( scope.enableRotate === false ) return;
  644. handleTouchMoveRotate( event );
  645. scope.update();
  646. break;
  647. case STATE.TOUCH_PAN:
  648. if ( scope.enablePan === false ) return;
  649. handleTouchMovePan( event );
  650. scope.update();
  651. break;
  652. case STATE.TOUCH_DOLLY_PAN:
  653. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  654. handleTouchMoveDollyPan( event );
  655. scope.update();
  656. break;
  657. case STATE.TOUCH_DOLLY_ROTATE:
  658. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  659. handleTouchMoveDollyRotate( event );
  660. scope.update();
  661. break;
  662. default:
  663. state = STATE.NONE;
  664. }
  665. }
  666. function onTouchEnd( event ) {
  667. handleTouchEnd( event );
  668. scope.dispatchEvent( _endEvent );
  669. state = STATE.NONE;
  670. }
  671. function onContextMenu( event ) {
  672. if ( scope.enabled === false ) return;
  673. event.preventDefault();
  674. }
  675. function addPointer( event ) {
  676. pointers.push( event );
  677. }
  678. function removePointer( event ) {
  679. delete pointerPositions[ event.pointerId ];
  680. for ( let i = 0; i < pointers.length; i ++ ) {
  681. if ( pointers[ i ].pointerId == event.pointerId ) {
  682. pointers.splice( i, 1 );
  683. return;
  684. }
  685. }
  686. }
  687. function trackPointer( event ) {
  688. let position = pointerPositions[ event.pointerId ];
  689. if ( position === undefined ) {
  690. position = new Vector2();
  691. pointerPositions[ event.pointerId ] = position;
  692. }
  693. position.set( event.pageX, event.pageY );
  694. }
  695. function getSecondPointerPosition( event ) {
  696. const pointer = ( event.pointerId === pointers[ 0 ].pointerId ) ? pointers[ 1 ] : pointers[ 0 ];
  697. return pointerPositions[ pointer.pointerId ];
  698. }
  699. //
  700. scope.domElement.addEventListener( 'contextmenu', onContextMenu );
  701. scope.domElement.addEventListener( 'pointerdown', onPointerDown );
  702. scope.domElement.addEventListener( 'pointercancel', onPointerCancel );
  703. scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
  704. // force an update at start
  705. this.update();
  706. }
  707. }
  708. // This set of controls performs orbiting, dollying (zooming), and panning.
  709. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  710. // This is very similar to OrbitControls, another set of touch behavior
  711. //
  712. // Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
  713. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  714. // Pan - left mouse, or arrow keys / touch: one-finger move
  715. class MapControls extends OrbitControls {
  716. constructor( object, domElement ) {
  717. super( object, domElement );
  718. this.screenSpacePanning = false; // pan orthogonal to world-space direction camera.up
  719. this.mouseButtons.LEFT = MOUSE.PAN;
  720. this.mouseButtons.RIGHT = MOUSE.ROTATE;
  721. this.touches.ONE = TOUCH.PAN;
  722. this.touches.TWO = TOUCH.DOLLY_ROTATE;
  723. }
  724. }
  725. export { OrbitControls, MapControls };