OrbitControls.js 22 KB

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