OrbitControls.js 25 KB

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