2
0

OrbitControls.js 24 KB

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