OrbitControls.js 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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 left mouse + ctrl/metaKey, 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. // The panning keys
  60. this.panKeys = [ 'ctrlKey', 'metaKey' ];
  61. // Mouse buttons
  62. this.mouseButtons = { LEFT: THREE.MOUSE.LEFT, MIDDLE: THREE.MOUSE.MIDDLE, RIGHT: THREE.MOUSE.RIGHT };
  63. // for reset
  64. this.target0 = this.target.clone();
  65. this.position0 = this.object.position.clone();
  66. this.zoom0 = this.object.zoom;
  67. //
  68. // public methods
  69. //
  70. this.getPolarAngle = function () {
  71. return spherical.phi;
  72. };
  73. this.getAzimuthalAngle = function () {
  74. return spherical.theta;
  75. };
  76. this.saveState = function () {
  77. scope.target0.copy( scope.target );
  78. scope.position0.copy( scope.object.position );
  79. scope.zoom0 = scope.object.zoom;
  80. };
  81. this.reset = function () {
  82. scope.target.copy( scope.target0 );
  83. scope.object.position.copy( scope.position0 );
  84. scope.object.zoom = scope.zoom0;
  85. scope.object.updateProjectionMatrix();
  86. scope.dispatchEvent( changeEvent );
  87. scope.update();
  88. state = STATE.NONE;
  89. };
  90. // this method is exposed, but perhaps it would be better if we can make it private...
  91. this.update = function () {
  92. var offset = new THREE.Vector3();
  93. // so camera.up is the orbit axis
  94. var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
  95. var quatInverse = quat.clone().inverse();
  96. var lastPosition = new THREE.Vector3();
  97. var lastQuaternion = new THREE.Quaternion();
  98. return function update() {
  99. var position = scope.object.position;
  100. offset.copy( position ).sub( scope.target );
  101. // rotate offset to "y-axis-is-up" space
  102. offset.applyQuaternion( quat );
  103. // angle from z-axis around y-axis
  104. spherical.setFromVector3( offset );
  105. if ( scope.autoRotate && state === STATE.NONE ) {
  106. rotateLeft( getAutoRotationAngle() );
  107. }
  108. spherical.theta += sphericalDelta.theta;
  109. spherical.phi += sphericalDelta.phi;
  110. // restrict theta to be between desired limits
  111. spherical.theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, spherical.theta ) );
  112. // restrict phi to be between desired limits
  113. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  114. spherical.makeSafe();
  115. spherical.radius *= scale;
  116. // restrict radius to be between desired limits
  117. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  118. // move target to panned location
  119. scope.target.add( panOffset );
  120. offset.setFromSpherical( spherical );
  121. // rotate offset back to "camera-up-vector-is-up" space
  122. offset.applyQuaternion( quatInverse );
  123. position.copy( scope.target ).add( offset );
  124. scope.object.lookAt( scope.target );
  125. if ( scope.enableDamping === true ) {
  126. sphericalDelta.theta *= ( 1 - scope.dampingFactor );
  127. sphericalDelta.phi *= ( 1 - scope.dampingFactor );
  128. panOffset.multiplyScalar( 1 - scope.dampingFactor );
  129. } else {
  130. sphericalDelta.set( 0, 0, 0 );
  131. panOffset.set( 0, 0, 0 );
  132. }
  133. scale = 1;
  134. // update condition is:
  135. // min(camera displacement, camera rotation in radians)^2 > EPS
  136. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  137. if ( zoomChanged ||
  138. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  139. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  140. scope.dispatchEvent( changeEvent );
  141. lastPosition.copy( scope.object.position );
  142. lastQuaternion.copy( scope.object.quaternion );
  143. zoomChanged = false;
  144. return true;
  145. }
  146. return false;
  147. };
  148. }();
  149. this.dispose = function () {
  150. scope.domElement.removeEventListener( 'contextmenu', onContextMenu, false );
  151. scope.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  152. scope.domElement.removeEventListener( 'wheel', onMouseWheel, false );
  153. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  154. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  155. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  156. document.removeEventListener( 'mousemove', onMouseMove, false );
  157. document.removeEventListener( 'mouseup', onMouseUp, false );
  158. window.removeEventListener( 'keydown', onKeyDown, false );
  159. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  160. };
  161. //
  162. // internals
  163. //
  164. var scope = this;
  165. var changeEvent = { type: 'change' };
  166. var startEvent = { type: 'start' };
  167. var endEvent = { type: 'end' };
  168. var STATE = { NONE: - 1, ROTATE: 0, DOLLY: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_DOLLY_PAN: 4 };
  169. var state = STATE.NONE;
  170. var EPS = 0.000001;
  171. // current position in spherical coordinates
  172. var spherical = new THREE.Spherical();
  173. var sphericalDelta = new THREE.Spherical();
  174. var scale = 1;
  175. var panOffset = new THREE.Vector3();
  176. var zoomChanged = false;
  177. var rotateStart = new THREE.Vector2();
  178. var rotateEnd = new THREE.Vector2();
  179. var rotateDelta = new THREE.Vector2();
  180. var panStart = new THREE.Vector2();
  181. var panEnd = new THREE.Vector2();
  182. var panDelta = new THREE.Vector2();
  183. var dollyStart = new THREE.Vector2();
  184. var dollyEnd = new THREE.Vector2();
  185. var dollyDelta = new THREE.Vector2();
  186. function getAutoRotationAngle() {
  187. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  188. }
  189. function getZoomScale() {
  190. return Math.pow( 0.95, scope.zoomSpeed );
  191. }
  192. function rotateLeft( angle ) {
  193. sphericalDelta.theta -= angle;
  194. }
  195. function rotateUp( angle ) {
  196. sphericalDelta.phi -= angle;
  197. }
  198. var panLeft = function () {
  199. var v = new THREE.Vector3();
  200. return function panLeft( distance, objectMatrix ) {
  201. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  202. v.multiplyScalar( - distance );
  203. panOffset.add( v );
  204. };
  205. }();
  206. var panUp = function () {
  207. var v = new THREE.Vector3();
  208. return function panUp( distance, objectMatrix ) {
  209. if ( scope.screenSpacePanning === true ) {
  210. v.setFromMatrixColumn( objectMatrix, 1 );
  211. } else {
  212. v.setFromMatrixColumn( objectMatrix, 0 );
  213. v.crossVectors( scope.object.up, v );
  214. }
  215. v.multiplyScalar( distance );
  216. panOffset.add( v );
  217. };
  218. }();
  219. // deltaX and deltaY are in pixels; right and down are positive
  220. var pan = function () {
  221. var offset = new THREE.Vector3();
  222. return function pan( deltaX, deltaY ) {
  223. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  224. if ( scope.object.isPerspectiveCamera ) {
  225. // perspective
  226. var position = scope.object.position;
  227. offset.copy( position ).sub( scope.target );
  228. var targetDistance = offset.length();
  229. // half of the fov is center to top of screen
  230. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  231. // we use only clientHeight here so aspect ratio does not distort speed
  232. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  233. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  234. } else if ( scope.object.isOrthographicCamera ) {
  235. // orthographic
  236. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  237. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  238. } else {
  239. // camera neither orthographic nor perspective
  240. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  241. scope.enablePan = false;
  242. }
  243. };
  244. }();
  245. function dollyIn( dollyScale ) {
  246. if ( scope.object.isPerspectiveCamera ) {
  247. scale /= dollyScale;
  248. } else if ( scope.object.isOrthographicCamera ) {
  249. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  250. scope.object.updateProjectionMatrix();
  251. zoomChanged = true;
  252. } else {
  253. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  254. scope.enableZoom = false;
  255. }
  256. }
  257. function dollyOut( dollyScale ) {
  258. if ( scope.object.isPerspectiveCamera ) {
  259. scale *= dollyScale;
  260. } else if ( scope.object.isOrthographicCamera ) {
  261. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  262. scope.object.updateProjectionMatrix();
  263. zoomChanged = true;
  264. } else {
  265. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  266. scope.enableZoom = false;
  267. }
  268. }
  269. function isPanEvent( event ) {
  270. var hasKey = function ( key ) {
  271. return event[ key ];
  272. };
  273. return scope.panKeys.some( hasKey );
  274. }
  275. //
  276. // event callbacks - update the object state
  277. //
  278. function handleMouseDownRotate( event ) {
  279. //console.log( 'handleMouseDownRotate' );
  280. rotateStart.set( event.clientX, event.clientY );
  281. }
  282. function handleMouseDownDolly( event ) {
  283. //console.log( 'handleMouseDownDolly' );
  284. dollyStart.set( event.clientX, event.clientY );
  285. }
  286. function handleMouseDownPan( event ) {
  287. //console.log( 'handleMouseDownPan' );
  288. panStart.set( event.clientX, event.clientY );
  289. }
  290. function handleMouseMoveRotate( event ) {
  291. //console.log( 'handleMouseMoveRotate' );
  292. rotateEnd.set( event.clientX, event.clientY );
  293. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  294. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  295. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  296. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  297. rotateStart.copy( rotateEnd );
  298. scope.update();
  299. }
  300. function handleMouseMoveDolly( event ) {
  301. //console.log( 'handleMouseMoveDolly' );
  302. dollyEnd.set( event.clientX, event.clientY );
  303. dollyDelta.subVectors( dollyEnd, dollyStart );
  304. if ( dollyDelta.y > 0 ) {
  305. dollyIn( getZoomScale() );
  306. } else if ( dollyDelta.y < 0 ) {
  307. dollyOut( getZoomScale() );
  308. }
  309. dollyStart.copy( dollyEnd );
  310. scope.update();
  311. }
  312. function handleMouseMovePan( event ) {
  313. //console.log( 'handleMouseMovePan' );
  314. panEnd.set( event.clientX, event.clientY );
  315. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  316. pan( panDelta.x, panDelta.y );
  317. panStart.copy( panEnd );
  318. scope.update();
  319. }
  320. function handleMouseUp( event ) {
  321. // console.log( 'handleMouseUp' );
  322. }
  323. function handleMouseWheel( event ) {
  324. // console.log( 'handleMouseWheel' );
  325. if ( event.deltaY < 0 ) {
  326. dollyOut( getZoomScale() );
  327. } else if ( event.deltaY > 0 ) {
  328. dollyIn( getZoomScale() );
  329. }
  330. scope.update();
  331. }
  332. function handleKeyDown( event ) {
  333. //console.log( 'handleKeyDown' );
  334. switch ( event.keyCode ) {
  335. case scope.keys.UP:
  336. pan( 0, scope.keyPanSpeed );
  337. scope.update();
  338. break;
  339. case scope.keys.BOTTOM:
  340. pan( 0, - scope.keyPanSpeed );
  341. scope.update();
  342. break;
  343. case scope.keys.LEFT:
  344. pan( scope.keyPanSpeed, 0 );
  345. scope.update();
  346. break;
  347. case scope.keys.RIGHT:
  348. pan( - scope.keyPanSpeed, 0 );
  349. scope.update();
  350. break;
  351. }
  352. }
  353. function handleTouchStartRotate( event ) {
  354. //console.log( 'handleTouchStartRotate' );
  355. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  356. }
  357. function handleTouchStartDollyPan( event ) {
  358. //console.log( 'handleTouchStartDollyPan' );
  359. if ( scope.enableZoom ) {
  360. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  361. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  362. var distance = Math.sqrt( dx * dx + dy * dy );
  363. dollyStart.set( 0, distance );
  364. }
  365. if ( scope.enablePan ) {
  366. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  367. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  368. panStart.set( x, y );
  369. }
  370. }
  371. function handleTouchMoveRotate( event ) {
  372. //console.log( 'handleTouchMoveRotate' );
  373. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  374. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  375. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  376. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  377. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  378. rotateStart.copy( rotateEnd );
  379. scope.update();
  380. }
  381. function handleTouchMoveDollyPan( event ) {
  382. //console.log( 'handleTouchMoveDollyPan' );
  383. if ( scope.enableZoom ) {
  384. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  385. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  386. var distance = Math.sqrt( dx * dx + dy * dy );
  387. dollyEnd.set( 0, distance );
  388. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  389. dollyIn( dollyDelta.y );
  390. dollyStart.copy( dollyEnd );
  391. }
  392. if ( scope.enablePan ) {
  393. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  394. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  395. panEnd.set( x, y );
  396. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  397. pan( panDelta.x, panDelta.y );
  398. panStart.copy( panEnd );
  399. }
  400. scope.update();
  401. }
  402. function handleTouchEnd( event ) {
  403. //console.log( 'handleTouchEnd' );
  404. }
  405. //
  406. // event handlers - FSM: listen for events and reset state
  407. //
  408. function onMouseDown( event ) {
  409. if ( scope.enabled === false ) return;
  410. event.preventDefault();
  411. switch ( event.button ) {
  412. case scope.mouseButtons.LEFT:
  413. if ( isPanEvent( event ) ) {
  414. if ( scope.enablePan === false ) return;
  415. handleMouseDownPan( event );
  416. state = STATE.PAN;
  417. } else {
  418. if ( scope.enableRotate === false ) return;
  419. handleMouseDownRotate( event );
  420. state = STATE.ROTATE;
  421. }
  422. break;
  423. case scope.mouseButtons.MIDDLE:
  424. if ( scope.enableZoom === false ) return;
  425. handleMouseDownDolly( event );
  426. state = STATE.DOLLY;
  427. break;
  428. case scope.mouseButtons.RIGHT:
  429. if ( scope.enablePan === false ) return;
  430. handleMouseDownPan( event );
  431. state = STATE.PAN;
  432. break;
  433. }
  434. if ( state !== STATE.NONE ) {
  435. document.addEventListener( 'mousemove', onMouseMove, false );
  436. document.addEventListener( 'mouseup', onMouseUp, false );
  437. scope.dispatchEvent( startEvent );
  438. }
  439. }
  440. function onMouseMove( event ) {
  441. if ( scope.enabled === false ) return;
  442. event.preventDefault();
  443. switch ( state ) {
  444. case STATE.ROTATE:
  445. if ( scope.enableRotate === false ) return;
  446. handleMouseMoveRotate( event );
  447. break;
  448. case STATE.DOLLY:
  449. if ( scope.enableZoom === false ) return;
  450. handleMouseMoveDolly( event );
  451. break;
  452. case STATE.PAN:
  453. if ( scope.enablePan === false ) return;
  454. handleMouseMovePan( event );
  455. break;
  456. }
  457. }
  458. function onMouseUp( event ) {
  459. if ( scope.enabled === false ) return;
  460. handleMouseUp( event );
  461. document.removeEventListener( 'mousemove', onMouseMove, false );
  462. document.removeEventListener( 'mouseup', onMouseUp, false );
  463. scope.dispatchEvent( endEvent );
  464. state = STATE.NONE;
  465. }
  466. function onMouseWheel( event ) {
  467. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  468. event.preventDefault();
  469. event.stopPropagation();
  470. scope.dispatchEvent( startEvent );
  471. handleMouseWheel( event );
  472. scope.dispatchEvent( endEvent );
  473. }
  474. function onKeyDown( event ) {
  475. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  476. handleKeyDown( event );
  477. }
  478. function onTouchStart( event ) {
  479. if ( scope.enabled === false ) return;
  480. event.preventDefault();
  481. switch ( event.touches.length ) {
  482. case 1: // one-fingered touch: rotate
  483. if ( scope.enableRotate === false ) return;
  484. handleTouchStartRotate( event );
  485. state = STATE.TOUCH_ROTATE;
  486. break;
  487. case 2: // two-fingered touch: dolly-pan
  488. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  489. handleTouchStartDollyPan( event );
  490. state = STATE.TOUCH_DOLLY_PAN;
  491. break;
  492. default:
  493. state = STATE.NONE;
  494. }
  495. if ( state !== STATE.NONE ) {
  496. scope.dispatchEvent( startEvent );
  497. }
  498. }
  499. function onTouchMove( event ) {
  500. if ( scope.enabled === false ) return;
  501. event.preventDefault();
  502. event.stopPropagation();
  503. switch ( event.touches.length ) {
  504. case 1: // one-fingered touch: rotate
  505. if ( scope.enableRotate === false ) return;
  506. if ( state !== STATE.TOUCH_ROTATE ) return; // is this needed?
  507. handleTouchMoveRotate( event );
  508. break;
  509. case 2: // two-fingered touch: dolly-pan
  510. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  511. if ( state !== STATE.TOUCH_DOLLY_PAN ) return; // is this needed?
  512. handleTouchMoveDollyPan( event );
  513. break;
  514. default:
  515. state = STATE.NONE;
  516. }
  517. }
  518. function onTouchEnd( event ) {
  519. if ( scope.enabled === false ) return;
  520. handleTouchEnd( event );
  521. scope.dispatchEvent( endEvent );
  522. state = STATE.NONE;
  523. }
  524. function onContextMenu( event ) {
  525. if ( scope.enabled === false ) return;
  526. event.preventDefault();
  527. }
  528. //
  529. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  530. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  531. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  532. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  533. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  534. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  535. window.addEventListener( 'keydown', onKeyDown, false );
  536. // force an update at start
  537. this.update();
  538. };
  539. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  540. THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;
  541. Object.defineProperties( THREE.OrbitControls.prototype, {
  542. center: {
  543. get: function () {
  544. console.warn( 'THREE.OrbitControls: .center has been renamed to .target' );
  545. return this.target;
  546. }
  547. },
  548. // backward compatibility
  549. noZoom: {
  550. get: function () {
  551. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  552. return ! this.enableZoom;
  553. },
  554. set: function ( value ) {
  555. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  556. this.enableZoom = ! value;
  557. }
  558. },
  559. noRotate: {
  560. get: function () {
  561. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  562. return ! this.enableRotate;
  563. },
  564. set: function ( value ) {
  565. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  566. this.enableRotate = ! value;
  567. }
  568. },
  569. noPan: {
  570. get: function () {
  571. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  572. return ! this.enablePan;
  573. },
  574. set: function ( value ) {
  575. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  576. this.enablePan = ! value;
  577. }
  578. },
  579. noKeys: {
  580. get: function () {
  581. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  582. return ! this.enableKeys;
  583. },
  584. set: function ( value ) {
  585. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  586. this.enableKeys = ! value;
  587. }
  588. },
  589. staticMoving: {
  590. get: function () {
  591. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  592. return ! this.enableDamping;
  593. },
  594. set: function ( value ) {
  595. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  596. this.enableDamping = ! value;
  597. }
  598. },
  599. dynamicDampingFactor: {
  600. get: function () {
  601. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  602. return this.dampingFactor;
  603. },
  604. set: function ( value ) {
  605. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  606. this.dampingFactor = value;
  607. }
  608. }
  609. } );