OrbitControls.js 22 KB

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