OrbitControls.js 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. */
  8. // This set of controls performs orbiting, dollying (zooming), and panning.
  9. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  10. //
  11. // Orbit - left mouse / touch: one finger move
  12. // Zoom - middle mouse, or mousewheel / touch: two finger spread or squish
  13. // Pan - right mouse, or arrow keys / touch: three finter swipe
  14. THREE.OrbitControls = function ( object, domElement ) {
  15. this.object = object;
  16. this.domElement = ( domElement !== undefined ) ? domElement : document;
  17. // Set to false to disable this control
  18. this.enabled = true;
  19. // "target" sets the location of focus, where the object orbits around
  20. this.target = new THREE.Vector3();
  21. // How far you can dolly in and out ( PerspectiveCamera only )
  22. this.minDistance = 0;
  23. this.maxDistance = Infinity;
  24. // How far you can zoom in and out ( OrthographicCamera only )
  25. this.minZoom = 0;
  26. this.maxZoom = Infinity;
  27. // How far you can orbit vertically, upper and lower limits.
  28. // Range is 0 to Math.PI radians.
  29. this.minPolarAngle = 0; // radians
  30. this.maxPolarAngle = Math.PI; // radians
  31. // How far you can orbit horizontally, upper and lower limits.
  32. // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
  33. this.minAzimuthAngle = - Infinity; // radians
  34. this.maxAzimuthAngle = Infinity; // radians
  35. // Set to true to enable damping (inertia)
  36. // If damping is enabled, you must call controls.update() in your animation loop
  37. this.enableDamping = false;
  38. this.dampingFactor = 0.25;
  39. // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
  40. // Set to false to disable zooming
  41. this.enableZoom = true;
  42. this.zoomSpeed = 1.0;
  43. // Set to false to disable rotating
  44. this.enableRotate = true;
  45. this.rotateSpeed = 1.0;
  46. // Set to false to disable panning
  47. this.enablePan = true;
  48. this.keyPanSpeed = 7.0; // pixels moved per arrow key push
  49. // Set to true to automatically rotate around the target
  50. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  51. this.autoRotate = false;
  52. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  53. // Set to false to disable use of the keys
  54. this.enableKeys = true;
  55. // The four arrow keys
  56. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  57. // Mouse buttons
  58. this.mouseButtons = { ORBIT: THREE.MOUSE.LEFT, ZOOM: THREE.MOUSE.MIDDLE, PAN: THREE.MOUSE.RIGHT };
  59. // for reset
  60. this.target0 = this.target.clone();
  61. this.position0 = this.object.position.clone();
  62. this.zoom0 = this.object.zoom;
  63. //
  64. // public methods
  65. //
  66. this.getPolarAngle = function () {
  67. return phi;
  68. };
  69. this.getAzimuthalAngle = function () {
  70. return theta;
  71. };
  72. this.reset = function () {
  73. scope.target.copy( scope.target0 );
  74. scope.object.position.copy( scope.position0 );
  75. scope.object.zoom = scope.zoom0;
  76. scope.object.updateProjectionMatrix();
  77. scope.dispatchEvent( changeEvent );
  78. scope.update();
  79. state = STATE.NONE;
  80. };
  81. // this method is exposed, but perhaps it would be better if we can make it private...
  82. this.update = function() {
  83. var offset = new THREE.Vector3();
  84. // so camera.up is the orbit axis
  85. var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
  86. var quatInverse = quat.clone().inverse();
  87. var lastPosition = new THREE.Vector3();
  88. var lastQuaternion = new THREE.Quaternion();
  89. return function () {
  90. var position = scope.object.position;
  91. offset.copy( position ).sub( scope.target );
  92. // rotate offset to "y-axis-is-up" space
  93. offset.applyQuaternion( quat );
  94. // angle from z-axis around y-axis
  95. spherical.fromVector3( offset );
  96. if ( scope.autoRotate && state === STATE.NONE ) {
  97. rotateLeft( getAutoRotationAngle() );
  98. }
  99. spherical.theta += sphericalDelta.theta;
  100. spherical.phi += sphericalDelta.phi;
  101. // restrict theta to be between desired limits
  102. spherical.theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, spherical.theta ) );
  103. // restrict phi to be between desired limits
  104. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  105. spherical.makeSafe();
  106. spherical.radius *= scale;
  107. // restrict radius to be between desired limits
  108. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  109. // move target to panned location
  110. scope.target.add( panOffset );
  111. offset = spherical.toVector3( offset );
  112. // rotate offset back to "camera-up-vector-is-up" space
  113. offset.applyQuaternion( quatInverse );
  114. position.copy( scope.target ).add( offset );
  115. scope.object.lookAt( scope.target );
  116. if ( scope.enableDamping === true ) {
  117. spherical.theta *= ( 1 - scope.dampingFactor );
  118. spherical.phi *= ( 1 - scope.dampingFactor );
  119. } else {
  120. sphericalDelta.set( 0, 0, 0 );
  121. }
  122. scale = 1;
  123. panOffset.set( 0, 0, 0 );
  124. // update condition is:
  125. // min(camera displacement, camera rotation in radians)^2 > EPS
  126. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  127. if ( zoomChanged ||
  128. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  129. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  130. scope.dispatchEvent( changeEvent );
  131. lastPosition.copy( scope.object.position );
  132. lastQuaternion.copy( scope.object.quaternion );
  133. zoomChanged = false;
  134. return true;
  135. }
  136. return false;
  137. };
  138. }();
  139. this.dispose = function() {
  140. scope.domElement.removeEventListener( 'contextmenu', onContextMenu, false );
  141. scope.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  142. scope.domElement.removeEventListener( 'mousewheel', onMouseWheel, false );
  143. scope.domElement.removeEventListener( 'MozMousePixelScroll', onMouseWheel, false ); // firefox
  144. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  145. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  146. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  147. document.removeEventListener( 'mousemove', onMouseMove, false );
  148. document.removeEventListener( 'mouseup', onMouseUp, false );
  149. document.removeEventListener( 'mouseout', onMouseUp, false );
  150. window.removeEventListener( 'keydown', onKeyDown, false );
  151. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  152. };
  153. //
  154. // internals
  155. //
  156. var scope = this;
  157. var changeEvent = { type: 'change' };
  158. var startEvent = { type: 'start' };
  159. var endEvent = { type: 'end' };
  160. var STATE = { NONE : - 1, ROTATE : 0, DOLLY : 1, PAN : 2, TOUCH_ROTATE : 3, TOUCH_DOLLY : 4, TOUCH_PAN : 5 };
  161. var state = STATE.NONE;
  162. var EPS = 0.000001;
  163. // current position in spherical coordinates
  164. var spherical = new THREE.Spherical();
  165. var sphericalDelta = new THREE.Spherical();
  166. var scale = 1;
  167. var panOffset = new THREE.Vector3();
  168. var zoomChanged = false;
  169. var rotateStart = new THREE.Vector2();
  170. var rotateEnd = new THREE.Vector2();
  171. var rotateDelta = new THREE.Vector2();
  172. var panStart = new THREE.Vector2();
  173. var panEnd = new THREE.Vector2();
  174. var panDelta = new THREE.Vector2();
  175. var dollyStart = new THREE.Vector2();
  176. var dollyEnd = new THREE.Vector2();
  177. var dollyDelta = new THREE.Vector2();
  178. function getAutoRotationAngle() {
  179. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  180. }
  181. function getZoomScale() {
  182. return Math.pow( 0.95, scope.zoomSpeed );
  183. }
  184. function rotateLeft( angle ) {
  185. sphericalDelta.theta -= angle;
  186. }
  187. function rotateUp( angle ) {
  188. sphericalDelta.phi -= angle;
  189. }
  190. var panLeft = function() {
  191. var v = new THREE.Vector3();
  192. return function panLeft( distance, objectMatrix ) {
  193. var te = objectMatrix.elements;
  194. // get X column of objectMatrix
  195. v.set( te[ 0 ], te[ 1 ], te[ 2 ] );
  196. v.multiplyScalar( - distance );
  197. panOffset.add( v );
  198. };
  199. }();
  200. var panUp = function() {
  201. var v = new THREE.Vector3();
  202. return function panUp( distance, objectMatrix ) {
  203. var te = objectMatrix.elements;
  204. // get Y column of objectMatrix
  205. v.set( te[ 4 ], te[ 5 ], te[ 6 ] );
  206. v.multiplyScalar( distance );
  207. panOffset.add( v );
  208. };
  209. }();
  210. // deltaX and deltaY are in pixels; right and down are positive
  211. var pan = function() {
  212. var offset = new THREE.Vector3();
  213. return function( deltaX, deltaY ) {
  214. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  215. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  216. // perspective
  217. var position = scope.object.position;
  218. offset.copy( position ).sub( scope.target );
  219. var targetDistance = offset.length();
  220. // half of the fov is center to top of screen
  221. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  222. // we actually don't use screenWidth, since perspective camera is fixed to screen height
  223. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  224. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  225. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  226. // orthographic
  227. panLeft( deltaX * ( scope.object.right - scope.object.left ) / element.clientWidth, scope.object.matrix );
  228. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / element.clientHeight, scope.object.matrix );
  229. } else {
  230. // camera neither orthographic nor perspective
  231. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  232. scope.enablePan = false;
  233. }
  234. };
  235. }();
  236. function dollyIn( dollyScale ) {
  237. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  238. scale /= dollyScale;
  239. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  240. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  241. scope.object.updateProjectionMatrix();
  242. zoomChanged = true;
  243. } else {
  244. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  245. scope.enableZoom = false;
  246. }
  247. }
  248. function dollyOut( dollyScale ) {
  249. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  250. scale *= dollyScale;
  251. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  252. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  253. scope.object.updateProjectionMatrix();
  254. zoomChanged = true;
  255. } else {
  256. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  257. scope.enableZoom = false;
  258. }
  259. }
  260. //
  261. // event callbacks - update the object state
  262. //
  263. function handleMouseDownRotate( event ) {
  264. //console.log( 'handleMouseDownRotate' );
  265. rotateStart.set( event.clientX, event.clientY );
  266. }
  267. function handleMouseDownDolly( event ) {
  268. //console.log( 'handleMouseDownDolly' );
  269. dollyStart.set( event.clientX, event.clientY );
  270. }
  271. function handleMouseDownPan( event ) {
  272. //console.log( 'handleMouseDownPan' );
  273. panStart.set( event.clientX, event.clientY );
  274. }
  275. function handleMouseMoveRotate( event ) {
  276. //console.log( 'handleMouseMoveRotate' );
  277. rotateEnd.set( event.clientX, event.clientY );
  278. rotateDelta.subVectors( rotateEnd, rotateStart );
  279. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  280. // rotating across whole screen goes 360 degrees around
  281. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  282. // rotating up and down along whole screen attempts to go 360, but limited to 180
  283. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  284. rotateStart.copy( rotateEnd );
  285. scope.update();
  286. }
  287. function handleMouseMoveDolly( event ) {
  288. //console.log( 'handleMouseMoveDolly' );
  289. dollyEnd.set( event.clientX, event.clientY );
  290. dollyDelta.subVectors( dollyEnd, dollyStart );
  291. if ( dollyDelta.y > 0 ) {
  292. dollyIn( getZoomScale() );
  293. } else if ( dollyDelta.y < 0 ) {
  294. dollyOut( getZoomScale() );
  295. }
  296. dollyStart.copy( dollyEnd );
  297. scope.update();
  298. }
  299. function handleMouseMovePan( event ) {
  300. //console.log( 'handleMouseMovePan' );
  301. panEnd.set( event.clientX, event.clientY );
  302. panDelta.subVectors( panEnd, panStart );
  303. pan( panDelta.x, panDelta.y );
  304. panStart.copy( panEnd );
  305. scope.update();
  306. }
  307. function handleMouseUp( event ) {
  308. //console.log( 'handleMouseUp' );
  309. }
  310. function handleMouseWheel( event ) {
  311. //console.log( 'handleMouseWheel' );
  312. var delta = 0;
  313. if ( event.wheelDelta !== undefined ) {
  314. // WebKit / Opera / Explorer 9
  315. delta = event.wheelDelta;
  316. } else if ( event.detail !== undefined ) {
  317. // Firefox
  318. delta = - event.detail;
  319. }
  320. if ( delta > 0 ) {
  321. dollyOut( getZoomScale() );
  322. } else if ( delta < 0 ) {
  323. dollyIn( getZoomScale() );
  324. }
  325. scope.update();
  326. }
  327. function handleKeyDown( event ) {
  328. //console.log( 'handleKeyDown' );
  329. switch ( event.keyCode ) {
  330. case scope.keys.UP:
  331. pan( 0, scope.keyPanSpeed );
  332. scope.update();
  333. break;
  334. case scope.keys.BOTTOM:
  335. pan( 0, - scope.keyPanSpeed );
  336. scope.update();
  337. break;
  338. case scope.keys.LEFT:
  339. pan( scope.keyPanSpeed, 0 );
  340. scope.update();
  341. break;
  342. case scope.keys.RIGHT:
  343. pan( - scope.keyPanSpeed, 0 );
  344. scope.update();
  345. break;
  346. }
  347. }
  348. function handleTouchStartRotate( event ) {
  349. //console.log( 'handleTouchStartRotate' );
  350. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  351. }
  352. function handleTouchStartDolly( event ) {
  353. //console.log( 'handleTouchStartDolly' );
  354. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  355. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  356. var distance = Math.sqrt( dx * dx + dy * dy );
  357. dollyStart.set( 0, distance );
  358. }
  359. function handleTouchStartPan( event ) {
  360. //console.log( 'handleTouchStartPan' );
  361. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  362. }
  363. function handleTouchMoveRotate( event ) {
  364. //console.log( 'handleTouchMoveRotate' );
  365. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  366. rotateDelta.subVectors( rotateEnd, rotateStart );
  367. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  368. // rotating across whole screen goes 360 degrees around
  369. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  370. // rotating up and down along whole screen attempts to go 360, but limited to 180
  371. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  372. rotateStart.copy( rotateEnd );
  373. scope.update();
  374. }
  375. function handleTouchMoveDolly( event ) {
  376. //console.log( 'handleTouchMoveDolly' );
  377. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  378. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  379. var distance = Math.sqrt( dx * dx + dy * dy );
  380. dollyEnd.set( 0, distance );
  381. dollyDelta.subVectors( dollyEnd, dollyStart );
  382. if ( dollyDelta.y > 0 ) {
  383. dollyOut( getZoomScale() );
  384. } else if ( dollyDelta.y < 0 ) {
  385. dollyIn( getZoomScale() );
  386. }
  387. dollyStart.copy( dollyEnd );
  388. scope.update();
  389. }
  390. function handleTouchMovePan( event ) {
  391. //console.log( 'handleTouchMovePan' );
  392. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  393. panDelta.subVectors( panEnd, panStart );
  394. pan( panDelta.x, panDelta.y );
  395. panStart.copy( panEnd );
  396. scope.update();
  397. }
  398. function handleTouchEnd( event ) {
  399. //console.log( 'handleTouchEnd' );
  400. }
  401. //
  402. // event handlers - FSM: listen for events and reset state
  403. //
  404. function onMouseDown( event ) {
  405. if ( scope.enabled === false ) return;
  406. event.preventDefault();
  407. if ( event.button === scope.mouseButtons.ORBIT ) {
  408. if ( scope.enableRotate === false ) return;
  409. handleMouseDownRotate( event );
  410. state = STATE.ROTATE;
  411. } else if ( event.button === scope.mouseButtons.ZOOM ) {
  412. if ( scope.enableZoom === false ) return;
  413. handleMouseDownDolly( event );
  414. state = STATE.DOLLY;
  415. } else if ( event.button === scope.mouseButtons.PAN ) {
  416. if ( scope.enablePan === false ) return;
  417. handleMouseDownPan( event );
  418. state = STATE.PAN;
  419. }
  420. if ( state !== STATE.NONE ) {
  421. document.addEventListener( 'mousemove', onMouseMove, false );
  422. document.addEventListener( 'mouseup', onMouseUp, false );
  423. document.addEventListener( 'mouseout', onMouseUp, false );
  424. scope.dispatchEvent( startEvent );
  425. }
  426. }
  427. function onMouseMove( event ) {
  428. if ( scope.enabled === false ) return;
  429. event.preventDefault();
  430. if ( state === STATE.ROTATE ) {
  431. if ( scope.enableRotate === false ) return;
  432. handleMouseMoveRotate( event );
  433. } else if ( state === STATE.DOLLY ) {
  434. if ( scope.enableZoom === false ) return;
  435. handleMouseMoveDolly( event );
  436. } else if ( state === STATE.PAN ) {
  437. if ( scope.enablePan === false ) return;
  438. handleMouseMovePan( event );
  439. }
  440. }
  441. function onMouseUp( event ) {
  442. if ( scope.enabled === false ) return;
  443. handleMouseUp( event );
  444. document.removeEventListener( 'mousemove', onMouseMove, false );
  445. document.removeEventListener( 'mouseup', onMouseUp, false );
  446. document.removeEventListener( 'mouseout', onMouseUp, false );
  447. scope.dispatchEvent( endEvent );
  448. state = STATE.NONE;
  449. }
  450. function onMouseWheel( event ) {
  451. if ( scope.enabled === false || scope.enableZoom === false || state !== STATE.NONE ) return;
  452. event.preventDefault();
  453. event.stopPropagation();
  454. handleMouseWheel( event );
  455. scope.dispatchEvent( startEvent ); // not sure why these are here...
  456. scope.dispatchEvent( endEvent );
  457. }
  458. function onKeyDown( event ) {
  459. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  460. handleKeyDown( event );
  461. }
  462. function onTouchStart( event ) {
  463. if ( scope.enabled === false ) return;
  464. switch ( event.touches.length ) {
  465. case 1: // one-fingered touch: rotate
  466. if ( scope.enableRotate === false ) return;
  467. handleTouchStartRotate( event );
  468. state = STATE.TOUCH_ROTATE;
  469. break;
  470. case 2: // two-fingered touch: dolly
  471. if ( scope.enableZoom === false ) return;
  472. handleTouchStartDolly( event );
  473. state = STATE.TOUCH_DOLLY;
  474. break;
  475. case 3: // three-fingered touch: pan
  476. if ( scope.enablePan === false ) return;
  477. handleTouchStartPan( event );
  478. state = STATE.TOUCH_PAN;
  479. break;
  480. default:
  481. state = STATE.NONE;
  482. }
  483. if ( state !== STATE.NONE ) {
  484. scope.dispatchEvent( startEvent );
  485. }
  486. }
  487. function onTouchMove( event ) {
  488. if ( scope.enabled === false ) return;
  489. event.preventDefault();
  490. event.stopPropagation();
  491. switch ( event.touches.length ) {
  492. case 1: // one-fingered touch: rotate
  493. if ( scope.enableRotate === false ) return;
  494. if ( state !== STATE.TOUCH_ROTATE ) return; // is this needed?...
  495. handleTouchMoveRotate( event );
  496. break;
  497. case 2: // two-fingered touch: dolly
  498. if ( scope.enableZoom === false ) return;
  499. if ( state !== STATE.TOUCH_DOLLY ) return; // is this needed?...
  500. handleTouchMoveDolly( event );
  501. break;
  502. case 3: // three-fingered touch: pan
  503. if ( scope.enablePan === false ) return;
  504. if ( state !== STATE.TOUCH_PAN ) return; // is this needed?...
  505. handleTouchMovePan( event );
  506. break;
  507. default:
  508. state = STATE.NONE;
  509. }
  510. }
  511. function onTouchEnd( event ) {
  512. if ( scope.enabled === false ) return;
  513. handleTouchEnd( event );
  514. scope.dispatchEvent( endEvent );
  515. state = STATE.NONE;
  516. }
  517. function onContextMenu( event ) {
  518. event.preventDefault();
  519. }
  520. //
  521. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  522. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  523. scope.domElement.addEventListener( 'mousewheel', onMouseWheel, false );
  524. scope.domElement.addEventListener( 'MozMousePixelScroll', onMouseWheel, false ); // firefox
  525. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  526. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  527. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  528. window.addEventListener( 'keydown', onKeyDown, false );
  529. // force an update at start
  530. this.update();
  531. };
  532. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  533. THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;
  534. Object.defineProperties( THREE.OrbitControls.prototype, {
  535. center: {
  536. get: function () {
  537. console.warn( 'THREE.OrbitControls: .center has been renamed to .target' );
  538. return this.target;
  539. }
  540. },
  541. // backward compatibility
  542. noZoom: {
  543. get: function () {
  544. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  545. return ! this.enableZoom;
  546. },
  547. set: function ( value ) {
  548. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  549. this.enableZoom = ! value;
  550. }
  551. },
  552. noRotate: {
  553. get: function () {
  554. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  555. return ! this.enableRotate;
  556. },
  557. set: function ( value ) {
  558. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  559. this.enableRotate = ! value;
  560. }
  561. },
  562. noPan: {
  563. get: function () {
  564. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  565. return ! this.enablePan;
  566. },
  567. set: function ( value ) {
  568. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  569. this.enablePan = ! value;
  570. }
  571. },
  572. noKeys: {
  573. get: function () {
  574. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  575. return ! this.enableKeys;
  576. },
  577. set: function ( value ) {
  578. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  579. this.enableKeys = ! value;
  580. }
  581. },
  582. staticMoving : {
  583. get: function () {
  584. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  585. return ! this.constraint.enableDamping;
  586. },
  587. set: function ( value ) {
  588. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  589. this.constraint.enableDamping = ! value;
  590. }
  591. },
  592. dynamicDampingFactor : {
  593. get: function () {
  594. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  595. return this.constraint.dampingFactor;
  596. },
  597. set: function ( value ) {
  598. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  599. this.constraint.dampingFactor = value;
  600. }
  601. }
  602. } );