OrbitControls.js 27 KB

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