OrbitControls.js 28 KB

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