OrbitControls.js 24 KB

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