OrbitControls.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  1. import {
  2. Controls,
  3. MOUSE,
  4. Quaternion,
  5. Spherical,
  6. TOUCH,
  7. Vector2,
  8. Vector3,
  9. Plane,
  10. Ray,
  11. MathUtils
  12. } from 'three';
  13. /**
  14. * Fires when the camera has been transformed by the controls.
  15. *
  16. * @event OrbitControls#change
  17. * @type {Object}
  18. */
  19. const _changeEvent = { type: 'change' };
  20. /**
  21. * Fires when an interaction was initiated.
  22. *
  23. * @event OrbitControls#start
  24. * @type {Object}
  25. */
  26. const _startEvent = { type: 'start' };
  27. /**
  28. * Fires when an interaction has finished.
  29. *
  30. * @event OrbitControls#end
  31. * @type {Object}
  32. */
  33. const _endEvent = { type: 'end' };
  34. const _ray = new Ray();
  35. const _plane = new Plane();
  36. const _TILT_LIMIT = Math.cos( 70 * MathUtils.DEG2RAD );
  37. const _v = new Vector3();
  38. const _twoPI = 2 * Math.PI;
  39. const _STATE = {
  40. NONE: - 1,
  41. ROTATE: 0,
  42. DOLLY: 1,
  43. PAN: 2,
  44. TOUCH_ROTATE: 3,
  45. TOUCH_PAN: 4,
  46. TOUCH_DOLLY_PAN: 5,
  47. TOUCH_DOLLY_ROTATE: 6
  48. };
  49. const _EPS = 0.000001;
  50. /**
  51. * Orbit controls allow the camera to orbit around a target.
  52. *
  53. * OrbitControls performs orbiting, dollying (zooming), and panning. Unlike {@link TrackballControls},
  54. * it maintains the "up" direction `object.up` (+Y by default).
  55. *
  56. * - Orbit: Left mouse / touch: one-finger move.
  57. * - Zoom: Middle mouse, or mousewheel / touch: two-finger spread or squish.
  58. * - Pan: Right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move.
  59. *
  60. * ```js
  61. * const controls = new OrbitControls( camera, renderer.domElement );
  62. *
  63. * // controls.update() must be called after any manual changes to the camera's transform
  64. * camera.position.set( 0, 20, 100 );
  65. * controls.update();
  66. *
  67. * function animate() {
  68. *
  69. * // required if controls.enableDamping or controls.autoRotate are set to true
  70. * controls.update();
  71. *
  72. * renderer.render( scene, camera );
  73. *
  74. * }
  75. * ```
  76. *
  77. * @augments Controls
  78. * @three_import import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  79. */
  80. class OrbitControls extends Controls {
  81. /**
  82. * Constructs a new controls instance.
  83. *
  84. * @param {Object3D} object - The object that is managed by the controls.
  85. * @param {?HTMLElement} domElement - The HTML element used for event listeners.
  86. */
  87. constructor( object, domElement = null ) {
  88. super( object, domElement );
  89. this.state = _STATE.NONE;
  90. /**
  91. * The focus point of the controls, the `object` orbits around this.
  92. * It can be updated manually at any point to change the focus of the controls.
  93. *
  94. * @type {Vector3}
  95. */
  96. this.target = new Vector3();
  97. /**
  98. * The focus point of the `minTargetRadius` and `maxTargetRadius` limits.
  99. * It can be updated manually at any point to change the center of interest
  100. * for the `target`.
  101. *
  102. * @type {Vector3}
  103. */
  104. this.cursor = new Vector3();
  105. /**
  106. * How far you can dolly in (perspective camera only).
  107. *
  108. * @type {number}
  109. * @default 0
  110. */
  111. this.minDistance = 0;
  112. /**
  113. * How far you can dolly out (perspective camera only).
  114. *
  115. * @type {number}
  116. * @default Infinity
  117. */
  118. this.maxDistance = Infinity;
  119. /**
  120. * How far you can zoom in (orthographic camera only).
  121. *
  122. * @type {number}
  123. * @default 0
  124. */
  125. this.minZoom = 0;
  126. /**
  127. * How far you can zoom out (orthographic camera only).
  128. *
  129. * @type {number}
  130. * @default Infinity
  131. */
  132. this.maxZoom = Infinity;
  133. /**
  134. * How close you can get the target to the 3D `cursor`.
  135. *
  136. * @type {number}
  137. * @default 0
  138. */
  139. this.minTargetRadius = 0;
  140. /**
  141. * How far you can move the target from the 3D `cursor`.
  142. *
  143. * @type {number}
  144. * @default Infinity
  145. */
  146. this.maxTargetRadius = Infinity;
  147. /**
  148. * How far you can orbit vertically, lower limit. Range is `[0, Math.PI]` radians.
  149. *
  150. * @type {number}
  151. * @default 0
  152. */
  153. this.minPolarAngle = 0;
  154. /**
  155. * How far you can orbit vertically, upper limit. Range is `[0, Math.PI]` radians.
  156. *
  157. * @type {number}
  158. * @default Math.PI
  159. */
  160. this.maxPolarAngle = Math.PI;
  161. /**
  162. * How far you can orbit horizontally, lower limit. If set, the interval `[ min, max ]`
  163. * must be a sub-interval of `[ - 2 PI, 2 PI ]`, with `( max - min < 2 PI )`.
  164. *
  165. * @type {number}
  166. * @default -Infinity
  167. */
  168. this.minAzimuthAngle = - Infinity;
  169. /**
  170. * How far you can orbit horizontally, upper limit. If set, the interval `[ min, max ]`
  171. * must be a sub-interval of `[ - 2 PI, 2 PI ]`, with `( max - min < 2 PI )`.
  172. *
  173. * @type {number}
  174. * @default -Infinity
  175. */
  176. this.maxAzimuthAngle = Infinity;
  177. /**
  178. * Set to `true` to enable damping (inertia), which can be used to give a sense of weight
  179. * to the controls. Note that if this is enabled, you must call `update()` in your animation
  180. * loop.
  181. *
  182. * @type {boolean}
  183. * @default false
  184. */
  185. this.enableDamping = false;
  186. /**
  187. * The damping inertia used if `enableDamping` is set to `true`.
  188. *
  189. * Note that for this to work, you must call `update()` in your animation loop.
  190. *
  191. * @type {number}
  192. * @default 0.05
  193. */
  194. this.dampingFactor = 0.05;
  195. /**
  196. * Enable or disable zooming (dollying) of the camera.
  197. *
  198. * @type {boolean}
  199. * @default true
  200. */
  201. this.enableZoom = true;
  202. /**
  203. * Speed of zooming / dollying.
  204. *
  205. * @type {number}
  206. * @default 1
  207. */
  208. this.zoomSpeed = 1.0;
  209. /**
  210. * Enable or disable horizontal and vertical rotation of the camera.
  211. *
  212. * Note that it is possible to disable a single axis by setting the min and max of the
  213. * `minPolarAngle` or `minAzimuthAngle` to the same value, which will cause the vertical
  214. * or horizontal rotation to be fixed at that value.
  215. *
  216. * @type {boolean}
  217. * @default true
  218. */
  219. this.enableRotate = true;
  220. /**
  221. * Speed of rotation.
  222. *
  223. * @type {number}
  224. * @default 1
  225. */
  226. this.rotateSpeed = 1.0;
  227. /**
  228. * How fast to rotate the camera when the keyboard is used.
  229. *
  230. * @type {number}
  231. * @default 1
  232. */
  233. this.keyRotateSpeed = 1.0;
  234. /**
  235. * Enable or disable camera panning.
  236. *
  237. * @type {boolean}
  238. * @default true
  239. */
  240. this.enablePan = true;
  241. /**
  242. * Speed of panning.
  243. *
  244. * @type {number}
  245. * @default 1
  246. */
  247. this.panSpeed = 1.0;
  248. /**
  249. * Defines how the camera's position is translated when panning. If `true`, the camera pans
  250. * in screen space. Otherwise, the camera pans in the plane orthogonal to the camera's up
  251. * direction.
  252. *
  253. * @type {boolean}
  254. * @default true
  255. */
  256. this.screenSpacePanning = true;
  257. /**
  258. * How fast to pan the camera when the keyboard is used in
  259. * pixels per keypress.
  260. *
  261. * @type {number}
  262. * @default 7
  263. */
  264. this.keyPanSpeed = 7.0;
  265. /**
  266. * Setting this property to `true` allows to zoom to the cursor's position.
  267. *
  268. * @type {boolean}
  269. * @default false
  270. */
  271. this.zoomToCursor = false;
  272. /**
  273. * Set to true to automatically rotate around the target
  274. *
  275. * Note that if this is enabled, you must call `update()` in your animation loop.
  276. * If you want the auto-rotate speed to be independent of the frame rate (the refresh
  277. * rate of the display), you must pass the time `deltaTime`, in seconds, to `update()`.
  278. *
  279. * @type {boolean}
  280. * @default false
  281. */
  282. this.autoRotate = false;
  283. /**
  284. * How fast to rotate around the target if `autoRotate` is `true`. The default equates to 30 seconds
  285. * per orbit at 60fps.
  286. *
  287. * Note that if `autoRotate` is enabled, you must call `update()` in your animation loop.
  288. *
  289. * @type {number}
  290. * @default 2
  291. */
  292. this.autoRotateSpeed = 2.0;
  293. /**
  294. * This object contains references to the keycodes for controlling camera panning.
  295. *
  296. * ```js
  297. * controls.keys = {
  298. * LEFT: 'ArrowLeft', //left arrow
  299. * UP: 'ArrowUp', // up arrow
  300. * RIGHT: 'ArrowRight', // right arrow
  301. * BOTTOM: 'ArrowDown' // down arrow
  302. * }
  303. * ```
  304. * @type {Object}
  305. */
  306. this.keys = { LEFT: 'ArrowLeft', UP: 'ArrowUp', RIGHT: 'ArrowRight', BOTTOM: 'ArrowDown' };
  307. /**
  308. * This object contains references to the mouse actions used by the controls.
  309. *
  310. * ```js
  311. * controls.mouseButtons = {
  312. * LEFT: THREE.MOUSE.ROTATE,
  313. * MIDDLE: THREE.MOUSE.DOLLY,
  314. * RIGHT: THREE.MOUSE.PAN
  315. * }
  316. * ```
  317. * @type {Object}
  318. */
  319. this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
  320. /**
  321. * This object contains references to the touch actions used by the controls.
  322. *
  323. * ```js
  324. * controls.mouseButtons = {
  325. * ONE: THREE.TOUCH.ROTATE,
  326. * TWO: THREE.TOUCH.DOLLY_PAN
  327. * }
  328. * ```
  329. * @type {Object}
  330. */
  331. this.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN };
  332. /**
  333. * Used internally by `saveState()` and `reset()`.
  334. *
  335. * @type {Vector3}
  336. */
  337. this.target0 = this.target.clone();
  338. /**
  339. * Used internally by `saveState()` and `reset()`.
  340. *
  341. * @type {Vector3}
  342. */
  343. this.position0 = this.object.position.clone();
  344. /**
  345. * Used internally by `saveState()` and `reset()`.
  346. *
  347. * @type {number}
  348. */
  349. this.zoom0 = this.object.zoom;
  350. // the target DOM element for key events
  351. this._domElementKeyEvents = null;
  352. // internals
  353. this._lastPosition = new Vector3();
  354. this._lastQuaternion = new Quaternion();
  355. this._lastTargetPosition = new Vector3();
  356. // so camera.up is the orbit axis
  357. this._quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
  358. this._quatInverse = this._quat.clone().invert();
  359. // current position in spherical coordinates
  360. this._spherical = new Spherical();
  361. this._sphericalDelta = new Spherical();
  362. this._scale = 1;
  363. this._panOffset = new Vector3();
  364. this._rotateStart = new Vector2();
  365. this._rotateEnd = new Vector2();
  366. this._rotateDelta = new Vector2();
  367. this._panStart = new Vector2();
  368. this._panEnd = new Vector2();
  369. this._panDelta = new Vector2();
  370. this._dollyStart = new Vector2();
  371. this._dollyEnd = new Vector2();
  372. this._dollyDelta = new Vector2();
  373. this._dollyDirection = new Vector3();
  374. this._mouse = new Vector2();
  375. this._performCursorZoom = false;
  376. this._pointers = [];
  377. this._pointerPositions = {};
  378. this._controlActive = false;
  379. // event listeners
  380. this._onPointerMove = onPointerMove.bind( this );
  381. this._onPointerDown = onPointerDown.bind( this );
  382. this._onPointerUp = onPointerUp.bind( this );
  383. this._onContextMenu = onContextMenu.bind( this );
  384. this._onMouseWheel = onMouseWheel.bind( this );
  385. this._onKeyDown = onKeyDown.bind( this );
  386. this._onTouchStart = onTouchStart.bind( this );
  387. this._onTouchMove = onTouchMove.bind( this );
  388. this._onMouseDown = onMouseDown.bind( this );
  389. this._onMouseMove = onMouseMove.bind( this );
  390. this._interceptControlDown = interceptControlDown.bind( this );
  391. this._interceptControlUp = interceptControlUp.bind( this );
  392. //
  393. if ( this.domElement !== null ) {
  394. this.connect( this.domElement );
  395. }
  396. this.update();
  397. }
  398. connect( element ) {
  399. super.connect( element );
  400. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  401. this.domElement.addEventListener( 'pointercancel', this._onPointerUp );
  402. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  403. this.domElement.addEventListener( 'wheel', this._onMouseWheel, { passive: false } );
  404. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  405. document.addEventListener( 'keydown', this._interceptControlDown, { passive: true, capture: true } );
  406. this.domElement.style.touchAction = 'none'; // disable touch scroll
  407. }
  408. disconnect() {
  409. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  410. this.domElement.ownerDocument.removeEventListener( 'pointermove', this._onPointerMove );
  411. this.domElement.ownerDocument.removeEventListener( 'pointerup', this._onPointerUp );
  412. this.domElement.removeEventListener( 'pointercancel', this._onPointerUp );
  413. this.domElement.removeEventListener( 'wheel', this._onMouseWheel );
  414. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  415. this.stopListenToKeyEvents();
  416. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  417. document.removeEventListener( 'keydown', this._interceptControlDown, { capture: true } );
  418. this.domElement.style.touchAction = 'auto';
  419. }
  420. dispose() {
  421. this.disconnect();
  422. }
  423. /**
  424. * Get the current vertical rotation, in radians.
  425. *
  426. * @return {number} The current vertical rotation, in radians.
  427. */
  428. getPolarAngle() {
  429. return this._spherical.phi;
  430. }
  431. /**
  432. * Get the current horizontal rotation, in radians.
  433. *
  434. * @return {number} The current horizontal rotation, in radians.
  435. */
  436. getAzimuthalAngle() {
  437. return this._spherical.theta;
  438. }
  439. /**
  440. * Returns the distance from the camera to the target.
  441. *
  442. * @return {number} The distance from the camera to the target.
  443. */
  444. getDistance() {
  445. return this.object.position.distanceTo( this.target );
  446. }
  447. /**
  448. * Adds key event listeners to the given DOM element.
  449. * `window` is a recommended argument for using this method.
  450. *
  451. * @param {HTMLElement} domElement - The DOM element
  452. */
  453. listenToKeyEvents( domElement ) {
  454. domElement.addEventListener( 'keydown', this._onKeyDown );
  455. this._domElementKeyEvents = domElement;
  456. }
  457. /**
  458. * Removes the key event listener previously defined with `listenToKeyEvents()`.
  459. */
  460. stopListenToKeyEvents() {
  461. if ( this._domElementKeyEvents !== null ) {
  462. this._domElementKeyEvents.removeEventListener( 'keydown', this._onKeyDown );
  463. this._domElementKeyEvents = null;
  464. }
  465. }
  466. /**
  467. * Save the current state of the controls. This can later be recovered with `reset()`.
  468. */
  469. saveState() {
  470. this.target0.copy( this.target );
  471. this.position0.copy( this.object.position );
  472. this.zoom0 = this.object.zoom;
  473. }
  474. /**
  475. * Reset the controls to their state from either the last time the `saveState()`
  476. * was called, or the initial state.
  477. */
  478. reset() {
  479. this.target.copy( this.target0 );
  480. this.object.position.copy( this.position0 );
  481. this.object.zoom = this.zoom0;
  482. this.object.updateProjectionMatrix();
  483. this.dispatchEvent( _changeEvent );
  484. this.update();
  485. this.state = _STATE.NONE;
  486. }
  487. /**
  488. * Programmatically pan the camera.
  489. *
  490. * @param {number} deltaX - The horizontal pan amount in pixels.
  491. * @param {number} deltaY - The vertical pan amount in pixels.
  492. */
  493. pan( deltaX, deltaY ) {
  494. this._pan( deltaX, deltaY );
  495. this.update();
  496. }
  497. /**
  498. * Programmatically dolly in (zoom in for perspective camera).
  499. *
  500. * @param {number} dollyScale - The dolly scale factor.
  501. */
  502. dollyIn( dollyScale ) {
  503. this._dollyIn( dollyScale );
  504. this.update();
  505. }
  506. /**
  507. * Programmatically dolly out (zoom out for perspective camera).
  508. *
  509. * @param {number} dollyScale - The dolly scale factor.
  510. */
  511. dollyOut( dollyScale ) {
  512. this._dollyOut( dollyScale );
  513. this.update();
  514. }
  515. /**
  516. * Programmatically rotate the camera left (around the vertical axis).
  517. *
  518. * @param {number} angle - The rotation angle in radians.
  519. */
  520. rotateLeft( angle ) {
  521. this._rotateLeft( angle );
  522. this.update();
  523. }
  524. /**
  525. * Programmatically rotate the camera up (around the horizontal axis).
  526. *
  527. * @param {number} angle - The rotation angle in radians.
  528. */
  529. rotateUp( angle ) {
  530. this._rotateUp( angle );
  531. this.update();
  532. }
  533. update( deltaTime = null ) {
  534. const position = this.object.position;
  535. _v.copy( position ).sub( this.target );
  536. // rotate offset to "y-axis-is-up" space
  537. _v.applyQuaternion( this._quat );
  538. // angle from z-axis around y-axis
  539. this._spherical.setFromVector3( _v );
  540. if ( this.autoRotate && this.state === _STATE.NONE ) {
  541. this._rotateLeft( this._getAutoRotationAngle( deltaTime ) );
  542. }
  543. if ( this.enableDamping ) {
  544. this._spherical.theta += this._sphericalDelta.theta * this.dampingFactor;
  545. this._spherical.phi += this._sphericalDelta.phi * this.dampingFactor;
  546. } else {
  547. this._spherical.theta += this._sphericalDelta.theta;
  548. this._spherical.phi += this._sphericalDelta.phi;
  549. }
  550. // restrict theta to be between desired limits
  551. let min = this.minAzimuthAngle;
  552. let max = this.maxAzimuthAngle;
  553. if ( isFinite( min ) && isFinite( max ) ) {
  554. if ( min < - Math.PI ) min += _twoPI; else if ( min > Math.PI ) min -= _twoPI;
  555. if ( max < - Math.PI ) max += _twoPI; else if ( max > Math.PI ) max -= _twoPI;
  556. if ( min <= max ) {
  557. this._spherical.theta = Math.max( min, Math.min( max, this._spherical.theta ) );
  558. } else {
  559. this._spherical.theta = ( this._spherical.theta > ( min + max ) / 2 ) ?
  560. Math.max( min, this._spherical.theta ) :
  561. Math.min( max, this._spherical.theta );
  562. }
  563. }
  564. // restrict phi to be between desired limits
  565. this._spherical.phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, this._spherical.phi ) );
  566. this._spherical.makeSafe();
  567. // move target to panned location
  568. if ( this.enableDamping === true ) {
  569. this.target.addScaledVector( this._panOffset, this.dampingFactor );
  570. } else {
  571. this.target.add( this._panOffset );
  572. }
  573. // Limit the target distance from the cursor to create a sphere around the center of interest
  574. this.target.sub( this.cursor );
  575. this.target.clampLength( this.minTargetRadius, this.maxTargetRadius );
  576. this.target.add( this.cursor );
  577. let zoomChanged = false;
  578. // adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera
  579. // we adjust zoom later in these cases
  580. if ( this.zoomToCursor && this._performCursorZoom || this.object.isOrthographicCamera ) {
  581. this._spherical.radius = this._clampDistance( this._spherical.radius );
  582. } else {
  583. const prevRadius = this._spherical.radius;
  584. this._spherical.radius = this._clampDistance( this._spherical.radius * this._scale );
  585. zoomChanged = prevRadius != this._spherical.radius;
  586. }
  587. _v.setFromSpherical( this._spherical );
  588. // rotate offset back to "camera-up-vector-is-up" space
  589. _v.applyQuaternion( this._quatInverse );
  590. position.copy( this.target ).add( _v );
  591. this.object.lookAt( this.target );
  592. if ( this.enableDamping === true ) {
  593. this._sphericalDelta.theta *= ( 1 - this.dampingFactor );
  594. this._sphericalDelta.phi *= ( 1 - this.dampingFactor );
  595. this._panOffset.multiplyScalar( 1 - this.dampingFactor );
  596. } else {
  597. this._sphericalDelta.set( 0, 0, 0 );
  598. this._panOffset.set( 0, 0, 0 );
  599. }
  600. // adjust camera position
  601. if ( this.zoomToCursor && this._performCursorZoom ) {
  602. let newRadius = null;
  603. if ( this.object.isPerspectiveCamera ) {
  604. // move the camera down the pointer ray
  605. // this method avoids floating point error
  606. const prevRadius = _v.length();
  607. newRadius = this._clampDistance( prevRadius * this._scale );
  608. const radiusDelta = prevRadius - newRadius;
  609. this.object.position.addScaledVector( this._dollyDirection, radiusDelta );
  610. this.object.updateMatrixWorld();
  611. zoomChanged = !! radiusDelta;
  612. } else if ( this.object.isOrthographicCamera ) {
  613. // adjust the ortho camera position based on zoom changes
  614. const mouseBefore = new Vector3( this._mouse.x, this._mouse.y, 0 );
  615. mouseBefore.unproject( this.object );
  616. const prevZoom = this.object.zoom;
  617. this.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / this._scale ) );
  618. this.object.updateProjectionMatrix();
  619. zoomChanged = prevZoom !== this.object.zoom;
  620. const mouseAfter = new Vector3( this._mouse.x, this._mouse.y, 0 );
  621. mouseAfter.unproject( this.object );
  622. this.object.position.sub( mouseAfter ).add( mouseBefore );
  623. this.object.updateMatrixWorld();
  624. newRadius = _v.length();
  625. } else {
  626. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled.' );
  627. this.zoomToCursor = false;
  628. }
  629. // handle the placement of the target
  630. if ( newRadius !== null ) {
  631. if ( this.screenSpacePanning ) {
  632. // position the orbit target in front of the new camera position
  633. this.target.set( 0, 0, - 1 )
  634. .transformDirection( this.object.matrix )
  635. .multiplyScalar( newRadius )
  636. .add( this.object.position );
  637. } else {
  638. // get the ray and translation plane to compute target
  639. _ray.origin.copy( this.object.position );
  640. _ray.direction.set( 0, 0, - 1 ).transformDirection( this.object.matrix );
  641. // if the camera is 20 degrees above the horizon then don't adjust the focus target to avoid
  642. // extremely large values
  643. if ( Math.abs( this.object.up.dot( _ray.direction ) ) < _TILT_LIMIT ) {
  644. this.object.lookAt( this.target );
  645. } else {
  646. _plane.setFromNormalAndCoplanarPoint( this.object.up, this.target );
  647. _ray.intersectPlane( _plane, this.target );
  648. }
  649. }
  650. }
  651. } else if ( this.object.isOrthographicCamera ) {
  652. const prevZoom = this.object.zoom;
  653. this.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / this._scale ) );
  654. if ( prevZoom !== this.object.zoom ) {
  655. this.object.updateProjectionMatrix();
  656. zoomChanged = true;
  657. }
  658. }
  659. this._scale = 1;
  660. this._performCursorZoom = false;
  661. // update condition is:
  662. // min(camera displacement, camera rotation in radians)^2 > EPS
  663. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  664. if ( zoomChanged ||
  665. this._lastPosition.distanceToSquared( this.object.position ) > _EPS ||
  666. 8 * ( 1 - this._lastQuaternion.dot( this.object.quaternion ) ) > _EPS ||
  667. this._lastTargetPosition.distanceToSquared( this.target ) > _EPS ) {
  668. this.dispatchEvent( _changeEvent );
  669. this._lastPosition.copy( this.object.position );
  670. this._lastQuaternion.copy( this.object.quaternion );
  671. this._lastTargetPosition.copy( this.target );
  672. return true;
  673. }
  674. return false;
  675. }
  676. _getAutoRotationAngle( deltaTime ) {
  677. if ( deltaTime !== null ) {
  678. return ( _twoPI / 60 * this.autoRotateSpeed ) * deltaTime;
  679. } else {
  680. return _twoPI / 60 / 60 * this.autoRotateSpeed;
  681. }
  682. }
  683. _getZoomScale( delta ) {
  684. const normalizedDelta = Math.abs( delta * 0.01 );
  685. return Math.pow( 0.95, this.zoomSpeed * normalizedDelta );
  686. }
  687. _rotateLeft( angle ) {
  688. this._sphericalDelta.theta -= angle;
  689. }
  690. _rotateUp( angle ) {
  691. this._sphericalDelta.phi -= angle;
  692. }
  693. _panLeft( distance, objectMatrix ) {
  694. _v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  695. _v.multiplyScalar( - distance );
  696. this._panOffset.add( _v );
  697. }
  698. _panUp( distance, objectMatrix ) {
  699. if ( this.screenSpacePanning === true ) {
  700. _v.setFromMatrixColumn( objectMatrix, 1 );
  701. } else {
  702. _v.setFromMatrixColumn( objectMatrix, 0 );
  703. _v.crossVectors( this.object.up, _v );
  704. }
  705. _v.multiplyScalar( distance );
  706. this._panOffset.add( _v );
  707. }
  708. // deltaX and deltaY are in pixels; right and down are positive
  709. _pan( deltaX, deltaY ) {
  710. const element = this.domElement;
  711. if ( this.object.isPerspectiveCamera ) {
  712. // perspective
  713. const position = this.object.position;
  714. _v.copy( position ).sub( this.target );
  715. let targetDistance = _v.length();
  716. // half of the fov is center to top of screen
  717. targetDistance *= Math.tan( ( this.object.fov / 2 ) * Math.PI / 180.0 );
  718. // we use only clientHeight here so aspect ratio does not distort speed
  719. this._panLeft( 2 * deltaX * targetDistance / element.clientHeight, this.object.matrix );
  720. this._panUp( 2 * deltaY * targetDistance / element.clientHeight, this.object.matrix );
  721. } else if ( this.object.isOrthographicCamera ) {
  722. // orthographic
  723. this._panLeft( deltaX * ( this.object.right - this.object.left ) / this.object.zoom / element.clientWidth, this.object.matrix );
  724. this._panUp( deltaY * ( this.object.top - this.object.bottom ) / this.object.zoom / element.clientHeight, this.object.matrix );
  725. } else {
  726. // camera neither orthographic nor perspective
  727. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  728. this.enablePan = false;
  729. }
  730. }
  731. _dollyOut( dollyScale ) {
  732. if ( this.object.isPerspectiveCamera || this.object.isOrthographicCamera ) {
  733. this._scale /= dollyScale;
  734. } else {
  735. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  736. this.enableZoom = false;
  737. }
  738. }
  739. _dollyIn( dollyScale ) {
  740. if ( this.object.isPerspectiveCamera || this.object.isOrthographicCamera ) {
  741. this._scale *= dollyScale;
  742. } else {
  743. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  744. this.enableZoom = false;
  745. }
  746. }
  747. _updateZoomParameters( x, y ) {
  748. if ( ! this.zoomToCursor ) {
  749. return;
  750. }
  751. this._performCursorZoom = true;
  752. const rect = this.domElement.getBoundingClientRect();
  753. const dx = x - rect.left;
  754. const dy = y - rect.top;
  755. const w = rect.width;
  756. const h = rect.height;
  757. this._mouse.x = ( dx / w ) * 2 - 1;
  758. this._mouse.y = - ( dy / h ) * 2 + 1;
  759. this._dollyDirection.set( this._mouse.x, this._mouse.y, 1 ).unproject( this.object ).sub( this.object.position ).normalize();
  760. }
  761. _clampDistance( dist ) {
  762. return Math.max( this.minDistance, Math.min( this.maxDistance, dist ) );
  763. }
  764. //
  765. // event callbacks - update the object state
  766. //
  767. _handleMouseDownRotate( event ) {
  768. this._rotateStart.set( event.clientX, event.clientY );
  769. }
  770. _handleMouseDownDolly( event ) {
  771. this._updateZoomParameters( event.clientX, event.clientX );
  772. this._dollyStart.set( event.clientX, event.clientY );
  773. }
  774. _handleMouseDownPan( event ) {
  775. this._panStart.set( event.clientX, event.clientY );
  776. }
  777. _handleMouseMoveRotate( event ) {
  778. this._rotateEnd.set( event.clientX, event.clientY );
  779. this._rotateDelta.subVectors( this._rotateEnd, this._rotateStart ).multiplyScalar( this.rotateSpeed );
  780. const element = this.domElement;
  781. this._rotateLeft( _twoPI * this._rotateDelta.x / element.clientHeight ); // yes, height
  782. this._rotateUp( _twoPI * this._rotateDelta.y / element.clientHeight );
  783. this._rotateStart.copy( this._rotateEnd );
  784. this.update();
  785. }
  786. _handleMouseMoveDolly( event ) {
  787. this._dollyEnd.set( event.clientX, event.clientY );
  788. this._dollyDelta.subVectors( this._dollyEnd, this._dollyStart );
  789. if ( this._dollyDelta.y > 0 ) {
  790. this._dollyOut( this._getZoomScale( this._dollyDelta.y ) );
  791. } else if ( this._dollyDelta.y < 0 ) {
  792. this._dollyIn( this._getZoomScale( this._dollyDelta.y ) );
  793. }
  794. this._dollyStart.copy( this._dollyEnd );
  795. this.update();
  796. }
  797. _handleMouseMovePan( event ) {
  798. this._panEnd.set( event.clientX, event.clientY );
  799. this._panDelta.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.panSpeed );
  800. this._pan( this._panDelta.x, this._panDelta.y );
  801. this._panStart.copy( this._panEnd );
  802. this.update();
  803. }
  804. _handleMouseWheel( event ) {
  805. this._updateZoomParameters( event.clientX, event.clientY );
  806. if ( event.deltaY < 0 ) {
  807. this._dollyIn( this._getZoomScale( event.deltaY ) );
  808. } else if ( event.deltaY > 0 ) {
  809. this._dollyOut( this._getZoomScale( event.deltaY ) );
  810. }
  811. this.update();
  812. }
  813. _handleKeyDown( event ) {
  814. let needsUpdate = false;
  815. switch ( event.code ) {
  816. case this.keys.UP:
  817. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  818. if ( this.enableRotate ) {
  819. this._rotateUp( _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
  820. }
  821. } else {
  822. if ( this.enablePan ) {
  823. this._pan( 0, this.keyPanSpeed );
  824. }
  825. }
  826. needsUpdate = true;
  827. break;
  828. case this.keys.BOTTOM:
  829. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  830. if ( this.enableRotate ) {
  831. this._rotateUp( - _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
  832. }
  833. } else {
  834. if ( this.enablePan ) {
  835. this._pan( 0, - this.keyPanSpeed );
  836. }
  837. }
  838. needsUpdate = true;
  839. break;
  840. case this.keys.LEFT:
  841. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  842. if ( this.enableRotate ) {
  843. this._rotateLeft( _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
  844. }
  845. } else {
  846. if ( this.enablePan ) {
  847. this._pan( this.keyPanSpeed, 0 );
  848. }
  849. }
  850. needsUpdate = true;
  851. break;
  852. case this.keys.RIGHT:
  853. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  854. if ( this.enableRotate ) {
  855. this._rotateLeft( - _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
  856. }
  857. } else {
  858. if ( this.enablePan ) {
  859. this._pan( - this.keyPanSpeed, 0 );
  860. }
  861. }
  862. needsUpdate = true;
  863. break;
  864. }
  865. if ( needsUpdate ) {
  866. // prevent the browser from scrolling on cursor keys
  867. event.preventDefault();
  868. this.update();
  869. }
  870. }
  871. _handleTouchStartRotate( event ) {
  872. if ( this._pointers.length === 1 ) {
  873. this._rotateStart.set( event.pageX, event.pageY );
  874. } else {
  875. const position = this._getSecondPointerPosition( event );
  876. const x = 0.5 * ( event.pageX + position.x );
  877. const y = 0.5 * ( event.pageY + position.y );
  878. this._rotateStart.set( x, y );
  879. }
  880. }
  881. _handleTouchStartPan( event ) {
  882. if ( this._pointers.length === 1 ) {
  883. this._panStart.set( event.pageX, event.pageY );
  884. } else {
  885. const position = this._getSecondPointerPosition( event );
  886. const x = 0.5 * ( event.pageX + position.x );
  887. const y = 0.5 * ( event.pageY + position.y );
  888. this._panStart.set( x, y );
  889. }
  890. }
  891. _handleTouchStartDolly( event ) {
  892. const position = this._getSecondPointerPosition( event );
  893. const dx = event.pageX - position.x;
  894. const dy = event.pageY - position.y;
  895. const distance = Math.sqrt( dx * dx + dy * dy );
  896. this._dollyStart.set( 0, distance );
  897. }
  898. _handleTouchStartDollyPan( event ) {
  899. if ( this.enableZoom ) this._handleTouchStartDolly( event );
  900. if ( this.enablePan ) this._handleTouchStartPan( event );
  901. }
  902. _handleTouchStartDollyRotate( event ) {
  903. if ( this.enableZoom ) this._handleTouchStartDolly( event );
  904. if ( this.enableRotate ) this._handleTouchStartRotate( event );
  905. }
  906. _handleTouchMoveRotate( event ) {
  907. if ( this._pointers.length == 1 ) {
  908. this._rotateEnd.set( event.pageX, event.pageY );
  909. } else {
  910. const position = this._getSecondPointerPosition( event );
  911. const x = 0.5 * ( event.pageX + position.x );
  912. const y = 0.5 * ( event.pageY + position.y );
  913. this._rotateEnd.set( x, y );
  914. }
  915. this._rotateDelta.subVectors( this._rotateEnd, this._rotateStart ).multiplyScalar( this.rotateSpeed );
  916. const element = this.domElement;
  917. this._rotateLeft( _twoPI * this._rotateDelta.x / element.clientHeight ); // yes, height
  918. this._rotateUp( _twoPI * this._rotateDelta.y / element.clientHeight );
  919. this._rotateStart.copy( this._rotateEnd );
  920. }
  921. _handleTouchMovePan( event ) {
  922. if ( this._pointers.length === 1 ) {
  923. this._panEnd.set( event.pageX, event.pageY );
  924. } else {
  925. const position = this._getSecondPointerPosition( event );
  926. const x = 0.5 * ( event.pageX + position.x );
  927. const y = 0.5 * ( event.pageY + position.y );
  928. this._panEnd.set( x, y );
  929. }
  930. this._panDelta.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.panSpeed );
  931. this._pan( this._panDelta.x, this._panDelta.y );
  932. this._panStart.copy( this._panEnd );
  933. }
  934. _handleTouchMoveDolly( event ) {
  935. const position = this._getSecondPointerPosition( event );
  936. const dx = event.pageX - position.x;
  937. const dy = event.pageY - position.y;
  938. const distance = Math.sqrt( dx * dx + dy * dy );
  939. this._dollyEnd.set( 0, distance );
  940. this._dollyDelta.set( 0, Math.pow( this._dollyEnd.y / this._dollyStart.y, this.zoomSpeed ) );
  941. this._dollyOut( this._dollyDelta.y );
  942. this._dollyStart.copy( this._dollyEnd );
  943. const centerX = ( event.pageX + position.x ) * 0.5;
  944. const centerY = ( event.pageY + position.y ) * 0.5;
  945. this._updateZoomParameters( centerX, centerY );
  946. }
  947. _handleTouchMoveDollyPan( event ) {
  948. if ( this.enableZoom ) this._handleTouchMoveDolly( event );
  949. if ( this.enablePan ) this._handleTouchMovePan( event );
  950. }
  951. _handleTouchMoveDollyRotate( event ) {
  952. if ( this.enableZoom ) this._handleTouchMoveDolly( event );
  953. if ( this.enableRotate ) this._handleTouchMoveRotate( event );
  954. }
  955. // pointers
  956. _addPointer( event ) {
  957. this._pointers.push( event.pointerId );
  958. }
  959. _removePointer( event ) {
  960. delete this._pointerPositions[ event.pointerId ];
  961. for ( let i = 0; i < this._pointers.length; i ++ ) {
  962. if ( this._pointers[ i ] == event.pointerId ) {
  963. this._pointers.splice( i, 1 );
  964. return;
  965. }
  966. }
  967. }
  968. _isTrackingPointer( event ) {
  969. for ( let i = 0; i < this._pointers.length; i ++ ) {
  970. if ( this._pointers[ i ] == event.pointerId ) return true;
  971. }
  972. return false;
  973. }
  974. _trackPointer( event ) {
  975. let position = this._pointerPositions[ event.pointerId ];
  976. if ( position === undefined ) {
  977. position = new Vector2();
  978. this._pointerPositions[ event.pointerId ] = position;
  979. }
  980. position.set( event.pageX, event.pageY );
  981. }
  982. _getSecondPointerPosition( event ) {
  983. const pointerId = ( event.pointerId === this._pointers[ 0 ] ) ? this._pointers[ 1 ] : this._pointers[ 0 ];
  984. return this._pointerPositions[ pointerId ];
  985. }
  986. //
  987. _customWheelEvent( event ) {
  988. const mode = event.deltaMode;
  989. // minimal wheel event altered to meet delta-zoom demand
  990. const newEvent = {
  991. clientX: event.clientX,
  992. clientY: event.clientY,
  993. deltaY: event.deltaY,
  994. };
  995. switch ( mode ) {
  996. case 1: // LINE_MODE
  997. newEvent.deltaY *= 16;
  998. break;
  999. case 2: // PAGE_MODE
  1000. newEvent.deltaY *= 100;
  1001. break;
  1002. }
  1003. // detect if event was triggered by pinching
  1004. if ( event.ctrlKey && ! this._controlActive ) {
  1005. newEvent.deltaY *= 10;
  1006. }
  1007. return newEvent;
  1008. }
  1009. }
  1010. function onPointerDown( event ) {
  1011. if ( this.enabled === false ) return;
  1012. if ( this._pointers.length === 0 ) {
  1013. this.domElement.setPointerCapture( event.pointerId );
  1014. this.domElement.ownerDocument.addEventListener( 'pointermove', this._onPointerMove );
  1015. this.domElement.ownerDocument.addEventListener( 'pointerup', this._onPointerUp );
  1016. }
  1017. //
  1018. if ( this._isTrackingPointer( event ) ) return;
  1019. //
  1020. this._addPointer( event );
  1021. if ( event.pointerType === 'touch' ) {
  1022. this._onTouchStart( event );
  1023. } else {
  1024. this._onMouseDown( event );
  1025. }
  1026. }
  1027. function onPointerMove( event ) {
  1028. if ( this.enabled === false ) return;
  1029. if ( event.pointerType === 'touch' ) {
  1030. this._onTouchMove( event );
  1031. } else {
  1032. this._onMouseMove( event );
  1033. }
  1034. }
  1035. function onPointerUp( event ) {
  1036. this._removePointer( event );
  1037. switch ( this._pointers.length ) {
  1038. case 0:
  1039. this.domElement.releasePointerCapture( event.pointerId );
  1040. this.domElement.ownerDocument.removeEventListener( 'pointermove', this._onPointerMove );
  1041. this.domElement.ownerDocument.removeEventListener( 'pointerup', this._onPointerUp );
  1042. this.dispatchEvent( _endEvent );
  1043. this.state = _STATE.NONE;
  1044. break;
  1045. case 1:
  1046. const pointerId = this._pointers[ 0 ];
  1047. const position = this._pointerPositions[ pointerId ];
  1048. // minimal placeholder event - allows state correction on pointer-up
  1049. this._onTouchStart( { pointerId: pointerId, pageX: position.x, pageY: position.y } );
  1050. break;
  1051. }
  1052. }
  1053. function onMouseDown( event ) {
  1054. let mouseAction;
  1055. switch ( event.button ) {
  1056. case 0:
  1057. mouseAction = this.mouseButtons.LEFT;
  1058. break;
  1059. case 1:
  1060. mouseAction = this.mouseButtons.MIDDLE;
  1061. break;
  1062. case 2:
  1063. mouseAction = this.mouseButtons.RIGHT;
  1064. break;
  1065. default:
  1066. mouseAction = - 1;
  1067. }
  1068. switch ( mouseAction ) {
  1069. case MOUSE.DOLLY:
  1070. if ( this.enableZoom === false ) return;
  1071. this._handleMouseDownDolly( event );
  1072. this.state = _STATE.DOLLY;
  1073. break;
  1074. case MOUSE.ROTATE:
  1075. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  1076. if ( this.enablePan === false ) return;
  1077. this._handleMouseDownPan( event );
  1078. this.state = _STATE.PAN;
  1079. } else {
  1080. if ( this.enableRotate === false ) return;
  1081. this._handleMouseDownRotate( event );
  1082. this.state = _STATE.ROTATE;
  1083. }
  1084. break;
  1085. case MOUSE.PAN:
  1086. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  1087. if ( this.enableRotate === false ) return;
  1088. this._handleMouseDownRotate( event );
  1089. this.state = _STATE.ROTATE;
  1090. } else {
  1091. if ( this.enablePan === false ) return;
  1092. this._handleMouseDownPan( event );
  1093. this.state = _STATE.PAN;
  1094. }
  1095. break;
  1096. default:
  1097. this.state = _STATE.NONE;
  1098. }
  1099. if ( this.state !== _STATE.NONE ) {
  1100. this.dispatchEvent( _startEvent );
  1101. }
  1102. }
  1103. function onMouseMove( event ) {
  1104. switch ( this.state ) {
  1105. case _STATE.ROTATE:
  1106. if ( this.enableRotate === false ) return;
  1107. this._handleMouseMoveRotate( event );
  1108. break;
  1109. case _STATE.DOLLY:
  1110. if ( this.enableZoom === false ) return;
  1111. this._handleMouseMoveDolly( event );
  1112. break;
  1113. case _STATE.PAN:
  1114. if ( this.enablePan === false ) return;
  1115. this._handleMouseMovePan( event );
  1116. break;
  1117. }
  1118. }
  1119. function onMouseWheel( event ) {
  1120. if ( this.enabled === false || this.enableZoom === false || this.state !== _STATE.NONE ) return;
  1121. event.preventDefault();
  1122. this.dispatchEvent( _startEvent );
  1123. this._handleMouseWheel( this._customWheelEvent( event ) );
  1124. this.dispatchEvent( _endEvent );
  1125. }
  1126. function onKeyDown( event ) {
  1127. if ( this.enabled === false ) return;
  1128. this._handleKeyDown( event );
  1129. }
  1130. function onTouchStart( event ) {
  1131. this._trackPointer( event );
  1132. switch ( this._pointers.length ) {
  1133. case 1:
  1134. switch ( this.touches.ONE ) {
  1135. case TOUCH.ROTATE:
  1136. if ( this.enableRotate === false ) return;
  1137. this._handleTouchStartRotate( event );
  1138. this.state = _STATE.TOUCH_ROTATE;
  1139. break;
  1140. case TOUCH.PAN:
  1141. if ( this.enablePan === false ) return;
  1142. this._handleTouchStartPan( event );
  1143. this.state = _STATE.TOUCH_PAN;
  1144. break;
  1145. default:
  1146. this.state = _STATE.NONE;
  1147. }
  1148. break;
  1149. case 2:
  1150. switch ( this.touches.TWO ) {
  1151. case TOUCH.DOLLY_PAN:
  1152. if ( this.enableZoom === false && this.enablePan === false ) return;
  1153. this._handleTouchStartDollyPan( event );
  1154. this.state = _STATE.TOUCH_DOLLY_PAN;
  1155. break;
  1156. case TOUCH.DOLLY_ROTATE:
  1157. if ( this.enableZoom === false && this.enableRotate === false ) return;
  1158. this._handleTouchStartDollyRotate( event );
  1159. this.state = _STATE.TOUCH_DOLLY_ROTATE;
  1160. break;
  1161. default:
  1162. this.state = _STATE.NONE;
  1163. }
  1164. break;
  1165. default:
  1166. this.state = _STATE.NONE;
  1167. }
  1168. if ( this.state !== _STATE.NONE ) {
  1169. this.dispatchEvent( _startEvent );
  1170. }
  1171. }
  1172. function onTouchMove( event ) {
  1173. this._trackPointer( event );
  1174. switch ( this.state ) {
  1175. case _STATE.TOUCH_ROTATE:
  1176. if ( this.enableRotate === false ) return;
  1177. this._handleTouchMoveRotate( event );
  1178. this.update();
  1179. break;
  1180. case _STATE.TOUCH_PAN:
  1181. if ( this.enablePan === false ) return;
  1182. this._handleTouchMovePan( event );
  1183. this.update();
  1184. break;
  1185. case _STATE.TOUCH_DOLLY_PAN:
  1186. if ( this.enableZoom === false && this.enablePan === false ) return;
  1187. this._handleTouchMoveDollyPan( event );
  1188. this.update();
  1189. break;
  1190. case _STATE.TOUCH_DOLLY_ROTATE:
  1191. if ( this.enableZoom === false && this.enableRotate === false ) return;
  1192. this._handleTouchMoveDollyRotate( event );
  1193. this.update();
  1194. break;
  1195. default:
  1196. this.state = _STATE.NONE;
  1197. }
  1198. }
  1199. function onContextMenu( event ) {
  1200. if ( this.enabled === false ) return;
  1201. event.preventDefault();
  1202. }
  1203. function interceptControlDown( event ) {
  1204. if ( event.key === 'Control' ) {
  1205. this._controlActive = true;
  1206. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  1207. document.addEventListener( 'keyup', this._interceptControlUp, { passive: true, capture: true } );
  1208. }
  1209. }
  1210. function interceptControlUp( event ) {
  1211. if ( event.key === 'Control' ) {
  1212. this._controlActive = false;
  1213. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  1214. document.removeEventListener( 'keyup', this._interceptControlUp, { passive: true, capture: true } );
  1215. }
  1216. }
  1217. export { OrbitControls };
粤ICP备19079148号