OrbitControls.js 27 KB

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