index.ts 23 KB

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