OrbitControls.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  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. this._cursorStyle = 'auto';
  351. // the target DOM element for key events
  352. this._domElementKeyEvents = null;
  353. // internals
  354. this._lastPosition = new Vector3();
  355. this._lastQuaternion = new Quaternion();
  356. this._lastTargetPosition = new Vector3();
  357. // so camera.up is the orbit axis
  358. this._quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
  359. this._quatInverse = this._quat.clone().invert();
  360. // current position in spherical coordinates
  361. this._spherical = new Spherical();
  362. this._sphericalDelta = new Spherical();
  363. this._scale = 1;
  364. this._panOffset = new Vector3();
  365. this._rotateStart = new Vector2();
  366. this._rotateEnd = new Vector2();
  367. this._rotateDelta = new Vector2();
  368. this._panStart = new Vector2();
  369. this._panEnd = new Vector2();
  370. this._panDelta = new Vector2();
  371. this._dollyStart = new Vector2();
  372. this._dollyEnd = new Vector2();
  373. this._dollyDelta = new Vector2();
  374. this._dollyDirection = new Vector3();
  375. this._mouse = new Vector2();
  376. this._performCursorZoom = false;
  377. this._pointers = [];
  378. this._pointerPositions = {};
  379. this._controlActive = false;
  380. // event listeners
  381. this._onPointerMove = onPointerMove.bind( this );
  382. this._onPointerDown = onPointerDown.bind( this );
  383. this._onPointerUp = onPointerUp.bind( this );
  384. this._onContextMenu = onContextMenu.bind( this );
  385. this._onMouseWheel = onMouseWheel.bind( this );
  386. this._onKeyDown = onKeyDown.bind( this );
  387. this._onTouchStart = onTouchStart.bind( this );
  388. this._onTouchMove = onTouchMove.bind( this );
  389. this._onMouseDown = onMouseDown.bind( this );
  390. this._onMouseMove = onMouseMove.bind( this );
  391. this._interceptControlDown = interceptControlDown.bind( this );
  392. this._interceptControlUp = interceptControlUp.bind( this );
  393. //
  394. if ( this.domElement !== null ) {
  395. this.connect( this.domElement );
  396. }
  397. this.update();
  398. }
  399. /**
  400. * Defines the visual representation of the cursor.
  401. *
  402. * @type {('auto'|'grab')}
  403. * @default 'auto'
  404. */
  405. set cursorStyle( type ) {
  406. this._cursorStyle = type;
  407. if ( type === 'grab' ) {
  408. this.domElement.style.cursor = 'grab';
  409. } else {
  410. this.domElement.style.cursor = 'auto';
  411. }
  412. }
  413. get cursorStyle() {
  414. return this._cursorStyle;
  415. }
  416. connect( element ) {
  417. super.connect( element );
  418. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  419. this.domElement.addEventListener( 'pointercancel', this._onPointerUp );
  420. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  421. this.domElement.addEventListener( 'wheel', this._onMouseWheel, { passive: false } );
  422. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  423. document.addEventListener( 'keydown', this._interceptControlDown, { passive: true, capture: true } );
  424. this.domElement.style.touchAction = 'none'; // disable touch scroll
  425. }
  426. disconnect() {
  427. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  428. this.domElement.ownerDocument.removeEventListener( 'pointermove', this._onPointerMove );
  429. this.domElement.ownerDocument.removeEventListener( 'pointerup', this._onPointerUp );
  430. this.domElement.removeEventListener( 'pointercancel', this._onPointerUp );
  431. this.domElement.removeEventListener( 'wheel', this._onMouseWheel );
  432. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  433. this.stopListenToKeyEvents();
  434. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  435. document.removeEventListener( 'keydown', this._interceptControlDown, { capture: true } );
  436. this.domElement.style.touchAction = 'auto';
  437. }
  438. dispose() {
  439. this.disconnect();
  440. }
  441. /**
  442. * Get the current vertical rotation, in radians.
  443. *
  444. * @return {number} The current vertical rotation, in radians.
  445. */
  446. getPolarAngle() {
  447. return this._spherical.phi;
  448. }
  449. /**
  450. * Get the current horizontal rotation, in radians.
  451. *
  452. * @return {number} The current horizontal rotation, in radians.
  453. */
  454. getAzimuthalAngle() {
  455. return this._spherical.theta;
  456. }
  457. /**
  458. * Returns the distance from the camera to the target.
  459. *
  460. * @return {number} The distance from the camera to the target.
  461. */
  462. getDistance() {
  463. return this.object.position.distanceTo( this.target );
  464. }
  465. /**
  466. * Adds key event listeners to the given DOM element.
  467. * `window` is a recommended argument for using this method.
  468. *
  469. * @param {HTMLElement} domElement - The DOM element
  470. */
  471. listenToKeyEvents( domElement ) {
  472. domElement.addEventListener( 'keydown', this._onKeyDown );
  473. this._domElementKeyEvents = domElement;
  474. }
  475. /**
  476. * Removes the key event listener previously defined with `listenToKeyEvents()`.
  477. */
  478. stopListenToKeyEvents() {
  479. if ( this._domElementKeyEvents !== null ) {
  480. this._domElementKeyEvents.removeEventListener( 'keydown', this._onKeyDown );
  481. this._domElementKeyEvents = null;
  482. }
  483. }
  484. /**
  485. * Save the current state of the controls. This can later be recovered with `reset()`.
  486. */
  487. saveState() {
  488. this.target0.copy( this.target );
  489. this.position0.copy( this.object.position );
  490. this.zoom0 = this.object.zoom;
  491. }
  492. /**
  493. * Reset the controls to their state from either the last time the `saveState()`
  494. * was called, or the initial state.
  495. */
  496. reset() {
  497. this.target.copy( this.target0 );
  498. this.object.position.copy( this.position0 );
  499. this.object.zoom = this.zoom0;
  500. this.object.updateProjectionMatrix();
  501. this.dispatchEvent( _changeEvent );
  502. this.update();
  503. this.state = _STATE.NONE;
  504. }
  505. /**
  506. * Programmatically pan the camera.
  507. *
  508. * @param {number} deltaX - The horizontal pan amount in pixels.
  509. * @param {number} deltaY - The vertical pan amount in pixels.
  510. */
  511. pan( deltaX, deltaY ) {
  512. this._pan( deltaX, deltaY );
  513. this.update();
  514. }
  515. /**
  516. * Programmatically dolly in (zoom in for perspective camera).
  517. *
  518. * @param {number} dollyScale - The dolly scale factor.
  519. */
  520. dollyIn( dollyScale ) {
  521. this._dollyIn( dollyScale );
  522. this.update();
  523. }
  524. /**
  525. * Programmatically dolly out (zoom out for perspective camera).
  526. *
  527. * @param {number} dollyScale - The dolly scale factor.
  528. */
  529. dollyOut( dollyScale ) {
  530. this._dollyOut( dollyScale );
  531. this.update();
  532. }
  533. /**
  534. * Programmatically rotate the camera left (around the vertical axis).
  535. *
  536. * @param {number} angle - The rotation angle in radians.
  537. */
  538. rotateLeft( angle ) {
  539. this._rotateLeft( angle );
  540. this.update();
  541. }
  542. /**
  543. * Programmatically rotate the camera up (around the horizontal axis).
  544. *
  545. * @param {number} angle - The rotation angle in radians.
  546. */
  547. rotateUp( angle ) {
  548. this._rotateUp( angle );
  549. this.update();
  550. }
  551. update( deltaTime = null ) {
  552. const position = this.object.position;
  553. _v.copy( position ).sub( this.target );
  554. // rotate offset to "y-axis-is-up" space
  555. _v.applyQuaternion( this._quat );
  556. // angle from z-axis around y-axis
  557. this._spherical.setFromVector3( _v );
  558. if ( this.autoRotate && this.state === _STATE.NONE ) {
  559. this._rotateLeft( this._getAutoRotationAngle( deltaTime ) );
  560. }
  561. if ( this.enableDamping ) {
  562. this._spherical.theta += this._sphericalDelta.theta * this.dampingFactor;
  563. this._spherical.phi += this._sphericalDelta.phi * this.dampingFactor;
  564. } else {
  565. this._spherical.theta += this._sphericalDelta.theta;
  566. this._spherical.phi += this._sphericalDelta.phi;
  567. }
  568. // restrict theta to be between desired limits
  569. let min = this.minAzimuthAngle;
  570. let max = this.maxAzimuthAngle;
  571. if ( isFinite( min ) && isFinite( max ) ) {
  572. if ( min < - Math.PI ) min += _twoPI; else if ( min > Math.PI ) min -= _twoPI;
  573. if ( max < - Math.PI ) max += _twoPI; else if ( max > Math.PI ) max -= _twoPI;
  574. if ( min <= max ) {
  575. this._spherical.theta = Math.max( min, Math.min( max, this._spherical.theta ) );
  576. } else {
  577. this._spherical.theta = ( this._spherical.theta > ( min + max ) / 2 ) ?
  578. Math.max( min, this._spherical.theta ) :
  579. Math.min( max, this._spherical.theta );
  580. }
  581. }
  582. // restrict phi to be between desired limits
  583. this._spherical.phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, this._spherical.phi ) );
  584. this._spherical.makeSafe();
  585. // move target to panned location
  586. if ( this.enableDamping === true ) {
  587. this.target.addScaledVector( this._panOffset, this.dampingFactor );
  588. } else {
  589. this.target.add( this._panOffset );
  590. }
  591. // Limit the target distance from the cursor to create a sphere around the center of interest
  592. this.target.sub( this.cursor );
  593. this.target.clampLength( this.minTargetRadius, this.maxTargetRadius );
  594. this.target.add( this.cursor );
  595. let zoomChanged = false;
  596. // adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera
  597. // we adjust zoom later in these cases
  598. if ( this.zoomToCursor && this._performCursorZoom || this.object.isOrthographicCamera ) {
  599. this._spherical.radius = this._clampDistance( this._spherical.radius );
  600. } else {
  601. const prevRadius = this._spherical.radius;
  602. this._spherical.radius = this._clampDistance( this._spherical.radius * this._scale );
  603. zoomChanged = prevRadius != this._spherical.radius;
  604. }
  605. _v.setFromSpherical( this._spherical );
  606. // rotate offset back to "camera-up-vector-is-up" space
  607. _v.applyQuaternion( this._quatInverse );
  608. position.copy( this.target ).add( _v );
  609. this.object.lookAt( this.target );
  610. if ( this.enableDamping === true ) {
  611. this._sphericalDelta.theta *= ( 1 - this.dampingFactor );
  612. this._sphericalDelta.phi *= ( 1 - this.dampingFactor );
  613. this._panOffset.multiplyScalar( 1 - this.dampingFactor );
  614. } else {
  615. this._sphericalDelta.set( 0, 0, 0 );
  616. this._panOffset.set( 0, 0, 0 );
  617. }
  618. // adjust camera position
  619. if ( this.zoomToCursor && this._performCursorZoom ) {
  620. let newRadius = null;
  621. if ( this.object.isPerspectiveCamera ) {
  622. // move the camera down the pointer ray
  623. // this method avoids floating point error
  624. const prevRadius = _v.length();
  625. newRadius = this._clampDistance( prevRadius * this._scale );
  626. const radiusDelta = prevRadius - newRadius;
  627. this.object.position.addScaledVector( this._dollyDirection, radiusDelta );
  628. this.object.updateMatrixWorld();
  629. zoomChanged = !! radiusDelta;
  630. } else if ( this.object.isOrthographicCamera ) {
  631. // adjust the ortho camera position based on zoom changes
  632. const mouseBefore = new Vector3( this._mouse.x, this._mouse.y, 0 );
  633. mouseBefore.unproject( this.object );
  634. const prevZoom = this.object.zoom;
  635. this.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / this._scale ) );
  636. this.object.updateProjectionMatrix();
  637. zoomChanged = prevZoom !== this.object.zoom;
  638. const mouseAfter = new Vector3( this._mouse.x, this._mouse.y, 0 );
  639. mouseAfter.unproject( this.object );
  640. this.object.position.sub( mouseAfter ).add( mouseBefore );
  641. this.object.updateMatrixWorld();
  642. newRadius = _v.length();
  643. } else {
  644. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled.' );
  645. this.zoomToCursor = false;
  646. }
  647. // handle the placement of the target
  648. if ( newRadius !== null ) {
  649. if ( this.screenSpacePanning ) {
  650. // position the orbit target in front of the new camera position
  651. this.target.set( 0, 0, - 1 )
  652. .transformDirection( this.object.matrix )
  653. .multiplyScalar( newRadius )
  654. .add( this.object.position );
  655. } else {
  656. // get the ray and translation plane to compute target
  657. _ray.origin.copy( this.object.position );
  658. _ray.direction.set( 0, 0, - 1 ).transformDirection( this.object.matrix );
  659. // if the camera is 20 degrees above the horizon then don't adjust the focus target to avoid
  660. // extremely large values
  661. if ( Math.abs( this.object.up.dot( _ray.direction ) ) < _TILT_LIMIT ) {
  662. this.object.lookAt( this.target );
  663. } else {
  664. _plane.setFromNormalAndCoplanarPoint( this.object.up, this.target );
  665. _ray.intersectPlane( _plane, this.target );
  666. }
  667. }
  668. }
  669. } else if ( this.object.isOrthographicCamera ) {
  670. const prevZoom = this.object.zoom;
  671. this.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / this._scale ) );
  672. if ( prevZoom !== this.object.zoom ) {
  673. this.object.updateProjectionMatrix();
  674. zoomChanged = true;
  675. }
  676. }
  677. this._scale = 1;
  678. this._performCursorZoom = false;
  679. // update condition is:
  680. // min(camera displacement, camera rotation in radians)^2 > EPS
  681. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  682. if ( zoomChanged ||
  683. this._lastPosition.distanceToSquared( this.object.position ) > _EPS ||
  684. 8 * ( 1 - this._lastQuaternion.dot( this.object.quaternion ) ) > _EPS ||
  685. this._lastTargetPosition.distanceToSquared( this.target ) > _EPS ) {
  686. this.dispatchEvent( _changeEvent );
  687. this._lastPosition.copy( this.object.position );
  688. this._lastQuaternion.copy( this.object.quaternion );
  689. this._lastTargetPosition.copy( this.target );
  690. return true;
  691. }
  692. return false;
  693. }
  694. _getAutoRotationAngle( deltaTime ) {
  695. if ( deltaTime !== null ) {
  696. return ( _twoPI / 60 * this.autoRotateSpeed ) * deltaTime;
  697. } else {
  698. return _twoPI / 60 / 60 * this.autoRotateSpeed;
  699. }
  700. }
  701. _getZoomScale( delta ) {
  702. const normalizedDelta = Math.abs( delta * 0.01 );
  703. return Math.pow( 0.95, this.zoomSpeed * normalizedDelta );
  704. }
  705. _rotateLeft( angle ) {
  706. this._sphericalDelta.theta -= angle;
  707. }
  708. _rotateUp( angle ) {
  709. this._sphericalDelta.phi -= angle;
  710. }
  711. _panLeft( distance, objectMatrix ) {
  712. _v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  713. _v.multiplyScalar( - distance );
  714. this._panOffset.add( _v );
  715. }
  716. _panUp( distance, objectMatrix ) {
  717. if ( this.screenSpacePanning === true ) {
  718. _v.setFromMatrixColumn( objectMatrix, 1 );
  719. } else {
  720. _v.setFromMatrixColumn( objectMatrix, 0 );
  721. _v.crossVectors( this.object.up, _v );
  722. }
  723. _v.multiplyScalar( distance );
  724. this._panOffset.add( _v );
  725. }
  726. // deltaX and deltaY are in pixels; right and down are positive
  727. _pan( deltaX, deltaY ) {
  728. const element = this.domElement;
  729. if ( this.object.isPerspectiveCamera ) {
  730. // perspective
  731. const position = this.object.position;
  732. _v.copy( position ).sub( this.target );
  733. let targetDistance = _v.length();
  734. // half of the fov is center to top of screen
  735. targetDistance *= Math.tan( ( this.object.fov / 2 ) * Math.PI / 180.0 );
  736. // we use only clientHeight here so aspect ratio does not distort speed
  737. this._panLeft( 2 * deltaX * targetDistance / element.clientHeight, this.object.matrix );
  738. this._panUp( 2 * deltaY * targetDistance / element.clientHeight, this.object.matrix );
  739. } else if ( this.object.isOrthographicCamera ) {
  740. // orthographic
  741. this._panLeft( deltaX * ( this.object.right - this.object.left ) / this.object.zoom / element.clientWidth, this.object.matrix );
  742. this._panUp( deltaY * ( this.object.top - this.object.bottom ) / this.object.zoom / element.clientHeight, this.object.matrix );
  743. } else {
  744. // camera neither orthographic nor perspective
  745. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  746. this.enablePan = false;
  747. }
  748. }
  749. _dollyOut( dollyScale ) {
  750. if ( this.object.isPerspectiveCamera || this.object.isOrthographicCamera ) {
  751. this._scale /= dollyScale;
  752. } else {
  753. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  754. this.enableZoom = false;
  755. }
  756. }
  757. _dollyIn( dollyScale ) {
  758. if ( this.object.isPerspectiveCamera || this.object.isOrthographicCamera ) {
  759. this._scale *= dollyScale;
  760. } else {
  761. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  762. this.enableZoom = false;
  763. }
  764. }
  765. _updateZoomParameters( x, y ) {
  766. if ( ! this.zoomToCursor ) {
  767. return;
  768. }
  769. this._performCursorZoom = true;
  770. const rect = this.domElement.getBoundingClientRect();
  771. const dx = x - rect.left;
  772. const dy = y - rect.top;
  773. const w = rect.width;
  774. const h = rect.height;
  775. this._mouse.x = ( dx / w ) * 2 - 1;
  776. this._mouse.y = - ( dy / h ) * 2 + 1;
  777. this._dollyDirection.set( this._mouse.x, this._mouse.y, 1 ).unproject( this.object ).sub( this.object.position ).normalize();
  778. }
  779. _clampDistance( dist ) {
  780. return Math.max( this.minDistance, Math.min( this.maxDistance, dist ) );
  781. }
  782. //
  783. // event callbacks - update the object state
  784. //
  785. _handleMouseDownRotate( event ) {
  786. this._rotateStart.set( event.clientX, event.clientY );
  787. }
  788. _handleMouseDownDolly( event ) {
  789. this._updateZoomParameters( event.clientX, event.clientX );
  790. this._dollyStart.set( event.clientX, event.clientY );
  791. }
  792. _handleMouseDownPan( event ) {
  793. this._panStart.set( event.clientX, event.clientY );
  794. }
  795. _handleMouseMoveRotate( event ) {
  796. this._rotateEnd.set( event.clientX, event.clientY );
  797. this._rotateDelta.subVectors( this._rotateEnd, this._rotateStart ).multiplyScalar( this.rotateSpeed );
  798. const element = this.domElement;
  799. this._rotateLeft( _twoPI * this._rotateDelta.x / element.clientHeight ); // yes, height
  800. this._rotateUp( _twoPI * this._rotateDelta.y / element.clientHeight );
  801. this._rotateStart.copy( this._rotateEnd );
  802. this.update();
  803. }
  804. _handleMouseMoveDolly( event ) {
  805. this._dollyEnd.set( event.clientX, event.clientY );
  806. this._dollyDelta.subVectors( this._dollyEnd, this._dollyStart );
  807. if ( this._dollyDelta.y > 0 ) {
  808. this._dollyOut( this._getZoomScale( this._dollyDelta.y ) );
  809. } else if ( this._dollyDelta.y < 0 ) {
  810. this._dollyIn( this._getZoomScale( this._dollyDelta.y ) );
  811. }
  812. this._dollyStart.copy( this._dollyEnd );
  813. this.update();
  814. }
  815. _handleMouseMovePan( event ) {
  816. this._panEnd.set( event.clientX, event.clientY );
  817. this._panDelta.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.panSpeed );
  818. this._pan( this._panDelta.x, this._panDelta.y );
  819. this._panStart.copy( this._panEnd );
  820. this.update();
  821. }
  822. _handleMouseWheel( event ) {
  823. this._updateZoomParameters( event.clientX, event.clientY );
  824. if ( event.deltaY < 0 ) {
  825. this._dollyIn( this._getZoomScale( event.deltaY ) );
  826. } else if ( event.deltaY > 0 ) {
  827. this._dollyOut( this._getZoomScale( event.deltaY ) );
  828. }
  829. this.update();
  830. }
  831. _handleKeyDown( event ) {
  832. let needsUpdate = false;
  833. switch ( event.code ) {
  834. case this.keys.UP:
  835. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  836. if ( this.enableRotate ) {
  837. this._rotateUp( _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
  838. }
  839. } else {
  840. if ( this.enablePan ) {
  841. this._pan( 0, this.keyPanSpeed );
  842. }
  843. }
  844. needsUpdate = true;
  845. break;
  846. case this.keys.BOTTOM:
  847. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  848. if ( this.enableRotate ) {
  849. this._rotateUp( - _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
  850. }
  851. } else {
  852. if ( this.enablePan ) {
  853. this._pan( 0, - this.keyPanSpeed );
  854. }
  855. }
  856. needsUpdate = true;
  857. break;
  858. case this.keys.LEFT:
  859. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  860. if ( this.enableRotate ) {
  861. this._rotateLeft( _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
  862. }
  863. } else {
  864. if ( this.enablePan ) {
  865. this._pan( this.keyPanSpeed, 0 );
  866. }
  867. }
  868. needsUpdate = true;
  869. break;
  870. case this.keys.RIGHT:
  871. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  872. if ( this.enableRotate ) {
  873. this._rotateLeft( - _twoPI * this.keyRotateSpeed / this.domElement.clientHeight );
  874. }
  875. } else {
  876. if ( this.enablePan ) {
  877. this._pan( - this.keyPanSpeed, 0 );
  878. }
  879. }
  880. needsUpdate = true;
  881. break;
  882. }
  883. if ( needsUpdate ) {
  884. // prevent the browser from scrolling on cursor keys
  885. event.preventDefault();
  886. this.update();
  887. }
  888. }
  889. _handleTouchStartRotate( event ) {
  890. if ( this._pointers.length === 1 ) {
  891. this._rotateStart.set( event.pageX, event.pageY );
  892. } else {
  893. const position = this._getSecondPointerPosition( event );
  894. const x = 0.5 * ( event.pageX + position.x );
  895. const y = 0.5 * ( event.pageY + position.y );
  896. this._rotateStart.set( x, y );
  897. }
  898. }
  899. _handleTouchStartPan( event ) {
  900. if ( this._pointers.length === 1 ) {
  901. this._panStart.set( event.pageX, event.pageY );
  902. } else {
  903. const position = this._getSecondPointerPosition( event );
  904. const x = 0.5 * ( event.pageX + position.x );
  905. const y = 0.5 * ( event.pageY + position.y );
  906. this._panStart.set( x, y );
  907. }
  908. }
  909. _handleTouchStartDolly( event ) {
  910. const position = this._getSecondPointerPosition( event );
  911. const dx = event.pageX - position.x;
  912. const dy = event.pageY - position.y;
  913. const distance = Math.sqrt( dx * dx + dy * dy );
  914. this._dollyStart.set( 0, distance );
  915. }
  916. _handleTouchStartDollyPan( event ) {
  917. if ( this.enableZoom ) this._handleTouchStartDolly( event );
  918. if ( this.enablePan ) this._handleTouchStartPan( event );
  919. }
  920. _handleTouchStartDollyRotate( event ) {
  921. if ( this.enableZoom ) this._handleTouchStartDolly( event );
  922. if ( this.enableRotate ) this._handleTouchStartRotate( event );
  923. }
  924. _handleTouchMoveRotate( event ) {
  925. if ( this._pointers.length == 1 ) {
  926. this._rotateEnd.set( event.pageX, event.pageY );
  927. } else {
  928. const position = this._getSecondPointerPosition( event );
  929. const x = 0.5 * ( event.pageX + position.x );
  930. const y = 0.5 * ( event.pageY + position.y );
  931. this._rotateEnd.set( x, y );
  932. }
  933. this._rotateDelta.subVectors( this._rotateEnd, this._rotateStart ).multiplyScalar( this.rotateSpeed );
  934. const element = this.domElement;
  935. this._rotateLeft( _twoPI * this._rotateDelta.x / element.clientHeight ); // yes, height
  936. this._rotateUp( _twoPI * this._rotateDelta.y / element.clientHeight );
  937. this._rotateStart.copy( this._rotateEnd );
  938. }
  939. _handleTouchMovePan( event ) {
  940. if ( this._pointers.length === 1 ) {
  941. this._panEnd.set( event.pageX, event.pageY );
  942. } else {
  943. const position = this._getSecondPointerPosition( event );
  944. const x = 0.5 * ( event.pageX + position.x );
  945. const y = 0.5 * ( event.pageY + position.y );
  946. this._panEnd.set( x, y );
  947. }
  948. this._panDelta.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.panSpeed );
  949. this._pan( this._panDelta.x, this._panDelta.y );
  950. this._panStart.copy( this._panEnd );
  951. }
  952. _handleTouchMoveDolly( event ) {
  953. const position = this._getSecondPointerPosition( event );
  954. const dx = event.pageX - position.x;
  955. const dy = event.pageY - position.y;
  956. const distance = Math.sqrt( dx * dx + dy * dy );
  957. this._dollyEnd.set( 0, distance );
  958. this._dollyDelta.set( 0, Math.pow( this._dollyEnd.y / this._dollyStart.y, this.zoomSpeed ) );
  959. this._dollyOut( this._dollyDelta.y );
  960. this._dollyStart.copy( this._dollyEnd );
  961. const centerX = ( event.pageX + position.x ) * 0.5;
  962. const centerY = ( event.pageY + position.y ) * 0.5;
  963. this._updateZoomParameters( centerX, centerY );
  964. }
  965. _handleTouchMoveDollyPan( event ) {
  966. if ( this.enableZoom ) this._handleTouchMoveDolly( event );
  967. if ( this.enablePan ) this._handleTouchMovePan( event );
  968. }
  969. _handleTouchMoveDollyRotate( event ) {
  970. if ( this.enableZoom ) this._handleTouchMoveDolly( event );
  971. if ( this.enableRotate ) this._handleTouchMoveRotate( event );
  972. }
  973. // pointers
  974. _addPointer( event ) {
  975. this._pointers.push( event.pointerId );
  976. }
  977. _removePointer( event ) {
  978. delete this._pointerPositions[ event.pointerId ];
  979. for ( let i = 0; i < this._pointers.length; i ++ ) {
  980. if ( this._pointers[ i ] == event.pointerId ) {
  981. this._pointers.splice( i, 1 );
  982. return;
  983. }
  984. }
  985. }
  986. _isTrackingPointer( event ) {
  987. for ( let i = 0; i < this._pointers.length; i ++ ) {
  988. if ( this._pointers[ i ] == event.pointerId ) return true;
  989. }
  990. return false;
  991. }
  992. _trackPointer( event ) {
  993. let position = this._pointerPositions[ event.pointerId ];
  994. if ( position === undefined ) {
  995. position = new Vector2();
  996. this._pointerPositions[ event.pointerId ] = position;
  997. }
  998. position.set( event.pageX, event.pageY );
  999. }
  1000. _getSecondPointerPosition( event ) {
  1001. const pointerId = ( event.pointerId === this._pointers[ 0 ] ) ? this._pointers[ 1 ] : this._pointers[ 0 ];
  1002. return this._pointerPositions[ pointerId ];
  1003. }
  1004. //
  1005. _customWheelEvent( event ) {
  1006. const mode = event.deltaMode;
  1007. // minimal wheel event altered to meet delta-zoom demand
  1008. const newEvent = {
  1009. clientX: event.clientX,
  1010. clientY: event.clientY,
  1011. deltaY: event.deltaY,
  1012. };
  1013. switch ( mode ) {
  1014. case 1: // LINE_MODE
  1015. newEvent.deltaY *= 16;
  1016. break;
  1017. case 2: // PAGE_MODE
  1018. newEvent.deltaY *= 100;
  1019. break;
  1020. }
  1021. // detect if event was triggered by pinching
  1022. if ( event.ctrlKey && ! this._controlActive ) {
  1023. newEvent.deltaY *= 10;
  1024. }
  1025. return newEvent;
  1026. }
  1027. }
  1028. function onPointerDown( event ) {
  1029. if ( this.enabled === false ) return;
  1030. if ( this._pointers.length === 0 ) {
  1031. this.domElement.setPointerCapture( event.pointerId );
  1032. this.domElement.ownerDocument.addEventListener( 'pointermove', this._onPointerMove );
  1033. this.domElement.ownerDocument.addEventListener( 'pointerup', this._onPointerUp );
  1034. }
  1035. //
  1036. if ( this._isTrackingPointer( event ) ) return;
  1037. //
  1038. this._addPointer( event );
  1039. if ( event.pointerType === 'touch' ) {
  1040. this._onTouchStart( event );
  1041. } else {
  1042. this._onMouseDown( event );
  1043. }
  1044. if ( this._cursorStyle === 'grab' ) {
  1045. this.domElement.style.cursor = 'grabbing';
  1046. }
  1047. }
  1048. function onPointerMove( event ) {
  1049. if ( this.enabled === false ) return;
  1050. if ( event.pointerType === 'touch' ) {
  1051. this._onTouchMove( event );
  1052. } else {
  1053. this._onMouseMove( event );
  1054. }
  1055. }
  1056. function onPointerUp( event ) {
  1057. this._removePointer( event );
  1058. switch ( this._pointers.length ) {
  1059. case 0:
  1060. this.domElement.releasePointerCapture( event.pointerId );
  1061. this.domElement.ownerDocument.removeEventListener( 'pointermove', this._onPointerMove );
  1062. this.domElement.ownerDocument.removeEventListener( 'pointerup', this._onPointerUp );
  1063. this.dispatchEvent( _endEvent );
  1064. this.state = _STATE.NONE;
  1065. if ( this._cursorStyle === 'grab' ) {
  1066. this.domElement.style.cursor = 'grab';
  1067. }
  1068. break;
  1069. case 1:
  1070. const pointerId = this._pointers[ 0 ];
  1071. const position = this._pointerPositions[ pointerId ];
  1072. // minimal placeholder event - allows state correction on pointer-up
  1073. this._onTouchStart( { pointerId: pointerId, pageX: position.x, pageY: position.y } );
  1074. break;
  1075. }
  1076. }
  1077. function onMouseDown( event ) {
  1078. let mouseAction;
  1079. switch ( event.button ) {
  1080. case 0:
  1081. mouseAction = this.mouseButtons.LEFT;
  1082. break;
  1083. case 1:
  1084. mouseAction = this.mouseButtons.MIDDLE;
  1085. break;
  1086. case 2:
  1087. mouseAction = this.mouseButtons.RIGHT;
  1088. break;
  1089. default:
  1090. mouseAction = - 1;
  1091. }
  1092. switch ( mouseAction ) {
  1093. case MOUSE.DOLLY:
  1094. if ( this.enableZoom === false ) return;
  1095. this._handleMouseDownDolly( event );
  1096. this.state = _STATE.DOLLY;
  1097. break;
  1098. case MOUSE.ROTATE:
  1099. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  1100. if ( this.enablePan === false ) return;
  1101. this._handleMouseDownPan( event );
  1102. this.state = _STATE.PAN;
  1103. } else {
  1104. if ( this.enableRotate === false ) return;
  1105. this._handleMouseDownRotate( event );
  1106. this.state = _STATE.ROTATE;
  1107. }
  1108. break;
  1109. case MOUSE.PAN:
  1110. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  1111. if ( this.enableRotate === false ) return;
  1112. this._handleMouseDownRotate( event );
  1113. this.state = _STATE.ROTATE;
  1114. } else {
  1115. if ( this.enablePan === false ) return;
  1116. this._handleMouseDownPan( event );
  1117. this.state = _STATE.PAN;
  1118. }
  1119. break;
  1120. default:
  1121. this.state = _STATE.NONE;
  1122. }
  1123. if ( this.state !== _STATE.NONE ) {
  1124. this.dispatchEvent( _startEvent );
  1125. }
  1126. }
  1127. function onMouseMove( event ) {
  1128. switch ( this.state ) {
  1129. case _STATE.ROTATE:
  1130. if ( this.enableRotate === false ) return;
  1131. this._handleMouseMoveRotate( event );
  1132. break;
  1133. case _STATE.DOLLY:
  1134. if ( this.enableZoom === false ) return;
  1135. this._handleMouseMoveDolly( event );
  1136. break;
  1137. case _STATE.PAN:
  1138. if ( this.enablePan === false ) return;
  1139. this._handleMouseMovePan( event );
  1140. break;
  1141. }
  1142. }
  1143. function onMouseWheel( event ) {
  1144. if ( this.enabled === false || this.enableZoom === false || this.state !== _STATE.NONE ) return;
  1145. event.preventDefault();
  1146. this.dispatchEvent( _startEvent );
  1147. this._handleMouseWheel( this._customWheelEvent( event ) );
  1148. this.dispatchEvent( _endEvent );
  1149. }
  1150. function onKeyDown( event ) {
  1151. if ( this.enabled === false ) return;
  1152. this._handleKeyDown( event );
  1153. }
  1154. function onTouchStart( event ) {
  1155. this._trackPointer( event );
  1156. switch ( this._pointers.length ) {
  1157. case 1:
  1158. switch ( this.touches.ONE ) {
  1159. case TOUCH.ROTATE:
  1160. if ( this.enableRotate === false ) return;
  1161. this._handleTouchStartRotate( event );
  1162. this.state = _STATE.TOUCH_ROTATE;
  1163. break;
  1164. case TOUCH.PAN:
  1165. if ( this.enablePan === false ) return;
  1166. this._handleTouchStartPan( event );
  1167. this.state = _STATE.TOUCH_PAN;
  1168. break;
  1169. default:
  1170. this.state = _STATE.NONE;
  1171. }
  1172. break;
  1173. case 2:
  1174. switch ( this.touches.TWO ) {
  1175. case TOUCH.DOLLY_PAN:
  1176. if ( this.enableZoom === false && this.enablePan === false ) return;
  1177. this._handleTouchStartDollyPan( event );
  1178. this.state = _STATE.TOUCH_DOLLY_PAN;
  1179. break;
  1180. case TOUCH.DOLLY_ROTATE:
  1181. if ( this.enableZoom === false && this.enableRotate === false ) return;
  1182. this._handleTouchStartDollyRotate( event );
  1183. this.state = _STATE.TOUCH_DOLLY_ROTATE;
  1184. break;
  1185. default:
  1186. this.state = _STATE.NONE;
  1187. }
  1188. break;
  1189. default:
  1190. this.state = _STATE.NONE;
  1191. }
  1192. if ( this.state !== _STATE.NONE ) {
  1193. this.dispatchEvent( _startEvent );
  1194. }
  1195. }
  1196. function onTouchMove( event ) {
  1197. this._trackPointer( event );
  1198. switch ( this.state ) {
  1199. case _STATE.TOUCH_ROTATE:
  1200. if ( this.enableRotate === false ) return;
  1201. this._handleTouchMoveRotate( event );
  1202. this.update();
  1203. break;
  1204. case _STATE.TOUCH_PAN:
  1205. if ( this.enablePan === false ) return;
  1206. this._handleTouchMovePan( event );
  1207. this.update();
  1208. break;
  1209. case _STATE.TOUCH_DOLLY_PAN:
  1210. if ( this.enableZoom === false && this.enablePan === false ) return;
  1211. this._handleTouchMoveDollyPan( event );
  1212. this.update();
  1213. break;
  1214. case _STATE.TOUCH_DOLLY_ROTATE:
  1215. if ( this.enableZoom === false && this.enableRotate === false ) return;
  1216. this._handleTouchMoveDollyRotate( event );
  1217. this.update();
  1218. break;
  1219. default:
  1220. this.state = _STATE.NONE;
  1221. }
  1222. }
  1223. function onContextMenu( event ) {
  1224. if ( this.enabled === false ) return;
  1225. event.preventDefault();
  1226. }
  1227. function interceptControlDown( event ) {
  1228. if ( event.key === 'Control' ) {
  1229. this._controlActive = true;
  1230. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  1231. document.addEventListener( 'keyup', this._interceptControlUp, { passive: true, capture: true } );
  1232. }
  1233. }
  1234. function interceptControlUp( event ) {
  1235. if ( event.key === 'Control' ) {
  1236. this._controlActive = false;
  1237. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  1238. document.removeEventListener( 'keyup', this._interceptControlUp, { passive: true, capture: true } );
  1239. }
  1240. }
  1241. export { OrbitControls };
粤ICP备19079148号