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