The website of the KaosCube
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1045 lines
26 KiB

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