three_addons_controls_OrbitControls__js.js 35 KB

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