OrbitControls.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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.ownerDocument.removeEventListener( 'pointermove', onPointerMove );
  191. scope.domElement.ownerDocument.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.ownerDocument.addEventListener( 'pointermove', onPointerMove );
  477. scope.domElement.ownerDocument.addEventListener( 'pointerup', onPointerUp );
  478. }
  479. //
  480. addPointer( event );
  481. if ( event.pointerType === 'touch' ) {
  482. onTouchStart( event );
  483. } else {
  484. onMouseDown( event );
  485. }
  486. }
  487. function onPointerMove( event ) {
  488. if ( scope.enabled === false ) return;
  489. if ( event.pointerType === 'touch' ) {
  490. onTouchMove( event );
  491. } else {
  492. onMouseMove( event );
  493. }
  494. }
  495. function onPointerUp( event ) {
  496. if ( scope.enabled === false ) return;
  497. if ( event.pointerType === 'touch' ) {
  498. onTouchEnd();
  499. } else {
  500. onMouseUp( event );
  501. }
  502. removePointer( event );
  503. //
  504. if ( pointers.length === 0 ) {
  505. scope.domElement.ownerDocument.removeEventListener( 'pointermove', onPointerMove );
  506. scope.domElement.ownerDocument.removeEventListener( 'pointerup', onPointerUp );
  507. }
  508. }
  509. function onPointerCancel( event ) {
  510. removePointer( event );
  511. }
  512. function onMouseDown( event ) {
  513. let mouseAction;
  514. switch ( event.button ) {
  515. case 0:
  516. mouseAction = scope.mouseButtons.LEFT;
  517. break;
  518. case 1:
  519. mouseAction = scope.mouseButtons.MIDDLE;
  520. break;
  521. case 2:
  522. mouseAction = scope.mouseButtons.RIGHT;
  523. break;
  524. default:
  525. mouseAction = - 1;
  526. }
  527. switch ( mouseAction ) {
  528. case MOUSE.DOLLY:
  529. if ( scope.enableZoom === false ) return;
  530. handleMouseDownDolly( event );
  531. state = STATE.DOLLY;
  532. break;
  533. case MOUSE.ROTATE:
  534. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  535. if ( scope.enablePan === false ) return;
  536. handleMouseDownPan( event );
  537. state = STATE.PAN;
  538. } else {
  539. if ( scope.enableRotate === false ) return;
  540. handleMouseDownRotate( event );
  541. state = STATE.ROTATE;
  542. }
  543. break;
  544. case MOUSE.PAN:
  545. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  546. if ( scope.enableRotate === false ) return;
  547. handleMouseDownRotate( event );
  548. state = STATE.ROTATE;
  549. } else {
  550. if ( scope.enablePan === false ) return;
  551. handleMouseDownPan( event );
  552. state = STATE.PAN;
  553. }
  554. break;
  555. default:
  556. state = STATE.NONE;
  557. }
  558. if ( state !== STATE.NONE ) {
  559. scope.dispatchEvent( _startEvent );
  560. }
  561. }
  562. function onMouseMove( event ) {
  563. if ( scope.enabled === false ) return;
  564. switch ( state ) {
  565. case STATE.ROTATE:
  566. if ( scope.enableRotate === false ) return;
  567. handleMouseMoveRotate( event );
  568. break;
  569. case STATE.DOLLY:
  570. if ( scope.enableZoom === false ) return;
  571. handleMouseMoveDolly( event );
  572. break;
  573. case STATE.PAN:
  574. if ( scope.enablePan === false ) return;
  575. handleMouseMovePan( event );
  576. break;
  577. }
  578. }
  579. function onMouseUp( event ) {
  580. handleMouseUp( event );
  581. scope.dispatchEvent( _endEvent );
  582. state = STATE.NONE;
  583. }
  584. function onMouseWheel( event ) {
  585. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  586. event.preventDefault();
  587. scope.dispatchEvent( _startEvent );
  588. handleMouseWheel( event );
  589. scope.dispatchEvent( _endEvent );
  590. }
  591. function onKeyDown( event ) {
  592. if ( scope.enabled === false || scope.enablePan === false ) return;
  593. handleKeyDown( event );
  594. }
  595. function onTouchStart( event ) {
  596. trackPointer( event );
  597. switch ( pointers.length ) {
  598. case 1:
  599. switch ( scope.touches.ONE ) {
  600. case TOUCH.ROTATE:
  601. if ( scope.enableRotate === false ) return;
  602. handleTouchStartRotate();
  603. state = STATE.TOUCH_ROTATE;
  604. break;
  605. case TOUCH.PAN:
  606. if ( scope.enablePan === false ) return;
  607. handleTouchStartPan();
  608. state = STATE.TOUCH_PAN;
  609. break;
  610. default:
  611. state = STATE.NONE;
  612. }
  613. break;
  614. case 2:
  615. switch ( scope.touches.TWO ) {
  616. case TOUCH.DOLLY_PAN:
  617. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  618. handleTouchStartDollyPan();
  619. state = STATE.TOUCH_DOLLY_PAN;
  620. break;
  621. case TOUCH.DOLLY_ROTATE:
  622. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  623. handleTouchStartDollyRotate();
  624. state = STATE.TOUCH_DOLLY_ROTATE;
  625. break;
  626. default:
  627. state = STATE.NONE;
  628. }
  629. break;
  630. default:
  631. state = STATE.NONE;
  632. }
  633. if ( state !== STATE.NONE ) {
  634. scope.dispatchEvent( _startEvent );
  635. }
  636. }
  637. function onTouchMove( event ) {
  638. trackPointer( event );
  639. switch ( state ) {
  640. case STATE.TOUCH_ROTATE:
  641. if ( scope.enableRotate === false ) return;
  642. handleTouchMoveRotate( event );
  643. scope.update();
  644. break;
  645. case STATE.TOUCH_PAN:
  646. if ( scope.enablePan === false ) return;
  647. handleTouchMovePan( event );
  648. scope.update();
  649. break;
  650. case STATE.TOUCH_DOLLY_PAN:
  651. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  652. handleTouchMoveDollyPan( event );
  653. scope.update();
  654. break;
  655. case STATE.TOUCH_DOLLY_ROTATE:
  656. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  657. handleTouchMoveDollyRotate( event );
  658. scope.update();
  659. break;
  660. default:
  661. state = STATE.NONE;
  662. }
  663. }
  664. function onTouchEnd( event ) {
  665. handleTouchEnd( event );
  666. scope.dispatchEvent( _endEvent );
  667. state = STATE.NONE;
  668. }
  669. function onContextMenu( event ) {
  670. if ( scope.enabled === false ) return;
  671. event.preventDefault();
  672. }
  673. function addPointer( event ) {
  674. pointers.push( event );
  675. }
  676. function removePointer( event ) {
  677. delete pointerPositions[ event.pointerId ];
  678. for ( let i = 0; i < pointers.length; i ++ ) {
  679. if ( pointers[ i ].pointerId == event.pointerId ) {
  680. pointers.splice( i, 1 );
  681. return;
  682. }
  683. }
  684. }
  685. function trackPointer( event ) {
  686. let position = pointerPositions[ event.pointerId ];
  687. if ( position === undefined ) {
  688. position = new Vector2();
  689. pointerPositions[ event.pointerId ] = position;
  690. }
  691. position.set( event.pageX, event.pageY );
  692. }
  693. function getSecondPointerPosition( event ) {
  694. const pointer = ( event.pointerId === pointers[ 0 ].pointerId ) ? pointers[ 1 ] : pointers[ 0 ];
  695. return pointerPositions[ pointer.pointerId ];
  696. }
  697. //
  698. scope.domElement.addEventListener( 'contextmenu', onContextMenu );
  699. scope.domElement.addEventListener( 'pointerdown', onPointerDown );
  700. scope.domElement.addEventListener( 'pointercancel', onPointerCancel );
  701. scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
  702. // force an update at start
  703. this.update();
  704. }
  705. }
  706. // This set of controls performs orbiting, dollying (zooming), and panning.
  707. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  708. // This is very similar to OrbitControls, another set of touch behavior
  709. //
  710. // Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
  711. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  712. // Pan - left mouse, or arrow keys / touch: one-finger move
  713. class MapControls extends OrbitControls {
  714. constructor( object, domElement ) {
  715. super( object, domElement );
  716. this.screenSpacePanning = false; // pan orthogonal to world-space direction camera.up
  717. this.mouseButtons.LEFT = MOUSE.PAN;
  718. this.mouseButtons.RIGHT = MOUSE.ROTATE;
  719. this.touches.ONE = TOUCH.PAN;
  720. this.touches.TWO = TOUCH.DOLLY_ROTATE;
  721. }
  722. }
  723. export { OrbitControls, MapControls };