OrbitControls.js 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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 finger 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( 'wheel', onMouseWheel, false );
  143. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  144. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  145. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  146. document.removeEventListener( 'mousemove', onMouseMove, false );
  147. document.removeEventListener( 'mouseup', onMouseUp, false );
  148. window.removeEventListener( 'keydown', onKeyDown, false );
  149. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  150. };
  151. //
  152. // internals
  153. //
  154. var scope = this;
  155. var changeEvent = { type: 'change' };
  156. var startEvent = { type: 'start' };
  157. var endEvent = { type: 'end' };
  158. var STATE = { NONE: - 1, ROTATE: 0, DOLLY: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_DOLLY: 4, TOUCH_PAN: 5 };
  159. var state = STATE.NONE;
  160. var EPS = 0.000001;
  161. // current position in spherical coordinates
  162. var spherical = new THREE.Spherical();
  163. var sphericalDelta = new THREE.Spherical();
  164. var scale = 1;
  165. var panOffset = new THREE.Vector3();
  166. var zoomChanged = false;
  167. var rotateStart = new THREE.Vector2();
  168. var rotateEnd = new THREE.Vector2();
  169. var rotateDelta = new THREE.Vector2();
  170. var panStart = new THREE.Vector2();
  171. var panEnd = new THREE.Vector2();
  172. var panDelta = new THREE.Vector2();
  173. var dollyStart = new THREE.Vector2();
  174. var dollyEnd = new THREE.Vector2();
  175. var dollyDelta = new THREE.Vector2();
  176. function getAutoRotationAngle() {
  177. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  178. }
  179. function getZoomScale() {
  180. return Math.pow( 0.95, scope.zoomSpeed );
  181. }
  182. function rotateLeft( angle ) {
  183. sphericalDelta.theta -= angle;
  184. }
  185. function rotateUp( angle ) {
  186. sphericalDelta.phi -= angle;
  187. }
  188. var panLeft = function () {
  189. var v = new THREE.Vector3();
  190. return function panLeft( distance, objectMatrix ) {
  191. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  192. v.multiplyScalar( - distance );
  193. panOffset.add( v );
  194. };
  195. }();
  196. var panUp = function () {
  197. var v = new THREE.Vector3();
  198. return function panUp( distance, objectMatrix ) {
  199. v.setFromMatrixColumn( objectMatrix, 1 ); // get Y column of objectMatrix
  200. v.multiplyScalar( distance );
  201. panOffset.add( v );
  202. };
  203. }();
  204. // deltaX and deltaY are in pixels; right and down are positive
  205. var pan = function () {
  206. var offset = new THREE.Vector3();
  207. return function pan( deltaX, deltaY ) {
  208. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  209. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  210. // perspective
  211. var position = scope.object.position;
  212. offset.copy( position ).sub( scope.target );
  213. var targetDistance = offset.length();
  214. // half of the fov is center to top of screen
  215. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  216. // we actually don't use screenWidth, since perspective camera is fixed to screen height
  217. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  218. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  219. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  220. // orthographic
  221. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  222. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  223. } else {
  224. // camera neither orthographic nor perspective
  225. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  226. scope.enablePan = false;
  227. }
  228. };
  229. }();
  230. function dollyIn( dollyScale ) {
  231. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  232. scale /= dollyScale;
  233. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  234. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  235. scope.object.updateProjectionMatrix();
  236. zoomChanged = true;
  237. } else {
  238. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  239. scope.enableZoom = false;
  240. }
  241. }
  242. function dollyOut( dollyScale ) {
  243. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  244. scale *= dollyScale;
  245. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  246. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  247. scope.object.updateProjectionMatrix();
  248. zoomChanged = true;
  249. } else {
  250. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  251. scope.enableZoom = false;
  252. }
  253. }
  254. //
  255. // event callbacks - update the object state
  256. //
  257. function handleMouseDownRotate( event ) {
  258. //console.log( 'handleMouseDownRotate' );
  259. rotateStart.set( event.clientX, event.clientY );
  260. }
  261. function handleMouseDownDolly( event ) {
  262. //console.log( 'handleMouseDownDolly' );
  263. dollyStart.set( event.clientX, event.clientY );
  264. }
  265. function handleMouseDownPan( event ) {
  266. //console.log( 'handleMouseDownPan' );
  267. panStart.set( event.clientX, event.clientY );
  268. }
  269. function handleMouseMoveRotate( event ) {
  270. //console.log( 'handleMouseMoveRotate' );
  271. rotateEnd.set( event.clientX, event.clientY );
  272. rotateDelta.subVectors( rotateEnd, rotateStart );
  273. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  274. // rotating across whole screen goes 360 degrees around
  275. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  276. // rotating up and down along whole screen attempts to go 360, but limited to 180
  277. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  278. rotateStart.copy( rotateEnd );
  279. scope.update();
  280. }
  281. function handleMouseMoveDolly( event ) {
  282. //console.log( 'handleMouseMoveDolly' );
  283. dollyEnd.set( event.clientX, event.clientY );
  284. dollyDelta.subVectors( dollyEnd, dollyStart );
  285. if ( dollyDelta.y > 0 ) {
  286. dollyIn( getZoomScale() );
  287. } else if ( dollyDelta.y < 0 ) {
  288. dollyOut( getZoomScale() );
  289. }
  290. dollyStart.copy( dollyEnd );
  291. scope.update();
  292. }
  293. function handleMouseMovePan( event ) {
  294. //console.log( 'handleMouseMovePan' );
  295. panEnd.set( event.clientX, event.clientY );
  296. panDelta.subVectors( panEnd, panStart );
  297. pan( panDelta.x, panDelta.y );
  298. panStart.copy( panEnd );
  299. scope.update();
  300. }
  301. function handleMouseUp( event ) {
  302. // console.log( 'handleMouseUp' );
  303. }
  304. function handleMouseWheel( event ) {
  305. // console.log( 'handleMouseWheel' );
  306. if ( event.deltaY < 0 ) {
  307. dollyOut( getZoomScale() );
  308. } else if ( event.deltaY > 0 ) {
  309. dollyIn( getZoomScale() );
  310. }
  311. scope.update();
  312. }
  313. function handleKeyDown( event ) {
  314. //console.log( 'handleKeyDown' );
  315. switch ( event.keyCode ) {
  316. case scope.keys.UP:
  317. pan( 0, scope.keyPanSpeed );
  318. scope.update();
  319. break;
  320. case scope.keys.BOTTOM:
  321. pan( 0, - scope.keyPanSpeed );
  322. scope.update();
  323. break;
  324. case scope.keys.LEFT:
  325. pan( scope.keyPanSpeed, 0 );
  326. scope.update();
  327. break;
  328. case scope.keys.RIGHT:
  329. pan( - scope.keyPanSpeed, 0 );
  330. scope.update();
  331. break;
  332. }
  333. }
  334. function handleTouchStartRotate( event ) {
  335. //console.log( 'handleTouchStartRotate' );
  336. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  337. }
  338. function handleTouchStartDolly( event ) {
  339. //console.log( 'handleTouchStartDolly' );
  340. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  341. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  342. var distance = Math.sqrt( dx * dx + dy * dy );
  343. dollyStart.set( 0, distance );
  344. }
  345. function handleTouchStartPan( event ) {
  346. //console.log( 'handleTouchStartPan' );
  347. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  348. }
  349. function handleTouchMoveRotate( event ) {
  350. //console.log( 'handleTouchMoveRotate' );
  351. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  352. rotateDelta.subVectors( rotateEnd, rotateStart );
  353. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  354. // rotating across whole screen goes 360 degrees around
  355. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  356. // rotating up and down along whole screen attempts to go 360, but limited to 180
  357. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  358. rotateStart.copy( rotateEnd );
  359. scope.update();
  360. }
  361. function handleTouchMoveDolly( event ) {
  362. //console.log( 'handleTouchMoveDolly' );
  363. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  364. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  365. var distance = Math.sqrt( dx * dx + dy * dy );
  366. dollyEnd.set( 0, distance );
  367. dollyDelta.subVectors( dollyEnd, dollyStart );
  368. if ( dollyDelta.y > 0 ) {
  369. dollyOut( getZoomScale() );
  370. } else if ( dollyDelta.y < 0 ) {
  371. dollyIn( getZoomScale() );
  372. }
  373. dollyStart.copy( dollyEnd );
  374. scope.update();
  375. }
  376. function handleTouchMovePan( event ) {
  377. //console.log( 'handleTouchMovePan' );
  378. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  379. panDelta.subVectors( panEnd, panStart );
  380. pan( panDelta.x, panDelta.y );
  381. panStart.copy( panEnd );
  382. scope.update();
  383. }
  384. function handleTouchEnd( event ) {
  385. //console.log( 'handleTouchEnd' );
  386. }
  387. //
  388. // event handlers - FSM: listen for events and reset state
  389. //
  390. function onMouseDown( event ) {
  391. if ( scope.enabled === false ) return;
  392. event.preventDefault();
  393. if ( event.button === scope.mouseButtons.ORBIT ) {
  394. if ( scope.enableRotate === false ) return;
  395. handleMouseDownRotate( event );
  396. state = STATE.ROTATE;
  397. } else if ( event.button === scope.mouseButtons.ZOOM ) {
  398. if ( scope.enableZoom === false ) return;
  399. handleMouseDownDolly( event );
  400. state = STATE.DOLLY;
  401. } else if ( event.button === scope.mouseButtons.PAN ) {
  402. if ( scope.enablePan === false ) return;
  403. handleMouseDownPan( event );
  404. state = STATE.PAN;
  405. }
  406. if ( state !== STATE.NONE ) {
  407. document.addEventListener( 'mousemove', onMouseMove, false );
  408. document.addEventListener( 'mouseup', onMouseUp, false );
  409. scope.dispatchEvent( startEvent );
  410. }
  411. }
  412. function onMouseMove( event ) {
  413. if ( scope.enabled === false ) return;
  414. event.preventDefault();
  415. if ( state === STATE.ROTATE ) {
  416. if ( scope.enableRotate === false ) return;
  417. handleMouseMoveRotate( event );
  418. } else if ( state === STATE.DOLLY ) {
  419. if ( scope.enableZoom === false ) return;
  420. handleMouseMoveDolly( event );
  421. } else if ( state === STATE.PAN ) {
  422. if ( scope.enablePan === false ) return;
  423. handleMouseMovePan( event );
  424. }
  425. }
  426. function onMouseUp( event ) {
  427. if ( scope.enabled === false ) return;
  428. handleMouseUp( event );
  429. document.removeEventListener( 'mousemove', onMouseMove, false );
  430. document.removeEventListener( 'mouseup', onMouseUp, false );
  431. scope.dispatchEvent( endEvent );
  432. state = STATE.NONE;
  433. }
  434. function onMouseWheel( event ) {
  435. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  436. event.preventDefault();
  437. event.stopPropagation();
  438. handleMouseWheel( event );
  439. scope.dispatchEvent( startEvent ); // not sure why these are here...
  440. scope.dispatchEvent( endEvent );
  441. }
  442. function onKeyDown( event ) {
  443. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  444. handleKeyDown( event );
  445. }
  446. function onTouchStart( event ) {
  447. if ( scope.enabled === false ) return;
  448. switch ( event.touches.length ) {
  449. case 1: // one-fingered touch: rotate
  450. if ( scope.enableRotate === false ) return;
  451. handleTouchStartRotate( event );
  452. state = STATE.TOUCH_ROTATE;
  453. break;
  454. case 2: // two-fingered touch: dolly
  455. if ( scope.enableZoom === false ) return;
  456. handleTouchStartDolly( event );
  457. state = STATE.TOUCH_DOLLY;
  458. break;
  459. case 3: // three-fingered touch: pan
  460. if ( scope.enablePan === false ) return;
  461. handleTouchStartPan( event );
  462. state = STATE.TOUCH_PAN;
  463. break;
  464. default:
  465. state = STATE.NONE;
  466. }
  467. if ( state !== STATE.NONE ) {
  468. scope.dispatchEvent( startEvent );
  469. }
  470. }
  471. function onTouchMove( event ) {
  472. if ( scope.enabled === false ) return;
  473. event.preventDefault();
  474. event.stopPropagation();
  475. switch ( event.touches.length ) {
  476. case 1: // one-fingered touch: rotate
  477. if ( scope.enableRotate === false ) return;
  478. if ( state !== STATE.TOUCH_ROTATE ) return; // is this needed?...
  479. handleTouchMoveRotate( event );
  480. break;
  481. case 2: // two-fingered touch: dolly
  482. if ( scope.enableZoom === false ) return;
  483. if ( state !== STATE.TOUCH_DOLLY ) return; // is this needed?...
  484. handleTouchMoveDolly( event );
  485. break;
  486. case 3: // three-fingered touch: pan
  487. if ( scope.enablePan === false ) return;
  488. if ( state !== STATE.TOUCH_PAN ) return; // is this needed?...
  489. handleTouchMovePan( event );
  490. break;
  491. default:
  492. state = STATE.NONE;
  493. }
  494. }
  495. function onTouchEnd( event ) {
  496. if ( scope.enabled === false ) return;
  497. handleTouchEnd( event );
  498. scope.dispatchEvent( endEvent );
  499. state = STATE.NONE;
  500. }
  501. function onContextMenu( event ) {
  502. event.preventDefault();
  503. }
  504. //
  505. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  506. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  507. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  508. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  509. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  510. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  511. window.addEventListener( 'keydown', onKeyDown, false );
  512. // force an update at start
  513. this.update();
  514. };
  515. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  516. THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;
  517. Object.defineProperties( THREE.OrbitControls.prototype, {
  518. center: {
  519. get: function () {
  520. console.warn( 'THREE.OrbitControls: .center has been renamed to .target' );
  521. return this.target;
  522. }
  523. },
  524. // backward compatibility
  525. noZoom: {
  526. get: function () {
  527. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  528. return ! this.enableZoom;
  529. },
  530. set: function ( value ) {
  531. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  532. this.enableZoom = ! value;
  533. }
  534. },
  535. noRotate: {
  536. get: function () {
  537. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  538. return ! this.enableRotate;
  539. },
  540. set: function ( value ) {
  541. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  542. this.enableRotate = ! value;
  543. }
  544. },
  545. noPan: {
  546. get: function () {
  547. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  548. return ! this.enablePan;
  549. },
  550. set: function ( value ) {
  551. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  552. this.enablePan = ! value;
  553. }
  554. },
  555. noKeys: {
  556. get: function () {
  557. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  558. return ! this.enableKeys;
  559. },
  560. set: function ( value ) {
  561. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  562. this.enableKeys = ! value;
  563. }
  564. },
  565. staticMoving: {
  566. get: function () {
  567. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  568. return ! this.enableDamping;
  569. },
  570. set: function ( value ) {
  571. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  572. this.enableDamping = ! value;
  573. }
  574. },
  575. dynamicDampingFactor: {
  576. get: function () {
  577. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  578. return this.dampingFactor;
  579. },
  580. set: function ( value ) {
  581. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  582. this.dampingFactor = value;
  583. }
  584. }
  585. } );