OrbitControls.js 22 KB

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