OrbitControls.js 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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
  60. this.mouseButtons = { ORBIT: THREE.MOUSE.LEFT, ZOOM: THREE.MOUSE.MIDDLE, PAN: THREE.MOUSE.RIGHT };
  61. // for reset
  62. this.target0 = this.target.clone();
  63. this.position0 = this.object.position.clone();
  64. this.zoom0 = this.object.zoom;
  65. //
  66. // public methods
  67. //
  68. this.getPolarAngle = function () {
  69. return spherical.phi;
  70. };
  71. this.getAzimuthalAngle = function () {
  72. return spherical.theta;
  73. };
  74. this.saveState = function () {
  75. scope.target0.copy( scope.target );
  76. scope.position0.copy( scope.object.position );
  77. scope.zoom0 = scope.object.zoom;
  78. };
  79. this.reset = function () {
  80. scope.target.copy( scope.target0 );
  81. scope.object.position.copy( scope.position0 );
  82. scope.object.zoom = scope.zoom0;
  83. scope.object.updateProjectionMatrix();
  84. scope.dispatchEvent( changeEvent );
  85. scope.update();
  86. state = STATE.NONE;
  87. };
  88. // this method is exposed, but perhaps it would be better if we can make it private...
  89. this.update = function () {
  90. var offset = new THREE.Vector3();
  91. // so camera.up is the orbit axis
  92. var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
  93. var quatInverse = quat.clone().inverse();
  94. var lastPosition = new THREE.Vector3();
  95. var lastQuaternion = new THREE.Quaternion();
  96. return function update() {
  97. var position = scope.object.position;
  98. offset.copy( position ).sub( scope.target );
  99. // rotate offset to "y-axis-is-up" space
  100. offset.applyQuaternion( quat );
  101. // angle from z-axis around y-axis
  102. spherical.setFromVector3( offset );
  103. if ( scope.autoRotate && state === STATE.NONE ) {
  104. rotateLeft( getAutoRotationAngle() );
  105. }
  106. spherical.theta += sphericalDelta.theta;
  107. spherical.phi += sphericalDelta.phi;
  108. // restrict theta to be between desired limits
  109. spherical.theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, spherical.theta ) );
  110. // restrict phi to be between desired limits
  111. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  112. spherical.makeSafe();
  113. spherical.radius *= scale;
  114. // restrict radius to be between desired limits
  115. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  116. // move target to panned location
  117. scope.target.add( panOffset );
  118. offset.setFromSpherical( spherical );
  119. // rotate offset back to "camera-up-vector-is-up" space
  120. offset.applyQuaternion( quatInverse );
  121. position.copy( scope.target ).add( offset );
  122. scope.object.lookAt( scope.target );
  123. if ( scope.enableDamping === true ) {
  124. sphericalDelta.theta *= ( 1 - scope.dampingFactor );
  125. sphericalDelta.phi *= ( 1 - scope.dampingFactor );
  126. panOffset.multiplyScalar( 1 - scope.dampingFactor );
  127. } else {
  128. sphericalDelta.set( 0, 0, 0 );
  129. panOffset.set( 0, 0, 0 );
  130. }
  131. scale = 1;
  132. // update condition is:
  133. // min(camera displacement, camera rotation in radians)^2 > EPS
  134. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  135. if ( zoomChanged ||
  136. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  137. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  138. scope.dispatchEvent( changeEvent );
  139. lastPosition.copy( scope.object.position );
  140. lastQuaternion.copy( scope.object.quaternion );
  141. zoomChanged = false;
  142. return true;
  143. }
  144. return false;
  145. };
  146. }();
  147. this.dispose = function () {
  148. scope.domElement.removeEventListener( 'contextmenu', onContextMenu, false );
  149. scope.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  150. scope.domElement.removeEventListener( 'wheel', onMouseWheel, false );
  151. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  152. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  153. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  154. document.removeEventListener( 'mousemove', onMouseMove, false );
  155. document.removeEventListener( 'mouseup', onMouseUp, false );
  156. window.removeEventListener( 'keydown', onKeyDown, false );
  157. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  158. };
  159. //
  160. // internals
  161. //
  162. var scope = this;
  163. var changeEvent = { type: 'change' };
  164. var startEvent = { type: 'start' };
  165. var endEvent = { type: 'end' };
  166. var STATE = { NONE: - 1, ROTATE: 0, DOLLY: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_DOLLY_PAN: 4 };
  167. var state = STATE.NONE;
  168. var EPS = 0.000001;
  169. // current position in spherical coordinates
  170. var spherical = new THREE.Spherical();
  171. var sphericalDelta = new THREE.Spherical();
  172. var scale = 1;
  173. var panOffset = new THREE.Vector3();
  174. var zoomChanged = false;
  175. var rotateStart = new THREE.Vector2();
  176. var rotateEnd = new THREE.Vector2();
  177. var rotateDelta = new THREE.Vector2();
  178. var panStart = new THREE.Vector2();
  179. var panEnd = new THREE.Vector2();
  180. var panDelta = new THREE.Vector2();
  181. var dollyStart = new THREE.Vector2();
  182. var dollyEnd = new THREE.Vector2();
  183. var dollyDelta = new THREE.Vector2();
  184. function getAutoRotationAngle() {
  185. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  186. }
  187. function getZoomScale() {
  188. return Math.pow( 0.95, scope.zoomSpeed );
  189. }
  190. function rotateLeft( angle ) {
  191. sphericalDelta.theta -= angle;
  192. }
  193. function rotateUp( angle ) {
  194. sphericalDelta.phi -= angle;
  195. }
  196. var panLeft = function () {
  197. var v = new THREE.Vector3();
  198. return function panLeft( distance, objectMatrix ) {
  199. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  200. v.multiplyScalar( - distance );
  201. panOffset.add( v );
  202. };
  203. }();
  204. var panUp = function () {
  205. var v = new THREE.Vector3();
  206. return function panUp( distance, objectMatrix ) {
  207. if ( scope.screenSpacePanning === true ) {
  208. v.setFromMatrixColumn( objectMatrix, 1 );
  209. } else {
  210. v.setFromMatrixColumn( objectMatrix, 0 );
  211. v.crossVectors( scope.object.up, v );
  212. }
  213. v.multiplyScalar( distance );
  214. panOffset.add( v );
  215. };
  216. }();
  217. // deltaX and deltaY are in pixels; right and down are positive
  218. var pan = function () {
  219. var offset = new THREE.Vector3();
  220. return function pan( deltaX, deltaY ) {
  221. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  222. if ( scope.object.isPerspectiveCamera ) {
  223. // perspective
  224. var position = scope.object.position;
  225. offset.copy( position ).sub( scope.target );
  226. var targetDistance = offset.length();
  227. // half of the fov is center to top of screen
  228. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  229. // we use only clientHeight here so aspect ratio does not distort speed
  230. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  231. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  232. } else if ( scope.object.isOrthographicCamera ) {
  233. // orthographic
  234. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  235. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  236. } else {
  237. // camera neither orthographic nor perspective
  238. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  239. scope.enablePan = false;
  240. }
  241. };
  242. }();
  243. function dollyIn( dollyScale ) {
  244. if ( scope.object.isPerspectiveCamera ) {
  245. scale /= dollyScale;
  246. } else if ( scope.object.isOrthographicCamera ) {
  247. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  248. scope.object.updateProjectionMatrix();
  249. zoomChanged = true;
  250. } else {
  251. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  252. scope.enableZoom = false;
  253. }
  254. }
  255. function dollyOut( dollyScale ) {
  256. if ( scope.object.isPerspectiveCamera ) {
  257. scale *= dollyScale;
  258. } else if ( scope.object.isOrthographicCamera ) {
  259. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  260. scope.object.updateProjectionMatrix();
  261. zoomChanged = true;
  262. } else {
  263. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  264. scope.enableZoom = false;
  265. }
  266. }
  267. //
  268. // event callbacks - update the object state
  269. //
  270. function handleMouseDownRotate( event ) {
  271. //console.log( 'handleMouseDownRotate' );
  272. rotateStart.set( event.clientX, event.clientY );
  273. }
  274. function handleMouseDownDolly( event ) {
  275. //console.log( 'handleMouseDownDolly' );
  276. dollyStart.set( event.clientX, event.clientY );
  277. }
  278. function handleMouseDownPan( event ) {
  279. //console.log( 'handleMouseDownPan' );
  280. panStart.set( event.clientX, event.clientY );
  281. }
  282. function handleMouseMoveRotate( event ) {
  283. //console.log( 'handleMouseMoveRotate' );
  284. rotateEnd.set( event.clientX, event.clientY );
  285. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );;
  286. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  287. // rotating across whole screen goes 360 degrees around
  288. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth );
  289. // rotating up and down along whole screen attempts to go 360, but limited to 180
  290. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  291. rotateStart.copy( rotateEnd );
  292. scope.update();
  293. }
  294. function handleMouseMoveDolly( event ) {
  295. //console.log( 'handleMouseMoveDolly' );
  296. dollyEnd.set( event.clientX, event.clientY );
  297. dollyDelta.subVectors( dollyEnd, dollyStart );
  298. if ( dollyDelta.y > 0 ) {
  299. dollyIn( getZoomScale() );
  300. } else if ( dollyDelta.y < 0 ) {
  301. dollyOut( getZoomScale() );
  302. }
  303. dollyStart.copy( dollyEnd );
  304. scope.update();
  305. }
  306. function handleMouseMovePan( event ) {
  307. //console.log( 'handleMouseMovePan' );
  308. panEnd.set( event.clientX, event.clientY );
  309. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  310. pan( panDelta.x, panDelta.y );
  311. panStart.copy( panEnd );
  312. scope.update();
  313. }
  314. function handleMouseUp( event ) {
  315. // console.log( 'handleMouseUp' );
  316. }
  317. function handleMouseWheel( event ) {
  318. // console.log( 'handleMouseWheel' );
  319. if ( event.deltaY < 0 ) {
  320. dollyOut( getZoomScale() );
  321. } else if ( event.deltaY > 0 ) {
  322. dollyIn( getZoomScale() );
  323. }
  324. scope.update();
  325. }
  326. function handleKeyDown( event ) {
  327. //console.log( 'handleKeyDown' );
  328. switch ( event.keyCode ) {
  329. case scope.keys.UP:
  330. pan( 0, scope.keyPanSpeed );
  331. scope.update();
  332. break;
  333. case scope.keys.BOTTOM:
  334. pan( 0, - scope.keyPanSpeed );
  335. scope.update();
  336. break;
  337. case scope.keys.LEFT:
  338. pan( scope.keyPanSpeed, 0 );
  339. scope.update();
  340. break;
  341. case scope.keys.RIGHT:
  342. pan( - scope.keyPanSpeed, 0 );
  343. scope.update();
  344. break;
  345. }
  346. }
  347. function handleTouchStartRotate( event ) {
  348. //console.log( 'handleTouchStartRotate' );
  349. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  350. }
  351. function handleTouchStartDollyPan( event ) {
  352. //console.log( 'handleTouchStartDollyPan' );
  353. if ( scope.enableZoom ) {
  354. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  355. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  356. var distance = Math.sqrt( dx * dx + dy * dy );
  357. dollyStart.set( 0, distance );
  358. }
  359. if ( scope.enablePan ) {
  360. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  361. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  362. panStart.set( x, y );
  363. }
  364. }
  365. function handleTouchMoveRotate( event ) {
  366. //console.log( 'handleTouchMoveRotate' );
  367. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  368. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  369. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  370. // rotating across whole screen goes 360 degrees around
  371. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth );
  372. // rotating up and down along whole screen attempts to go 360, but limited to 180
  373. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  374. rotateStart.copy( rotateEnd );
  375. scope.update();
  376. }
  377. function handleTouchMoveDollyPan( event ) {
  378. //console.log( 'handleTouchMoveDollyPan' );
  379. if ( scope.enableZoom ) {
  380. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  381. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  382. var distance = Math.sqrt( dx * dx + dy * dy );
  383. dollyEnd.set( 0, distance );
  384. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  385. dollyIn( dollyDelta.y );
  386. dollyStart.copy( dollyEnd );
  387. }
  388. if ( scope.enablePan ) {
  389. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  390. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  391. panEnd.set( x, y );
  392. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  393. pan( panDelta.x, panDelta.y );
  394. panStart.copy( panEnd );
  395. }
  396. scope.update();
  397. }
  398. function handleTouchEnd( event ) {
  399. //console.log( 'handleTouchEnd' );
  400. }
  401. //
  402. // event handlers - FSM: listen for events and reset state
  403. //
  404. function onMouseDown( event ) {
  405. if ( scope.enabled === false ) return;
  406. event.preventDefault();
  407. switch ( event.button ) {
  408. case scope.mouseButtons.ORBIT:
  409. if ( scope.enableRotate === false ) return;
  410. handleMouseDownRotate( event );
  411. state = STATE.ROTATE;
  412. break;
  413. case scope.mouseButtons.ZOOM:
  414. if ( scope.enableZoom === false ) return;
  415. handleMouseDownDolly( event );
  416. state = STATE.DOLLY;
  417. break;
  418. case scope.mouseButtons.PAN:
  419. if ( scope.enablePan === false ) return;
  420. handleMouseDownPan( event );
  421. state = STATE.PAN;
  422. break;
  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. } );