OrbitControls.js 32 KB

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