OrbitControls.js 22 KB

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