OrbitControls.js 38 KB

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