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