OrbitControls.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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 phi;
  68. };
  69. this.getAzimuthalAngle = function () {
  70. return 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 () {
  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. theta = Math.atan2( offset.x, offset.z );
  96. // angle from y-axis
  97. phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y );
  98. if ( scope.autoRotate && state === STATE.NONE ) {
  99. rotateLeft( getAutoRotationAngle() );
  100. }
  101. theta += thetaDelta;
  102. phi += phiDelta;
  103. // restrict theta to be between desired limits
  104. theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, theta ) );
  105. // restrict phi to be between desired limits
  106. phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, phi ) );
  107. // restrict phi to be betwee EPS and PI-EPS
  108. phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );
  109. var radius = offset.length() * scale;
  110. // restrict radius to be between desired limits
  111. radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, radius ) );
  112. // move target to panned location
  113. scope.target.add( panOffset );
  114. offset.x = radius * Math.sin( phi ) * Math.sin( theta );
  115. offset.y = radius * Math.cos( phi );
  116. offset.z = radius * Math.sin( phi ) * Math.cos( theta );
  117. // rotate offset back to "camera-up-vector-is-up" space
  118. offset.applyQuaternion( quatInverse );
  119. position.copy( scope.target ).add( offset );
  120. scope.object.lookAt( scope.target );
  121. if ( scope.enableDamping === true ) {
  122. thetaDelta *= ( 1 - scope.dampingFactor );
  123. phiDelta *= ( 1 - scope.dampingFactor );
  124. } else {
  125. thetaDelta = 0;
  126. phiDelta = 0;
  127. }
  128. scale = 1;
  129. panOffset.set( 0, 0, 0 );
  130. // update condition is:
  131. // min(camera displacement, camera rotation in radians)^2 > EPS
  132. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  133. if ( zoomChanged ||
  134. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  135. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  136. scope.dispatchEvent( changeEvent );
  137. lastPosition.copy( scope.object.position );
  138. lastQuaternion.copy( scope.object.quaternion );
  139. zoomChanged = false;
  140. return true;
  141. }
  142. return false;
  143. };
  144. }();
  145. this.dispose = function() {
  146. scope.domElement.removeEventListener( 'contextmenu', contextmenu, false );
  147. scope.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  148. scope.domElement.removeEventListener( 'mousewheel', onMouseWheel, false );
  149. scope.domElement.removeEventListener( 'MozMousePixelScroll', onMouseWheel, false ); // firefox
  150. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  151. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  152. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  153. document.removeEventListener( 'mousemove', onMouseMove, false );
  154. document.removeEventListener( 'mouseup', onMouseUp, false );
  155. document.removeEventListener( 'mouseout', 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 : 4, TOUCH_PAN : 5 };
  167. var state = STATE.NONE;
  168. var EPS = 0.000001;
  169. // current position in spherical coordinates
  170. var theta;
  171. var phi;
  172. var phiDelta = 0;
  173. var thetaDelta = 0;
  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. thetaDelta -= angle;
  194. }
  195. function rotateUp( angle ) {
  196. phiDelta -= angle;
  197. }
  198. var panLeft = function() {
  199. var v = new THREE.Vector3();
  200. return function panLeft( distance, objectMatrix ) {
  201. var te = objectMatrix.elements;
  202. // get X column of objectMatrix
  203. v.set( te[ 0 ], te[ 1 ], te[ 2 ] );
  204. v.multiplyScalar( - distance );
  205. panOffset.add( v );
  206. };
  207. }();
  208. var panUp = function() {
  209. var v = new THREE.Vector3();
  210. return function panUp( distance, objectMatrix ) {
  211. var te = objectMatrix.elements;
  212. // get Y column of objectMatrix
  213. v.set( te[ 4 ], te[ 5 ], te[ 6 ] );
  214. v.multiplyScalar( distance );
  215. panOffset.add( v );
  216. };
  217. }();
  218. // deltaX and deltaY are in pixels; right and down are positive
  219. var pan = function() {
  220. var offset = new THREE.Vector3();
  221. return function( deltaX, deltaY ) {
  222. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  223. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  224. // perspective
  225. var position = scope.object.position;
  226. offset.copy( position ).sub( scope.target );
  227. var targetDistance = offset.length();
  228. // half of the fov is center to top of screen
  229. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  230. // we actually don't use screenWidth, since perspective camera is fixed to screen height
  231. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  232. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  233. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  234. // orthographic
  235. panLeft( deltaX * ( scope.object.right - scope.object.left ) / element.clientWidth, scope.object.matrix );
  236. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / element.clientHeight, scope.object.matrix );
  237. } else {
  238. // camera neither orthographic nor perspective
  239. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  240. scope.enablePan = false;
  241. }
  242. };
  243. }();
  244. function dollyIn( dollyScale ) {
  245. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  246. scale /= dollyScale;
  247. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  248. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  249. scope.object.updateProjectionMatrix();
  250. zoomChanged = true;
  251. } else {
  252. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  253. scope.enableZoom = false;
  254. }
  255. }
  256. function dollyOut( dollyScale ) {
  257. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  258. scale *= dollyScale;
  259. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  260. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  261. scope.object.updateProjectionMatrix();
  262. zoomChanged = true;
  263. } else {
  264. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  265. scope.enableZoom = false;
  266. }
  267. }
  268. //
  269. // event callbacks - update the object state
  270. //
  271. handleMouseDownRotate = function( event ) {
  272. //console.log( 'handleMouseDownRotate' );
  273. rotateStart.set( event.clientX, event.clientY );
  274. };
  275. handleMouseDownDolly = function( event ) {
  276. //console.log( 'handleMouseDownDolly' );
  277. dollyStart.set( event.clientX, event.clientY );
  278. };
  279. handleMouseDownPan = function( event ) {
  280. //console.log( 'handleMouseDownPan' );
  281. panStart.set( event.clientX, event.clientY );
  282. };
  283. handleMouseMoveRotate = function( event ) {
  284. //console.log( 'handleMouseMoveRotate' );
  285. rotateEnd.set( event.clientX, event.clientY );
  286. rotateDelta.subVectors( rotateEnd, rotateStart );
  287. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  288. // rotating across whole screen goes 360 degrees around
  289. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  290. // rotating up and down along whole screen attempts to go 360, but limited to 180
  291. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  292. rotateStart.copy( rotateEnd );
  293. scope.update();
  294. };
  295. handleMouseMoveDolly = function( event ) {
  296. //console.log( 'handleMouseMoveDolly' );
  297. dollyEnd.set( event.clientX, event.clientY );
  298. dollyDelta.subVectors( dollyEnd, dollyStart );
  299. if ( dollyDelta.y > 0 ) {
  300. dollyIn( scope.getZoomScale() );
  301. } else if ( dollyDelta.y < 0 ) {
  302. dollyOut( scope.getZoomScale() );
  303. }
  304. dollyStart.copy( dollyEnd );
  305. scope.update();
  306. };
  307. handleMouseMovePan = function( event ) {
  308. //console.log( 'handleMouseMovePan' );
  309. panEnd.set( event.clientX, event.clientY );
  310. panDelta.subVectors( panEnd, panStart );
  311. pan( panDelta.x, panDelta.y );
  312. panStart.copy( panEnd );
  313. scope.update();
  314. };
  315. handleMouseUp = function( event ) {
  316. //console.log( 'handleMouseUp' );
  317. };
  318. handleMouseWheel = function( event ) {
  319. //console.log( 'handleMouseWheel' );
  320. var delta = 0;
  321. if ( event.wheelDelta !== undefined ) {
  322. // WebKit / Opera / Explorer 9
  323. delta = event.wheelDelta;
  324. } else if ( event.detail !== undefined ) {
  325. // Firefox
  326. delta = - event.detail;
  327. }
  328. if ( delta > 0 ) {
  329. dollyOut( getZoomScale() );
  330. } else if ( delta < 0 ) {
  331. dollyIn( getZoomScale() );
  332. }
  333. scope.update();
  334. };
  335. handleKeyDown = function( event ) {
  336. //console.log( 'handleKeyDown' );
  337. switch ( event.keyCode ) {
  338. case scope.keys.UP:
  339. pan( 0, scope.keyPanSpeed );
  340. scope.update();
  341. break;
  342. case scope.keys.BOTTOM:
  343. pan( 0, - scope.keyPanSpeed );
  344. scope.update();
  345. break;
  346. case scope.keys.LEFT:
  347. pan( scope.keyPanSpeed, 0 );
  348. scope.update();
  349. break;
  350. case scope.keys.RIGHT:
  351. pan( - scope.keyPanSpeed, 0 );
  352. scope.update();
  353. break;
  354. }
  355. };
  356. handleTouchStartRotate = function( event ) {
  357. //console.log( 'handleTouchStartRotate' );
  358. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  359. };
  360. handleTouchStartDolly = function( event ) {
  361. //console.log( 'handleTouchStartDolly' );
  362. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  363. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  364. var distance = Math.sqrt( dx * dx + dy * dy );
  365. dollyStart.set( 0, distance );
  366. };
  367. handleTouchStartPan = function( event ) {
  368. //console.log( 'handleTouchStartPan' );
  369. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  370. };
  371. handleTouchMoveRotate = function( event ) {
  372. //console.log( 'handleTouchMoveRotate' );
  373. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  374. rotateDelta.subVectors( rotateEnd, rotateStart );
  375. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  376. // rotating across whole screen goes 360 degrees around
  377. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  378. // rotating up and down along whole screen attempts to go 360, but limited to 180
  379. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  380. rotateStart.copy( rotateEnd );
  381. scope.update();
  382. };
  383. handleTouchMoveDolly = function( event ) {
  384. //console.log( 'handleTouchMoveDolly' );
  385. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  386. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  387. var distance = Math.sqrt( dx * dx + dy * dy );
  388. dollyEnd.set( 0, distance );
  389. dollyDelta.subVectors( dollyEnd, dollyStart );
  390. if ( dollyDelta.y > 0 ) {
  391. dollyOut( getZoomScale() );
  392. } else if ( dollyDelta.y < 0 ) {
  393. dollyIn( getZoomScale() );
  394. }
  395. dollyStart.copy( dollyEnd );
  396. scope.update();
  397. };
  398. handleTouchMovePan = function( event ) {
  399. //console.log( 'handleTouchMovePan' );
  400. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  401. panDelta.subVectors( panEnd, panStart );
  402. pan( panDelta.x, panDelta.y );
  403. panStart.copy( panEnd );
  404. scope.update();
  405. };
  406. handleTouchEnd = function( event ) {
  407. //console.log( 'handleTouchEnd' );
  408. };
  409. //
  410. // event handlers - FSM: listen for events and reset state
  411. //
  412. function onMouseDown( event ) {
  413. if ( scope.enabled === false ) return;
  414. event.preventDefault();
  415. if ( event.button === scope.mouseButtons.ORBIT ) {
  416. if ( scope.enableRotate === false ) return;
  417. handleMouseDownRotate( event );
  418. state = STATE.ROTATE;
  419. } else if ( event.button === scope.mouseButtons.ZOOM ) {
  420. if ( scope.enableZoom === false ) return;
  421. handleMouseDownDolly( event );
  422. state = STATE.DOLLY;
  423. } else if ( event.button === scope.mouseButtons.PAN ) {
  424. if ( scope.enablePan === false ) return;
  425. handleMouseDownPan( event );
  426. state = STATE.PAN;
  427. }
  428. if ( state !== STATE.NONE ) {
  429. document.addEventListener( 'mousemove', onMouseMove, false );
  430. document.addEventListener( 'mouseup', onMouseUp, false );
  431. document.addEventListener( 'mouseout', onMouseUp, false );
  432. scope.dispatchEvent( startEvent );
  433. }
  434. };
  435. function onMouseMove( event ) {
  436. if ( scope.enabled === false ) return;
  437. event.preventDefault();
  438. if ( state === STATE.ROTATE ) {
  439. if ( scope.enableRotate === false ) return;
  440. handleMouseMoveRotate( event );
  441. } else if ( state === STATE.DOLLY ) {
  442. if ( scope.enableZoom === false ) return;
  443. handleMouseMoveDolly( event );
  444. } else if ( state === STATE.PAN ) {
  445. if ( scope.enablePan === false ) return;
  446. handleMouseMovePan( event );
  447. }
  448. };
  449. function onMouseUp( event ) {
  450. if ( scope.enabled === false ) return;
  451. handleMouseUp( event );
  452. document.removeEventListener( 'mousemove', onMouseMove, false );
  453. document.removeEventListener( 'mouseup', onMouseUp, false );
  454. document.removeEventListener( 'mouseout', onMouseUp, false );
  455. scope.dispatchEvent( endEvent );
  456. state = STATE.NONE;
  457. };
  458. function onMouseWheel( event ) {
  459. if ( scope.enabled === false || scope.enableZoom === false || state !== STATE.NONE ) return;
  460. event.preventDefault();
  461. event.stopPropagation();
  462. handleMouseWheel( event );
  463. scope.dispatchEvent( startEvent ); // not sure why these are here...
  464. scope.dispatchEvent( endEvent );
  465. };
  466. function onKeyDown( event ) {
  467. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  468. handleKeyDown( event );
  469. };
  470. function onTouchStart( event ) {
  471. if ( scope.enabled === false ) return;
  472. switch ( event.touches.length ) {
  473. case 1: // one-fingered touch: rotate
  474. if ( scope.enableRotate === false ) return;
  475. handleTouchStartRotate( event );
  476. state = STATE.TOUCH_ROTATE;
  477. break;
  478. case 2: // two-fingered touch: dolly
  479. if ( scope.enableZoom === false ) return;
  480. handleTouchStartDolly( event );
  481. state = STATE.TOUCH_DOLLY;
  482. break;
  483. case 3: // three-fingered touch: pan
  484. if ( scope.enablePan === false ) return;
  485. handleTouchStartPan( event );
  486. state = STATE.TOUCH_PAN;
  487. break;
  488. default:
  489. state = STATE.NONE;
  490. }
  491. if ( state !== STATE.NONE ) {
  492. scope.dispatchEvent( startEvent );
  493. }
  494. };
  495. function onTouchMove( event ) {
  496. if ( scope.enabled === false ) return;
  497. event.preventDefault();
  498. event.stopPropagation();
  499. switch ( event.touches.length ) {
  500. case 1: // one-fingered touch: rotate
  501. if ( scope.enableRotate === false ) return;
  502. if ( state !== STATE.TOUCH_ROTATE ) return; // is this needed?...
  503. handleTouchMoveRotate( event );
  504. break;
  505. case 2: // two-fingered touch: dolly
  506. if ( scope.enableZoom === false ) return;
  507. if ( state !== STATE.TOUCH_DOLLY ) return; // is this needed?...
  508. handleTouchMoveDolly( event );
  509. break;
  510. case 3: // three-fingered touch: pan
  511. if ( scope.enablePan === false ) return;
  512. if ( state !== STATE.TOUCH_PAN ) return; // is this needed?...
  513. handleTouchMovePan( event );
  514. break;
  515. default:
  516. state = STATE.NONE;
  517. }
  518. };
  519. function onTouchEnd( event ) {
  520. if ( scope.enabled === false ) return;
  521. handleTouchEnd( event );
  522. scope.dispatchEvent( endEvent );
  523. state = STATE.NONE;
  524. };
  525. onContextMenu = function( event ) {
  526. //console.log( 'onContextMenu' );
  527. event.preventDefault();
  528. }
  529. //
  530. function init() {
  531. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  532. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  533. scope.domElement.addEventListener( 'mousewheel', onMouseWheel, false );
  534. scope.domElement.addEventListener( 'MozMousePixelScroll', onMouseWheel, false ); // firefox
  535. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  536. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  537. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  538. window.addEventListener( 'keydown', onKeyDown, false );
  539. };
  540. init();
  541. // force an update at start
  542. this.update();
  543. };
  544. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  545. THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;
  546. Object.defineProperties( THREE.OrbitControls.prototype, {
  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.constraint.enableDamping;
  592. },
  593. set: function ( value ) {
  594. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  595. this.constraint.enableDamping = ! value;
  596. }
  597. },
  598. dynamicDampingFactor : {
  599. get: function () {
  600. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  601. return this.constraint.dampingFactor;
  602. },
  603. set: function ( value ) {
  604. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  605. this.constraint.dampingFactor = value;
  606. }
  607. }
  608. } );