OrbitControls.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. import {
  15. EventDispatcher,
  16. MOUSE,
  17. Quaternion,
  18. Spherical,
  19. Vector2,
  20. Vector3
  21. } from "../../../build/three.module.js";
  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. switch ( event.keyCode ) {
  335. case scope.keys.UP:
  336. pan( 0, scope.keyPanSpeed );
  337. scope.update();
  338. break;
  339. case scope.keys.BOTTOM:
  340. pan( 0, - scope.keyPanSpeed );
  341. scope.update();
  342. break;
  343. case scope.keys.LEFT:
  344. pan( scope.keyPanSpeed, 0 );
  345. scope.update();
  346. break;
  347. case scope.keys.RIGHT:
  348. pan( - scope.keyPanSpeed, 0 );
  349. scope.update();
  350. break;
  351. }
  352. }
  353. function handleTouchStartRotate( event ) {
  354. //console.log( 'handleTouchStartRotate' );
  355. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  356. }
  357. function handleTouchStartDollyPan( event ) {
  358. //console.log( 'handleTouchStartDollyPan' );
  359. if ( scope.enableZoom ) {
  360. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  361. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  362. var distance = Math.sqrt( dx * dx + dy * dy );
  363. dollyStart.set( 0, distance );
  364. }
  365. if ( scope.enablePan ) {
  366. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  367. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  368. panStart.set( x, y );
  369. }
  370. }
  371. function handleTouchMoveRotate( event ) {
  372. //console.log( 'handleTouchMoveRotate' );
  373. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  374. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  375. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  376. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  377. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  378. rotateStart.copy( rotateEnd );
  379. scope.update();
  380. }
  381. function handleTouchMoveDollyPan( event ) {
  382. //console.log( 'handleTouchMoveDollyPan' );
  383. if ( scope.enableZoom ) {
  384. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  385. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  386. var distance = Math.sqrt( dx * dx + dy * dy );
  387. dollyEnd.set( 0, distance );
  388. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  389. dollyIn( dollyDelta.y );
  390. dollyStart.copy( dollyEnd );
  391. }
  392. if ( scope.enablePan ) {
  393. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  394. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  395. panEnd.set( x, y );
  396. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  397. pan( panDelta.x, panDelta.y );
  398. panStart.copy( panEnd );
  399. }
  400. scope.update();
  401. }
  402. function handleTouchEnd( event ) {
  403. //console.log( 'handleTouchEnd' );
  404. }
  405. //
  406. // event handlers - FSM: listen for events and reset state
  407. //
  408. function onMouseDown( event ) {
  409. if ( scope.enabled === false ) return;
  410. event.preventDefault();
  411. switch ( event.button ) {
  412. case scope.mouseButtons.LEFT:
  413. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  414. if ( scope.enablePan === false ) return;
  415. handleMouseDownPan( event );
  416. state = STATE.PAN;
  417. } else {
  418. if ( scope.enableRotate === false ) return;
  419. handleMouseDownRotate( event );
  420. state = STATE.ROTATE;
  421. }
  422. break;
  423. case scope.mouseButtons.MIDDLE:
  424. if ( scope.enableZoom === false ) return;
  425. handleMouseDownDolly( event );
  426. state = STATE.DOLLY;
  427. break;
  428. case scope.mouseButtons.RIGHT:
  429. if ( scope.enablePan === false ) return;
  430. handleMouseDownPan( event );
  431. state = STATE.PAN;
  432. break;
  433. }
  434. if ( state !== STATE.NONE ) {
  435. document.addEventListener( 'mousemove', onMouseMove, false );
  436. document.addEventListener( 'mouseup', onMouseUp, false );
  437. scope.dispatchEvent( startEvent );
  438. }
  439. }
  440. function onMouseMove( event ) {
  441. if ( scope.enabled === false ) return;
  442. event.preventDefault();
  443. switch ( state ) {
  444. case STATE.ROTATE:
  445. if ( scope.enableRotate === false ) return;
  446. handleMouseMoveRotate( event );
  447. break;
  448. case STATE.DOLLY:
  449. if ( scope.enableZoom === false ) return;
  450. handleMouseMoveDolly( event );
  451. break;
  452. case STATE.PAN:
  453. if ( scope.enablePan === false ) return;
  454. handleMouseMovePan( event );
  455. break;
  456. }
  457. }
  458. function onMouseUp( event ) {
  459. if ( scope.enabled === false ) return;
  460. handleMouseUp( event );
  461. document.removeEventListener( 'mousemove', onMouseMove, false );
  462. document.removeEventListener( 'mouseup', onMouseUp, false );
  463. scope.dispatchEvent( endEvent );
  464. state = STATE.NONE;
  465. }
  466. function onMouseWheel( event ) {
  467. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  468. event.preventDefault();
  469. event.stopPropagation();
  470. scope.dispatchEvent( startEvent );
  471. handleMouseWheel( event );
  472. scope.dispatchEvent( endEvent );
  473. }
  474. function onKeyDown( event ) {
  475. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  476. handleKeyDown( event );
  477. }
  478. function onTouchStart( event ) {
  479. if ( scope.enabled === false ) return;
  480. event.preventDefault();
  481. switch ( event.touches.length ) {
  482. case 1: // one-fingered touch: rotate
  483. if ( scope.enableRotate === false ) return;
  484. handleTouchStartRotate( event );
  485. state = STATE.TOUCH_ROTATE;
  486. break;
  487. case 2: // two-fingered touch: dolly-pan
  488. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  489. handleTouchStartDollyPan( event );
  490. state = STATE.TOUCH_DOLLY_PAN;
  491. break;
  492. default:
  493. state = STATE.NONE;
  494. }
  495. if ( state !== STATE.NONE ) {
  496. scope.dispatchEvent( startEvent );
  497. }
  498. }
  499. function onTouchMove( event ) {
  500. if ( scope.enabled === false ) return;
  501. event.preventDefault();
  502. event.stopPropagation();
  503. switch ( event.touches.length ) {
  504. case 1: // one-fingered touch: rotate
  505. if ( scope.enableRotate === false ) return;
  506. if ( state !== STATE.TOUCH_ROTATE ) return; // is this needed?
  507. handleTouchMoveRotate( event );
  508. break;
  509. case 2: // two-fingered touch: dolly-pan
  510. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  511. if ( state !== STATE.TOUCH_DOLLY_PAN ) return; // is this needed?
  512. handleTouchMoveDollyPan( event );
  513. break;
  514. default:
  515. state = STATE.NONE;
  516. }
  517. }
  518. function onTouchEnd( event ) {
  519. if ( scope.enabled === false ) return;
  520. handleTouchEnd( event );
  521. scope.dispatchEvent( endEvent );
  522. state = STATE.NONE;
  523. }
  524. function onContextMenu( event ) {
  525. if ( scope.enabled === false ) return;
  526. event.preventDefault();
  527. }
  528. //
  529. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  530. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  531. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  532. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  533. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  534. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  535. window.addEventListener( 'keydown', onKeyDown, false );
  536. // force an update at start
  537. this.update();
  538. };
  539. OrbitControls.prototype = Object.create( EventDispatcher.prototype );
  540. OrbitControls.prototype.constructor = OrbitControls;
  541. Object.defineProperties( OrbitControls.prototype, {
  542. center: {
  543. get: function () {
  544. console.warn( 'THREE.OrbitControls: .center has been renamed to .target' );
  545. return this.target;
  546. }
  547. },
  548. // backward compatibility
  549. noZoom: {
  550. get: function () {
  551. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  552. return ! this.enableZoom;
  553. },
  554. set: function ( value ) {
  555. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  556. this.enableZoom = ! value;
  557. }
  558. },
  559. noRotate: {
  560. get: function () {
  561. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  562. return ! this.enableRotate;
  563. },
  564. set: function ( value ) {
  565. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  566. this.enableRotate = ! value;
  567. }
  568. },
  569. noPan: {
  570. get: function () {
  571. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  572. return ! this.enablePan;
  573. },
  574. set: function ( value ) {
  575. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  576. this.enablePan = ! value;
  577. }
  578. },
  579. noKeys: {
  580. get: function () {
  581. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  582. return ! this.enableKeys;
  583. },
  584. set: function ( value ) {
  585. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  586. this.enableKeys = ! value;
  587. }
  588. },
  589. staticMoving: {
  590. get: function () {
  591. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  592. return ! this.enableDamping;
  593. },
  594. set: function ( value ) {
  595. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  596. this.enableDamping = ! value;
  597. }
  598. },
  599. dynamicDampingFactor: {
  600. get: function () {
  601. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  602. return this.dampingFactor;
  603. },
  604. set: function ( value ) {
  605. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  606. this.dampingFactor = value;
  607. }
  608. }
  609. } );
  610. export { OrbitControls };