OrbitControls.js 27 KB

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