OrbitControls.js 27 KB

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