2
0

OrbitControls.js 26 KB

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