OrbitControls.js 25 KB

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