OrbitControls.js 24 KB

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