OrbitControls.js 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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. pan( 0, scope.keyPanSpeed );
  366. needsUpdate = true;
  367. break;
  368. case scope.keys.BOTTOM:
  369. pan( 0, - scope.keyPanSpeed );
  370. needsUpdate = true;
  371. break;
  372. case scope.keys.LEFT:
  373. pan( scope.keyPanSpeed, 0 );
  374. needsUpdate = true;
  375. break;
  376. case scope.keys.RIGHT:
  377. pan( - scope.keyPanSpeed, 0 );
  378. needsUpdate = true;
  379. break;
  380. }
  381. if ( needsUpdate ) {
  382. // prevent the browser from scrolling on cursor keys
  383. event.preventDefault();
  384. scope.update();
  385. }
  386. }
  387. function handleTouchStartRotate() {
  388. if ( pointers.length === 1 ) {
  389. rotateStart.set( pointers[ 0 ].pageX, pointers[ 0 ].pageY );
  390. } else {
  391. const x = 0.5 * ( pointers[ 0 ].pageX + pointers[ 1 ].pageX );
  392. const y = 0.5 * ( pointers[ 0 ].pageY + pointers[ 1 ].pageY );
  393. rotateStart.set( x, y );
  394. }
  395. }
  396. function handleTouchStartPan() {
  397. if ( pointers.length === 1 ) {
  398. panStart.set( pointers[ 0 ].pageX, pointers[ 0 ].pageY );
  399. } else {
  400. const x = 0.5 * ( pointers[ 0 ].pageX + pointers[ 1 ].pageX );
  401. const y = 0.5 * ( pointers[ 0 ].pageY + pointers[ 1 ].pageY );
  402. panStart.set( x, y );
  403. }
  404. }
  405. function handleTouchStartDolly() {
  406. const dx = pointers[ 0 ].pageX - pointers[ 1 ].pageX;
  407. const dy = pointers[ 0 ].pageY - pointers[ 1 ].pageY;
  408. const distance = Math.sqrt( dx * dx + dy * dy );
  409. dollyStart.set( 0, distance );
  410. }
  411. function handleTouchStartDollyPan() {
  412. if ( scope.enableZoom ) handleTouchStartDolly();
  413. if ( scope.enablePan ) handleTouchStartPan();
  414. }
  415. function handleTouchStartDollyRotate() {
  416. if ( scope.enableZoom ) handleTouchStartDolly();
  417. if ( scope.enableRotate ) handleTouchStartRotate();
  418. }
  419. function handleTouchMoveRotate( event ) {
  420. if ( pointers.length == 1 ) {
  421. rotateEnd.set( event.pageX, event.pageY );
  422. } else {
  423. const position = getSecondPointerPosition( event );
  424. const x = 0.5 * ( event.pageX + position.x );
  425. const y = 0.5 * ( event.pageY + position.y );
  426. rotateEnd.set( x, y );
  427. }
  428. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  429. const element = scope.domElement;
  430. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  431. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  432. rotateStart.copy( rotateEnd );
  433. }
  434. function handleTouchMovePan( event ) {
  435. if ( pointers.length === 1 ) {
  436. panEnd.set( event.pageX, event.pageY );
  437. } else {
  438. const position = getSecondPointerPosition( event );
  439. const x = 0.5 * ( event.pageX + position.x );
  440. const y = 0.5 * ( event.pageY + position.y );
  441. panEnd.set( x, y );
  442. }
  443. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  444. pan( panDelta.x, panDelta.y );
  445. panStart.copy( panEnd );
  446. }
  447. function handleTouchMoveDolly( event ) {
  448. const position = getSecondPointerPosition( event );
  449. const dx = event.pageX - position.x;
  450. const dy = event.pageY - position.y;
  451. const distance = Math.sqrt( dx * dx + dy * dy );
  452. dollyEnd.set( 0, distance );
  453. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  454. dollyOut( dollyDelta.y );
  455. dollyStart.copy( dollyEnd );
  456. }
  457. function handleTouchMoveDollyPan( event ) {
  458. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  459. if ( scope.enablePan ) handleTouchMovePan( event );
  460. }
  461. function handleTouchMoveDollyRotate( event ) {
  462. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  463. if ( scope.enableRotate ) handleTouchMoveRotate( event );
  464. }
  465. //
  466. // event handlers - FSM: listen for events and reset state
  467. //
  468. function onPointerDown( event ) {
  469. if ( scope.enabled === false ) return;
  470. if ( pointers.length === 0 ) {
  471. scope.domElement.setPointerCapture( event.pointerId );
  472. scope.domElement.addEventListener( 'pointermove', onPointerMove );
  473. scope.domElement.addEventListener( 'pointerup', onPointerUp );
  474. }
  475. //
  476. addPointer( event );
  477. if ( event.pointerType === 'touch' ) {
  478. onTouchStart( event );
  479. } else {
  480. onMouseDown( event );
  481. }
  482. }
  483. function onPointerMove( event ) {
  484. if ( scope.enabled === false ) return;
  485. if ( event.pointerType === 'touch' ) {
  486. onTouchMove( event );
  487. } else {
  488. onMouseMove( event );
  489. }
  490. }
  491. function onPointerUp( event ) {
  492. removePointer( event );
  493. if ( pointers.length === 0 ) {
  494. scope.domElement.releasePointerCapture( event.pointerId );
  495. scope.domElement.removeEventListener( 'pointermove', onPointerMove );
  496. scope.domElement.removeEventListener( 'pointerup', onPointerUp );
  497. }
  498. scope.dispatchEvent( _endEvent );
  499. state = STATE.NONE;
  500. }
  501. function onPointerCancel( event ) {
  502. removePointer( event );
  503. }
  504. function onMouseDown( event ) {
  505. let mouseAction;
  506. switch ( event.button ) {
  507. case 0:
  508. mouseAction = scope.mouseButtons.LEFT;
  509. break;
  510. case 1:
  511. mouseAction = scope.mouseButtons.MIDDLE;
  512. break;
  513. case 2:
  514. mouseAction = scope.mouseButtons.RIGHT;
  515. break;
  516. default:
  517. mouseAction = - 1;
  518. }
  519. switch ( mouseAction ) {
  520. case MOUSE.DOLLY:
  521. if ( scope.enableZoom === false ) return;
  522. handleMouseDownDolly( event );
  523. state = STATE.DOLLY;
  524. break;
  525. case MOUSE.ROTATE:
  526. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  527. if ( scope.enablePan === false ) return;
  528. handleMouseDownPan( event );
  529. state = STATE.PAN;
  530. } else {
  531. if ( scope.enableRotate === false ) return;
  532. handleMouseDownRotate( event );
  533. state = STATE.ROTATE;
  534. }
  535. break;
  536. case MOUSE.PAN:
  537. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  538. if ( scope.enableRotate === false ) return;
  539. handleMouseDownRotate( event );
  540. state = STATE.ROTATE;
  541. } else {
  542. if ( scope.enablePan === false ) return;
  543. handleMouseDownPan( event );
  544. state = STATE.PAN;
  545. }
  546. break;
  547. default:
  548. state = STATE.NONE;
  549. }
  550. if ( state !== STATE.NONE ) {
  551. scope.dispatchEvent( _startEvent );
  552. }
  553. }
  554. function onMouseMove( event ) {
  555. switch ( state ) {
  556. case STATE.ROTATE:
  557. if ( scope.enableRotate === false ) return;
  558. handleMouseMoveRotate( event );
  559. break;
  560. case STATE.DOLLY:
  561. if ( scope.enableZoom === false ) return;
  562. handleMouseMoveDolly( event );
  563. break;
  564. case STATE.PAN:
  565. if ( scope.enablePan === false ) return;
  566. handleMouseMovePan( event );
  567. break;
  568. }
  569. }
  570. function onMouseWheel( event ) {
  571. if ( scope.enabled === false || scope.enableZoom === false || state !== STATE.NONE ) return;
  572. event.preventDefault();
  573. scope.dispatchEvent( _startEvent );
  574. handleMouseWheel( event );
  575. scope.dispatchEvent( _endEvent );
  576. }
  577. function onKeyDown( event ) {
  578. if ( scope.enabled === false || scope.enablePan === false ) return;
  579. handleKeyDown( event );
  580. }
  581. function onTouchStart( event ) {
  582. trackPointer( event );
  583. switch ( pointers.length ) {
  584. case 1:
  585. switch ( scope.touches.ONE ) {
  586. case TOUCH.ROTATE:
  587. if ( scope.enableRotate === false ) return;
  588. handleTouchStartRotate();
  589. state = STATE.TOUCH_ROTATE;
  590. break;
  591. case TOUCH.PAN:
  592. if ( scope.enablePan === false ) return;
  593. handleTouchStartPan();
  594. state = STATE.TOUCH_PAN;
  595. break;
  596. default:
  597. state = STATE.NONE;
  598. }
  599. break;
  600. case 2:
  601. switch ( scope.touches.TWO ) {
  602. case TOUCH.DOLLY_PAN:
  603. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  604. handleTouchStartDollyPan();
  605. state = STATE.TOUCH_DOLLY_PAN;
  606. break;
  607. case TOUCH.DOLLY_ROTATE:
  608. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  609. handleTouchStartDollyRotate();
  610. state = STATE.TOUCH_DOLLY_ROTATE;
  611. break;
  612. default:
  613. state = STATE.NONE;
  614. }
  615. break;
  616. default:
  617. state = STATE.NONE;
  618. }
  619. if ( state !== STATE.NONE ) {
  620. scope.dispatchEvent( _startEvent );
  621. }
  622. }
  623. function onTouchMove( event ) {
  624. trackPointer( event );
  625. switch ( state ) {
  626. case STATE.TOUCH_ROTATE:
  627. if ( scope.enableRotate === false ) return;
  628. handleTouchMoveRotate( event );
  629. scope.update();
  630. break;
  631. case STATE.TOUCH_PAN:
  632. if ( scope.enablePan === false ) return;
  633. handleTouchMovePan( event );
  634. scope.update();
  635. break;
  636. case STATE.TOUCH_DOLLY_PAN:
  637. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  638. handleTouchMoveDollyPan( event );
  639. scope.update();
  640. break;
  641. case STATE.TOUCH_DOLLY_ROTATE:
  642. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  643. handleTouchMoveDollyRotate( event );
  644. scope.update();
  645. break;
  646. default:
  647. state = STATE.NONE;
  648. }
  649. }
  650. function onContextMenu( event ) {
  651. if ( scope.enabled === false ) return;
  652. event.preventDefault();
  653. }
  654. function addPointer( event ) {
  655. pointers.push( event );
  656. }
  657. function removePointer( event ) {
  658. delete pointerPositions[ event.pointerId ];
  659. for ( let i = 0; i < pointers.length; i ++ ) {
  660. if ( pointers[ i ].pointerId == event.pointerId ) {
  661. pointers.splice( i, 1 );
  662. return;
  663. }
  664. }
  665. }
  666. function trackPointer( event ) {
  667. let position = pointerPositions[ event.pointerId ];
  668. if ( position === undefined ) {
  669. position = new Vector2();
  670. pointerPositions[ event.pointerId ] = position;
  671. }
  672. position.set( event.pageX, event.pageY );
  673. }
  674. function getSecondPointerPosition( event ) {
  675. const pointer = ( event.pointerId === pointers[ 0 ].pointerId ) ? pointers[ 1 ] : pointers[ 0 ];
  676. return pointerPositions[ pointer.pointerId ];
  677. }
  678. //
  679. scope.domElement.addEventListener( 'contextmenu', onContextMenu );
  680. scope.domElement.addEventListener( 'pointerdown', onPointerDown );
  681. scope.domElement.addEventListener( 'pointercancel', onPointerCancel );
  682. scope.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
  683. // force an update at start
  684. this.update();
  685. }
  686. }
  687. // This set of controls performs orbiting, dollying (zooming), and panning.
  688. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  689. // This is very similar to OrbitControls, another set of touch behavior
  690. //
  691. // Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
  692. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  693. // Pan - left mouse, or arrow keys / touch: one-finger move
  694. class MapControls extends OrbitControls {
  695. constructor( object, domElement ) {
  696. super( object, domElement );
  697. this.screenSpacePanning = false; // pan orthogonal to world-space direction camera.up
  698. this.mouseButtons.LEFT = MOUSE.PAN;
  699. this.mouseButtons.RIGHT = MOUSE.ROTATE;
  700. this.touches.ONE = TOUCH.PAN;
  701. this.touches.TWO = TOUCH.DOLLY_ROTATE;
  702. }
  703. }
  704. export { OrbitControls, MapControls };