OrbitControls.js 25 KB

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