OrbitControls.js 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /**
  2. * @author qiao / https://github.com/qiao
  3. * @author mrdoob / http://mrdoob.com
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author WestLangley / http://github.com/WestLangley
  6. * @author erich666 / http://erichaines.com
  7. */
  8. // This set of controls performs orbiting, dollying (zooming), and panning.
  9. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  10. //
  11. // Orbit - left mouse / touch: one-finger move
  12. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  13. // Pan - right mouse, or arrow keys / touch: two-finger move
  14. THREE.OrbitControls = function ( object, domElement ) {
  15. this.object = object;
  16. this.domElement = ( domElement !== undefined ) ? domElement : document;
  17. // Set to false to disable this control
  18. this.enabled = true;
  19. // "target" sets the location of focus, where the object orbits around
  20. this.target = new THREE.Vector3();
  21. // How far you can dolly in and out ( PerspectiveCamera only )
  22. this.minDistance = 0;
  23. this.maxDistance = Infinity;
  24. // How far you can zoom in and out ( OrthographicCamera only )
  25. this.minZoom = 0;
  26. this.maxZoom = Infinity;
  27. // How far you can orbit vertically, upper and lower limits.
  28. // Range is 0 to Math.PI radians.
  29. this.minPolarAngle = 0; // radians
  30. this.maxPolarAngle = Math.PI; // radians
  31. // How far you can orbit horizontally, upper and lower limits.
  32. // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
  33. this.minAzimuthAngle = - Infinity; // radians
  34. this.maxAzimuthAngle = Infinity; // radians
  35. // Set to true to enable damping (inertia)
  36. // If damping is enabled, you must call controls.update() in your animation loop
  37. this.enableDamping = false;
  38. this.dampingFactor = 0.25;
  39. // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
  40. // Set to false to disable zooming
  41. this.enableZoom = true;
  42. this.zoomSpeed = 1.0;
  43. // Set to false to disable rotating
  44. this.enableRotate = true;
  45. this.rotateSpeed = 1.0;
  46. // Set to false to disable panning
  47. this.enablePan = true;
  48. this.panSpeed = 1.0;
  49. this.screenSpacePanning = false; // if true, pan in screen-space
  50. this.keyPanSpeed = 7.0; // pixels moved per arrow key push
  51. // Set to true to automatically rotate around the target
  52. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  53. this.autoRotate = false;
  54. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  55. // Set to false to disable use of the keys
  56. this.enableKeys = true;
  57. // The four arrow keys
  58. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  59. // Mouse buttons
  60. this.mouseButtons = { ORBIT: THREE.MOUSE.LEFT, ZOOM: THREE.MOUSE.MIDDLE, PAN: THREE.MOUSE.RIGHT };
  61. // for reset
  62. this.target0 = this.target.clone();
  63. this.position0 = this.object.position.clone();
  64. this.zoom0 = this.object.zoom;
  65. //
  66. // public methods
  67. //
  68. this.getStateForMouseDown = function ( event ) {
  69. if ( event.button === scope.mouseButtons.ORBIT && ! event.ctrlKey ) return STATE.ROTATE;
  70. if ( event.button === scope.mouseButtons.ZOOM ) return STATE.DOLLY;
  71. if ( event.button === scope.mouseButtons.PAN || ( event.button === scope.mouseButtons.ORBIT && event.ctrlKey ) ) return STATE.PAN;
  72. return STATE.NONE;
  73. };
  74. this.getPolarAngle = function () {
  75. return spherical.phi;
  76. };
  77. this.getAzimuthalAngle = function () {
  78. return spherical.theta;
  79. };
  80. this.saveState = function () {
  81. scope.target0.copy( scope.target );
  82. scope.position0.copy( scope.object.position );
  83. scope.zoom0 = scope.object.zoom;
  84. };
  85. this.reset = function () {
  86. scope.target.copy( scope.target0 );
  87. scope.object.position.copy( scope.position0 );
  88. scope.object.zoom = scope.zoom0;
  89. scope.object.updateProjectionMatrix();
  90. scope.dispatchEvent( changeEvent );
  91. scope.update();
  92. state = STATE.NONE;
  93. };
  94. // this method is exposed, but perhaps it would be better if we can make it private...
  95. this.update = function () {
  96. var offset = new THREE.Vector3();
  97. // so camera.up is the orbit axis
  98. var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
  99. var quatInverse = quat.clone().inverse();
  100. var lastPosition = new THREE.Vector3();
  101. var lastQuaternion = new THREE.Quaternion();
  102. return function update() {
  103. var position = scope.object.position;
  104. offset.copy( position ).sub( scope.target );
  105. // rotate offset to "y-axis-is-up" space
  106. offset.applyQuaternion( quat );
  107. // angle from z-axis around y-axis
  108. spherical.setFromVector3( offset );
  109. if ( scope.autoRotate && state === STATE.NONE ) {
  110. rotateLeft( getAutoRotationAngle() );
  111. }
  112. spherical.theta += sphericalDelta.theta;
  113. spherical.phi += sphericalDelta.phi;
  114. // restrict theta to be between desired limits
  115. spherical.theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, spherical.theta ) );
  116. // restrict phi to be between desired limits
  117. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  118. spherical.makeSafe();
  119. spherical.radius *= scale;
  120. // restrict radius to be between desired limits
  121. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  122. // move target to panned location
  123. scope.target.add( panOffset );
  124. offset.setFromSpherical( spherical );
  125. // rotate offset back to "camera-up-vector-is-up" space
  126. offset.applyQuaternion( quatInverse );
  127. position.copy( scope.target ).add( offset );
  128. scope.object.lookAt( scope.target );
  129. if ( scope.enableDamping === true ) {
  130. sphericalDelta.theta *= ( 1 - scope.dampingFactor );
  131. sphericalDelta.phi *= ( 1 - scope.dampingFactor );
  132. panOffset.multiplyScalar( 1 - scope.dampingFactor );
  133. } else {
  134. sphericalDelta.set( 0, 0, 0 );
  135. panOffset.set( 0, 0, 0 );
  136. }
  137. scale = 1;
  138. // update condition is:
  139. // min(camera displacement, camera rotation in radians)^2 > EPS
  140. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  141. if ( zoomChanged ||
  142. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  143. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  144. scope.dispatchEvent( changeEvent );
  145. lastPosition.copy( scope.object.position );
  146. lastQuaternion.copy( scope.object.quaternion );
  147. zoomChanged = false;
  148. return true;
  149. }
  150. return false;
  151. };
  152. }();
  153. this.dispose = function () {
  154. scope.domElement.removeEventListener( 'contextmenu', onContextMenu, false );
  155. scope.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  156. scope.domElement.removeEventListener( 'wheel', onMouseWheel, false );
  157. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  158. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  159. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  160. document.removeEventListener( 'mousemove', onMouseMove, false );
  161. document.removeEventListener( 'mouseup', onMouseUp, false );
  162. window.removeEventListener( 'keydown', onKeyDown, false );
  163. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  164. };
  165. //
  166. // internals
  167. //
  168. var scope = this;
  169. var changeEvent = { type: 'change' };
  170. var startEvent = { type: 'start' };
  171. var endEvent = { type: 'end' };
  172. var STATE = { NONE: - 1, ROTATE: 0, DOLLY: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_DOLLY_PAN: 4 };
  173. var state = STATE.NONE;
  174. var EPS = 0.000001;
  175. // current position in spherical coordinates
  176. var spherical = new THREE.Spherical();
  177. var sphericalDelta = new THREE.Spherical();
  178. var scale = 1;
  179. var panOffset = new THREE.Vector3();
  180. var zoomChanged = false;
  181. var rotateStart = new THREE.Vector2();
  182. var rotateEnd = new THREE.Vector2();
  183. var rotateDelta = new THREE.Vector2();
  184. var panStart = new THREE.Vector2();
  185. var panEnd = new THREE.Vector2();
  186. var panDelta = new THREE.Vector2();
  187. var dollyStart = new THREE.Vector2();
  188. var dollyEnd = new THREE.Vector2();
  189. var dollyDelta = new THREE.Vector2();
  190. function getAutoRotationAngle() {
  191. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  192. }
  193. function getZoomScale() {
  194. return Math.pow( 0.95, scope.zoomSpeed );
  195. }
  196. function rotateLeft( angle ) {
  197. sphericalDelta.theta -= angle;
  198. }
  199. function rotateUp( angle ) {
  200. sphericalDelta.phi -= angle;
  201. }
  202. var panLeft = function () {
  203. var v = new THREE.Vector3();
  204. return function panLeft( distance, objectMatrix ) {
  205. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  206. v.multiplyScalar( - distance );
  207. panOffset.add( v );
  208. };
  209. }();
  210. var panUp = function () {
  211. var v = new THREE.Vector3();
  212. return function panUp( distance, objectMatrix ) {
  213. if ( scope.screenSpacePanning === true ) {
  214. v.setFromMatrixColumn( objectMatrix, 1 );
  215. } else {
  216. v.setFromMatrixColumn( objectMatrix, 0 );
  217. v.crossVectors( scope.object.up, v );
  218. }
  219. v.multiplyScalar( distance );
  220. panOffset.add( v );
  221. };
  222. }();
  223. // deltaX and deltaY are in pixels; right and down are positive
  224. var pan = function () {
  225. var offset = new THREE.Vector3();
  226. return function pan( deltaX, deltaY ) {
  227. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  228. if ( scope.object.isPerspectiveCamera ) {
  229. // perspective
  230. var position = scope.object.position;
  231. offset.copy( position ).sub( scope.target );
  232. var targetDistance = offset.length();
  233. // half of the fov is center to top of screen
  234. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  235. // we use only clientHeight here so aspect ratio does not distort speed
  236. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  237. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  238. } else if ( scope.object.isOrthographicCamera ) {
  239. // orthographic
  240. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  241. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  242. } else {
  243. // camera neither orthographic nor perspective
  244. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  245. scope.enablePan = false;
  246. }
  247. };
  248. }();
  249. function dollyIn( dollyScale ) {
  250. if ( scope.object.isPerspectiveCamera ) {
  251. scale /= dollyScale;
  252. } else if ( scope.object.isOrthographicCamera ) {
  253. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  254. scope.object.updateProjectionMatrix();
  255. zoomChanged = true;
  256. } else {
  257. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  258. scope.enableZoom = false;
  259. }
  260. }
  261. function dollyOut( dollyScale ) {
  262. if ( scope.object.isPerspectiveCamera ) {
  263. scale *= dollyScale;
  264. } else if ( scope.object.isOrthographicCamera ) {
  265. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  266. scope.object.updateProjectionMatrix();
  267. zoomChanged = true;
  268. } else {
  269. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  270. scope.enableZoom = false;
  271. }
  272. }
  273. //
  274. // event callbacks - update the object state
  275. //
  276. function handleMouseDownRotate( event ) {
  277. //console.log( 'handleMouseDownRotate' );
  278. rotateStart.set( event.clientX, event.clientY );
  279. }
  280. function handleMouseDownDolly( event ) {
  281. //console.log( 'handleMouseDownDolly' );
  282. dollyStart.set( event.clientX, event.clientY );
  283. }
  284. function handleMouseDownPan( event ) {
  285. //console.log( 'handleMouseDownPan' );
  286. panStart.set( event.clientX, event.clientY );
  287. }
  288. function handleMouseMoveRotate( event ) {
  289. //console.log( 'handleMouseMoveRotate' );
  290. rotateEnd.set( event.clientX, event.clientY );
  291. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  292. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  293. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  294. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  295. rotateStart.copy( rotateEnd );
  296. scope.update();
  297. }
  298. function handleMouseMoveDolly( event ) {
  299. //console.log( 'handleMouseMoveDolly' );
  300. dollyEnd.set( event.clientX, event.clientY );
  301. dollyDelta.subVectors( dollyEnd, dollyStart );
  302. if ( dollyDelta.y > 0 ) {
  303. dollyIn( getZoomScale() );
  304. } else if ( dollyDelta.y < 0 ) {
  305. dollyOut( getZoomScale() );
  306. }
  307. dollyStart.copy( dollyEnd );
  308. scope.update();
  309. }
  310. function handleMouseMovePan( event ) {
  311. //console.log( 'handleMouseMovePan' );
  312. panEnd.set( event.clientX, event.clientY );
  313. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  314. pan( panDelta.x, panDelta.y );
  315. panStart.copy( panEnd );
  316. scope.update();
  317. }
  318. function handleMouseUp( event ) {
  319. // console.log( 'handleMouseUp' );
  320. }
  321. function handleMouseWheel( event ) {
  322. // console.log( 'handleMouseWheel' );
  323. if ( event.deltaY < 0 ) {
  324. dollyOut( getZoomScale() );
  325. } else if ( event.deltaY > 0 ) {
  326. dollyIn( getZoomScale() );
  327. }
  328. scope.update();
  329. }
  330. function handleKeyDown( event ) {
  331. //console.log( 'handleKeyDown' );
  332. switch ( event.keyCode ) {
  333. case scope.keys.UP:
  334. pan( 0, scope.keyPanSpeed );
  335. scope.update();
  336. break;
  337. case scope.keys.BOTTOM:
  338. pan( 0, - scope.keyPanSpeed );
  339. scope.update();
  340. break;
  341. case scope.keys.LEFT:
  342. pan( scope.keyPanSpeed, 0 );
  343. scope.update();
  344. break;
  345. case scope.keys.RIGHT:
  346. pan( - scope.keyPanSpeed, 0 );
  347. scope.update();
  348. break;
  349. }
  350. }
  351. function handleTouchStartRotate( event ) {
  352. //console.log( 'handleTouchStartRotate' );
  353. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  354. }
  355. function handleTouchStartDollyPan( event ) {
  356. //console.log( 'handleTouchStartDollyPan' );
  357. if ( scope.enableZoom ) {
  358. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  359. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  360. var distance = Math.sqrt( dx * dx + dy * dy );
  361. dollyStart.set( 0, distance );
  362. }
  363. if ( scope.enablePan ) {
  364. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  365. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  366. panStart.set( x, y );
  367. }
  368. }
  369. function handleTouchMoveRotate( event ) {
  370. //console.log( 'handleTouchMoveRotate' );
  371. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  372. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  373. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  374. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  375. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  376. rotateStart.copy( rotateEnd );
  377. scope.update();
  378. }
  379. function handleTouchMoveDollyPan( event ) {
  380. //console.log( 'handleTouchMoveDollyPan' );
  381. if ( scope.enableZoom ) {
  382. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  383. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  384. var distance = Math.sqrt( dx * dx + dy * dy );
  385. dollyEnd.set( 0, distance );
  386. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  387. dollyIn( dollyDelta.y );
  388. dollyStart.copy( dollyEnd );
  389. }
  390. if ( scope.enablePan ) {
  391. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  392. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  393. panEnd.set( x, y );
  394. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  395. pan( panDelta.x, panDelta.y );
  396. panStart.copy( panEnd );
  397. }
  398. scope.update();
  399. }
  400. function handleTouchEnd( event ) {
  401. //console.log( 'handleTouchEnd' );
  402. }
  403. //
  404. // event handlers - FSM: listen for events and reset state
  405. //
  406. function onMouseDown( event ) {
  407. if ( scope.enabled === false ) return;
  408. event.preventDefault();
  409. var newState = scope.getStateForMouseDown( event );
  410. switch ( newState ) {
  411. case STATE.ROTATE:
  412. if ( scope.enableRotate === false ) return;
  413. handleMouseDownRotate( event );
  414. break;
  415. case STATE.DOLLY:
  416. if ( scope.enableZoom === false ) return;
  417. handleMouseDownDolly( event );
  418. break;
  419. case STATE.PAN:
  420. if ( scope.enablePan === false ) return;
  421. handleMouseDownPan( event );
  422. break;
  423. default: // STATE.NONE
  424. return;
  425. }
  426. state = newState;
  427. document.addEventListener( 'mousemove', onMouseMove, false );
  428. document.addEventListener( 'mouseup', onMouseUp, false );
  429. scope.dispatchEvent( startEvent );
  430. }
  431. function onMouseMove( event ) {
  432. if ( scope.enabled === false ) return;
  433. event.preventDefault();
  434. switch ( state ) {
  435. case STATE.ROTATE:
  436. if ( scope.enableRotate === false ) return;
  437. handleMouseMoveRotate( event );
  438. break;
  439. case STATE.DOLLY:
  440. if ( scope.enableZoom === false ) return;
  441. handleMouseMoveDolly( event );
  442. break;
  443. case STATE.PAN:
  444. if ( scope.enablePan === false ) return;
  445. handleMouseMovePan( event );
  446. break;
  447. }
  448. }
  449. function onMouseUp( event ) {
  450. if ( scope.enabled === false ) return;
  451. handleMouseUp( event );
  452. document.removeEventListener( 'mousemove', onMouseMove, false );
  453. document.removeEventListener( 'mouseup', onMouseUp, false );
  454. scope.dispatchEvent( endEvent );
  455. state = STATE.NONE;
  456. }
  457. function onMouseWheel( event ) {
  458. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  459. event.preventDefault();
  460. event.stopPropagation();
  461. scope.dispatchEvent( startEvent );
  462. handleMouseWheel( event );
  463. scope.dispatchEvent( endEvent );
  464. }
  465. function onKeyDown( event ) {
  466. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  467. handleKeyDown( event );
  468. }
  469. function onTouchStart( event ) {
  470. if ( scope.enabled === false ) return;
  471. event.preventDefault();
  472. switch ( event.touches.length ) {
  473. case 1: // one-fingered touch: rotate
  474. if ( scope.enableRotate === false ) return;
  475. handleTouchStartRotate( event );
  476. state = STATE.TOUCH_ROTATE;
  477. break;
  478. case 2: // two-fingered touch: dolly-pan
  479. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  480. handleTouchStartDollyPan( event );
  481. state = STATE.TOUCH_DOLLY_PAN;
  482. break;
  483. default:
  484. state = STATE.NONE;
  485. }
  486. if ( state !== STATE.NONE ) {
  487. scope.dispatchEvent( startEvent );
  488. }
  489. }
  490. function onTouchMove( event ) {
  491. if ( scope.enabled === false ) return;
  492. event.preventDefault();
  493. event.stopPropagation();
  494. switch ( event.touches.length ) {
  495. case 1: // one-fingered touch: rotate
  496. if ( scope.enableRotate === false ) return;
  497. if ( state !== STATE.TOUCH_ROTATE ) return; // is this needed?
  498. handleTouchMoveRotate( event );
  499. break;
  500. case 2: // two-fingered touch: dolly-pan
  501. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  502. if ( state !== STATE.TOUCH_DOLLY_PAN ) return; // is this needed?
  503. handleTouchMoveDollyPan( 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. if ( scope.enabled === false ) return;
  517. event.preventDefault();
  518. }
  519. //
  520. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  521. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  522. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  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.enableDamping;
  584. },
  585. set: function ( value ) {
  586. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  587. this.enableDamping = ! value;
  588. }
  589. },
  590. dynamicDampingFactor: {
  591. get: function () {
  592. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  593. return this.dampingFactor;
  594. },
  595. set: function ( value ) {
  596. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  597. this.dampingFactor = value;
  598. }
  599. }
  600. } );