OrbitControls.js 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. /*global THREE, console */
  9. ( function () {
  10. function OrbitConstraint ( object ) {
  11. this.object = object;
  12. // "target" sets the location of focus, where the object orbits around
  13. // and where it pans with respect to.
  14. this.target = new THREE.Vector3();
  15. // Limits to how far you can dolly in and out ( PerspectiveCamera only )
  16. this.minDistance = 0;
  17. this.maxDistance = Infinity;
  18. // Limits to how far you can zoom in and out ( OrthographicCamera only )
  19. this.minZoom = 0;
  20. this.maxZoom = Infinity;
  21. // How far you can orbit vertically, upper and lower limits.
  22. // Range is 0 to Math.PI radians.
  23. this.minPolarAngle = 0; // radians
  24. this.maxPolarAngle = Math.PI; // radians
  25. // How far you can orbit horizontally, upper and lower limits.
  26. // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
  27. this.minAzimuthAngle = - Infinity; // radians
  28. this.maxAzimuthAngle = Infinity; // radians
  29. // Set to true to enable damping (inertia)
  30. // If damping is enabled, you must call controls.update() in your animation loop
  31. this.enableDamping = false;
  32. this.dampingFactor = 0.25;
  33. ////////////
  34. // internals
  35. var scope = this;
  36. var EPS = 0.000001;
  37. // Current position in spherical coordinate system.
  38. var theta;
  39. var phi;
  40. // Pending changes
  41. var phiDelta = 0;
  42. var thetaDelta = 0;
  43. var scale = 1;
  44. var panOffset = new THREE.Vector3();
  45. var zoomChanged = false;
  46. // API
  47. this.getPolarAngle = function () {
  48. return phi;
  49. };
  50. this.getAzimuthalAngle = function () {
  51. return theta;
  52. };
  53. this.rotateLeft = function ( angle ) {
  54. thetaDelta -= angle;
  55. };
  56. this.rotateUp = function ( angle ) {
  57. phiDelta -= angle;
  58. };
  59. // pass in distance in world space to move left
  60. this.panLeft = function() {
  61. var v = new THREE.Vector3();
  62. return function panLeft ( distance ) {
  63. var te = this.object.matrix.elements;
  64. // get X column of matrix
  65. v.set( te[ 0 ], te[ 1 ], te[ 2 ] );
  66. v.multiplyScalar( - distance );
  67. panOffset.add( v );
  68. };
  69. }();
  70. // pass in distance in world space to move up
  71. this.panUp = function() {
  72. var v = new THREE.Vector3();
  73. return function panUp ( distance ) {
  74. var te = this.object.matrix.elements;
  75. // get Y column of matrix
  76. v.set( te[ 4 ], te[ 5 ], te[ 6 ] );
  77. v.multiplyScalar( distance );
  78. panOffset.add( v );
  79. };
  80. }();
  81. // pass in x,y of change desired in pixel space,
  82. // right and down are positive
  83. this.pan = function ( deltaX, deltaY, screenWidth, screenHeight ) {
  84. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  85. // perspective
  86. var position = scope.object.position;
  87. var offset = position.clone().sub( scope.target );
  88. var targetDistance = offset.length();
  89. // half of the fov is center to top of screen
  90. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  91. // we actually don't use screenWidth, since perspective camera is fixed to screen height
  92. scope.panLeft( 2 * deltaX * targetDistance / screenHeight );
  93. scope.panUp( 2 * deltaY * targetDistance / screenHeight );
  94. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  95. // orthographic
  96. scope.panLeft( deltaX * ( scope.object.right - scope.object.left ) / screenWidth );
  97. scope.panUp( deltaY * ( scope.object.top - scope.object.bottom ) / screenHeight );
  98. } else {
  99. // camera neither orthographic or perspective
  100. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  101. }
  102. };
  103. this.dollyIn = function ( dollyScale ) {
  104. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  105. scale /= dollyScale;
  106. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  107. scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom * dollyScale ) );
  108. scope.object.updateProjectionMatrix();
  109. zoomChanged = true;
  110. } else {
  111. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  112. }
  113. };
  114. this.dollyOut = function ( dollyScale ) {
  115. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  116. scale *= dollyScale;
  117. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  118. scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / dollyScale ) );
  119. scope.object.updateProjectionMatrix();
  120. zoomChanged = true;
  121. } else {
  122. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  123. }
  124. };
  125. this.update = function() {
  126. var offset = new THREE.Vector3();
  127. // so camera.up is the orbit axis
  128. var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
  129. var quatInverse = quat.clone().inverse();
  130. var lastPosition = new THREE.Vector3();
  131. var lastQuaternion = new THREE.Quaternion();
  132. return function () {
  133. var position = this.object.position;
  134. offset.copy( position ).sub( this.target );
  135. // rotate offset to "y-axis-is-up" space
  136. offset.applyQuaternion( quat );
  137. // angle from z-axis around y-axis
  138. theta = Math.atan2( offset.x, offset.z );
  139. // angle from y-axis
  140. phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y );
  141. theta += thetaDelta;
  142. phi += phiDelta;
  143. // restrict theta to be between desired limits
  144. theta = Math.max( this.minAzimuthAngle, Math.min( this.maxAzimuthAngle, theta ) );
  145. // restrict phi to be between desired limits
  146. phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, phi ) );
  147. // restrict phi to be betwee EPS and PI-EPS
  148. phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );
  149. var radius = offset.length() * scale;
  150. // restrict radius to be between desired limits
  151. radius = Math.max( this.minDistance, Math.min( this.maxDistance, radius ) );
  152. // move target to panned location
  153. this.target.add( panOffset );
  154. offset.x = radius * Math.sin( phi ) * Math.sin( theta );
  155. offset.y = radius * Math.cos( phi );
  156. offset.z = radius * Math.sin( phi ) * Math.cos( theta );
  157. // rotate offset back to "camera-up-vector-is-up" space
  158. offset.applyQuaternion( quatInverse );
  159. position.copy( this.target ).add( offset );
  160. this.object.lookAt( this.target );
  161. if ( this.enableDamping === true ) {
  162. thetaDelta *= ( 1 - this.dampingFactor );
  163. phiDelta *= ( 1 - this.dampingFactor );
  164. } else {
  165. thetaDelta = 0;
  166. phiDelta = 0;
  167. }
  168. scale = 1;
  169. panOffset.set( 0, 0, 0 );
  170. // update condition is:
  171. // min(camera displacement, camera rotation in radians)^2 > EPS
  172. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  173. if ( zoomChanged ||
  174. lastPosition.distanceToSquared( this.object.position ) > EPS ||
  175. 8 * ( 1 - lastQuaternion.dot( this.object.quaternion ) ) > EPS ) {
  176. lastPosition.copy( this.object.position );
  177. lastQuaternion.copy( this.object.quaternion );
  178. zoomChanged = false;
  179. return true;
  180. }
  181. return false;
  182. };
  183. }();
  184. };
  185. // This set of controls performs orbiting, dollying (zooming), and panning. It maintains
  186. // the "up" direction as +Y, unlike the TrackballControls. Touch on tablet and phones is
  187. // supported.
  188. //
  189. // Orbit - left mouse / touch: one finger move
  190. // Zoom - middle mouse, or mousewheel / touch: two finger spread or squish
  191. // Pan - right mouse, or arrow keys / touch: three finter swipe
  192. THREE.OrbitControls = function ( object, domElement ) {
  193. var constraint = new OrbitConstraint( object );
  194. this.domElement = ( domElement !== undefined ) ? domElement : document;
  195. // API
  196. Object.defineProperty( this, 'constraint', {
  197. get: function() {
  198. return constraint;
  199. }
  200. } );
  201. this.getPolarAngle = function () {
  202. return constraint.getPolarAngle();
  203. };
  204. this.getAzimuthalAngle = function () {
  205. return constraint.getAzimuthalAngle();
  206. };
  207. // Set to false to disable this control
  208. this.enabled = true;
  209. // center is old, deprecated; use "target" instead
  210. this.center = this.target;
  211. // This option actually enables dollying in and out; left as "zoom" for
  212. // backwards compatibility.
  213. // Set to false to disable zooming
  214. this.enableZoom = true;
  215. this.zoomSpeed = 1.0;
  216. // Set to false to disable rotating
  217. this.enableRotate = true;
  218. this.rotateSpeed = 1.0;
  219. // Set to false to disable panning
  220. this.enablePan = true;
  221. this.keyPanSpeed = 7.0; // pixels moved per arrow key push
  222. // Set to true to automatically rotate around the target
  223. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  224. this.autoRotate = false;
  225. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  226. // Set to false to disable use of the keys
  227. this.enableKeys = true;
  228. // The four arrow keys
  229. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  230. // Mouse buttons
  231. this.mouseButtons = { ORBIT: THREE.MOUSE.LEFT, ZOOM: THREE.MOUSE.MIDDLE, PAN: THREE.MOUSE.RIGHT };
  232. ////////////
  233. // internals
  234. var scope = this;
  235. var rotateStart = new THREE.Vector2();
  236. var rotateEnd = new THREE.Vector2();
  237. var rotateDelta = new THREE.Vector2();
  238. var panStart = new THREE.Vector2();
  239. var panEnd = new THREE.Vector2();
  240. var panDelta = new THREE.Vector2();
  241. var dollyStart = new THREE.Vector2();
  242. var dollyEnd = new THREE.Vector2();
  243. var dollyDelta = new THREE.Vector2();
  244. var STATE = { NONE : - 1, ROTATE : 0, DOLLY : 1, PAN : 2, TOUCH_ROTATE : 3, TOUCH_DOLLY : 4, TOUCH_PAN : 5 };
  245. var state = STATE.NONE;
  246. // for reset
  247. this.target0 = this.target.clone();
  248. this.position0 = this.object.position.clone();
  249. this.zoom0 = this.object.zoom;
  250. // events
  251. var changeEvent = { type: 'change' };
  252. var startEvent = { type: 'start' };
  253. var endEvent = { type: 'end' };
  254. // pass in x,y of change desired in pixel space,
  255. // right and down are positive
  256. function pan( deltaX, deltaY ) {
  257. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  258. constraint.pan( deltaX, deltaY, element.clientWidth, element.clientHeight );
  259. }
  260. this.update = function () {
  261. if ( this.autoRotate && state === STATE.NONE ) {
  262. constraint.rotateLeft( getAutoRotationAngle() );
  263. }
  264. if ( constraint.update() === true ) {
  265. this.dispatchEvent( changeEvent );
  266. }
  267. };
  268. this.reset = function () {
  269. state = STATE.NONE;
  270. this.target.copy( this.target0 );
  271. this.object.position.copy( this.position0 );
  272. this.object.zoom = this.zoom0;
  273. this.object.updateProjectionMatrix();
  274. this.dispatchEvent( changeEvent );
  275. this.update();
  276. };
  277. function getAutoRotationAngle() {
  278. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  279. }
  280. function getZoomScale() {
  281. return Math.pow( 0.95, scope.zoomSpeed );
  282. }
  283. function onMouseDown( event ) {
  284. if ( scope.enabled === false ) return;
  285. event.preventDefault();
  286. if ( event.button === scope.mouseButtons.ORBIT ) {
  287. if ( scope.enableRotate === false ) return;
  288. state = STATE.ROTATE;
  289. rotateStart.set( event.clientX, event.clientY );
  290. } else if ( event.button === scope.mouseButtons.ZOOM ) {
  291. if ( scope.enableZoom === false ) return;
  292. state = STATE.DOLLY;
  293. dollyStart.set( event.clientX, event.clientY );
  294. } else if ( event.button === scope.mouseButtons.PAN ) {
  295. if ( scope.enablePan === false ) return;
  296. state = STATE.PAN;
  297. panStart.set( event.clientX, event.clientY );
  298. }
  299. if ( state !== STATE.NONE ) {
  300. document.addEventListener( 'mousemove', onMouseMove, false );
  301. document.addEventListener( 'mouseup', onMouseUp, false );
  302. scope.dispatchEvent( startEvent );
  303. }
  304. }
  305. function onMouseMove( event ) {
  306. if ( scope.enabled === false ) return;
  307. event.preventDefault();
  308. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  309. if ( state === STATE.ROTATE ) {
  310. if ( scope.enableRotate === false ) return;
  311. rotateEnd.set( event.clientX, event.clientY );
  312. rotateDelta.subVectors( rotateEnd, rotateStart );
  313. // rotating across whole screen goes 360 degrees around
  314. constraint.rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  315. // rotating up and down along whole screen attempts to go 360, but limited to 180
  316. constraint.rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  317. rotateStart.copy( rotateEnd );
  318. } else if ( state === STATE.DOLLY ) {
  319. if ( scope.enableZoom === false ) return;
  320. dollyEnd.set( event.clientX, event.clientY );
  321. dollyDelta.subVectors( dollyEnd, dollyStart );
  322. if ( dollyDelta.y > 0 ) {
  323. constraint.dollyIn( getZoomScale() );
  324. } else if ( dollyDelta.y < 0 ) {
  325. constraint.dollyOut( getZoomScale() );
  326. }
  327. dollyStart.copy( dollyEnd );
  328. } else if ( state === STATE.PAN ) {
  329. if ( scope.enablePan === false ) return;
  330. panEnd.set( event.clientX, event.clientY );
  331. panDelta.subVectors( panEnd, panStart );
  332. pan( panDelta.x, panDelta.y );
  333. panStart.copy( panEnd );
  334. }
  335. if ( state !== STATE.NONE ) scope.update();
  336. }
  337. function onMouseUp( /* event */ ) {
  338. if ( scope.enabled === false ) return;
  339. document.removeEventListener( 'mousemove', onMouseMove, false );
  340. document.removeEventListener( 'mouseup', onMouseUp, false );
  341. scope.dispatchEvent( endEvent );
  342. state = STATE.NONE;
  343. }
  344. function onMouseWheel( event ) {
  345. if ( scope.enabled === false || scope.enableZoom === false || state !== STATE.NONE ) return;
  346. event.preventDefault();
  347. event.stopPropagation();
  348. var delta = 0;
  349. if ( event.wheelDelta !== undefined ) {
  350. // WebKit / Opera / Explorer 9
  351. delta = event.wheelDelta;
  352. } else if ( event.detail !== undefined ) {
  353. // Firefox
  354. delta = - event.detail;
  355. }
  356. if ( delta > 0 ) {
  357. constraint.dollyOut( getZoomScale() );
  358. } else if ( delta < 0 ) {
  359. constraint.dollyIn( getZoomScale() );
  360. }
  361. scope.update();
  362. scope.dispatchEvent( startEvent );
  363. scope.dispatchEvent( endEvent );
  364. }
  365. function onKeyDown( event ) {
  366. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  367. switch ( event.keyCode ) {
  368. case scope.keys.UP:
  369. pan( 0, scope.keyPanSpeed );
  370. scope.update();
  371. break;
  372. case scope.keys.BOTTOM:
  373. pan( 0, - scope.keyPanSpeed );
  374. scope.update();
  375. break;
  376. case scope.keys.LEFT:
  377. pan( scope.keyPanSpeed, 0 );
  378. scope.update();
  379. break;
  380. case scope.keys.RIGHT:
  381. pan( - scope.keyPanSpeed, 0 );
  382. scope.update();
  383. break;
  384. }
  385. }
  386. function touchstart( event ) {
  387. if ( scope.enabled === false ) return;
  388. switch ( event.touches.length ) {
  389. case 1: // one-fingered touch: rotate
  390. if ( scope.enableRotate === false ) return;
  391. state = STATE.TOUCH_ROTATE;
  392. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  393. break;
  394. case 2: // two-fingered touch: dolly
  395. if ( scope.enableZoom === false ) return;
  396. state = STATE.TOUCH_DOLLY;
  397. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  398. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  399. var distance = Math.sqrt( dx * dx + dy * dy );
  400. dollyStart.set( 0, distance );
  401. break;
  402. case 3: // three-fingered touch: pan
  403. if ( scope.enablePan === false ) return;
  404. state = STATE.TOUCH_PAN;
  405. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  406. break;
  407. default:
  408. state = STATE.NONE;
  409. }
  410. if ( state !== STATE.NONE ) scope.dispatchEvent( startEvent );
  411. }
  412. function touchmove( event ) {
  413. if ( scope.enabled === false ) return;
  414. event.preventDefault();
  415. event.stopPropagation();
  416. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  417. switch ( event.touches.length ) {
  418. case 1: // one-fingered touch: rotate
  419. if ( scope.enableRotate === false ) return;
  420. if ( state !== STATE.TOUCH_ROTATE ) return;
  421. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  422. rotateDelta.subVectors( rotateEnd, rotateStart );
  423. // rotating across whole screen goes 360 degrees around
  424. constraint.rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  425. // rotating up and down along whole screen attempts to go 360, but limited to 180
  426. constraint.rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  427. rotateStart.copy( rotateEnd );
  428. scope.update();
  429. break;
  430. case 2: // two-fingered touch: dolly
  431. if ( scope.enableZoom === false ) return;
  432. if ( state !== STATE.TOUCH_DOLLY ) return;
  433. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  434. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  435. var distance = Math.sqrt( dx * dx + dy * dy );
  436. dollyEnd.set( 0, distance );
  437. dollyDelta.subVectors( dollyEnd, dollyStart );
  438. if ( dollyDelta.y > 0 ) {
  439. constraint.dollyOut( getZoomScale() );
  440. } else if ( dollyDelta.y < 0 ) {
  441. constraint.dollyIn( getZoomScale() );
  442. }
  443. dollyStart.copy( dollyEnd );
  444. scope.update();
  445. break;
  446. case 3: // three-fingered touch: pan
  447. if ( scope.enablePan === false ) return;
  448. if ( state !== STATE.TOUCH_PAN ) return;
  449. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  450. panDelta.subVectors( panEnd, panStart );
  451. pan( panDelta.x, panDelta.y );
  452. panStart.copy( panEnd );
  453. scope.update();
  454. break;
  455. default:
  456. state = STATE.NONE;
  457. }
  458. }
  459. function touchend( /* event */ ) {
  460. if ( scope.enabled === false ) return;
  461. scope.dispatchEvent( endEvent );
  462. state = STATE.NONE;
  463. }
  464. function contextmenu( event ) {
  465. event.preventDefault();
  466. }
  467. this.dispose = function() {
  468. this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
  469. this.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  470. this.domElement.removeEventListener( 'mousewheel', onMouseWheel, false );
  471. this.domElement.removeEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox
  472. this.domElement.removeEventListener( 'touchstart', touchstart, false );
  473. this.domElement.removeEventListener( 'touchend', touchend, false );
  474. this.domElement.removeEventListener( 'touchmove', touchmove, false );
  475. document.removeEventListener( 'mousemove', onMouseMove, false );
  476. document.removeEventListener( 'mouseup', onMouseUp, false );
  477. window.removeEventListener( 'keydown', onKeyDown, false );
  478. }
  479. this.domElement.addEventListener( 'contextmenu', contextmenu, false );
  480. this.domElement.addEventListener( 'mousedown', onMouseDown, false );
  481. this.domElement.addEventListener( 'mousewheel', onMouseWheel, false );
  482. this.domElement.addEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox
  483. this.domElement.addEventListener( 'touchstart', touchstart, false );
  484. this.domElement.addEventListener( 'touchend', touchend, false );
  485. this.domElement.addEventListener( 'touchmove', touchmove, false );
  486. window.addEventListener( 'keydown', onKeyDown, false );
  487. // force an update at start
  488. this.update();
  489. };
  490. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  491. THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;
  492. Object.defineProperties( THREE.OrbitControls.prototype, {
  493. object: {
  494. get: function () {
  495. return this.constraint.object;
  496. }
  497. },
  498. target: {
  499. get: function () {
  500. return this.constraint.target;
  501. },
  502. set: function ( value ) {
  503. console.warn( 'THREE.OrbitControls: target is now immutable. Use target.set() instead.' );
  504. this.constraint.target.copy( value );
  505. }
  506. },
  507. minDistance : {
  508. get: function () {
  509. return this.constraint.minDistance;
  510. },
  511. set: function ( value ) {
  512. this.constraint.minDistance = value;
  513. }
  514. },
  515. maxDistance : {
  516. get: function () {
  517. return this.constraint.maxDistance;
  518. },
  519. set: function ( value ) {
  520. this.constraint.maxDistance = value;
  521. }
  522. },
  523. minZoom : {
  524. get: function () {
  525. return this.constraint.minZoom;
  526. },
  527. set: function ( value ) {
  528. this.constraint.minZoom = value;
  529. }
  530. },
  531. maxZoom : {
  532. get: function () {
  533. return this.constraint.maxZoom;
  534. },
  535. set: function ( value ) {
  536. this.constraint.maxZoom = value;
  537. }
  538. },
  539. minPolarAngle : {
  540. get: function () {
  541. return this.constraint.minPolarAngle;
  542. },
  543. set: function ( value ) {
  544. this.constraint.minPolarAngle = value;
  545. }
  546. },
  547. maxPolarAngle : {
  548. get: function () {
  549. return this.constraint.maxPolarAngle;
  550. },
  551. set: function ( value ) {
  552. this.constraint.maxPolarAngle = value;
  553. }
  554. },
  555. minAzimuthAngle : {
  556. get: function () {
  557. return this.constraint.minAzimuthAngle;
  558. },
  559. set: function ( value ) {
  560. this.constraint.minAzimuthAngle = value;
  561. }
  562. },
  563. maxAzimuthAngle : {
  564. get: function () {
  565. return this.constraint.maxAzimuthAngle;
  566. },
  567. set: function ( value ) {
  568. this.constraint.maxAzimuthAngle = value;
  569. }
  570. },
  571. enableDamping : {
  572. get: function () {
  573. return this.constraint.enableDamping;
  574. },
  575. set: function ( value ) {
  576. this.constraint.enableDamping = value;
  577. }
  578. },
  579. dampingFactor : {
  580. get: function () {
  581. return this.constraint.dampingFactor;
  582. },
  583. set: function ( value ) {
  584. this.constraint.dampingFactor = value;
  585. }
  586. },
  587. // backward compatibility
  588. noZoom: {
  589. get: function () {
  590. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  591. return ! this.enableZoom;
  592. },
  593. set: function ( value ) {
  594. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  595. this.enableZoom = ! value;
  596. }
  597. },
  598. noRotate: {
  599. get: function () {
  600. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  601. return ! this.enableRotate;
  602. },
  603. set: function ( value ) {
  604. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  605. this.enableRotate = ! value;
  606. }
  607. },
  608. noPan: {
  609. get: function () {
  610. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  611. return ! this.enablePan;
  612. },
  613. set: function ( value ) {
  614. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  615. this.enablePan = ! value;
  616. }
  617. },
  618. noKeys: {
  619. get: function () {
  620. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  621. return ! this.enableKeys;
  622. },
  623. set: function ( value ) {
  624. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  625. this.enableKeys = ! value;
  626. }
  627. },
  628. staticMoving : {
  629. get: function () {
  630. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  631. return ! this.constraint.enableDamping;
  632. },
  633. set: function ( value ) {
  634. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  635. this.constraint.enableDamping = ! value;
  636. }
  637. },
  638. dynamicDampingFactor : {
  639. get: function () {
  640. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  641. return this.constraint.dampingFactor;
  642. },
  643. set: function ( value ) {
  644. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  645. this.constraint.dampingFactor = value;
  646. }
  647. }
  648. } );
  649. }() );