OrbitControls.js 22 KB

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