OrbitControls.js 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  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. // OrbitControls performs orbiting, dollying (zooming), and panning.
  14. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  15. //
  16. // Orbit - left mouse / touch: one-finger move
  17. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  18. // Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move
  19. const _changeEvent = { type: 'change' };
  20. const _startEvent = { type: 'start' };
  21. const _endEvent = { type: 'end' };
  22. const _ray = new Ray();
  23. const _plane = new Plane();
  24. const _TILT_LIMIT = Math.cos( 70 * MathUtils.DEG2RAD );
  25. const _v = new Vector3();
  26. const _twoPI = 2 * Math.PI;
  27. const _STATE = {
  28. NONE: - 1,
  29. ROTATE: 0,
  30. DOLLY: 1,
  31. PAN: 2,
  32. TOUCH_ROTATE: 3,
  33. TOUCH_PAN: 4,
  34. TOUCH_DOLLY_PAN: 5,
  35. TOUCH_DOLLY_ROTATE: 6
  36. };
  37. const _EPS = 0.000001;
  38. class OrbitControls extends Controls {
  39. constructor( object, domElement = null ) {
  40. super( object, domElement );
  41. this.state = _STATE.NONE;
  42. // Set to false to disable this control
  43. this.enabled = true;
  44. // "target" sets the location of focus, where the object orbits around
  45. this.target = new Vector3();
  46. // Sets the 3D cursor (similar to Blender), from which the maxTargetRadius takes effect
  47. this.cursor = new Vector3();
  48. // How far you can dolly in and out ( PerspectiveCamera only )
  49. this.minDistance = 0;
  50. this.maxDistance = Infinity;
  51. // How far you can zoom in and out ( OrthographicCamera only )
  52. this.minZoom = 0;
  53. this.maxZoom = Infinity;
  54. // Limit camera target within a spherical area around the cursor
  55. this.minTargetRadius = 0;
  56. this.maxTargetRadius = Infinity;
  57. // How far you can orbit vertically, upper and lower limits.
  58. // Range is 0 to Math.PI radians.
  59. this.minPolarAngle = 0; // radians
  60. this.maxPolarAngle = Math.PI; // radians
  61. // How far you can orbit horizontally, upper and lower limits.
  62. // If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI )
  63. this.minAzimuthAngle = - Infinity; // radians
  64. this.maxAzimuthAngle = Infinity; // radians
  65. // Set to true to enable damping (inertia)
  66. // If damping is enabled, you must call controls.update() in your animation loop
  67. this.enableDamping = false;
  68. this.dampingFactor = 0.05;
  69. // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
  70. // Set to false to disable zooming
  71. this.enableZoom = true;
  72. this.zoomSpeed = 1.0;
  73. // Set to false to disable rotating
  74. this.enableRotate = true;
  75. this.rotateSpeed = 1.0;
  76. // Set to false to disable panning
  77. this.enablePan = true;
  78. this.panSpeed = 1.0;
  79. this.screenSpacePanning = true; // if false, pan orthogonal to world-space direction camera.up
  80. this.keyPanSpeed = 7.0; // pixels moved per arrow key push
  81. this.zoomToCursor = false;
  82. // Set to true to automatically rotate around the target
  83. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  84. this.autoRotate = false;
  85. this.autoRotateSpeed = 2.0; // 30 seconds per orbit when fps is 60
  86. // The four arrow keys
  87. this.keys = { LEFT: 'ArrowLeft', UP: 'ArrowUp', RIGHT: 'ArrowRight', BOTTOM: 'ArrowDown' };
  88. // Mouse buttons
  89. this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
  90. // Touch fingers
  91. this.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN };
  92. // for reset
  93. this.target0 = this.target.clone();
  94. this.position0 = this.object.position.clone();
  95. this.zoom0 = this.object.zoom;
  96. // the target DOM element for key events
  97. this._domElementKeyEvents = null;
  98. // internals
  99. this._lastPosition = new Vector3();
  100. this._lastQuaternion = new Quaternion();
  101. this._lastTargetPosition = new Vector3();
  102. // so camera.up is the orbit axis
  103. this._quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
  104. this._quatInverse = this._quat.clone().invert();
  105. // current position in spherical coordinates
  106. this._spherical = new Spherical();
  107. this._sphericalDelta = new Spherical();
  108. this._scale = 1;
  109. this._panOffset = new Vector3();
  110. this._rotateStart = new Vector2();
  111. this._rotateEnd = new Vector2();
  112. this._rotateDelta = new Vector2();
  113. this._panStart = new Vector2();
  114. this._panEnd = new Vector2();
  115. this._panDelta = new Vector2();
  116. this._dollyStart = new Vector2();
  117. this._dollyEnd = new Vector2();
  118. this._dollyDelta = new Vector2();
  119. this._dollyDirection = new Vector3();
  120. this._mouse = new Vector2();
  121. this._performCursorZoom = false;
  122. this._pointers = [];
  123. this._pointerPositions = {};
  124. this._controlActive = false;
  125. // event listeners
  126. this._onPointerMove = onPointerMove.bind( this );
  127. this._onPointerDown = onPointerDown.bind( this );
  128. this._onPointerUp = onPointerUp.bind( this );
  129. this._onContextMenu = onContextMenu.bind( this );
  130. this._onMouseWheel = onMouseWheel.bind( this );
  131. this._onKeyDown = onKeyDown.bind( this );
  132. this._onTouchStart = onTouchStart.bind( this );
  133. this._onTouchMove = onTouchMove.bind( this );
  134. this._onMouseDown = onMouseDown.bind( this );
  135. this._onMouseMove = onMouseMove.bind( this );
  136. this._interceptControlDown = interceptControlDown.bind( this );
  137. this._interceptControlUp = interceptControlUp.bind( this );
  138. //
  139. if ( this.domElement !== null ) {
  140. this.connect();
  141. }
  142. this.update();
  143. }
  144. connect() {
  145. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  146. this.domElement.addEventListener( 'pointercancel', this._onPointerUp );
  147. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  148. this.domElement.addEventListener( 'wheel', this._onMouseWheel, { passive: false } );
  149. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  150. document.addEventListener( 'keydown', this._interceptControlDown, { passive: true, capture: true } );
  151. this.domElement.style.touchAction = 'none'; // disable touch scroll
  152. }
  153. disconnect() {
  154. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  155. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  156. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  157. this.domElement.removeEventListener( 'pointercancel', this._onPointerUp );
  158. this.domElement.removeEventListener( 'wheel', this._onMouseWheel );
  159. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  160. this.stopListenToKeyEvents();
  161. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  162. document.removeEventListener( 'keydown', this._interceptControlDown, { capture: true } );
  163. this.domElement.style.touchAction = 'auto';
  164. }
  165. dispose() {
  166. this.disconnect();
  167. }
  168. getPolarAngle() {
  169. return this._spherical.phi;
  170. }
  171. getAzimuthalAngle() {
  172. return this._spherical.theta;
  173. }
  174. getDistance() {
  175. return this.object.position.distanceTo( this.target );
  176. }
  177. listenToKeyEvents( domElement ) {
  178. domElement.addEventListener( 'keydown', this._onKeyDown );
  179. this._domElementKeyEvents = domElement;
  180. }
  181. stopListenToKeyEvents() {
  182. if ( this._domElementKeyEvents !== null ) {
  183. this._domElementKeyEvents.removeEventListener( 'keydown', this._onKeyDown );
  184. this._domElementKeyEvents = null;
  185. }
  186. }
  187. saveState() {
  188. this.target0.copy( this.target );
  189. this.position0.copy( this.object.position );
  190. this.zoom0 = this.object.zoom;
  191. }
  192. reset() {
  193. this.target.copy( this.target0 );
  194. this.object.position.copy( this.position0 );
  195. this.object.zoom = this.zoom0;
  196. this.object.updateProjectionMatrix();
  197. this.dispatchEvent( _changeEvent );
  198. this.update();
  199. this.state = _STATE.NONE;
  200. }
  201. update( deltaTime = null ) {
  202. const position = this.object.position;
  203. _v.copy( position ).sub( this.target );
  204. // rotate offset to "y-axis-is-up" space
  205. _v.applyQuaternion( this._quat );
  206. // angle from z-axis around y-axis
  207. this._spherical.setFromVector3( _v );
  208. if ( this.autoRotate && this.state === _STATE.NONE ) {
  209. this._rotateLeft( this._getAutoRotationAngle( deltaTime ) );
  210. }
  211. if ( this.enableDamping ) {
  212. this._spherical.theta += this._sphericalDelta.theta * this.dampingFactor;
  213. this._spherical.phi += this._sphericalDelta.phi * this.dampingFactor;
  214. } else {
  215. this._spherical.theta += this._sphericalDelta.theta;
  216. this._spherical.phi += this._sphericalDelta.phi;
  217. }
  218. // restrict theta to be between desired limits
  219. let min = this.minAzimuthAngle;
  220. let max = this.maxAzimuthAngle;
  221. if ( isFinite( min ) && isFinite( max ) ) {
  222. if ( min < - Math.PI ) min += _twoPI; else if ( min > Math.PI ) min -= _twoPI;
  223. if ( max < - Math.PI ) max += _twoPI; else if ( max > Math.PI ) max -= _twoPI;
  224. if ( min <= max ) {
  225. this._spherical.theta = Math.max( min, Math.min( max, this._spherical.theta ) );
  226. } else {
  227. this._spherical.theta = ( this._spherical.theta > ( min + max ) / 2 ) ?
  228. Math.max( min, this._spherical.theta ) :
  229. Math.min( max, this._spherical.theta );
  230. }
  231. }
  232. // restrict phi to be between desired limits
  233. this._spherical.phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, this._spherical.phi ) );
  234. this._spherical.makeSafe();
  235. // move target to panned location
  236. if ( this.enableDamping === true ) {
  237. this.target.addScaledVector( this._panOffset, this.dampingFactor );
  238. } else {
  239. this.target.add( this._panOffset );
  240. }
  241. // Limit the target distance from the cursor to create a sphere around the center of interest
  242. this.target.sub( this.cursor );
  243. this.target.clampLength( this.minTargetRadius, this.maxTargetRadius );
  244. this.target.add( this.cursor );
  245. let zoomChanged = false;
  246. // adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera
  247. // we adjust zoom later in these cases
  248. if ( this.zoomToCursor && this._performCursorZoom || this.object.isOrthographicCamera ) {
  249. this._spherical.radius = this._clampDistance( this._spherical.radius );
  250. } else {
  251. const prevRadius = this._spherical.radius;
  252. this._spherical.radius = this._clampDistance( this._spherical.radius * this._scale );
  253. zoomChanged = prevRadius != this._spherical.radius;
  254. }
  255. _v.setFromSpherical( this._spherical );
  256. // rotate offset back to "camera-up-vector-is-up" space
  257. _v.applyQuaternion( this._quatInverse );
  258. position.copy( this.target ).add( _v );
  259. this.object.lookAt( this.target );
  260. if ( this.enableDamping === true ) {
  261. this._sphericalDelta.theta *= ( 1 - this.dampingFactor );
  262. this._sphericalDelta.phi *= ( 1 - this.dampingFactor );
  263. this._panOffset.multiplyScalar( 1 - this.dampingFactor );
  264. } else {
  265. this._sphericalDelta.set( 0, 0, 0 );
  266. this._panOffset.set( 0, 0, 0 );
  267. }
  268. // adjust camera position
  269. if ( this.zoomToCursor && this._performCursorZoom ) {
  270. let newRadius = null;
  271. if ( this.object.isPerspectiveCamera ) {
  272. // move the camera down the pointer ray
  273. // this method avoids floating point error
  274. const prevRadius = _v.length();
  275. newRadius = this._clampDistance( prevRadius * this._scale );
  276. const radiusDelta = prevRadius - newRadius;
  277. this.object.position.addScaledVector( this._dollyDirection, radiusDelta );
  278. this.object.updateMatrixWorld();
  279. zoomChanged = !! radiusDelta;
  280. } else if ( this.object.isOrthographicCamera ) {
  281. // adjust the ortho camera position based on zoom changes
  282. const mouseBefore = new Vector3( this._mouse.x, this._mouse.y, 0 );
  283. mouseBefore.unproject( this.object );
  284. const prevZoom = this.object.zoom;
  285. this.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / this._scale ) );
  286. this.object.updateProjectionMatrix();
  287. zoomChanged = prevZoom !== this.object.zoom;
  288. const mouseAfter = new Vector3( this._mouse.x, this._mouse.y, 0 );
  289. mouseAfter.unproject( this.object );
  290. this.object.position.sub( mouseAfter ).add( mouseBefore );
  291. this.object.updateMatrixWorld();
  292. newRadius = _v.length();
  293. } else {
  294. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled.' );
  295. this.zoomToCursor = false;
  296. }
  297. // handle the placement of the target
  298. if ( newRadius !== null ) {
  299. if ( this.screenSpacePanning ) {
  300. // position the orbit target in front of the new camera position
  301. this.target.set( 0, 0, - 1 )
  302. .transformDirection( this.object.matrix )
  303. .multiplyScalar( newRadius )
  304. .add( this.object.position );
  305. } else {
  306. // get the ray and translation plane to compute target
  307. _ray.origin.copy( this.object.position );
  308. _ray.direction.set( 0, 0, - 1 ).transformDirection( this.object.matrix );
  309. // if the camera is 20 degrees above the horizon then don't adjust the focus target to avoid
  310. // extremely large values
  311. if ( Math.abs( this.object.up.dot( _ray.direction ) ) < _TILT_LIMIT ) {
  312. this.object.lookAt( this.target );
  313. } else {
  314. _plane.setFromNormalAndCoplanarPoint( this.object.up, this.target );
  315. _ray.intersectPlane( _plane, this.target );
  316. }
  317. }
  318. }
  319. } else if ( this.object.isOrthographicCamera ) {
  320. const prevZoom = this.object.zoom;
  321. this.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / this._scale ) );
  322. if ( prevZoom !== this.object.zoom ) {
  323. this.object.updateProjectionMatrix();
  324. zoomChanged = true;
  325. }
  326. }
  327. this._scale = 1;
  328. this._performCursorZoom = false;
  329. // update condition is:
  330. // min(camera displacement, camera rotation in radians)^2 > EPS
  331. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  332. if ( zoomChanged ||
  333. this._lastPosition.distanceToSquared( this.object.position ) > _EPS ||
  334. 8 * ( 1 - this._lastQuaternion.dot( this.object.quaternion ) ) > _EPS ||
  335. this._lastTargetPosition.distanceToSquared( this.target ) > _EPS ) {
  336. this.dispatchEvent( _changeEvent );
  337. this._lastPosition.copy( this.object.position );
  338. this._lastQuaternion.copy( this.object.quaternion );
  339. this._lastTargetPosition.copy( this.target );
  340. return true;
  341. }
  342. return false;
  343. }
  344. _getAutoRotationAngle( deltaTime ) {
  345. if ( deltaTime !== null ) {
  346. return ( _twoPI / 60 * this.autoRotateSpeed ) * deltaTime;
  347. } else {
  348. return _twoPI / 60 / 60 * this.autoRotateSpeed;
  349. }
  350. }
  351. _getZoomScale( delta ) {
  352. const normalizedDelta = Math.abs( delta * 0.01 );
  353. return Math.pow( 0.95, this.zoomSpeed * normalizedDelta );
  354. }
  355. _rotateLeft( angle ) {
  356. this._sphericalDelta.theta -= angle;
  357. }
  358. _rotateUp( angle ) {
  359. this._sphericalDelta.phi -= angle;
  360. }
  361. _panLeft( distance, objectMatrix ) {
  362. _v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  363. _v.multiplyScalar( - distance );
  364. this._panOffset.add( _v );
  365. }
  366. _panUp( distance, objectMatrix ) {
  367. if ( this.screenSpacePanning === true ) {
  368. _v.setFromMatrixColumn( objectMatrix, 1 );
  369. } else {
  370. _v.setFromMatrixColumn( objectMatrix, 0 );
  371. _v.crossVectors( this.object.up, _v );
  372. }
  373. _v.multiplyScalar( distance );
  374. this._panOffset.add( _v );
  375. }
  376. // deltaX and deltaY are in pixels; right and down are positive
  377. _pan( deltaX, deltaY ) {
  378. const element = this.domElement;
  379. if ( this.object.isPerspectiveCamera ) {
  380. // perspective
  381. const position = this.object.position;
  382. _v.copy( position ).sub( this.target );
  383. let targetDistance = _v.length();
  384. // half of the fov is center to top of screen
  385. targetDistance *= Math.tan( ( this.object.fov / 2 ) * Math.PI / 180.0 );
  386. // we use only clientHeight here so aspect ratio does not distort speed
  387. this._panLeft( 2 * deltaX * targetDistance / element.clientHeight, this.object.matrix );
  388. this._panUp( 2 * deltaY * targetDistance / element.clientHeight, this.object.matrix );
  389. } else if ( this.object.isOrthographicCamera ) {
  390. // orthographic
  391. this._panLeft( deltaX * ( this.object.right - this.object.left ) / this.object.zoom / element.clientWidth, this.object.matrix );
  392. this._panUp( deltaY * ( this.object.top - this.object.bottom ) / this.object.zoom / element.clientHeight, this.object.matrix );
  393. } else {
  394. // camera neither orthographic nor perspective
  395. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  396. this.enablePan = false;
  397. }
  398. }
  399. _dollyOut( dollyScale ) {
  400. if ( this.object.isPerspectiveCamera || this.object.isOrthographicCamera ) {
  401. this._scale /= dollyScale;
  402. } else {
  403. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  404. this.enableZoom = false;
  405. }
  406. }
  407. _dollyIn( dollyScale ) {
  408. if ( this.object.isPerspectiveCamera || this.object.isOrthographicCamera ) {
  409. this._scale *= dollyScale;
  410. } else {
  411. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  412. this.enableZoom = false;
  413. }
  414. }
  415. _updateZoomParameters( x, y ) {
  416. if ( ! this.zoomToCursor ) {
  417. return;
  418. }
  419. this._performCursorZoom = true;
  420. const rect = this.domElement.getBoundingClientRect();
  421. const dx = x - rect.left;
  422. const dy = y - rect.top;
  423. const w = rect.width;
  424. const h = rect.height;
  425. this._mouse.x = ( dx / w ) * 2 - 1;
  426. this._mouse.y = - ( dy / h ) * 2 + 1;
  427. this._dollyDirection.set( this._mouse.x, this._mouse.y, 1 ).unproject( this.object ).sub( this.object.position ).normalize();
  428. }
  429. _clampDistance( dist ) {
  430. return Math.max( this.minDistance, Math.min( this.maxDistance, dist ) );
  431. }
  432. //
  433. // event callbacks - update the object state
  434. //
  435. _handleMouseDownRotate( event ) {
  436. this._rotateStart.set( event.clientX, event.clientY );
  437. }
  438. _handleMouseDownDolly( event ) {
  439. this._updateZoomParameters( event.clientX, event.clientX );
  440. this._dollyStart.set( event.clientX, event.clientY );
  441. }
  442. _handleMouseDownPan( event ) {
  443. this._panStart.set( event.clientX, event.clientY );
  444. }
  445. _handleMouseMoveRotate( event ) {
  446. this._rotateEnd.set( event.clientX, event.clientY );
  447. this._rotateDelta.subVectors( this._rotateEnd, this._rotateStart ).multiplyScalar( this.rotateSpeed );
  448. const element = this.domElement;
  449. this._rotateLeft( _twoPI * this._rotateDelta.x / element.clientHeight ); // yes, height
  450. this._rotateUp( _twoPI * this._rotateDelta.y / element.clientHeight );
  451. this._rotateStart.copy( this._rotateEnd );
  452. this.update();
  453. }
  454. _handleMouseMoveDolly( event ) {
  455. this._dollyEnd.set( event.clientX, event.clientY );
  456. this._dollyDelta.subVectors( this._dollyEnd, this._dollyStart );
  457. if ( this._dollyDelta.y > 0 ) {
  458. this._dollyOut( this._getZoomScale( this._dollyDelta.y ) );
  459. } else if ( this._dollyDelta.y < 0 ) {
  460. this._dollyIn( this._getZoomScale( this._dollyDelta.y ) );
  461. }
  462. this._dollyStart.copy( this._dollyEnd );
  463. this.update();
  464. }
  465. _handleMouseMovePan( event ) {
  466. this._panEnd.set( event.clientX, event.clientY );
  467. this._panDelta.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.panSpeed );
  468. this._pan( this._panDelta.x, this._panDelta.y );
  469. this._panStart.copy( this._panEnd );
  470. this.update();
  471. }
  472. _handleMouseWheel( event ) {
  473. this._updateZoomParameters( event.clientX, event.clientY );
  474. if ( event.deltaY < 0 ) {
  475. this._dollyIn( this._getZoomScale( event.deltaY ) );
  476. } else if ( event.deltaY > 0 ) {
  477. this._dollyOut( this._getZoomScale( event.deltaY ) );
  478. }
  479. this.update();
  480. }
  481. _handleKeyDown( event ) {
  482. let needsUpdate = false;
  483. switch ( event.code ) {
  484. case this.keys.UP:
  485. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  486. this._rotateUp( _twoPI * this.rotateSpeed / this.domElement.clientHeight );
  487. } else {
  488. this._pan( 0, this.keyPanSpeed );
  489. }
  490. needsUpdate = true;
  491. break;
  492. case this.keys.BOTTOM:
  493. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  494. this._rotateUp( - _twoPI * this.rotateSpeed / this.domElement.clientHeight );
  495. } else {
  496. this._pan( 0, - this.keyPanSpeed );
  497. }
  498. needsUpdate = true;
  499. break;
  500. case this.keys.LEFT:
  501. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  502. this._rotateLeft( _twoPI * this.rotateSpeed / this.domElement.clientHeight );
  503. } else {
  504. this._pan( this.keyPanSpeed, 0 );
  505. }
  506. needsUpdate = true;
  507. break;
  508. case this.keys.RIGHT:
  509. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  510. this._rotateLeft( - _twoPI * this.rotateSpeed / this.domElement.clientHeight );
  511. } else {
  512. this._pan( - this.keyPanSpeed, 0 );
  513. }
  514. needsUpdate = true;
  515. break;
  516. }
  517. if ( needsUpdate ) {
  518. // prevent the browser from scrolling on cursor keys
  519. event.preventDefault();
  520. this.update();
  521. }
  522. }
  523. _handleTouchStartRotate( event ) {
  524. if ( this._pointers.length === 1 ) {
  525. this._rotateStart.set( event.pageX, event.pageY );
  526. } else {
  527. const position = this._getSecondPointerPosition( event );
  528. const x = 0.5 * ( event.pageX + position.x );
  529. const y = 0.5 * ( event.pageY + position.y );
  530. this._rotateStart.set( x, y );
  531. }
  532. }
  533. _handleTouchStartPan( event ) {
  534. if ( this._pointers.length === 1 ) {
  535. this._panStart.set( event.pageX, event.pageY );
  536. } else {
  537. const position = this._getSecondPointerPosition( event );
  538. const x = 0.5 * ( event.pageX + position.x );
  539. const y = 0.5 * ( event.pageY + position.y );
  540. this._panStart.set( x, y );
  541. }
  542. }
  543. _handleTouchStartDolly( event ) {
  544. const position = this._getSecondPointerPosition( event );
  545. const dx = event.pageX - position.x;
  546. const dy = event.pageY - position.y;
  547. const distance = Math.sqrt( dx * dx + dy * dy );
  548. this._dollyStart.set( 0, distance );
  549. }
  550. _handleTouchStartDollyPan( event ) {
  551. if ( this.enableZoom ) this._handleTouchStartDolly( event );
  552. if ( this.enablePan ) this._handleTouchStartPan( event );
  553. }
  554. _handleTouchStartDollyRotate( event ) {
  555. if ( this.enableZoom ) this._handleTouchStartDolly( event );
  556. if ( this.enableRotate ) this._handleTouchStartRotate( event );
  557. }
  558. _handleTouchMoveRotate( event ) {
  559. if ( this._pointers.length == 1 ) {
  560. this._rotateEnd.set( event.pageX, event.pageY );
  561. } else {
  562. const position = this._getSecondPointerPosition( event );
  563. const x = 0.5 * ( event.pageX + position.x );
  564. const y = 0.5 * ( event.pageY + position.y );
  565. this._rotateEnd.set( x, y );
  566. }
  567. this._rotateDelta.subVectors( this._rotateEnd, this._rotateStart ).multiplyScalar( this.rotateSpeed );
  568. const element = this.domElement;
  569. this._rotateLeft( _twoPI * this._rotateDelta.x / element.clientHeight ); // yes, height
  570. this._rotateUp( _twoPI * this._rotateDelta.y / element.clientHeight );
  571. this._rotateStart.copy( this._rotateEnd );
  572. }
  573. _handleTouchMovePan( event ) {
  574. if ( this._pointers.length === 1 ) {
  575. this._panEnd.set( event.pageX, event.pageY );
  576. } else {
  577. const position = this._getSecondPointerPosition( event );
  578. const x = 0.5 * ( event.pageX + position.x );
  579. const y = 0.5 * ( event.pageY + position.y );
  580. this._panEnd.set( x, y );
  581. }
  582. this._panDelta.subVectors( this._panEnd, this._panStart ).multiplyScalar( this.panSpeed );
  583. this._pan( this._panDelta.x, this._panDelta.y );
  584. this._panStart.copy( this._panEnd );
  585. }
  586. _handleTouchMoveDolly( event ) {
  587. const position = this._getSecondPointerPosition( event );
  588. const dx = event.pageX - position.x;
  589. const dy = event.pageY - position.y;
  590. const distance = Math.sqrt( dx * dx + dy * dy );
  591. this._dollyEnd.set( 0, distance );
  592. this._dollyDelta.set( 0, Math.pow( this._dollyEnd.y / this._dollyStart.y, this.zoomSpeed ) );
  593. this._dollyOut( this._dollyDelta.y );
  594. this._dollyStart.copy( this._dollyEnd );
  595. const centerX = ( event.pageX + position.x ) * 0.5;
  596. const centerY = ( event.pageY + position.y ) * 0.5;
  597. this._updateZoomParameters( centerX, centerY );
  598. }
  599. _handleTouchMoveDollyPan( event ) {
  600. if ( this.enableZoom ) this._handleTouchMoveDolly( event );
  601. if ( this.enablePan ) this._handleTouchMovePan( event );
  602. }
  603. _handleTouchMoveDollyRotate( event ) {
  604. if ( this.enableZoom ) this._handleTouchMoveDolly( event );
  605. if ( this.enableRotate ) this._handleTouchMoveRotate( event );
  606. }
  607. // pointers
  608. _addPointer( event ) {
  609. this._pointers.push( event.pointerId );
  610. }
  611. _removePointer( event ) {
  612. delete this._pointerPositions[ event.pointerId ];
  613. for ( let i = 0; i < this._pointers.length; i ++ ) {
  614. if ( this._pointers[ i ] == event.pointerId ) {
  615. this._pointers.splice( i, 1 );
  616. return;
  617. }
  618. }
  619. }
  620. _isTrackingPointer( event ) {
  621. for ( let i = 0; i < this._pointers.length; i ++ ) {
  622. if ( this._pointers[ i ] == event.pointerId ) return true;
  623. }
  624. return false;
  625. }
  626. _trackPointer( event ) {
  627. let position = this._pointerPositions[ event.pointerId ];
  628. if ( position === undefined ) {
  629. position = new Vector2();
  630. this._pointerPositions[ event.pointerId ] = position;
  631. }
  632. position.set( event.pageX, event.pageY );
  633. }
  634. _getSecondPointerPosition( event ) {
  635. const pointerId = ( event.pointerId === this._pointers[ 0 ] ) ? this._pointers[ 1 ] : this._pointers[ 0 ];
  636. return this._pointerPositions[ pointerId ];
  637. }
  638. //
  639. _customWheelEvent( event ) {
  640. const mode = event.deltaMode;
  641. // minimal wheel event altered to meet delta-zoom demand
  642. const newEvent = {
  643. clientX: event.clientX,
  644. clientY: event.clientY,
  645. deltaY: event.deltaY,
  646. };
  647. switch ( mode ) {
  648. case 1: // LINE_MODE
  649. newEvent.deltaY *= 16;
  650. break;
  651. case 2: // PAGE_MODE
  652. newEvent.deltaY *= 100;
  653. break;
  654. }
  655. // detect if event was triggered by pinching
  656. if ( event.ctrlKey && ! this._controlActive ) {
  657. newEvent.deltaY *= 10;
  658. }
  659. return newEvent;
  660. }
  661. }
  662. function onPointerDown( event ) {
  663. if ( this.enabled === false ) return;
  664. if ( this._pointers.length === 0 ) {
  665. this.domElement.setPointerCapture( event.pointerId );
  666. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  667. this.domElement.addEventListener( 'pointerup', this._onPointerUp );
  668. }
  669. //
  670. if ( this._isTrackingPointer( event ) ) return;
  671. //
  672. this._addPointer( event );
  673. if ( event.pointerType === 'touch' ) {
  674. this._onTouchStart( event );
  675. } else {
  676. this._onMouseDown( event );
  677. }
  678. }
  679. function onPointerMove( event ) {
  680. if ( this.enabled === false ) return;
  681. if ( event.pointerType === 'touch' ) {
  682. this._onTouchMove( event );
  683. } else {
  684. this._onMouseMove( event );
  685. }
  686. }
  687. function onPointerUp( event ) {
  688. this._removePointer( event );
  689. switch ( this._pointers.length ) {
  690. case 0:
  691. this.domElement.releasePointerCapture( event.pointerId );
  692. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  693. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  694. this.dispatchEvent( _endEvent );
  695. this.state = _STATE.NONE;
  696. break;
  697. case 1:
  698. const pointerId = this._pointers[ 0 ];
  699. const position = this._pointerPositions[ pointerId ];
  700. // minimal placeholder event - allows state correction on pointer-up
  701. this._onTouchStart( { pointerId: pointerId, pageX: position.x, pageY: position.y } );
  702. break;
  703. }
  704. }
  705. function onMouseDown( event ) {
  706. let mouseAction;
  707. switch ( event.button ) {
  708. case 0:
  709. mouseAction = this.mouseButtons.LEFT;
  710. break;
  711. case 1:
  712. mouseAction = this.mouseButtons.MIDDLE;
  713. break;
  714. case 2:
  715. mouseAction = this.mouseButtons.RIGHT;
  716. break;
  717. default:
  718. mouseAction = - 1;
  719. }
  720. switch ( mouseAction ) {
  721. case MOUSE.DOLLY:
  722. if ( this.enableZoom === false ) return;
  723. this._handleMouseDownDolly( event );
  724. this.state = _STATE.DOLLY;
  725. break;
  726. case MOUSE.ROTATE:
  727. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  728. if ( this.enablePan === false ) return;
  729. this._handleMouseDownPan( event );
  730. this.state = _STATE.PAN;
  731. } else {
  732. if ( this.enableRotate === false ) return;
  733. this._handleMouseDownRotate( event );
  734. this.state = _STATE.ROTATE;
  735. }
  736. break;
  737. case MOUSE.PAN:
  738. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  739. if ( this.enableRotate === false ) return;
  740. this._handleMouseDownRotate( event );
  741. this.state = _STATE.ROTATE;
  742. } else {
  743. if ( this.enablePan === false ) return;
  744. this._handleMouseDownPan( event );
  745. this.state = _STATE.PAN;
  746. }
  747. break;
  748. default:
  749. this.state = _STATE.NONE;
  750. }
  751. if ( this.state !== _STATE.NONE ) {
  752. this.dispatchEvent( _startEvent );
  753. }
  754. }
  755. function onMouseMove( event ) {
  756. switch ( this.state ) {
  757. case _STATE.ROTATE:
  758. if ( this.enableRotate === false ) return;
  759. this._handleMouseMoveRotate( event );
  760. break;
  761. case _STATE.DOLLY:
  762. if ( this.enableZoom === false ) return;
  763. this._handleMouseMoveDolly( event );
  764. break;
  765. case _STATE.PAN:
  766. if ( this.enablePan === false ) return;
  767. this._handleMouseMovePan( event );
  768. break;
  769. }
  770. }
  771. function onMouseWheel( event ) {
  772. if ( this.enabled === false || this.enableZoom === false || this.state !== _STATE.NONE ) return;
  773. event.preventDefault();
  774. this.dispatchEvent( _startEvent );
  775. this._handleMouseWheel( this._customWheelEvent( event ) );
  776. this.dispatchEvent( _endEvent );
  777. }
  778. function onKeyDown( event ) {
  779. if ( this.enabled === false || this.enablePan === false ) return;
  780. this._handleKeyDown( event );
  781. }
  782. function onTouchStart( event ) {
  783. this._trackPointer( event );
  784. switch ( this._pointers.length ) {
  785. case 1:
  786. switch ( this.touches.ONE ) {
  787. case TOUCH.ROTATE:
  788. if ( this.enableRotate === false ) return;
  789. this._handleTouchStartRotate( event );
  790. this.state = _STATE.TOUCH_ROTATE;
  791. break;
  792. case TOUCH.PAN:
  793. if ( this.enablePan === false ) return;
  794. this._handleTouchStartPan( event );
  795. this.state = _STATE.TOUCH_PAN;
  796. break;
  797. default:
  798. this.state = _STATE.NONE;
  799. }
  800. break;
  801. case 2:
  802. switch ( this.touches.TWO ) {
  803. case TOUCH.DOLLY_PAN:
  804. if ( this.enableZoom === false && this.enablePan === false ) return;
  805. this._handleTouchStartDollyPan( event );
  806. this.state = _STATE.TOUCH_DOLLY_PAN;
  807. break;
  808. case TOUCH.DOLLY_ROTATE:
  809. if ( this.enableZoom === false && this.enableRotate === false ) return;
  810. this._handleTouchStartDollyRotate( event );
  811. this.state = _STATE.TOUCH_DOLLY_ROTATE;
  812. break;
  813. default:
  814. this.state = _STATE.NONE;
  815. }
  816. break;
  817. default:
  818. this.state = _STATE.NONE;
  819. }
  820. if ( this.state !== _STATE.NONE ) {
  821. this.dispatchEvent( _startEvent );
  822. }
  823. }
  824. function onTouchMove( event ) {
  825. this._trackPointer( event );
  826. switch ( this.state ) {
  827. case _STATE.TOUCH_ROTATE:
  828. if ( this.enableRotate === false ) return;
  829. this._handleTouchMoveRotate( event );
  830. this.update();
  831. break;
  832. case _STATE.TOUCH_PAN:
  833. if ( this.enablePan === false ) return;
  834. this._handleTouchMovePan( event );
  835. this.update();
  836. break;
  837. case _STATE.TOUCH_DOLLY_PAN:
  838. if ( this.enableZoom === false && this.enablePan === false ) return;
  839. this._handleTouchMoveDollyPan( event );
  840. this.update();
  841. break;
  842. case _STATE.TOUCH_DOLLY_ROTATE:
  843. if ( this.enableZoom === false && this.enableRotate === false ) return;
  844. this._handleTouchMoveDollyRotate( event );
  845. this.update();
  846. break;
  847. default:
  848. this.state = _STATE.NONE;
  849. }
  850. }
  851. function onContextMenu( event ) {
  852. if ( this.enabled === false ) return;
  853. event.preventDefault();
  854. }
  855. function interceptControlDown( event ) {
  856. if ( event.key === 'Control' ) {
  857. this._controlActive = true;
  858. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  859. document.addEventListener( 'keyup', this._interceptControlUp, { passive: true, capture: true } );
  860. }
  861. }
  862. function interceptControlUp( event ) {
  863. if ( event.key === 'Control' ) {
  864. this._controlActive = false;
  865. const document = this.domElement.getRootNode(); // offscreen canvas compatibility
  866. document.removeEventListener( 'keyup', this._interceptControlUp, { passive: true, capture: true } );
  867. }
  868. }
  869. export { OrbitControls };