OrbitControls.js 25 KB

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