OrbitControls.js 23 KB

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