OrbitControls.js 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /**
  2. * @author qiao / https://github.com/qiao
  3. * @author mrdoob / http://mrdoob.com
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author WestLangley / http://github.com/WestLangley
  6. * @author erich666 / http://erichaines.com
  7. */
  8. // This set of controls performs orbiting, dollying (zooming), and panning.
  9. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  10. //
  11. // Orbit - left mouse / touch: one finger move
  12. // Zoom - middle mouse, or mousewheel / touch: two finger spread or squish
  13. // Pan - right mouse, or arrow keys / touch: three finger swipe
  14. THREE.OrbitControls = function ( object, domElement ) {
  15. this.object = object;
  16. this.domElement = ( domElement !== undefined ) ? domElement : document;
  17. // Set to false to disable this control
  18. this.enabled = true;
  19. // "target" sets the location of focus, where the object orbits around
  20. this.target = new THREE.Vector3();
  21. // How far you can dolly in and out ( PerspectiveCamera only )
  22. this.minDistance = 0;
  23. this.maxDistance = Infinity;
  24. // How far you can zoom in and out ( OrthographicCamera only )
  25. this.minZoom = 0;
  26. this.maxZoom = Infinity;
  27. // How far you can orbit vertically, upper and lower limits.
  28. // Range is 0 to Math.PI radians.
  29. this.minPolarAngle = 0; // radians
  30. this.maxPolarAngle = Math.PI; // radians
  31. // How far you can orbit horizontally, upper and lower limits.
  32. // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
  33. this.minAzimuthAngle = - Infinity; // radians
  34. this.maxAzimuthAngle = Infinity; // radians
  35. // Set to true to enable damping (inertia)
  36. // If damping is enabled, you must call controls.update() in your animation loop
  37. this.enableDamping = false;
  38. this.dampingFactor = 0.25;
  39. // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
  40. // Set to false to disable zooming
  41. this.enableZoom = true;
  42. this.zoomSpeed = 1.0;
  43. // Set to false to disable rotating
  44. this.enableRotate = true;
  45. this.rotateSpeed = 1.0;
  46. // Set to false to disable panning
  47. this.enablePan = true;
  48. this.panningMode = THREE.ScreenSpacePanning; // alternate THREE.HorizontalPanning
  49. this.keyPanSpeed = 7.0; // pixels moved per arrow key push
  50. // Set to true to automatically rotate around the target
  51. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  52. this.autoRotate = false;
  53. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  54. // Set to false to disable use of the keys
  55. this.enableKeys = true;
  56. // The four arrow keys
  57. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  58. // Mouse buttons
  59. this.mouseButtons = { ORBIT: THREE.MOUSE.LEFT, ZOOM: THREE.MOUSE.MIDDLE, PAN: THREE.MOUSE.RIGHT };
  60. // for reset
  61. this.target0 = this.target.clone();
  62. this.position0 = this.object.position.clone();
  63. this.zoom0 = this.object.zoom;
  64. //
  65. // public methods
  66. //
  67. this.getPolarAngle = function () {
  68. return spherical.phi;
  69. };
  70. this.getAzimuthalAngle = function () {
  71. return spherical.theta;
  72. };
  73. this.saveState = function () {
  74. scope.target0.copy( scope.target );
  75. scope.position0.copy( scope.object.position );
  76. scope.zoom0 = scope.object.zoom;
  77. };
  78. this.reset = function () {
  79. scope.target.copy( scope.target0 );
  80. scope.object.position.copy( scope.position0 );
  81. scope.object.zoom = scope.zoom0;
  82. scope.object.updateProjectionMatrix();
  83. scope.dispatchEvent( changeEvent );
  84. scope.update();
  85. state = STATE.NONE;
  86. };
  87. // this method is exposed, but perhaps it would be better if we can make it private...
  88. this.update = function () {
  89. var offset = new THREE.Vector3();
  90. // so camera.up is the orbit axis
  91. var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
  92. var quatInverse = quat.clone().inverse();
  93. var lastPosition = new THREE.Vector3();
  94. var lastQuaternion = new THREE.Quaternion();
  95. return function update() {
  96. var position = scope.object.position;
  97. offset.copy( position ).sub( scope.target );
  98. // rotate offset to "y-axis-is-up" space
  99. offset.applyQuaternion( quat );
  100. // angle from z-axis around y-axis
  101. spherical.setFromVector3( offset );
  102. if ( scope.autoRotate && state === STATE.NONE ) {
  103. rotateLeft( getAutoRotationAngle() );
  104. }
  105. spherical.theta += sphericalDelta.theta;
  106. spherical.phi += sphericalDelta.phi;
  107. // restrict theta to be between desired limits
  108. spherical.theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, spherical.theta ) );
  109. // restrict phi to be between desired limits
  110. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  111. spherical.makeSafe();
  112. spherical.radius *= scale;
  113. // restrict radius to be between desired limits
  114. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  115. // move target to panned location
  116. scope.target.add( panOffset );
  117. offset.setFromSpherical( spherical );
  118. // rotate offset back to "camera-up-vector-is-up" space
  119. offset.applyQuaternion( quatInverse );
  120. position.copy( scope.target ).add( offset );
  121. scope.object.lookAt( scope.target );
  122. if ( scope.enableDamping === true ) {
  123. sphericalDelta.theta *= ( 1 - scope.dampingFactor );
  124. sphericalDelta.phi *= ( 1 - scope.dampingFactor );
  125. } else {
  126. sphericalDelta.set( 0, 0, 0 );
  127. }
  128. scale = 1;
  129. panOffset.set( 0, 0, 0 );
  130. // update condition is:
  131. // min(camera displacement, camera rotation in radians)^2 > EPS
  132. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  133. if ( zoomChanged ||
  134. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  135. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  136. scope.dispatchEvent( changeEvent );
  137. lastPosition.copy( scope.object.position );
  138. lastQuaternion.copy( scope.object.quaternion );
  139. zoomChanged = false;
  140. return true;
  141. }
  142. return false;
  143. };
  144. }();
  145. this.dispose = function () {
  146. scope.domElement.removeEventListener( 'contextmenu', onContextMenu, false );
  147. scope.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  148. scope.domElement.removeEventListener( 'wheel', onMouseWheel, false );
  149. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  150. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  151. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  152. document.removeEventListener( 'mousemove', onMouseMove, false );
  153. document.removeEventListener( 'mouseup', onMouseUp, false );
  154. window.removeEventListener( 'keydown', onKeyDown, false );
  155. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  156. };
  157. //
  158. // internals
  159. //
  160. var scope = this;
  161. var changeEvent = { type: 'change' };
  162. var startEvent = { type: 'start' };
  163. var endEvent = { type: 'end' };
  164. var STATE = { NONE: - 1, ROTATE: 0, DOLLY: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_DOLLY: 4, TOUCH_PAN: 5 };
  165. var state = STATE.NONE;
  166. var EPS = 0.000001;
  167. // current position in spherical coordinates
  168. var spherical = new THREE.Spherical();
  169. var sphericalDelta = new THREE.Spherical();
  170. var scale = 1;
  171. var panOffset = new THREE.Vector3();
  172. var zoomChanged = false;
  173. var rotateStart = new THREE.Vector2();
  174. var rotateEnd = new THREE.Vector2();
  175. var rotateDelta = new THREE.Vector2();
  176. var panStart = new THREE.Vector2();
  177. var panEnd = new THREE.Vector2();
  178. var panDelta = new THREE.Vector2();
  179. var dollyStart = new THREE.Vector2();
  180. var dollyEnd = new THREE.Vector2();
  181. var dollyDelta = new THREE.Vector2();
  182. function getAutoRotationAngle() {
  183. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  184. }
  185. function getZoomScale() {
  186. return Math.pow( 0.95, scope.zoomSpeed );
  187. }
  188. function rotateLeft( angle ) {
  189. sphericalDelta.theta -= angle;
  190. }
  191. function rotateUp( angle ) {
  192. sphericalDelta.phi -= angle;
  193. }
  194. var panLeft = function () {
  195. var v = new THREE.Vector3();
  196. return function panLeft( distance, objectMatrix ) {
  197. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  198. v.multiplyScalar( - distance );
  199. panOffset.add( v );
  200. };
  201. }();
  202. var panUp = function () {
  203. var v = new THREE.Vector3();
  204. return function panUp( distance, objectMatrix ) {
  205. switch ( scope.panningMode ) {
  206. case THREE.ScreenSpacePanning:
  207. v.setFromMatrixColumn( objectMatrix, 1 );
  208. break;
  209. case THREE.HorizontalPanning:
  210. v.setFromMatrixColumn( objectMatrix, 0 );
  211. v.crossVectors( scope.object.up, v );
  212. break;
  213. }
  214. v.multiplyScalar( distance );
  215. panOffset.add( v );
  216. };
  217. }();
  218. // deltaX and deltaY are in pixels; right and down are positive
  219. var pan = function () {
  220. var offset = new THREE.Vector3();
  221. return function pan( deltaX, deltaY ) {
  222. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  223. if ( scope.object.isPerspectiveCamera ) {
  224. // perspective
  225. var position = scope.object.position;
  226. offset.copy( position ).sub( scope.target );
  227. var targetDistance = offset.length();
  228. // half of the fov is center to top of screen
  229. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  230. // we actually don't use screenWidth, since perspective camera is fixed to screen height
  231. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  232. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  233. } else if ( scope.object.isOrthographicCamera ) {
  234. // orthographic
  235. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  236. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  237. } else {
  238. // camera neither orthographic nor perspective
  239. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  240. scope.enablePan = false;
  241. }
  242. };
  243. }();
  244. function dollyIn( dollyScale ) {
  245. if ( scope.object.isPerspectiveCamera ) {
  246. scale /= dollyScale;
  247. } else if ( scope.object.isOrthographicCamera ) {
  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. function dollyOut( dollyScale ) {
  257. if ( scope.object.isPerspectiveCamera ) {
  258. scale *= dollyScale;
  259. } else if ( scope.object.isOrthographicCamera ) {
  260. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  261. scope.object.updateProjectionMatrix();
  262. zoomChanged = true;
  263. } else {
  264. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  265. scope.enableZoom = false;
  266. }
  267. }
  268. //
  269. // event callbacks - update the object state
  270. //
  271. function handleMouseDownRotate( event ) {
  272. //console.log( 'handleMouseDownRotate' );
  273. rotateStart.set( event.clientX, event.clientY );
  274. }
  275. function handleMouseDownDolly( event ) {
  276. //console.log( 'handleMouseDownDolly' );
  277. dollyStart.set( event.clientX, event.clientY );
  278. }
  279. function handleMouseDownPan( event ) {
  280. //console.log( 'handleMouseDownPan' );
  281. panStart.set( event.clientX, event.clientY );
  282. }
  283. function handleMouseMoveRotate( event ) {
  284. //console.log( 'handleMouseMoveRotate' );
  285. rotateEnd.set( event.clientX, event.clientY );
  286. rotateDelta.subVectors( rotateEnd, rotateStart );
  287. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  288. // rotating across whole screen goes 360 degrees around
  289. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  290. // rotating up and down along whole screen attempts to go 360, but limited to 180
  291. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  292. rotateStart.copy( rotateEnd );
  293. scope.update();
  294. }
  295. function handleMouseMoveDolly( event ) {
  296. //console.log( 'handleMouseMoveDolly' );
  297. dollyEnd.set( event.clientX, event.clientY );
  298. dollyDelta.subVectors( dollyEnd, dollyStart );
  299. if ( dollyDelta.y > 0 ) {
  300. dollyIn( getZoomScale() );
  301. } else if ( dollyDelta.y < 0 ) {
  302. dollyOut( getZoomScale() );
  303. }
  304. dollyStart.copy( dollyEnd );
  305. scope.update();
  306. }
  307. function handleMouseMovePan( event ) {
  308. //console.log( 'handleMouseMovePan' );
  309. panEnd.set( event.clientX, event.clientY );
  310. panDelta.subVectors( panEnd, panStart );
  311. pan( panDelta.x, panDelta.y );
  312. panStart.copy( panEnd );
  313. scope.update();
  314. }
  315. function handleMouseUp( event ) {
  316. // console.log( 'handleMouseUp' );
  317. }
  318. function handleMouseWheel( event ) {
  319. // console.log( 'handleMouseWheel' );
  320. if ( event.deltaY < 0 ) {
  321. dollyOut( getZoomScale() );
  322. } else if ( event.deltaY > 0 ) {
  323. dollyIn( getZoomScale() );
  324. }
  325. scope.update();
  326. }
  327. function handleKeyDown( event ) {
  328. //console.log( 'handleKeyDown' );
  329. switch ( event.keyCode ) {
  330. case scope.keys.UP:
  331. pan( 0, scope.keyPanSpeed );
  332. scope.update();
  333. break;
  334. case scope.keys.BOTTOM:
  335. pan( 0, - scope.keyPanSpeed );
  336. scope.update();
  337. break;
  338. case scope.keys.LEFT:
  339. pan( scope.keyPanSpeed, 0 );
  340. scope.update();
  341. break;
  342. case scope.keys.RIGHT:
  343. pan( - scope.keyPanSpeed, 0 );
  344. scope.update();
  345. break;
  346. }
  347. }
  348. function handleTouchStartRotate( event ) {
  349. //console.log( 'handleTouchStartRotate' );
  350. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  351. }
  352. function handleTouchStartDolly( event ) {
  353. //console.log( 'handleTouchStartDolly' );
  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. function handleTouchStartPan( event ) {
  360. //console.log( 'handleTouchStartPan' );
  361. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  362. }
  363. function handleTouchMoveRotate( event ) {
  364. //console.log( 'handleTouchMoveRotate' );
  365. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  366. rotateDelta.subVectors( rotateEnd, rotateStart );
  367. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  368. // rotating across whole screen goes 360 degrees around
  369. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  370. // rotating up and down along whole screen attempts to go 360, but limited to 180
  371. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  372. rotateStart.copy( rotateEnd );
  373. scope.update();
  374. }
  375. function handleTouchMoveDolly( event ) {
  376. //console.log( 'handleTouchMoveDolly' );
  377. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  378. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  379. var distance = Math.sqrt( dx * dx + dy * dy );
  380. dollyEnd.set( 0, distance );
  381. dollyDelta.subVectors( dollyEnd, dollyStart );
  382. if ( dollyDelta.y > 0 ) {
  383. dollyOut( getZoomScale() );
  384. } else if ( dollyDelta.y < 0 ) {
  385. dollyIn( getZoomScale() );
  386. }
  387. dollyStart.copy( dollyEnd );
  388. scope.update();
  389. }
  390. function handleTouchMovePan( event ) {
  391. //console.log( 'handleTouchMovePan' );
  392. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  393. panDelta.subVectors( panEnd, panStart );
  394. pan( panDelta.x, panDelta.y );
  395. panStart.copy( panEnd );
  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. switch ( event.touches.length ) {
  471. case 1: // one-fingered touch: rotate
  472. if ( scope.enableRotate === false ) return;
  473. handleTouchStartRotate( event );
  474. state = STATE.TOUCH_ROTATE;
  475. break;
  476. case 2: // two-fingered touch: dolly
  477. if ( scope.enableZoom === false ) return;
  478. handleTouchStartDolly( event );
  479. state = STATE.TOUCH_DOLLY;
  480. break;
  481. case 3: // three-fingered touch: pan
  482. if ( scope.enablePan === false ) return;
  483. handleTouchStartPan( event );
  484. state = STATE.TOUCH_PAN;
  485. break;
  486. default:
  487. state = STATE.NONE;
  488. }
  489. if ( state !== STATE.NONE ) {
  490. scope.dispatchEvent( startEvent );
  491. }
  492. }
  493. function onTouchMove( event ) {
  494. if ( scope.enabled === false ) return;
  495. event.preventDefault();
  496. event.stopPropagation();
  497. switch ( event.touches.length ) {
  498. case 1: // one-fingered touch: rotate
  499. if ( scope.enableRotate === false ) return;
  500. if ( state !== STATE.TOUCH_ROTATE ) return; // is this needed?...
  501. handleTouchMoveRotate( event );
  502. break;
  503. case 2: // two-fingered touch: dolly
  504. if ( scope.enableZoom === false ) return;
  505. if ( state !== STATE.TOUCH_DOLLY ) return; // is this needed?...
  506. handleTouchMoveDolly( event );
  507. break;
  508. case 3: // three-fingered touch: pan
  509. if ( scope.enablePan === false ) return;
  510. if ( state !== STATE.TOUCH_PAN ) return; // is this needed?...
  511. handleTouchMovePan( event );
  512. break;
  513. default:
  514. state = STATE.NONE;
  515. }
  516. }
  517. function onTouchEnd( event ) {
  518. if ( scope.enabled === false ) return;
  519. handleTouchEnd( event );
  520. scope.dispatchEvent( endEvent );
  521. state = STATE.NONE;
  522. }
  523. function onContextMenu( event ) {
  524. if ( scope.enabled === false ) return;
  525. event.preventDefault();
  526. }
  527. //
  528. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  529. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  530. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  531. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  532. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  533. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  534. window.addEventListener( 'keydown', onKeyDown, false );
  535. // force an update at start
  536. this.update();
  537. };
  538. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  539. THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;
  540. Object.defineProperties( THREE.OrbitControls.prototype, {
  541. center: {
  542. get: function () {
  543. console.warn( 'THREE.OrbitControls: .center has been renamed to .target' );
  544. return this.target;
  545. }
  546. },
  547. // backward compatibility
  548. noZoom: {
  549. get: function () {
  550. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  551. return ! this.enableZoom;
  552. },
  553. set: function ( value ) {
  554. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  555. this.enableZoom = ! value;
  556. }
  557. },
  558. noRotate: {
  559. get: function () {
  560. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  561. return ! this.enableRotate;
  562. },
  563. set: function ( value ) {
  564. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  565. this.enableRotate = ! value;
  566. }
  567. },
  568. noPan: {
  569. get: function () {
  570. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  571. return ! this.enablePan;
  572. },
  573. set: function ( value ) {
  574. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  575. this.enablePan = ! value;
  576. }
  577. },
  578. noKeys: {
  579. get: function () {
  580. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  581. return ! this.enableKeys;
  582. },
  583. set: function ( value ) {
  584. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  585. this.enableKeys = ! value;
  586. }
  587. },
  588. staticMoving: {
  589. get: function () {
  590. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  591. return ! this.enableDamping;
  592. },
  593. set: function ( value ) {
  594. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  595. this.enableDamping = ! value;
  596. }
  597. },
  598. dynamicDampingFactor: {
  599. get: function () {
  600. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  601. return this.dampingFactor;
  602. },
  603. set: function ( value ) {
  604. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  605. this.dampingFactor = value;
  606. }
  607. }
  608. } );
  609. THREE.ScreenSpacePanning = 0;
  610. THREE.HorizontalPanning = 1;