OrbitControls.js 22 KB

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