OrbitControls.js 25 KB

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