orbit_controls.js 22 KB

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