OrbitControls.js 26 KB

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