OrbitControls.js 22 KB

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