OrbitControls.js 24 KB

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