@thatopen/components 3.2.6 → 3.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -11988,6 +11988,14 @@ class OrthoPerspectiveCamera extends SimpleCamera {
11988
11988
  this.disableUserInput();
11989
11989
  }
11990
11990
  }
11991
+ /**
11992
+ * Adds a custom {@link NavigationMode} to the camera that can be used using the {@link OrthoPerspectiveCamera.set} method.
11993
+ *
11994
+ * @param mode - The custom {@link NavigationMode} to add.
11995
+ */
11996
+ addCustomNavigationMode(mode) {
11997
+ this._navigationModes.set(mode.id, mode);
11998
+ }
11991
11999
  disableUserInput() {
11992
12000
  this._userInputButtons.left = this.controls.mouseButtons.left;
11993
12001
  this._userInputButtons.right = this.controls.mouseButtons.right;
package/dist/index.d.ts CHANGED
@@ -2648,7 +2648,7 @@ export declare class OrthoPerspectiveCamera extends SimpleCamera {
2648
2648
  readonly threePersp: THREE.PerspectiveCamera;
2649
2649
  protected readonly _userInputButtons: any;
2650
2650
  protected readonly _frustumSize = 50;
2651
- protected readonly _navigationModes: Map<NavModeID, NavigationMode>;
2651
+ protected readonly _navigationModes: Map<string, NavigationMode>;
2652
2652
  protected _mode: NavigationMode | null;
2653
2653
  private previousSize;
2654
2654
  /**
@@ -2668,7 +2668,7 @@ export declare class OrthoPerspectiveCamera extends SimpleCamera {
2668
2668
  *
2669
2669
  * @param mode - The {@link NavigationMode} to set.
2670
2670
  */
2671
- set(mode: NavModeID): void;
2671
+ set(mode: string): void;
2672
2672
  /**
2673
2673
  * Make the camera view fit all the specified meshes.
2674
2674
  *
@@ -2683,6 +2683,12 @@ export declare class OrthoPerspectiveCamera extends SimpleCamera {
2683
2683
  * @param active - whether to enable or disable user inputs.
2684
2684
  */
2685
2685
  setUserInput(active: boolean): void;
2686
+ /**
2687
+ * Adds a custom {@link NavigationMode} to the camera that can be used using the {@link OrthoPerspectiveCamera.set} method.
2688
+ *
2689
+ * @param mode - The custom {@link NavigationMode} to add.
2690
+ */
2691
+ addCustomNavigationMode(mode: NavigationMode): void;
2686
2692
  private disableUserInput;
2687
2693
  private enableUserInput;
2688
2694
  private newOrthoCamera;
@@ -5,7 +5,7 @@
5
5
  * (c) 2017 @yomotsu
6
6
  * Released under the MIT License.
7
7
  */
8
- const ye=1,we=2,ve=4,be=Object.freeze({NONE:0,ROTATE:1,TRUCK:2,SCREEN_PAN:4,OFFSET:8,DOLLY:16,ZOOM:32,TOUCH_ROTATE:64,TOUCH_TRUCK:128,TOUCH_SCREEN_PAN:256,TOUCH_OFFSET:512,TOUCH_DOLLY:1024,TOUCH_ZOOM:2048,TOUCH_DOLLY_TRUCK:4096,TOUCH_DOLLY_SCREEN_PAN:8192,TOUCH_DOLLY_OFFSET:16384,TOUCH_DOLLY_ROTATE:32768,TOUCH_ZOOM_TRUCK:65536,TOUCH_ZOOM_OFFSET:131072,TOUCH_ZOOM_SCREEN_PAN:262144,TOUCH_ZOOM_ROTATE:524288}),xe=0,Ee=1,Te=-1;function Ce(t){return t.isPerspectiveCamera}function Se(t){return t.isOrthographicCamera}const Ae=2*Math.PI,Oe=Math.PI/2,Pe=Math.PI/180;function Ie(t,e,i){return Math.max(e,Math.min(i,t))}function De(t,e=1e-5){return Math.abs(t)<e}function ke(t,e,i=1e-5){return De(t-e,i)}function Me(t,e){return Math.round(t/e)*e}function ze(t){return isFinite(t)?t:t<0?-Number.MAX_VALUE:Number.MAX_VALUE}function Be(t){return Math.abs(t)<Number.MAX_VALUE?t:t*(1/0)}function Ne(t,e,i,s,n=1/0,r){const o=2/(s=Math.max(1e-4,s)),a=o*r,l=1/(1+a+.48*a*a+.235*a*a*a);let h=t-e;const c=e,d=n*s;h=Ie(h,-d,d),e=t-h;const u=(i.value+o*h)*r;i.value=(i.value-o*u)*l;let p=e+(h+u)*l;return c-t>0==p>c&&(p=c,i.value=(p-c)/r),p}function Ue(t,e,i,s,n=1/0,r,o){const a=2/(s=Math.max(1e-4,s)),l=a*r,h=1/(1+l+.48*l*l+.235*l*l*l);let c=e.x,d=e.y,u=e.z,p=t.x-c,f=t.y-d,m=t.z-u;const g=c,_=d,y=u,w=n*s,v=p*p+f*f+m*m;if(v>w*w){const t=Math.sqrt(v);p=p/t*w,f=f/t*w,m=m/t*w}c=t.x-p,d=t.y-f,u=t.z-m;const b=(i.x+a*p)*r,x=(i.y+a*f)*r,E=(i.z+a*m)*r;i.x=(i.x-a*b)*h,i.y=(i.y-a*x)*h,i.z=(i.z-a*E)*h,o.x=c+(p+b)*h,o.y=d+(f+x)*h,o.z=u+(m+E)*h;const T=g-t.x,C=_-t.y,S=y-t.z;return T*(o.x-g)+C*(o.y-_)+S*(o.z-y)>0&&(o.x=g,o.y=_,o.z=y,i.x=(o.x-g)/r,i.y=(o.y-_)/r,i.z=(o.z-y)/r),o}function Le(t,e){e.set(0,0),t.forEach(t=>{e.x+=t.clientX,e.y+=t.clientY}),e.x/=t.length,e.y/=t.length}function Re(t,e){return!!Se(t)&&(console.warn(`${e} is not supported in OrthographicCamera`),!0)}class Ve{constructor(){this._listeners={}}addEventListener(t,e){const i=this._listeners;void 0===i[t]&&(i[t]=[]),-1===i[t].indexOf(e)&&i[t].push(e)}hasEventListener(t,e){const i=this._listeners;return void 0!==i[t]&&-1!==i[t].indexOf(e)}removeEventListener(t,e){const i=this._listeners[t];if(void 0!==i){const t=i.indexOf(e);-1!==t&&i.splice(t,1)}}removeAllEventListeners(t){t?Array.isArray(this._listeners[t])&&(this._listeners[t].length=0):this._listeners={}}dispatchEvent(t){const e=this._listeners[t.type];if(void 0!==e){t.target=this;const i=e.slice(0);for(let e=0,s=i.length;e<s;e++)i[e].call(this,t)}}}var Fe;const je=1/8,He=/Mac/.test(null===(Fe=null===globalThis||void 0===globalThis?void 0:globalThis.navigator)||void 0===Fe?void 0:Fe.platform);let Ze,$e,We,Ye,Ge,qe,Xe,Qe,Ke,Je,ti,ei,ii,si,ni,ri,oi,ai,li,hi,ci,di,ui;class pi extends Ve{static install(t){Ze=t.THREE,$e=Object.freeze(new Ze.Vector3(0,0,0)),We=Object.freeze(new Ze.Vector3(0,1,0)),Ye=Object.freeze(new Ze.Vector3(0,0,1)),Ge=new Ze.Vector2,qe=new Ze.Vector3,Xe=new Ze.Vector3,Qe=new Ze.Vector3,Ke=new Ze.Vector3,Je=new Ze.Vector3,ti=new Ze.Vector3,ei=new Ze.Vector3,ii=new Ze.Vector3,si=new Ze.Vector3,ni=new Ze.Spherical,ri=new Ze.Spherical,oi=new Ze.Box3,ai=new Ze.Box3,li=new Ze.Sphere,hi=new Ze.Quaternion,ci=new Ze.Quaternion,di=new Ze.Matrix4,ui=new Ze.Raycaster}static get ACTION(){return be}set verticalDragToForward(t){console.warn("camera-controls: `verticalDragToForward` was removed. Use `mouseButtons.left = CameraControls.ACTION.SCREEN_PAN` instead.")}constructor(t,e){super(),this.minPolarAngle=0,this.maxPolarAngle=Math.PI,this.minAzimuthAngle=-1/0,this.maxAzimuthAngle=1/0,this.minDistance=Number.EPSILON,this.maxDistance=1/0,this.infinityDolly=!1,this.minZoom=.01,this.maxZoom=1/0,this.smoothTime=.25,this.draggingSmoothTime=.125,this.maxSpeed=1/0,this.azimuthRotateSpeed=1,this.polarRotateSpeed=1,this.dollySpeed=1,this.dollyDragInverted=!1,this.truckSpeed=2,this.dollyToCursor=!1,this.dragToOffset=!1,this.boundaryFriction=0,this.restThreshold=.01,this.colliderMeshes=[],this.cancel=()=>{},this._enabled=!0,this._state=be.NONE,this._viewport=null,this._changedDolly=0,this._changedZoom=0,this._hasRested=!0,this._boundaryEnclosesCamera=!1,this._needsUpdate=!0,this._updatedLastTime=!1,this._elementRect=new DOMRect,this._isDragging=!1,this._dragNeedsUpdate=!0,this._activePointers=[],this._lockedPointer=null,this._interactiveArea=new DOMRect(0,0,1,1),this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._isUserControllingOffset=!1,this._isUserControllingZoom=!1,this._lastDollyDirection=xe,this._thetaVelocity={value:0},this._phiVelocity={value:0},this._radiusVelocity={value:0},this._targetVelocity=new Ze.Vector3,this._focalOffsetVelocity=new Ze.Vector3,this._zoomVelocity={value:0},this._truckInternal=(t,e,i,s)=>{let n,r;if(Ce(this._camera)){const i=qe.copy(this._camera.position).sub(this._target),s=this._camera.getEffectiveFOV()*Pe,o=i.length()*Math.tan(.5*s);n=this.truckSpeed*t*o/this._elementRect.height,r=this.truckSpeed*e*o/this._elementRect.height}else{if(!Se(this._camera))return;{const i=this._camera;n=this.truckSpeed*t*(i.right-i.left)/i.zoom/this._elementRect.width,r=this.truckSpeed*e*(i.top-i.bottom)/i.zoom/this._elementRect.height}}s?(i?this.setFocalOffset(this._focalOffsetEnd.x+n,this._focalOffsetEnd.y,this._focalOffsetEnd.z,!0):this.truck(n,0,!0),this.forward(-r,!0)):i?this.setFocalOffset(this._focalOffsetEnd.x+n,this._focalOffsetEnd.y+r,this._focalOffsetEnd.z,!0):this.truck(n,r,!0)},this._rotateInternal=(t,e)=>{const i=Ae*this.azimuthRotateSpeed*t/this._elementRect.height,s=Ae*this.polarRotateSpeed*e/this._elementRect.height;this.rotate(i,s,!0)},this._dollyInternal=(t,e,i)=>{const s=Math.pow(.95,-t*this.dollySpeed),n=this._sphericalEnd.radius,r=this._sphericalEnd.radius*s,o=Ie(r,this.minDistance,this.maxDistance),a=o-r;this.infinityDolly&&this.dollyToCursor?this._dollyToNoClamp(r,!0):this.infinityDolly&&!this.dollyToCursor?(this.dollyInFixed(a,!0),this._dollyToNoClamp(o,!0)):this._dollyToNoClamp(o,!0),this.dollyToCursor&&(this._changedDolly+=(this.infinityDolly?r:o)-n,this._dollyControlCoord.set(e,i)),this._lastDollyDirection=Math.sign(-t)},this._zoomInternal=(t,e,i)=>{const s=Math.pow(.95,t*this.dollySpeed),n=this._zoom,r=this._zoom*s;this.zoomTo(r,!0),this.dollyToCursor&&(this._changedZoom+=r-n,this._dollyControlCoord.set(e,i))},void 0===Ze&&console.error("camera-controls: `THREE` is undefined. You must first run `CameraControls.install( { THREE: THREE } )`. Check the docs for further information."),this._camera=t,this._yAxisUpSpace=(new Ze.Quaternion).setFromUnitVectors(this._camera.up,We),this._yAxisUpSpaceInverse=this._yAxisUpSpace.clone().invert(),this._state=be.NONE,this._target=new Ze.Vector3,this._targetEnd=this._target.clone(),this._focalOffset=new Ze.Vector3,this._focalOffsetEnd=this._focalOffset.clone(),this._spherical=(new Ze.Spherical).setFromVector3(qe.copy(this._camera.position).applyQuaternion(this._yAxisUpSpace)),this._sphericalEnd=this._spherical.clone(),this._lastDistance=this._spherical.radius,this._zoom=this._camera.zoom,this._zoomEnd=this._zoom,this._lastZoom=this._zoom,this._nearPlaneCorners=[new Ze.Vector3,new Ze.Vector3,new Ze.Vector3,new Ze.Vector3],this._updateNearPlaneCorners(),this._boundary=new Ze.Box3(new Ze.Vector3(-1/0,-1/0,-1/0),new Ze.Vector3(1/0,1/0,1/0)),this._cameraUp0=this._camera.up.clone(),this._target0=this._target.clone(),this._position0=this._camera.position.clone(),this._zoom0=this._zoom,this._focalOffset0=this._focalOffset.clone(),this._dollyControlCoord=new Ze.Vector2,this.mouseButtons={left:be.ROTATE,middle:be.DOLLY,right:be.TRUCK,wheel:Ce(this._camera)?be.DOLLY:Se(this._camera)?be.ZOOM:be.NONE},this.touches={one:be.TOUCH_ROTATE,two:Ce(this._camera)?be.TOUCH_DOLLY_TRUCK:Se(this._camera)?be.TOUCH_ZOOM_TRUCK:be.NONE,three:be.TOUCH_TRUCK};const i=new Ze.Vector2,s=new Ze.Vector2,n=new Ze.Vector2,r=t=>{if(!this._enabled||!this._domElement)return;if(0!==this._interactiveArea.left||0!==this._interactiveArea.top||1!==this._interactiveArea.width||1!==this._interactiveArea.height){const e=this._domElement.getBoundingClientRect(),i=t.clientX/e.width,s=t.clientY/e.height;if(i<this._interactiveArea.left||i>this._interactiveArea.right||s<this._interactiveArea.top||s>this._interactiveArea.bottom)return}const e="mouse"!==t.pointerType?null:(t.buttons&ye)===ye?ye:(t.buttons&ve)===ve?ve:(t.buttons&we)===we?we:null;if(null!==e){const t=this._findPointerByMouseButton(e);t&&this._disposePointer(t)}if((t.buttons&ye)===ye&&this._lockedPointer)return;const i={pointerId:t.pointerId,clientX:t.clientX,clientY:t.clientY,deltaX:0,deltaY:0,mouseButton:e};this._activePointers.push(i),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.ownerDocument.addEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.addEventListener("pointerup",a),this._isDragging=!0,d(t)},o=t=>{t.cancelable&&t.preventDefault();const e=t.pointerId,i=this._lockedPointer||this._findPointerById(e);if(i){if(i.clientX=t.clientX,i.clientY=t.clientY,i.deltaX=t.movementX,i.deltaY=t.movementY,this._state=0,"touch"===t.pointerType)switch(this._activePointers.length){case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else(!this._isDragging&&this._lockedPointer||this._isDragging&&(t.buttons&ye)===ye)&&(this._state=this._state|this.mouseButtons.left),this._isDragging&&(t.buttons&ve)===ve&&(this._state=this._state|this.mouseButtons.middle),this._isDragging&&(t.buttons&we)===we&&(this._state=this._state|this.mouseButtons.right);u()}},a=t=>{const e=this._findPointerById(t.pointerId);if(!e||e!==this._lockedPointer){if(e&&this._disposePointer(e),"touch"===t.pointerType)switch(this._activePointers.length){case 0:this._state=be.NONE;break;case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else this._state=be.NONE;p()}};let l=-1;const h=t=>{if(!this._domElement)return;if(!this._enabled||this.mouseButtons.wheel===be.NONE)return;if(0!==this._interactiveArea.left||0!==this._interactiveArea.top||1!==this._interactiveArea.width||1!==this._interactiveArea.height){const e=this._domElement.getBoundingClientRect(),i=t.clientX/e.width,s=t.clientY/e.height;if(i<this._interactiveArea.left||i>this._interactiveArea.right||s<this._interactiveArea.top||s>this._interactiveArea.bottom)return}if(t.preventDefault(),this.dollyToCursor||this.mouseButtons.wheel===be.ROTATE||this.mouseButtons.wheel===be.TRUCK){const t=performance.now();l-t<1e3&&this._getClientRect(this._elementRect),l=t}const e=He?-1:-3,i=1===t.deltaMode||t.ctrlKey?t.deltaY/e:t.deltaY/(10*e),s=this.dollyToCursor?(t.clientX-this._elementRect.x)/this._elementRect.width*2-1:0,n=this.dollyToCursor?(t.clientY-this._elementRect.y)/this._elementRect.height*-2+1:0;switch(this.mouseButtons.wheel){case be.ROTATE:this._rotateInternal(t.deltaX,t.deltaY),this._isUserControllingRotate=!0;break;case be.TRUCK:this._truckInternal(t.deltaX,t.deltaY,!1,!1),this._isUserControllingTruck=!0;break;case be.SCREEN_PAN:this._truckInternal(t.deltaX,t.deltaY,!1,!0),this._isUserControllingTruck=!0;break;case be.OFFSET:this._truckInternal(t.deltaX,t.deltaY,!0,!1),this._isUserControllingOffset=!0;break;case be.DOLLY:this._dollyInternal(-i,s,n),this._isUserControllingDolly=!0;break;case be.ZOOM:this._zoomInternal(-i,s,n),this._isUserControllingZoom=!0}this.dispatchEvent({type:"control"})},c=t=>{if(this._domElement&&this._enabled){if(this.mouseButtons.right===pi.ACTION.NONE){const e=t instanceof PointerEvent?t.pointerId:0,i=this._findPointerById(e);return i&&this._disposePointer(i),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),void this._domElement.ownerDocument.removeEventListener("pointerup",a)}t.preventDefault()}},d=t=>{if(!this._enabled)return;Le(this._activePointers,Ge),this._getClientRect(this._elementRect),i.copy(Ge),s.copy(Ge);if(this._activePointers.length>=2){const t=Ge.x-this._activePointers[1].clientX,e=Ge.y-this._activePointers[1].clientY,i=Math.sqrt(t*t+e*e);n.set(0,i);const r=.5*(this._activePointers[0].clientX+this._activePointers[1].clientX),o=.5*(this._activePointers[0].clientY+this._activePointers[1].clientY);s.set(r,o)}if(this._state=0,t)if("pointerType"in t&&"touch"===t.pointerType)switch(this._activePointers.length){case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else this._lockedPointer||(t.buttons&ye)!==ye||(this._state=this._state|this.mouseButtons.left),(t.buttons&ve)===ve&&(this._state=this._state|this.mouseButtons.middle),(t.buttons&we)===we&&(this._state=this._state|this.mouseButtons.right);else this._lockedPointer&&(this._state=this._state|this.mouseButtons.left);(this._state&be.ROTATE)!==be.ROTATE&&(this._state&be.TOUCH_ROTATE)!==be.TOUCH_ROTATE&&(this._state&be.TOUCH_DOLLY_ROTATE)!==be.TOUCH_DOLLY_ROTATE&&(this._state&be.TOUCH_ZOOM_ROTATE)!==be.TOUCH_ZOOM_ROTATE||(this._sphericalEnd.theta=this._spherical.theta,this._sphericalEnd.phi=this._spherical.phi,this._thetaVelocity.value=0,this._phiVelocity.value=0),(this._state&be.TRUCK)!==be.TRUCK&&(this._state&be.SCREEN_PAN)!==be.SCREEN_PAN&&(this._state&be.TOUCH_TRUCK)!==be.TOUCH_TRUCK&&(this._state&be.TOUCH_SCREEN_PAN)!==be.TOUCH_SCREEN_PAN&&(this._state&be.TOUCH_DOLLY_TRUCK)!==be.TOUCH_DOLLY_TRUCK&&(this._state&be.TOUCH_DOLLY_SCREEN_PAN)!==be.TOUCH_DOLLY_SCREEN_PAN&&(this._state&be.TOUCH_ZOOM_TRUCK)!==be.TOUCH_ZOOM_TRUCK&&(this._state&be.TOUCH_ZOOM_SCREEN_PAN)!==be.TOUCH_DOLLY_SCREEN_PAN||(this._targetEnd.copy(this._target),this._targetVelocity.set(0,0,0)),(this._state&be.DOLLY)!==be.DOLLY&&(this._state&be.TOUCH_DOLLY)!==be.TOUCH_DOLLY&&(this._state&be.TOUCH_DOLLY_TRUCK)!==be.TOUCH_DOLLY_TRUCK&&(this._state&be.TOUCH_DOLLY_SCREEN_PAN)!==be.TOUCH_DOLLY_SCREEN_PAN&&(this._state&be.TOUCH_DOLLY_OFFSET)!==be.TOUCH_DOLLY_OFFSET&&(this._state&be.TOUCH_DOLLY_ROTATE)!==be.TOUCH_DOLLY_ROTATE||(this._sphericalEnd.radius=this._spherical.radius,this._radiusVelocity.value=0),(this._state&be.ZOOM)!==be.ZOOM&&(this._state&be.TOUCH_ZOOM)!==be.TOUCH_ZOOM&&(this._state&be.TOUCH_ZOOM_TRUCK)!==be.TOUCH_ZOOM_TRUCK&&(this._state&be.TOUCH_ZOOM_SCREEN_PAN)!==be.TOUCH_ZOOM_SCREEN_PAN&&(this._state&be.TOUCH_ZOOM_OFFSET)!==be.TOUCH_ZOOM_OFFSET&&(this._state&be.TOUCH_ZOOM_ROTATE)!==be.TOUCH_ZOOM_ROTATE||(this._zoomEnd=this._zoom,this._zoomVelocity.value=0),(this._state&be.OFFSET)!==be.OFFSET&&(this._state&be.TOUCH_OFFSET)!==be.TOUCH_OFFSET&&(this._state&be.TOUCH_DOLLY_OFFSET)!==be.TOUCH_DOLLY_OFFSET&&(this._state&be.TOUCH_ZOOM_OFFSET)!==be.TOUCH_ZOOM_OFFSET||(this._focalOffsetEnd.copy(this._focalOffset),this._focalOffsetVelocity.set(0,0,0)),this.dispatchEvent({type:"controlstart"})},u=()=>{if(!this._enabled||!this._dragNeedsUpdate)return;this._dragNeedsUpdate=!1,Le(this._activePointers,Ge);const t=this._domElement&&this._domElement.ownerDocument.pointerLockElement===this._domElement?this._lockedPointer||this._activePointers[0]:null,e=t?-t.deltaX:s.x-Ge.x,r=t?-t.deltaY:s.y-Ge.y;if(s.copy(Ge),(this._state&be.ROTATE)!==be.ROTATE&&(this._state&be.TOUCH_ROTATE)!==be.TOUCH_ROTATE&&(this._state&be.TOUCH_DOLLY_ROTATE)!==be.TOUCH_DOLLY_ROTATE&&(this._state&be.TOUCH_ZOOM_ROTATE)!==be.TOUCH_ZOOM_ROTATE||(this._rotateInternal(e,r),this._isUserControllingRotate=!0),(this._state&be.DOLLY)===be.DOLLY||(this._state&be.ZOOM)===be.ZOOM){const t=this.dollyToCursor?(i.x-this._elementRect.x)/this._elementRect.width*2-1:0,e=this.dollyToCursor?(i.y-this._elementRect.y)/this._elementRect.height*-2+1:0,s=this.dollyDragInverted?-1:1;(this._state&be.DOLLY)===be.DOLLY?(this._dollyInternal(s*r*je,t,e),this._isUserControllingDolly=!0):(this._zoomInternal(s*r*je,t,e),this._isUserControllingZoom=!0)}if((this._state&be.TOUCH_DOLLY)===be.TOUCH_DOLLY||(this._state&be.TOUCH_ZOOM)===be.TOUCH_ZOOM||(this._state&be.TOUCH_DOLLY_TRUCK)===be.TOUCH_DOLLY_TRUCK||(this._state&be.TOUCH_ZOOM_TRUCK)===be.TOUCH_ZOOM_TRUCK||(this._state&be.TOUCH_DOLLY_SCREEN_PAN)===be.TOUCH_DOLLY_SCREEN_PAN||(this._state&be.TOUCH_ZOOM_SCREEN_PAN)===be.TOUCH_ZOOM_SCREEN_PAN||(this._state&be.TOUCH_DOLLY_OFFSET)===be.TOUCH_DOLLY_OFFSET||(this._state&be.TOUCH_ZOOM_OFFSET)===be.TOUCH_ZOOM_OFFSET||(this._state&be.TOUCH_DOLLY_ROTATE)===be.TOUCH_DOLLY_ROTATE||(this._state&be.TOUCH_ZOOM_ROTATE)===be.TOUCH_ZOOM_ROTATE){const t=Ge.x-this._activePointers[1].clientX,e=Ge.y-this._activePointers[1].clientY,i=Math.sqrt(t*t+e*e),r=n.y-i;n.set(0,i);const o=this.dollyToCursor?(s.x-this._elementRect.x)/this._elementRect.width*2-1:0,a=this.dollyToCursor?(s.y-this._elementRect.y)/this._elementRect.height*-2+1:0;(this._state&be.TOUCH_DOLLY)===be.TOUCH_DOLLY||(this._state&be.TOUCH_DOLLY_ROTATE)===be.TOUCH_DOLLY_ROTATE||(this._state&be.TOUCH_DOLLY_TRUCK)===be.TOUCH_DOLLY_TRUCK||(this._state&be.TOUCH_DOLLY_SCREEN_PAN)===be.TOUCH_DOLLY_SCREEN_PAN||(this._state&be.TOUCH_DOLLY_OFFSET)===be.TOUCH_DOLLY_OFFSET?(this._dollyInternal(r*je,o,a),this._isUserControllingDolly=!0):(this._zoomInternal(r*je,o,a),this._isUserControllingZoom=!0)}(this._state&be.TRUCK)!==be.TRUCK&&(this._state&be.TOUCH_TRUCK)!==be.TOUCH_TRUCK&&(this._state&be.TOUCH_DOLLY_TRUCK)!==be.TOUCH_DOLLY_TRUCK&&(this._state&be.TOUCH_ZOOM_TRUCK)!==be.TOUCH_ZOOM_TRUCK||(this._truckInternal(e,r,!1,!1),this._isUserControllingTruck=!0),(this._state&be.SCREEN_PAN)!==be.SCREEN_PAN&&(this._state&be.TOUCH_SCREEN_PAN)!==be.TOUCH_SCREEN_PAN&&(this._state&be.TOUCH_DOLLY_SCREEN_PAN)!==be.TOUCH_DOLLY_SCREEN_PAN&&(this._state&be.TOUCH_ZOOM_SCREEN_PAN)!==be.TOUCH_ZOOM_SCREEN_PAN||(this._truckInternal(e,r,!1,!0),this._isUserControllingTruck=!0),(this._state&be.OFFSET)!==be.OFFSET&&(this._state&be.TOUCH_OFFSET)!==be.TOUCH_OFFSET&&(this._state&be.TOUCH_DOLLY_OFFSET)!==be.TOUCH_DOLLY_OFFSET&&(this._state&be.TOUCH_ZOOM_OFFSET)!==be.TOUCH_ZOOM_OFFSET||(this._truckInternal(e,r,!0,!1),this._isUserControllingOffset=!0),this.dispatchEvent({type:"control"})},p=()=>{Le(this._activePointers,Ge),s.copy(Ge),this._dragNeedsUpdate=!1,(0===this._activePointers.length||1===this._activePointers.length&&this._activePointers[0]===this._lockedPointer)&&(this._isDragging=!1),0===this._activePointers.length&&this._domElement&&(this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this.dispatchEvent({type:"controlend"}))};this.lockPointer=()=>{this._enabled&&this._domElement&&(this.cancel(),this._lockedPointer={pointerId:-1,clientX:0,clientY:0,deltaX:0,deltaY:0,mouseButton:null},this._activePointers.push(this._lockedPointer),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.requestPointerLock(),this._domElement.ownerDocument.addEventListener("pointerlockchange",f),this._domElement.ownerDocument.addEventListener("pointerlockerror",m),this._domElement.ownerDocument.addEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.addEventListener("pointerup",a),d())},this.unlockPointer=()=>{var t,e,i;null!==this._lockedPointer&&(this._disposePointer(this._lockedPointer),this._lockedPointer=null),null===(t=this._domElement)||void 0===t||t.ownerDocument.exitPointerLock(),null===(e=this._domElement)||void 0===e||e.ownerDocument.removeEventListener("pointerlockchange",f),null===(i=this._domElement)||void 0===i||i.ownerDocument.removeEventListener("pointerlockerror",m),this.cancel()};const f=()=>{this._domElement&&this._domElement.ownerDocument.pointerLockElement===this._domElement||this.unlockPointer()},m=()=>{this.unlockPointer()};this._addAllEventListeners=t=>{this._domElement=t,this._domElement.style.touchAction="none",this._domElement.style.userSelect="none",this._domElement.style.webkitUserSelect="none",this._domElement.addEventListener("pointerdown",r),this._domElement.addEventListener("pointercancel",a),this._domElement.addEventListener("wheel",h,{passive:!1}),this._domElement.addEventListener("contextmenu",c)},this._removeAllEventListeners=()=>{this._domElement&&(this._domElement.style.touchAction="",this._domElement.style.userSelect="",this._domElement.style.webkitUserSelect="",this._domElement.removeEventListener("pointerdown",r),this._domElement.removeEventListener("pointercancel",a),this._domElement.removeEventListener("wheel",h,{passive:!1}),this._domElement.removeEventListener("contextmenu",c),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.ownerDocument.removeEventListener("pointerlockchange",f),this._domElement.ownerDocument.removeEventListener("pointerlockerror",m))},this.cancel=()=>{this._state!==be.NONE&&(this._state=be.NONE,this._activePointers.length=0,p())},e&&this.connect(e),this.update(0)}get camera(){return this._camera}set camera(t){this._camera=t,this.updateCameraUp(),this._camera.updateProjectionMatrix(),this._updateNearPlaneCorners(),this._needsUpdate=!0}get enabled(){return this._enabled}set enabled(t){this._enabled=t,this._domElement&&(t?(this._domElement.style.touchAction="none",this._domElement.style.userSelect="none",this._domElement.style.webkitUserSelect="none"):(this.cancel(),this._domElement.style.touchAction="",this._domElement.style.userSelect="",this._domElement.style.webkitUserSelect=""))}get active(){return!this._hasRested}get currentAction(){return this._state}get distance(){return this._spherical.radius}set distance(t){this._spherical.radius===t&&this._sphericalEnd.radius===t||(this._spherical.radius=t,this._sphericalEnd.radius=t,this._needsUpdate=!0)}get azimuthAngle(){return this._spherical.theta}set azimuthAngle(t){this._spherical.theta===t&&this._sphericalEnd.theta===t||(this._spherical.theta=t,this._sphericalEnd.theta=t,this._needsUpdate=!0)}get polarAngle(){return this._spherical.phi}set polarAngle(t){this._spherical.phi===t&&this._sphericalEnd.phi===t||(this._spherical.phi=t,this._sphericalEnd.phi=t,this._needsUpdate=!0)}get boundaryEnclosesCamera(){return this._boundaryEnclosesCamera}set boundaryEnclosesCamera(t){this._boundaryEnclosesCamera=t,this._needsUpdate=!0}set interactiveArea(t){this._interactiveArea.width=Ie(t.width,0,1),this._interactiveArea.height=Ie(t.height,0,1),this._interactiveArea.x=Ie(t.x,0,1-this._interactiveArea.width),this._interactiveArea.y=Ie(t.y,0,1-this._interactiveArea.height)}addEventListener(t,e){super.addEventListener(t,e)}removeEventListener(t,e){super.removeEventListener(t,e)}rotate(t,e,i=!1){return this.rotateTo(this._sphericalEnd.theta+t,this._sphericalEnd.phi+e,i)}rotateAzimuthTo(t,e=!1){return this.rotateTo(t,this._sphericalEnd.phi,e)}rotatePolarTo(t,e=!1){return this.rotateTo(this._sphericalEnd.theta,t,e)}rotateTo(t,e,i=!1){this._isUserControllingRotate=!1;const s=Ie(t,this.minAzimuthAngle,this.maxAzimuthAngle),n=Ie(e,this.minPolarAngle,this.maxPolarAngle);this._sphericalEnd.theta=s,this._sphericalEnd.phi=n,this._sphericalEnd.makeSafe(),this._needsUpdate=!0,i||(this._spherical.theta=this._sphericalEnd.theta,this._spherical.phi=this._sphericalEnd.phi);const r=!i||ke(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ke(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold);return this._createOnRestPromise(r)}dolly(t,e=!1){return this.dollyTo(this._sphericalEnd.radius-t,e)}dollyTo(t,e=!1){return this._isUserControllingDolly=!1,this._lastDollyDirection=xe,this._changedDolly=0,this._dollyToNoClamp(Ie(t,this.minDistance,this.maxDistance),e)}_dollyToNoClamp(t,e=!1){const i=this._sphericalEnd.radius;if(this.colliderMeshes.length>=1){const e=this._collisionTest(),s=ke(e,this._spherical.radius);if(!(i>t)&&s)return Promise.resolve();this._sphericalEnd.radius=Math.min(t,e)}else this._sphericalEnd.radius=t;this._needsUpdate=!0,e||(this._spherical.radius=this._sphericalEnd.radius);const s=!e||ke(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(s)}dollyInFixed(t,e=!1){this._targetEnd.add(this._getCameraDirection(Ke).multiplyScalar(t)),e||this._target.copy(this._targetEnd);const i=!e||ke(this._target.x,this._targetEnd.x,this.restThreshold)&&ke(this._target.y,this._targetEnd.y,this.restThreshold)&&ke(this._target.z,this._targetEnd.z,this.restThreshold);return this._createOnRestPromise(i)}zoom(t,e=!1){return this.zoomTo(this._zoomEnd+t,e)}zoomTo(t,e=!1){this._isUserControllingZoom=!1,this._zoomEnd=Ie(t,this.minZoom,this.maxZoom),this._needsUpdate=!0,e||(this._zoom=this._zoomEnd);const i=!e||ke(this._zoom,this._zoomEnd,this.restThreshold);return this._changedZoom=0,this._createOnRestPromise(i)}pan(t,e,i=!1){return console.warn("`pan` has been renamed to `truck`"),this.truck(t,e,i)}truck(t,e,i=!1){this._camera.updateMatrix(),Je.setFromMatrixColumn(this._camera.matrix,0),ti.setFromMatrixColumn(this._camera.matrix,1),Je.multiplyScalar(t),ti.multiplyScalar(-e);const s=qe.copy(Je).add(ti),n=Xe.copy(this._targetEnd).add(s);return this.moveTo(n.x,n.y,n.z,i)}forward(t,e=!1){qe.setFromMatrixColumn(this._camera.matrix,0),qe.crossVectors(this._camera.up,qe),qe.multiplyScalar(t);const i=Xe.copy(this._targetEnd).add(qe);return this.moveTo(i.x,i.y,i.z,e)}elevate(t,e=!1){return qe.copy(this._camera.up).multiplyScalar(t),this.moveTo(this._targetEnd.x+qe.x,this._targetEnd.y+qe.y,this._targetEnd.z+qe.z,e)}moveTo(t,e,i,s=!1){this._isUserControllingTruck=!1;const n=qe.set(t,e,i).sub(this._targetEnd);this._encloseToBoundary(this._targetEnd,n,this.boundaryFriction),this._needsUpdate=!0,s||this._target.copy(this._targetEnd);const r=!s||ke(this._target.x,this._targetEnd.x,this.restThreshold)&&ke(this._target.y,this._targetEnd.y,this.restThreshold)&&ke(this._target.z,this._targetEnd.z,this.restThreshold);return this._createOnRestPromise(r)}lookInDirectionOf(t,e,i,s=!1){const n=qe.set(t,e,i).sub(this._targetEnd).normalize().multiplyScalar(-this._sphericalEnd.radius).add(this._targetEnd);return this.setPosition(n.x,n.y,n.z,s)}fitToBox(t,e,{cover:i=!1,paddingLeft:s=0,paddingRight:n=0,paddingBottom:r=0,paddingTop:o=0}={}){const a=[],l=t.isBox3?oi.copy(t):oi.setFromObject(t);l.isEmpty()&&(console.warn("camera-controls: fitTo() cannot be used with an empty box. Aborting"),Promise.resolve());const h=Me(this._sphericalEnd.theta,Oe),c=Me(this._sphericalEnd.phi,Oe);a.push(this.rotateTo(h,c,e));const d=qe.setFromSpherical(this._sphericalEnd).normalize(),u=hi.setFromUnitVectors(d,Ye),p=ke(Math.abs(d.y),1);p&&u.multiply(ci.setFromAxisAngle(We,h)),u.multiply(this._yAxisUpSpaceInverse);const f=ai.makeEmpty();Xe.copy(l.min).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.min).setX(l.max.x).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.min).setY(l.max.y).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.max).setZ(l.min.z).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.min).setZ(l.max.z).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.max).setY(l.min.y).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.max).setX(l.min.x).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.max).applyQuaternion(u),f.expandByPoint(Xe),f.min.x-=s,f.min.y-=r,f.max.x+=n,f.max.y+=o,u.setFromUnitVectors(Ye,d),p&&u.premultiply(ci.invert()),u.premultiply(this._yAxisUpSpace);const m=f.getSize(qe),g=f.getCenter(Xe).applyQuaternion(u);if(Ce(this._camera)){const t=this.getDistanceToFitBox(m.x,m.y,m.z,i);a.push(this.moveTo(g.x,g.y,g.z,e)),a.push(this.dollyTo(t,e)),a.push(this.setFocalOffset(0,0,0,e))}else if(Se(this._camera)){const t=this._camera,s=t.right-t.left,n=t.top-t.bottom,r=i?Math.max(s/m.x,n/m.y):Math.min(s/m.x,n/m.y);a.push(this.moveTo(g.x,g.y,g.z,e)),a.push(this.zoomTo(r,e)),a.push(this.setFocalOffset(0,0,0,e))}return Promise.all(a)}fitToSphere(t,e){const i=[],s="isObject3D"in t?pi.createBoundingSphere(t,li):li.copy(t);if(i.push(this.moveTo(s.center.x,s.center.y,s.center.z,e)),Ce(this._camera)){const t=this.getDistanceToFitSphere(s.radius);i.push(this.dollyTo(t,e))}else if(Se(this._camera)){const t=this._camera.right-this._camera.left,n=this._camera.top-this._camera.bottom,r=2*s.radius,o=Math.min(t/r,n/r);i.push(this.zoomTo(o,e))}return i.push(this.setFocalOffset(0,0,0,e)),Promise.all(i)}setLookAt(t,e,i,s,n,r,o=!1){this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._lastDollyDirection=xe,this._changedDolly=0;const a=Xe.set(s,n,r),l=qe.set(t,e,i);this._targetEnd.copy(a),this._sphericalEnd.setFromVector3(l.sub(a).applyQuaternion(this._yAxisUpSpace)),this.normalizeRotations(),this._needsUpdate=!0,o||(this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd));const h=!o||ke(this._target.x,this._targetEnd.x,this.restThreshold)&&ke(this._target.y,this._targetEnd.y,this.restThreshold)&&ke(this._target.z,this._targetEnd.z,this.restThreshold)&&ke(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ke(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&ke(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(h)}lerpLookAt(t,e,i,s,n,r,o,a,l,h,c,d,u,p=!1){this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._lastDollyDirection=xe,this._changedDolly=0;const f=qe.set(s,n,r),m=Xe.set(t,e,i);ni.setFromVector3(m.sub(f).applyQuaternion(this._yAxisUpSpace));const g=Qe.set(h,c,d),_=Xe.set(o,a,l);ri.setFromVector3(_.sub(g).applyQuaternion(this._yAxisUpSpace)),this._targetEnd.copy(f.lerp(g,u));const y=ri.theta-ni.theta,w=ri.phi-ni.phi,v=ri.radius-ni.radius;this._sphericalEnd.set(ni.radius+v*u,ni.phi+w*u,ni.theta+y*u),this.normalizeRotations(),this._needsUpdate=!0,p||(this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd));const b=!p||ke(this._target.x,this._targetEnd.x,this.restThreshold)&&ke(this._target.y,this._targetEnd.y,this.restThreshold)&&ke(this._target.z,this._targetEnd.z,this.restThreshold)&&ke(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ke(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&ke(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(b)}setPosition(t,e,i,s=!1){return this.setLookAt(t,e,i,this._targetEnd.x,this._targetEnd.y,this._targetEnd.z,s)}setTarget(t,e,i,s=!1){const n=this.getPosition(qe),r=this.setLookAt(n.x,n.y,n.z,t,e,i,s);return this._sphericalEnd.phi=Ie(this._sphericalEnd.phi,this.minPolarAngle,this.maxPolarAngle),r}setFocalOffset(t,e,i,s=!1){this._isUserControllingOffset=!1,this._focalOffsetEnd.set(t,e,i),this._needsUpdate=!0,s||this._focalOffset.copy(this._focalOffsetEnd);const n=!s||ke(this._focalOffset.x,this._focalOffsetEnd.x,this.restThreshold)&&ke(this._focalOffset.y,this._focalOffsetEnd.y,this.restThreshold)&&ke(this._focalOffset.z,this._focalOffsetEnd.z,this.restThreshold);return this._createOnRestPromise(n)}setOrbitPoint(t,e,i){this._camera.updateMatrixWorld(),Je.setFromMatrixColumn(this._camera.matrixWorldInverse,0),ti.setFromMatrixColumn(this._camera.matrixWorldInverse,1),ei.setFromMatrixColumn(this._camera.matrixWorldInverse,2);const s=qe.set(t,e,i),n=s.distanceTo(this._camera.position),r=s.sub(this._camera.position);Je.multiplyScalar(r.x),ti.multiplyScalar(r.y),ei.multiplyScalar(r.z),qe.copy(Je).add(ti).add(ei),qe.z=qe.z+n,this.dollyTo(n,!1),this.setFocalOffset(-qe.x,qe.y,-qe.z,!1),this.moveTo(t,e,i,!1)}setBoundary(t){if(!t)return this._boundary.min.set(-1/0,-1/0,-1/0),this._boundary.max.set(1/0,1/0,1/0),void(this._needsUpdate=!0);this._boundary.copy(t),this._boundary.clampPoint(this._targetEnd,this._targetEnd),this._needsUpdate=!0}setViewport(t,e,i,s){null!==t?(this._viewport=this._viewport||new Ze.Vector4,"number"==typeof t?this._viewport.set(t,e,i,s):this._viewport.copy(t)):this._viewport=null}getDistanceToFitBox(t,e,i,s=!1){if(Re(this._camera,"getDistanceToFitBox"))return this._spherical.radius;const n=t/e,r=this._camera.getEffectiveFOV()*Pe,o=this._camera.aspect;return.5*((s?n>o:n<o)?e:t/o)/Math.tan(.5*r)+.5*i}getDistanceToFitSphere(t){if(Re(this._camera,"getDistanceToFitSphere"))return this._spherical.radius;const e=this._camera.getEffectiveFOV()*Pe,i=2*Math.atan(Math.tan(.5*e)*this._camera.aspect),s=1<this._camera.aspect?e:i;return t/Math.sin(.5*s)}getTarget(t,e=!0){return(t&&t.isVector3?t:new Ze.Vector3).copy(e?this._targetEnd:this._target)}getPosition(t,e=!0){return(t&&t.isVector3?t:new Ze.Vector3).setFromSpherical(e?this._sphericalEnd:this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(e?this._targetEnd:this._target)}getSpherical(t,e=!0){return(t||new Ze.Spherical).copy(e?this._sphericalEnd:this._spherical)}getFocalOffset(t,e=!0){return(t&&t.isVector3?t:new Ze.Vector3).copy(e?this._focalOffsetEnd:this._focalOffset)}normalizeRotations(){this._sphericalEnd.theta=this._sphericalEnd.theta%Ae,this._sphericalEnd.theta<0&&(this._sphericalEnd.theta+=Ae),this._spherical.theta+=Ae*Math.round((this._sphericalEnd.theta-this._spherical.theta)/Ae)}stop(){this._focalOffset.copy(this._focalOffsetEnd),this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd),this._zoom=this._zoomEnd}reset(t=!1){if(!ke(this._camera.up.x,this._cameraUp0.x)||!ke(this._camera.up.y,this._cameraUp0.y)||!ke(this._camera.up.z,this._cameraUp0.z)){this._camera.up.copy(this._cameraUp0);const t=this.getPosition(qe);this.updateCameraUp(),this.setPosition(t.x,t.y,t.z)}const e=[this.setLookAt(this._position0.x,this._position0.y,this._position0.z,this._target0.x,this._target0.y,this._target0.z,t),this.setFocalOffset(this._focalOffset0.x,this._focalOffset0.y,this._focalOffset0.z,t),this.zoomTo(this._zoom0,t)];return Promise.all(e)}saveState(){this._cameraUp0.copy(this._camera.up),this.getTarget(this._target0),this.getPosition(this._position0),this._zoom0=this._zoom,this._focalOffset0.copy(this._focalOffset)}updateCameraUp(){this._yAxisUpSpace.setFromUnitVectors(this._camera.up,We),this._yAxisUpSpaceInverse.copy(this._yAxisUpSpace).invert()}applyCameraUp(){const t=qe.subVectors(this._target,this._camera.position).normalize(),e=Xe.crossVectors(t,this._camera.up);this._camera.up.crossVectors(e,t).normalize(),this._camera.updateMatrixWorld();const i=this.getPosition(qe);this.updateCameraUp(),this.setPosition(i.x,i.y,i.z)}update(t){const e=this._sphericalEnd.theta-this._spherical.theta,i=this._sphericalEnd.phi-this._spherical.phi,s=this._sphericalEnd.radius-this._spherical.radius,n=ii.subVectors(this._targetEnd,this._target),r=si.subVectors(this._focalOffsetEnd,this._focalOffset),o=this._zoomEnd-this._zoom;if(De(e))this._thetaVelocity.value=0,this._spherical.theta=this._sphericalEnd.theta;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.theta=Ne(this._spherical.theta,this._sphericalEnd.theta,this._thetaVelocity,e,1/0,t),this._needsUpdate=!0}if(De(i))this._phiVelocity.value=0,this._spherical.phi=this._sphericalEnd.phi;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.phi=Ne(this._spherical.phi,this._sphericalEnd.phi,this._phiVelocity,e,1/0,t),this._needsUpdate=!0}if(De(s))this._radiusVelocity.value=0,this._spherical.radius=this._sphericalEnd.radius;else{const e=this._isUserControllingDolly?this.draggingSmoothTime:this.smoothTime;this._spherical.radius=Ne(this._spherical.radius,this._sphericalEnd.radius,this._radiusVelocity,e,this.maxSpeed,t),this._needsUpdate=!0}if(De(n.x)&&De(n.y)&&De(n.z))this._targetVelocity.set(0,0,0),this._target.copy(this._targetEnd);else{const e=this._isUserControllingTruck?this.draggingSmoothTime:this.smoothTime;Ue(this._target,this._targetEnd,this._targetVelocity,e,this.maxSpeed,t,this._target),this._needsUpdate=!0}if(De(r.x)&&De(r.y)&&De(r.z))this._focalOffsetVelocity.set(0,0,0),this._focalOffset.copy(this._focalOffsetEnd);else{const e=this._isUserControllingOffset?this.draggingSmoothTime:this.smoothTime;Ue(this._focalOffset,this._focalOffsetEnd,this._focalOffsetVelocity,e,this.maxSpeed,t,this._focalOffset),this._needsUpdate=!0}if(De(o))this._zoomVelocity.value=0,this._zoom=this._zoomEnd;else{const e=this._isUserControllingZoom?this.draggingSmoothTime:this.smoothTime;this._zoom=Ne(this._zoom,this._zoomEnd,this._zoomVelocity,e,1/0,t)}if(this.dollyToCursor)if(Ce(this._camera)&&0!==this._changedDolly){const t=this._spherical.radius-this._lastDistance,e=this._camera,i=this._getCameraDirection(Ke),s=qe.copy(i).cross(e.up).normalize();0===s.lengthSq()&&(s.x=1);const n=Xe.crossVectors(s,i),r=this._sphericalEnd.radius*Math.tan(e.getEffectiveFOV()*Pe*.5),o=(this._sphericalEnd.radius-t-this._sphericalEnd.radius)/this._sphericalEnd.radius,a=Qe.copy(this._targetEnd).add(s.multiplyScalar(this._dollyControlCoord.x*r*e.aspect)).add(n.multiplyScalar(this._dollyControlCoord.y*r)),l=qe.copy(this._targetEnd).lerp(a,o),h=this._lastDollyDirection===Ee&&this._spherical.radius<=this.minDistance,c=this._lastDollyDirection===Te&&this.maxDistance<=this._spherical.radius;if(this.infinityDolly&&(h||c)){this._sphericalEnd.radius-=t,this._spherical.radius-=t;const e=Xe.copy(i).multiplyScalar(-t);l.add(e)}this._boundary.clampPoint(l,l);const d=Xe.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedDolly-=t,De(this._changedDolly)&&(this._changedDolly=0)}else if(Se(this._camera)&&0!==this._changedZoom){const t=this._zoom-this._lastZoom,e=this._camera,i=qe.set(this._dollyControlCoord.x,this._dollyControlCoord.y,(e.near+e.far)/(e.near-e.far)).unproject(e),s=Xe.set(0,0,-1).applyQuaternion(e.quaternion),n=Qe.copy(i).add(s.multiplyScalar(-i.dot(e.up))),r=-(this._zoom-t-this._zoom)/this._zoom,o=this._getCameraDirection(Ke),a=this._targetEnd.dot(o),l=qe.copy(this._targetEnd).lerp(n,r),h=l.dot(o),c=o.multiplyScalar(h-a);l.sub(c),this._boundary.clampPoint(l,l);const d=Xe.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedZoom-=t,De(this._changedZoom)&&(this._changedZoom=0)}this._camera.zoom!==this._zoom&&(this._camera.zoom=this._zoom,this._camera.updateProjectionMatrix(),this._updateNearPlaneCorners(),this._needsUpdate=!0),this._dragNeedsUpdate=!0;const a=this._collisionTest();this._spherical.radius=Math.min(this._spherical.radius,a),this._spherical.makeSafe(),this._camera.position.setFromSpherical(this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(this._target),this._camera.lookAt(this._target);(!De(this._focalOffset.x)||!De(this._focalOffset.y)||!De(this._focalOffset.z))&&(Je.setFromMatrixColumn(this._camera.matrix,0),ti.setFromMatrixColumn(this._camera.matrix,1),ei.setFromMatrixColumn(this._camera.matrix,2),Je.multiplyScalar(this._focalOffset.x),ti.multiplyScalar(-this._focalOffset.y),ei.multiplyScalar(this._focalOffset.z),qe.copy(Je).add(ti).add(ei),this._camera.position.add(qe),this._camera.updateMatrixWorld()),this._boundaryEnclosesCamera&&this._encloseToBoundary(this._camera.position.copy(this._target),qe.setFromSpherical(this._spherical).applyQuaternion(this._yAxisUpSpaceInverse),1);const l=this._needsUpdate;return l&&!this._updatedLastTime?(this._hasRested=!1,this.dispatchEvent({type:"wake"}),this.dispatchEvent({type:"update"})):l?(this.dispatchEvent({type:"update"}),De(e,this.restThreshold)&&De(i,this.restThreshold)&&De(s,this.restThreshold)&&De(n.x,this.restThreshold)&&De(n.y,this.restThreshold)&&De(n.z,this.restThreshold)&&De(r.x,this.restThreshold)&&De(r.y,this.restThreshold)&&De(r.z,this.restThreshold)&&De(o,this.restThreshold)&&!this._hasRested&&(this._hasRested=!0,this.dispatchEvent({type:"rest"}))):!l&&this._updatedLastTime&&this.dispatchEvent({type:"sleep"}),this._lastDistance=this._spherical.radius,this._lastZoom=this._zoom,this._updatedLastTime=l,this._needsUpdate=!1,l}toJSON(){return JSON.stringify({enabled:this._enabled,minDistance:this.minDistance,maxDistance:ze(this.maxDistance),minZoom:this.minZoom,maxZoom:ze(this.maxZoom),minPolarAngle:this.minPolarAngle,maxPolarAngle:ze(this.maxPolarAngle),minAzimuthAngle:ze(this.minAzimuthAngle),maxAzimuthAngle:ze(this.maxAzimuthAngle),smoothTime:this.smoothTime,draggingSmoothTime:this.draggingSmoothTime,dollySpeed:this.dollySpeed,truckSpeed:this.truckSpeed,dollyToCursor:this.dollyToCursor,target:this._targetEnd.toArray(),position:qe.setFromSpherical(this._sphericalEnd).add(this._targetEnd).toArray(),zoom:this._zoomEnd,focalOffset:this._focalOffsetEnd.toArray(),target0:this._target0.toArray(),position0:this._position0.toArray(),zoom0:this._zoom0,focalOffset0:this._focalOffset0.toArray()})}fromJSON(t,e=!1){const i=JSON.parse(t);this.enabled=i.enabled,this.minDistance=i.minDistance,this.maxDistance=Be(i.maxDistance),this.minZoom=i.minZoom,this.maxZoom=Be(i.maxZoom),this.minPolarAngle=i.minPolarAngle,this.maxPolarAngle=Be(i.maxPolarAngle),this.minAzimuthAngle=Be(i.minAzimuthAngle),this.maxAzimuthAngle=Be(i.maxAzimuthAngle),this.smoothTime=i.smoothTime,this.draggingSmoothTime=i.draggingSmoothTime,this.dollySpeed=i.dollySpeed,this.truckSpeed=i.truckSpeed,this.dollyToCursor=i.dollyToCursor,this._target0.fromArray(i.target0),this._position0.fromArray(i.position0),this._zoom0=i.zoom0,this._focalOffset0.fromArray(i.focalOffset0),this.moveTo(i.target[0],i.target[1],i.target[2],e),ni.setFromVector3(qe.fromArray(i.position).sub(this._targetEnd).applyQuaternion(this._yAxisUpSpace)),this.rotateTo(ni.theta,ni.phi,e),this.dollyTo(ni.radius,e),this.zoomTo(i.zoom,e),this.setFocalOffset(i.focalOffset[0],i.focalOffset[1],i.focalOffset[2],e),this._needsUpdate=!0}connect(t){this._domElement?console.warn("camera-controls is already connected."):(t.setAttribute("data-camera-controls-version","2.10.1"),this._addAllEventListeners(t),this._getClientRect(this._elementRect))}disconnect(){this.cancel(),this._removeAllEventListeners(),this._domElement&&(this._domElement.removeAttribute("data-camera-controls-version"),this._domElement=void 0)}dispose(){this.removeAllEventListeners(),this.disconnect()}_getTargetDirection(t){return t.setFromSpherical(this._spherical).divideScalar(this._spherical.radius).applyQuaternion(this._yAxisUpSpaceInverse)}_getCameraDirection(t){return this._getTargetDirection(t).negate()}_findPointerById(t){return this._activePointers.find(e=>e.pointerId===t)}_findPointerByMouseButton(t){return this._activePointers.find(e=>e.mouseButton===t)}_disposePointer(t){this._activePointers.splice(this._activePointers.indexOf(t),1)}_encloseToBoundary(t,e,i){const s=e.lengthSq();if(0===s)return t;const n=Xe.copy(e).add(t),r=this._boundary.clampPoint(n,Qe).sub(n),o=r.lengthSq();if(0===o)return t.add(e);if(o===s)return t;if(0===i)return t.add(e).add(r);{const s=1+i*o/e.dot(r);return t.add(Xe.copy(e).multiplyScalar(s)).add(r.multiplyScalar(1-i))}}_updateNearPlaneCorners(){if(Ce(this._camera)){const t=this._camera,e=t.near,i=t.getEffectiveFOV()*Pe,s=Math.tan(.5*i)*e,n=s*t.aspect;this._nearPlaneCorners[0].set(-n,-s,0),this._nearPlaneCorners[1].set(n,-s,0),this._nearPlaneCorners[2].set(n,s,0),this._nearPlaneCorners[3].set(-n,s,0)}else if(Se(this._camera)){const t=this._camera,e=1/t.zoom,i=t.left*e,s=t.right*e,n=t.top*e,r=t.bottom*e;this._nearPlaneCorners[0].set(i,n,0),this._nearPlaneCorners[1].set(s,n,0),this._nearPlaneCorners[2].set(s,r,0),this._nearPlaneCorners[3].set(i,r,0)}}_collisionTest(){let t=1/0;if(!(this.colliderMeshes.length>=1))return t;if(Re(this._camera,"_collisionTest"))return t;const e=this._getTargetDirection(Ke);di.lookAt($e,e,this._camera.up);for(let i=0;i<4;i++){const s=Xe.copy(this._nearPlaneCorners[i]);s.applyMatrix4(di);const n=Qe.addVectors(this._target,s);ui.set(n,e),ui.far=this._spherical.radius+1;const r=ui.intersectObjects(this.colliderMeshes);0!==r.length&&r[0].distance<t&&(t=r[0].distance)}return t}_getClientRect(t){if(!this._domElement)return;const e=this._domElement.getBoundingClientRect();return t.x=e.left,t.y=e.top,this._viewport?(t.x+=this._viewport.x,t.y+=e.height-this._viewport.w-this._viewport.y,t.width=this._viewport.z,t.height=this._viewport.w):(t.width=e.width,t.height=e.height),t}_createOnRestPromise(t){return t?Promise.resolve():(this._hasRested=!1,this.dispatchEvent({type:"transitionstart"}),new Promise(t=>{const e=()=>{this.removeEventListener("rest",e),t()};this.addEventListener("rest",e)}))}_addAllEventListeners(t){}_removeAllEventListeners(){}get dampingFactor(){return console.warn(".dampingFactor has been deprecated. use smoothTime (in seconds) instead."),0}set dampingFactor(t){console.warn(".dampingFactor has been deprecated. use smoothTime (in seconds) instead.")}get draggingDampingFactor(){return console.warn(".draggingDampingFactor has been deprecated. use draggingSmoothTime (in seconds) instead."),0}set draggingDampingFactor(t){console.warn(".draggingDampingFactor has been deprecated. use draggingSmoothTime (in seconds) instead.")}static createBoundingSphere(t,e=new Ze.Sphere){const i=e,s=i.center;oi.makeEmpty(),t.traverseVisible(t=>{t.isMesh&&oi.expandByObject(t)}),oi.getCenter(s);let n=0;return t.traverseVisible(t=>{if(!t.isMesh)return;const e=t;if(!e.geometry)return;const i=e.geometry.clone();i.applyMatrix4(e.matrixWorld);const r=i.attributes.position;for(let t=0,e=r.count;t<e;t++)qe.fromBufferAttribute(r,t),n=Math.max(n,s.distanceToSquared(qe))}),i.radius=Math.sqrt(n),i}}class fi extends p{constructor(t){super(t),e(this,"onBeforeUpdate",new h),e(this,"onAfterUpdate",new h),e(this,"onAspectUpdated",new h),e(this,"onDisposed",new h),e(this,"three"),e(this,"_allControls",new Map),e(this,"updateAspect",()=>{var t;if(this.currentWorld&&this.currentWorld.renderer)if(this.three instanceof a.OrthographicCamera)this.onAspectUpdated.trigger();else if(null==(t=this.currentWorld.renderer)?void 0:t.isResizeable()){const t=this.currentWorld.renderer.getSize();this.three.aspect=t.width/t.height,this.three.updateProjectionMatrix(),this.onAspectUpdated.trigger()}}),this.three=this.setupCamera(),this.setupEvents(!0),this.worlds.onItemSet.add(({value:t})=>{const e=this.newCameraControls();this._allControls.set(t.uuid,e)}),this.worlds.onBeforeDelete.add(({value:t})=>{const e=this._allControls.get(t.uuid);e&&(e.dispose(),this._allControls.delete(t.uuid))})}get controls(){if(!this.currentWorld)throw new Error("This camera needs a world to work!");const t=this._allControls.get(this.currentWorld.uuid);if(!t)throw new Error("Controls not found!");return t}get enabled(){return null!==this.currentWorld&&this.controls.enabled}set enabled(t){null!==this.currentWorld&&(this.controls.enabled=t)}set currentWorld(t){if(super.currentWorld=t,!t)return;this.worlds.get(t.uuid)||this.worlds.set(t.uuid,t)}get currentWorld(){return this._currentWorld}dispose(){this.setupEvents(!1),this.onAspectUpdated.reset(),this.onBeforeUpdate.reset(),this.onAfterUpdate.reset(),this.three.removeFromParent(),this.onDisposed.trigger(),this.onDisposed.reset();for(const[t,e]of this._allControls)e.dispose();this.worlds.clear()}async fitToItems(t){const e=await this.getItemsBounding(t);await this.controls.fitToSphere(e,!0)}async setOrbitToItems(t){const e=await this.getItemsBounding(t);this.controls.setOrbitPoint(e.center.x,e.center.y,e.center.z)}update(t){this.enabled&&(this.onBeforeUpdate.trigger(this),this.controls.update(t),this.onAfterUpdate.trigger(this))}async getItemsBounding(t){const e=this.components.get(Xt),i=this.components.get(se);i.list.clear();const s=new a.Sphere;if(t)await i.addFromModelIdMap(t);else for(const[,t]of e.list)i.list.add(t.box);return i.get().getBoundingSphere(s),i.list.clear(),s}setupCamera(){const t=window.innerWidth/window.innerHeight,e=new a.PerspectiveCamera(60,t,1,1e3);return e.position.set(50,50,50),e.lookAt(new a.Vector3(0,0,0)),e}newCameraControls(){if(!this.currentWorld)throw new Error("This camera needs a world to work!");if(!this.currentWorld.renderer)throw new Error("This camera needs a renderer to work!");pi.install({THREE:fi.getSubsetOfThree()});const{domElement:t}=this.currentWorld.renderer.three,e=new pi(this.three,t);return e.smoothTime=.2,e.dollyToCursor=!0,e.infinityDolly=!0,e.minDistance=6,e}setupEvents(t){t?window.addEventListener("resize",this.updateAspect):window.removeEventListener("resize",this.updateAspect)}static getSubsetOfThree(){return{MOUSE:a.MOUSE,Vector2:a.Vector2,Vector3:a.Vector3,Vector4:a.Vector4,Quaternion:a.Quaternion,Matrix4:a.Matrix4,Spherical:a.Spherical,Box3:a.Box3,Sphere:a.Sphere,Raycaster:a.Raycaster,MathUtils:a.MathUtils}}}const mi=class t extends d{constructor(i){super(i),e(this,"onAfterUpdate",new h),e(this,"onBeforeUpdate",new h),e(this,"onDisposed",new h),e(this,"list",new Ht),e(this,"enabled",!0),i.add(t.uuid,this)}create(){const t=new ue(this.components),e=t.uuid;if(this.list.has(e))throw new Error("There is already a world with this name!");return this.list.set(e,t),t}delete(t){if(!this.list.has(t.uuid))throw new Error("The provided world is not found in the list!");this.list.delete(t.uuid),t.dispose()}dispose(){this.enabled=!1;for(const[t,e]of this.list)e.dispose();this.list.clear(),this.onDisposed.trigger()}update(t){if(this.enabled)for(const[e,i]of this.list)i.update(t)}};e(mi,"uuid","fdb61dc4-2ec1-4966-b83d-54ea795fad4a");let gi=mi;class _i extends $t{constructor(){super(...arguments),e(this,"_config",{visible:{value:!0,type:"Boolean"},color:{value:new a.Color,type:"Color"},primarySize:{type:"Number",interpolable:!0,value:1,min:0,max:1e3},secondarySize:{type:"Number",interpolable:!0,value:10,min:0,max:1e3},distance:{type:"Number",interpolable:!0,value:500,min:0,max:500}})}get visible(){return this._config.visible.value}set visible(t){this._config.visible.value=t,this._component.visible=t}get color(){return this._config.color.value}set color(t){this._config.color.value=t,this._component.material.uniforms.uColor.value=t,this._component.material.uniformsNeedUpdate=!0}get primarySize(){return this._config.primarySize.value}set primarySize(t){this._config.primarySize.value=t,this._component.material.uniforms.uSize1.value=t,this._component.material.uniformsNeedUpdate=!0}get secondarySize(){return this._config.secondarySize.value}set secondarySize(t){this._config.secondarySize.value=t,this._component.material.uniforms.uSize2.value=t,this._component.material.uniformsNeedUpdate=!0}get distance(){return this._config.distance.value}set distance(t){this._config.distance.value=t,this._component.material.uniforms.uDistance.value=t,this._component.material.uniformsNeedUpdate=!0}}class yi{constructor(t,i){e(this,"onDisposed",new h),e(this,"onSetup",new h),e(this,"isSetup",!1),e(this,"world"),e(this,"components"),e(this,"config"),e(this,"_defaultConfig",{visible:!0,color:new a.Color(12303291),primarySize:1,secondarySize:10,distance:500}),e(this,"three"),e(this,"_fade",3),e(this,"updateZoom",()=>{this.world.camera instanceof fi&&(this.material.uniforms.uZoom.value=this.world.camera.three.zoom)}),this.world=i;const{color:s,primarySize:n,secondarySize:r,distance:o}=this._defaultConfig;this.components=t,this.config=new _i(this,this.components,"Grid");const l=new a.PlaneGeometry(2,2,1,1),c=new a.ShaderMaterial({side:a.DoubleSide,uniforms:{uSize1:{value:n},uSize2:{value:r},uColor:{value:s},uDistance:{value:o},uFade:{value:this._fade},uZoom:{value:1}},transparent:!0,vertexShader:"\n \n varying vec3 worldPosition;\n \n uniform float uDistance;\n \n void main() {\n \n vec3 pos = position.xzy * uDistance;\n pos.xz += cameraPosition.xz;\n \n worldPosition = pos;\n \n gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);\n \n }\n ",fragmentShader:"\n \n varying vec3 worldPosition;\n \n uniform float uZoom;\n uniform float uFade;\n uniform float uSize1;\n uniform float uSize2;\n uniform vec3 uColor;\n uniform float uDistance;\n \n \n \n float getGrid(float size) {\n \n vec2 r = worldPosition.xz / size;\n \n \n vec2 grid = abs(fract(r - 0.5) - 0.5) / fwidth(r);\n float line = min(grid.x, grid.y);\n \n \n return 1.0 - min(line, 1.0);\n }\n \n void main() {\n \n \n float d = 1.0 - min(distance(cameraPosition.xz, worldPosition.xz) / uDistance, 1.0);\n \n float g1 = getGrid(uSize1);\n float g2 = getGrid(uSize2);\n \n // Ortho camera fades the grid away when zooming out\n float minZoom = step(0.2, uZoom);\n float zoomFactor = pow(min(uZoom, 1.), 2.) * minZoom;\n \n gl_FragColor = vec4(uColor.rgb, mix(g2, g1, g1) * pow(d, uFade));\n gl_FragColor.a = mix(0.5 * gl_FragColor.a, gl_FragColor.a, g2) * zoomFactor;\n \n if ( gl_FragColor.a <= 0.0 ) discard;\n \n \n }\n \n ",extensions:{derivatives:!0}});this.three=new a.Mesh(l,c),this.three.frustumCulled=!1,i.scene.three.add(this.three),this.setupEvents(!0)}get visible(){return this.three.visible}set visible(t){if(this.three.visible=t,t){this.world.scene.three.add(this.three)}else this.three.removeFromParent()}get material(){return this.three.material}get fade(){return 3===this._fade}set fade(t){this._fade=t?3:0,this.material.uniforms.uFade.value=this._fade}setup(t){const e={...this._defaultConfig,...t};this.config.visible=!0,this.config.color=e.color,this.config.primarySize=e.primarySize,this.config.secondarySize=e.secondarySize,this.config.distance=e.distance,this.isSetup=!0,this.onSetup.trigger()}dispose(){this.setupEvents(!1);this.components.get(Yt).list.delete(this.config.uuid);this.components.get(g).destroy(this.three),this.onDisposed.trigger(),this.onDisposed.reset(),this.world=null,this.components=null}setupEvents(t){if(this.world.isDisposing)return;if(!(this.world.camera instanceof fi))return;const e=this.world.camera.controls;t?e.addEventListener("update",this.updateZoom):e.removeEventListener("update",this.updateZoom)}}const wi=class t extends d{constructor(i){super(i),e(this,"list",new Map),e(this,"onDisposed",new h),e(this,"enabled",!0),i.add(t.uuid,this)}create(t){if(this.list.has(t.uuid))throw new Error("This world already has a grid!");const e=new yi(this.components,t);return this.list.set(t.uuid,e),t.onDisposed.add(()=>{this.delete(t)}),e}delete(t){const e=this.list.get(t.uuid);e&&e.dispose(),this.list.delete(t.uuid)}dispose(){for(const[t,e]of this.list)e.dispose();this.list.clear(),this.onDisposed.trigger(),this.onDisposed.reset()}};e(wi,"uuid","d1e814d5-b81c-4452-87a2-f039375e0489");let vi=wi;const bi=1.25,xi=65535,Ei=Math.pow(2,-24),Ti=Symbol("SKIP_GENERATION");function Ci(t){return function(t){return t.index?t.index.count:t.attributes.position.count}(t)/3}function Si(t,e){if(!t.index){const i=t.attributes.position.count,n=function(t,e=ArrayBuffer){return t>65535?new Uint32Array(new e(4*t)):new Uint16Array(new e(2*t))}(i,e.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer);t.setIndex(new s.BufferAttribute(n,1));for(let t=0;t<i;t++)n[t]=t}}function Ai(t){const e=Ci(t),i=t.drawRange,s=i.start/3,n=(i.start+i.count)/3,r=Math.max(0,s),o=Math.min(e,n)-r;return[{offset:Math.floor(r),count:Math.floor(o)}]}function Oi(t){if(!t.groups||!t.groups.length)return Ai(t);const e=[],i=new Set,s=t.drawRange,n=s.start/3,r=(s.start+s.count)/3;for(const e of t.groups){const t=e.start/3,s=(e.start+e.count)/3;i.add(Math.max(n,t)),i.add(Math.min(r,s))}const o=Array.from(i.values()).sort((t,e)=>t-e);for(let t=0;t<o.length-1;t++){const i=o[t],s=o[t+1];e.push({offset:Math.floor(i),count:Math.floor(s-i)})}return e}function Pi(t,e,i){return i.min.x=e[t],i.min.y=e[t+1],i.min.z=e[t+2],i.max.x=e[t+3],i.max.y=e[t+4],i.max.z=e[t+5],i}function Ii(t){let e=-1,i=-1/0;for(let s=0;s<3;s++){const n=t[s+3]-t[s];n>i&&(i=n,e=s)}return e}function Di(t,e){e.set(t)}function ki(t,e,i){let s,n;for(let r=0;r<3;r++){const o=r+3;s=t[r],n=e[r],i[r]=s<n?s:n,s=t[o],n=e[o],i[o]=s>n?s:n}}function Mi(t,e,i){for(let s=0;s<3;s++){const n=e[t+2*s],r=e[t+2*s+1],o=n-r,a=n+r;o<i[s]&&(i[s]=o),a>i[s+3]&&(i[s+3]=a)}}function zi(t){const e=t[3]-t[0],i=t[4]-t[1],s=t[5]-t[2];return 2*(e*i+i*s+s*e)}function Bi(t,e,i,s,n=null){let r=1/0,o=1/0,a=1/0,l=-1/0,h=-1/0,c=-1/0,d=1/0,u=1/0,p=1/0,f=-1/0,m=-1/0,g=-1/0;const _=null!==n;for(let s=6*e,n=6*(e+i);s<n;s+=6){const e=t[s+0],i=t[s+1],n=e-i,y=e+i;n<r&&(r=n),y>l&&(l=y),_&&e<d&&(d=e),_&&e>f&&(f=e);const w=t[s+2],v=t[s+3],b=w-v,x=w+v;b<o&&(o=b),x>h&&(h=x),_&&w<u&&(u=w),_&&w>m&&(m=w);const E=t[s+4],T=t[s+5],C=E-T,S=E+T;C<a&&(a=C),S>c&&(c=S),_&&E<p&&(p=E),_&&E>g&&(g=E)}s[0]=r,s[1]=o,s[2]=a,s[3]=l,s[4]=h,s[5]=c,_&&(n[0]=d,n[1]=u,n[2]=p,n[3]=f,n[4]=m,n[5]=g)}const Ni=32,Ui=(t,e)=>t.candidate-e.candidate,Li=new Array(Ni).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),Ri=new Float32Array(6);class Vi{constructor(){}}function Fi(t,e,i,s,n,r){let o=s,a=s+n-1;const l=r.pos,h=2*r.axis;for(;;){for(;o<=a&&i[6*o+h]<l;)o++;for(;o<=a&&i[6*a+h]>=l;)a--;if(!(o<a))return o;for(let t=0;t<3;t++){let i=e[3*o+t];e[3*o+t]=e[3*a+t],e[3*a+t]=i}for(let t=0;t<6;t++){let e=i[6*o+t];i[6*o+t]=i[6*a+t],i[6*a+t]=e}o++,a--}}function ji(t,e,i,s,n,r){let o=s,a=s+n-1;const l=r.pos,h=2*r.axis;for(;;){for(;o<=a&&i[6*o+h]<l;)o++;for(;o<=a&&i[6*a+h]>=l;)a--;if(!(o<a))return o;{let e=t[o];t[o]=t[a],t[a]=e;for(let t=0;t<6;t++){let e=i[6*o+t];i[6*o+t]=i[6*a+t],i[6*a+t]=e}o++,a--}}}function Hi(t,e){const i=t.geometry,s=i.index?i.index.array:null,n=e.maxDepth,r=e.verbose,o=e.maxLeafTris,a=e.strategy,l=e.onProgress,h=Ci(i),c=t._indirectBuffer;let d=!1;const u=new Float32Array(6),p=new Float32Array(6),f=function(t,e){var i;(i=e)[0]=i[1]=i[2]=1/0,i[3]=i[4]=i[5]=-1/0;const s=t.attributes.position,n=t.index?t.index.array:null,r=Ci(t),o=new Float32Array(6*r),a=s.normalized,l=s.array,h=s.offset||0;let c=3;s.isInterleavedBufferAttribute&&(c=s.data.stride);const d=["getX","getY","getZ"];for(let t=0;t<r;t++){const i=3*t,r=6*t;let u=i+0,p=i+1,f=i+2;n&&(u=n[u],p=n[p],f=n[f]),a||(u=u*c+h,p=p*c+h,f=f*c+h);for(let t=0;t<3;t++){let i,n,h;a?(i=s[d[t]](u),n=s[d[t]](p),h=s[d[t]](f)):(i=l[u+t],n=l[p+t],h=l[f+t]);let c=i;n<c&&(c=n),h<c&&(c=h);let m=i;n>m&&(m=n),h>m&&(m=h);const g=(m-c)/2,_=2*t;o[r+_+0]=c+g,o[r+_+1]=g+(Math.abs(c)+g)*Ei,c<e[t]&&(e[t]=c),m>e[t+3]&&(e[t+3]=m)}}return o}(i,u),m=e.indirect?ji:Fi,g=[],_=e.indirect?Ai(i):Oi(i);if(1===_.length){const t=_[0],e=new Vi;e.boundingData=u,function(t,e,i,s){let n=1/0,r=1/0,o=1/0,a=-1/0,l=-1/0,h=-1/0;for(let s=6*e,c=6*(e+i);s<c;s+=6){const e=t[s+0];e<n&&(n=e),e>a&&(a=e);const i=t[s+2];i<r&&(r=i),i>l&&(l=i);const c=t[s+4];c<o&&(o=c),c>h&&(h=c)}s[0]=n,s[1]=r,s[2]=o,s[3]=a,s[4]=l,s[5]=h}(f,t.offset,t.count,p),w(e,t.offset,t.count,p),g.push(e)}else for(let t of _){const e=new Vi;e.boundingData=new Float32Array(6),Bi(f,t.offset,t.count,e.boundingData,p),w(e,t.offset,t.count,p),g.push(e)}return g;function y(t){l&&l(t/h)}function w(t,e,l,h=null,u=0){if(!d&&u>=n&&(d=!0,r&&(console.warn(`MeshBVH: Max depth of ${n} reached when generating BVH. Consider increasing maxDepth.`),console.warn(i))),l<=o||u>=n)return y(e+l),t.offset=e,t.count=l,t;const g=function(t,e,i,s,n,r){let o=-1,a=0;if(0===r)o=Ii(e),-1!==o&&(a=(e[o]+e[o+3])/2);else if(1===r)o=Ii(t),-1!==o&&(a=function(t,e,i,s){let n=0;for(let r=e,o=e+i;r<o;r++)n+=t[6*r+2*s];return n/i}(i,s,n,o));else if(2===r){const r=zi(t);let l=bi*n;const h=6*s,c=6*(s+n);for(let t=0;t<3;t++){const s=e[t],d=(e[t+3]-s)/Ni;if(n<8){const e=[...Li];e.length=n;let s=0;for(let n=h;n<c;n+=6,s++){const r=e[s];r.candidate=i[n+2*t],r.count=0;const{bounds:o,leftCacheBounds:a,rightCacheBounds:l}=r;for(let t=0;t<3;t++)l[t]=1/0,l[t+3]=-1/0,a[t]=1/0,a[t+3]=-1/0,o[t]=1/0,o[t+3]=-1/0;Mi(n,i,o)}e.sort(Ui);let d=n;for(let t=0;t<d;t++){const i=e[t];for(;t+1<d&&e[t+1].candidate===i.candidate;)e.splice(t+1,1),d--}for(let s=h;s<c;s+=6){const n=i[s+2*t];for(let t=0;t<d;t++){const r=e[t];n>=r.candidate?Mi(s,i,r.rightCacheBounds):(Mi(s,i,r.leftCacheBounds),r.count++)}}for(let i=0;i<d;i++){const s=e[i],h=s.count,c=n-s.count,d=s.leftCacheBounds,u=s.rightCacheBounds;let p=0;0!==h&&(p=zi(d)/r);let f=0;0!==c&&(f=zi(u)/r);const m=1+bi*(p*h+f*c);m<l&&(o=t,l=m,a=s.candidate)}}else{for(let t=0;t<Ni;t++){const e=Li[t];e.count=0,e.candidate=s+d+t*d;const i=e.bounds;for(let t=0;t<3;t++)i[t]=1/0,i[t+3]=-1/0}for(let e=h;e<c;e+=6){let n=~~((i[e+2*t]-s)/d);n>=Ni&&(n=31);const r=Li[n];r.count++,Mi(e,i,r.bounds)}const e=Li[31];Di(e.bounds,e.rightCacheBounds);for(let t=30;t>=0;t--){const e=Li[t],i=Li[t+1];ki(e.bounds,i.rightCacheBounds,e.rightCacheBounds)}let u=0;for(let e=0;e<31;e++){const i=Li[e],s=i.count,h=i.bounds,c=Li[e+1].rightCacheBounds;0!==s&&(0===u?Di(h,Ri):ki(h,Ri,Ri)),u+=s;let d=0,p=0;0!==u&&(d=zi(Ri)/r);const f=n-u;0!==f&&(p=zi(c)/r);const m=1+bi*(d*u+p*f);m<l&&(o=t,l=m,a=i.candidate)}}}}else console.warn(`MeshBVH: Invalid build strategy value ${r} used.`);return{axis:o,pos:a}}(t.boundingData,h,f,e,l,a);if(-1===g.axis)return y(e+l),t.offset=e,t.count=l,t;const _=m(c,s,f,e,l,g);if(_===e||_===e+l)y(e+l),t.offset=e,t.count=l;else{t.splitAxis=g.axis;const i=new Vi,s=e,n=_-e;t.left=i,i.boundingData=new Float32Array(6),Bi(f,s,n,i.boundingData,p),w(i,s,n,p,u+1);const r=new Vi,o=_,a=l-n;t.right=r,r.boundingData=new Float32Array(6),Bi(f,o,a,r.boundingData,p),w(r,o,a,p,u+1)}return t}}function Zi(t,e){const i=t.geometry;e.indirect&&(t._indirectBuffer=function(t,e){const i=(t.index?t.index.count:t.attributes.position.count)/3,s=i>65536,n=s?4:2,r=e?new SharedArrayBuffer(i*n):new ArrayBuffer(i*n),o=s?new Uint32Array(r):new Uint16Array(r);for(let t=0,e=o.length;t<e;t++)o[t]=t;return o}(i,e.useSharedArrayBuffer),function(t){if(0===t.groups.length)return!1;const e=Ci(t),i=Oi(t).sort((t,e)=>t.offset-e.offset),s=i[i.length-1];s.count=Math.min(e-s.offset,s.count);let n=0;return i.forEach(({count:t})=>n+=t),e!==n}(i)&&!e.verbose&&console.warn('MeshBVH: Provided geometry contains groups that do not fully span the vertex contents while using the "indirect" option. BVH may incorrectly report intersections on unrendered portions of the geometry.')),t._indirectBuffer||Si(i,e);const s=Hi(t,e);let n,r,o;const a=[],l=e.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer;for(let t=0;t<s.length;t++){const e=s[t];const i=new l(32*h(e));n=new Float32Array(i),r=new Uint32Array(i),o=new Uint16Array(i),c(0,e),a.push(i)}return void(t._roots=a);function h(t){return t.count?1:1+h(t.left)+h(t.right)}function c(t,e){const i=t/4,s=t/2,a=!!e.count,l=e.boundingData;for(let t=0;t<6;t++)n[i+t]=l[t];if(a){const n=e.offset,a=e.count;return r[i+6]=n,o[s+14]=a,o[s+15]=xi,t+32}{const s=e.left,n=e.right,o=e.splitAxis;let a;if(a=c(t+32,s),a/4>Math.pow(2,32))throw new Error("MeshBVH: Cannot store child pointer greater than 32 bits.");return r[i+6]=a/4,a=c(a,n),r[i+7]=o,a}}}class $i{constructor(){this.min=1/0,this.max=-1/0}setFromPointsField(t,e){let i=1/0,s=-1/0;for(let n=0,r=t.length;n<r;n++){const r=t[n][e];i=r<i?r:i,s=r>s?r:s}this.min=i,this.max=s}setFromPoints(t,e){let i=1/0,s=-1/0;for(let n=0,r=e.length;n<r;n++){const r=e[n],o=t.dot(r);i=o<i?o:i,s=o>s?o:s}this.min=i,this.max=s}isSeparated(t){return this.min>t.max||t.min>this.max}}$i.prototype.setFromBox=function(){const t=new s.Vector3;return function(e,i){const s=i.min,n=i.max;let r=1/0,o=-1/0;for(let i=0;i<=1;i++)for(let a=0;a<=1;a++)for(let l=0;l<=1;l++){t.x=s.x*i+n.x*(1-i),t.y=s.y*a+n.y*(1-a),t.z=s.z*l+n.z*(1-l);const h=e.dot(t);r=Math.min(h,r),o=Math.max(h,o)}this.min=r,this.max=o}}();const Wi=function(){const t=new s.Vector3,e=new s.Vector3,i=new s.Vector3;return function(s,n,r){const o=s.start,a=t,l=n.start,h=e;i.subVectors(o,l),t.subVectors(s.end,s.start),e.subVectors(n.end,n.start);const c=i.dot(h),d=h.dot(a),u=h.dot(h),p=i.dot(a),f=a.dot(a)*u-d*d;let m,g;m=0!==f?(c*d-p*u)/f:0,g=(c+m*d)/u,r.x=m,r.y=g}}(),Yi=function(){const t=new s.Vector2,e=new s.Vector3,i=new s.Vector3;return function(s,n,r,o){Wi(s,n,t);let a=t.x,l=t.y;if(a>=0&&a<=1&&l>=0&&l<=1)return s.at(a,r),void n.at(l,o);if(a>=0&&a<=1)return l<0?n.at(0,o):n.at(1,o),void s.closestPointToPoint(o,!0,r);if(l>=0&&l<=1)return a<0?s.at(0,r):s.at(1,r),void n.closestPointToPoint(r,!0,o);{let t,h;t=a<0?s.start:s.end,h=l<0?n.start:n.end;const c=e,d=i;return s.closestPointToPoint(h,!0,e),n.closestPointToPoint(t,!0,i),c.distanceToSquared(h)<=d.distanceToSquared(t)?(r.copy(c),void o.copy(h)):(r.copy(t),void o.copy(d))}}}(),Gi=function(){const t=new s.Vector3,e=new s.Vector3,i=new s.Plane,n=new s.Line3;return function(s,r){const{radius:o,center:a}=s,{a:l,b:h,c:c}=r;n.start=l,n.end=h;if(n.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;n.start=l,n.end=c;if(n.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;n.start=h,n.end=c;if(n.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;const d=r.getPlane(i);if(Math.abs(d.distanceToPoint(a))<=o){const t=d.projectPoint(a,e);if(r.containsPoint(t))return!0}return!1}}();function qi(t){return Math.abs(t)<1e-15}class Xi extends s.Triangle{constructor(...t){super(...t),this.isExtendedTriangle=!0,this.satAxes=new Array(4).fill().map(()=>new s.Vector3),this.satBounds=new Array(4).fill().map(()=>new $i),this.points=[this.a,this.b,this.c],this.sphere=new s.Sphere,this.plane=new s.Plane,this.needsUpdate=!0}intersectsSphere(t){return Gi(t,this)}update(){const t=this.a,e=this.b,i=this.c,s=this.points,n=this.satAxes,r=this.satBounds,o=n[0],a=r[0];this.getNormal(o),a.setFromPoints(o,s);const l=n[1],h=r[1];l.subVectors(t,e),h.setFromPoints(l,s);const c=n[2],d=r[2];c.subVectors(e,i),d.setFromPoints(c,s);const u=n[3],p=r[3];u.subVectors(i,t),p.setFromPoints(u,s),this.sphere.setFromPoints(this.points),this.plane.setFromNormalAndCoplanarPoint(o,t),this.needsUpdate=!1}}Xi.prototype.closestPointToSegment=function(){const t=new s.Vector3,e=new s.Vector3,i=new s.Line3;return function(s,n=null,r=null){const{start:o,end:a}=s,l=this.points;let h,c=1/0;for(let o=0;o<3;o++){const a=(o+1)%3;i.start.copy(l[o]),i.end.copy(l[a]),Yi(i,s,t,e),h=t.distanceToSquared(e),h<c&&(c=h,n&&n.copy(t),r&&r.copy(e))}return this.closestPointToPoint(o,t),h=o.distanceToSquared(t),h<c&&(c=h,n&&n.copy(t),r&&r.copy(o)),this.closestPointToPoint(a,t),h=a.distanceToSquared(t),h<c&&(c=h,n&&n.copy(t),r&&r.copy(a)),Math.sqrt(c)}}(),Xi.prototype.intersectsTriangle=function(){const t=new Xi,e=new Array(3),i=new Array(3),n=new $i,r=new $i,o=new s.Vector3,a=new s.Vector3,l=new s.Vector3,h=new s.Vector3,c=new s.Vector3,d=new s.Line3,u=new s.Line3,p=new s.Line3,f=new s.Vector3;function m(t,e,i){const s=t.points;let n=0,r=-1;for(let t=0;t<3;t++){const{start:o,end:l}=d;o.copy(s[t]),l.copy(s[(t+1)%3]),d.delta(a);const h=qi(e.distanceToPoint(o));if(qi(e.normal.dot(a))&&h){i.copy(d),n=2;break}const c=e.intersectLine(d,f);if(!c&&h&&f.copy(o),(c||h)&&!qi(f.distanceTo(l))){if(n<=1){(1===n?i.start:i.end).copy(f),h&&(r=n)}else if(n>=2){(1===r?i.start:i.end).copy(f),n=2;break}if(n++,2===n&&-1===r)break}}return n}return function(s,a=null,d=!1){this.needsUpdate&&this.update(),s.isExtendedTriangle?s.needsUpdate&&s.update():(t.copy(s),t.update(),s=t);const f=this.plane,g=s.plane;if(Math.abs(f.normal.dot(g.normal))>1-1e-10){const t=this.satBounds,l=this.satAxes;i[0]=s.a,i[1]=s.b,i[2]=s.c;for(let e=0;e<4;e++){const s=t[e],r=l[e];if(n.setFromPoints(r,i),s.isSeparated(n))return!1}const h=s.satBounds,c=s.satAxes;e[0]=this.a,e[1]=this.b,e[2]=this.c;for(let t=0;t<4;t++){const i=h[t],s=c[t];if(n.setFromPoints(s,e),i.isSeparated(n))return!1}for(let t=0;t<4;t++){const s=l[t];for(let t=0;t<4;t++){const a=c[t];if(o.crossVectors(s,a),n.setFromPoints(o,e),r.setFromPoints(o,i),n.isSeparated(r))return!1}}return a&&(d||console.warn("ExtendedTriangle.intersectsTriangle: Triangles are coplanar which does not support an output edge. Setting edge to 0, 0, 0."),a.start.set(0,0,0),a.end.set(0,0,0)),!0}{const t=m(this,g,u);if(1===t&&s.containsPoint(u.end))return a&&(a.start.copy(u.end),a.end.copy(u.end)),!0;if(2!==t)return!1;const e=m(s,f,p);if(1===e&&this.containsPoint(p.end))return a&&(a.start.copy(p.end),a.end.copy(p.end)),!0;if(2!==e)return!1;if(u.delta(l),p.delta(h),l.dot(h)<0){let t=p.start;p.start=p.end,p.end=t}const i=u.start.dot(l),n=u.end.dot(l),r=p.start.dot(l),o=p.end.dot(l);return(i===o||r===n||n<r!==i<o)&&(a&&(c.subVectors(u.start,p.start),c.dot(l)>0?a.start.copy(u.start):a.start.copy(p.start),c.subVectors(u.end,p.end),c.dot(l)<0?a.end.copy(u.end):a.end.copy(p.end)),!0)}}}(),Xi.prototype.distanceToPoint=function(){const t=new s.Vector3;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),Xi.prototype.distanceToTriangle=function(){const t=new s.Vector3,e=new s.Vector3,i=["a","b","c"],n=new s.Line3,r=new s.Line3;return function(s,o=null,a=null){const l=o||a?n:null;if(this.intersectsTriangle(s,l))return(o||a)&&(o&&l.getCenter(o),a&&l.getCenter(a)),0;let h=1/0;for(let e=0;e<3;e++){let n;const r=i[e],l=s[r];this.closestPointToPoint(l,t),n=l.distanceToSquared(t),n<h&&(h=n,o&&o.copy(t),a&&a.copy(l));const c=this[r];s.closestPointToPoint(c,t),n=c.distanceToSquared(t),n<h&&(h=n,o&&o.copy(c),a&&a.copy(t))}for(let l=0;l<3;l++){const c=i[l],d=i[(l+1)%3];n.set(this[c],this[d]);for(let l=0;l<3;l++){const c=i[l],d=i[(l+1)%3];r.set(s[c],s[d]),Yi(n,r,t,e);const u=t.distanceToSquared(e);u<h&&(h=u,o&&o.copy(t),a&&a.copy(e))}}return Math.sqrt(h)}}();class Qi{constructor(t,e,i){this.isOrientedBox=!0,this.min=new s.Vector3,this.max=new s.Vector3,this.matrix=new s.Matrix4,this.invMatrix=new s.Matrix4,this.points=new Array(8).fill().map(()=>new s.Vector3),this.satAxes=new Array(3).fill().map(()=>new s.Vector3),this.satBounds=new Array(3).fill().map(()=>new $i),this.alignedSatBounds=new Array(3).fill().map(()=>new $i),this.needsUpdate=!1,t&&this.min.copy(t),e&&this.max.copy(e),i&&this.matrix.copy(i)}set(t,e,i){this.min.copy(t),this.max.copy(e),this.matrix.copy(i),this.needsUpdate=!0}copy(t){this.min.copy(t.min),this.max.copy(t.max),this.matrix.copy(t.matrix),this.needsUpdate=!0}}Qi.prototype.update=function(){return function(){const t=this.matrix,e=this.min,i=this.max,s=this.points;for(let n=0;n<=1;n++)for(let r=0;r<=1;r++)for(let o=0;o<=1;o++){const a=s[1*n|2*r|4*o];a.x=n?i.x:e.x,a.y=r?i.y:e.y,a.z=o?i.z:e.z,a.applyMatrix4(t)}const n=this.satBounds,r=this.satAxes,o=s[0];for(let t=0;t<3;t++){const e=r[t],i=n[t],a=s[1<<t];e.subVectors(o,a),i.setFromPoints(e,s)}const a=this.alignedSatBounds;a[0].setFromPointsField(s,"x"),a[1].setFromPointsField(s,"y"),a[2].setFromPointsField(s,"z"),this.invMatrix.copy(this.matrix).invert(),this.needsUpdate=!1}}(),Qi.prototype.intersectsBox=function(){const t=new $i;return function(e){this.needsUpdate&&this.update();const i=e.min,s=e.max,n=this.satBounds,r=this.satAxes,o=this.alignedSatBounds;if(t.min=i.x,t.max=s.x,o[0].isSeparated(t))return!1;if(t.min=i.y,t.max=s.y,o[1].isSeparated(t))return!1;if(t.min=i.z,t.max=s.z,o[2].isSeparated(t))return!1;for(let i=0;i<3;i++){const s=r[i],o=n[i];if(t.setFromBox(s,e),o.isSeparated(t))return!1}return!0}}(),Qi.prototype.intersectsTriangle=function(){const t=new Xi,e=new Array(3),i=new $i,n=new $i,r=new s.Vector3;return function(s){this.needsUpdate&&this.update(),s.isExtendedTriangle?s.needsUpdate&&s.update():(t.copy(s),t.update(),s=t);const o=this.satBounds,a=this.satAxes;e[0]=s.a,e[1]=s.b,e[2]=s.c;for(let t=0;t<3;t++){const s=o[t],n=a[t];if(i.setFromPoints(n,e),s.isSeparated(i))return!1}const l=s.satBounds,h=s.satAxes,c=this.points;for(let t=0;t<3;t++){const e=l[t],s=h[t];if(i.setFromPoints(s,c),e.isSeparated(i))return!1}for(let t=0;t<3;t++){const s=a[t];for(let t=0;t<4;t++){const o=h[t];if(r.crossVectors(s,o),i.setFromPoints(r,e),n.setFromPoints(r,c),i.isSeparated(n))return!1}}return!0}}(),Qi.prototype.closestPointToPoint=function(){return function(t,e){return this.needsUpdate&&this.update(),e.copy(t).applyMatrix4(this.invMatrix).clamp(this.min,this.max).applyMatrix4(this.matrix),e}}(),Qi.prototype.distanceToPoint=function(){const t=new s.Vector3;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),Qi.prototype.distanceToBox=function(){const t=["x","y","z"],e=new Array(12).fill().map(()=>new s.Line3),i=new Array(12).fill().map(()=>new s.Line3),n=new s.Vector3,r=new s.Vector3;return function(s,o=0,a=null,l=null){if(this.needsUpdate&&this.update(),this.intersectsBox(s))return(a||l)&&(s.getCenter(r),this.closestPointToPoint(r,n),s.closestPointToPoint(n,r),a&&a.copy(n),l&&l.copy(r)),0;const h=o*o,c=s.min,d=s.max,u=this.points;let p=1/0;for(let t=0;t<8;t++){const e=u[t];r.copy(e).clamp(c,d);const i=e.distanceToSquared(r);if(i<p&&(p=i,a&&a.copy(e),l&&l.copy(r),i<h))return Math.sqrt(i)}let f=0;for(let s=0;s<3;s++)for(let n=0;n<=1;n++)for(let r=0;r<=1;r++){const o=(s+1)%3,a=(s+2)%3,l=1<<s|n<<o|r<<a,h=u[n<<o|r<<a],p=u[l];e[f].set(h,p);const m=t[s],g=t[o],_=t[a],y=i[f],w=y.start,v=y.end;w[m]=c[m],w[g]=n?c[g]:d[g],w[_]=r?c[_]:d[g],v[m]=d[m],v[g]=n?c[g]:d[g],v[_]=r?c[_]:d[g],f++}for(let t=0;t<=1;t++)for(let e=0;e<=1;e++)for(let i=0;i<=1;i++){r.x=t?d.x:c.x,r.y=e?d.y:c.y,r.z=i?d.z:c.z,this.closestPointToPoint(r,n);const s=r.distanceToSquared(n);if(s<p&&(p=s,a&&a.copy(n),l&&l.copy(r),s<h))return Math.sqrt(s)}for(let t=0;t<12;t++){const s=e[t];for(let t=0;t<12;t++){const e=i[t];Yi(s,e,n,r);const o=n.distanceToSquared(r);if(o<p&&(p=o,a&&a.copy(n),l&&l.copy(r),o<h))return Math.sqrt(o)}}return Math.sqrt(p)}}();class Ki{constructor(t){this._getNewPrimitive=t,this._primitives=[]}getPrimitive(){const t=this._primitives;return 0===t.length?this._getNewPrimitive():t.pop()}releasePrimitive(t){this._primitives.push(t)}}class Ji extends Ki{constructor(){super(()=>new Xi)}}const ts=new Ji;function es(t,e){return 65535===e[t+15]}function is(t,e){return e[t+6]}function ss(t,e){return e[t+14]}function ns(t){return t+8}function rs(t,e){return e[t+6]}function os(t,e){return e[t+7]}function as(t){return t}const ls=new class{constructor(){this.float32Array=null,this.uint16Array=null,this.uint32Array=null;const t=[];let e=null;this.setBuffer=i=>{e&&t.push(e),e=i,this.float32Array=new Float32Array(i),this.uint16Array=new Uint16Array(i),this.uint32Array=new Uint32Array(i)},this.clearBuffer=()=>{e=null,this.float32Array=null,this.uint16Array=null,this.uint32Array=null,0!==t.length&&this.setBuffer(t.pop())}}};let hs,cs;const ds=[],us=new Ki(()=>new s.Box3);function ps(t,e,i,s,n,r){hs=us.getPrimitive(),cs=us.getPrimitive(),ds.push(hs,cs),ls.setBuffer(t._roots[e]);const o=fs(0,t.geometry,i,s,n,r);ls.clearBuffer(),us.releasePrimitive(hs),us.releasePrimitive(cs),ds.pop(),ds.pop();const a=ds.length;return a>0&&(cs=ds[a-1],hs=ds[a-2]),o}function fs(t,e,i,s,n=null,r=0,o=0){const{float32Array:a,uint16Array:l,uint32Array:h}=ls;let c=2*t;if(es(c,l)){const e=is(t,h),i=ss(c,l);return Pi(t,a,hs),s(e,i,!1,o,r+t,hs)}{let c=function(t){const{uint16Array:e,uint32Array:i}=ls;let s=2*t;for(;!es(s,e);)s=2*(t=ns(t));return is(t,i)},d=function(t){const{uint16Array:e,uint32Array:i}=ls;let s=2*t;for(;!es(s,e);)s=2*(t=rs(t,i));return is(t,i)+ss(s,e)};const u=ns(t),p=rs(t,h);let f,m,g,_,y=u,w=p;if(n&&(g=hs,_=cs,Pi(y,a,g),Pi(w,a,_),f=n(g),m=n(_),m<f)){y=p,w=u;const t=f;f=m,m=t,g=_}g||(g=hs,Pi(y,a,g));const v=i(g,es(2*y,l),f,o+1,r+y);let b;if(2===v){const t=c(y);b=s(t,d(y)-t,!0,o+1,r+y,g)}else b=v&&fs(y,e,i,s,n,r,o+1);if(b)return!0;_=cs,Pi(w,a,_);const x=i(_,es(2*w,l),m,o+1,r+w);let E;if(2===x){const t=c(w);E=s(t,d(w)-t,!0,o+1,r+w,_)}else E=x&&fs(w,e,i,s,n,r,o+1);return!!E}}const ms=new s.Vector3,gs=new s.Vector3;const _s=new s.Vector3,ys=new s.Vector3,ws=new s.Vector3,vs=new s.Vector2,bs=new s.Vector2,xs=new s.Vector2,Es=new s.Vector3,Ts=new s.Vector3,Cs=new s.Vector3,Ss=new s.Vector3;function As(t,e,i,n,r,o,a,l,h){_s.fromBufferAttribute(e,o),ys.fromBufferAttribute(e,a),ws.fromBufferAttribute(e,l);const c=function(t,e,i,n,r,o){let a;return a=o===s.BackSide?t.intersectTriangle(n,i,e,!0,r):t.intersectTriangle(e,i,n,o!==s.DoubleSide,r),null===a?null:{distance:t.origin.distanceTo(r),point:r.clone()}}(t,_s,ys,ws,Ss,h);if(c){n&&(vs.fromBufferAttribute(n,o),bs.fromBufferAttribute(n,a),xs.fromBufferAttribute(n,l),c.uv=s.Triangle.getInterpolation(Ss,_s,ys,ws,vs,bs,xs,new s.Vector2)),r&&(vs.fromBufferAttribute(r,o),bs.fromBufferAttribute(r,a),xs.fromBufferAttribute(r,l),c.uv1=s.Triangle.getInterpolation(Ss,_s,ys,ws,vs,bs,xs,new s.Vector2)),i&&(Es.fromBufferAttribute(i,o),Ts.fromBufferAttribute(i,a),Cs.fromBufferAttribute(i,l),c.normal=s.Triangle.getInterpolation(Ss,_s,ys,ws,Es,Ts,Cs,new s.Vector3),c.normal.dot(t.direction)>0&&c.normal.multiplyScalar(-1));const e={a:o,b:a,c:l,normal:new s.Vector3,materialIndex:0};s.Triangle.getNormal(_s,ys,ws,e.normal),c.face=e,c.faceIndex=o}return c}function Os(t,e,i,s,n){const r=3*s;let o=r+0,a=r+1,l=r+2;const h=t.index;t.index&&(o=h.getX(o),a=h.getX(a),l=h.getX(l));const{position:c,normal:d,uv:u,uv1:p}=t.attributes,f=As(i,c,d,u,p,o,a,l,e);return f?(f.faceIndex=s,n&&n.push(f),f):null}function Ps(t,e,i,s){const n=t.a,r=t.b,o=t.c;let a=e,l=e+1,h=e+2;i&&(a=i.getX(a),l=i.getX(l),h=i.getX(h)),n.x=s.getX(a),n.y=s.getY(a),n.z=s.getZ(a),r.x=s.getX(l),r.y=s.getY(l),r.z=s.getZ(l),o.x=s.getX(h),o.y=s.getY(h),o.z=s.getZ(h)}function Is(t,e,i,s,n,r,o){const{geometry:a}=i,{index:l}=a,h=a.attributes.position;for(let i=t,a=e+t;i<a;i++){let t;if(t=i,Ps(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}function Ds(t,e=null){e&&Array.isArray(e)&&(e=new Set(e));const i=t.geometry,s=i.index?i.index.array:null,n=i.attributes.position;let r,o,a,l,h=0;const c=t._roots;for(let t=0,e=c.length;t<e;t++)r=c[t],o=new Uint32Array(r),a=new Uint16Array(r),l=new Float32Array(r),d(0,h),h+=r.byteLength;function d(t,i,r=!1){const h=2*t;if(a[h+15]===xi){const e=o[t+6];let i=1/0,r=1/0,c=1/0,d=-1/0,u=-1/0,p=-1/0;for(let t=3*e,o=3*(e+a[h+14]);t<o;t++){let e=s[t];const o=n.getX(e),a=n.getY(e),l=n.getZ(e);o<i&&(i=o),o>d&&(d=o),a<r&&(r=a),a>u&&(u=a),l<c&&(c=l),l>p&&(p=l)}return(l[t+0]!==i||l[t+1]!==r||l[t+2]!==c||l[t+3]!==d||l[t+4]!==u||l[t+5]!==p)&&(l[t+0]=i,l[t+1]=r,l[t+2]=c,l[t+3]=d,l[t+4]=u,l[t+5]=p,!0)}{const s=t+8,n=o[t+6],a=s+i,h=n+i;let c=r,u=!1,p=!1;e?c||(u=e.has(a),p=e.has(h),c=!u&&!p):(u=!0,p=!0);const f=c||p;let m=!1;(c||u)&&(m=d(s,i,c));let g=!1;f&&(g=d(n,i,c));const _=m||g;if(_)for(let e=0;e<3;e++){const i=s+e,r=n+e,o=l[i],a=l[i+3],h=l[r],c=l[r+3];l[t+e]=o<h?o:h,l[t+e+3]=a>c?a:c}return _}}}const ks=new s.Box3;function Ms(t,e,i,s){return Pi(t,e,ks),i.intersectBox(ks,s)}function zs(t,e,i,s,n,r,o){const{geometry:a}=i,{index:l}=a,h=a.attributes.position;for(let a=t,c=e+t;a<c;a++){let t;if(t=i.resolveTriangleIndex(a),Ps(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}const Bs=new s.Vector3;function Ns(t,e,i,s,n){ls.setBuffer(t._roots[e]),Us(0,t,i,s,n),ls.clearBuffer()}function Us(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=ls,l=2*t;if(es(l,o)){!function(t,e,i,s,n,r){const{geometry:o,_indirectBuffer:a}=t;for(let t=s,a=s+n;t<a;t++)Os(o,e,i,t,r)}(e,i,s,is(t,a),ss(l,o),n)}else{const o=ns(t);Ms(o,r,s,Bs)&&Us(o,e,i,s,n);const l=rs(t,a);Ms(l,r,s,Bs)&&Us(l,e,i,s,n)}}const Ls=new s.Vector3,Rs=["x","y","z"];function Vs(t,e,i,s){ls.setBuffer(t._roots[e]);const n=Fs(0,t,i,s);return ls.clearBuffer(),n}function Fs(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=ls;let a=2*t;if(es(a,r)){return function(t,e,i,s,n){const{geometry:r,_indirectBuffer:o}=t;let a=1/0,l=null;for(let t=s,o=s+n;t<o;t++){let s;s=Os(r,e,i,t),s&&s.distance<a&&(l=s,a=s.distance)}return l}(e,i,s,is(t,o),ss(a,r))}{const r=os(t,o),a=Rs[r],l=s.direction[a]>=0;let h,c;l?(h=ns(t),c=rs(t,o)):(h=rs(t,o),c=ns(t));const d=Ms(h,n,s,Ls)?Fs(h,e,i,s):null;if(d){const t=d.point[a];if(l?t<=n[c+r]:t>=n[c+r+3])return d}const u=Ms(c,n,s,Ls)?Fs(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const js=new s.Box3,Hs=new Xi,Zs=new Xi,$s=new s.Matrix4,Ws=new Qi,Ys=new Qi;function Gs(t,e,i,s){ls.setBuffer(t._roots[e]);const n=qs(0,t,i,s);return ls.clearBuffer(),n}function qs(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=ls;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),Ws.set(i.boundingBox.min,i.boundingBox.max,s),n=Ws);if(!es(l,o)){const o=t+8,l=a[t+6];Pi(o,r,js);if(n.intersectsBox(js)&&qs(o,e,i,s,n))return!0;Pi(l,r,js);return!!(n.intersectsBox(js)&&qs(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=is(t,a),f=ss(l,o);if($s.copy(s).invert(),i.boundsTree){Pi(t,r,Ys),Ys.matrix.copy($s),Ys.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>Ys.intersectsBox(t),intersectsTriangle:t=>{t.a.applyMatrix4(s),t.b.applyMatrix4(s),t.c.applyMatrix4(s),t.needsUpdate=!0;for(let e=3*p,i=3*(f+p);e<i;e+=3)if(Ps(Zs,e,h,c),Zs.needsUpdate=!0,t.intersectsTriangle(Zs))return!0;return!1}})}for(let t=3*p,e=3*(f+p);t<e;t+=3){Ps(Hs,t,h,c),Hs.a.applyMatrix4($s),Hs.b.applyMatrix4($s),Hs.c.applyMatrix4($s),Hs.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(Ps(Zs,t,d,u),Zs.needsUpdate=!0,Hs.intersectsTriangle(Zs))return!0}}}const Xs=new s.Matrix4,Qs=new Qi,Ks=new Qi,Js=new s.Vector3,tn=new s.Vector3,en=new s.Vector3,sn=new s.Vector3;function nn(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),Qs.set(e.boundingBox.min,e.boundingBox.max,i),Qs.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=ts.getPrimitive(),p=ts.getPrimitive();let f=Js,m=tn,g=null,_=null;n&&(g=en,_=sn);let y=1/0,w=null,v=null;return Xs.copy(i).invert(),Ks.matrix.copy(Xs),t.shapecast({boundsTraverseOrder:t=>Qs.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(Ks.min.copy(t.min),Ks.max.copy(t.max),Ks.needsUpdate=!0),!0),intersectsRange:(t,s)=>{if(e.boundsTree){return e.boundsTree.shapecast({boundsTraverseOrder:t=>Ks.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o,intersectsRange:(e,n)=>{for(let o=e,a=e+n;o<a;o++){Ps(p,3*o,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=t,i=t+s;e<i;e++){Ps(u,3*e,h,l),u.needsUpdate=!0;const t=u.distanceToTriangle(p,f,g);if(t<y&&(m.copy(f),_&&_.copy(g),y=t,w=e,v=o),t<r)return!0}}}})}for(let n=0,o=Ci(e);n<o;n++){Ps(p,3*n,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=t,i=t+s;e<i;e++){Ps(u,3*e,h,l),u.needsUpdate=!0;const t=u.distanceToTriangle(p,f,g);if(t<y&&(m.copy(f),_&&_.copy(g),y=t,w=e,v=n),t<r)return!0}}}}),ts.releasePrimitive(u),ts.releasePrimitive(p),y===1/0?null:(s.point?s.point.copy(m):s.point=m.clone(),s.distance=y,s.faceIndex=w,n&&(n.point?n.point.copy(_):n.point=_.clone(),n.point.applyMatrix4(Xs),m.applyMatrix4(Xs),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}function rn(t,e=null){e&&Array.isArray(e)&&(e=new Set(e));const i=t.geometry,s=i.index?i.index.array:null,n=i.attributes.position;let r,o,a,l,h=0;const c=t._roots;for(let t=0,e=c.length;t<e;t++)r=c[t],o=new Uint32Array(r),a=new Uint16Array(r),l=new Float32Array(r),d(0,h),h+=r.byteLength;function d(i,r,h=!1){const c=2*i;if(a[c+15]===xi){const e=o[i+6];let r=1/0,h=1/0,d=1/0,u=-1/0,p=-1/0,f=-1/0;for(let i=e,o=e+a[c+14];i<o;i++){const e=3*t.resolveTriangleIndex(i);for(let t=0;t<3;t++){let i=e+t;i=s?s[i]:i;const o=n.getX(i),a=n.getY(i),l=n.getZ(i);o<r&&(r=o),o>u&&(u=o),a<h&&(h=a),a>p&&(p=a),l<d&&(d=l),l>f&&(f=l)}}return(l[i+0]!==r||l[i+1]!==h||l[i+2]!==d||l[i+3]!==u||l[i+4]!==p||l[i+5]!==f)&&(l[i+0]=r,l[i+1]=h,l[i+2]=d,l[i+3]=u,l[i+4]=p,l[i+5]=f,!0)}{const t=i+8,s=o[i+6],n=t+r,a=s+r;let c=h,u=!1,p=!1;e?c||(u=e.has(n),p=e.has(a),c=!u&&!p):(u=!0,p=!0);const f=c||p;let m=!1;(c||u)&&(m=d(t,r,c));let g=!1;f&&(g=d(s,r,c));const _=m||g;if(_)for(let e=0;e<3;e++){const n=t+e,r=s+e,o=l[n],a=l[n+3],h=l[r],c=l[r+3];l[i+e]=o<h?o:h,l[i+e+3]=a>c?a:c}return _}}}const on=new s.Vector3;function an(t,e,i,s,n){ls.setBuffer(t._roots[e]),ln(0,t,i,s,n),ls.clearBuffer()}function ln(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=ls,l=2*t;if(es(l,o)){!function(t,e,i,s,n,r){const{geometry:o,_indirectBuffer:a}=t;for(let t=s,l=s+n;t<l;t++)Os(o,e,i,a?a[t]:t,r)}(e,i,s,is(t,a),ss(l,o),n)}else{const o=ns(t);Ms(o,r,s,on)&&ln(o,e,i,s,n);const l=rs(t,a);Ms(l,r,s,on)&&ln(l,e,i,s,n)}}const hn=new s.Vector3,cn=["x","y","z"];function dn(t,e,i,s){ls.setBuffer(t._roots[e]);const n=un(0,t,i,s);return ls.clearBuffer(),n}function un(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=ls;let a=2*t;if(es(a,r)){return function(t,e,i,s,n){const{geometry:r,_indirectBuffer:o}=t;let a=1/0,l=null;for(let t=s,h=s+n;t<h;t++){let s;s=Os(r,e,i,o?o[t]:t),s&&s.distance<a&&(l=s,a=s.distance)}return l}(e,i,s,is(t,o),ss(a,r))}{const r=os(t,o),a=cn[r],l=s.direction[a]>=0;let h,c;l?(h=ns(t),c=rs(t,o)):(h=rs(t,o),c=ns(t));const d=Ms(h,n,s,hn)?un(h,e,i,s):null;if(d){const t=d.point[a];if(l?t<=n[c+r]:t>=n[c+r+3])return d}const u=Ms(c,n,s,hn)?un(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const pn=new s.Box3,fn=new Xi,mn=new Xi,gn=new s.Matrix4,_n=new Qi,yn=new Qi;function wn(t,e,i,s){ls.setBuffer(t._roots[e]);const n=vn(0,t,i,s);return ls.clearBuffer(),n}function vn(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=ls;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),_n.set(i.boundingBox.min,i.boundingBox.max,s),n=_n);if(!es(l,o)){const o=t+8,l=a[t+6];Pi(o,r,pn);if(n.intersectsBox(pn)&&vn(o,e,i,s,n))return!0;Pi(l,r,pn);return!!(n.intersectsBox(pn)&&vn(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=is(t,a),f=ss(l,o);if(gn.copy(s).invert(),i.boundsTree){Pi(t,r,yn),yn.matrix.copy(gn),yn.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>yn.intersectsBox(t),intersectsTriangle:t=>{t.a.applyMatrix4(s),t.b.applyMatrix4(s),t.c.applyMatrix4(s),t.needsUpdate=!0;for(let i=p,s=f+p;i<s;i++)if(Ps(mn,3*e.resolveTriangleIndex(i),h,c),mn.needsUpdate=!0,t.intersectsTriangle(mn))return!0;return!1}})}for(let t=p,i=f+p;t<i;t++){const i=e.resolveTriangleIndex(t);Ps(fn,3*i,h,c),fn.a.applyMatrix4(gn),fn.b.applyMatrix4(gn),fn.c.applyMatrix4(gn),fn.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(Ps(mn,t,d,u),mn.needsUpdate=!0,fn.intersectsTriangle(mn))return!0}}}const bn=new s.Matrix4,xn=new Qi,En=new Qi,Tn=new s.Vector3,Cn=new s.Vector3,Sn=new s.Vector3,An=new s.Vector3;function On(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),xn.set(e.boundingBox.min,e.boundingBox.max,i),xn.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=ts.getPrimitive(),p=ts.getPrimitive();let f=Tn,m=Cn,g=null,_=null;n&&(g=Sn,_=An);let y=1/0,w=null,v=null;return bn.copy(i).invert(),En.matrix.copy(bn),t.shapecast({boundsTraverseOrder:t=>xn.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(En.min.copy(t.min),En.max.copy(t.max),En.needsUpdate=!0),!0),intersectsRange:(s,n)=>{if(e.boundsTree){const a=e.boundsTree;return a.shapecast({boundsTraverseOrder:t=>En.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o,intersectsRange:(e,o)=>{for(let b=e,x=e+o;b<x;b++){const e=a.resolveTriangleIndex(b);Ps(p,3*e,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=s,i=s+n;e<i;e++){const i=t.resolveTriangleIndex(e);Ps(u,3*i,h,l),u.needsUpdate=!0;const s=u.distanceToTriangle(p,f,g);if(s<y&&(m.copy(f),_&&_.copy(g),y=s,w=e,v=b),s<r)return!0}}}})}for(let o=0,a=Ci(e);o<a;o++){Ps(p,3*o,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=s,i=s+n;e<i;e++){const i=t.resolveTriangleIndex(e);Ps(u,3*i,h,l),u.needsUpdate=!0;const s=u.distanceToTriangle(p,f,g);if(s<y&&(m.copy(f),_&&_.copy(g),y=s,w=e,v=o),s<r)return!0}}}}),ts.releasePrimitive(u),ts.releasePrimitive(p),y===1/0?null:(s.point?s.point.copy(m):s.point=m.clone(),s.distance=y,s.faceIndex=w,n&&(n.point?n.point.copy(_):n.point=_.clone(),n.point.applyMatrix4(bn),m.applyMatrix4(bn),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}const Pn=new ls.constructor,In=new ls.constructor,Dn=new Ki(()=>new s.Box3),kn=new s.Box3,Mn=new s.Box3,zn=new s.Box3,Bn=new s.Box3;let Nn=!1;function Un(t,e,i,s,n,r=0,o=0,a=0,l=0,h=null,c=!1){let d,u;c?(d=In,u=Pn):(d=Pn,u=In);const p=d.float32Array,f=d.uint32Array,m=d.uint16Array,g=u.float32Array,_=u.uint32Array,y=u.uint16Array,w=2*e,v=es(2*t,m),b=es(w,y);let x=!1;if(b&&v)x=c?n(is(e,_),ss(2*e,y),is(t,f),ss(2*t,m),l,o+e,a,r+t):n(is(t,f),ss(2*t,m),is(e,_),ss(2*e,y),a,r+t,l,o+e);else if(b){const h=Dn.getPrimitive();Pi(e,g,h),h.applyMatrix4(i);const d=ns(t),u=rs(t,f);Pi(d,p,kn),Pi(u,p,Mn);const m=h.intersectsBox(kn),_=h.intersectsBox(Mn);x=m&&Un(e,d,s,i,n,o,r,l,a+1,h,!c)||_&&Un(e,u,s,i,n,o,r,l,a+1,h,!c),Dn.releasePrimitive(h)}else{const d=ns(e),u=rs(e,_);Pi(d,g,zn),Pi(u,g,Bn);const m=h.intersectsBox(zn),y=h.intersectsBox(Bn);if(m&&y)x=Un(t,d,i,s,n,r,o,a,l+1,h,c)||Un(t,u,i,s,n,r,o,a,l+1,h,c);else if(m)if(v)x=Un(t,d,i,s,n,r,o,a,l+1,h,c);else{const e=Dn.getPrimitive();e.copy(zn).applyMatrix4(i);const h=ns(t),u=rs(t,f);Pi(h,p,kn),Pi(u,p,Mn);const m=e.intersectsBox(kn),g=e.intersectsBox(Mn);x=m&&Un(d,h,s,i,n,o,r,l,a+1,e,!c)||g&&Un(d,u,s,i,n,o,r,l,a+1,e,!c),Dn.releasePrimitive(e)}else if(y)if(v)x=Un(t,u,i,s,n,r,o,a,l+1,h,c);else{const e=Dn.getPrimitive();e.copy(Bn).applyMatrix4(i);const h=ns(t),d=rs(t,f);Pi(h,p,kn),Pi(d,p,Mn);const m=e.intersectsBox(kn),g=e.intersectsBox(Mn);x=m&&Un(u,h,s,i,n,o,r,l,a+1,e,!c)||g&&Un(u,d,s,i,n,o,r,l,a+1,e,!c),Dn.releasePrimitive(e)}}return x}const Ln=new Qi,Rn=new s.Box3;class Vn{static serialize(t,e={}){e={cloneBuffers:!0,...e};const i=t.geometry,s=t._roots,n=t._indirectBuffer,r=i.getIndex();let o;return o=e.cloneBuffers?{roots:s.map(t=>t.slice()),index:r.array.slice(),indirectBuffer:n?n.slice():null}:{roots:s,index:r.array,indirectBuffer:n},o}static deserialize(t,e,i={}){i={setIndex:!0,indirect:Boolean(t.indirectBuffer),...i};const{index:n,roots:r,indirectBuffer:o}=t,a=new Vn(e,{...i,[Ti]:!0});if(a._roots=r,a._indirectBuffer=o||null,i.setIndex){const i=e.getIndex();if(null===i){const i=new s.BufferAttribute(t.index,1,!1);e.setIndex(i)}else i.array!==n&&(i.array.set(n),i.needsUpdate=!0)}return a}get indirect(){return!!this._indirectBuffer}constructor(t,e={}){if(!t.isBufferGeometry)throw new Error("MeshBVH: Only BufferGeometries are supported.");if(t.index&&t.index.isInterleavedBufferAttribute)throw new Error("MeshBVH: InterleavedBufferAttribute is not supported for the index attribute.");if((e=Object.assign({strategy:0,maxDepth:40,maxLeafTris:10,verbose:!0,useSharedArrayBuffer:!1,setBoundingBox:!0,onProgress:null,indirect:!1,[Ti]:!1},e)).useSharedArrayBuffer&&"undefined"==typeof SharedArrayBuffer)throw new Error("MeshBVH: SharedArrayBuffer is not available.");this.geometry=t,this._roots=null,this._indirectBuffer=null,e[Ti]||(Zi(this,e),!t.boundingBox&&e.setBoundingBox&&(t.boundingBox=this.getBoundingBox(new s.Box3)));const{_indirectBuffer:i}=this;this.resolveTriangleIndex=e.indirect?t=>i[t]:t=>t}refit(t=null){return(this.indirect?rn:Ds)(this,t)}traverse(t,e=0){const i=this._roots[e],s=new Uint32Array(i),n=new Uint16Array(i);!function e(r,o=0){const a=2*r,l=n[a+15]===xi;if(l){const e=s[r+6],h=n[a+14];t(o,l,new Float32Array(i,4*r,6),e,h)}else{const n=r+8,a=s[r+6],h=s[r+7];t(o,l,new Float32Array(i,4*r,6),h)||(e(n,o+1),e(a,o+1))}}(0)}raycast(t,e=s.FrontSide){const i=this._roots,n=this.geometry,r=[],o=e.isMaterial,a=Array.isArray(e),l=n.groups,h=o?e.side:e,c=this.indirect?an:Ns;for(let s=0,n=i.length;s<n;s++){const i=a?e[l[s].materialIndex].side:h,n=r.length;if(c(this,s,i,t,r),a){const t=l[s].materialIndex;for(let e=n,i=r.length;e<i;e++)r[e].face.materialIndex=t}}return r}raycastFirst(t,e=s.FrontSide){const i=this._roots,n=this.geometry,r=e.isMaterial,o=Array.isArray(e);let a=null;const l=n.groups,h=r?e.side:e,c=this.indirect?dn:Vs;for(let s=0,n=i.length;s<n;s++){const i=c(this,s,o?e[l[s].materialIndex].side:h,t);null!=i&&(null==a||i.distance<a.distance)&&(a=i,o&&(i.face.materialIndex=l[s].materialIndex))}return a}intersectsGeometry(t,e){let i=!1;const s=this._roots,n=this.indirect?wn:Gs;for(let r=0,o=s.length;r<o&&(i=n(this,r,t,e),!i);r++);return i}shapecast(t){const e=ts.getPrimitive(),i=this.indirect?zs:Is;let{boundsTraverseOrder:s,intersectsBounds:n,intersectsRange:r,intersectsTriangle:o}=t;if(r&&o){const t=r;r=(s,n,r,a,l)=>!!t(s,n,r,a,l)||i(s,n,this,o,r,a,e)}else r||(r=o?(t,s,n,r)=>i(t,s,this,o,n,r,e):(t,e,i)=>i);let a=!1,l=0;const h=this._roots;for(let t=0,e=h.length;t<e;t++){const e=h[t];if(a=ps(this,t,n,r,s,l),a)break;l+=e.byteLength}return ts.releasePrimitive(e),a}bvhcast(t,e,i){let{intersectsRanges:n,intersectsTriangles:r}=i;const o=ts.getPrimitive(),a=this.geometry.index,l=this.geometry.attributes.position,h=this.indirect?t=>{const e=this.resolveTriangleIndex(t);Ps(o,3*e,a,l)}:t=>{Ps(o,3*t,a,l)},c=ts.getPrimitive(),d=t.geometry.index,u=t.geometry.attributes.position,p=t.indirect?e=>{const i=t.resolveTriangleIndex(e);Ps(c,3*i,d,u)}:t=>{Ps(c,3*t,d,u)};if(r){const t=(t,i,s,n,a,l,d,u)=>{for(let f=s,m=s+n;f<m;f++){p(f),c.a.applyMatrix4(e),c.b.applyMatrix4(e),c.c.applyMatrix4(e),c.needsUpdate=!0;for(let e=t,s=t+i;e<s;e++)if(h(e),o.needsUpdate=!0,r(o,c,e,f,a,l,d,u))return!0}return!1};if(n){const e=n;n=function(i,s,n,r,o,a,l,h){return!!e(i,s,n,r,o,a,l,h)||t(i,s,n,r,o,a,l,h)}}else n=t}return function(t,e,i,n){if(Nn)throw new Error("MeshBVH: Recursive calls to bvhcast not supported.");Nn=!0;const r=t._roots,o=e._roots;let a,l=0,h=0;const c=(new s.Matrix4).copy(i).invert();for(let t=0,e=r.length;t<e;t++){Pn.setBuffer(r[t]),h=0;const e=Dn.getPrimitive();Pi(as(0),Pn.float32Array,e),e.applyMatrix4(c);for(let s=0,r=o.length;s<r&&(In.setBuffer(o[t]),a=Un(0,0,i,c,n,l,h,0,0,e),In.clearBuffer(),h+=o[s].length,!a);s++);if(Dn.releasePrimitive(e),Pn.clearBuffer(),l+=r[t].length,a)break}return Nn=!1,a}(this,t,e,n)}intersectsBox(t,e){return Ln.set(t.min,t.max,e),Ln.needsUpdate=!0,this.shapecast({intersectsBounds:t=>Ln.intersectsBox(t),intersectsTriangle:t=>Ln.intersectsTriangle(t)})}intersectsSphere(t){return this.shapecast({intersectsBounds:e=>t.intersectsBox(e),intersectsTriangle:e=>e.intersectsSphere(t)})}closestPointToGeometry(t,e,i={},s={},n=0,r=1/0){return(this.indirect?On:nn)(this,t,e,i,s,n,r)}closestPointToPoint(t,e={},i=0,s=1/0){return function(t,e,i={},s=0,n=1/0){const r=s*s,o=n*n;let a=1/0,l=null;if(t.shapecast({boundsTraverseOrder:t=>(ms.copy(e).clamp(t.min,t.max),ms.distanceToSquared(e)),intersectsBounds:(t,e,i)=>i<a&&i<o,intersectsTriangle:(t,i)=>{t.closestPointToPoint(e,ms);const s=e.distanceToSquared(ms);return s<a&&(gs.copy(ms),a=s,l=i),s<r}}),a===1/0)return null;const h=Math.sqrt(a);return i.point?i.point.copy(gs):i.point=gs.clone(),i.distance=h,i.faceIndex=l,i}(this,t,e,i,s)}getBoundingBox(t){t.makeEmpty();return this._roots.forEach(e=>{Pi(0,new Float32Array(e),Rn),t.union(Rn)}),t}}function Fn(t,e,i){return null===t?null:(t.point.applyMatrix4(e.matrixWorld),t.distance=t.point.distanceTo(i.ray.origin),t.object=e,t.distance<i.near||t.distance>i.far?null:t)}const jn=new s.Ray,Hn=new s.Matrix4,Zn=s.Mesh.prototype.raycast;function $n(t,e){if(this.geometry.boundsTree){if(void 0===this.material)return;Hn.copy(this.matrixWorld).invert(),jn.copy(t.ray).applyMatrix4(Hn);const i=this.geometry.boundsTree;if(!0===t.firstHitOnly){const s=Fn(i.raycastFirst(jn,this.material),this,t);s&&e.push(s)}else{const s=i.raycast(jn,this.material);for(let i=0,n=s.length;i<n;i++){const n=Fn(s[i],this,t);n&&e.push(n)}}}else Zn.call(this,t,e)}function Wn(t){return this.boundsTree=new Vn(this,t),this.boundsTree}function Yn(){this.boundsTree=null}const Gn=class t{constructor(){e(this,"onDisposed",new h),e(this,"list",new i.DataMap),e(this,"enabled",!1),e(this,"_clock"),e(this,"onInit",new h),e(this,"update",()=>{if(!this.enabled)return;const t=this._clock.getDelta();for(const[e,i]of this.list)i.enabled&&i.isUpdateable()&&i.update(t);requestAnimationFrame(this.update)}),this._clock=new a.Clock,t.setupBVH()}add(t,e){if(this.list.has(t))throw new Error("You're trying to add a component that already exists in the components instance. Use Components.get() instead.");v.validate(t),this.list.set(t,e)}get(t){const e=t.uuid;if(!this.list.has(e)){const i=new t(this);return i.isDisposeable()&&i.onDisposed.add(()=>this.list.delete(e)),this.list.has(e)||this.add(e,i),i}return this.list.get(e)}init(){this.enabled=!0;for(const[t,e]of this.list.entries())e.enabled=!0;this._clock.start(),this.update(),this.onInit.trigger()}dispose(){let t;this.enabled=!1;for(const[e,i]of this.list)i.enabled=!1,e!==Xt.uuid?i.isDisposeable()&&i.dispose():t=i;null==t||t.dispose(),this._clock.stop(),this.onDisposed.trigger()}static setupBVH(){a.BufferGeometry.prototype.computeBoundsTree=Wn,a.BufferGeometry.prototype.disposeBoundsTree=Yn,a.Mesh.prototype.raycast=$n}};e(Gn,"release","2.4.3");let qn=Gn;class Xn{constructor(t){e(this,"enabled",!1),e(this,"id","FirstPerson"),this.camera=t}set(t){if(this.enabled=t,t){if("Perspective"!==this.camera.projection.current)return void this.camera.set("Orbit");this.setupFirstPersonCamera()}}setupFirstPersonCamera(){const t=this.camera.controls,e=new a.Vector3;t.distance--,t.getPosition(e),t.minDistance=1,t.maxDistance=1,t.distance=1,t.moveTo(e.x,e.y,e.z),t.truckSpeed=50,t.mouseButtons.wheel=pi.ACTION.DOLLY,t.touches.two=pi.ACTION.TOUCH_ZOOM_TRUCK}}class Qn{constructor(t){e(this,"enabled",!0),e(this,"id","Orbit"),this.camera=t,this.activateOrbitControls()}set(t){this.enabled=t,t&&this.activateOrbitControls()}activateOrbitControls(){const t=this.camera.controls;t.minDistance=1,t.maxDistance=300;const e=new a.Vector3;t.getPosition(e);const i=e.length();t.distance=i,t.truckSpeed=2;const{rotation:s}=this.camera.three,n=new a.Vector3(0,0,-1).applyEuler(s),r=e.addScaledVector(n,i);t.moveTo(r.x,r.y,r.z)}}class Kn{constructor(t){e(this,"enabled",!1),e(this,"id","Plan"),e(this,"mouseAction1"),e(this,"mouseAction2"),e(this,"mouseInitialized",!1),e(this,"defaultAzimuthSpeed"),e(this,"defaultPolarSpeed"),this.camera=t,this.defaultAzimuthSpeed=t.controls.azimuthRotateSpeed,this.defaultPolarSpeed=t.controls.polarRotateSpeed}set(t){this.enabled=t;const e=this.camera.controls;e.azimuthRotateSpeed=t?0:this.defaultAzimuthSpeed,e.polarRotateSpeed=t?0:this.defaultPolarSpeed,this.mouseInitialized||(this.mouseAction1=e.touches.one,this.mouseAction2=e.touches.two,this.mouseInitialized=!0),t?(e.mouseButtons.left=pi.ACTION.TRUCK,e.touches.one=pi.ACTION.TOUCH_TRUCK,e.touches.two=pi.ACTION.TOUCH_ZOOM):(e.mouseButtons.left=pi.ACTION.ROTATE,e.touches.one=this.mouseAction1,e.touches.two=this.mouseAction2)}}class Jn{constructor(t){e(this,"onChanged",new h),e(this,"current","Perspective"),e(this,"camera"),e(this,"matchOrthoDistanceEnabled",!1),e(this,"_component"),e(this,"_previousDistance",-1),this._component=t,this.camera=t.three}async set(t){this.current!==t&&("Orthographic"===t?this.setOrthoCamera():await this.setPerspectiveCamera(),this.onChanged.trigger(this.camera))}async toggle(){const t="Perspective"===this.current?"Orthographic":"Perspective";await this.set(t)}setOrthoCamera(){if(null===this._component.mode)return;if("FirstPerson"===this._component.mode.id)return;this._previousDistance=this._component.controls.distance,this._component.controls.distance=200;const t=this.getPerspectiveDims();if(!t)return;const{width:e,height:i}=t;this.setupOrthoCamera(i,e),this.camera=this._component.threeOrtho,this.current="Orthographic"}getPerspectiveDims(){const t=this._component.currentWorld;if(!t||!t.renderer)return null;const e=new a.Vector3;this._component.threePersp.getWorldDirection(e);const i=new a.Vector3;this._component.controls.getTarget(i);const s=i.clone().sub(this._component.threePersp.position).dot(e),n=t.renderer.getSize(),r=n.x/n.y,o=this._component.threePersp,l=2*s*Math.atan(o.fov*(Math.PI/180)/2);return{width:l*r,height:l}}setupOrthoCamera(t,e){this._component.controls.mouseButtons.wheel=pi.ACTION.ZOOM,this._component.controls.mouseButtons.middle=pi.ACTION.ZOOM;const i=this._component.threePersp,s=this._component.threeOrtho;s.zoom=1,s.left=e/-2,s.right=e/2,s.top=t/2,s.bottom=t/-2,s.updateProjectionMatrix(),s.position.copy(i.position),s.quaternion.copy(i.quaternion),this._component.controls.camera=s}getDistance(){const t=this._component.threePersp,e=this._component.threeOrtho;return(e.top-e.bottom)/e.zoom/(2*Math.atan(t.fov*(Math.PI/180)/2))}async setPerspectiveCamera(){this._component.controls.mouseButtons.wheel=pi.ACTION.DOLLY,this._component.controls.mouseButtons.middle=pi.ACTION.DOLLY;const t=this._component.threePersp,e=this._component.threeOrtho;t.position.copy(e.position),t.quaternion.copy(e.quaternion),this._component.controls.mouseButtons.wheel=pi.ACTION.DOLLY,this.matchOrthoDistanceEnabled?this._component.controls.distance=this.getDistance():this._component.controls.distance=this._previousDistance,await this._component.controls.zoomTo(1),t.updateProjectionMatrix(),this._component.controls.camera=t,this.camera=t,this.current="Perspective"}}class tr extends fi{constructor(t){super(t),e(this,"projection"),e(this,"threeOrtho"),e(this,"threePersp"),e(this,"_userInputButtons",{}),e(this,"_frustumSize",50),e(this,"_navigationModes",new Map),e(this,"_mode",null),e(this,"previousSize",null),this.threePersp=this.three,this.threeOrtho=this.newOrthoCamera(),this.projection=new Jn(this),this.onAspectUpdated.add(()=>{this.setOrthoPerspCameraAspect()}),this.projection.onChanged.add(t=>{this.three=t,this.updateAspect()}),this.worlds.onItemSet.add(()=>{this._navigationModes.clear(),this._navigationModes.set("Orbit",new Qn(this)),this._navigationModes.set("FirstPerson",new Xn(this)),this._navigationModes.set("Plan",new Kn(this)),this._mode=this._navigationModes.get("Orbit"),this.mode.set(!0,{preventTargetAdjustment:!0}),this.currentWorld&&this.currentWorld.renderer&&(this.previousSize=this.currentWorld.renderer.getSize().clone())}),this.worlds.onItemDeleted.add(()=>{this._navigationModes.clear()})}get mode(){if(!this._mode)throw new Error("Mode not found, camera not initialized");return this._mode}dispose(){super.dispose(),this.threeOrtho.removeFromParent()}set(t){if(null!==this.mode&&this.mode.id!==t){if(this.mode.set(!1),!this._navigationModes.has(t))throw new Error("The specified mode does not exist!");this._mode=this._navigationModes.get(t),this.mode.set(!0)}}async fit(t,e=1.5){if(!this.enabled)return;const i=Number.MAX_VALUE,s=Number.MIN_VALUE,n=new a.Vector3(i,i,i),r=new a.Vector3(s,s,s);for(const e of t){const t=(new a.Box3).setFromObject(e);t.min.x<n.x&&(n.x=t.min.x),t.min.y<n.y&&(n.y=t.min.y),t.min.z<n.z&&(n.z=t.min.z),t.max.x>r.x&&(r.x=t.max.x),t.max.y>r.y&&(r.y=t.max.y),t.max.z>r.z&&(r.z=t.max.z)}const o=new a.Box3(n,r),l=this.components.get(Xt);if(l.initialized)for(const[,t]of l.list){const e=t.box;e.min.x<n.x&&(n.x=e.min.x),e.min.y<n.y&&(n.y=e.min.y),e.min.z<n.z&&(n.z=e.min.z),e.max.x>r.x&&(r.x=e.max.x),e.max.y>r.y&&(r.y=e.max.y),e.max.z>r.z&&(r.z=e.max.z)}const h=new a.Vector3;o.getSize(h);const c=new a.Vector3;o.getCenter(c);const d=Math.max(h.x,h.y,h.z)*e,u=new a.Sphere(c,d);await this.controls.fitToSphere(u,!0)}setUserInput(t){t?this.enableUserInput():this.disableUserInput()}disableUserInput(){this._userInputButtons.left=this.controls.mouseButtons.left,this._userInputButtons.right=this.controls.mouseButtons.right,this._userInputButtons.middle=this.controls.mouseButtons.middle,this._userInputButtons.wheel=this.controls.mouseButtons.wheel,this.controls.mouseButtons.left=0,this.controls.mouseButtons.right=0,this.controls.mouseButtons.middle=0,this.controls.mouseButtons.wheel=0}enableUserInput(){0!==Object.keys(this._userInputButtons).length&&(this.controls.mouseButtons.left=this._userInputButtons.left,this.controls.mouseButtons.right=this._userInputButtons.right,this.controls.mouseButtons.middle=this._userInputButtons.middle,this.controls.mouseButtons.wheel=this._userInputButtons.wheel)}newOrthoCamera(){const t=window.innerWidth/window.innerHeight;return new a.OrthographicCamera(this._frustumSize*t/-2,this._frustumSize*t/2,this._frustumSize/2,this._frustumSize/-2,.1,1e3)}setOrthoPerspCameraAspect(){if(!this.currentWorld||!this.currentWorld.renderer)return;if(!this.previousSize)return;const t=this.currentWorld.renderer.getSize(),e=this.threeOrtho.top,i=this.threeOrtho.right,s=e*(t.y/this.previousSize.y),n=i*(t.x/this.previousSize.x);this.threeOrtho.left=-n,this.threeOrtho.right=n,this.threeOrtho.top=s,this.threeOrtho.bottom=-s,this.threeOrtho.updateProjectionMatrix(),this.previousSize.copy(t)}}function er(t){throw new Error('Could not dynamically require "'+t+'". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.')}var ir={exports:{}};
8
+ const ye=1,we=2,ve=4,be=Object.freeze({NONE:0,ROTATE:1,TRUCK:2,SCREEN_PAN:4,OFFSET:8,DOLLY:16,ZOOM:32,TOUCH_ROTATE:64,TOUCH_TRUCK:128,TOUCH_SCREEN_PAN:256,TOUCH_OFFSET:512,TOUCH_DOLLY:1024,TOUCH_ZOOM:2048,TOUCH_DOLLY_TRUCK:4096,TOUCH_DOLLY_SCREEN_PAN:8192,TOUCH_DOLLY_OFFSET:16384,TOUCH_DOLLY_ROTATE:32768,TOUCH_ZOOM_TRUCK:65536,TOUCH_ZOOM_OFFSET:131072,TOUCH_ZOOM_SCREEN_PAN:262144,TOUCH_ZOOM_ROTATE:524288}),xe=0,Ee=1,Te=-1;function Ce(t){return t.isPerspectiveCamera}function Se(t){return t.isOrthographicCamera}const Ae=2*Math.PI,Oe=Math.PI/2,Pe=Math.PI/180;function Ie(t,e,i){return Math.max(e,Math.min(i,t))}function De(t,e=1e-5){return Math.abs(t)<e}function ke(t,e,i=1e-5){return De(t-e,i)}function Me(t,e){return Math.round(t/e)*e}function ze(t){return isFinite(t)?t:t<0?-Number.MAX_VALUE:Number.MAX_VALUE}function Be(t){return Math.abs(t)<Number.MAX_VALUE?t:t*(1/0)}function Ne(t,e,i,s,n=1/0,r){const o=2/(s=Math.max(1e-4,s)),a=o*r,l=1/(1+a+.48*a*a+.235*a*a*a);let h=t-e;const c=e,d=n*s;h=Ie(h,-d,d),e=t-h;const u=(i.value+o*h)*r;i.value=(i.value-o*u)*l;let p=e+(h+u)*l;return c-t>0==p>c&&(p=c,i.value=(p-c)/r),p}function Ue(t,e,i,s,n=1/0,r,o){const a=2/(s=Math.max(1e-4,s)),l=a*r,h=1/(1+l+.48*l*l+.235*l*l*l);let c=e.x,d=e.y,u=e.z,p=t.x-c,f=t.y-d,m=t.z-u;const g=c,_=d,y=u,w=n*s,v=p*p+f*f+m*m;if(v>w*w){const t=Math.sqrt(v);p=p/t*w,f=f/t*w,m=m/t*w}c=t.x-p,d=t.y-f,u=t.z-m;const b=(i.x+a*p)*r,x=(i.y+a*f)*r,E=(i.z+a*m)*r;i.x=(i.x-a*b)*h,i.y=(i.y-a*x)*h,i.z=(i.z-a*E)*h,o.x=c+(p+b)*h,o.y=d+(f+x)*h,o.z=u+(m+E)*h;const T=g-t.x,C=_-t.y,S=y-t.z;return T*(o.x-g)+C*(o.y-_)+S*(o.z-y)>0&&(o.x=g,o.y=_,o.z=y,i.x=(o.x-g)/r,i.y=(o.y-_)/r,i.z=(o.z-y)/r),o}function Le(t,e){e.set(0,0),t.forEach(t=>{e.x+=t.clientX,e.y+=t.clientY}),e.x/=t.length,e.y/=t.length}function Re(t,e){return!!Se(t)&&(console.warn(`${e} is not supported in OrthographicCamera`),!0)}class Ve{constructor(){this._listeners={}}addEventListener(t,e){const i=this._listeners;void 0===i[t]&&(i[t]=[]),-1===i[t].indexOf(e)&&i[t].push(e)}hasEventListener(t,e){const i=this._listeners;return void 0!==i[t]&&-1!==i[t].indexOf(e)}removeEventListener(t,e){const i=this._listeners[t];if(void 0!==i){const t=i.indexOf(e);-1!==t&&i.splice(t,1)}}removeAllEventListeners(t){t?Array.isArray(this._listeners[t])&&(this._listeners[t].length=0):this._listeners={}}dispatchEvent(t){const e=this._listeners[t.type];if(void 0!==e){t.target=this;const i=e.slice(0);for(let e=0,s=i.length;e<s;e++)i[e].call(this,t)}}}var Fe;const je=1/8,He=/Mac/.test(null===(Fe=null===globalThis||void 0===globalThis?void 0:globalThis.navigator)||void 0===Fe?void 0:Fe.platform);let Ze,$e,We,Ye,Ge,qe,Xe,Qe,Ke,Je,ti,ei,ii,si,ni,ri,oi,ai,li,hi,ci,di,ui;class pi extends Ve{static install(t){Ze=t.THREE,$e=Object.freeze(new Ze.Vector3(0,0,0)),We=Object.freeze(new Ze.Vector3(0,1,0)),Ye=Object.freeze(new Ze.Vector3(0,0,1)),Ge=new Ze.Vector2,qe=new Ze.Vector3,Xe=new Ze.Vector3,Qe=new Ze.Vector3,Ke=new Ze.Vector3,Je=new Ze.Vector3,ti=new Ze.Vector3,ei=new Ze.Vector3,ii=new Ze.Vector3,si=new Ze.Vector3,ni=new Ze.Spherical,ri=new Ze.Spherical,oi=new Ze.Box3,ai=new Ze.Box3,li=new Ze.Sphere,hi=new Ze.Quaternion,ci=new Ze.Quaternion,di=new Ze.Matrix4,ui=new Ze.Raycaster}static get ACTION(){return be}set verticalDragToForward(t){console.warn("camera-controls: `verticalDragToForward` was removed. Use `mouseButtons.left = CameraControls.ACTION.SCREEN_PAN` instead.")}constructor(t,e){super(),this.minPolarAngle=0,this.maxPolarAngle=Math.PI,this.minAzimuthAngle=-1/0,this.maxAzimuthAngle=1/0,this.minDistance=Number.EPSILON,this.maxDistance=1/0,this.infinityDolly=!1,this.minZoom=.01,this.maxZoom=1/0,this.smoothTime=.25,this.draggingSmoothTime=.125,this.maxSpeed=1/0,this.azimuthRotateSpeed=1,this.polarRotateSpeed=1,this.dollySpeed=1,this.dollyDragInverted=!1,this.truckSpeed=2,this.dollyToCursor=!1,this.dragToOffset=!1,this.boundaryFriction=0,this.restThreshold=.01,this.colliderMeshes=[],this.cancel=()=>{},this._enabled=!0,this._state=be.NONE,this._viewport=null,this._changedDolly=0,this._changedZoom=0,this._hasRested=!0,this._boundaryEnclosesCamera=!1,this._needsUpdate=!0,this._updatedLastTime=!1,this._elementRect=new DOMRect,this._isDragging=!1,this._dragNeedsUpdate=!0,this._activePointers=[],this._lockedPointer=null,this._interactiveArea=new DOMRect(0,0,1,1),this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._isUserControllingOffset=!1,this._isUserControllingZoom=!1,this._lastDollyDirection=xe,this._thetaVelocity={value:0},this._phiVelocity={value:0},this._radiusVelocity={value:0},this._targetVelocity=new Ze.Vector3,this._focalOffsetVelocity=new Ze.Vector3,this._zoomVelocity={value:0},this._truckInternal=(t,e,i,s)=>{let n,r;if(Ce(this._camera)){const i=qe.copy(this._camera.position).sub(this._target),s=this._camera.getEffectiveFOV()*Pe,o=i.length()*Math.tan(.5*s);n=this.truckSpeed*t*o/this._elementRect.height,r=this.truckSpeed*e*o/this._elementRect.height}else{if(!Se(this._camera))return;{const i=this._camera;n=this.truckSpeed*t*(i.right-i.left)/i.zoom/this._elementRect.width,r=this.truckSpeed*e*(i.top-i.bottom)/i.zoom/this._elementRect.height}}s?(i?this.setFocalOffset(this._focalOffsetEnd.x+n,this._focalOffsetEnd.y,this._focalOffsetEnd.z,!0):this.truck(n,0,!0),this.forward(-r,!0)):i?this.setFocalOffset(this._focalOffsetEnd.x+n,this._focalOffsetEnd.y+r,this._focalOffsetEnd.z,!0):this.truck(n,r,!0)},this._rotateInternal=(t,e)=>{const i=Ae*this.azimuthRotateSpeed*t/this._elementRect.height,s=Ae*this.polarRotateSpeed*e/this._elementRect.height;this.rotate(i,s,!0)},this._dollyInternal=(t,e,i)=>{const s=Math.pow(.95,-t*this.dollySpeed),n=this._sphericalEnd.radius,r=this._sphericalEnd.radius*s,o=Ie(r,this.minDistance,this.maxDistance),a=o-r;this.infinityDolly&&this.dollyToCursor?this._dollyToNoClamp(r,!0):this.infinityDolly&&!this.dollyToCursor?(this.dollyInFixed(a,!0),this._dollyToNoClamp(o,!0)):this._dollyToNoClamp(o,!0),this.dollyToCursor&&(this._changedDolly+=(this.infinityDolly?r:o)-n,this._dollyControlCoord.set(e,i)),this._lastDollyDirection=Math.sign(-t)},this._zoomInternal=(t,e,i)=>{const s=Math.pow(.95,t*this.dollySpeed),n=this._zoom,r=this._zoom*s;this.zoomTo(r,!0),this.dollyToCursor&&(this._changedZoom+=r-n,this._dollyControlCoord.set(e,i))},void 0===Ze&&console.error("camera-controls: `THREE` is undefined. You must first run `CameraControls.install( { THREE: THREE } )`. Check the docs for further information."),this._camera=t,this._yAxisUpSpace=(new Ze.Quaternion).setFromUnitVectors(this._camera.up,We),this._yAxisUpSpaceInverse=this._yAxisUpSpace.clone().invert(),this._state=be.NONE,this._target=new Ze.Vector3,this._targetEnd=this._target.clone(),this._focalOffset=new Ze.Vector3,this._focalOffsetEnd=this._focalOffset.clone(),this._spherical=(new Ze.Spherical).setFromVector3(qe.copy(this._camera.position).applyQuaternion(this._yAxisUpSpace)),this._sphericalEnd=this._spherical.clone(),this._lastDistance=this._spherical.radius,this._zoom=this._camera.zoom,this._zoomEnd=this._zoom,this._lastZoom=this._zoom,this._nearPlaneCorners=[new Ze.Vector3,new Ze.Vector3,new Ze.Vector3,new Ze.Vector3],this._updateNearPlaneCorners(),this._boundary=new Ze.Box3(new Ze.Vector3(-1/0,-1/0,-1/0),new Ze.Vector3(1/0,1/0,1/0)),this._cameraUp0=this._camera.up.clone(),this._target0=this._target.clone(),this._position0=this._camera.position.clone(),this._zoom0=this._zoom,this._focalOffset0=this._focalOffset.clone(),this._dollyControlCoord=new Ze.Vector2,this.mouseButtons={left:be.ROTATE,middle:be.DOLLY,right:be.TRUCK,wheel:Ce(this._camera)?be.DOLLY:Se(this._camera)?be.ZOOM:be.NONE},this.touches={one:be.TOUCH_ROTATE,two:Ce(this._camera)?be.TOUCH_DOLLY_TRUCK:Se(this._camera)?be.TOUCH_ZOOM_TRUCK:be.NONE,three:be.TOUCH_TRUCK};const i=new Ze.Vector2,s=new Ze.Vector2,n=new Ze.Vector2,r=t=>{if(!this._enabled||!this._domElement)return;if(0!==this._interactiveArea.left||0!==this._interactiveArea.top||1!==this._interactiveArea.width||1!==this._interactiveArea.height){const e=this._domElement.getBoundingClientRect(),i=t.clientX/e.width,s=t.clientY/e.height;if(i<this._interactiveArea.left||i>this._interactiveArea.right||s<this._interactiveArea.top||s>this._interactiveArea.bottom)return}const e="mouse"!==t.pointerType?null:(t.buttons&ye)===ye?ye:(t.buttons&ve)===ve?ve:(t.buttons&we)===we?we:null;if(null!==e){const t=this._findPointerByMouseButton(e);t&&this._disposePointer(t)}if((t.buttons&ye)===ye&&this._lockedPointer)return;const i={pointerId:t.pointerId,clientX:t.clientX,clientY:t.clientY,deltaX:0,deltaY:0,mouseButton:e};this._activePointers.push(i),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.ownerDocument.addEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.addEventListener("pointerup",a),this._isDragging=!0,d(t)},o=t=>{t.cancelable&&t.preventDefault();const e=t.pointerId,i=this._lockedPointer||this._findPointerById(e);if(i){if(i.clientX=t.clientX,i.clientY=t.clientY,i.deltaX=t.movementX,i.deltaY=t.movementY,this._state=0,"touch"===t.pointerType)switch(this._activePointers.length){case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else(!this._isDragging&&this._lockedPointer||this._isDragging&&(t.buttons&ye)===ye)&&(this._state=this._state|this.mouseButtons.left),this._isDragging&&(t.buttons&ve)===ve&&(this._state=this._state|this.mouseButtons.middle),this._isDragging&&(t.buttons&we)===we&&(this._state=this._state|this.mouseButtons.right);u()}},a=t=>{const e=this._findPointerById(t.pointerId);if(!e||e!==this._lockedPointer){if(e&&this._disposePointer(e),"touch"===t.pointerType)switch(this._activePointers.length){case 0:this._state=be.NONE;break;case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else this._state=be.NONE;p()}};let l=-1;const h=t=>{if(!this._domElement)return;if(!this._enabled||this.mouseButtons.wheel===be.NONE)return;if(0!==this._interactiveArea.left||0!==this._interactiveArea.top||1!==this._interactiveArea.width||1!==this._interactiveArea.height){const e=this._domElement.getBoundingClientRect(),i=t.clientX/e.width,s=t.clientY/e.height;if(i<this._interactiveArea.left||i>this._interactiveArea.right||s<this._interactiveArea.top||s>this._interactiveArea.bottom)return}if(t.preventDefault(),this.dollyToCursor||this.mouseButtons.wheel===be.ROTATE||this.mouseButtons.wheel===be.TRUCK){const t=performance.now();l-t<1e3&&this._getClientRect(this._elementRect),l=t}const e=He?-1:-3,i=1===t.deltaMode||t.ctrlKey?t.deltaY/e:t.deltaY/(10*e),s=this.dollyToCursor?(t.clientX-this._elementRect.x)/this._elementRect.width*2-1:0,n=this.dollyToCursor?(t.clientY-this._elementRect.y)/this._elementRect.height*-2+1:0;switch(this.mouseButtons.wheel){case be.ROTATE:this._rotateInternal(t.deltaX,t.deltaY),this._isUserControllingRotate=!0;break;case be.TRUCK:this._truckInternal(t.deltaX,t.deltaY,!1,!1),this._isUserControllingTruck=!0;break;case be.SCREEN_PAN:this._truckInternal(t.deltaX,t.deltaY,!1,!0),this._isUserControllingTruck=!0;break;case be.OFFSET:this._truckInternal(t.deltaX,t.deltaY,!0,!1),this._isUserControllingOffset=!0;break;case be.DOLLY:this._dollyInternal(-i,s,n),this._isUserControllingDolly=!0;break;case be.ZOOM:this._zoomInternal(-i,s,n),this._isUserControllingZoom=!0}this.dispatchEvent({type:"control"})},c=t=>{if(this._domElement&&this._enabled){if(this.mouseButtons.right===pi.ACTION.NONE){const e=t instanceof PointerEvent?t.pointerId:0,i=this._findPointerById(e);return i&&this._disposePointer(i),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),void this._domElement.ownerDocument.removeEventListener("pointerup",a)}t.preventDefault()}},d=t=>{if(!this._enabled)return;Le(this._activePointers,Ge),this._getClientRect(this._elementRect),i.copy(Ge),s.copy(Ge);if(this._activePointers.length>=2){const t=Ge.x-this._activePointers[1].clientX,e=Ge.y-this._activePointers[1].clientY,i=Math.sqrt(t*t+e*e);n.set(0,i);const r=.5*(this._activePointers[0].clientX+this._activePointers[1].clientX),o=.5*(this._activePointers[0].clientY+this._activePointers[1].clientY);s.set(r,o)}if(this._state=0,t)if("pointerType"in t&&"touch"===t.pointerType)switch(this._activePointers.length){case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else this._lockedPointer||(t.buttons&ye)!==ye||(this._state=this._state|this.mouseButtons.left),(t.buttons&ve)===ve&&(this._state=this._state|this.mouseButtons.middle),(t.buttons&we)===we&&(this._state=this._state|this.mouseButtons.right);else this._lockedPointer&&(this._state=this._state|this.mouseButtons.left);(this._state&be.ROTATE)!==be.ROTATE&&(this._state&be.TOUCH_ROTATE)!==be.TOUCH_ROTATE&&(this._state&be.TOUCH_DOLLY_ROTATE)!==be.TOUCH_DOLLY_ROTATE&&(this._state&be.TOUCH_ZOOM_ROTATE)!==be.TOUCH_ZOOM_ROTATE||(this._sphericalEnd.theta=this._spherical.theta,this._sphericalEnd.phi=this._spherical.phi,this._thetaVelocity.value=0,this._phiVelocity.value=0),(this._state&be.TRUCK)!==be.TRUCK&&(this._state&be.SCREEN_PAN)!==be.SCREEN_PAN&&(this._state&be.TOUCH_TRUCK)!==be.TOUCH_TRUCK&&(this._state&be.TOUCH_SCREEN_PAN)!==be.TOUCH_SCREEN_PAN&&(this._state&be.TOUCH_DOLLY_TRUCK)!==be.TOUCH_DOLLY_TRUCK&&(this._state&be.TOUCH_DOLLY_SCREEN_PAN)!==be.TOUCH_DOLLY_SCREEN_PAN&&(this._state&be.TOUCH_ZOOM_TRUCK)!==be.TOUCH_ZOOM_TRUCK&&(this._state&be.TOUCH_ZOOM_SCREEN_PAN)!==be.TOUCH_DOLLY_SCREEN_PAN||(this._targetEnd.copy(this._target),this._targetVelocity.set(0,0,0)),(this._state&be.DOLLY)!==be.DOLLY&&(this._state&be.TOUCH_DOLLY)!==be.TOUCH_DOLLY&&(this._state&be.TOUCH_DOLLY_TRUCK)!==be.TOUCH_DOLLY_TRUCK&&(this._state&be.TOUCH_DOLLY_SCREEN_PAN)!==be.TOUCH_DOLLY_SCREEN_PAN&&(this._state&be.TOUCH_DOLLY_OFFSET)!==be.TOUCH_DOLLY_OFFSET&&(this._state&be.TOUCH_DOLLY_ROTATE)!==be.TOUCH_DOLLY_ROTATE||(this._sphericalEnd.radius=this._spherical.radius,this._radiusVelocity.value=0),(this._state&be.ZOOM)!==be.ZOOM&&(this._state&be.TOUCH_ZOOM)!==be.TOUCH_ZOOM&&(this._state&be.TOUCH_ZOOM_TRUCK)!==be.TOUCH_ZOOM_TRUCK&&(this._state&be.TOUCH_ZOOM_SCREEN_PAN)!==be.TOUCH_ZOOM_SCREEN_PAN&&(this._state&be.TOUCH_ZOOM_OFFSET)!==be.TOUCH_ZOOM_OFFSET&&(this._state&be.TOUCH_ZOOM_ROTATE)!==be.TOUCH_ZOOM_ROTATE||(this._zoomEnd=this._zoom,this._zoomVelocity.value=0),(this._state&be.OFFSET)!==be.OFFSET&&(this._state&be.TOUCH_OFFSET)!==be.TOUCH_OFFSET&&(this._state&be.TOUCH_DOLLY_OFFSET)!==be.TOUCH_DOLLY_OFFSET&&(this._state&be.TOUCH_ZOOM_OFFSET)!==be.TOUCH_ZOOM_OFFSET||(this._focalOffsetEnd.copy(this._focalOffset),this._focalOffsetVelocity.set(0,0,0)),this.dispatchEvent({type:"controlstart"})},u=()=>{if(!this._enabled||!this._dragNeedsUpdate)return;this._dragNeedsUpdate=!1,Le(this._activePointers,Ge);const t=this._domElement&&this._domElement.ownerDocument.pointerLockElement===this._domElement?this._lockedPointer||this._activePointers[0]:null,e=t?-t.deltaX:s.x-Ge.x,r=t?-t.deltaY:s.y-Ge.y;if(s.copy(Ge),(this._state&be.ROTATE)!==be.ROTATE&&(this._state&be.TOUCH_ROTATE)!==be.TOUCH_ROTATE&&(this._state&be.TOUCH_DOLLY_ROTATE)!==be.TOUCH_DOLLY_ROTATE&&(this._state&be.TOUCH_ZOOM_ROTATE)!==be.TOUCH_ZOOM_ROTATE||(this._rotateInternal(e,r),this._isUserControllingRotate=!0),(this._state&be.DOLLY)===be.DOLLY||(this._state&be.ZOOM)===be.ZOOM){const t=this.dollyToCursor?(i.x-this._elementRect.x)/this._elementRect.width*2-1:0,e=this.dollyToCursor?(i.y-this._elementRect.y)/this._elementRect.height*-2+1:0,s=this.dollyDragInverted?-1:1;(this._state&be.DOLLY)===be.DOLLY?(this._dollyInternal(s*r*je,t,e),this._isUserControllingDolly=!0):(this._zoomInternal(s*r*je,t,e),this._isUserControllingZoom=!0)}if((this._state&be.TOUCH_DOLLY)===be.TOUCH_DOLLY||(this._state&be.TOUCH_ZOOM)===be.TOUCH_ZOOM||(this._state&be.TOUCH_DOLLY_TRUCK)===be.TOUCH_DOLLY_TRUCK||(this._state&be.TOUCH_ZOOM_TRUCK)===be.TOUCH_ZOOM_TRUCK||(this._state&be.TOUCH_DOLLY_SCREEN_PAN)===be.TOUCH_DOLLY_SCREEN_PAN||(this._state&be.TOUCH_ZOOM_SCREEN_PAN)===be.TOUCH_ZOOM_SCREEN_PAN||(this._state&be.TOUCH_DOLLY_OFFSET)===be.TOUCH_DOLLY_OFFSET||(this._state&be.TOUCH_ZOOM_OFFSET)===be.TOUCH_ZOOM_OFFSET||(this._state&be.TOUCH_DOLLY_ROTATE)===be.TOUCH_DOLLY_ROTATE||(this._state&be.TOUCH_ZOOM_ROTATE)===be.TOUCH_ZOOM_ROTATE){const t=Ge.x-this._activePointers[1].clientX,e=Ge.y-this._activePointers[1].clientY,i=Math.sqrt(t*t+e*e),r=n.y-i;n.set(0,i);const o=this.dollyToCursor?(s.x-this._elementRect.x)/this._elementRect.width*2-1:0,a=this.dollyToCursor?(s.y-this._elementRect.y)/this._elementRect.height*-2+1:0;(this._state&be.TOUCH_DOLLY)===be.TOUCH_DOLLY||(this._state&be.TOUCH_DOLLY_ROTATE)===be.TOUCH_DOLLY_ROTATE||(this._state&be.TOUCH_DOLLY_TRUCK)===be.TOUCH_DOLLY_TRUCK||(this._state&be.TOUCH_DOLLY_SCREEN_PAN)===be.TOUCH_DOLLY_SCREEN_PAN||(this._state&be.TOUCH_DOLLY_OFFSET)===be.TOUCH_DOLLY_OFFSET?(this._dollyInternal(r*je,o,a),this._isUserControllingDolly=!0):(this._zoomInternal(r*je,o,a),this._isUserControllingZoom=!0)}(this._state&be.TRUCK)!==be.TRUCK&&(this._state&be.TOUCH_TRUCK)!==be.TOUCH_TRUCK&&(this._state&be.TOUCH_DOLLY_TRUCK)!==be.TOUCH_DOLLY_TRUCK&&(this._state&be.TOUCH_ZOOM_TRUCK)!==be.TOUCH_ZOOM_TRUCK||(this._truckInternal(e,r,!1,!1),this._isUserControllingTruck=!0),(this._state&be.SCREEN_PAN)!==be.SCREEN_PAN&&(this._state&be.TOUCH_SCREEN_PAN)!==be.TOUCH_SCREEN_PAN&&(this._state&be.TOUCH_DOLLY_SCREEN_PAN)!==be.TOUCH_DOLLY_SCREEN_PAN&&(this._state&be.TOUCH_ZOOM_SCREEN_PAN)!==be.TOUCH_ZOOM_SCREEN_PAN||(this._truckInternal(e,r,!1,!0),this._isUserControllingTruck=!0),(this._state&be.OFFSET)!==be.OFFSET&&(this._state&be.TOUCH_OFFSET)!==be.TOUCH_OFFSET&&(this._state&be.TOUCH_DOLLY_OFFSET)!==be.TOUCH_DOLLY_OFFSET&&(this._state&be.TOUCH_ZOOM_OFFSET)!==be.TOUCH_ZOOM_OFFSET||(this._truckInternal(e,r,!0,!1),this._isUserControllingOffset=!0),this.dispatchEvent({type:"control"})},p=()=>{Le(this._activePointers,Ge),s.copy(Ge),this._dragNeedsUpdate=!1,(0===this._activePointers.length||1===this._activePointers.length&&this._activePointers[0]===this._lockedPointer)&&(this._isDragging=!1),0===this._activePointers.length&&this._domElement&&(this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this.dispatchEvent({type:"controlend"}))};this.lockPointer=()=>{this._enabled&&this._domElement&&(this.cancel(),this._lockedPointer={pointerId:-1,clientX:0,clientY:0,deltaX:0,deltaY:0,mouseButton:null},this._activePointers.push(this._lockedPointer),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.requestPointerLock(),this._domElement.ownerDocument.addEventListener("pointerlockchange",f),this._domElement.ownerDocument.addEventListener("pointerlockerror",m),this._domElement.ownerDocument.addEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.addEventListener("pointerup",a),d())},this.unlockPointer=()=>{var t,e,i;null!==this._lockedPointer&&(this._disposePointer(this._lockedPointer),this._lockedPointer=null),null===(t=this._domElement)||void 0===t||t.ownerDocument.exitPointerLock(),null===(e=this._domElement)||void 0===e||e.ownerDocument.removeEventListener("pointerlockchange",f),null===(i=this._domElement)||void 0===i||i.ownerDocument.removeEventListener("pointerlockerror",m),this.cancel()};const f=()=>{this._domElement&&this._domElement.ownerDocument.pointerLockElement===this._domElement||this.unlockPointer()},m=()=>{this.unlockPointer()};this._addAllEventListeners=t=>{this._domElement=t,this._domElement.style.touchAction="none",this._domElement.style.userSelect="none",this._domElement.style.webkitUserSelect="none",this._domElement.addEventListener("pointerdown",r),this._domElement.addEventListener("pointercancel",a),this._domElement.addEventListener("wheel",h,{passive:!1}),this._domElement.addEventListener("contextmenu",c)},this._removeAllEventListeners=()=>{this._domElement&&(this._domElement.style.touchAction="",this._domElement.style.userSelect="",this._domElement.style.webkitUserSelect="",this._domElement.removeEventListener("pointerdown",r),this._domElement.removeEventListener("pointercancel",a),this._domElement.removeEventListener("wheel",h,{passive:!1}),this._domElement.removeEventListener("contextmenu",c),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.ownerDocument.removeEventListener("pointerlockchange",f),this._domElement.ownerDocument.removeEventListener("pointerlockerror",m))},this.cancel=()=>{this._state!==be.NONE&&(this._state=be.NONE,this._activePointers.length=0,p())},e&&this.connect(e),this.update(0)}get camera(){return this._camera}set camera(t){this._camera=t,this.updateCameraUp(),this._camera.updateProjectionMatrix(),this._updateNearPlaneCorners(),this._needsUpdate=!0}get enabled(){return this._enabled}set enabled(t){this._enabled=t,this._domElement&&(t?(this._domElement.style.touchAction="none",this._domElement.style.userSelect="none",this._domElement.style.webkitUserSelect="none"):(this.cancel(),this._domElement.style.touchAction="",this._domElement.style.userSelect="",this._domElement.style.webkitUserSelect=""))}get active(){return!this._hasRested}get currentAction(){return this._state}get distance(){return this._spherical.radius}set distance(t){this._spherical.radius===t&&this._sphericalEnd.radius===t||(this._spherical.radius=t,this._sphericalEnd.radius=t,this._needsUpdate=!0)}get azimuthAngle(){return this._spherical.theta}set azimuthAngle(t){this._spherical.theta===t&&this._sphericalEnd.theta===t||(this._spherical.theta=t,this._sphericalEnd.theta=t,this._needsUpdate=!0)}get polarAngle(){return this._spherical.phi}set polarAngle(t){this._spherical.phi===t&&this._sphericalEnd.phi===t||(this._spherical.phi=t,this._sphericalEnd.phi=t,this._needsUpdate=!0)}get boundaryEnclosesCamera(){return this._boundaryEnclosesCamera}set boundaryEnclosesCamera(t){this._boundaryEnclosesCamera=t,this._needsUpdate=!0}set interactiveArea(t){this._interactiveArea.width=Ie(t.width,0,1),this._interactiveArea.height=Ie(t.height,0,1),this._interactiveArea.x=Ie(t.x,0,1-this._interactiveArea.width),this._interactiveArea.y=Ie(t.y,0,1-this._interactiveArea.height)}addEventListener(t,e){super.addEventListener(t,e)}removeEventListener(t,e){super.removeEventListener(t,e)}rotate(t,e,i=!1){return this.rotateTo(this._sphericalEnd.theta+t,this._sphericalEnd.phi+e,i)}rotateAzimuthTo(t,e=!1){return this.rotateTo(t,this._sphericalEnd.phi,e)}rotatePolarTo(t,e=!1){return this.rotateTo(this._sphericalEnd.theta,t,e)}rotateTo(t,e,i=!1){this._isUserControllingRotate=!1;const s=Ie(t,this.minAzimuthAngle,this.maxAzimuthAngle),n=Ie(e,this.minPolarAngle,this.maxPolarAngle);this._sphericalEnd.theta=s,this._sphericalEnd.phi=n,this._sphericalEnd.makeSafe(),this._needsUpdate=!0,i||(this._spherical.theta=this._sphericalEnd.theta,this._spherical.phi=this._sphericalEnd.phi);const r=!i||ke(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ke(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold);return this._createOnRestPromise(r)}dolly(t,e=!1){return this.dollyTo(this._sphericalEnd.radius-t,e)}dollyTo(t,e=!1){return this._isUserControllingDolly=!1,this._lastDollyDirection=xe,this._changedDolly=0,this._dollyToNoClamp(Ie(t,this.minDistance,this.maxDistance),e)}_dollyToNoClamp(t,e=!1){const i=this._sphericalEnd.radius;if(this.colliderMeshes.length>=1){const e=this._collisionTest(),s=ke(e,this._spherical.radius);if(!(i>t)&&s)return Promise.resolve();this._sphericalEnd.radius=Math.min(t,e)}else this._sphericalEnd.radius=t;this._needsUpdate=!0,e||(this._spherical.radius=this._sphericalEnd.radius);const s=!e||ke(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(s)}dollyInFixed(t,e=!1){this._targetEnd.add(this._getCameraDirection(Ke).multiplyScalar(t)),e||this._target.copy(this._targetEnd);const i=!e||ke(this._target.x,this._targetEnd.x,this.restThreshold)&&ke(this._target.y,this._targetEnd.y,this.restThreshold)&&ke(this._target.z,this._targetEnd.z,this.restThreshold);return this._createOnRestPromise(i)}zoom(t,e=!1){return this.zoomTo(this._zoomEnd+t,e)}zoomTo(t,e=!1){this._isUserControllingZoom=!1,this._zoomEnd=Ie(t,this.minZoom,this.maxZoom),this._needsUpdate=!0,e||(this._zoom=this._zoomEnd);const i=!e||ke(this._zoom,this._zoomEnd,this.restThreshold);return this._changedZoom=0,this._createOnRestPromise(i)}pan(t,e,i=!1){return console.warn("`pan` has been renamed to `truck`"),this.truck(t,e,i)}truck(t,e,i=!1){this._camera.updateMatrix(),Je.setFromMatrixColumn(this._camera.matrix,0),ti.setFromMatrixColumn(this._camera.matrix,1),Je.multiplyScalar(t),ti.multiplyScalar(-e);const s=qe.copy(Je).add(ti),n=Xe.copy(this._targetEnd).add(s);return this.moveTo(n.x,n.y,n.z,i)}forward(t,e=!1){qe.setFromMatrixColumn(this._camera.matrix,0),qe.crossVectors(this._camera.up,qe),qe.multiplyScalar(t);const i=Xe.copy(this._targetEnd).add(qe);return this.moveTo(i.x,i.y,i.z,e)}elevate(t,e=!1){return qe.copy(this._camera.up).multiplyScalar(t),this.moveTo(this._targetEnd.x+qe.x,this._targetEnd.y+qe.y,this._targetEnd.z+qe.z,e)}moveTo(t,e,i,s=!1){this._isUserControllingTruck=!1;const n=qe.set(t,e,i).sub(this._targetEnd);this._encloseToBoundary(this._targetEnd,n,this.boundaryFriction),this._needsUpdate=!0,s||this._target.copy(this._targetEnd);const r=!s||ke(this._target.x,this._targetEnd.x,this.restThreshold)&&ke(this._target.y,this._targetEnd.y,this.restThreshold)&&ke(this._target.z,this._targetEnd.z,this.restThreshold);return this._createOnRestPromise(r)}lookInDirectionOf(t,e,i,s=!1){const n=qe.set(t,e,i).sub(this._targetEnd).normalize().multiplyScalar(-this._sphericalEnd.radius).add(this._targetEnd);return this.setPosition(n.x,n.y,n.z,s)}fitToBox(t,e,{cover:i=!1,paddingLeft:s=0,paddingRight:n=0,paddingBottom:r=0,paddingTop:o=0}={}){const a=[],l=t.isBox3?oi.copy(t):oi.setFromObject(t);l.isEmpty()&&(console.warn("camera-controls: fitTo() cannot be used with an empty box. Aborting"),Promise.resolve());const h=Me(this._sphericalEnd.theta,Oe),c=Me(this._sphericalEnd.phi,Oe);a.push(this.rotateTo(h,c,e));const d=qe.setFromSpherical(this._sphericalEnd).normalize(),u=hi.setFromUnitVectors(d,Ye),p=ke(Math.abs(d.y),1);p&&u.multiply(ci.setFromAxisAngle(We,h)),u.multiply(this._yAxisUpSpaceInverse);const f=ai.makeEmpty();Xe.copy(l.min).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.min).setX(l.max.x).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.min).setY(l.max.y).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.max).setZ(l.min.z).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.min).setZ(l.max.z).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.max).setY(l.min.y).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.max).setX(l.min.x).applyQuaternion(u),f.expandByPoint(Xe),Xe.copy(l.max).applyQuaternion(u),f.expandByPoint(Xe),f.min.x-=s,f.min.y-=r,f.max.x+=n,f.max.y+=o,u.setFromUnitVectors(Ye,d),p&&u.premultiply(ci.invert()),u.premultiply(this._yAxisUpSpace);const m=f.getSize(qe),g=f.getCenter(Xe).applyQuaternion(u);if(Ce(this._camera)){const t=this.getDistanceToFitBox(m.x,m.y,m.z,i);a.push(this.moveTo(g.x,g.y,g.z,e)),a.push(this.dollyTo(t,e)),a.push(this.setFocalOffset(0,0,0,e))}else if(Se(this._camera)){const t=this._camera,s=t.right-t.left,n=t.top-t.bottom,r=i?Math.max(s/m.x,n/m.y):Math.min(s/m.x,n/m.y);a.push(this.moveTo(g.x,g.y,g.z,e)),a.push(this.zoomTo(r,e)),a.push(this.setFocalOffset(0,0,0,e))}return Promise.all(a)}fitToSphere(t,e){const i=[],s="isObject3D"in t?pi.createBoundingSphere(t,li):li.copy(t);if(i.push(this.moveTo(s.center.x,s.center.y,s.center.z,e)),Ce(this._camera)){const t=this.getDistanceToFitSphere(s.radius);i.push(this.dollyTo(t,e))}else if(Se(this._camera)){const t=this._camera.right-this._camera.left,n=this._camera.top-this._camera.bottom,r=2*s.radius,o=Math.min(t/r,n/r);i.push(this.zoomTo(o,e))}return i.push(this.setFocalOffset(0,0,0,e)),Promise.all(i)}setLookAt(t,e,i,s,n,r,o=!1){this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._lastDollyDirection=xe,this._changedDolly=0;const a=Xe.set(s,n,r),l=qe.set(t,e,i);this._targetEnd.copy(a),this._sphericalEnd.setFromVector3(l.sub(a).applyQuaternion(this._yAxisUpSpace)),this.normalizeRotations(),this._needsUpdate=!0,o||(this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd));const h=!o||ke(this._target.x,this._targetEnd.x,this.restThreshold)&&ke(this._target.y,this._targetEnd.y,this.restThreshold)&&ke(this._target.z,this._targetEnd.z,this.restThreshold)&&ke(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ke(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&ke(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(h)}lerpLookAt(t,e,i,s,n,r,o,a,l,h,c,d,u,p=!1){this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._lastDollyDirection=xe,this._changedDolly=0;const f=qe.set(s,n,r),m=Xe.set(t,e,i);ni.setFromVector3(m.sub(f).applyQuaternion(this._yAxisUpSpace));const g=Qe.set(h,c,d),_=Xe.set(o,a,l);ri.setFromVector3(_.sub(g).applyQuaternion(this._yAxisUpSpace)),this._targetEnd.copy(f.lerp(g,u));const y=ri.theta-ni.theta,w=ri.phi-ni.phi,v=ri.radius-ni.radius;this._sphericalEnd.set(ni.radius+v*u,ni.phi+w*u,ni.theta+y*u),this.normalizeRotations(),this._needsUpdate=!0,p||(this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd));const b=!p||ke(this._target.x,this._targetEnd.x,this.restThreshold)&&ke(this._target.y,this._targetEnd.y,this.restThreshold)&&ke(this._target.z,this._targetEnd.z,this.restThreshold)&&ke(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ke(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&ke(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(b)}setPosition(t,e,i,s=!1){return this.setLookAt(t,e,i,this._targetEnd.x,this._targetEnd.y,this._targetEnd.z,s)}setTarget(t,e,i,s=!1){const n=this.getPosition(qe),r=this.setLookAt(n.x,n.y,n.z,t,e,i,s);return this._sphericalEnd.phi=Ie(this._sphericalEnd.phi,this.minPolarAngle,this.maxPolarAngle),r}setFocalOffset(t,e,i,s=!1){this._isUserControllingOffset=!1,this._focalOffsetEnd.set(t,e,i),this._needsUpdate=!0,s||this._focalOffset.copy(this._focalOffsetEnd);const n=!s||ke(this._focalOffset.x,this._focalOffsetEnd.x,this.restThreshold)&&ke(this._focalOffset.y,this._focalOffsetEnd.y,this.restThreshold)&&ke(this._focalOffset.z,this._focalOffsetEnd.z,this.restThreshold);return this._createOnRestPromise(n)}setOrbitPoint(t,e,i){this._camera.updateMatrixWorld(),Je.setFromMatrixColumn(this._camera.matrixWorldInverse,0),ti.setFromMatrixColumn(this._camera.matrixWorldInverse,1),ei.setFromMatrixColumn(this._camera.matrixWorldInverse,2);const s=qe.set(t,e,i),n=s.distanceTo(this._camera.position),r=s.sub(this._camera.position);Je.multiplyScalar(r.x),ti.multiplyScalar(r.y),ei.multiplyScalar(r.z),qe.copy(Je).add(ti).add(ei),qe.z=qe.z+n,this.dollyTo(n,!1),this.setFocalOffset(-qe.x,qe.y,-qe.z,!1),this.moveTo(t,e,i,!1)}setBoundary(t){if(!t)return this._boundary.min.set(-1/0,-1/0,-1/0),this._boundary.max.set(1/0,1/0,1/0),void(this._needsUpdate=!0);this._boundary.copy(t),this._boundary.clampPoint(this._targetEnd,this._targetEnd),this._needsUpdate=!0}setViewport(t,e,i,s){null!==t?(this._viewport=this._viewport||new Ze.Vector4,"number"==typeof t?this._viewport.set(t,e,i,s):this._viewport.copy(t)):this._viewport=null}getDistanceToFitBox(t,e,i,s=!1){if(Re(this._camera,"getDistanceToFitBox"))return this._spherical.radius;const n=t/e,r=this._camera.getEffectiveFOV()*Pe,o=this._camera.aspect;return.5*((s?n>o:n<o)?e:t/o)/Math.tan(.5*r)+.5*i}getDistanceToFitSphere(t){if(Re(this._camera,"getDistanceToFitSphere"))return this._spherical.radius;const e=this._camera.getEffectiveFOV()*Pe,i=2*Math.atan(Math.tan(.5*e)*this._camera.aspect),s=1<this._camera.aspect?e:i;return t/Math.sin(.5*s)}getTarget(t,e=!0){return(t&&t.isVector3?t:new Ze.Vector3).copy(e?this._targetEnd:this._target)}getPosition(t,e=!0){return(t&&t.isVector3?t:new Ze.Vector3).setFromSpherical(e?this._sphericalEnd:this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(e?this._targetEnd:this._target)}getSpherical(t,e=!0){return(t||new Ze.Spherical).copy(e?this._sphericalEnd:this._spherical)}getFocalOffset(t,e=!0){return(t&&t.isVector3?t:new Ze.Vector3).copy(e?this._focalOffsetEnd:this._focalOffset)}normalizeRotations(){this._sphericalEnd.theta=this._sphericalEnd.theta%Ae,this._sphericalEnd.theta<0&&(this._sphericalEnd.theta+=Ae),this._spherical.theta+=Ae*Math.round((this._sphericalEnd.theta-this._spherical.theta)/Ae)}stop(){this._focalOffset.copy(this._focalOffsetEnd),this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd),this._zoom=this._zoomEnd}reset(t=!1){if(!ke(this._camera.up.x,this._cameraUp0.x)||!ke(this._camera.up.y,this._cameraUp0.y)||!ke(this._camera.up.z,this._cameraUp0.z)){this._camera.up.copy(this._cameraUp0);const t=this.getPosition(qe);this.updateCameraUp(),this.setPosition(t.x,t.y,t.z)}const e=[this.setLookAt(this._position0.x,this._position0.y,this._position0.z,this._target0.x,this._target0.y,this._target0.z,t),this.setFocalOffset(this._focalOffset0.x,this._focalOffset0.y,this._focalOffset0.z,t),this.zoomTo(this._zoom0,t)];return Promise.all(e)}saveState(){this._cameraUp0.copy(this._camera.up),this.getTarget(this._target0),this.getPosition(this._position0),this._zoom0=this._zoom,this._focalOffset0.copy(this._focalOffset)}updateCameraUp(){this._yAxisUpSpace.setFromUnitVectors(this._camera.up,We),this._yAxisUpSpaceInverse.copy(this._yAxisUpSpace).invert()}applyCameraUp(){const t=qe.subVectors(this._target,this._camera.position).normalize(),e=Xe.crossVectors(t,this._camera.up);this._camera.up.crossVectors(e,t).normalize(),this._camera.updateMatrixWorld();const i=this.getPosition(qe);this.updateCameraUp(),this.setPosition(i.x,i.y,i.z)}update(t){const e=this._sphericalEnd.theta-this._spherical.theta,i=this._sphericalEnd.phi-this._spherical.phi,s=this._sphericalEnd.radius-this._spherical.radius,n=ii.subVectors(this._targetEnd,this._target),r=si.subVectors(this._focalOffsetEnd,this._focalOffset),o=this._zoomEnd-this._zoom;if(De(e))this._thetaVelocity.value=0,this._spherical.theta=this._sphericalEnd.theta;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.theta=Ne(this._spherical.theta,this._sphericalEnd.theta,this._thetaVelocity,e,1/0,t),this._needsUpdate=!0}if(De(i))this._phiVelocity.value=0,this._spherical.phi=this._sphericalEnd.phi;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.phi=Ne(this._spherical.phi,this._sphericalEnd.phi,this._phiVelocity,e,1/0,t),this._needsUpdate=!0}if(De(s))this._radiusVelocity.value=0,this._spherical.radius=this._sphericalEnd.radius;else{const e=this._isUserControllingDolly?this.draggingSmoothTime:this.smoothTime;this._spherical.radius=Ne(this._spherical.radius,this._sphericalEnd.radius,this._radiusVelocity,e,this.maxSpeed,t),this._needsUpdate=!0}if(De(n.x)&&De(n.y)&&De(n.z))this._targetVelocity.set(0,0,0),this._target.copy(this._targetEnd);else{const e=this._isUserControllingTruck?this.draggingSmoothTime:this.smoothTime;Ue(this._target,this._targetEnd,this._targetVelocity,e,this.maxSpeed,t,this._target),this._needsUpdate=!0}if(De(r.x)&&De(r.y)&&De(r.z))this._focalOffsetVelocity.set(0,0,0),this._focalOffset.copy(this._focalOffsetEnd);else{const e=this._isUserControllingOffset?this.draggingSmoothTime:this.smoothTime;Ue(this._focalOffset,this._focalOffsetEnd,this._focalOffsetVelocity,e,this.maxSpeed,t,this._focalOffset),this._needsUpdate=!0}if(De(o))this._zoomVelocity.value=0,this._zoom=this._zoomEnd;else{const e=this._isUserControllingZoom?this.draggingSmoothTime:this.smoothTime;this._zoom=Ne(this._zoom,this._zoomEnd,this._zoomVelocity,e,1/0,t)}if(this.dollyToCursor)if(Ce(this._camera)&&0!==this._changedDolly){const t=this._spherical.radius-this._lastDistance,e=this._camera,i=this._getCameraDirection(Ke),s=qe.copy(i).cross(e.up).normalize();0===s.lengthSq()&&(s.x=1);const n=Xe.crossVectors(s,i),r=this._sphericalEnd.radius*Math.tan(e.getEffectiveFOV()*Pe*.5),o=(this._sphericalEnd.radius-t-this._sphericalEnd.radius)/this._sphericalEnd.radius,a=Qe.copy(this._targetEnd).add(s.multiplyScalar(this._dollyControlCoord.x*r*e.aspect)).add(n.multiplyScalar(this._dollyControlCoord.y*r)),l=qe.copy(this._targetEnd).lerp(a,o),h=this._lastDollyDirection===Ee&&this._spherical.radius<=this.minDistance,c=this._lastDollyDirection===Te&&this.maxDistance<=this._spherical.radius;if(this.infinityDolly&&(h||c)){this._sphericalEnd.radius-=t,this._spherical.radius-=t;const e=Xe.copy(i).multiplyScalar(-t);l.add(e)}this._boundary.clampPoint(l,l);const d=Xe.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedDolly-=t,De(this._changedDolly)&&(this._changedDolly=0)}else if(Se(this._camera)&&0!==this._changedZoom){const t=this._zoom-this._lastZoom,e=this._camera,i=qe.set(this._dollyControlCoord.x,this._dollyControlCoord.y,(e.near+e.far)/(e.near-e.far)).unproject(e),s=Xe.set(0,0,-1).applyQuaternion(e.quaternion),n=Qe.copy(i).add(s.multiplyScalar(-i.dot(e.up))),r=-(this._zoom-t-this._zoom)/this._zoom,o=this._getCameraDirection(Ke),a=this._targetEnd.dot(o),l=qe.copy(this._targetEnd).lerp(n,r),h=l.dot(o),c=o.multiplyScalar(h-a);l.sub(c),this._boundary.clampPoint(l,l);const d=Xe.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedZoom-=t,De(this._changedZoom)&&(this._changedZoom=0)}this._camera.zoom!==this._zoom&&(this._camera.zoom=this._zoom,this._camera.updateProjectionMatrix(),this._updateNearPlaneCorners(),this._needsUpdate=!0),this._dragNeedsUpdate=!0;const a=this._collisionTest();this._spherical.radius=Math.min(this._spherical.radius,a),this._spherical.makeSafe(),this._camera.position.setFromSpherical(this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(this._target),this._camera.lookAt(this._target);(!De(this._focalOffset.x)||!De(this._focalOffset.y)||!De(this._focalOffset.z))&&(Je.setFromMatrixColumn(this._camera.matrix,0),ti.setFromMatrixColumn(this._camera.matrix,1),ei.setFromMatrixColumn(this._camera.matrix,2),Je.multiplyScalar(this._focalOffset.x),ti.multiplyScalar(-this._focalOffset.y),ei.multiplyScalar(this._focalOffset.z),qe.copy(Je).add(ti).add(ei),this._camera.position.add(qe),this._camera.updateMatrixWorld()),this._boundaryEnclosesCamera&&this._encloseToBoundary(this._camera.position.copy(this._target),qe.setFromSpherical(this._spherical).applyQuaternion(this._yAxisUpSpaceInverse),1);const l=this._needsUpdate;return l&&!this._updatedLastTime?(this._hasRested=!1,this.dispatchEvent({type:"wake"}),this.dispatchEvent({type:"update"})):l?(this.dispatchEvent({type:"update"}),De(e,this.restThreshold)&&De(i,this.restThreshold)&&De(s,this.restThreshold)&&De(n.x,this.restThreshold)&&De(n.y,this.restThreshold)&&De(n.z,this.restThreshold)&&De(r.x,this.restThreshold)&&De(r.y,this.restThreshold)&&De(r.z,this.restThreshold)&&De(o,this.restThreshold)&&!this._hasRested&&(this._hasRested=!0,this.dispatchEvent({type:"rest"}))):!l&&this._updatedLastTime&&this.dispatchEvent({type:"sleep"}),this._lastDistance=this._spherical.radius,this._lastZoom=this._zoom,this._updatedLastTime=l,this._needsUpdate=!1,l}toJSON(){return JSON.stringify({enabled:this._enabled,minDistance:this.minDistance,maxDistance:ze(this.maxDistance),minZoom:this.minZoom,maxZoom:ze(this.maxZoom),minPolarAngle:this.minPolarAngle,maxPolarAngle:ze(this.maxPolarAngle),minAzimuthAngle:ze(this.minAzimuthAngle),maxAzimuthAngle:ze(this.maxAzimuthAngle),smoothTime:this.smoothTime,draggingSmoothTime:this.draggingSmoothTime,dollySpeed:this.dollySpeed,truckSpeed:this.truckSpeed,dollyToCursor:this.dollyToCursor,target:this._targetEnd.toArray(),position:qe.setFromSpherical(this._sphericalEnd).add(this._targetEnd).toArray(),zoom:this._zoomEnd,focalOffset:this._focalOffsetEnd.toArray(),target0:this._target0.toArray(),position0:this._position0.toArray(),zoom0:this._zoom0,focalOffset0:this._focalOffset0.toArray()})}fromJSON(t,e=!1){const i=JSON.parse(t);this.enabled=i.enabled,this.minDistance=i.minDistance,this.maxDistance=Be(i.maxDistance),this.minZoom=i.minZoom,this.maxZoom=Be(i.maxZoom),this.minPolarAngle=i.minPolarAngle,this.maxPolarAngle=Be(i.maxPolarAngle),this.minAzimuthAngle=Be(i.minAzimuthAngle),this.maxAzimuthAngle=Be(i.maxAzimuthAngle),this.smoothTime=i.smoothTime,this.draggingSmoothTime=i.draggingSmoothTime,this.dollySpeed=i.dollySpeed,this.truckSpeed=i.truckSpeed,this.dollyToCursor=i.dollyToCursor,this._target0.fromArray(i.target0),this._position0.fromArray(i.position0),this._zoom0=i.zoom0,this._focalOffset0.fromArray(i.focalOffset0),this.moveTo(i.target[0],i.target[1],i.target[2],e),ni.setFromVector3(qe.fromArray(i.position).sub(this._targetEnd).applyQuaternion(this._yAxisUpSpace)),this.rotateTo(ni.theta,ni.phi,e),this.dollyTo(ni.radius,e),this.zoomTo(i.zoom,e),this.setFocalOffset(i.focalOffset[0],i.focalOffset[1],i.focalOffset[2],e),this._needsUpdate=!0}connect(t){this._domElement?console.warn("camera-controls is already connected."):(t.setAttribute("data-camera-controls-version","2.10.1"),this._addAllEventListeners(t),this._getClientRect(this._elementRect))}disconnect(){this.cancel(),this._removeAllEventListeners(),this._domElement&&(this._domElement.removeAttribute("data-camera-controls-version"),this._domElement=void 0)}dispose(){this.removeAllEventListeners(),this.disconnect()}_getTargetDirection(t){return t.setFromSpherical(this._spherical).divideScalar(this._spherical.radius).applyQuaternion(this._yAxisUpSpaceInverse)}_getCameraDirection(t){return this._getTargetDirection(t).negate()}_findPointerById(t){return this._activePointers.find(e=>e.pointerId===t)}_findPointerByMouseButton(t){return this._activePointers.find(e=>e.mouseButton===t)}_disposePointer(t){this._activePointers.splice(this._activePointers.indexOf(t),1)}_encloseToBoundary(t,e,i){const s=e.lengthSq();if(0===s)return t;const n=Xe.copy(e).add(t),r=this._boundary.clampPoint(n,Qe).sub(n),o=r.lengthSq();if(0===o)return t.add(e);if(o===s)return t;if(0===i)return t.add(e).add(r);{const s=1+i*o/e.dot(r);return t.add(Xe.copy(e).multiplyScalar(s)).add(r.multiplyScalar(1-i))}}_updateNearPlaneCorners(){if(Ce(this._camera)){const t=this._camera,e=t.near,i=t.getEffectiveFOV()*Pe,s=Math.tan(.5*i)*e,n=s*t.aspect;this._nearPlaneCorners[0].set(-n,-s,0),this._nearPlaneCorners[1].set(n,-s,0),this._nearPlaneCorners[2].set(n,s,0),this._nearPlaneCorners[3].set(-n,s,0)}else if(Se(this._camera)){const t=this._camera,e=1/t.zoom,i=t.left*e,s=t.right*e,n=t.top*e,r=t.bottom*e;this._nearPlaneCorners[0].set(i,n,0),this._nearPlaneCorners[1].set(s,n,0),this._nearPlaneCorners[2].set(s,r,0),this._nearPlaneCorners[3].set(i,r,0)}}_collisionTest(){let t=1/0;if(!(this.colliderMeshes.length>=1))return t;if(Re(this._camera,"_collisionTest"))return t;const e=this._getTargetDirection(Ke);di.lookAt($e,e,this._camera.up);for(let i=0;i<4;i++){const s=Xe.copy(this._nearPlaneCorners[i]);s.applyMatrix4(di);const n=Qe.addVectors(this._target,s);ui.set(n,e),ui.far=this._spherical.radius+1;const r=ui.intersectObjects(this.colliderMeshes);0!==r.length&&r[0].distance<t&&(t=r[0].distance)}return t}_getClientRect(t){if(!this._domElement)return;const e=this._domElement.getBoundingClientRect();return t.x=e.left,t.y=e.top,this._viewport?(t.x+=this._viewport.x,t.y+=e.height-this._viewport.w-this._viewport.y,t.width=this._viewport.z,t.height=this._viewport.w):(t.width=e.width,t.height=e.height),t}_createOnRestPromise(t){return t?Promise.resolve():(this._hasRested=!1,this.dispatchEvent({type:"transitionstart"}),new Promise(t=>{const e=()=>{this.removeEventListener("rest",e),t()};this.addEventListener("rest",e)}))}_addAllEventListeners(t){}_removeAllEventListeners(){}get dampingFactor(){return console.warn(".dampingFactor has been deprecated. use smoothTime (in seconds) instead."),0}set dampingFactor(t){console.warn(".dampingFactor has been deprecated. use smoothTime (in seconds) instead.")}get draggingDampingFactor(){return console.warn(".draggingDampingFactor has been deprecated. use draggingSmoothTime (in seconds) instead."),0}set draggingDampingFactor(t){console.warn(".draggingDampingFactor has been deprecated. use draggingSmoothTime (in seconds) instead.")}static createBoundingSphere(t,e=new Ze.Sphere){const i=e,s=i.center;oi.makeEmpty(),t.traverseVisible(t=>{t.isMesh&&oi.expandByObject(t)}),oi.getCenter(s);let n=0;return t.traverseVisible(t=>{if(!t.isMesh)return;const e=t;if(!e.geometry)return;const i=e.geometry.clone();i.applyMatrix4(e.matrixWorld);const r=i.attributes.position;for(let t=0,e=r.count;t<e;t++)qe.fromBufferAttribute(r,t),n=Math.max(n,s.distanceToSquared(qe))}),i.radius=Math.sqrt(n),i}}class fi extends p{constructor(t){super(t),e(this,"onBeforeUpdate",new h),e(this,"onAfterUpdate",new h),e(this,"onAspectUpdated",new h),e(this,"onDisposed",new h),e(this,"three"),e(this,"_allControls",new Map),e(this,"updateAspect",()=>{var t;if(this.currentWorld&&this.currentWorld.renderer)if(this.three instanceof a.OrthographicCamera)this.onAspectUpdated.trigger();else if(null==(t=this.currentWorld.renderer)?void 0:t.isResizeable()){const t=this.currentWorld.renderer.getSize();this.three.aspect=t.width/t.height,this.three.updateProjectionMatrix(),this.onAspectUpdated.trigger()}}),this.three=this.setupCamera(),this.setupEvents(!0),this.worlds.onItemSet.add(({value:t})=>{const e=this.newCameraControls();this._allControls.set(t.uuid,e)}),this.worlds.onBeforeDelete.add(({value:t})=>{const e=this._allControls.get(t.uuid);e&&(e.dispose(),this._allControls.delete(t.uuid))})}get controls(){if(!this.currentWorld)throw new Error("This camera needs a world to work!");const t=this._allControls.get(this.currentWorld.uuid);if(!t)throw new Error("Controls not found!");return t}get enabled(){return null!==this.currentWorld&&this.controls.enabled}set enabled(t){null!==this.currentWorld&&(this.controls.enabled=t)}set currentWorld(t){if(super.currentWorld=t,!t)return;this.worlds.get(t.uuid)||this.worlds.set(t.uuid,t)}get currentWorld(){return this._currentWorld}dispose(){this.setupEvents(!1),this.onAspectUpdated.reset(),this.onBeforeUpdate.reset(),this.onAfterUpdate.reset(),this.three.removeFromParent(),this.onDisposed.trigger(),this.onDisposed.reset();for(const[t,e]of this._allControls)e.dispose();this.worlds.clear()}async fitToItems(t){const e=await this.getItemsBounding(t);await this.controls.fitToSphere(e,!0)}async setOrbitToItems(t){const e=await this.getItemsBounding(t);this.controls.setOrbitPoint(e.center.x,e.center.y,e.center.z)}update(t){this.enabled&&(this.onBeforeUpdate.trigger(this),this.controls.update(t),this.onAfterUpdate.trigger(this))}async getItemsBounding(t){const e=this.components.get(Xt),i=this.components.get(se);i.list.clear();const s=new a.Sphere;if(t)await i.addFromModelIdMap(t);else for(const[,t]of e.list)i.list.add(t.box);return i.get().getBoundingSphere(s),i.list.clear(),s}setupCamera(){const t=window.innerWidth/window.innerHeight,e=new a.PerspectiveCamera(60,t,1,1e3);return e.position.set(50,50,50),e.lookAt(new a.Vector3(0,0,0)),e}newCameraControls(){if(!this.currentWorld)throw new Error("This camera needs a world to work!");if(!this.currentWorld.renderer)throw new Error("This camera needs a renderer to work!");pi.install({THREE:fi.getSubsetOfThree()});const{domElement:t}=this.currentWorld.renderer.three,e=new pi(this.three,t);return e.smoothTime=.2,e.dollyToCursor=!0,e.infinityDolly=!0,e.minDistance=6,e}setupEvents(t){t?window.addEventListener("resize",this.updateAspect):window.removeEventListener("resize",this.updateAspect)}static getSubsetOfThree(){return{MOUSE:a.MOUSE,Vector2:a.Vector2,Vector3:a.Vector3,Vector4:a.Vector4,Quaternion:a.Quaternion,Matrix4:a.Matrix4,Spherical:a.Spherical,Box3:a.Box3,Sphere:a.Sphere,Raycaster:a.Raycaster,MathUtils:a.MathUtils}}}const mi=class t extends d{constructor(i){super(i),e(this,"onAfterUpdate",new h),e(this,"onBeforeUpdate",new h),e(this,"onDisposed",new h),e(this,"list",new Ht),e(this,"enabled",!0),i.add(t.uuid,this)}create(){const t=new ue(this.components),e=t.uuid;if(this.list.has(e))throw new Error("There is already a world with this name!");return this.list.set(e,t),t}delete(t){if(!this.list.has(t.uuid))throw new Error("The provided world is not found in the list!");this.list.delete(t.uuid),t.dispose()}dispose(){this.enabled=!1;for(const[t,e]of this.list)e.dispose();this.list.clear(),this.onDisposed.trigger()}update(t){if(this.enabled)for(const[e,i]of this.list)i.update(t)}};e(mi,"uuid","fdb61dc4-2ec1-4966-b83d-54ea795fad4a");let gi=mi;class _i extends $t{constructor(){super(...arguments),e(this,"_config",{visible:{value:!0,type:"Boolean"},color:{value:new a.Color,type:"Color"},primarySize:{type:"Number",interpolable:!0,value:1,min:0,max:1e3},secondarySize:{type:"Number",interpolable:!0,value:10,min:0,max:1e3},distance:{type:"Number",interpolable:!0,value:500,min:0,max:500}})}get visible(){return this._config.visible.value}set visible(t){this._config.visible.value=t,this._component.visible=t}get color(){return this._config.color.value}set color(t){this._config.color.value=t,this._component.material.uniforms.uColor.value=t,this._component.material.uniformsNeedUpdate=!0}get primarySize(){return this._config.primarySize.value}set primarySize(t){this._config.primarySize.value=t,this._component.material.uniforms.uSize1.value=t,this._component.material.uniformsNeedUpdate=!0}get secondarySize(){return this._config.secondarySize.value}set secondarySize(t){this._config.secondarySize.value=t,this._component.material.uniforms.uSize2.value=t,this._component.material.uniformsNeedUpdate=!0}get distance(){return this._config.distance.value}set distance(t){this._config.distance.value=t,this._component.material.uniforms.uDistance.value=t,this._component.material.uniformsNeedUpdate=!0}}class yi{constructor(t,i){e(this,"onDisposed",new h),e(this,"onSetup",new h),e(this,"isSetup",!1),e(this,"world"),e(this,"components"),e(this,"config"),e(this,"_defaultConfig",{visible:!0,color:new a.Color(12303291),primarySize:1,secondarySize:10,distance:500}),e(this,"three"),e(this,"_fade",3),e(this,"updateZoom",()=>{this.world.camera instanceof fi&&(this.material.uniforms.uZoom.value=this.world.camera.three.zoom)}),this.world=i;const{color:s,primarySize:n,secondarySize:r,distance:o}=this._defaultConfig;this.components=t,this.config=new _i(this,this.components,"Grid");const l=new a.PlaneGeometry(2,2,1,1),c=new a.ShaderMaterial({side:a.DoubleSide,uniforms:{uSize1:{value:n},uSize2:{value:r},uColor:{value:s},uDistance:{value:o},uFade:{value:this._fade},uZoom:{value:1}},transparent:!0,vertexShader:"\n \n varying vec3 worldPosition;\n \n uniform float uDistance;\n \n void main() {\n \n vec3 pos = position.xzy * uDistance;\n pos.xz += cameraPosition.xz;\n \n worldPosition = pos;\n \n gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);\n \n }\n ",fragmentShader:"\n \n varying vec3 worldPosition;\n \n uniform float uZoom;\n uniform float uFade;\n uniform float uSize1;\n uniform float uSize2;\n uniform vec3 uColor;\n uniform float uDistance;\n \n \n \n float getGrid(float size) {\n \n vec2 r = worldPosition.xz / size;\n \n \n vec2 grid = abs(fract(r - 0.5) - 0.5) / fwidth(r);\n float line = min(grid.x, grid.y);\n \n \n return 1.0 - min(line, 1.0);\n }\n \n void main() {\n \n \n float d = 1.0 - min(distance(cameraPosition.xz, worldPosition.xz) / uDistance, 1.0);\n \n float g1 = getGrid(uSize1);\n float g2 = getGrid(uSize2);\n \n // Ortho camera fades the grid away when zooming out\n float minZoom = step(0.2, uZoom);\n float zoomFactor = pow(min(uZoom, 1.), 2.) * minZoom;\n \n gl_FragColor = vec4(uColor.rgb, mix(g2, g1, g1) * pow(d, uFade));\n gl_FragColor.a = mix(0.5 * gl_FragColor.a, gl_FragColor.a, g2) * zoomFactor;\n \n if ( gl_FragColor.a <= 0.0 ) discard;\n \n \n }\n \n ",extensions:{derivatives:!0}});this.three=new a.Mesh(l,c),this.three.frustumCulled=!1,i.scene.three.add(this.three),this.setupEvents(!0)}get visible(){return this.three.visible}set visible(t){if(this.three.visible=t,t){this.world.scene.three.add(this.three)}else this.three.removeFromParent()}get material(){return this.three.material}get fade(){return 3===this._fade}set fade(t){this._fade=t?3:0,this.material.uniforms.uFade.value=this._fade}setup(t){const e={...this._defaultConfig,...t};this.config.visible=!0,this.config.color=e.color,this.config.primarySize=e.primarySize,this.config.secondarySize=e.secondarySize,this.config.distance=e.distance,this.isSetup=!0,this.onSetup.trigger()}dispose(){this.setupEvents(!1);this.components.get(Yt).list.delete(this.config.uuid);this.components.get(g).destroy(this.three),this.onDisposed.trigger(),this.onDisposed.reset(),this.world=null,this.components=null}setupEvents(t){if(this.world.isDisposing)return;if(!(this.world.camera instanceof fi))return;const e=this.world.camera.controls;t?e.addEventListener("update",this.updateZoom):e.removeEventListener("update",this.updateZoom)}}const wi=class t extends d{constructor(i){super(i),e(this,"list",new Map),e(this,"onDisposed",new h),e(this,"enabled",!0),i.add(t.uuid,this)}create(t){if(this.list.has(t.uuid))throw new Error("This world already has a grid!");const e=new yi(this.components,t);return this.list.set(t.uuid,e),t.onDisposed.add(()=>{this.delete(t)}),e}delete(t){const e=this.list.get(t.uuid);e&&e.dispose(),this.list.delete(t.uuid)}dispose(){for(const[t,e]of this.list)e.dispose();this.list.clear(),this.onDisposed.trigger(),this.onDisposed.reset()}};e(wi,"uuid","d1e814d5-b81c-4452-87a2-f039375e0489");let vi=wi;const bi=1.25,xi=65535,Ei=Math.pow(2,-24),Ti=Symbol("SKIP_GENERATION");function Ci(t){return function(t){return t.index?t.index.count:t.attributes.position.count}(t)/3}function Si(t,e){if(!t.index){const i=t.attributes.position.count,n=function(t,e=ArrayBuffer){return t>65535?new Uint32Array(new e(4*t)):new Uint16Array(new e(2*t))}(i,e.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer);t.setIndex(new s.BufferAttribute(n,1));for(let t=0;t<i;t++)n[t]=t}}function Ai(t){const e=Ci(t),i=t.drawRange,s=i.start/3,n=(i.start+i.count)/3,r=Math.max(0,s),o=Math.min(e,n)-r;return[{offset:Math.floor(r),count:Math.floor(o)}]}function Oi(t){if(!t.groups||!t.groups.length)return Ai(t);const e=[],i=new Set,s=t.drawRange,n=s.start/3,r=(s.start+s.count)/3;for(const e of t.groups){const t=e.start/3,s=(e.start+e.count)/3;i.add(Math.max(n,t)),i.add(Math.min(r,s))}const o=Array.from(i.values()).sort((t,e)=>t-e);for(let t=0;t<o.length-1;t++){const i=o[t],s=o[t+1];e.push({offset:Math.floor(i),count:Math.floor(s-i)})}return e}function Pi(t,e,i){return i.min.x=e[t],i.min.y=e[t+1],i.min.z=e[t+2],i.max.x=e[t+3],i.max.y=e[t+4],i.max.z=e[t+5],i}function Ii(t){let e=-1,i=-1/0;for(let s=0;s<3;s++){const n=t[s+3]-t[s];n>i&&(i=n,e=s)}return e}function Di(t,e){e.set(t)}function ki(t,e,i){let s,n;for(let r=0;r<3;r++){const o=r+3;s=t[r],n=e[r],i[r]=s<n?s:n,s=t[o],n=e[o],i[o]=s>n?s:n}}function Mi(t,e,i){for(let s=0;s<3;s++){const n=e[t+2*s],r=e[t+2*s+1],o=n-r,a=n+r;o<i[s]&&(i[s]=o),a>i[s+3]&&(i[s+3]=a)}}function zi(t){const e=t[3]-t[0],i=t[4]-t[1],s=t[5]-t[2];return 2*(e*i+i*s+s*e)}function Bi(t,e,i,s,n=null){let r=1/0,o=1/0,a=1/0,l=-1/0,h=-1/0,c=-1/0,d=1/0,u=1/0,p=1/0,f=-1/0,m=-1/0,g=-1/0;const _=null!==n;for(let s=6*e,n=6*(e+i);s<n;s+=6){const e=t[s+0],i=t[s+1],n=e-i,y=e+i;n<r&&(r=n),y>l&&(l=y),_&&e<d&&(d=e),_&&e>f&&(f=e);const w=t[s+2],v=t[s+3],b=w-v,x=w+v;b<o&&(o=b),x>h&&(h=x),_&&w<u&&(u=w),_&&w>m&&(m=w);const E=t[s+4],T=t[s+5],C=E-T,S=E+T;C<a&&(a=C),S>c&&(c=S),_&&E<p&&(p=E),_&&E>g&&(g=E)}s[0]=r,s[1]=o,s[2]=a,s[3]=l,s[4]=h,s[5]=c,_&&(n[0]=d,n[1]=u,n[2]=p,n[3]=f,n[4]=m,n[5]=g)}const Ni=32,Ui=(t,e)=>t.candidate-e.candidate,Li=new Array(Ni).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),Ri=new Float32Array(6);class Vi{constructor(){}}function Fi(t,e,i,s,n,r){let o=s,a=s+n-1;const l=r.pos,h=2*r.axis;for(;;){for(;o<=a&&i[6*o+h]<l;)o++;for(;o<=a&&i[6*a+h]>=l;)a--;if(!(o<a))return o;for(let t=0;t<3;t++){let i=e[3*o+t];e[3*o+t]=e[3*a+t],e[3*a+t]=i}for(let t=0;t<6;t++){let e=i[6*o+t];i[6*o+t]=i[6*a+t],i[6*a+t]=e}o++,a--}}function ji(t,e,i,s,n,r){let o=s,a=s+n-1;const l=r.pos,h=2*r.axis;for(;;){for(;o<=a&&i[6*o+h]<l;)o++;for(;o<=a&&i[6*a+h]>=l;)a--;if(!(o<a))return o;{let e=t[o];t[o]=t[a],t[a]=e;for(let t=0;t<6;t++){let e=i[6*o+t];i[6*o+t]=i[6*a+t],i[6*a+t]=e}o++,a--}}}function Hi(t,e){const i=t.geometry,s=i.index?i.index.array:null,n=e.maxDepth,r=e.verbose,o=e.maxLeafTris,a=e.strategy,l=e.onProgress,h=Ci(i),c=t._indirectBuffer;let d=!1;const u=new Float32Array(6),p=new Float32Array(6),f=function(t,e){var i;(i=e)[0]=i[1]=i[2]=1/0,i[3]=i[4]=i[5]=-1/0;const s=t.attributes.position,n=t.index?t.index.array:null,r=Ci(t),o=new Float32Array(6*r),a=s.normalized,l=s.array,h=s.offset||0;let c=3;s.isInterleavedBufferAttribute&&(c=s.data.stride);const d=["getX","getY","getZ"];for(let t=0;t<r;t++){const i=3*t,r=6*t;let u=i+0,p=i+1,f=i+2;n&&(u=n[u],p=n[p],f=n[f]),a||(u=u*c+h,p=p*c+h,f=f*c+h);for(let t=0;t<3;t++){let i,n,h;a?(i=s[d[t]](u),n=s[d[t]](p),h=s[d[t]](f)):(i=l[u+t],n=l[p+t],h=l[f+t]);let c=i;n<c&&(c=n),h<c&&(c=h);let m=i;n>m&&(m=n),h>m&&(m=h);const g=(m-c)/2,_=2*t;o[r+_+0]=c+g,o[r+_+1]=g+(Math.abs(c)+g)*Ei,c<e[t]&&(e[t]=c),m>e[t+3]&&(e[t+3]=m)}}return o}(i,u),m=e.indirect?ji:Fi,g=[],_=e.indirect?Ai(i):Oi(i);if(1===_.length){const t=_[0],e=new Vi;e.boundingData=u,function(t,e,i,s){let n=1/0,r=1/0,o=1/0,a=-1/0,l=-1/0,h=-1/0;for(let s=6*e,c=6*(e+i);s<c;s+=6){const e=t[s+0];e<n&&(n=e),e>a&&(a=e);const i=t[s+2];i<r&&(r=i),i>l&&(l=i);const c=t[s+4];c<o&&(o=c),c>h&&(h=c)}s[0]=n,s[1]=r,s[2]=o,s[3]=a,s[4]=l,s[5]=h}(f,t.offset,t.count,p),w(e,t.offset,t.count,p),g.push(e)}else for(let t of _){const e=new Vi;e.boundingData=new Float32Array(6),Bi(f,t.offset,t.count,e.boundingData,p),w(e,t.offset,t.count,p),g.push(e)}return g;function y(t){l&&l(t/h)}function w(t,e,l,h=null,u=0){if(!d&&u>=n&&(d=!0,r&&(console.warn(`MeshBVH: Max depth of ${n} reached when generating BVH. Consider increasing maxDepth.`),console.warn(i))),l<=o||u>=n)return y(e+l),t.offset=e,t.count=l,t;const g=function(t,e,i,s,n,r){let o=-1,a=0;if(0===r)o=Ii(e),-1!==o&&(a=(e[o]+e[o+3])/2);else if(1===r)o=Ii(t),-1!==o&&(a=function(t,e,i,s){let n=0;for(let r=e,o=e+i;r<o;r++)n+=t[6*r+2*s];return n/i}(i,s,n,o));else if(2===r){const r=zi(t);let l=bi*n;const h=6*s,c=6*(s+n);for(let t=0;t<3;t++){const s=e[t],d=(e[t+3]-s)/Ni;if(n<8){const e=[...Li];e.length=n;let s=0;for(let n=h;n<c;n+=6,s++){const r=e[s];r.candidate=i[n+2*t],r.count=0;const{bounds:o,leftCacheBounds:a,rightCacheBounds:l}=r;for(let t=0;t<3;t++)l[t]=1/0,l[t+3]=-1/0,a[t]=1/0,a[t+3]=-1/0,o[t]=1/0,o[t+3]=-1/0;Mi(n,i,o)}e.sort(Ui);let d=n;for(let t=0;t<d;t++){const i=e[t];for(;t+1<d&&e[t+1].candidate===i.candidate;)e.splice(t+1,1),d--}for(let s=h;s<c;s+=6){const n=i[s+2*t];for(let t=0;t<d;t++){const r=e[t];n>=r.candidate?Mi(s,i,r.rightCacheBounds):(Mi(s,i,r.leftCacheBounds),r.count++)}}for(let i=0;i<d;i++){const s=e[i],h=s.count,c=n-s.count,d=s.leftCacheBounds,u=s.rightCacheBounds;let p=0;0!==h&&(p=zi(d)/r);let f=0;0!==c&&(f=zi(u)/r);const m=1+bi*(p*h+f*c);m<l&&(o=t,l=m,a=s.candidate)}}else{for(let t=0;t<Ni;t++){const e=Li[t];e.count=0,e.candidate=s+d+t*d;const i=e.bounds;for(let t=0;t<3;t++)i[t]=1/0,i[t+3]=-1/0}for(let e=h;e<c;e+=6){let n=~~((i[e+2*t]-s)/d);n>=Ni&&(n=31);const r=Li[n];r.count++,Mi(e,i,r.bounds)}const e=Li[31];Di(e.bounds,e.rightCacheBounds);for(let t=30;t>=0;t--){const e=Li[t],i=Li[t+1];ki(e.bounds,i.rightCacheBounds,e.rightCacheBounds)}let u=0;for(let e=0;e<31;e++){const i=Li[e],s=i.count,h=i.bounds,c=Li[e+1].rightCacheBounds;0!==s&&(0===u?Di(h,Ri):ki(h,Ri,Ri)),u+=s;let d=0,p=0;0!==u&&(d=zi(Ri)/r);const f=n-u;0!==f&&(p=zi(c)/r);const m=1+bi*(d*u+p*f);m<l&&(o=t,l=m,a=i.candidate)}}}}else console.warn(`MeshBVH: Invalid build strategy value ${r} used.`);return{axis:o,pos:a}}(t.boundingData,h,f,e,l,a);if(-1===g.axis)return y(e+l),t.offset=e,t.count=l,t;const _=m(c,s,f,e,l,g);if(_===e||_===e+l)y(e+l),t.offset=e,t.count=l;else{t.splitAxis=g.axis;const i=new Vi,s=e,n=_-e;t.left=i,i.boundingData=new Float32Array(6),Bi(f,s,n,i.boundingData,p),w(i,s,n,p,u+1);const r=new Vi,o=_,a=l-n;t.right=r,r.boundingData=new Float32Array(6),Bi(f,o,a,r.boundingData,p),w(r,o,a,p,u+1)}return t}}function Zi(t,e){const i=t.geometry;e.indirect&&(t._indirectBuffer=function(t,e){const i=(t.index?t.index.count:t.attributes.position.count)/3,s=i>65536,n=s?4:2,r=e?new SharedArrayBuffer(i*n):new ArrayBuffer(i*n),o=s?new Uint32Array(r):new Uint16Array(r);for(let t=0,e=o.length;t<e;t++)o[t]=t;return o}(i,e.useSharedArrayBuffer),function(t){if(0===t.groups.length)return!1;const e=Ci(t),i=Oi(t).sort((t,e)=>t.offset-e.offset),s=i[i.length-1];s.count=Math.min(e-s.offset,s.count);let n=0;return i.forEach(({count:t})=>n+=t),e!==n}(i)&&!e.verbose&&console.warn('MeshBVH: Provided geometry contains groups that do not fully span the vertex contents while using the "indirect" option. BVH may incorrectly report intersections on unrendered portions of the geometry.')),t._indirectBuffer||Si(i,e);const s=Hi(t,e);let n,r,o;const a=[],l=e.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer;for(let t=0;t<s.length;t++){const e=s[t];const i=new l(32*h(e));n=new Float32Array(i),r=new Uint32Array(i),o=new Uint16Array(i),c(0,e),a.push(i)}return void(t._roots=a);function h(t){return t.count?1:1+h(t.left)+h(t.right)}function c(t,e){const i=t/4,s=t/2,a=!!e.count,l=e.boundingData;for(let t=0;t<6;t++)n[i+t]=l[t];if(a){const n=e.offset,a=e.count;return r[i+6]=n,o[s+14]=a,o[s+15]=xi,t+32}{const s=e.left,n=e.right,o=e.splitAxis;let a;if(a=c(t+32,s),a/4>Math.pow(2,32))throw new Error("MeshBVH: Cannot store child pointer greater than 32 bits.");return r[i+6]=a/4,a=c(a,n),r[i+7]=o,a}}}class $i{constructor(){this.min=1/0,this.max=-1/0}setFromPointsField(t,e){let i=1/0,s=-1/0;for(let n=0,r=t.length;n<r;n++){const r=t[n][e];i=r<i?r:i,s=r>s?r:s}this.min=i,this.max=s}setFromPoints(t,e){let i=1/0,s=-1/0;for(let n=0,r=e.length;n<r;n++){const r=e[n],o=t.dot(r);i=o<i?o:i,s=o>s?o:s}this.min=i,this.max=s}isSeparated(t){return this.min>t.max||t.min>this.max}}$i.prototype.setFromBox=function(){const t=new s.Vector3;return function(e,i){const s=i.min,n=i.max;let r=1/0,o=-1/0;for(let i=0;i<=1;i++)for(let a=0;a<=1;a++)for(let l=0;l<=1;l++){t.x=s.x*i+n.x*(1-i),t.y=s.y*a+n.y*(1-a),t.z=s.z*l+n.z*(1-l);const h=e.dot(t);r=Math.min(h,r),o=Math.max(h,o)}this.min=r,this.max=o}}();const Wi=function(){const t=new s.Vector3,e=new s.Vector3,i=new s.Vector3;return function(s,n,r){const o=s.start,a=t,l=n.start,h=e;i.subVectors(o,l),t.subVectors(s.end,s.start),e.subVectors(n.end,n.start);const c=i.dot(h),d=h.dot(a),u=h.dot(h),p=i.dot(a),f=a.dot(a)*u-d*d;let m,g;m=0!==f?(c*d-p*u)/f:0,g=(c+m*d)/u,r.x=m,r.y=g}}(),Yi=function(){const t=new s.Vector2,e=new s.Vector3,i=new s.Vector3;return function(s,n,r,o){Wi(s,n,t);let a=t.x,l=t.y;if(a>=0&&a<=1&&l>=0&&l<=1)return s.at(a,r),void n.at(l,o);if(a>=0&&a<=1)return l<0?n.at(0,o):n.at(1,o),void s.closestPointToPoint(o,!0,r);if(l>=0&&l<=1)return a<0?s.at(0,r):s.at(1,r),void n.closestPointToPoint(r,!0,o);{let t,h;t=a<0?s.start:s.end,h=l<0?n.start:n.end;const c=e,d=i;return s.closestPointToPoint(h,!0,e),n.closestPointToPoint(t,!0,i),c.distanceToSquared(h)<=d.distanceToSquared(t)?(r.copy(c),void o.copy(h)):(r.copy(t),void o.copy(d))}}}(),Gi=function(){const t=new s.Vector3,e=new s.Vector3,i=new s.Plane,n=new s.Line3;return function(s,r){const{radius:o,center:a}=s,{a:l,b:h,c:c}=r;n.start=l,n.end=h;if(n.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;n.start=l,n.end=c;if(n.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;n.start=h,n.end=c;if(n.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;const d=r.getPlane(i);if(Math.abs(d.distanceToPoint(a))<=o){const t=d.projectPoint(a,e);if(r.containsPoint(t))return!0}return!1}}();function qi(t){return Math.abs(t)<1e-15}class Xi extends s.Triangle{constructor(...t){super(...t),this.isExtendedTriangle=!0,this.satAxes=new Array(4).fill().map(()=>new s.Vector3),this.satBounds=new Array(4).fill().map(()=>new $i),this.points=[this.a,this.b,this.c],this.sphere=new s.Sphere,this.plane=new s.Plane,this.needsUpdate=!0}intersectsSphere(t){return Gi(t,this)}update(){const t=this.a,e=this.b,i=this.c,s=this.points,n=this.satAxes,r=this.satBounds,o=n[0],a=r[0];this.getNormal(o),a.setFromPoints(o,s);const l=n[1],h=r[1];l.subVectors(t,e),h.setFromPoints(l,s);const c=n[2],d=r[2];c.subVectors(e,i),d.setFromPoints(c,s);const u=n[3],p=r[3];u.subVectors(i,t),p.setFromPoints(u,s),this.sphere.setFromPoints(this.points),this.plane.setFromNormalAndCoplanarPoint(o,t),this.needsUpdate=!1}}Xi.prototype.closestPointToSegment=function(){const t=new s.Vector3,e=new s.Vector3,i=new s.Line3;return function(s,n=null,r=null){const{start:o,end:a}=s,l=this.points;let h,c=1/0;for(let o=0;o<3;o++){const a=(o+1)%3;i.start.copy(l[o]),i.end.copy(l[a]),Yi(i,s,t,e),h=t.distanceToSquared(e),h<c&&(c=h,n&&n.copy(t),r&&r.copy(e))}return this.closestPointToPoint(o,t),h=o.distanceToSquared(t),h<c&&(c=h,n&&n.copy(t),r&&r.copy(o)),this.closestPointToPoint(a,t),h=a.distanceToSquared(t),h<c&&(c=h,n&&n.copy(t),r&&r.copy(a)),Math.sqrt(c)}}(),Xi.prototype.intersectsTriangle=function(){const t=new Xi,e=new Array(3),i=new Array(3),n=new $i,r=new $i,o=new s.Vector3,a=new s.Vector3,l=new s.Vector3,h=new s.Vector3,c=new s.Vector3,d=new s.Line3,u=new s.Line3,p=new s.Line3,f=new s.Vector3;function m(t,e,i){const s=t.points;let n=0,r=-1;for(let t=0;t<3;t++){const{start:o,end:l}=d;o.copy(s[t]),l.copy(s[(t+1)%3]),d.delta(a);const h=qi(e.distanceToPoint(o));if(qi(e.normal.dot(a))&&h){i.copy(d),n=2;break}const c=e.intersectLine(d,f);if(!c&&h&&f.copy(o),(c||h)&&!qi(f.distanceTo(l))){if(n<=1){(1===n?i.start:i.end).copy(f),h&&(r=n)}else if(n>=2){(1===r?i.start:i.end).copy(f),n=2;break}if(n++,2===n&&-1===r)break}}return n}return function(s,a=null,d=!1){this.needsUpdate&&this.update(),s.isExtendedTriangle?s.needsUpdate&&s.update():(t.copy(s),t.update(),s=t);const f=this.plane,g=s.plane;if(Math.abs(f.normal.dot(g.normal))>1-1e-10){const t=this.satBounds,l=this.satAxes;i[0]=s.a,i[1]=s.b,i[2]=s.c;for(let e=0;e<4;e++){const s=t[e],r=l[e];if(n.setFromPoints(r,i),s.isSeparated(n))return!1}const h=s.satBounds,c=s.satAxes;e[0]=this.a,e[1]=this.b,e[2]=this.c;for(let t=0;t<4;t++){const i=h[t],s=c[t];if(n.setFromPoints(s,e),i.isSeparated(n))return!1}for(let t=0;t<4;t++){const s=l[t];for(let t=0;t<4;t++){const a=c[t];if(o.crossVectors(s,a),n.setFromPoints(o,e),r.setFromPoints(o,i),n.isSeparated(r))return!1}}return a&&(d||console.warn("ExtendedTriangle.intersectsTriangle: Triangles are coplanar which does not support an output edge. Setting edge to 0, 0, 0."),a.start.set(0,0,0),a.end.set(0,0,0)),!0}{const t=m(this,g,u);if(1===t&&s.containsPoint(u.end))return a&&(a.start.copy(u.end),a.end.copy(u.end)),!0;if(2!==t)return!1;const e=m(s,f,p);if(1===e&&this.containsPoint(p.end))return a&&(a.start.copy(p.end),a.end.copy(p.end)),!0;if(2!==e)return!1;if(u.delta(l),p.delta(h),l.dot(h)<0){let t=p.start;p.start=p.end,p.end=t}const i=u.start.dot(l),n=u.end.dot(l),r=p.start.dot(l),o=p.end.dot(l);return(i===o||r===n||n<r!==i<o)&&(a&&(c.subVectors(u.start,p.start),c.dot(l)>0?a.start.copy(u.start):a.start.copy(p.start),c.subVectors(u.end,p.end),c.dot(l)<0?a.end.copy(u.end):a.end.copy(p.end)),!0)}}}(),Xi.prototype.distanceToPoint=function(){const t=new s.Vector3;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),Xi.prototype.distanceToTriangle=function(){const t=new s.Vector3,e=new s.Vector3,i=["a","b","c"],n=new s.Line3,r=new s.Line3;return function(s,o=null,a=null){const l=o||a?n:null;if(this.intersectsTriangle(s,l))return(o||a)&&(o&&l.getCenter(o),a&&l.getCenter(a)),0;let h=1/0;for(let e=0;e<3;e++){let n;const r=i[e],l=s[r];this.closestPointToPoint(l,t),n=l.distanceToSquared(t),n<h&&(h=n,o&&o.copy(t),a&&a.copy(l));const c=this[r];s.closestPointToPoint(c,t),n=c.distanceToSquared(t),n<h&&(h=n,o&&o.copy(c),a&&a.copy(t))}for(let l=0;l<3;l++){const c=i[l],d=i[(l+1)%3];n.set(this[c],this[d]);for(let l=0;l<3;l++){const c=i[l],d=i[(l+1)%3];r.set(s[c],s[d]),Yi(n,r,t,e);const u=t.distanceToSquared(e);u<h&&(h=u,o&&o.copy(t),a&&a.copy(e))}}return Math.sqrt(h)}}();class Qi{constructor(t,e,i){this.isOrientedBox=!0,this.min=new s.Vector3,this.max=new s.Vector3,this.matrix=new s.Matrix4,this.invMatrix=new s.Matrix4,this.points=new Array(8).fill().map(()=>new s.Vector3),this.satAxes=new Array(3).fill().map(()=>new s.Vector3),this.satBounds=new Array(3).fill().map(()=>new $i),this.alignedSatBounds=new Array(3).fill().map(()=>new $i),this.needsUpdate=!1,t&&this.min.copy(t),e&&this.max.copy(e),i&&this.matrix.copy(i)}set(t,e,i){this.min.copy(t),this.max.copy(e),this.matrix.copy(i),this.needsUpdate=!0}copy(t){this.min.copy(t.min),this.max.copy(t.max),this.matrix.copy(t.matrix),this.needsUpdate=!0}}Qi.prototype.update=function(){return function(){const t=this.matrix,e=this.min,i=this.max,s=this.points;for(let n=0;n<=1;n++)for(let r=0;r<=1;r++)for(let o=0;o<=1;o++){const a=s[1*n|2*r|4*o];a.x=n?i.x:e.x,a.y=r?i.y:e.y,a.z=o?i.z:e.z,a.applyMatrix4(t)}const n=this.satBounds,r=this.satAxes,o=s[0];for(let t=0;t<3;t++){const e=r[t],i=n[t],a=s[1<<t];e.subVectors(o,a),i.setFromPoints(e,s)}const a=this.alignedSatBounds;a[0].setFromPointsField(s,"x"),a[1].setFromPointsField(s,"y"),a[2].setFromPointsField(s,"z"),this.invMatrix.copy(this.matrix).invert(),this.needsUpdate=!1}}(),Qi.prototype.intersectsBox=function(){const t=new $i;return function(e){this.needsUpdate&&this.update();const i=e.min,s=e.max,n=this.satBounds,r=this.satAxes,o=this.alignedSatBounds;if(t.min=i.x,t.max=s.x,o[0].isSeparated(t))return!1;if(t.min=i.y,t.max=s.y,o[1].isSeparated(t))return!1;if(t.min=i.z,t.max=s.z,o[2].isSeparated(t))return!1;for(let i=0;i<3;i++){const s=r[i],o=n[i];if(t.setFromBox(s,e),o.isSeparated(t))return!1}return!0}}(),Qi.prototype.intersectsTriangle=function(){const t=new Xi,e=new Array(3),i=new $i,n=new $i,r=new s.Vector3;return function(s){this.needsUpdate&&this.update(),s.isExtendedTriangle?s.needsUpdate&&s.update():(t.copy(s),t.update(),s=t);const o=this.satBounds,a=this.satAxes;e[0]=s.a,e[1]=s.b,e[2]=s.c;for(let t=0;t<3;t++){const s=o[t],n=a[t];if(i.setFromPoints(n,e),s.isSeparated(i))return!1}const l=s.satBounds,h=s.satAxes,c=this.points;for(let t=0;t<3;t++){const e=l[t],s=h[t];if(i.setFromPoints(s,c),e.isSeparated(i))return!1}for(let t=0;t<3;t++){const s=a[t];for(let t=0;t<4;t++){const o=h[t];if(r.crossVectors(s,o),i.setFromPoints(r,e),n.setFromPoints(r,c),i.isSeparated(n))return!1}}return!0}}(),Qi.prototype.closestPointToPoint=function(){return function(t,e){return this.needsUpdate&&this.update(),e.copy(t).applyMatrix4(this.invMatrix).clamp(this.min,this.max).applyMatrix4(this.matrix),e}}(),Qi.prototype.distanceToPoint=function(){const t=new s.Vector3;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),Qi.prototype.distanceToBox=function(){const t=["x","y","z"],e=new Array(12).fill().map(()=>new s.Line3),i=new Array(12).fill().map(()=>new s.Line3),n=new s.Vector3,r=new s.Vector3;return function(s,o=0,a=null,l=null){if(this.needsUpdate&&this.update(),this.intersectsBox(s))return(a||l)&&(s.getCenter(r),this.closestPointToPoint(r,n),s.closestPointToPoint(n,r),a&&a.copy(n),l&&l.copy(r)),0;const h=o*o,c=s.min,d=s.max,u=this.points;let p=1/0;for(let t=0;t<8;t++){const e=u[t];r.copy(e).clamp(c,d);const i=e.distanceToSquared(r);if(i<p&&(p=i,a&&a.copy(e),l&&l.copy(r),i<h))return Math.sqrt(i)}let f=0;for(let s=0;s<3;s++)for(let n=0;n<=1;n++)for(let r=0;r<=1;r++){const o=(s+1)%3,a=(s+2)%3,l=1<<s|n<<o|r<<a,h=u[n<<o|r<<a],p=u[l];e[f].set(h,p);const m=t[s],g=t[o],_=t[a],y=i[f],w=y.start,v=y.end;w[m]=c[m],w[g]=n?c[g]:d[g],w[_]=r?c[_]:d[g],v[m]=d[m],v[g]=n?c[g]:d[g],v[_]=r?c[_]:d[g],f++}for(let t=0;t<=1;t++)for(let e=0;e<=1;e++)for(let i=0;i<=1;i++){r.x=t?d.x:c.x,r.y=e?d.y:c.y,r.z=i?d.z:c.z,this.closestPointToPoint(r,n);const s=r.distanceToSquared(n);if(s<p&&(p=s,a&&a.copy(n),l&&l.copy(r),s<h))return Math.sqrt(s)}for(let t=0;t<12;t++){const s=e[t];for(let t=0;t<12;t++){const e=i[t];Yi(s,e,n,r);const o=n.distanceToSquared(r);if(o<p&&(p=o,a&&a.copy(n),l&&l.copy(r),o<h))return Math.sqrt(o)}}return Math.sqrt(p)}}();class Ki{constructor(t){this._getNewPrimitive=t,this._primitives=[]}getPrimitive(){const t=this._primitives;return 0===t.length?this._getNewPrimitive():t.pop()}releasePrimitive(t){this._primitives.push(t)}}class Ji extends Ki{constructor(){super(()=>new Xi)}}const ts=new Ji;function es(t,e){return 65535===e[t+15]}function is(t,e){return e[t+6]}function ss(t,e){return e[t+14]}function ns(t){return t+8}function rs(t,e){return e[t+6]}function os(t,e){return e[t+7]}function as(t){return t}const ls=new class{constructor(){this.float32Array=null,this.uint16Array=null,this.uint32Array=null;const t=[];let e=null;this.setBuffer=i=>{e&&t.push(e),e=i,this.float32Array=new Float32Array(i),this.uint16Array=new Uint16Array(i),this.uint32Array=new Uint32Array(i)},this.clearBuffer=()=>{e=null,this.float32Array=null,this.uint16Array=null,this.uint32Array=null,0!==t.length&&this.setBuffer(t.pop())}}};let hs,cs;const ds=[],us=new Ki(()=>new s.Box3);function ps(t,e,i,s,n,r){hs=us.getPrimitive(),cs=us.getPrimitive(),ds.push(hs,cs),ls.setBuffer(t._roots[e]);const o=fs(0,t.geometry,i,s,n,r);ls.clearBuffer(),us.releasePrimitive(hs),us.releasePrimitive(cs),ds.pop(),ds.pop();const a=ds.length;return a>0&&(cs=ds[a-1],hs=ds[a-2]),o}function fs(t,e,i,s,n=null,r=0,o=0){const{float32Array:a,uint16Array:l,uint32Array:h}=ls;let c=2*t;if(es(c,l)){const e=is(t,h),i=ss(c,l);return Pi(t,a,hs),s(e,i,!1,o,r+t,hs)}{let c=function(t){const{uint16Array:e,uint32Array:i}=ls;let s=2*t;for(;!es(s,e);)s=2*(t=ns(t));return is(t,i)},d=function(t){const{uint16Array:e,uint32Array:i}=ls;let s=2*t;for(;!es(s,e);)s=2*(t=rs(t,i));return is(t,i)+ss(s,e)};const u=ns(t),p=rs(t,h);let f,m,g,_,y=u,w=p;if(n&&(g=hs,_=cs,Pi(y,a,g),Pi(w,a,_),f=n(g),m=n(_),m<f)){y=p,w=u;const t=f;f=m,m=t,g=_}g||(g=hs,Pi(y,a,g));const v=i(g,es(2*y,l),f,o+1,r+y);let b;if(2===v){const t=c(y);b=s(t,d(y)-t,!0,o+1,r+y,g)}else b=v&&fs(y,e,i,s,n,r,o+1);if(b)return!0;_=cs,Pi(w,a,_);const x=i(_,es(2*w,l),m,o+1,r+w);let E;if(2===x){const t=c(w);E=s(t,d(w)-t,!0,o+1,r+w,_)}else E=x&&fs(w,e,i,s,n,r,o+1);return!!E}}const ms=new s.Vector3,gs=new s.Vector3;const _s=new s.Vector3,ys=new s.Vector3,ws=new s.Vector3,vs=new s.Vector2,bs=new s.Vector2,xs=new s.Vector2,Es=new s.Vector3,Ts=new s.Vector3,Cs=new s.Vector3,Ss=new s.Vector3;function As(t,e,i,n,r,o,a,l,h){_s.fromBufferAttribute(e,o),ys.fromBufferAttribute(e,a),ws.fromBufferAttribute(e,l);const c=function(t,e,i,n,r,o){let a;return a=o===s.BackSide?t.intersectTriangle(n,i,e,!0,r):t.intersectTriangle(e,i,n,o!==s.DoubleSide,r),null===a?null:{distance:t.origin.distanceTo(r),point:r.clone()}}(t,_s,ys,ws,Ss,h);if(c){n&&(vs.fromBufferAttribute(n,o),bs.fromBufferAttribute(n,a),xs.fromBufferAttribute(n,l),c.uv=s.Triangle.getInterpolation(Ss,_s,ys,ws,vs,bs,xs,new s.Vector2)),r&&(vs.fromBufferAttribute(r,o),bs.fromBufferAttribute(r,a),xs.fromBufferAttribute(r,l),c.uv1=s.Triangle.getInterpolation(Ss,_s,ys,ws,vs,bs,xs,new s.Vector2)),i&&(Es.fromBufferAttribute(i,o),Ts.fromBufferAttribute(i,a),Cs.fromBufferAttribute(i,l),c.normal=s.Triangle.getInterpolation(Ss,_s,ys,ws,Es,Ts,Cs,new s.Vector3),c.normal.dot(t.direction)>0&&c.normal.multiplyScalar(-1));const e={a:o,b:a,c:l,normal:new s.Vector3,materialIndex:0};s.Triangle.getNormal(_s,ys,ws,e.normal),c.face=e,c.faceIndex=o}return c}function Os(t,e,i,s,n){const r=3*s;let o=r+0,a=r+1,l=r+2;const h=t.index;t.index&&(o=h.getX(o),a=h.getX(a),l=h.getX(l));const{position:c,normal:d,uv:u,uv1:p}=t.attributes,f=As(i,c,d,u,p,o,a,l,e);return f?(f.faceIndex=s,n&&n.push(f),f):null}function Ps(t,e,i,s){const n=t.a,r=t.b,o=t.c;let a=e,l=e+1,h=e+2;i&&(a=i.getX(a),l=i.getX(l),h=i.getX(h)),n.x=s.getX(a),n.y=s.getY(a),n.z=s.getZ(a),r.x=s.getX(l),r.y=s.getY(l),r.z=s.getZ(l),o.x=s.getX(h),o.y=s.getY(h),o.z=s.getZ(h)}function Is(t,e,i,s,n,r,o){const{geometry:a}=i,{index:l}=a,h=a.attributes.position;for(let i=t,a=e+t;i<a;i++){let t;if(t=i,Ps(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}function Ds(t,e=null){e&&Array.isArray(e)&&(e=new Set(e));const i=t.geometry,s=i.index?i.index.array:null,n=i.attributes.position;let r,o,a,l,h=0;const c=t._roots;for(let t=0,e=c.length;t<e;t++)r=c[t],o=new Uint32Array(r),a=new Uint16Array(r),l=new Float32Array(r),d(0,h),h+=r.byteLength;function d(t,i,r=!1){const h=2*t;if(a[h+15]===xi){const e=o[t+6];let i=1/0,r=1/0,c=1/0,d=-1/0,u=-1/0,p=-1/0;for(let t=3*e,o=3*(e+a[h+14]);t<o;t++){let e=s[t];const o=n.getX(e),a=n.getY(e),l=n.getZ(e);o<i&&(i=o),o>d&&(d=o),a<r&&(r=a),a>u&&(u=a),l<c&&(c=l),l>p&&(p=l)}return(l[t+0]!==i||l[t+1]!==r||l[t+2]!==c||l[t+3]!==d||l[t+4]!==u||l[t+5]!==p)&&(l[t+0]=i,l[t+1]=r,l[t+2]=c,l[t+3]=d,l[t+4]=u,l[t+5]=p,!0)}{const s=t+8,n=o[t+6],a=s+i,h=n+i;let c=r,u=!1,p=!1;e?c||(u=e.has(a),p=e.has(h),c=!u&&!p):(u=!0,p=!0);const f=c||p;let m=!1;(c||u)&&(m=d(s,i,c));let g=!1;f&&(g=d(n,i,c));const _=m||g;if(_)for(let e=0;e<3;e++){const i=s+e,r=n+e,o=l[i],a=l[i+3],h=l[r],c=l[r+3];l[t+e]=o<h?o:h,l[t+e+3]=a>c?a:c}return _}}}const ks=new s.Box3;function Ms(t,e,i,s){return Pi(t,e,ks),i.intersectBox(ks,s)}function zs(t,e,i,s,n,r,o){const{geometry:a}=i,{index:l}=a,h=a.attributes.position;for(let a=t,c=e+t;a<c;a++){let t;if(t=i.resolveTriangleIndex(a),Ps(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}const Bs=new s.Vector3;function Ns(t,e,i,s,n){ls.setBuffer(t._roots[e]),Us(0,t,i,s,n),ls.clearBuffer()}function Us(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=ls,l=2*t;if(es(l,o)){!function(t,e,i,s,n,r){const{geometry:o,_indirectBuffer:a}=t;for(let t=s,a=s+n;t<a;t++)Os(o,e,i,t,r)}(e,i,s,is(t,a),ss(l,o),n)}else{const o=ns(t);Ms(o,r,s,Bs)&&Us(o,e,i,s,n);const l=rs(t,a);Ms(l,r,s,Bs)&&Us(l,e,i,s,n)}}const Ls=new s.Vector3,Rs=["x","y","z"];function Vs(t,e,i,s){ls.setBuffer(t._roots[e]);const n=Fs(0,t,i,s);return ls.clearBuffer(),n}function Fs(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=ls;let a=2*t;if(es(a,r)){return function(t,e,i,s,n){const{geometry:r,_indirectBuffer:o}=t;let a=1/0,l=null;for(let t=s,o=s+n;t<o;t++){let s;s=Os(r,e,i,t),s&&s.distance<a&&(l=s,a=s.distance)}return l}(e,i,s,is(t,o),ss(a,r))}{const r=os(t,o),a=Rs[r],l=s.direction[a]>=0;let h,c;l?(h=ns(t),c=rs(t,o)):(h=rs(t,o),c=ns(t));const d=Ms(h,n,s,Ls)?Fs(h,e,i,s):null;if(d){const t=d.point[a];if(l?t<=n[c+r]:t>=n[c+r+3])return d}const u=Ms(c,n,s,Ls)?Fs(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const js=new s.Box3,Hs=new Xi,Zs=new Xi,$s=new s.Matrix4,Ws=new Qi,Ys=new Qi;function Gs(t,e,i,s){ls.setBuffer(t._roots[e]);const n=qs(0,t,i,s);return ls.clearBuffer(),n}function qs(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=ls;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),Ws.set(i.boundingBox.min,i.boundingBox.max,s),n=Ws);if(!es(l,o)){const o=t+8,l=a[t+6];Pi(o,r,js);if(n.intersectsBox(js)&&qs(o,e,i,s,n))return!0;Pi(l,r,js);return!!(n.intersectsBox(js)&&qs(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=is(t,a),f=ss(l,o);if($s.copy(s).invert(),i.boundsTree){Pi(t,r,Ys),Ys.matrix.copy($s),Ys.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>Ys.intersectsBox(t),intersectsTriangle:t=>{t.a.applyMatrix4(s),t.b.applyMatrix4(s),t.c.applyMatrix4(s),t.needsUpdate=!0;for(let e=3*p,i=3*(f+p);e<i;e+=3)if(Ps(Zs,e,h,c),Zs.needsUpdate=!0,t.intersectsTriangle(Zs))return!0;return!1}})}for(let t=3*p,e=3*(f+p);t<e;t+=3){Ps(Hs,t,h,c),Hs.a.applyMatrix4($s),Hs.b.applyMatrix4($s),Hs.c.applyMatrix4($s),Hs.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(Ps(Zs,t,d,u),Zs.needsUpdate=!0,Hs.intersectsTriangle(Zs))return!0}}}const Xs=new s.Matrix4,Qs=new Qi,Ks=new Qi,Js=new s.Vector3,tn=new s.Vector3,en=new s.Vector3,sn=new s.Vector3;function nn(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),Qs.set(e.boundingBox.min,e.boundingBox.max,i),Qs.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=ts.getPrimitive(),p=ts.getPrimitive();let f=Js,m=tn,g=null,_=null;n&&(g=en,_=sn);let y=1/0,w=null,v=null;return Xs.copy(i).invert(),Ks.matrix.copy(Xs),t.shapecast({boundsTraverseOrder:t=>Qs.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(Ks.min.copy(t.min),Ks.max.copy(t.max),Ks.needsUpdate=!0),!0),intersectsRange:(t,s)=>{if(e.boundsTree){return e.boundsTree.shapecast({boundsTraverseOrder:t=>Ks.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o,intersectsRange:(e,n)=>{for(let o=e,a=e+n;o<a;o++){Ps(p,3*o,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=t,i=t+s;e<i;e++){Ps(u,3*e,h,l),u.needsUpdate=!0;const t=u.distanceToTriangle(p,f,g);if(t<y&&(m.copy(f),_&&_.copy(g),y=t,w=e,v=o),t<r)return!0}}}})}for(let n=0,o=Ci(e);n<o;n++){Ps(p,3*n,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=t,i=t+s;e<i;e++){Ps(u,3*e,h,l),u.needsUpdate=!0;const t=u.distanceToTriangle(p,f,g);if(t<y&&(m.copy(f),_&&_.copy(g),y=t,w=e,v=n),t<r)return!0}}}}),ts.releasePrimitive(u),ts.releasePrimitive(p),y===1/0?null:(s.point?s.point.copy(m):s.point=m.clone(),s.distance=y,s.faceIndex=w,n&&(n.point?n.point.copy(_):n.point=_.clone(),n.point.applyMatrix4(Xs),m.applyMatrix4(Xs),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}function rn(t,e=null){e&&Array.isArray(e)&&(e=new Set(e));const i=t.geometry,s=i.index?i.index.array:null,n=i.attributes.position;let r,o,a,l,h=0;const c=t._roots;for(let t=0,e=c.length;t<e;t++)r=c[t],o=new Uint32Array(r),a=new Uint16Array(r),l=new Float32Array(r),d(0,h),h+=r.byteLength;function d(i,r,h=!1){const c=2*i;if(a[c+15]===xi){const e=o[i+6];let r=1/0,h=1/0,d=1/0,u=-1/0,p=-1/0,f=-1/0;for(let i=e,o=e+a[c+14];i<o;i++){const e=3*t.resolveTriangleIndex(i);for(let t=0;t<3;t++){let i=e+t;i=s?s[i]:i;const o=n.getX(i),a=n.getY(i),l=n.getZ(i);o<r&&(r=o),o>u&&(u=o),a<h&&(h=a),a>p&&(p=a),l<d&&(d=l),l>f&&(f=l)}}return(l[i+0]!==r||l[i+1]!==h||l[i+2]!==d||l[i+3]!==u||l[i+4]!==p||l[i+5]!==f)&&(l[i+0]=r,l[i+1]=h,l[i+2]=d,l[i+3]=u,l[i+4]=p,l[i+5]=f,!0)}{const t=i+8,s=o[i+6],n=t+r,a=s+r;let c=h,u=!1,p=!1;e?c||(u=e.has(n),p=e.has(a),c=!u&&!p):(u=!0,p=!0);const f=c||p;let m=!1;(c||u)&&(m=d(t,r,c));let g=!1;f&&(g=d(s,r,c));const _=m||g;if(_)for(let e=0;e<3;e++){const n=t+e,r=s+e,o=l[n],a=l[n+3],h=l[r],c=l[r+3];l[i+e]=o<h?o:h,l[i+e+3]=a>c?a:c}return _}}}const on=new s.Vector3;function an(t,e,i,s,n){ls.setBuffer(t._roots[e]),ln(0,t,i,s,n),ls.clearBuffer()}function ln(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=ls,l=2*t;if(es(l,o)){!function(t,e,i,s,n,r){const{geometry:o,_indirectBuffer:a}=t;for(let t=s,l=s+n;t<l;t++)Os(o,e,i,a?a[t]:t,r)}(e,i,s,is(t,a),ss(l,o),n)}else{const o=ns(t);Ms(o,r,s,on)&&ln(o,e,i,s,n);const l=rs(t,a);Ms(l,r,s,on)&&ln(l,e,i,s,n)}}const hn=new s.Vector3,cn=["x","y","z"];function dn(t,e,i,s){ls.setBuffer(t._roots[e]);const n=un(0,t,i,s);return ls.clearBuffer(),n}function un(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=ls;let a=2*t;if(es(a,r)){return function(t,e,i,s,n){const{geometry:r,_indirectBuffer:o}=t;let a=1/0,l=null;for(let t=s,h=s+n;t<h;t++){let s;s=Os(r,e,i,o?o[t]:t),s&&s.distance<a&&(l=s,a=s.distance)}return l}(e,i,s,is(t,o),ss(a,r))}{const r=os(t,o),a=cn[r],l=s.direction[a]>=0;let h,c;l?(h=ns(t),c=rs(t,o)):(h=rs(t,o),c=ns(t));const d=Ms(h,n,s,hn)?un(h,e,i,s):null;if(d){const t=d.point[a];if(l?t<=n[c+r]:t>=n[c+r+3])return d}const u=Ms(c,n,s,hn)?un(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const pn=new s.Box3,fn=new Xi,mn=new Xi,gn=new s.Matrix4,_n=new Qi,yn=new Qi;function wn(t,e,i,s){ls.setBuffer(t._roots[e]);const n=vn(0,t,i,s);return ls.clearBuffer(),n}function vn(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=ls;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),_n.set(i.boundingBox.min,i.boundingBox.max,s),n=_n);if(!es(l,o)){const o=t+8,l=a[t+6];Pi(o,r,pn);if(n.intersectsBox(pn)&&vn(o,e,i,s,n))return!0;Pi(l,r,pn);return!!(n.intersectsBox(pn)&&vn(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=is(t,a),f=ss(l,o);if(gn.copy(s).invert(),i.boundsTree){Pi(t,r,yn),yn.matrix.copy(gn),yn.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>yn.intersectsBox(t),intersectsTriangle:t=>{t.a.applyMatrix4(s),t.b.applyMatrix4(s),t.c.applyMatrix4(s),t.needsUpdate=!0;for(let i=p,s=f+p;i<s;i++)if(Ps(mn,3*e.resolveTriangleIndex(i),h,c),mn.needsUpdate=!0,t.intersectsTriangle(mn))return!0;return!1}})}for(let t=p,i=f+p;t<i;t++){const i=e.resolveTriangleIndex(t);Ps(fn,3*i,h,c),fn.a.applyMatrix4(gn),fn.b.applyMatrix4(gn),fn.c.applyMatrix4(gn),fn.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(Ps(mn,t,d,u),mn.needsUpdate=!0,fn.intersectsTriangle(mn))return!0}}}const bn=new s.Matrix4,xn=new Qi,En=new Qi,Tn=new s.Vector3,Cn=new s.Vector3,Sn=new s.Vector3,An=new s.Vector3;function On(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),xn.set(e.boundingBox.min,e.boundingBox.max,i),xn.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=ts.getPrimitive(),p=ts.getPrimitive();let f=Tn,m=Cn,g=null,_=null;n&&(g=Sn,_=An);let y=1/0,w=null,v=null;return bn.copy(i).invert(),En.matrix.copy(bn),t.shapecast({boundsTraverseOrder:t=>xn.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(En.min.copy(t.min),En.max.copy(t.max),En.needsUpdate=!0),!0),intersectsRange:(s,n)=>{if(e.boundsTree){const a=e.boundsTree;return a.shapecast({boundsTraverseOrder:t=>En.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o,intersectsRange:(e,o)=>{for(let b=e,x=e+o;b<x;b++){const e=a.resolveTriangleIndex(b);Ps(p,3*e,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=s,i=s+n;e<i;e++){const i=t.resolveTriangleIndex(e);Ps(u,3*i,h,l),u.needsUpdate=!0;const s=u.distanceToTriangle(p,f,g);if(s<y&&(m.copy(f),_&&_.copy(g),y=s,w=e,v=b),s<r)return!0}}}})}for(let o=0,a=Ci(e);o<a;o++){Ps(p,3*o,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=s,i=s+n;e<i;e++){const i=t.resolveTriangleIndex(e);Ps(u,3*i,h,l),u.needsUpdate=!0;const s=u.distanceToTriangle(p,f,g);if(s<y&&(m.copy(f),_&&_.copy(g),y=s,w=e,v=o),s<r)return!0}}}}),ts.releasePrimitive(u),ts.releasePrimitive(p),y===1/0?null:(s.point?s.point.copy(m):s.point=m.clone(),s.distance=y,s.faceIndex=w,n&&(n.point?n.point.copy(_):n.point=_.clone(),n.point.applyMatrix4(bn),m.applyMatrix4(bn),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}const Pn=new ls.constructor,In=new ls.constructor,Dn=new Ki(()=>new s.Box3),kn=new s.Box3,Mn=new s.Box3,zn=new s.Box3,Bn=new s.Box3;let Nn=!1;function Un(t,e,i,s,n,r=0,o=0,a=0,l=0,h=null,c=!1){let d,u;c?(d=In,u=Pn):(d=Pn,u=In);const p=d.float32Array,f=d.uint32Array,m=d.uint16Array,g=u.float32Array,_=u.uint32Array,y=u.uint16Array,w=2*e,v=es(2*t,m),b=es(w,y);let x=!1;if(b&&v)x=c?n(is(e,_),ss(2*e,y),is(t,f),ss(2*t,m),l,o+e,a,r+t):n(is(t,f),ss(2*t,m),is(e,_),ss(2*e,y),a,r+t,l,o+e);else if(b){const h=Dn.getPrimitive();Pi(e,g,h),h.applyMatrix4(i);const d=ns(t),u=rs(t,f);Pi(d,p,kn),Pi(u,p,Mn);const m=h.intersectsBox(kn),_=h.intersectsBox(Mn);x=m&&Un(e,d,s,i,n,o,r,l,a+1,h,!c)||_&&Un(e,u,s,i,n,o,r,l,a+1,h,!c),Dn.releasePrimitive(h)}else{const d=ns(e),u=rs(e,_);Pi(d,g,zn),Pi(u,g,Bn);const m=h.intersectsBox(zn),y=h.intersectsBox(Bn);if(m&&y)x=Un(t,d,i,s,n,r,o,a,l+1,h,c)||Un(t,u,i,s,n,r,o,a,l+1,h,c);else if(m)if(v)x=Un(t,d,i,s,n,r,o,a,l+1,h,c);else{const e=Dn.getPrimitive();e.copy(zn).applyMatrix4(i);const h=ns(t),u=rs(t,f);Pi(h,p,kn),Pi(u,p,Mn);const m=e.intersectsBox(kn),g=e.intersectsBox(Mn);x=m&&Un(d,h,s,i,n,o,r,l,a+1,e,!c)||g&&Un(d,u,s,i,n,o,r,l,a+1,e,!c),Dn.releasePrimitive(e)}else if(y)if(v)x=Un(t,u,i,s,n,r,o,a,l+1,h,c);else{const e=Dn.getPrimitive();e.copy(Bn).applyMatrix4(i);const h=ns(t),d=rs(t,f);Pi(h,p,kn),Pi(d,p,Mn);const m=e.intersectsBox(kn),g=e.intersectsBox(Mn);x=m&&Un(u,h,s,i,n,o,r,l,a+1,e,!c)||g&&Un(u,d,s,i,n,o,r,l,a+1,e,!c),Dn.releasePrimitive(e)}}return x}const Ln=new Qi,Rn=new s.Box3;class Vn{static serialize(t,e={}){e={cloneBuffers:!0,...e};const i=t.geometry,s=t._roots,n=t._indirectBuffer,r=i.getIndex();let o;return o=e.cloneBuffers?{roots:s.map(t=>t.slice()),index:r.array.slice(),indirectBuffer:n?n.slice():null}:{roots:s,index:r.array,indirectBuffer:n},o}static deserialize(t,e,i={}){i={setIndex:!0,indirect:Boolean(t.indirectBuffer),...i};const{index:n,roots:r,indirectBuffer:o}=t,a=new Vn(e,{...i,[Ti]:!0});if(a._roots=r,a._indirectBuffer=o||null,i.setIndex){const i=e.getIndex();if(null===i){const i=new s.BufferAttribute(t.index,1,!1);e.setIndex(i)}else i.array!==n&&(i.array.set(n),i.needsUpdate=!0)}return a}get indirect(){return!!this._indirectBuffer}constructor(t,e={}){if(!t.isBufferGeometry)throw new Error("MeshBVH: Only BufferGeometries are supported.");if(t.index&&t.index.isInterleavedBufferAttribute)throw new Error("MeshBVH: InterleavedBufferAttribute is not supported for the index attribute.");if((e=Object.assign({strategy:0,maxDepth:40,maxLeafTris:10,verbose:!0,useSharedArrayBuffer:!1,setBoundingBox:!0,onProgress:null,indirect:!1,[Ti]:!1},e)).useSharedArrayBuffer&&"undefined"==typeof SharedArrayBuffer)throw new Error("MeshBVH: SharedArrayBuffer is not available.");this.geometry=t,this._roots=null,this._indirectBuffer=null,e[Ti]||(Zi(this,e),!t.boundingBox&&e.setBoundingBox&&(t.boundingBox=this.getBoundingBox(new s.Box3)));const{_indirectBuffer:i}=this;this.resolveTriangleIndex=e.indirect?t=>i[t]:t=>t}refit(t=null){return(this.indirect?rn:Ds)(this,t)}traverse(t,e=0){const i=this._roots[e],s=new Uint32Array(i),n=new Uint16Array(i);!function e(r,o=0){const a=2*r,l=n[a+15]===xi;if(l){const e=s[r+6],h=n[a+14];t(o,l,new Float32Array(i,4*r,6),e,h)}else{const n=r+8,a=s[r+6],h=s[r+7];t(o,l,new Float32Array(i,4*r,6),h)||(e(n,o+1),e(a,o+1))}}(0)}raycast(t,e=s.FrontSide){const i=this._roots,n=this.geometry,r=[],o=e.isMaterial,a=Array.isArray(e),l=n.groups,h=o?e.side:e,c=this.indirect?an:Ns;for(let s=0,n=i.length;s<n;s++){const i=a?e[l[s].materialIndex].side:h,n=r.length;if(c(this,s,i,t,r),a){const t=l[s].materialIndex;for(let e=n,i=r.length;e<i;e++)r[e].face.materialIndex=t}}return r}raycastFirst(t,e=s.FrontSide){const i=this._roots,n=this.geometry,r=e.isMaterial,o=Array.isArray(e);let a=null;const l=n.groups,h=r?e.side:e,c=this.indirect?dn:Vs;for(let s=0,n=i.length;s<n;s++){const i=c(this,s,o?e[l[s].materialIndex].side:h,t);null!=i&&(null==a||i.distance<a.distance)&&(a=i,o&&(i.face.materialIndex=l[s].materialIndex))}return a}intersectsGeometry(t,e){let i=!1;const s=this._roots,n=this.indirect?wn:Gs;for(let r=0,o=s.length;r<o&&(i=n(this,r,t,e),!i);r++);return i}shapecast(t){const e=ts.getPrimitive(),i=this.indirect?zs:Is;let{boundsTraverseOrder:s,intersectsBounds:n,intersectsRange:r,intersectsTriangle:o}=t;if(r&&o){const t=r;r=(s,n,r,a,l)=>!!t(s,n,r,a,l)||i(s,n,this,o,r,a,e)}else r||(r=o?(t,s,n,r)=>i(t,s,this,o,n,r,e):(t,e,i)=>i);let a=!1,l=0;const h=this._roots;for(let t=0,e=h.length;t<e;t++){const e=h[t];if(a=ps(this,t,n,r,s,l),a)break;l+=e.byteLength}return ts.releasePrimitive(e),a}bvhcast(t,e,i){let{intersectsRanges:n,intersectsTriangles:r}=i;const o=ts.getPrimitive(),a=this.geometry.index,l=this.geometry.attributes.position,h=this.indirect?t=>{const e=this.resolveTriangleIndex(t);Ps(o,3*e,a,l)}:t=>{Ps(o,3*t,a,l)},c=ts.getPrimitive(),d=t.geometry.index,u=t.geometry.attributes.position,p=t.indirect?e=>{const i=t.resolveTriangleIndex(e);Ps(c,3*i,d,u)}:t=>{Ps(c,3*t,d,u)};if(r){const t=(t,i,s,n,a,l,d,u)=>{for(let f=s,m=s+n;f<m;f++){p(f),c.a.applyMatrix4(e),c.b.applyMatrix4(e),c.c.applyMatrix4(e),c.needsUpdate=!0;for(let e=t,s=t+i;e<s;e++)if(h(e),o.needsUpdate=!0,r(o,c,e,f,a,l,d,u))return!0}return!1};if(n){const e=n;n=function(i,s,n,r,o,a,l,h){return!!e(i,s,n,r,o,a,l,h)||t(i,s,n,r,o,a,l,h)}}else n=t}return function(t,e,i,n){if(Nn)throw new Error("MeshBVH: Recursive calls to bvhcast not supported.");Nn=!0;const r=t._roots,o=e._roots;let a,l=0,h=0;const c=(new s.Matrix4).copy(i).invert();for(let t=0,e=r.length;t<e;t++){Pn.setBuffer(r[t]),h=0;const e=Dn.getPrimitive();Pi(as(0),Pn.float32Array,e),e.applyMatrix4(c);for(let s=0,r=o.length;s<r&&(In.setBuffer(o[t]),a=Un(0,0,i,c,n,l,h,0,0,e),In.clearBuffer(),h+=o[s].length,!a);s++);if(Dn.releasePrimitive(e),Pn.clearBuffer(),l+=r[t].length,a)break}return Nn=!1,a}(this,t,e,n)}intersectsBox(t,e){return Ln.set(t.min,t.max,e),Ln.needsUpdate=!0,this.shapecast({intersectsBounds:t=>Ln.intersectsBox(t),intersectsTriangle:t=>Ln.intersectsTriangle(t)})}intersectsSphere(t){return this.shapecast({intersectsBounds:e=>t.intersectsBox(e),intersectsTriangle:e=>e.intersectsSphere(t)})}closestPointToGeometry(t,e,i={},s={},n=0,r=1/0){return(this.indirect?On:nn)(this,t,e,i,s,n,r)}closestPointToPoint(t,e={},i=0,s=1/0){return function(t,e,i={},s=0,n=1/0){const r=s*s,o=n*n;let a=1/0,l=null;if(t.shapecast({boundsTraverseOrder:t=>(ms.copy(e).clamp(t.min,t.max),ms.distanceToSquared(e)),intersectsBounds:(t,e,i)=>i<a&&i<o,intersectsTriangle:(t,i)=>{t.closestPointToPoint(e,ms);const s=e.distanceToSquared(ms);return s<a&&(gs.copy(ms),a=s,l=i),s<r}}),a===1/0)return null;const h=Math.sqrt(a);return i.point?i.point.copy(gs):i.point=gs.clone(),i.distance=h,i.faceIndex=l,i}(this,t,e,i,s)}getBoundingBox(t){t.makeEmpty();return this._roots.forEach(e=>{Pi(0,new Float32Array(e),Rn),t.union(Rn)}),t}}function Fn(t,e,i){return null===t?null:(t.point.applyMatrix4(e.matrixWorld),t.distance=t.point.distanceTo(i.ray.origin),t.object=e,t.distance<i.near||t.distance>i.far?null:t)}const jn=new s.Ray,Hn=new s.Matrix4,Zn=s.Mesh.prototype.raycast;function $n(t,e){if(this.geometry.boundsTree){if(void 0===this.material)return;Hn.copy(this.matrixWorld).invert(),jn.copy(t.ray).applyMatrix4(Hn);const i=this.geometry.boundsTree;if(!0===t.firstHitOnly){const s=Fn(i.raycastFirst(jn,this.material),this,t);s&&e.push(s)}else{const s=i.raycast(jn,this.material);for(let i=0,n=s.length;i<n;i++){const n=Fn(s[i],this,t);n&&e.push(n)}}}else Zn.call(this,t,e)}function Wn(t){return this.boundsTree=new Vn(this,t),this.boundsTree}function Yn(){this.boundsTree=null}const Gn=class t{constructor(){e(this,"onDisposed",new h),e(this,"list",new i.DataMap),e(this,"enabled",!1),e(this,"_clock"),e(this,"onInit",new h),e(this,"update",()=>{if(!this.enabled)return;const t=this._clock.getDelta();for(const[e,i]of this.list)i.enabled&&i.isUpdateable()&&i.update(t);requestAnimationFrame(this.update)}),this._clock=new a.Clock,t.setupBVH()}add(t,e){if(this.list.has(t))throw new Error("You're trying to add a component that already exists in the components instance. Use Components.get() instead.");v.validate(t),this.list.set(t,e)}get(t){const e=t.uuid;if(!this.list.has(e)){const i=new t(this);return i.isDisposeable()&&i.onDisposed.add(()=>this.list.delete(e)),this.list.has(e)||this.add(e,i),i}return this.list.get(e)}init(){this.enabled=!0;for(const[t,e]of this.list.entries())e.enabled=!0;this._clock.start(),this.update(),this.onInit.trigger()}dispose(){let t;this.enabled=!1;for(const[e,i]of this.list)i.enabled=!1,e!==Xt.uuid?i.isDisposeable()&&i.dispose():t=i;null==t||t.dispose(),this._clock.stop(),this.onDisposed.trigger()}static setupBVH(){a.BufferGeometry.prototype.computeBoundsTree=Wn,a.BufferGeometry.prototype.disposeBoundsTree=Yn,a.Mesh.prototype.raycast=$n}};e(Gn,"release","2.4.3");let qn=Gn;class Xn{constructor(t){e(this,"enabled",!1),e(this,"id","FirstPerson"),this.camera=t}set(t){if(this.enabled=t,t){if("Perspective"!==this.camera.projection.current)return void this.camera.set("Orbit");this.setupFirstPersonCamera()}}setupFirstPersonCamera(){const t=this.camera.controls,e=new a.Vector3;t.distance--,t.getPosition(e),t.minDistance=1,t.maxDistance=1,t.distance=1,t.moveTo(e.x,e.y,e.z),t.truckSpeed=50,t.mouseButtons.wheel=pi.ACTION.DOLLY,t.touches.two=pi.ACTION.TOUCH_ZOOM_TRUCK}}class Qn{constructor(t){e(this,"enabled",!0),e(this,"id","Orbit"),this.camera=t,this.activateOrbitControls()}set(t){this.enabled=t,t&&this.activateOrbitControls()}activateOrbitControls(){const t=this.camera.controls;t.minDistance=1,t.maxDistance=300;const e=new a.Vector3;t.getPosition(e);const i=e.length();t.distance=i,t.truckSpeed=2;const{rotation:s}=this.camera.three,n=new a.Vector3(0,0,-1).applyEuler(s),r=e.addScaledVector(n,i);t.moveTo(r.x,r.y,r.z)}}class Kn{constructor(t){e(this,"enabled",!1),e(this,"id","Plan"),e(this,"mouseAction1"),e(this,"mouseAction2"),e(this,"mouseInitialized",!1),e(this,"defaultAzimuthSpeed"),e(this,"defaultPolarSpeed"),this.camera=t,this.defaultAzimuthSpeed=t.controls.azimuthRotateSpeed,this.defaultPolarSpeed=t.controls.polarRotateSpeed}set(t){this.enabled=t;const e=this.camera.controls;e.azimuthRotateSpeed=t?0:this.defaultAzimuthSpeed,e.polarRotateSpeed=t?0:this.defaultPolarSpeed,this.mouseInitialized||(this.mouseAction1=e.touches.one,this.mouseAction2=e.touches.two,this.mouseInitialized=!0),t?(e.mouseButtons.left=pi.ACTION.TRUCK,e.touches.one=pi.ACTION.TOUCH_TRUCK,e.touches.two=pi.ACTION.TOUCH_ZOOM):(e.mouseButtons.left=pi.ACTION.ROTATE,e.touches.one=this.mouseAction1,e.touches.two=this.mouseAction2)}}class Jn{constructor(t){e(this,"onChanged",new h),e(this,"current","Perspective"),e(this,"camera"),e(this,"matchOrthoDistanceEnabled",!1),e(this,"_component"),e(this,"_previousDistance",-1),this._component=t,this.camera=t.three}async set(t){this.current!==t&&("Orthographic"===t?this.setOrthoCamera():await this.setPerspectiveCamera(),this.onChanged.trigger(this.camera))}async toggle(){const t="Perspective"===this.current?"Orthographic":"Perspective";await this.set(t)}setOrthoCamera(){if(null===this._component.mode)return;if("FirstPerson"===this._component.mode.id)return;this._previousDistance=this._component.controls.distance,this._component.controls.distance=200;const t=this.getPerspectiveDims();if(!t)return;const{width:e,height:i}=t;this.setupOrthoCamera(i,e),this.camera=this._component.threeOrtho,this.current="Orthographic"}getPerspectiveDims(){const t=this._component.currentWorld;if(!t||!t.renderer)return null;const e=new a.Vector3;this._component.threePersp.getWorldDirection(e);const i=new a.Vector3;this._component.controls.getTarget(i);const s=i.clone().sub(this._component.threePersp.position).dot(e),n=t.renderer.getSize(),r=n.x/n.y,o=this._component.threePersp,l=2*s*Math.atan(o.fov*(Math.PI/180)/2);return{width:l*r,height:l}}setupOrthoCamera(t,e){this._component.controls.mouseButtons.wheel=pi.ACTION.ZOOM,this._component.controls.mouseButtons.middle=pi.ACTION.ZOOM;const i=this._component.threePersp,s=this._component.threeOrtho;s.zoom=1,s.left=e/-2,s.right=e/2,s.top=t/2,s.bottom=t/-2,s.updateProjectionMatrix(),s.position.copy(i.position),s.quaternion.copy(i.quaternion),this._component.controls.camera=s}getDistance(){const t=this._component.threePersp,e=this._component.threeOrtho;return(e.top-e.bottom)/e.zoom/(2*Math.atan(t.fov*(Math.PI/180)/2))}async setPerspectiveCamera(){this._component.controls.mouseButtons.wheel=pi.ACTION.DOLLY,this._component.controls.mouseButtons.middle=pi.ACTION.DOLLY;const t=this._component.threePersp,e=this._component.threeOrtho;t.position.copy(e.position),t.quaternion.copy(e.quaternion),this._component.controls.mouseButtons.wheel=pi.ACTION.DOLLY,this.matchOrthoDistanceEnabled?this._component.controls.distance=this.getDistance():this._component.controls.distance=this._previousDistance,await this._component.controls.zoomTo(1),t.updateProjectionMatrix(),this._component.controls.camera=t,this.camera=t,this.current="Perspective"}}class tr extends fi{constructor(t){super(t),e(this,"projection"),e(this,"threeOrtho"),e(this,"threePersp"),e(this,"_userInputButtons",{}),e(this,"_frustumSize",50),e(this,"_navigationModes",new Map),e(this,"_mode",null),e(this,"previousSize",null),this.threePersp=this.three,this.threeOrtho=this.newOrthoCamera(),this.projection=new Jn(this),this.onAspectUpdated.add(()=>{this.setOrthoPerspCameraAspect()}),this.projection.onChanged.add(t=>{this.three=t,this.updateAspect()}),this.worlds.onItemSet.add(()=>{this._navigationModes.clear(),this._navigationModes.set("Orbit",new Qn(this)),this._navigationModes.set("FirstPerson",new Xn(this)),this._navigationModes.set("Plan",new Kn(this)),this._mode=this._navigationModes.get("Orbit"),this.mode.set(!0,{preventTargetAdjustment:!0}),this.currentWorld&&this.currentWorld.renderer&&(this.previousSize=this.currentWorld.renderer.getSize().clone())}),this.worlds.onItemDeleted.add(()=>{this._navigationModes.clear()})}get mode(){if(!this._mode)throw new Error("Mode not found, camera not initialized");return this._mode}dispose(){super.dispose(),this.threeOrtho.removeFromParent()}set(t){if(null!==this.mode&&this.mode.id!==t){if(this.mode.set(!1),!this._navigationModes.has(t))throw new Error("The specified mode does not exist!");this._mode=this._navigationModes.get(t),this.mode.set(!0)}}async fit(t,e=1.5){if(!this.enabled)return;const i=Number.MAX_VALUE,s=Number.MIN_VALUE,n=new a.Vector3(i,i,i),r=new a.Vector3(s,s,s);for(const e of t){const t=(new a.Box3).setFromObject(e);t.min.x<n.x&&(n.x=t.min.x),t.min.y<n.y&&(n.y=t.min.y),t.min.z<n.z&&(n.z=t.min.z),t.max.x>r.x&&(r.x=t.max.x),t.max.y>r.y&&(r.y=t.max.y),t.max.z>r.z&&(r.z=t.max.z)}const o=new a.Box3(n,r),l=this.components.get(Xt);if(l.initialized)for(const[,t]of l.list){const e=t.box;e.min.x<n.x&&(n.x=e.min.x),e.min.y<n.y&&(n.y=e.min.y),e.min.z<n.z&&(n.z=e.min.z),e.max.x>r.x&&(r.x=e.max.x),e.max.y>r.y&&(r.y=e.max.y),e.max.z>r.z&&(r.z=e.max.z)}const h=new a.Vector3;o.getSize(h);const c=new a.Vector3;o.getCenter(c);const d=Math.max(h.x,h.y,h.z)*e,u=new a.Sphere(c,d);await this.controls.fitToSphere(u,!0)}setUserInput(t){t?this.enableUserInput():this.disableUserInput()}addCustomNavigationMode(t){this._navigationModes.set(t.id,t)}disableUserInput(){this._userInputButtons.left=this.controls.mouseButtons.left,this._userInputButtons.right=this.controls.mouseButtons.right,this._userInputButtons.middle=this.controls.mouseButtons.middle,this._userInputButtons.wheel=this.controls.mouseButtons.wheel,this.controls.mouseButtons.left=0,this.controls.mouseButtons.right=0,this.controls.mouseButtons.middle=0,this.controls.mouseButtons.wheel=0}enableUserInput(){0!==Object.keys(this._userInputButtons).length&&(this.controls.mouseButtons.left=this._userInputButtons.left,this.controls.mouseButtons.right=this._userInputButtons.right,this.controls.mouseButtons.middle=this._userInputButtons.middle,this.controls.mouseButtons.wheel=this._userInputButtons.wheel)}newOrthoCamera(){const t=window.innerWidth/window.innerHeight;return new a.OrthographicCamera(this._frustumSize*t/-2,this._frustumSize*t/2,this._frustumSize/2,this._frustumSize/-2,.1,1e3)}setOrthoPerspCameraAspect(){if(!this.currentWorld||!this.currentWorld.renderer)return;if(!this.previousSize)return;const t=this.currentWorld.renderer.getSize(),e=this.threeOrtho.top,i=this.threeOrtho.right,s=e*(t.y/this.previousSize.y),n=i*(t.x/this.previousSize.x);this.threeOrtho.left=-n,this.threeOrtho.right=n,this.threeOrtho.top=s,this.threeOrtho.bottom=-s,this.threeOrtho.updateProjectionMatrix(),this.previousSize.copy(t)}}function er(t){throw new Error('Could not dynamically require "'+t+'". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.')}var ir={exports:{}};
9
9
  /*!
10
10
 
11
11
  JSZip v3.10.1 - A JavaScript class for generating and reading zip files
@@ -4,7 +4,7 @@ var t=Object.defineProperty,e=(e,i,s)=>(((e,i,s)=>{i in e?t(e,i,{enumerable:!0,c
4
4
  * https://github.com/yomotsu/camera-controls
5
5
  * (c) 2017 @yomotsu
6
6
  * Released under the MIT License.
7
- */const Ge=1,Qe=2,Ke=4,Je=Object.freeze({NONE:0,ROTATE:1,TRUCK:2,SCREEN_PAN:4,OFFSET:8,DOLLY:16,ZOOM:32,TOUCH_ROTATE:64,TOUCH_TRUCK:128,TOUCH_SCREEN_PAN:256,TOUCH_OFFSET:512,TOUCH_DOLLY:1024,TOUCH_ZOOM:2048,TOUCH_DOLLY_TRUCK:4096,TOUCH_DOLLY_SCREEN_PAN:8192,TOUCH_DOLLY_OFFSET:16384,TOUCH_DOLLY_ROTATE:32768,TOUCH_ZOOM_TRUCK:65536,TOUCH_ZOOM_OFFSET:131072,TOUCH_ZOOM_SCREEN_PAN:262144,TOUCH_ZOOM_ROTATE:524288}),ti=0,ei=1,ii=-1;function si(t){return t.isPerspectiveCamera}function ni(t){return t.isOrthographicCamera}const ri=2*Math.PI,oi=Math.PI/2,ai=Math.PI/180;function li(t,e,i){return Math.max(e,Math.min(i,t))}function hi(t,e=1e-5){return Math.abs(t)<e}function ci(t,e,i=1e-5){return hi(t-e,i)}function di(t,e){return Math.round(t/e)*e}function ui(t){return isFinite(t)?t:t<0?-Number.MAX_VALUE:Number.MAX_VALUE}function pi(t){return Math.abs(t)<Number.MAX_VALUE?t:t*(1/0)}function fi(t,e,i,s,n=1/0,r){const o=2/(s=Math.max(1e-4,s)),a=o*r,l=1/(1+a+.48*a*a+.235*a*a*a);let h=t-e;const c=e,d=n*s;h=li(h,-d,d),e=t-h;const u=(i.value+o*h)*r;i.value=(i.value-o*u)*l;let p=e+(h+u)*l;return c-t>0==p>c&&(p=c,i.value=(p-c)/r),p}function mi(t,e,i,s,n=1/0,r,o){const a=2/(s=Math.max(1e-4,s)),l=a*r,h=1/(1+l+.48*l*l+.235*l*l*l);let c=e.x,d=e.y,u=e.z,p=t.x-c,f=t.y-d,m=t.z-u;const g=c,_=d,y=u,w=n*s,v=p*p+f*f+m*m;if(v>w*w){const t=Math.sqrt(v);p=p/t*w,f=f/t*w,m=m/t*w}c=t.x-p,d=t.y-f,u=t.z-m;const b=(i.x+a*p)*r,x=(i.y+a*f)*r,E=(i.z+a*m)*r;i.x=(i.x-a*b)*h,i.y=(i.y-a*x)*h,i.z=(i.z-a*E)*h,o.x=c+(p+b)*h,o.y=d+(f+x)*h,o.z=u+(m+E)*h;const T=g-t.x,C=_-t.y,S=y-t.z;return T*(o.x-g)+C*(o.y-_)+S*(o.z-y)>0&&(o.x=g,o.y=_,o.z=y,i.x=(o.x-g)/r,i.y=(o.y-_)/r,i.z=(o.z-y)/r),o}function gi(t,e){e.set(0,0),t.forEach(t=>{e.x+=t.clientX,e.y+=t.clientY}),e.x/=t.length,e.y/=t.length}function _i(t,e){return!!ni(t)&&(console.warn(`${e} is not supported in OrthographicCamera`),!0)}class yi{constructor(){this._listeners={}}addEventListener(t,e){const i=this._listeners;void 0===i[t]&&(i[t]=[]),-1===i[t].indexOf(e)&&i[t].push(e)}hasEventListener(t,e){const i=this._listeners;return void 0!==i[t]&&-1!==i[t].indexOf(e)}removeEventListener(t,e){const i=this._listeners[t];if(void 0!==i){const t=i.indexOf(e);-1!==t&&i.splice(t,1)}}removeAllEventListeners(t){t?Array.isArray(this._listeners[t])&&(this._listeners[t].length=0):this._listeners={}}dispatchEvent(t){const e=this._listeners[t.type];if(void 0!==e){t.target=this;const i=e.slice(0);for(let e=0,s=i.length;e<s;e++)i[e].call(this,t)}}}var wi;const vi=1/8,bi=/Mac/.test(null===(wi=null===globalThis||void 0===globalThis?void 0:globalThis.navigator)||void 0===wi?void 0:wi.platform);let xi,Ei,Ti,Ci,Si,Ai,Oi,Pi,Ii,ki,Di,zi,Mi,Bi,Ni,Ui,Li,Ri,Fi,Vi,ji,Zi,Hi;class $i extends yi{static install(t){xi=t.THREE,Ei=Object.freeze(new xi.Vector3(0,0,0)),Ti=Object.freeze(new xi.Vector3(0,1,0)),Ci=Object.freeze(new xi.Vector3(0,0,1)),Si=new xi.Vector2,Ai=new xi.Vector3,Oi=new xi.Vector3,Pi=new xi.Vector3,Ii=new xi.Vector3,ki=new xi.Vector3,Di=new xi.Vector3,zi=new xi.Vector3,Mi=new xi.Vector3,Bi=new xi.Vector3,Ni=new xi.Spherical,Ui=new xi.Spherical,Li=new xi.Box3,Ri=new xi.Box3,Fi=new xi.Sphere,Vi=new xi.Quaternion,ji=new xi.Quaternion,Zi=new xi.Matrix4,Hi=new xi.Raycaster}static get ACTION(){return Je}set verticalDragToForward(t){console.warn("camera-controls: `verticalDragToForward` was removed. Use `mouseButtons.left = CameraControls.ACTION.SCREEN_PAN` instead.")}constructor(t,e){super(),this.minPolarAngle=0,this.maxPolarAngle=Math.PI,this.minAzimuthAngle=-1/0,this.maxAzimuthAngle=1/0,this.minDistance=Number.EPSILON,this.maxDistance=1/0,this.infinityDolly=!1,this.minZoom=.01,this.maxZoom=1/0,this.smoothTime=.25,this.draggingSmoothTime=.125,this.maxSpeed=1/0,this.azimuthRotateSpeed=1,this.polarRotateSpeed=1,this.dollySpeed=1,this.dollyDragInverted=!1,this.truckSpeed=2,this.dollyToCursor=!1,this.dragToOffset=!1,this.boundaryFriction=0,this.restThreshold=.01,this.colliderMeshes=[],this.cancel=()=>{},this._enabled=!0,this._state=Je.NONE,this._viewport=null,this._changedDolly=0,this._changedZoom=0,this._hasRested=!0,this._boundaryEnclosesCamera=!1,this._needsUpdate=!0,this._updatedLastTime=!1,this._elementRect=new DOMRect,this._isDragging=!1,this._dragNeedsUpdate=!0,this._activePointers=[],this._lockedPointer=null,this._interactiveArea=new DOMRect(0,0,1,1),this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._isUserControllingOffset=!1,this._isUserControllingZoom=!1,this._lastDollyDirection=ti,this._thetaVelocity={value:0},this._phiVelocity={value:0},this._radiusVelocity={value:0},this._targetVelocity=new xi.Vector3,this._focalOffsetVelocity=new xi.Vector3,this._zoomVelocity={value:0},this._truckInternal=(t,e,i,s)=>{let n,r;if(si(this._camera)){const i=Ai.copy(this._camera.position).sub(this._target),s=this._camera.getEffectiveFOV()*ai,o=i.length()*Math.tan(.5*s);n=this.truckSpeed*t*o/this._elementRect.height,r=this.truckSpeed*e*o/this._elementRect.height}else{if(!ni(this._camera))return;{const i=this._camera;n=this.truckSpeed*t*(i.right-i.left)/i.zoom/this._elementRect.width,r=this.truckSpeed*e*(i.top-i.bottom)/i.zoom/this._elementRect.height}}s?(i?this.setFocalOffset(this._focalOffsetEnd.x+n,this._focalOffsetEnd.y,this._focalOffsetEnd.z,!0):this.truck(n,0,!0),this.forward(-r,!0)):i?this.setFocalOffset(this._focalOffsetEnd.x+n,this._focalOffsetEnd.y+r,this._focalOffsetEnd.z,!0):this.truck(n,r,!0)},this._rotateInternal=(t,e)=>{const i=ri*this.azimuthRotateSpeed*t/this._elementRect.height,s=ri*this.polarRotateSpeed*e/this._elementRect.height;this.rotate(i,s,!0)},this._dollyInternal=(t,e,i)=>{const s=Math.pow(.95,-t*this.dollySpeed),n=this._sphericalEnd.radius,r=this._sphericalEnd.radius*s,o=li(r,this.minDistance,this.maxDistance),a=o-r;this.infinityDolly&&this.dollyToCursor?this._dollyToNoClamp(r,!0):this.infinityDolly&&!this.dollyToCursor?(this.dollyInFixed(a,!0),this._dollyToNoClamp(o,!0)):this._dollyToNoClamp(o,!0),this.dollyToCursor&&(this._changedDolly+=(this.infinityDolly?r:o)-n,this._dollyControlCoord.set(e,i)),this._lastDollyDirection=Math.sign(-t)},this._zoomInternal=(t,e,i)=>{const s=Math.pow(.95,t*this.dollySpeed),n=this._zoom,r=this._zoom*s;this.zoomTo(r,!0),this.dollyToCursor&&(this._changedZoom+=r-n,this._dollyControlCoord.set(e,i))},void 0===xi&&console.error("camera-controls: `THREE` is undefined. You must first run `CameraControls.install( { THREE: THREE } )`. Check the docs for further information."),this._camera=t,this._yAxisUpSpace=(new xi.Quaternion).setFromUnitVectors(this._camera.up,Ti),this._yAxisUpSpaceInverse=this._yAxisUpSpace.clone().invert(),this._state=Je.NONE,this._target=new xi.Vector3,this._targetEnd=this._target.clone(),this._focalOffset=new xi.Vector3,this._focalOffsetEnd=this._focalOffset.clone(),this._spherical=(new xi.Spherical).setFromVector3(Ai.copy(this._camera.position).applyQuaternion(this._yAxisUpSpace)),this._sphericalEnd=this._spherical.clone(),this._lastDistance=this._spherical.radius,this._zoom=this._camera.zoom,this._zoomEnd=this._zoom,this._lastZoom=this._zoom,this._nearPlaneCorners=[new xi.Vector3,new xi.Vector3,new xi.Vector3,new xi.Vector3],this._updateNearPlaneCorners(),this._boundary=new xi.Box3(new xi.Vector3(-1/0,-1/0,-1/0),new xi.Vector3(1/0,1/0,1/0)),this._cameraUp0=this._camera.up.clone(),this._target0=this._target.clone(),this._position0=this._camera.position.clone(),this._zoom0=this._zoom,this._focalOffset0=this._focalOffset.clone(),this._dollyControlCoord=new xi.Vector2,this.mouseButtons={left:Je.ROTATE,middle:Je.DOLLY,right:Je.TRUCK,wheel:si(this._camera)?Je.DOLLY:ni(this._camera)?Je.ZOOM:Je.NONE},this.touches={one:Je.TOUCH_ROTATE,two:si(this._camera)?Je.TOUCH_DOLLY_TRUCK:ni(this._camera)?Je.TOUCH_ZOOM_TRUCK:Je.NONE,three:Je.TOUCH_TRUCK};const i=new xi.Vector2,s=new xi.Vector2,n=new xi.Vector2,r=t=>{if(!this._enabled||!this._domElement)return;if(0!==this._interactiveArea.left||0!==this._interactiveArea.top||1!==this._interactiveArea.width||1!==this._interactiveArea.height){const e=this._domElement.getBoundingClientRect(),i=t.clientX/e.width,s=t.clientY/e.height;if(i<this._interactiveArea.left||i>this._interactiveArea.right||s<this._interactiveArea.top||s>this._interactiveArea.bottom)return}const e="mouse"!==t.pointerType?null:(t.buttons&Ge)===Ge?Ge:(t.buttons&Ke)===Ke?Ke:(t.buttons&Qe)===Qe?Qe:null;if(null!==e){const t=this._findPointerByMouseButton(e);t&&this._disposePointer(t)}if((t.buttons&Ge)===Ge&&this._lockedPointer)return;const i={pointerId:t.pointerId,clientX:t.clientX,clientY:t.clientY,deltaX:0,deltaY:0,mouseButton:e};this._activePointers.push(i),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.ownerDocument.addEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.addEventListener("pointerup",a),this._isDragging=!0,d(t)},o=t=>{t.cancelable&&t.preventDefault();const e=t.pointerId,i=this._lockedPointer||this._findPointerById(e);if(i){if(i.clientX=t.clientX,i.clientY=t.clientY,i.deltaX=t.movementX,i.deltaY=t.movementY,this._state=0,"touch"===t.pointerType)switch(this._activePointers.length){case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else(!this._isDragging&&this._lockedPointer||this._isDragging&&(t.buttons&Ge)===Ge)&&(this._state=this._state|this.mouseButtons.left),this._isDragging&&(t.buttons&Ke)===Ke&&(this._state=this._state|this.mouseButtons.middle),this._isDragging&&(t.buttons&Qe)===Qe&&(this._state=this._state|this.mouseButtons.right);u()}},a=t=>{const e=this._findPointerById(t.pointerId);if(!e||e!==this._lockedPointer){if(e&&this._disposePointer(e),"touch"===t.pointerType)switch(this._activePointers.length){case 0:this._state=Je.NONE;break;case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else this._state=Je.NONE;p()}};let l=-1;const h=t=>{if(!this._domElement)return;if(!this._enabled||this.mouseButtons.wheel===Je.NONE)return;if(0!==this._interactiveArea.left||0!==this._interactiveArea.top||1!==this._interactiveArea.width||1!==this._interactiveArea.height){const e=this._domElement.getBoundingClientRect(),i=t.clientX/e.width,s=t.clientY/e.height;if(i<this._interactiveArea.left||i>this._interactiveArea.right||s<this._interactiveArea.top||s>this._interactiveArea.bottom)return}if(t.preventDefault(),this.dollyToCursor||this.mouseButtons.wheel===Je.ROTATE||this.mouseButtons.wheel===Je.TRUCK){const t=performance.now();l-t<1e3&&this._getClientRect(this._elementRect),l=t}const e=bi?-1:-3,i=1===t.deltaMode||t.ctrlKey?t.deltaY/e:t.deltaY/(10*e),s=this.dollyToCursor?(t.clientX-this._elementRect.x)/this._elementRect.width*2-1:0,n=this.dollyToCursor?(t.clientY-this._elementRect.y)/this._elementRect.height*-2+1:0;switch(this.mouseButtons.wheel){case Je.ROTATE:this._rotateInternal(t.deltaX,t.deltaY),this._isUserControllingRotate=!0;break;case Je.TRUCK:this._truckInternal(t.deltaX,t.deltaY,!1,!1),this._isUserControllingTruck=!0;break;case Je.SCREEN_PAN:this._truckInternal(t.deltaX,t.deltaY,!1,!0),this._isUserControllingTruck=!0;break;case Je.OFFSET:this._truckInternal(t.deltaX,t.deltaY,!0,!1),this._isUserControllingOffset=!0;break;case Je.DOLLY:this._dollyInternal(-i,s,n),this._isUserControllingDolly=!0;break;case Je.ZOOM:this._zoomInternal(-i,s,n),this._isUserControllingZoom=!0}this.dispatchEvent({type:"control"})},c=t=>{if(this._domElement&&this._enabled){if(this.mouseButtons.right===$i.ACTION.NONE){const e=t instanceof PointerEvent?t.pointerId:0,i=this._findPointerById(e);return i&&this._disposePointer(i),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),void this._domElement.ownerDocument.removeEventListener("pointerup",a)}t.preventDefault()}},d=t=>{if(!this._enabled)return;gi(this._activePointers,Si),this._getClientRect(this._elementRect),i.copy(Si),s.copy(Si);if(this._activePointers.length>=2){const t=Si.x-this._activePointers[1].clientX,e=Si.y-this._activePointers[1].clientY,i=Math.sqrt(t*t+e*e);n.set(0,i);const r=.5*(this._activePointers[0].clientX+this._activePointers[1].clientX),o=.5*(this._activePointers[0].clientY+this._activePointers[1].clientY);s.set(r,o)}if(this._state=0,t)if("pointerType"in t&&"touch"===t.pointerType)switch(this._activePointers.length){case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else this._lockedPointer||(t.buttons&Ge)!==Ge||(this._state=this._state|this.mouseButtons.left),(t.buttons&Ke)===Ke&&(this._state=this._state|this.mouseButtons.middle),(t.buttons&Qe)===Qe&&(this._state=this._state|this.mouseButtons.right);else this._lockedPointer&&(this._state=this._state|this.mouseButtons.left);(this._state&Je.ROTATE)!==Je.ROTATE&&(this._state&Je.TOUCH_ROTATE)!==Je.TOUCH_ROTATE&&(this._state&Je.TOUCH_DOLLY_ROTATE)!==Je.TOUCH_DOLLY_ROTATE&&(this._state&Je.TOUCH_ZOOM_ROTATE)!==Je.TOUCH_ZOOM_ROTATE||(this._sphericalEnd.theta=this._spherical.theta,this._sphericalEnd.phi=this._spherical.phi,this._thetaVelocity.value=0,this._phiVelocity.value=0),(this._state&Je.TRUCK)!==Je.TRUCK&&(this._state&Je.SCREEN_PAN)!==Je.SCREEN_PAN&&(this._state&Je.TOUCH_TRUCK)!==Je.TOUCH_TRUCK&&(this._state&Je.TOUCH_SCREEN_PAN)!==Je.TOUCH_SCREEN_PAN&&(this._state&Je.TOUCH_DOLLY_TRUCK)!==Je.TOUCH_DOLLY_TRUCK&&(this._state&Je.TOUCH_DOLLY_SCREEN_PAN)!==Je.TOUCH_DOLLY_SCREEN_PAN&&(this._state&Je.TOUCH_ZOOM_TRUCK)!==Je.TOUCH_ZOOM_TRUCK&&(this._state&Je.TOUCH_ZOOM_SCREEN_PAN)!==Je.TOUCH_DOLLY_SCREEN_PAN||(this._targetEnd.copy(this._target),this._targetVelocity.set(0,0,0)),(this._state&Je.DOLLY)!==Je.DOLLY&&(this._state&Je.TOUCH_DOLLY)!==Je.TOUCH_DOLLY&&(this._state&Je.TOUCH_DOLLY_TRUCK)!==Je.TOUCH_DOLLY_TRUCK&&(this._state&Je.TOUCH_DOLLY_SCREEN_PAN)!==Je.TOUCH_DOLLY_SCREEN_PAN&&(this._state&Je.TOUCH_DOLLY_OFFSET)!==Je.TOUCH_DOLLY_OFFSET&&(this._state&Je.TOUCH_DOLLY_ROTATE)!==Je.TOUCH_DOLLY_ROTATE||(this._sphericalEnd.radius=this._spherical.radius,this._radiusVelocity.value=0),(this._state&Je.ZOOM)!==Je.ZOOM&&(this._state&Je.TOUCH_ZOOM)!==Je.TOUCH_ZOOM&&(this._state&Je.TOUCH_ZOOM_TRUCK)!==Je.TOUCH_ZOOM_TRUCK&&(this._state&Je.TOUCH_ZOOM_SCREEN_PAN)!==Je.TOUCH_ZOOM_SCREEN_PAN&&(this._state&Je.TOUCH_ZOOM_OFFSET)!==Je.TOUCH_ZOOM_OFFSET&&(this._state&Je.TOUCH_ZOOM_ROTATE)!==Je.TOUCH_ZOOM_ROTATE||(this._zoomEnd=this._zoom,this._zoomVelocity.value=0),(this._state&Je.OFFSET)!==Je.OFFSET&&(this._state&Je.TOUCH_OFFSET)!==Je.TOUCH_OFFSET&&(this._state&Je.TOUCH_DOLLY_OFFSET)!==Je.TOUCH_DOLLY_OFFSET&&(this._state&Je.TOUCH_ZOOM_OFFSET)!==Je.TOUCH_ZOOM_OFFSET||(this._focalOffsetEnd.copy(this._focalOffset),this._focalOffsetVelocity.set(0,0,0)),this.dispatchEvent({type:"controlstart"})},u=()=>{if(!this._enabled||!this._dragNeedsUpdate)return;this._dragNeedsUpdate=!1,gi(this._activePointers,Si);const t=this._domElement&&this._domElement.ownerDocument.pointerLockElement===this._domElement?this._lockedPointer||this._activePointers[0]:null,e=t?-t.deltaX:s.x-Si.x,r=t?-t.deltaY:s.y-Si.y;if(s.copy(Si),(this._state&Je.ROTATE)!==Je.ROTATE&&(this._state&Je.TOUCH_ROTATE)!==Je.TOUCH_ROTATE&&(this._state&Je.TOUCH_DOLLY_ROTATE)!==Je.TOUCH_DOLLY_ROTATE&&(this._state&Je.TOUCH_ZOOM_ROTATE)!==Je.TOUCH_ZOOM_ROTATE||(this._rotateInternal(e,r),this._isUserControllingRotate=!0),(this._state&Je.DOLLY)===Je.DOLLY||(this._state&Je.ZOOM)===Je.ZOOM){const t=this.dollyToCursor?(i.x-this._elementRect.x)/this._elementRect.width*2-1:0,e=this.dollyToCursor?(i.y-this._elementRect.y)/this._elementRect.height*-2+1:0,s=this.dollyDragInverted?-1:1;(this._state&Je.DOLLY)===Je.DOLLY?(this._dollyInternal(s*r*vi,t,e),this._isUserControllingDolly=!0):(this._zoomInternal(s*r*vi,t,e),this._isUserControllingZoom=!0)}if((this._state&Je.TOUCH_DOLLY)===Je.TOUCH_DOLLY||(this._state&Je.TOUCH_ZOOM)===Je.TOUCH_ZOOM||(this._state&Je.TOUCH_DOLLY_TRUCK)===Je.TOUCH_DOLLY_TRUCK||(this._state&Je.TOUCH_ZOOM_TRUCK)===Je.TOUCH_ZOOM_TRUCK||(this._state&Je.TOUCH_DOLLY_SCREEN_PAN)===Je.TOUCH_DOLLY_SCREEN_PAN||(this._state&Je.TOUCH_ZOOM_SCREEN_PAN)===Je.TOUCH_ZOOM_SCREEN_PAN||(this._state&Je.TOUCH_DOLLY_OFFSET)===Je.TOUCH_DOLLY_OFFSET||(this._state&Je.TOUCH_ZOOM_OFFSET)===Je.TOUCH_ZOOM_OFFSET||(this._state&Je.TOUCH_DOLLY_ROTATE)===Je.TOUCH_DOLLY_ROTATE||(this._state&Je.TOUCH_ZOOM_ROTATE)===Je.TOUCH_ZOOM_ROTATE){const t=Si.x-this._activePointers[1].clientX,e=Si.y-this._activePointers[1].clientY,i=Math.sqrt(t*t+e*e),r=n.y-i;n.set(0,i);const o=this.dollyToCursor?(s.x-this._elementRect.x)/this._elementRect.width*2-1:0,a=this.dollyToCursor?(s.y-this._elementRect.y)/this._elementRect.height*-2+1:0;(this._state&Je.TOUCH_DOLLY)===Je.TOUCH_DOLLY||(this._state&Je.TOUCH_DOLLY_ROTATE)===Je.TOUCH_DOLLY_ROTATE||(this._state&Je.TOUCH_DOLLY_TRUCK)===Je.TOUCH_DOLLY_TRUCK||(this._state&Je.TOUCH_DOLLY_SCREEN_PAN)===Je.TOUCH_DOLLY_SCREEN_PAN||(this._state&Je.TOUCH_DOLLY_OFFSET)===Je.TOUCH_DOLLY_OFFSET?(this._dollyInternal(r*vi,o,a),this._isUserControllingDolly=!0):(this._zoomInternal(r*vi,o,a),this._isUserControllingZoom=!0)}(this._state&Je.TRUCK)!==Je.TRUCK&&(this._state&Je.TOUCH_TRUCK)!==Je.TOUCH_TRUCK&&(this._state&Je.TOUCH_DOLLY_TRUCK)!==Je.TOUCH_DOLLY_TRUCK&&(this._state&Je.TOUCH_ZOOM_TRUCK)!==Je.TOUCH_ZOOM_TRUCK||(this._truckInternal(e,r,!1,!1),this._isUserControllingTruck=!0),(this._state&Je.SCREEN_PAN)!==Je.SCREEN_PAN&&(this._state&Je.TOUCH_SCREEN_PAN)!==Je.TOUCH_SCREEN_PAN&&(this._state&Je.TOUCH_DOLLY_SCREEN_PAN)!==Je.TOUCH_DOLLY_SCREEN_PAN&&(this._state&Je.TOUCH_ZOOM_SCREEN_PAN)!==Je.TOUCH_ZOOM_SCREEN_PAN||(this._truckInternal(e,r,!1,!0),this._isUserControllingTruck=!0),(this._state&Je.OFFSET)!==Je.OFFSET&&(this._state&Je.TOUCH_OFFSET)!==Je.TOUCH_OFFSET&&(this._state&Je.TOUCH_DOLLY_OFFSET)!==Je.TOUCH_DOLLY_OFFSET&&(this._state&Je.TOUCH_ZOOM_OFFSET)!==Je.TOUCH_ZOOM_OFFSET||(this._truckInternal(e,r,!0,!1),this._isUserControllingOffset=!0),this.dispatchEvent({type:"control"})},p=()=>{gi(this._activePointers,Si),s.copy(Si),this._dragNeedsUpdate=!1,(0===this._activePointers.length||1===this._activePointers.length&&this._activePointers[0]===this._lockedPointer)&&(this._isDragging=!1),0===this._activePointers.length&&this._domElement&&(this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this.dispatchEvent({type:"controlend"}))};this.lockPointer=()=>{this._enabled&&this._domElement&&(this.cancel(),this._lockedPointer={pointerId:-1,clientX:0,clientY:0,deltaX:0,deltaY:0,mouseButton:null},this._activePointers.push(this._lockedPointer),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.requestPointerLock(),this._domElement.ownerDocument.addEventListener("pointerlockchange",f),this._domElement.ownerDocument.addEventListener("pointerlockerror",m),this._domElement.ownerDocument.addEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.addEventListener("pointerup",a),d())},this.unlockPointer=()=>{var t,e,i;null!==this._lockedPointer&&(this._disposePointer(this._lockedPointer),this._lockedPointer=null),null===(t=this._domElement)||void 0===t||t.ownerDocument.exitPointerLock(),null===(e=this._domElement)||void 0===e||e.ownerDocument.removeEventListener("pointerlockchange",f),null===(i=this._domElement)||void 0===i||i.ownerDocument.removeEventListener("pointerlockerror",m),this.cancel()};const f=()=>{this._domElement&&this._domElement.ownerDocument.pointerLockElement===this._domElement||this.unlockPointer()},m=()=>{this.unlockPointer()};this._addAllEventListeners=t=>{this._domElement=t,this._domElement.style.touchAction="none",this._domElement.style.userSelect="none",this._domElement.style.webkitUserSelect="none",this._domElement.addEventListener("pointerdown",r),this._domElement.addEventListener("pointercancel",a),this._domElement.addEventListener("wheel",h,{passive:!1}),this._domElement.addEventListener("contextmenu",c)},this._removeAllEventListeners=()=>{this._domElement&&(this._domElement.style.touchAction="",this._domElement.style.userSelect="",this._domElement.style.webkitUserSelect="",this._domElement.removeEventListener("pointerdown",r),this._domElement.removeEventListener("pointercancel",a),this._domElement.removeEventListener("wheel",h,{passive:!1}),this._domElement.removeEventListener("contextmenu",c),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.ownerDocument.removeEventListener("pointerlockchange",f),this._domElement.ownerDocument.removeEventListener("pointerlockerror",m))},this.cancel=()=>{this._state!==Je.NONE&&(this._state=Je.NONE,this._activePointers.length=0,p())},e&&this.connect(e),this.update(0)}get camera(){return this._camera}set camera(t){this._camera=t,this.updateCameraUp(),this._camera.updateProjectionMatrix(),this._updateNearPlaneCorners(),this._needsUpdate=!0}get enabled(){return this._enabled}set enabled(t){this._enabled=t,this._domElement&&(t?(this._domElement.style.touchAction="none",this._domElement.style.userSelect="none",this._domElement.style.webkitUserSelect="none"):(this.cancel(),this._domElement.style.touchAction="",this._domElement.style.userSelect="",this._domElement.style.webkitUserSelect=""))}get active(){return!this._hasRested}get currentAction(){return this._state}get distance(){return this._spherical.radius}set distance(t){this._spherical.radius===t&&this._sphericalEnd.radius===t||(this._spherical.radius=t,this._sphericalEnd.radius=t,this._needsUpdate=!0)}get azimuthAngle(){return this._spherical.theta}set azimuthAngle(t){this._spherical.theta===t&&this._sphericalEnd.theta===t||(this._spherical.theta=t,this._sphericalEnd.theta=t,this._needsUpdate=!0)}get polarAngle(){return this._spherical.phi}set polarAngle(t){this._spherical.phi===t&&this._sphericalEnd.phi===t||(this._spherical.phi=t,this._sphericalEnd.phi=t,this._needsUpdate=!0)}get boundaryEnclosesCamera(){return this._boundaryEnclosesCamera}set boundaryEnclosesCamera(t){this._boundaryEnclosesCamera=t,this._needsUpdate=!0}set interactiveArea(t){this._interactiveArea.width=li(t.width,0,1),this._interactiveArea.height=li(t.height,0,1),this._interactiveArea.x=li(t.x,0,1-this._interactiveArea.width),this._interactiveArea.y=li(t.y,0,1-this._interactiveArea.height)}addEventListener(t,e){super.addEventListener(t,e)}removeEventListener(t,e){super.removeEventListener(t,e)}rotate(t,e,i=!1){return this.rotateTo(this._sphericalEnd.theta+t,this._sphericalEnd.phi+e,i)}rotateAzimuthTo(t,e=!1){return this.rotateTo(t,this._sphericalEnd.phi,e)}rotatePolarTo(t,e=!1){return this.rotateTo(this._sphericalEnd.theta,t,e)}rotateTo(t,e,i=!1){this._isUserControllingRotate=!1;const s=li(t,this.minAzimuthAngle,this.maxAzimuthAngle),n=li(e,this.minPolarAngle,this.maxPolarAngle);this._sphericalEnd.theta=s,this._sphericalEnd.phi=n,this._sphericalEnd.makeSafe(),this._needsUpdate=!0,i||(this._spherical.theta=this._sphericalEnd.theta,this._spherical.phi=this._sphericalEnd.phi);const r=!i||ci(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ci(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold);return this._createOnRestPromise(r)}dolly(t,e=!1){return this.dollyTo(this._sphericalEnd.radius-t,e)}dollyTo(t,e=!1){return this._isUserControllingDolly=!1,this._lastDollyDirection=ti,this._changedDolly=0,this._dollyToNoClamp(li(t,this.minDistance,this.maxDistance),e)}_dollyToNoClamp(t,e=!1){const i=this._sphericalEnd.radius;if(this.colliderMeshes.length>=1){const e=this._collisionTest(),s=ci(e,this._spherical.radius);if(!(i>t)&&s)return Promise.resolve();this._sphericalEnd.radius=Math.min(t,e)}else this._sphericalEnd.radius=t;this._needsUpdate=!0,e||(this._spherical.radius=this._sphericalEnd.radius);const s=!e||ci(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(s)}dollyInFixed(t,e=!1){this._targetEnd.add(this._getCameraDirection(Ii).multiplyScalar(t)),e||this._target.copy(this._targetEnd);const i=!e||ci(this._target.x,this._targetEnd.x,this.restThreshold)&&ci(this._target.y,this._targetEnd.y,this.restThreshold)&&ci(this._target.z,this._targetEnd.z,this.restThreshold);return this._createOnRestPromise(i)}zoom(t,e=!1){return this.zoomTo(this._zoomEnd+t,e)}zoomTo(t,e=!1){this._isUserControllingZoom=!1,this._zoomEnd=li(t,this.minZoom,this.maxZoom),this._needsUpdate=!0,e||(this._zoom=this._zoomEnd);const i=!e||ci(this._zoom,this._zoomEnd,this.restThreshold);return this._changedZoom=0,this._createOnRestPromise(i)}pan(t,e,i=!1){return console.warn("`pan` has been renamed to `truck`"),this.truck(t,e,i)}truck(t,e,i=!1){this._camera.updateMatrix(),ki.setFromMatrixColumn(this._camera.matrix,0),Di.setFromMatrixColumn(this._camera.matrix,1),ki.multiplyScalar(t),Di.multiplyScalar(-e);const s=Ai.copy(ki).add(Di),n=Oi.copy(this._targetEnd).add(s);return this.moveTo(n.x,n.y,n.z,i)}forward(t,e=!1){Ai.setFromMatrixColumn(this._camera.matrix,0),Ai.crossVectors(this._camera.up,Ai),Ai.multiplyScalar(t);const i=Oi.copy(this._targetEnd).add(Ai);return this.moveTo(i.x,i.y,i.z,e)}elevate(t,e=!1){return Ai.copy(this._camera.up).multiplyScalar(t),this.moveTo(this._targetEnd.x+Ai.x,this._targetEnd.y+Ai.y,this._targetEnd.z+Ai.z,e)}moveTo(t,e,i,s=!1){this._isUserControllingTruck=!1;const n=Ai.set(t,e,i).sub(this._targetEnd);this._encloseToBoundary(this._targetEnd,n,this.boundaryFriction),this._needsUpdate=!0,s||this._target.copy(this._targetEnd);const r=!s||ci(this._target.x,this._targetEnd.x,this.restThreshold)&&ci(this._target.y,this._targetEnd.y,this.restThreshold)&&ci(this._target.z,this._targetEnd.z,this.restThreshold);return this._createOnRestPromise(r)}lookInDirectionOf(t,e,i,s=!1){const n=Ai.set(t,e,i).sub(this._targetEnd).normalize().multiplyScalar(-this._sphericalEnd.radius).add(this._targetEnd);return this.setPosition(n.x,n.y,n.z,s)}fitToBox(t,e,{cover:i=!1,paddingLeft:s=0,paddingRight:n=0,paddingBottom:r=0,paddingTop:o=0}={}){const a=[],l=t.isBox3?Li.copy(t):Li.setFromObject(t);l.isEmpty()&&(console.warn("camera-controls: fitTo() cannot be used with an empty box. Aborting"),Promise.resolve());const h=di(this._sphericalEnd.theta,oi),c=di(this._sphericalEnd.phi,oi);a.push(this.rotateTo(h,c,e));const d=Ai.setFromSpherical(this._sphericalEnd).normalize(),u=Vi.setFromUnitVectors(d,Ci),p=ci(Math.abs(d.y),1);p&&u.multiply(ji.setFromAxisAngle(Ti,h)),u.multiply(this._yAxisUpSpaceInverse);const f=Ri.makeEmpty();Oi.copy(l.min).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.min).setX(l.max.x).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.min).setY(l.max.y).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.max).setZ(l.min.z).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.min).setZ(l.max.z).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.max).setY(l.min.y).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.max).setX(l.min.x).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.max).applyQuaternion(u),f.expandByPoint(Oi),f.min.x-=s,f.min.y-=r,f.max.x+=n,f.max.y+=o,u.setFromUnitVectors(Ci,d),p&&u.premultiply(ji.invert()),u.premultiply(this._yAxisUpSpace);const m=f.getSize(Ai),g=f.getCenter(Oi).applyQuaternion(u);if(si(this._camera)){const t=this.getDistanceToFitBox(m.x,m.y,m.z,i);a.push(this.moveTo(g.x,g.y,g.z,e)),a.push(this.dollyTo(t,e)),a.push(this.setFocalOffset(0,0,0,e))}else if(ni(this._camera)){const t=this._camera,s=t.right-t.left,n=t.top-t.bottom,r=i?Math.max(s/m.x,n/m.y):Math.min(s/m.x,n/m.y);a.push(this.moveTo(g.x,g.y,g.z,e)),a.push(this.zoomTo(r,e)),a.push(this.setFocalOffset(0,0,0,e))}return Promise.all(a)}fitToSphere(t,e){const i=[],s="isObject3D"in t?$i.createBoundingSphere(t,Fi):Fi.copy(t);if(i.push(this.moveTo(s.center.x,s.center.y,s.center.z,e)),si(this._camera)){const t=this.getDistanceToFitSphere(s.radius);i.push(this.dollyTo(t,e))}else if(ni(this._camera)){const t=this._camera.right-this._camera.left,n=this._camera.top-this._camera.bottom,r=2*s.radius,o=Math.min(t/r,n/r);i.push(this.zoomTo(o,e))}return i.push(this.setFocalOffset(0,0,0,e)),Promise.all(i)}setLookAt(t,e,i,s,n,r,o=!1){this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._lastDollyDirection=ti,this._changedDolly=0;const a=Oi.set(s,n,r),l=Ai.set(t,e,i);this._targetEnd.copy(a),this._sphericalEnd.setFromVector3(l.sub(a).applyQuaternion(this._yAxisUpSpace)),this.normalizeRotations(),this._needsUpdate=!0,o||(this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd));const h=!o||ci(this._target.x,this._targetEnd.x,this.restThreshold)&&ci(this._target.y,this._targetEnd.y,this.restThreshold)&&ci(this._target.z,this._targetEnd.z,this.restThreshold)&&ci(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ci(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&ci(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(h)}lerpLookAt(t,e,i,s,n,r,o,a,l,h,c,d,u,p=!1){this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._lastDollyDirection=ti,this._changedDolly=0;const f=Ai.set(s,n,r),m=Oi.set(t,e,i);Ni.setFromVector3(m.sub(f).applyQuaternion(this._yAxisUpSpace));const g=Pi.set(h,c,d),_=Oi.set(o,a,l);Ui.setFromVector3(_.sub(g).applyQuaternion(this._yAxisUpSpace)),this._targetEnd.copy(f.lerp(g,u));const y=Ui.theta-Ni.theta,w=Ui.phi-Ni.phi,v=Ui.radius-Ni.radius;this._sphericalEnd.set(Ni.radius+v*u,Ni.phi+w*u,Ni.theta+y*u),this.normalizeRotations(),this._needsUpdate=!0,p||(this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd));const b=!p||ci(this._target.x,this._targetEnd.x,this.restThreshold)&&ci(this._target.y,this._targetEnd.y,this.restThreshold)&&ci(this._target.z,this._targetEnd.z,this.restThreshold)&&ci(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ci(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&ci(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(b)}setPosition(t,e,i,s=!1){return this.setLookAt(t,e,i,this._targetEnd.x,this._targetEnd.y,this._targetEnd.z,s)}setTarget(t,e,i,s=!1){const n=this.getPosition(Ai),r=this.setLookAt(n.x,n.y,n.z,t,e,i,s);return this._sphericalEnd.phi=li(this._sphericalEnd.phi,this.minPolarAngle,this.maxPolarAngle),r}setFocalOffset(t,e,i,s=!1){this._isUserControllingOffset=!1,this._focalOffsetEnd.set(t,e,i),this._needsUpdate=!0,s||this._focalOffset.copy(this._focalOffsetEnd);const n=!s||ci(this._focalOffset.x,this._focalOffsetEnd.x,this.restThreshold)&&ci(this._focalOffset.y,this._focalOffsetEnd.y,this.restThreshold)&&ci(this._focalOffset.z,this._focalOffsetEnd.z,this.restThreshold);return this._createOnRestPromise(n)}setOrbitPoint(t,e,i){this._camera.updateMatrixWorld(),ki.setFromMatrixColumn(this._camera.matrixWorldInverse,0),Di.setFromMatrixColumn(this._camera.matrixWorldInverse,1),zi.setFromMatrixColumn(this._camera.matrixWorldInverse,2);const s=Ai.set(t,e,i),n=s.distanceTo(this._camera.position),r=s.sub(this._camera.position);ki.multiplyScalar(r.x),Di.multiplyScalar(r.y),zi.multiplyScalar(r.z),Ai.copy(ki).add(Di).add(zi),Ai.z=Ai.z+n,this.dollyTo(n,!1),this.setFocalOffset(-Ai.x,Ai.y,-Ai.z,!1),this.moveTo(t,e,i,!1)}setBoundary(t){if(!t)return this._boundary.min.set(-1/0,-1/0,-1/0),this._boundary.max.set(1/0,1/0,1/0),void(this._needsUpdate=!0);this._boundary.copy(t),this._boundary.clampPoint(this._targetEnd,this._targetEnd),this._needsUpdate=!0}setViewport(t,e,i,s){null!==t?(this._viewport=this._viewport||new xi.Vector4,"number"==typeof t?this._viewport.set(t,e,i,s):this._viewport.copy(t)):this._viewport=null}getDistanceToFitBox(t,e,i,s=!1){if(_i(this._camera,"getDistanceToFitBox"))return this._spherical.radius;const n=t/e,r=this._camera.getEffectiveFOV()*ai,o=this._camera.aspect;return.5*((s?n>o:n<o)?e:t/o)/Math.tan(.5*r)+.5*i}getDistanceToFitSphere(t){if(_i(this._camera,"getDistanceToFitSphere"))return this._spherical.radius;const e=this._camera.getEffectiveFOV()*ai,i=2*Math.atan(Math.tan(.5*e)*this._camera.aspect),s=1<this._camera.aspect?e:i;return t/Math.sin(.5*s)}getTarget(t,e=!0){return(t&&t.isVector3?t:new xi.Vector3).copy(e?this._targetEnd:this._target)}getPosition(t,e=!0){return(t&&t.isVector3?t:new xi.Vector3).setFromSpherical(e?this._sphericalEnd:this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(e?this._targetEnd:this._target)}getSpherical(t,e=!0){return(t||new xi.Spherical).copy(e?this._sphericalEnd:this._spherical)}getFocalOffset(t,e=!0){return(t&&t.isVector3?t:new xi.Vector3).copy(e?this._focalOffsetEnd:this._focalOffset)}normalizeRotations(){this._sphericalEnd.theta=this._sphericalEnd.theta%ri,this._sphericalEnd.theta<0&&(this._sphericalEnd.theta+=ri),this._spherical.theta+=ri*Math.round((this._sphericalEnd.theta-this._spherical.theta)/ri)}stop(){this._focalOffset.copy(this._focalOffsetEnd),this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd),this._zoom=this._zoomEnd}reset(t=!1){if(!ci(this._camera.up.x,this._cameraUp0.x)||!ci(this._camera.up.y,this._cameraUp0.y)||!ci(this._camera.up.z,this._cameraUp0.z)){this._camera.up.copy(this._cameraUp0);const t=this.getPosition(Ai);this.updateCameraUp(),this.setPosition(t.x,t.y,t.z)}const e=[this.setLookAt(this._position0.x,this._position0.y,this._position0.z,this._target0.x,this._target0.y,this._target0.z,t),this.setFocalOffset(this._focalOffset0.x,this._focalOffset0.y,this._focalOffset0.z,t),this.zoomTo(this._zoom0,t)];return Promise.all(e)}saveState(){this._cameraUp0.copy(this._camera.up),this.getTarget(this._target0),this.getPosition(this._position0),this._zoom0=this._zoom,this._focalOffset0.copy(this._focalOffset)}updateCameraUp(){this._yAxisUpSpace.setFromUnitVectors(this._camera.up,Ti),this._yAxisUpSpaceInverse.copy(this._yAxisUpSpace).invert()}applyCameraUp(){const t=Ai.subVectors(this._target,this._camera.position).normalize(),e=Oi.crossVectors(t,this._camera.up);this._camera.up.crossVectors(e,t).normalize(),this._camera.updateMatrixWorld();const i=this.getPosition(Ai);this.updateCameraUp(),this.setPosition(i.x,i.y,i.z)}update(t){const e=this._sphericalEnd.theta-this._spherical.theta,i=this._sphericalEnd.phi-this._spherical.phi,s=this._sphericalEnd.radius-this._spherical.radius,n=Mi.subVectors(this._targetEnd,this._target),r=Bi.subVectors(this._focalOffsetEnd,this._focalOffset),o=this._zoomEnd-this._zoom;if(hi(e))this._thetaVelocity.value=0,this._spherical.theta=this._sphericalEnd.theta;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.theta=fi(this._spherical.theta,this._sphericalEnd.theta,this._thetaVelocity,e,1/0,t),this._needsUpdate=!0}if(hi(i))this._phiVelocity.value=0,this._spherical.phi=this._sphericalEnd.phi;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.phi=fi(this._spherical.phi,this._sphericalEnd.phi,this._phiVelocity,e,1/0,t),this._needsUpdate=!0}if(hi(s))this._radiusVelocity.value=0,this._spherical.radius=this._sphericalEnd.radius;else{const e=this._isUserControllingDolly?this.draggingSmoothTime:this.smoothTime;this._spherical.radius=fi(this._spherical.radius,this._sphericalEnd.radius,this._radiusVelocity,e,this.maxSpeed,t),this._needsUpdate=!0}if(hi(n.x)&&hi(n.y)&&hi(n.z))this._targetVelocity.set(0,0,0),this._target.copy(this._targetEnd);else{const e=this._isUserControllingTruck?this.draggingSmoothTime:this.smoothTime;mi(this._target,this._targetEnd,this._targetVelocity,e,this.maxSpeed,t,this._target),this._needsUpdate=!0}if(hi(r.x)&&hi(r.y)&&hi(r.z))this._focalOffsetVelocity.set(0,0,0),this._focalOffset.copy(this._focalOffsetEnd);else{const e=this._isUserControllingOffset?this.draggingSmoothTime:this.smoothTime;mi(this._focalOffset,this._focalOffsetEnd,this._focalOffsetVelocity,e,this.maxSpeed,t,this._focalOffset),this._needsUpdate=!0}if(hi(o))this._zoomVelocity.value=0,this._zoom=this._zoomEnd;else{const e=this._isUserControllingZoom?this.draggingSmoothTime:this.smoothTime;this._zoom=fi(this._zoom,this._zoomEnd,this._zoomVelocity,e,1/0,t)}if(this.dollyToCursor)if(si(this._camera)&&0!==this._changedDolly){const t=this._spherical.radius-this._lastDistance,e=this._camera,i=this._getCameraDirection(Ii),s=Ai.copy(i).cross(e.up).normalize();0===s.lengthSq()&&(s.x=1);const n=Oi.crossVectors(s,i),r=this._sphericalEnd.radius*Math.tan(e.getEffectiveFOV()*ai*.5),o=(this._sphericalEnd.radius-t-this._sphericalEnd.radius)/this._sphericalEnd.radius,a=Pi.copy(this._targetEnd).add(s.multiplyScalar(this._dollyControlCoord.x*r*e.aspect)).add(n.multiplyScalar(this._dollyControlCoord.y*r)),l=Ai.copy(this._targetEnd).lerp(a,o),h=this._lastDollyDirection===ei&&this._spherical.radius<=this.minDistance,c=this._lastDollyDirection===ii&&this.maxDistance<=this._spherical.radius;if(this.infinityDolly&&(h||c)){this._sphericalEnd.radius-=t,this._spherical.radius-=t;const e=Oi.copy(i).multiplyScalar(-t);l.add(e)}this._boundary.clampPoint(l,l);const d=Oi.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedDolly-=t,hi(this._changedDolly)&&(this._changedDolly=0)}else if(ni(this._camera)&&0!==this._changedZoom){const t=this._zoom-this._lastZoom,e=this._camera,i=Ai.set(this._dollyControlCoord.x,this._dollyControlCoord.y,(e.near+e.far)/(e.near-e.far)).unproject(e),s=Oi.set(0,0,-1).applyQuaternion(e.quaternion),n=Pi.copy(i).add(s.multiplyScalar(-i.dot(e.up))),r=-(this._zoom-t-this._zoom)/this._zoom,o=this._getCameraDirection(Ii),a=this._targetEnd.dot(o),l=Ai.copy(this._targetEnd).lerp(n,r),h=l.dot(o),c=o.multiplyScalar(h-a);l.sub(c),this._boundary.clampPoint(l,l);const d=Oi.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedZoom-=t,hi(this._changedZoom)&&(this._changedZoom=0)}this._camera.zoom!==this._zoom&&(this._camera.zoom=this._zoom,this._camera.updateProjectionMatrix(),this._updateNearPlaneCorners(),this._needsUpdate=!0),this._dragNeedsUpdate=!0;const a=this._collisionTest();this._spherical.radius=Math.min(this._spherical.radius,a),this._spherical.makeSafe(),this._camera.position.setFromSpherical(this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(this._target),this._camera.lookAt(this._target);(!hi(this._focalOffset.x)||!hi(this._focalOffset.y)||!hi(this._focalOffset.z))&&(ki.setFromMatrixColumn(this._camera.matrix,0),Di.setFromMatrixColumn(this._camera.matrix,1),zi.setFromMatrixColumn(this._camera.matrix,2),ki.multiplyScalar(this._focalOffset.x),Di.multiplyScalar(-this._focalOffset.y),zi.multiplyScalar(this._focalOffset.z),Ai.copy(ki).add(Di).add(zi),this._camera.position.add(Ai),this._camera.updateMatrixWorld()),this._boundaryEnclosesCamera&&this._encloseToBoundary(this._camera.position.copy(this._target),Ai.setFromSpherical(this._spherical).applyQuaternion(this._yAxisUpSpaceInverse),1);const l=this._needsUpdate;return l&&!this._updatedLastTime?(this._hasRested=!1,this.dispatchEvent({type:"wake"}),this.dispatchEvent({type:"update"})):l?(this.dispatchEvent({type:"update"}),hi(e,this.restThreshold)&&hi(i,this.restThreshold)&&hi(s,this.restThreshold)&&hi(n.x,this.restThreshold)&&hi(n.y,this.restThreshold)&&hi(n.z,this.restThreshold)&&hi(r.x,this.restThreshold)&&hi(r.y,this.restThreshold)&&hi(r.z,this.restThreshold)&&hi(o,this.restThreshold)&&!this._hasRested&&(this._hasRested=!0,this.dispatchEvent({type:"rest"}))):!l&&this._updatedLastTime&&this.dispatchEvent({type:"sleep"}),this._lastDistance=this._spherical.radius,this._lastZoom=this._zoom,this._updatedLastTime=l,this._needsUpdate=!1,l}toJSON(){return JSON.stringify({enabled:this._enabled,minDistance:this.minDistance,maxDistance:ui(this.maxDistance),minZoom:this.minZoom,maxZoom:ui(this.maxZoom),minPolarAngle:this.minPolarAngle,maxPolarAngle:ui(this.maxPolarAngle),minAzimuthAngle:ui(this.minAzimuthAngle),maxAzimuthAngle:ui(this.maxAzimuthAngle),smoothTime:this.smoothTime,draggingSmoothTime:this.draggingSmoothTime,dollySpeed:this.dollySpeed,truckSpeed:this.truckSpeed,dollyToCursor:this.dollyToCursor,target:this._targetEnd.toArray(),position:Ai.setFromSpherical(this._sphericalEnd).add(this._targetEnd).toArray(),zoom:this._zoomEnd,focalOffset:this._focalOffsetEnd.toArray(),target0:this._target0.toArray(),position0:this._position0.toArray(),zoom0:this._zoom0,focalOffset0:this._focalOffset0.toArray()})}fromJSON(t,e=!1){const i=JSON.parse(t);this.enabled=i.enabled,this.minDistance=i.minDistance,this.maxDistance=pi(i.maxDistance),this.minZoom=i.minZoom,this.maxZoom=pi(i.maxZoom),this.minPolarAngle=i.minPolarAngle,this.maxPolarAngle=pi(i.maxPolarAngle),this.minAzimuthAngle=pi(i.minAzimuthAngle),this.maxAzimuthAngle=pi(i.maxAzimuthAngle),this.smoothTime=i.smoothTime,this.draggingSmoothTime=i.draggingSmoothTime,this.dollySpeed=i.dollySpeed,this.truckSpeed=i.truckSpeed,this.dollyToCursor=i.dollyToCursor,this._target0.fromArray(i.target0),this._position0.fromArray(i.position0),this._zoom0=i.zoom0,this._focalOffset0.fromArray(i.focalOffset0),this.moveTo(i.target[0],i.target[1],i.target[2],e),Ni.setFromVector3(Ai.fromArray(i.position).sub(this._targetEnd).applyQuaternion(this._yAxisUpSpace)),this.rotateTo(Ni.theta,Ni.phi,e),this.dollyTo(Ni.radius,e),this.zoomTo(i.zoom,e),this.setFocalOffset(i.focalOffset[0],i.focalOffset[1],i.focalOffset[2],e),this._needsUpdate=!0}connect(t){this._domElement?console.warn("camera-controls is already connected."):(t.setAttribute("data-camera-controls-version","2.10.1"),this._addAllEventListeners(t),this._getClientRect(this._elementRect))}disconnect(){this.cancel(),this._removeAllEventListeners(),this._domElement&&(this._domElement.removeAttribute("data-camera-controls-version"),this._domElement=void 0)}dispose(){this.removeAllEventListeners(),this.disconnect()}_getTargetDirection(t){return t.setFromSpherical(this._spherical).divideScalar(this._spherical.radius).applyQuaternion(this._yAxisUpSpaceInverse)}_getCameraDirection(t){return this._getTargetDirection(t).negate()}_findPointerById(t){return this._activePointers.find(e=>e.pointerId===t)}_findPointerByMouseButton(t){return this._activePointers.find(e=>e.mouseButton===t)}_disposePointer(t){this._activePointers.splice(this._activePointers.indexOf(t),1)}_encloseToBoundary(t,e,i){const s=e.lengthSq();if(0===s)return t;const n=Oi.copy(e).add(t),r=this._boundary.clampPoint(n,Pi).sub(n),o=r.lengthSq();if(0===o)return t.add(e);if(o===s)return t;if(0===i)return t.add(e).add(r);{const s=1+i*o/e.dot(r);return t.add(Oi.copy(e).multiplyScalar(s)).add(r.multiplyScalar(1-i))}}_updateNearPlaneCorners(){if(si(this._camera)){const t=this._camera,e=t.near,i=t.getEffectiveFOV()*ai,s=Math.tan(.5*i)*e,n=s*t.aspect;this._nearPlaneCorners[0].set(-n,-s,0),this._nearPlaneCorners[1].set(n,-s,0),this._nearPlaneCorners[2].set(n,s,0),this._nearPlaneCorners[3].set(-n,s,0)}else if(ni(this._camera)){const t=this._camera,e=1/t.zoom,i=t.left*e,s=t.right*e,n=t.top*e,r=t.bottom*e;this._nearPlaneCorners[0].set(i,n,0),this._nearPlaneCorners[1].set(s,n,0),this._nearPlaneCorners[2].set(s,r,0),this._nearPlaneCorners[3].set(i,r,0)}}_collisionTest(){let t=1/0;if(!(this.colliderMeshes.length>=1))return t;if(_i(this._camera,"_collisionTest"))return t;const e=this._getTargetDirection(Ii);Zi.lookAt(Ei,e,this._camera.up);for(let i=0;i<4;i++){const s=Oi.copy(this._nearPlaneCorners[i]);s.applyMatrix4(Zi);const n=Pi.addVectors(this._target,s);Hi.set(n,e),Hi.far=this._spherical.radius+1;const r=Hi.intersectObjects(this.colliderMeshes);0!==r.length&&r[0].distance<t&&(t=r[0].distance)}return t}_getClientRect(t){if(!this._domElement)return;const e=this._domElement.getBoundingClientRect();return t.x=e.left,t.y=e.top,this._viewport?(t.x+=this._viewport.x,t.y+=e.height-this._viewport.w-this._viewport.y,t.width=this._viewport.z,t.height=this._viewport.w):(t.width=e.width,t.height=e.height),t}_createOnRestPromise(t){return t?Promise.resolve():(this._hasRested=!1,this.dispatchEvent({type:"transitionstart"}),new Promise(t=>{const e=()=>{this.removeEventListener("rest",e),t()};this.addEventListener("rest",e)}))}_addAllEventListeners(t){}_removeAllEventListeners(){}get dampingFactor(){return console.warn(".dampingFactor has been deprecated. use smoothTime (in seconds) instead."),0}set dampingFactor(t){console.warn(".dampingFactor has been deprecated. use smoothTime (in seconds) instead.")}get draggingDampingFactor(){return console.warn(".draggingDampingFactor has been deprecated. use draggingSmoothTime (in seconds) instead."),0}set draggingDampingFactor(t){console.warn(".draggingDampingFactor has been deprecated. use draggingSmoothTime (in seconds) instead.")}static createBoundingSphere(t,e=new xi.Sphere){const i=e,s=i.center;Li.makeEmpty(),t.traverseVisible(t=>{t.isMesh&&Li.expandByObject(t)}),Li.getCenter(s);let n=0;return t.traverseVisible(t=>{if(!t.isMesh)return;const e=t;if(!e.geometry)return;const i=e.geometry.clone();i.applyMatrix4(e.matrixWorld);const r=i.attributes.position;for(let t=0,e=r.count;t<e;t++)Ai.fromBufferAttribute(r,t),n=Math.max(n,s.distanceToSquared(Ai))}),i.radius=Math.sqrt(n),i}}class Yi extends j{constructor(t){super(t),e(this,"onBeforeUpdate",new U),e(this,"onAfterUpdate",new U),e(this,"onAspectUpdated",new U),e(this,"onDisposed",new U),e(this,"three"),e(this,"_allControls",new Map),e(this,"updateAspect",()=>{var t;if(this.currentWorld&&this.currentWorld.renderer)if(this.three instanceof r.OrthographicCamera)this.onAspectUpdated.trigger();else if(null==(t=this.currentWorld.renderer)?void 0:t.isResizeable()){const t=this.currentWorld.renderer.getSize();this.three.aspect=t.width/t.height,this.three.updateProjectionMatrix(),this.onAspectUpdated.trigger()}}),this.three=this.setupCamera(),this.setupEvents(!0),this.worlds.onItemSet.add(({value:t})=>{const e=this.newCameraControls();this._allControls.set(t.uuid,e)}),this.worlds.onBeforeDelete.add(({value:t})=>{const e=this._allControls.get(t.uuid);e&&(e.dispose(),this._allControls.delete(t.uuid))})}get controls(){if(!this.currentWorld)throw new Error("This camera needs a world to work!");const t=this._allControls.get(this.currentWorld.uuid);if(!t)throw new Error("Controls not found!");return t}get enabled(){return null!==this.currentWorld&&this.controls.enabled}set enabled(t){null!==this.currentWorld&&(this.controls.enabled=t)}set currentWorld(t){if(super.currentWorld=t,!t)return;this.worlds.get(t.uuid)||this.worlds.set(t.uuid,t)}get currentWorld(){return this._currentWorld}dispose(){this.setupEvents(!1),this.onAspectUpdated.reset(),this.onBeforeUpdate.reset(),this.onAfterUpdate.reset(),this.three.removeFromParent(),this.onDisposed.trigger(),this.onDisposed.reset();for(const[t,e]of this._allControls)e.dispose();this.worlds.clear()}async fitToItems(t){const e=await this.getItemsBounding(t);await this.controls.fitToSphere(e,!0)}async setOrbitToItems(t){const e=await this.getItemsBounding(t);this.controls.setOrbitPoint(e.center.x,e.center.y,e.center.z)}update(t){this.enabled&&(this.onBeforeUpdate.trigger(this),this.controls.update(t),this.onAfterUpdate.trigger(this))}async getItemsBounding(t){const e=this.components.get(Ae),i=this.components.get(Me);i.list.clear();const s=new r.Sphere;if(t)await i.addFromModelIdMap(t);else for(const[,t]of e.list)i.list.add(t.box);return i.get().getBoundingSphere(s),i.list.clear(),s}setupCamera(){const t=window.innerWidth/window.innerHeight,e=new r.PerspectiveCamera(60,t,1,1e3);return e.position.set(50,50,50),e.lookAt(new r.Vector3(0,0,0)),e}newCameraControls(){if(!this.currentWorld)throw new Error("This camera needs a world to work!");if(!this.currentWorld.renderer)throw new Error("This camera needs a renderer to work!");$i.install({THREE:Yi.getSubsetOfThree()});const{domElement:t}=this.currentWorld.renderer.three,e=new $i(this.three,t);return e.smoothTime=.2,e.dollyToCursor=!0,e.infinityDolly=!0,e.minDistance=6,e}setupEvents(t){t?window.addEventListener("resize",this.updateAspect):window.removeEventListener("resize",this.updateAspect)}static getSubsetOfThree(){return{MOUSE:r.MOUSE,Vector2:r.Vector2,Vector3:r.Vector3,Vector4:r.Vector4,Quaternion:r.Quaternion,Matrix4:r.Matrix4,Spherical:r.Spherical,Box3:r.Box3,Sphere:r.Sphere,Raycaster:r.Raycaster,MathUtils:r.MathUtils}}}const Wi=class t extends F{constructor(i){super(i),e(this,"onAfterUpdate",new U),e(this,"onBeforeUpdate",new U),e(this,"onDisposed",new U),e(this,"list",new we),e(this,"enabled",!0),i.add(t.uuid,this)}create(){const t=new Ze(this.components),e=t.uuid;if(this.list.has(e))throw new Error("There is already a world with this name!");return this.list.set(e,t),t}delete(t){if(!this.list.has(t.uuid))throw new Error("The provided world is not found in the list!");this.list.delete(t.uuid),t.dispose()}dispose(){this.enabled=!1;for(const[t,e]of this.list)e.dispose();this.list.clear(),this.onDisposed.trigger()}update(t){if(this.enabled)for(const[e,i]of this.list)i.update(t)}};e(Wi,"uuid","fdb61dc4-2ec1-4966-b83d-54ea795fad4a");let Xi=Wi;class qi extends xe{constructor(){super(...arguments),e(this,"_config",{visible:{value:!0,type:"Boolean"},color:{value:new r.Color,type:"Color"},primarySize:{type:"Number",interpolable:!0,value:1,min:0,max:1e3},secondarySize:{type:"Number",interpolable:!0,value:10,min:0,max:1e3},distance:{type:"Number",interpolable:!0,value:500,min:0,max:500}})}get visible(){return this._config.visible.value}set visible(t){this._config.visible.value=t,this._component.visible=t}get color(){return this._config.color.value}set color(t){this._config.color.value=t,this._component.material.uniforms.uColor.value=t,this._component.material.uniformsNeedUpdate=!0}get primarySize(){return this._config.primarySize.value}set primarySize(t){this._config.primarySize.value=t,this._component.material.uniforms.uSize1.value=t,this._component.material.uniformsNeedUpdate=!0}get secondarySize(){return this._config.secondarySize.value}set secondarySize(t){this._config.secondarySize.value=t,this._component.material.uniforms.uSize2.value=t,this._component.material.uniformsNeedUpdate=!0}get distance(){return this._config.distance.value}set distance(t){this._config.distance.value=t,this._component.material.uniforms.uDistance.value=t,this._component.material.uniformsNeedUpdate=!0}}class Gi{constructor(t,i){e(this,"onDisposed",new U),e(this,"onSetup",new U),e(this,"isSetup",!1),e(this,"world"),e(this,"components"),e(this,"config"),e(this,"_defaultConfig",{visible:!0,color:new r.Color(12303291),primarySize:1,secondarySize:10,distance:500}),e(this,"three"),e(this,"_fade",3),e(this,"updateZoom",()=>{this.world.camera instanceof Yi&&(this.material.uniforms.uZoom.value=this.world.camera.three.zoom)}),this.world=i;const{color:s,primarySize:n,secondarySize:o,distance:a}=this._defaultConfig;this.components=t,this.config=new qi(this,this.components,"Grid");const l=new r.PlaneGeometry(2,2,1,1),h=new r.ShaderMaterial({side:r.DoubleSide,uniforms:{uSize1:{value:n},uSize2:{value:o},uColor:{value:s},uDistance:{value:a},uFade:{value:this._fade},uZoom:{value:1}},transparent:!0,vertexShader:"\n \n varying vec3 worldPosition;\n \n uniform float uDistance;\n \n void main() {\n \n vec3 pos = position.xzy * uDistance;\n pos.xz += cameraPosition.xz;\n \n worldPosition = pos;\n \n gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);\n \n }\n ",fragmentShader:"\n \n varying vec3 worldPosition;\n \n uniform float uZoom;\n uniform float uFade;\n uniform float uSize1;\n uniform float uSize2;\n uniform vec3 uColor;\n uniform float uDistance;\n \n \n \n float getGrid(float size) {\n \n vec2 r = worldPosition.xz / size;\n \n \n vec2 grid = abs(fract(r - 0.5) - 0.5) / fwidth(r);\n float line = min(grid.x, grid.y);\n \n \n return 1.0 - min(line, 1.0);\n }\n \n void main() {\n \n \n float d = 1.0 - min(distance(cameraPosition.xz, worldPosition.xz) / uDistance, 1.0);\n \n float g1 = getGrid(uSize1);\n float g2 = getGrid(uSize2);\n \n // Ortho camera fades the grid away when zooming out\n float minZoom = step(0.2, uZoom);\n float zoomFactor = pow(min(uZoom, 1.), 2.) * minZoom;\n \n gl_FragColor = vec4(uColor.rgb, mix(g2, g1, g1) * pow(d, uFade));\n gl_FragColor.a = mix(0.5 * gl_FragColor.a, gl_FragColor.a, g2) * zoomFactor;\n \n if ( gl_FragColor.a <= 0.0 ) discard;\n \n \n }\n \n ",extensions:{derivatives:!0}});this.three=new r.Mesh(l,h),this.three.frustumCulled=!1,i.scene.three.add(this.three),this.setupEvents(!0)}get visible(){return this.three.visible}set visible(t){if(this.three.visible=t,t){this.world.scene.three.add(this.three)}else this.three.removeFromParent()}get material(){return this.three.material}get fade(){return 3===this._fade}set fade(t){this._fade=t?3:0,this.material.uniforms.uFade.value=this._fade}setup(t){const e={...this._defaultConfig,...t};this.config.visible=!0,this.config.color=e.color,this.config.primarySize=e.primarySize,this.config.secondarySize=e.secondarySize,this.config.distance=e.distance,this.isSetup=!0,this.onSetup.trigger()}dispose(){this.setupEvents(!1);this.components.get(Te).list.delete(this.config.uuid);this.components.get($).destroy(this.three),this.onDisposed.trigger(),this.onDisposed.reset(),this.world=null,this.components=null}setupEvents(t){if(this.world.isDisposing)return;if(!(this.world.camera instanceof Yi))return;const e=this.world.camera.controls;t?e.addEventListener("update",this.updateZoom):e.removeEventListener("update",this.updateZoom)}}const Qi=class t extends F{constructor(i){super(i),e(this,"list",new Map),e(this,"onDisposed",new U),e(this,"enabled",!0),i.add(t.uuid,this)}create(t){if(this.list.has(t.uuid))throw new Error("This world already has a grid!");const e=new Gi(this.components,t);return this.list.set(t.uuid,e),t.onDisposed.add(()=>{this.delete(t)}),e}delete(t){const e=this.list.get(t.uuid);e&&e.dispose(),this.list.delete(t.uuid)}dispose(){for(const[t,e]of this.list)e.dispose();this.list.clear(),this.onDisposed.trigger(),this.onDisposed.reset()}};e(Qi,"uuid","d1e814d5-b81c-4452-87a2-f039375e0489");let Ki=Qi;const Ji=1.25,ts=65535,es=Math.pow(2,-24),is=Symbol("SKIP_GENERATION");function ss(t){return function(t){return t.index?t.index.count:t.attributes.position.count}(t)/3}function ns(t,e){if(!t.index){const i=t.attributes.position.count,s=function(t,e=ArrayBuffer){return t>65535?new Uint32Array(new e(4*t)):new Uint16Array(new e(2*t))}(i,e.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer);t.setIndex(new o(s,1));for(let t=0;t<i;t++)s[t]=t}}function rs(t){const e=ss(t),i=t.drawRange,s=i.start/3,n=(i.start+i.count)/3,r=Math.max(0,s),o=Math.min(e,n)-r;return[{offset:Math.floor(r),count:Math.floor(o)}]}function os(t){if(!t.groups||!t.groups.length)return rs(t);const e=[],i=new Set,s=t.drawRange,n=s.start/3,r=(s.start+s.count)/3;for(const e of t.groups){const t=e.start/3,s=(e.start+e.count)/3;i.add(Math.max(n,t)),i.add(Math.min(r,s))}const o=Array.from(i.values()).sort((t,e)=>t-e);for(let t=0;t<o.length-1;t++){const i=o[t],s=o[t+1];e.push({offset:Math.floor(i),count:Math.floor(s-i)})}return e}function as(t,e,i){return i.min.x=e[t],i.min.y=e[t+1],i.min.z=e[t+2],i.max.x=e[t+3],i.max.y=e[t+4],i.max.z=e[t+5],i}function ls(t){let e=-1,i=-1/0;for(let s=0;s<3;s++){const n=t[s+3]-t[s];n>i&&(i=n,e=s)}return e}function hs(t,e){e.set(t)}function cs(t,e,i){let s,n;for(let r=0;r<3;r++){const o=r+3;s=t[r],n=e[r],i[r]=s<n?s:n,s=t[o],n=e[o],i[o]=s>n?s:n}}function ds(t,e,i){for(let s=0;s<3;s++){const n=e[t+2*s],r=e[t+2*s+1],o=n-r,a=n+r;o<i[s]&&(i[s]=o),a>i[s+3]&&(i[s+3]=a)}}function us(t){const e=t[3]-t[0],i=t[4]-t[1],s=t[5]-t[2];return 2*(e*i+i*s+s*e)}function ps(t,e,i,s,n=null){let r=1/0,o=1/0,a=1/0,l=-1/0,h=-1/0,c=-1/0,d=1/0,u=1/0,p=1/0,f=-1/0,m=-1/0,g=-1/0;const _=null!==n;for(let s=6*e,n=6*(e+i);s<n;s+=6){const e=t[s+0],i=t[s+1],n=e-i,y=e+i;n<r&&(r=n),y>l&&(l=y),_&&e<d&&(d=e),_&&e>f&&(f=e);const w=t[s+2],v=t[s+3],b=w-v,x=w+v;b<o&&(o=b),x>h&&(h=x),_&&w<u&&(u=w),_&&w>m&&(m=w);const E=t[s+4],T=t[s+5],C=E-T,S=E+T;C<a&&(a=C),S>c&&(c=S),_&&E<p&&(p=E),_&&E>g&&(g=E)}s[0]=r,s[1]=o,s[2]=a,s[3]=l,s[4]=h,s[5]=c,_&&(n[0]=d,n[1]=u,n[2]=p,n[3]=f,n[4]=m,n[5]=g)}const fs=32,ms=(t,e)=>t.candidate-e.candidate,gs=new Array(fs).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),_s=new Float32Array(6);class ys{constructor(){}}function ws(t,e,i,s,n,r){let o=s,a=s+n-1;const l=r.pos,h=2*r.axis;for(;;){for(;o<=a&&i[6*o+h]<l;)o++;for(;o<=a&&i[6*a+h]>=l;)a--;if(!(o<a))return o;for(let t=0;t<3;t++){let i=e[3*o+t];e[3*o+t]=e[3*a+t],e[3*a+t]=i}for(let t=0;t<6;t++){let e=i[6*o+t];i[6*o+t]=i[6*a+t],i[6*a+t]=e}o++,a--}}function vs(t,e,i,s,n,r){let o=s,a=s+n-1;const l=r.pos,h=2*r.axis;for(;;){for(;o<=a&&i[6*o+h]<l;)o++;for(;o<=a&&i[6*a+h]>=l;)a--;if(!(o<a))return o;{let e=t[o];t[o]=t[a],t[a]=e;for(let t=0;t<6;t++){let e=i[6*o+t];i[6*o+t]=i[6*a+t],i[6*a+t]=e}o++,a--}}}function bs(t,e){const i=t.geometry,s=i.index?i.index.array:null,n=e.maxDepth,r=e.verbose,o=e.maxLeafTris,a=e.strategy,l=e.onProgress,h=ss(i),c=t._indirectBuffer;let d=!1;const u=new Float32Array(6),p=new Float32Array(6),f=function(t,e){var i;(i=e)[0]=i[1]=i[2]=1/0,i[3]=i[4]=i[5]=-1/0;const s=t.attributes.position,n=t.index?t.index.array:null,r=ss(t),o=new Float32Array(6*r),a=s.normalized,l=s.array,h=s.offset||0;let c=3;s.isInterleavedBufferAttribute&&(c=s.data.stride);const d=["getX","getY","getZ"];for(let t=0;t<r;t++){const i=3*t,r=6*t;let u=i+0,p=i+1,f=i+2;n&&(u=n[u],p=n[p],f=n[f]),a||(u=u*c+h,p=p*c+h,f=f*c+h);for(let t=0;t<3;t++){let i,n,h;a?(i=s[d[t]](u),n=s[d[t]](p),h=s[d[t]](f)):(i=l[u+t],n=l[p+t],h=l[f+t]);let c=i;n<c&&(c=n),h<c&&(c=h);let m=i;n>m&&(m=n),h>m&&(m=h);const g=(m-c)/2,_=2*t;o[r+_+0]=c+g,o[r+_+1]=g+(Math.abs(c)+g)*es,c<e[t]&&(e[t]=c),m>e[t+3]&&(e[t+3]=m)}}return o}(i,u),m=e.indirect?vs:ws,g=[],_=e.indirect?rs(i):os(i);if(1===_.length){const t=_[0],e=new ys;e.boundingData=u,function(t,e,i,s){let n=1/0,r=1/0,o=1/0,a=-1/0,l=-1/0,h=-1/0;for(let s=6*e,c=6*(e+i);s<c;s+=6){const e=t[s+0];e<n&&(n=e),e>a&&(a=e);const i=t[s+2];i<r&&(r=i),i>l&&(l=i);const c=t[s+4];c<o&&(o=c),c>h&&(h=c)}s[0]=n,s[1]=r,s[2]=o,s[3]=a,s[4]=l,s[5]=h}(f,t.offset,t.count,p),w(e,t.offset,t.count,p),g.push(e)}else for(let t of _){const e=new ys;e.boundingData=new Float32Array(6),ps(f,t.offset,t.count,e.boundingData,p),w(e,t.offset,t.count,p),g.push(e)}return g;function y(t){l&&l(t/h)}function w(t,e,l,h=null,u=0){if(!d&&u>=n&&(d=!0,r&&(console.warn(`MeshBVH: Max depth of ${n} reached when generating BVH. Consider increasing maxDepth.`),console.warn(i))),l<=o||u>=n)return y(e+l),t.offset=e,t.count=l,t;const g=function(t,e,i,s,n,r){let o=-1,a=0;if(0===r)o=ls(e),-1!==o&&(a=(e[o]+e[o+3])/2);else if(1===r)o=ls(t),-1!==o&&(a=function(t,e,i,s){let n=0;for(let r=e,o=e+i;r<o;r++)n+=t[6*r+2*s];return n/i}(i,s,n,o));else if(2===r){const r=us(t);let l=Ji*n;const h=6*s,c=6*(s+n);for(let t=0;t<3;t++){const s=e[t],d=(e[t+3]-s)/fs;if(n<8){const e=[...gs];e.length=n;let s=0;for(let n=h;n<c;n+=6,s++){const r=e[s];r.candidate=i[n+2*t],r.count=0;const{bounds:o,leftCacheBounds:a,rightCacheBounds:l}=r;for(let t=0;t<3;t++)l[t]=1/0,l[t+3]=-1/0,a[t]=1/0,a[t+3]=-1/0,o[t]=1/0,o[t+3]=-1/0;ds(n,i,o)}e.sort(ms);let d=n;for(let t=0;t<d;t++){const i=e[t];for(;t+1<d&&e[t+1].candidate===i.candidate;)e.splice(t+1,1),d--}for(let s=h;s<c;s+=6){const n=i[s+2*t];for(let t=0;t<d;t++){const r=e[t];n>=r.candidate?ds(s,i,r.rightCacheBounds):(ds(s,i,r.leftCacheBounds),r.count++)}}for(let i=0;i<d;i++){const s=e[i],h=s.count,c=n-s.count,d=s.leftCacheBounds,u=s.rightCacheBounds;let p=0;0!==h&&(p=us(d)/r);let f=0;0!==c&&(f=us(u)/r);const m=1+Ji*(p*h+f*c);m<l&&(o=t,l=m,a=s.candidate)}}else{for(let t=0;t<fs;t++){const e=gs[t];e.count=0,e.candidate=s+d+t*d;const i=e.bounds;for(let t=0;t<3;t++)i[t]=1/0,i[t+3]=-1/0}for(let e=h;e<c;e+=6){let n=~~((i[e+2*t]-s)/d);n>=fs&&(n=31);const r=gs[n];r.count++,ds(e,i,r.bounds)}const e=gs[31];hs(e.bounds,e.rightCacheBounds);for(let t=30;t>=0;t--){const e=gs[t],i=gs[t+1];cs(e.bounds,i.rightCacheBounds,e.rightCacheBounds)}let u=0;for(let e=0;e<31;e++){const i=gs[e],s=i.count,h=i.bounds,c=gs[e+1].rightCacheBounds;0!==s&&(0===u?hs(h,_s):cs(h,_s,_s)),u+=s;let d=0,p=0;0!==u&&(d=us(_s)/r);const f=n-u;0!==f&&(p=us(c)/r);const m=1+Ji*(d*u+p*f);m<l&&(o=t,l=m,a=i.candidate)}}}}else console.warn(`MeshBVH: Invalid build strategy value ${r} used.`);return{axis:o,pos:a}}(t.boundingData,h,f,e,l,a);if(-1===g.axis)return y(e+l),t.offset=e,t.count=l,t;const _=m(c,s,f,e,l,g);if(_===e||_===e+l)y(e+l),t.offset=e,t.count=l;else{t.splitAxis=g.axis;const i=new ys,s=e,n=_-e;t.left=i,i.boundingData=new Float32Array(6),ps(f,s,n,i.boundingData,p),w(i,s,n,p,u+1);const r=new ys,o=_,a=l-n;t.right=r,r.boundingData=new Float32Array(6),ps(f,o,a,r.boundingData,p),w(r,o,a,p,u+1)}return t}}function xs(t,e){const i=t.geometry;e.indirect&&(t._indirectBuffer=function(t,e){const i=(t.index?t.index.count:t.attributes.position.count)/3,s=i>65536,n=s?4:2,r=e?new SharedArrayBuffer(i*n):new ArrayBuffer(i*n),o=s?new Uint32Array(r):new Uint16Array(r);for(let t=0,e=o.length;t<e;t++)o[t]=t;return o}(i,e.useSharedArrayBuffer),function(t){if(0===t.groups.length)return!1;const e=ss(t),i=os(t).sort((t,e)=>t.offset-e.offset),s=i[i.length-1];s.count=Math.min(e-s.offset,s.count);let n=0;return i.forEach(({count:t})=>n+=t),e!==n}(i)&&!e.verbose&&console.warn('MeshBVH: Provided geometry contains groups that do not fully span the vertex contents while using the "indirect" option. BVH may incorrectly report intersections on unrendered portions of the geometry.')),t._indirectBuffer||ns(i,e);const s=bs(t,e);let n,r,o;const a=[],l=e.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer;for(let t=0;t<s.length;t++){const e=s[t];const i=new l(32*h(e));n=new Float32Array(i),r=new Uint32Array(i),o=new Uint16Array(i),c(0,e),a.push(i)}return void(t._roots=a);function h(t){return t.count?1:1+h(t.left)+h(t.right)}function c(t,e){const i=t/4,s=t/2,a=!!e.count,l=e.boundingData;for(let t=0;t<6;t++)n[i+t]=l[t];if(a){const n=e.offset,a=e.count;return r[i+6]=n,o[s+14]=a,o[s+15]=ts,t+32}{const s=e.left,n=e.right,o=e.splitAxis;let a;if(a=c(t+32,s),a/4>Math.pow(2,32))throw new Error("MeshBVH: Cannot store child pointer greater than 32 bits.");return r[i+6]=a/4,a=c(a,n),r[i+7]=o,a}}}class Es{constructor(){this.min=1/0,this.max=-1/0}setFromPointsField(t,e){let i=1/0,s=-1/0;for(let n=0,r=t.length;n<r;n++){const r=t[n][e];i=r<i?r:i,s=r>s?r:s}this.min=i,this.max=s}setFromPoints(t,e){let i=1/0,s=-1/0;for(let n=0,r=e.length;n<r;n++){const r=e[n],o=t.dot(r);i=o<i?o:i,s=o>s?o:s}this.min=i,this.max=s}isSeparated(t){return this.min>t.max||t.min>this.max}}Es.prototype.setFromBox=function(){const t=new a;return function(e,i){const s=i.min,n=i.max;let r=1/0,o=-1/0;for(let i=0;i<=1;i++)for(let a=0;a<=1;a++)for(let l=0;l<=1;l++){t.x=s.x*i+n.x*(1-i),t.y=s.y*a+n.y*(1-a),t.z=s.z*l+n.z*(1-l);const h=e.dot(t);r=Math.min(h,r),o=Math.max(h,o)}this.min=r,this.max=o}}();const Ts=function(){const t=new a,e=new a,i=new a;return function(s,n,r){const o=s.start,a=t,l=n.start,h=e;i.subVectors(o,l),t.subVectors(s.end,s.start),e.subVectors(n.end,n.start);const c=i.dot(h),d=h.dot(a),u=h.dot(h),p=i.dot(a),f=a.dot(a)*u-d*d;let m,g;m=0!==f?(c*d-p*u)/f:0,g=(c+m*d)/u,r.x=m,r.y=g}}(),Cs=function(){const t=new c,e=new a,i=new a;return function(s,n,r,o){Ts(s,n,t);let a=t.x,l=t.y;if(a>=0&&a<=1&&l>=0&&l<=1)return s.at(a,r),void n.at(l,o);if(a>=0&&a<=1)return l<0?n.at(0,o):n.at(1,o),void s.closestPointToPoint(o,!0,r);if(l>=0&&l<=1)return a<0?s.at(0,r):s.at(1,r),void n.closestPointToPoint(r,!0,o);{let t,h;t=a<0?s.start:s.end,h=l<0?n.start:n.end;const c=e,d=i;return s.closestPointToPoint(h,!0,e),n.closestPointToPoint(t,!0,i),c.distanceToSquared(h)<=d.distanceToSquared(t)?(r.copy(c),void o.copy(h)):(r.copy(t),void o.copy(d))}}}(),Ss=function(){const t=new a,e=new a,i=new l,s=new h;return function(n,r){const{radius:o,center:a}=n,{a:l,b:h,c:c}=r;s.start=l,s.end=h;if(s.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;s.start=l,s.end=c;if(s.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;s.start=h,s.end=c;if(s.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;const d=r.getPlane(i);if(Math.abs(d.distanceToPoint(a))<=o){const t=d.projectPoint(a,e);if(r.containsPoint(t))return!0}return!1}}();function As(t){return Math.abs(t)<1e-15}class Os extends d{constructor(...t){super(...t),this.isExtendedTriangle=!0,this.satAxes=new Array(4).fill().map(()=>new a),this.satBounds=new Array(4).fill().map(()=>new Es),this.points=[this.a,this.b,this.c],this.sphere=new u,this.plane=new l,this.needsUpdate=!0}intersectsSphere(t){return Ss(t,this)}update(){const t=this.a,e=this.b,i=this.c,s=this.points,n=this.satAxes,r=this.satBounds,o=n[0],a=r[0];this.getNormal(o),a.setFromPoints(o,s);const l=n[1],h=r[1];l.subVectors(t,e),h.setFromPoints(l,s);const c=n[2],d=r[2];c.subVectors(e,i),d.setFromPoints(c,s);const u=n[3],p=r[3];u.subVectors(i,t),p.setFromPoints(u,s),this.sphere.setFromPoints(this.points),this.plane.setFromNormalAndCoplanarPoint(o,t),this.needsUpdate=!1}}Os.prototype.closestPointToSegment=function(){const t=new a,e=new a,i=new h;return function(s,n=null,r=null){const{start:o,end:a}=s,l=this.points;let h,c=1/0;for(let o=0;o<3;o++){const a=(o+1)%3;i.start.copy(l[o]),i.end.copy(l[a]),Cs(i,s,t,e),h=t.distanceToSquared(e),h<c&&(c=h,n&&n.copy(t),r&&r.copy(e))}return this.closestPointToPoint(o,t),h=o.distanceToSquared(t),h<c&&(c=h,n&&n.copy(t),r&&r.copy(o)),this.closestPointToPoint(a,t),h=a.distanceToSquared(t),h<c&&(c=h,n&&n.copy(t),r&&r.copy(a)),Math.sqrt(c)}}(),Os.prototype.intersectsTriangle=function(){const t=new Os,e=new Array(3),i=new Array(3),s=new Es,n=new Es,r=new a,o=new a,l=new a,c=new a,d=new a,u=new h,p=new h,f=new h,m=new a;function g(t,e,i){const s=t.points;let n=0,r=-1;for(let t=0;t<3;t++){const{start:a,end:l}=u;a.copy(s[t]),l.copy(s[(t+1)%3]),u.delta(o);const h=As(e.distanceToPoint(a));if(As(e.normal.dot(o))&&h){i.copy(u),n=2;break}const c=e.intersectLine(u,m);if(!c&&h&&m.copy(a),(c||h)&&!As(m.distanceTo(l))){if(n<=1){(1===n?i.start:i.end).copy(m),h&&(r=n)}else if(n>=2){(1===r?i.start:i.end).copy(m),n=2;break}if(n++,2===n&&-1===r)break}}return n}return function(o,a=null,h=!1){this.needsUpdate&&this.update(),o.isExtendedTriangle?o.needsUpdate&&o.update():(t.copy(o),t.update(),o=t);const u=this.plane,m=o.plane;if(Math.abs(u.normal.dot(m.normal))>1-1e-10){const t=this.satBounds,l=this.satAxes;i[0]=o.a,i[1]=o.b,i[2]=o.c;for(let e=0;e<4;e++){const n=t[e],r=l[e];if(s.setFromPoints(r,i),n.isSeparated(s))return!1}const c=o.satBounds,d=o.satAxes;e[0]=this.a,e[1]=this.b,e[2]=this.c;for(let t=0;t<4;t++){const i=c[t],n=d[t];if(s.setFromPoints(n,e),i.isSeparated(s))return!1}for(let t=0;t<4;t++){const o=l[t];for(let t=0;t<4;t++){const a=d[t];if(r.crossVectors(o,a),s.setFromPoints(r,e),n.setFromPoints(r,i),s.isSeparated(n))return!1}}return a&&(h||console.warn("ExtendedTriangle.intersectsTriangle: Triangles are coplanar which does not support an output edge. Setting edge to 0, 0, 0."),a.start.set(0,0,0),a.end.set(0,0,0)),!0}{const t=g(this,m,p);if(1===t&&o.containsPoint(p.end))return a&&(a.start.copy(p.end),a.end.copy(p.end)),!0;if(2!==t)return!1;const e=g(o,u,f);if(1===e&&this.containsPoint(f.end))return a&&(a.start.copy(f.end),a.end.copy(f.end)),!0;if(2!==e)return!1;if(p.delta(l),f.delta(c),l.dot(c)<0){let t=f.start;f.start=f.end,f.end=t}const i=p.start.dot(l),s=p.end.dot(l),n=f.start.dot(l),r=f.end.dot(l);return(i===r||n===s||s<n!==i<r)&&(a&&(d.subVectors(p.start,f.start),d.dot(l)>0?a.start.copy(p.start):a.start.copy(f.start),d.subVectors(p.end,f.end),d.dot(l)<0?a.end.copy(p.end):a.end.copy(f.end)),!0)}}}(),Os.prototype.distanceToPoint=function(){const t=new a;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),Os.prototype.distanceToTriangle=function(){const t=new a,e=new a,i=["a","b","c"],s=new h,n=new h;return function(r,o=null,a=null){const l=o||a?s:null;if(this.intersectsTriangle(r,l))return(o||a)&&(o&&l.getCenter(o),a&&l.getCenter(a)),0;let h=1/0;for(let e=0;e<3;e++){let s;const n=i[e],l=r[n];this.closestPointToPoint(l,t),s=l.distanceToSquared(t),s<h&&(h=s,o&&o.copy(t),a&&a.copy(l));const c=this[n];r.closestPointToPoint(c,t),s=c.distanceToSquared(t),s<h&&(h=s,o&&o.copy(c),a&&a.copy(t))}for(let l=0;l<3;l++){const c=i[l],d=i[(l+1)%3];s.set(this[c],this[d]);for(let l=0;l<3;l++){const c=i[l],d=i[(l+1)%3];n.set(r[c],r[d]),Cs(s,n,t,e);const u=t.distanceToSquared(e);u<h&&(h=u,o&&o.copy(t),a&&a.copy(e))}}return Math.sqrt(h)}}();class Ps{constructor(t,e,i){this.isOrientedBox=!0,this.min=new a,this.max=new a,this.matrix=new p,this.invMatrix=new p,this.points=new Array(8).fill().map(()=>new a),this.satAxes=new Array(3).fill().map(()=>new a),this.satBounds=new Array(3).fill().map(()=>new Es),this.alignedSatBounds=new Array(3).fill().map(()=>new Es),this.needsUpdate=!1,t&&this.min.copy(t),e&&this.max.copy(e),i&&this.matrix.copy(i)}set(t,e,i){this.min.copy(t),this.max.copy(e),this.matrix.copy(i),this.needsUpdate=!0}copy(t){this.min.copy(t.min),this.max.copy(t.max),this.matrix.copy(t.matrix),this.needsUpdate=!0}}Ps.prototype.update=function(){return function(){const t=this.matrix,e=this.min,i=this.max,s=this.points;for(let n=0;n<=1;n++)for(let r=0;r<=1;r++)for(let o=0;o<=1;o++){const a=s[1*n|2*r|4*o];a.x=n?i.x:e.x,a.y=r?i.y:e.y,a.z=o?i.z:e.z,a.applyMatrix4(t)}const n=this.satBounds,r=this.satAxes,o=s[0];for(let t=0;t<3;t++){const e=r[t],i=n[t],a=s[1<<t];e.subVectors(o,a),i.setFromPoints(e,s)}const a=this.alignedSatBounds;a[0].setFromPointsField(s,"x"),a[1].setFromPointsField(s,"y"),a[2].setFromPointsField(s,"z"),this.invMatrix.copy(this.matrix).invert(),this.needsUpdate=!1}}(),Ps.prototype.intersectsBox=function(){const t=new Es;return function(e){this.needsUpdate&&this.update();const i=e.min,s=e.max,n=this.satBounds,r=this.satAxes,o=this.alignedSatBounds;if(t.min=i.x,t.max=s.x,o[0].isSeparated(t))return!1;if(t.min=i.y,t.max=s.y,o[1].isSeparated(t))return!1;if(t.min=i.z,t.max=s.z,o[2].isSeparated(t))return!1;for(let i=0;i<3;i++){const s=r[i],o=n[i];if(t.setFromBox(s,e),o.isSeparated(t))return!1}return!0}}(),Ps.prototype.intersectsTriangle=function(){const t=new Os,e=new Array(3),i=new Es,s=new Es,n=new a;return function(r){this.needsUpdate&&this.update(),r.isExtendedTriangle?r.needsUpdate&&r.update():(t.copy(r),t.update(),r=t);const o=this.satBounds,a=this.satAxes;e[0]=r.a,e[1]=r.b,e[2]=r.c;for(let t=0;t<3;t++){const s=o[t],n=a[t];if(i.setFromPoints(n,e),s.isSeparated(i))return!1}const l=r.satBounds,h=r.satAxes,c=this.points;for(let t=0;t<3;t++){const e=l[t],s=h[t];if(i.setFromPoints(s,c),e.isSeparated(i))return!1}for(let t=0;t<3;t++){const r=a[t];for(let t=0;t<4;t++){const o=h[t];if(n.crossVectors(r,o),i.setFromPoints(n,e),s.setFromPoints(n,c),i.isSeparated(s))return!1}}return!0}}(),Ps.prototype.closestPointToPoint=function(){return function(t,e){return this.needsUpdate&&this.update(),e.copy(t).applyMatrix4(this.invMatrix).clamp(this.min,this.max).applyMatrix4(this.matrix),e}}(),Ps.prototype.distanceToPoint=function(){const t=new a;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),Ps.prototype.distanceToBox=function(){const t=["x","y","z"],e=new Array(12).fill().map(()=>new h),i=new Array(12).fill().map(()=>new h),s=new a,n=new a;return function(r,o=0,a=null,l=null){if(this.needsUpdate&&this.update(),this.intersectsBox(r))return(a||l)&&(r.getCenter(n),this.closestPointToPoint(n,s),r.closestPointToPoint(s,n),a&&a.copy(s),l&&l.copy(n)),0;const h=o*o,c=r.min,d=r.max,u=this.points;let p=1/0;for(let t=0;t<8;t++){const e=u[t];n.copy(e).clamp(c,d);const i=e.distanceToSquared(n);if(i<p&&(p=i,a&&a.copy(e),l&&l.copy(n),i<h))return Math.sqrt(i)}let f=0;for(let s=0;s<3;s++)for(let n=0;n<=1;n++)for(let r=0;r<=1;r++){const o=(s+1)%3,a=(s+2)%3,l=1<<s|n<<o|r<<a,h=u[n<<o|r<<a],p=u[l];e[f].set(h,p);const m=t[s],g=t[o],_=t[a],y=i[f],w=y.start,v=y.end;w[m]=c[m],w[g]=n?c[g]:d[g],w[_]=r?c[_]:d[g],v[m]=d[m],v[g]=n?c[g]:d[g],v[_]=r?c[_]:d[g],f++}for(let t=0;t<=1;t++)for(let e=0;e<=1;e++)for(let i=0;i<=1;i++){n.x=t?d.x:c.x,n.y=e?d.y:c.y,n.z=i?d.z:c.z,this.closestPointToPoint(n,s);const r=n.distanceToSquared(s);if(r<p&&(p=r,a&&a.copy(s),l&&l.copy(n),r<h))return Math.sqrt(r)}for(let t=0;t<12;t++){const r=e[t];for(let t=0;t<12;t++){const e=i[t];Cs(r,e,s,n);const o=s.distanceToSquared(n);if(o<p&&(p=o,a&&a.copy(s),l&&l.copy(n),o<h))return Math.sqrt(o)}}return Math.sqrt(p)}}();class Is{constructor(t){this._getNewPrimitive=t,this._primitives=[]}getPrimitive(){const t=this._primitives;return 0===t.length?this._getNewPrimitive():t.pop()}releasePrimitive(t){this._primitives.push(t)}}class ks extends Is{constructor(){super(()=>new Os)}}const Ds=new ks;function zs(t,e){return 65535===e[t+15]}function Ms(t,e){return e[t+6]}function Bs(t,e){return e[t+14]}function Ns(t){return t+8}function Us(t,e){return e[t+6]}function Ls(t,e){return e[t+7]}function Rs(t){return t}const Fs=new class{constructor(){this.float32Array=null,this.uint16Array=null,this.uint32Array=null;const t=[];let e=null;this.setBuffer=i=>{e&&t.push(e),e=i,this.float32Array=new Float32Array(i),this.uint16Array=new Uint16Array(i),this.uint32Array=new Uint32Array(i)},this.clearBuffer=()=>{e=null,this.float32Array=null,this.uint16Array=null,this.uint32Array=null,0!==t.length&&this.setBuffer(t.pop())}}};let Vs,js;const Zs=[],Hs=new Is(()=>new f);function $s(t,e,i,s,n,r){Vs=Hs.getPrimitive(),js=Hs.getPrimitive(),Zs.push(Vs,js),Fs.setBuffer(t._roots[e]);const o=Ys(0,t.geometry,i,s,n,r);Fs.clearBuffer(),Hs.releasePrimitive(Vs),Hs.releasePrimitive(js),Zs.pop(),Zs.pop();const a=Zs.length;return a>0&&(js=Zs[a-1],Vs=Zs[a-2]),o}function Ys(t,e,i,s,n=null,r=0,o=0){const{float32Array:a,uint16Array:l,uint32Array:h}=Fs;let c=2*t;if(zs(c,l)){const e=Ms(t,h),i=Bs(c,l);return as(t,a,Vs),s(e,i,!1,o,r+t,Vs)}{let c=function(t){const{uint16Array:e,uint32Array:i}=Fs;let s=2*t;for(;!zs(s,e);)s=2*(t=Ns(t));return Ms(t,i)},d=function(t){const{uint16Array:e,uint32Array:i}=Fs;let s=2*t;for(;!zs(s,e);)s=2*(t=Us(t,i));return Ms(t,i)+Bs(s,e)};const u=Ns(t),p=Us(t,h);let f,m,g,_,y=u,w=p;if(n&&(g=Vs,_=js,as(y,a,g),as(w,a,_),f=n(g),m=n(_),m<f)){y=p,w=u;const t=f;f=m,m=t,g=_}g||(g=Vs,as(y,a,g));const v=i(g,zs(2*y,l),f,o+1,r+y);let b;if(2===v){const t=c(y);b=s(t,d(y)-t,!0,o+1,r+y,g)}else b=v&&Ys(y,e,i,s,n,r,o+1);if(b)return!0;_=js,as(w,a,_);const x=i(_,zs(2*w,l),m,o+1,r+w);let E;if(2===x){const t=c(w);E=s(t,d(w)-t,!0,o+1,r+w,_)}else E=x&&Ys(w,e,i,s,n,r,o+1);return!!E}}const Ws=new a,Xs=new a;const qs=new a,Gs=new a,Qs=new a,Ks=new c,Js=new c,tn=new c,en=new a,sn=new a,nn=new a,rn=new a;function on(t,e,i,s,n,r,o,l,h){qs.fromBufferAttribute(e,r),Gs.fromBufferAttribute(e,o),Qs.fromBufferAttribute(e,l);const u=function(t,e,i,s,n,r){let o;return o=r===m?t.intersectTriangle(s,i,e,!0,n):t.intersectTriangle(e,i,s,r!==g,n),null===o?null:{distance:t.origin.distanceTo(n),point:n.clone()}}(t,qs,Gs,Qs,rn,h);if(u){s&&(Ks.fromBufferAttribute(s,r),Js.fromBufferAttribute(s,o),tn.fromBufferAttribute(s,l),u.uv=d.getInterpolation(rn,qs,Gs,Qs,Ks,Js,tn,new c)),n&&(Ks.fromBufferAttribute(n,r),Js.fromBufferAttribute(n,o),tn.fromBufferAttribute(n,l),u.uv1=d.getInterpolation(rn,qs,Gs,Qs,Ks,Js,tn,new c)),i&&(en.fromBufferAttribute(i,r),sn.fromBufferAttribute(i,o),nn.fromBufferAttribute(i,l),u.normal=d.getInterpolation(rn,qs,Gs,Qs,en,sn,nn,new a),u.normal.dot(t.direction)>0&&u.normal.multiplyScalar(-1));const e={a:r,b:o,c:l,normal:new a,materialIndex:0};d.getNormal(qs,Gs,Qs,e.normal),u.face=e,u.faceIndex=r}return u}function an(t,e,i,s,n){const r=3*s;let o=r+0,a=r+1,l=r+2;const h=t.index;t.index&&(o=h.getX(o),a=h.getX(a),l=h.getX(l));const{position:c,normal:d,uv:u,uv1:p}=t.attributes,f=on(i,c,d,u,p,o,a,l,e);return f?(f.faceIndex=s,n&&n.push(f),f):null}function ln(t,e,i,s){const n=t.a,r=t.b,o=t.c;let a=e,l=e+1,h=e+2;i&&(a=i.getX(a),l=i.getX(l),h=i.getX(h)),n.x=s.getX(a),n.y=s.getY(a),n.z=s.getZ(a),r.x=s.getX(l),r.y=s.getY(l),r.z=s.getZ(l),o.x=s.getX(h),o.y=s.getY(h),o.z=s.getZ(h)}function hn(t,e,i,s,n,r,o){const{geometry:a}=i,{index:l}=a,h=a.attributes.position;for(let i=t,a=e+t;i<a;i++){let t;if(t=i,ln(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}function cn(t,e=null){e&&Array.isArray(e)&&(e=new Set(e));const i=t.geometry,s=i.index?i.index.array:null,n=i.attributes.position;let r,o,a,l,h=0;const c=t._roots;for(let t=0,e=c.length;t<e;t++)r=c[t],o=new Uint32Array(r),a=new Uint16Array(r),l=new Float32Array(r),d(0,h),h+=r.byteLength;function d(t,i,r=!1){const h=2*t;if(a[h+15]===ts){const e=o[t+6];let i=1/0,r=1/0,c=1/0,d=-1/0,u=-1/0,p=-1/0;for(let t=3*e,o=3*(e+a[h+14]);t<o;t++){let e=s[t];const o=n.getX(e),a=n.getY(e),l=n.getZ(e);o<i&&(i=o),o>d&&(d=o),a<r&&(r=a),a>u&&(u=a),l<c&&(c=l),l>p&&(p=l)}return(l[t+0]!==i||l[t+1]!==r||l[t+2]!==c||l[t+3]!==d||l[t+4]!==u||l[t+5]!==p)&&(l[t+0]=i,l[t+1]=r,l[t+2]=c,l[t+3]=d,l[t+4]=u,l[t+5]=p,!0)}{const s=t+8,n=o[t+6],a=s+i,h=n+i;let c=r,u=!1,p=!1;e?c||(u=e.has(a),p=e.has(h),c=!u&&!p):(u=!0,p=!0);const f=c||p;let m=!1;(c||u)&&(m=d(s,i,c));let g=!1;f&&(g=d(n,i,c));const _=m||g;if(_)for(let e=0;e<3;e++){const i=s+e,r=n+e,o=l[i],a=l[i+3],h=l[r],c=l[r+3];l[t+e]=o<h?o:h,l[t+e+3]=a>c?a:c}return _}}}const dn=new f;function un(t,e,i,s){return as(t,e,dn),i.intersectBox(dn,s)}function pn(t,e,i,s,n,r,o){const{geometry:a}=i,{index:l}=a,h=a.attributes.position;for(let a=t,c=e+t;a<c;a++){let t;if(t=i.resolveTriangleIndex(a),ln(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}const fn=new a;function mn(t,e,i,s,n){Fs.setBuffer(t._roots[e]),gn(0,t,i,s,n),Fs.clearBuffer()}function gn(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=Fs,l=2*t;if(zs(l,o)){!function(t,e,i,s,n,r){const{geometry:o,_indirectBuffer:a}=t;for(let t=s,a=s+n;t<a;t++)an(o,e,i,t,r)}(e,i,s,Ms(t,a),Bs(l,o),n)}else{const o=Ns(t);un(o,r,s,fn)&&gn(o,e,i,s,n);const l=Us(t,a);un(l,r,s,fn)&&gn(l,e,i,s,n)}}const _n=new a,yn=["x","y","z"];function wn(t,e,i,s){Fs.setBuffer(t._roots[e]);const n=vn(0,t,i,s);return Fs.clearBuffer(),n}function vn(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=Fs;let a=2*t;if(zs(a,r)){return function(t,e,i,s,n){const{geometry:r,_indirectBuffer:o}=t;let a=1/0,l=null;for(let t=s,o=s+n;t<o;t++){let s;s=an(r,e,i,t),s&&s.distance<a&&(l=s,a=s.distance)}return l}(e,i,s,Ms(t,o),Bs(a,r))}{const r=Ls(t,o),a=yn[r],l=s.direction[a]>=0;let h,c;l?(h=Ns(t),c=Us(t,o)):(h=Us(t,o),c=Ns(t));const d=un(h,n,s,_n)?vn(h,e,i,s):null;if(d){const t=d.point[a];if(l?t<=n[c+r]:t>=n[c+r+3])return d}const u=un(c,n,s,_n)?vn(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const bn=new f,xn=new Os,En=new Os,Tn=new p,Cn=new Ps,Sn=new Ps;function An(t,e,i,s){Fs.setBuffer(t._roots[e]);const n=On(0,t,i,s);return Fs.clearBuffer(),n}function On(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=Fs;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),Cn.set(i.boundingBox.min,i.boundingBox.max,s),n=Cn);if(!zs(l,o)){const o=t+8,l=a[t+6];as(o,r,bn);if(n.intersectsBox(bn)&&On(o,e,i,s,n))return!0;as(l,r,bn);return!!(n.intersectsBox(bn)&&On(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=Ms(t,a),f=Bs(l,o);if(Tn.copy(s).invert(),i.boundsTree){as(t,r,Sn),Sn.matrix.copy(Tn),Sn.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>Sn.intersectsBox(t),intersectsTriangle:t=>{t.a.applyMatrix4(s),t.b.applyMatrix4(s),t.c.applyMatrix4(s),t.needsUpdate=!0;for(let e=3*p,i=3*(f+p);e<i;e+=3)if(ln(En,e,h,c),En.needsUpdate=!0,t.intersectsTriangle(En))return!0;return!1}})}for(let t=3*p,e=3*(f+p);t<e;t+=3){ln(xn,t,h,c),xn.a.applyMatrix4(Tn),xn.b.applyMatrix4(Tn),xn.c.applyMatrix4(Tn),xn.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(ln(En,t,d,u),En.needsUpdate=!0,xn.intersectsTriangle(En))return!0}}}const Pn=new p,In=new Ps,kn=new Ps,Dn=new a,zn=new a,Mn=new a,Bn=new a;function Nn(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),In.set(e.boundingBox.min,e.boundingBox.max,i),In.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=Ds.getPrimitive(),p=Ds.getPrimitive();let f=Dn,m=zn,g=null,_=null;n&&(g=Mn,_=Bn);let y=1/0,w=null,v=null;return Pn.copy(i).invert(),kn.matrix.copy(Pn),t.shapecast({boundsTraverseOrder:t=>In.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(kn.min.copy(t.min),kn.max.copy(t.max),kn.needsUpdate=!0),!0),intersectsRange:(t,s)=>{if(e.boundsTree){return e.boundsTree.shapecast({boundsTraverseOrder:t=>kn.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o,intersectsRange:(e,n)=>{for(let o=e,a=e+n;o<a;o++){ln(p,3*o,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=t,i=t+s;e<i;e++){ln(u,3*e,h,l),u.needsUpdate=!0;const t=u.distanceToTriangle(p,f,g);if(t<y&&(m.copy(f),_&&_.copy(g),y=t,w=e,v=o),t<r)return!0}}}})}for(let n=0,o=ss(e);n<o;n++){ln(p,3*n,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=t,i=t+s;e<i;e++){ln(u,3*e,h,l),u.needsUpdate=!0;const t=u.distanceToTriangle(p,f,g);if(t<y&&(m.copy(f),_&&_.copy(g),y=t,w=e,v=n),t<r)return!0}}}}),Ds.releasePrimitive(u),Ds.releasePrimitive(p),y===1/0?null:(s.point?s.point.copy(m):s.point=m.clone(),s.distance=y,s.faceIndex=w,n&&(n.point?n.point.copy(_):n.point=_.clone(),n.point.applyMatrix4(Pn),m.applyMatrix4(Pn),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}function Un(t,e=null){e&&Array.isArray(e)&&(e=new Set(e));const i=t.geometry,s=i.index?i.index.array:null,n=i.attributes.position;let r,o,a,l,h=0;const c=t._roots;for(let t=0,e=c.length;t<e;t++)r=c[t],o=new Uint32Array(r),a=new Uint16Array(r),l=new Float32Array(r),d(0,h),h+=r.byteLength;function d(i,r,h=!1){const c=2*i;if(a[c+15]===ts){const e=o[i+6];let r=1/0,h=1/0,d=1/0,u=-1/0,p=-1/0,f=-1/0;for(let i=e,o=e+a[c+14];i<o;i++){const e=3*t.resolveTriangleIndex(i);for(let t=0;t<3;t++){let i=e+t;i=s?s[i]:i;const o=n.getX(i),a=n.getY(i),l=n.getZ(i);o<r&&(r=o),o>u&&(u=o),a<h&&(h=a),a>p&&(p=a),l<d&&(d=l),l>f&&(f=l)}}return(l[i+0]!==r||l[i+1]!==h||l[i+2]!==d||l[i+3]!==u||l[i+4]!==p||l[i+5]!==f)&&(l[i+0]=r,l[i+1]=h,l[i+2]=d,l[i+3]=u,l[i+4]=p,l[i+5]=f,!0)}{const t=i+8,s=o[i+6],n=t+r,a=s+r;let c=h,u=!1,p=!1;e?c||(u=e.has(n),p=e.has(a),c=!u&&!p):(u=!0,p=!0);const f=c||p;let m=!1;(c||u)&&(m=d(t,r,c));let g=!1;f&&(g=d(s,r,c));const _=m||g;if(_)for(let e=0;e<3;e++){const n=t+e,r=s+e,o=l[n],a=l[n+3],h=l[r],c=l[r+3];l[i+e]=o<h?o:h,l[i+e+3]=a>c?a:c}return _}}}const Ln=new a;function Rn(t,e,i,s,n){Fs.setBuffer(t._roots[e]),Fn(0,t,i,s,n),Fs.clearBuffer()}function Fn(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=Fs,l=2*t;if(zs(l,o)){!function(t,e,i,s,n,r){const{geometry:o,_indirectBuffer:a}=t;for(let t=s,l=s+n;t<l;t++)an(o,e,i,a?a[t]:t,r)}(e,i,s,Ms(t,a),Bs(l,o),n)}else{const o=Ns(t);un(o,r,s,Ln)&&Fn(o,e,i,s,n);const l=Us(t,a);un(l,r,s,Ln)&&Fn(l,e,i,s,n)}}const Vn=new a,jn=["x","y","z"];function Zn(t,e,i,s){Fs.setBuffer(t._roots[e]);const n=Hn(0,t,i,s);return Fs.clearBuffer(),n}function Hn(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=Fs;let a=2*t;if(zs(a,r)){return function(t,e,i,s,n){const{geometry:r,_indirectBuffer:o}=t;let a=1/0,l=null;for(let t=s,h=s+n;t<h;t++){let s;s=an(r,e,i,o?o[t]:t),s&&s.distance<a&&(l=s,a=s.distance)}return l}(e,i,s,Ms(t,o),Bs(a,r))}{const r=Ls(t,o),a=jn[r],l=s.direction[a]>=0;let h,c;l?(h=Ns(t),c=Us(t,o)):(h=Us(t,o),c=Ns(t));const d=un(h,n,s,Vn)?Hn(h,e,i,s):null;if(d){const t=d.point[a];if(l?t<=n[c+r]:t>=n[c+r+3])return d}const u=un(c,n,s,Vn)?Hn(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const $n=new f,Yn=new Os,Wn=new Os,Xn=new p,qn=new Ps,Gn=new Ps;function Qn(t,e,i,s){Fs.setBuffer(t._roots[e]);const n=Kn(0,t,i,s);return Fs.clearBuffer(),n}function Kn(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=Fs;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),qn.set(i.boundingBox.min,i.boundingBox.max,s),n=qn);if(!zs(l,o)){const o=t+8,l=a[t+6];as(o,r,$n);if(n.intersectsBox($n)&&Kn(o,e,i,s,n))return!0;as(l,r,$n);return!!(n.intersectsBox($n)&&Kn(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=Ms(t,a),f=Bs(l,o);if(Xn.copy(s).invert(),i.boundsTree){as(t,r,Gn),Gn.matrix.copy(Xn),Gn.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>Gn.intersectsBox(t),intersectsTriangle:t=>{t.a.applyMatrix4(s),t.b.applyMatrix4(s),t.c.applyMatrix4(s),t.needsUpdate=!0;for(let i=p,s=f+p;i<s;i++)if(ln(Wn,3*e.resolveTriangleIndex(i),h,c),Wn.needsUpdate=!0,t.intersectsTriangle(Wn))return!0;return!1}})}for(let t=p,i=f+p;t<i;t++){const i=e.resolveTriangleIndex(t);ln(Yn,3*i,h,c),Yn.a.applyMatrix4(Xn),Yn.b.applyMatrix4(Xn),Yn.c.applyMatrix4(Xn),Yn.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(ln(Wn,t,d,u),Wn.needsUpdate=!0,Yn.intersectsTriangle(Wn))return!0}}}const Jn=new p,tr=new Ps,er=new Ps,ir=new a,sr=new a,nr=new a,rr=new a;function or(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),tr.set(e.boundingBox.min,e.boundingBox.max,i),tr.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=Ds.getPrimitive(),p=Ds.getPrimitive();let f=ir,m=sr,g=null,_=null;n&&(g=nr,_=rr);let y=1/0,w=null,v=null;return Jn.copy(i).invert(),er.matrix.copy(Jn),t.shapecast({boundsTraverseOrder:t=>tr.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(er.min.copy(t.min),er.max.copy(t.max),er.needsUpdate=!0),!0),intersectsRange:(s,n)=>{if(e.boundsTree){const a=e.boundsTree;return a.shapecast({boundsTraverseOrder:t=>er.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o,intersectsRange:(e,o)=>{for(let b=e,x=e+o;b<x;b++){const e=a.resolveTriangleIndex(b);ln(p,3*e,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=s,i=s+n;e<i;e++){const i=t.resolveTriangleIndex(e);ln(u,3*i,h,l),u.needsUpdate=!0;const s=u.distanceToTriangle(p,f,g);if(s<y&&(m.copy(f),_&&_.copy(g),y=s,w=e,v=b),s<r)return!0}}}})}for(let o=0,a=ss(e);o<a;o++){ln(p,3*o,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=s,i=s+n;e<i;e++){const i=t.resolveTriangleIndex(e);ln(u,3*i,h,l),u.needsUpdate=!0;const s=u.distanceToTriangle(p,f,g);if(s<y&&(m.copy(f),_&&_.copy(g),y=s,w=e,v=o),s<r)return!0}}}}),Ds.releasePrimitive(u),Ds.releasePrimitive(p),y===1/0?null:(s.point?s.point.copy(m):s.point=m.clone(),s.distance=y,s.faceIndex=w,n&&(n.point?n.point.copy(_):n.point=_.clone(),n.point.applyMatrix4(Jn),m.applyMatrix4(Jn),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}const ar=new Fs.constructor,lr=new Fs.constructor,hr=new Is(()=>new f),cr=new f,dr=new f,ur=new f,pr=new f;let fr=!1;function mr(t,e,i,s,n,r=0,o=0,a=0,l=0,h=null,c=!1){let d,u;c?(d=lr,u=ar):(d=ar,u=lr);const p=d.float32Array,f=d.uint32Array,m=d.uint16Array,g=u.float32Array,_=u.uint32Array,y=u.uint16Array,w=2*e,v=zs(2*t,m),b=zs(w,y);let x=!1;if(b&&v)x=c?n(Ms(e,_),Bs(2*e,y),Ms(t,f),Bs(2*t,m),l,o+e,a,r+t):n(Ms(t,f),Bs(2*t,m),Ms(e,_),Bs(2*e,y),a,r+t,l,o+e);else if(b){const h=hr.getPrimitive();as(e,g,h),h.applyMatrix4(i);const d=Ns(t),u=Us(t,f);as(d,p,cr),as(u,p,dr);const m=h.intersectsBox(cr),_=h.intersectsBox(dr);x=m&&mr(e,d,s,i,n,o,r,l,a+1,h,!c)||_&&mr(e,u,s,i,n,o,r,l,a+1,h,!c),hr.releasePrimitive(h)}else{const d=Ns(e),u=Us(e,_);as(d,g,ur),as(u,g,pr);const m=h.intersectsBox(ur),y=h.intersectsBox(pr);if(m&&y)x=mr(t,d,i,s,n,r,o,a,l+1,h,c)||mr(t,u,i,s,n,r,o,a,l+1,h,c);else if(m)if(v)x=mr(t,d,i,s,n,r,o,a,l+1,h,c);else{const e=hr.getPrimitive();e.copy(ur).applyMatrix4(i);const h=Ns(t),u=Us(t,f);as(h,p,cr),as(u,p,dr);const m=e.intersectsBox(cr),g=e.intersectsBox(dr);x=m&&mr(d,h,s,i,n,o,r,l,a+1,e,!c)||g&&mr(d,u,s,i,n,o,r,l,a+1,e,!c),hr.releasePrimitive(e)}else if(y)if(v)x=mr(t,u,i,s,n,r,o,a,l+1,h,c);else{const e=hr.getPrimitive();e.copy(pr).applyMatrix4(i);const h=Ns(t),d=Us(t,f);as(h,p,cr),as(d,p,dr);const m=e.intersectsBox(cr),g=e.intersectsBox(dr);x=m&&mr(u,h,s,i,n,o,r,l,a+1,e,!c)||g&&mr(u,d,s,i,n,o,r,l,a+1,e,!c),hr.releasePrimitive(e)}}return x}const gr=new Ps,_r=new f;class yr{static serialize(t,e={}){e={cloneBuffers:!0,...e};const i=t.geometry,s=t._roots,n=t._indirectBuffer,r=i.getIndex();let o;return o=e.cloneBuffers?{roots:s.map(t=>t.slice()),index:r.array.slice(),indirectBuffer:n?n.slice():null}:{roots:s,index:r.array,indirectBuffer:n},o}static deserialize(t,e,i={}){i={setIndex:!0,indirect:Boolean(t.indirectBuffer),...i};const{index:s,roots:n,indirectBuffer:r}=t,a=new yr(e,{...i,[is]:!0});if(a._roots=n,a._indirectBuffer=r||null,i.setIndex){const i=e.getIndex();if(null===i){const i=new o(t.index,1,!1);e.setIndex(i)}else i.array!==s&&(i.array.set(s),i.needsUpdate=!0)}return a}get indirect(){return!!this._indirectBuffer}constructor(t,e={}){if(!t.isBufferGeometry)throw new Error("MeshBVH: Only BufferGeometries are supported.");if(t.index&&t.index.isInterleavedBufferAttribute)throw new Error("MeshBVH: InterleavedBufferAttribute is not supported for the index attribute.");if((e=Object.assign({strategy:0,maxDepth:40,maxLeafTris:10,verbose:!0,useSharedArrayBuffer:!1,setBoundingBox:!0,onProgress:null,indirect:!1,[is]:!1},e)).useSharedArrayBuffer&&"undefined"==typeof SharedArrayBuffer)throw new Error("MeshBVH: SharedArrayBuffer is not available.");this.geometry=t,this._roots=null,this._indirectBuffer=null,e[is]||(xs(this,e),!t.boundingBox&&e.setBoundingBox&&(t.boundingBox=this.getBoundingBox(new f)));const{_indirectBuffer:i}=this;this.resolveTriangleIndex=e.indirect?t=>i[t]:t=>t}refit(t=null){return(this.indirect?Un:cn)(this,t)}traverse(t,e=0){const i=this._roots[e],s=new Uint32Array(i),n=new Uint16Array(i);!function e(r,o=0){const a=2*r,l=n[a+15]===ts;if(l){const e=s[r+6],h=n[a+14];t(o,l,new Float32Array(i,4*r,6),e,h)}else{const n=r+8,a=s[r+6],h=s[r+7];t(o,l,new Float32Array(i,4*r,6),h)||(e(n,o+1),e(a,o+1))}}(0)}raycast(t,e=_){const i=this._roots,s=this.geometry,n=[],r=e.isMaterial,o=Array.isArray(e),a=s.groups,l=r?e.side:e,h=this.indirect?Rn:mn;for(let s=0,r=i.length;s<r;s++){const i=o?e[a[s].materialIndex].side:l,r=n.length;if(h(this,s,i,t,n),o){const t=a[s].materialIndex;for(let e=r,i=n.length;e<i;e++)n[e].face.materialIndex=t}}return n}raycastFirst(t,e=_){const i=this._roots,s=this.geometry,n=e.isMaterial,r=Array.isArray(e);let o=null;const a=s.groups,l=n?e.side:e,h=this.indirect?Zn:wn;for(let s=0,n=i.length;s<n;s++){const i=h(this,s,r?e[a[s].materialIndex].side:l,t);null!=i&&(null==o||i.distance<o.distance)&&(o=i,r&&(i.face.materialIndex=a[s].materialIndex))}return o}intersectsGeometry(t,e){let i=!1;const s=this._roots,n=this.indirect?Qn:An;for(let r=0,o=s.length;r<o&&(i=n(this,r,t,e),!i);r++);return i}shapecast(t){const e=Ds.getPrimitive(),i=this.indirect?pn:hn;let{boundsTraverseOrder:s,intersectsBounds:n,intersectsRange:r,intersectsTriangle:o}=t;if(r&&o){const t=r;r=(s,n,r,a,l)=>!!t(s,n,r,a,l)||i(s,n,this,o,r,a,e)}else r||(r=o?(t,s,n,r)=>i(t,s,this,o,n,r,e):(t,e,i)=>i);let a=!1,l=0;const h=this._roots;for(let t=0,e=h.length;t<e;t++){const e=h[t];if(a=$s(this,t,n,r,s,l),a)break;l+=e.byteLength}return Ds.releasePrimitive(e),a}bvhcast(t,e,i){let{intersectsRanges:s,intersectsTriangles:n}=i;const r=Ds.getPrimitive(),o=this.geometry.index,a=this.geometry.attributes.position,l=this.indirect?t=>{const e=this.resolveTriangleIndex(t);ln(r,3*e,o,a)}:t=>{ln(r,3*t,o,a)},h=Ds.getPrimitive(),c=t.geometry.index,d=t.geometry.attributes.position,u=t.indirect?e=>{const i=t.resolveTriangleIndex(e);ln(h,3*i,c,d)}:t=>{ln(h,3*t,c,d)};if(n){const t=(t,i,s,o,a,c,d,p)=>{for(let f=s,m=s+o;f<m;f++){u(f),h.a.applyMatrix4(e),h.b.applyMatrix4(e),h.c.applyMatrix4(e),h.needsUpdate=!0;for(let e=t,s=t+i;e<s;e++)if(l(e),r.needsUpdate=!0,n(r,h,e,f,a,c,d,p))return!0}return!1};if(s){const e=s;s=function(i,s,n,r,o,a,l,h){return!!e(i,s,n,r,o,a,l,h)||t(i,s,n,r,o,a,l,h)}}else s=t}return function(t,e,i,s){if(fr)throw new Error("MeshBVH: Recursive calls to bvhcast not supported.");fr=!0;const n=t._roots,r=e._roots;let o,a=0,l=0;const h=(new p).copy(i).invert();for(let t=0,e=n.length;t<e;t++){ar.setBuffer(n[t]),l=0;const e=hr.getPrimitive();as(Rs(0),ar.float32Array,e),e.applyMatrix4(h);for(let n=0,c=r.length;n<c&&(lr.setBuffer(r[t]),o=mr(0,0,i,h,s,a,l,0,0,e),lr.clearBuffer(),l+=r[n].length,!o);n++);if(hr.releasePrimitive(e),ar.clearBuffer(),a+=n[t].length,o)break}return fr=!1,o}(this,t,e,s)}intersectsBox(t,e){return gr.set(t.min,t.max,e),gr.needsUpdate=!0,this.shapecast({intersectsBounds:t=>gr.intersectsBox(t),intersectsTriangle:t=>gr.intersectsTriangle(t)})}intersectsSphere(t){return this.shapecast({intersectsBounds:e=>t.intersectsBox(e),intersectsTriangle:e=>e.intersectsSphere(t)})}closestPointToGeometry(t,e,i={},s={},n=0,r=1/0){return(this.indirect?or:Nn)(this,t,e,i,s,n,r)}closestPointToPoint(t,e={},i=0,s=1/0){return function(t,e,i={},s=0,n=1/0){const r=s*s,o=n*n;let a=1/0,l=null;if(t.shapecast({boundsTraverseOrder:t=>(Ws.copy(e).clamp(t.min,t.max),Ws.distanceToSquared(e)),intersectsBounds:(t,e,i)=>i<a&&i<o,intersectsTriangle:(t,i)=>{t.closestPointToPoint(e,Ws);const s=e.distanceToSquared(Ws);return s<a&&(Xs.copy(Ws),a=s,l=i),s<r}}),a===1/0)return null;const h=Math.sqrt(a);return i.point?i.point.copy(Xs):i.point=Xs.clone(),i.distance=h,i.faceIndex=l,i}(this,t,e,i,s)}getBoundingBox(t){t.makeEmpty();return this._roots.forEach(e=>{as(0,new Float32Array(e),_r),t.union(_r)}),t}}function wr(t,e,i){return null===t?null:(t.point.applyMatrix4(e.matrixWorld),t.distance=t.point.distanceTo(i.ray.origin),t.object=e,t.distance<i.near||t.distance>i.far?null:t)}const vr=new y,br=new p,xr=w.prototype.raycast;function Er(t,e){if(this.geometry.boundsTree){if(void 0===this.material)return;br.copy(this.matrixWorld).invert(),vr.copy(t.ray).applyMatrix4(br);const i=this.geometry.boundsTree;if(!0===t.firstHitOnly){const s=wr(i.raycastFirst(vr,this.material),this,t);s&&e.push(s)}else{const s=i.raycast(vr,this.material);for(let i=0,n=s.length;i<n;i++){const n=wr(s[i],this,t);n&&e.push(n)}}}else xr.call(this,t,e)}function Tr(t){return this.boundsTree=new yr(this,t),this.boundsTree}function Cr(){this.boundsTree=null}const Sr=class t{constructor(){e(this,"onDisposed",new U),e(this,"list",new s),e(this,"enabled",!1),e(this,"_clock"),e(this,"onInit",new U),e(this,"update",()=>{if(!this.enabled)return;const t=this._clock.getDelta();for(const[e,i]of this.list)i.enabled&&i.isUpdateable()&&i.update(t);requestAnimationFrame(this.update)}),this._clock=new r.Clock,t.setupBVH()}add(t,e){if(this.list.has(t))throw new Error("You're trying to add a component that already exists in the components instance. Use Components.get() instead.");q.validate(t),this.list.set(t,e)}get(t){const e=t.uuid;if(!this.list.has(e)){const i=new t(this);return i.isDisposeable()&&i.onDisposed.add(()=>this.list.delete(e)),this.list.has(e)||this.add(e,i),i}return this.list.get(e)}init(){this.enabled=!0;for(const[t,e]of this.list.entries())e.enabled=!0;this._clock.start(),this.update(),this.onInit.trigger()}dispose(){let t;this.enabled=!1;for(const[e,i]of this.list)i.enabled=!1,e!==Ae.uuid?i.isDisposeable()&&i.dispose():t=i;null==t||t.dispose(),this._clock.stop(),this.onDisposed.trigger()}static setupBVH(){r.BufferGeometry.prototype.computeBoundsTree=Tr,r.BufferGeometry.prototype.disposeBoundsTree=Cr,r.Mesh.prototype.raycast=Er}};e(Sr,"release","2.4.3");let Ar=Sr;class Or{constructor(t){e(this,"enabled",!1),e(this,"id","FirstPerson"),this.camera=t}set(t){if(this.enabled=t,t){if("Perspective"!==this.camera.projection.current)return void this.camera.set("Orbit");this.setupFirstPersonCamera()}}setupFirstPersonCamera(){const t=this.camera.controls,e=new r.Vector3;t.distance--,t.getPosition(e),t.minDistance=1,t.maxDistance=1,t.distance=1,t.moveTo(e.x,e.y,e.z),t.truckSpeed=50,t.mouseButtons.wheel=$i.ACTION.DOLLY,t.touches.two=$i.ACTION.TOUCH_ZOOM_TRUCK}}class Pr{constructor(t){e(this,"enabled",!0),e(this,"id","Orbit"),this.camera=t,this.activateOrbitControls()}set(t){this.enabled=t,t&&this.activateOrbitControls()}activateOrbitControls(){const t=this.camera.controls;t.minDistance=1,t.maxDistance=300;const e=new r.Vector3;t.getPosition(e);const i=e.length();t.distance=i,t.truckSpeed=2;const{rotation:s}=this.camera.three,n=new r.Vector3(0,0,-1).applyEuler(s),o=e.addScaledVector(n,i);t.moveTo(o.x,o.y,o.z)}}class Ir{constructor(t){e(this,"enabled",!1),e(this,"id","Plan"),e(this,"mouseAction1"),e(this,"mouseAction2"),e(this,"mouseInitialized",!1),e(this,"defaultAzimuthSpeed"),e(this,"defaultPolarSpeed"),this.camera=t,this.defaultAzimuthSpeed=t.controls.azimuthRotateSpeed,this.defaultPolarSpeed=t.controls.polarRotateSpeed}set(t){this.enabled=t;const e=this.camera.controls;e.azimuthRotateSpeed=t?0:this.defaultAzimuthSpeed,e.polarRotateSpeed=t?0:this.defaultPolarSpeed,this.mouseInitialized||(this.mouseAction1=e.touches.one,this.mouseAction2=e.touches.two,this.mouseInitialized=!0),t?(e.mouseButtons.left=$i.ACTION.TRUCK,e.touches.one=$i.ACTION.TOUCH_TRUCK,e.touches.two=$i.ACTION.TOUCH_ZOOM):(e.mouseButtons.left=$i.ACTION.ROTATE,e.touches.one=this.mouseAction1,e.touches.two=this.mouseAction2)}}class kr{constructor(t){e(this,"onChanged",new U),e(this,"current","Perspective"),e(this,"camera"),e(this,"matchOrthoDistanceEnabled",!1),e(this,"_component"),e(this,"_previousDistance",-1),this._component=t,this.camera=t.three}async set(t){this.current!==t&&("Orthographic"===t?this.setOrthoCamera():await this.setPerspectiveCamera(),this.onChanged.trigger(this.camera))}async toggle(){const t="Perspective"===this.current?"Orthographic":"Perspective";await this.set(t)}setOrthoCamera(){if(null===this._component.mode)return;if("FirstPerson"===this._component.mode.id)return;this._previousDistance=this._component.controls.distance,this._component.controls.distance=200;const t=this.getPerspectiveDims();if(!t)return;const{width:e,height:i}=t;this.setupOrthoCamera(i,e),this.camera=this._component.threeOrtho,this.current="Orthographic"}getPerspectiveDims(){const t=this._component.currentWorld;if(!t||!t.renderer)return null;const e=new r.Vector3;this._component.threePersp.getWorldDirection(e);const i=new r.Vector3;this._component.controls.getTarget(i);const s=i.clone().sub(this._component.threePersp.position).dot(e),n=t.renderer.getSize(),o=n.x/n.y,a=this._component.threePersp,l=2*s*Math.atan(a.fov*(Math.PI/180)/2);return{width:l*o,height:l}}setupOrthoCamera(t,e){this._component.controls.mouseButtons.wheel=$i.ACTION.ZOOM,this._component.controls.mouseButtons.middle=$i.ACTION.ZOOM;const i=this._component.threePersp,s=this._component.threeOrtho;s.zoom=1,s.left=e/-2,s.right=e/2,s.top=t/2,s.bottom=t/-2,s.updateProjectionMatrix(),s.position.copy(i.position),s.quaternion.copy(i.quaternion),this._component.controls.camera=s}getDistance(){const t=this._component.threePersp,e=this._component.threeOrtho;return(e.top-e.bottom)/e.zoom/(2*Math.atan(t.fov*(Math.PI/180)/2))}async setPerspectiveCamera(){this._component.controls.mouseButtons.wheel=$i.ACTION.DOLLY,this._component.controls.mouseButtons.middle=$i.ACTION.DOLLY;const t=this._component.threePersp,e=this._component.threeOrtho;t.position.copy(e.position),t.quaternion.copy(e.quaternion),this._component.controls.mouseButtons.wheel=$i.ACTION.DOLLY,this.matchOrthoDistanceEnabled?this._component.controls.distance=this.getDistance():this._component.controls.distance=this._previousDistance,await this._component.controls.zoomTo(1),t.updateProjectionMatrix(),this._component.controls.camera=t,this.camera=t,this.current="Perspective"}}class Dr extends Yi{constructor(t){super(t),e(this,"projection"),e(this,"threeOrtho"),e(this,"threePersp"),e(this,"_userInputButtons",{}),e(this,"_frustumSize",50),e(this,"_navigationModes",new Map),e(this,"_mode",null),e(this,"previousSize",null),this.threePersp=this.three,this.threeOrtho=this.newOrthoCamera(),this.projection=new kr(this),this.onAspectUpdated.add(()=>{this.setOrthoPerspCameraAspect()}),this.projection.onChanged.add(t=>{this.three=t,this.updateAspect()}),this.worlds.onItemSet.add(()=>{this._navigationModes.clear(),this._navigationModes.set("Orbit",new Pr(this)),this._navigationModes.set("FirstPerson",new Or(this)),this._navigationModes.set("Plan",new Ir(this)),this._mode=this._navigationModes.get("Orbit"),this.mode.set(!0,{preventTargetAdjustment:!0}),this.currentWorld&&this.currentWorld.renderer&&(this.previousSize=this.currentWorld.renderer.getSize().clone())}),this.worlds.onItemDeleted.add(()=>{this._navigationModes.clear()})}get mode(){if(!this._mode)throw new Error("Mode not found, camera not initialized");return this._mode}dispose(){super.dispose(),this.threeOrtho.removeFromParent()}set(t){if(null!==this.mode&&this.mode.id!==t){if(this.mode.set(!1),!this._navigationModes.has(t))throw new Error("The specified mode does not exist!");this._mode=this._navigationModes.get(t),this.mode.set(!0)}}async fit(t,e=1.5){if(!this.enabled)return;const i=Number.MAX_VALUE,s=Number.MIN_VALUE,n=new r.Vector3(i,i,i),o=new r.Vector3(s,s,s);for(const e of t){const t=(new r.Box3).setFromObject(e);t.min.x<n.x&&(n.x=t.min.x),t.min.y<n.y&&(n.y=t.min.y),t.min.z<n.z&&(n.z=t.min.z),t.max.x>o.x&&(o.x=t.max.x),t.max.y>o.y&&(o.y=t.max.y),t.max.z>o.z&&(o.z=t.max.z)}const a=new r.Box3(n,o),l=this.components.get(Ae);if(l.initialized)for(const[,t]of l.list){const e=t.box;e.min.x<n.x&&(n.x=e.min.x),e.min.y<n.y&&(n.y=e.min.y),e.min.z<n.z&&(n.z=e.min.z),e.max.x>o.x&&(o.x=e.max.x),e.max.y>o.y&&(o.y=e.max.y),e.max.z>o.z&&(o.z=e.max.z)}const h=new r.Vector3;a.getSize(h);const c=new r.Vector3;a.getCenter(c);const d=Math.max(h.x,h.y,h.z)*e,u=new r.Sphere(c,d);await this.controls.fitToSphere(u,!0)}setUserInput(t){t?this.enableUserInput():this.disableUserInput()}disableUserInput(){this._userInputButtons.left=this.controls.mouseButtons.left,this._userInputButtons.right=this.controls.mouseButtons.right,this._userInputButtons.middle=this.controls.mouseButtons.middle,this._userInputButtons.wheel=this.controls.mouseButtons.wheel,this.controls.mouseButtons.left=0,this.controls.mouseButtons.right=0,this.controls.mouseButtons.middle=0,this.controls.mouseButtons.wheel=0}enableUserInput(){0!==Object.keys(this._userInputButtons).length&&(this.controls.mouseButtons.left=this._userInputButtons.left,this.controls.mouseButtons.right=this._userInputButtons.right,this.controls.mouseButtons.middle=this._userInputButtons.middle,this.controls.mouseButtons.wheel=this._userInputButtons.wheel)}newOrthoCamera(){const t=window.innerWidth/window.innerHeight;return new r.OrthographicCamera(this._frustumSize*t/-2,this._frustumSize*t/2,this._frustumSize/2,this._frustumSize/-2,.1,1e3)}setOrthoPerspCameraAspect(){if(!this.currentWorld||!this.currentWorld.renderer)return;if(!this.previousSize)return;const t=this.currentWorld.renderer.getSize(),e=this.threeOrtho.top,i=this.threeOrtho.right,s=e*(t.y/this.previousSize.y),n=i*(t.x/this.previousSize.x);this.threeOrtho.left=-n,this.threeOrtho.right=n,this.threeOrtho.top=s,this.threeOrtho.bottom=-s,this.threeOrtho.updateProjectionMatrix(),this.previousSize.copy(t)}}function zr(t){throw new Error('Could not dynamically require "'+t+'". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.')}var Mr={exports:{}};
7
+ */const Ge=1,Qe=2,Ke=4,Je=Object.freeze({NONE:0,ROTATE:1,TRUCK:2,SCREEN_PAN:4,OFFSET:8,DOLLY:16,ZOOM:32,TOUCH_ROTATE:64,TOUCH_TRUCK:128,TOUCH_SCREEN_PAN:256,TOUCH_OFFSET:512,TOUCH_DOLLY:1024,TOUCH_ZOOM:2048,TOUCH_DOLLY_TRUCK:4096,TOUCH_DOLLY_SCREEN_PAN:8192,TOUCH_DOLLY_OFFSET:16384,TOUCH_DOLLY_ROTATE:32768,TOUCH_ZOOM_TRUCK:65536,TOUCH_ZOOM_OFFSET:131072,TOUCH_ZOOM_SCREEN_PAN:262144,TOUCH_ZOOM_ROTATE:524288}),ti=0,ei=1,ii=-1;function si(t){return t.isPerspectiveCamera}function ni(t){return t.isOrthographicCamera}const ri=2*Math.PI,oi=Math.PI/2,ai=Math.PI/180;function li(t,e,i){return Math.max(e,Math.min(i,t))}function hi(t,e=1e-5){return Math.abs(t)<e}function ci(t,e,i=1e-5){return hi(t-e,i)}function di(t,e){return Math.round(t/e)*e}function ui(t){return isFinite(t)?t:t<0?-Number.MAX_VALUE:Number.MAX_VALUE}function pi(t){return Math.abs(t)<Number.MAX_VALUE?t:t*(1/0)}function fi(t,e,i,s,n=1/0,r){const o=2/(s=Math.max(1e-4,s)),a=o*r,l=1/(1+a+.48*a*a+.235*a*a*a);let h=t-e;const c=e,d=n*s;h=li(h,-d,d),e=t-h;const u=(i.value+o*h)*r;i.value=(i.value-o*u)*l;let p=e+(h+u)*l;return c-t>0==p>c&&(p=c,i.value=(p-c)/r),p}function mi(t,e,i,s,n=1/0,r,o){const a=2/(s=Math.max(1e-4,s)),l=a*r,h=1/(1+l+.48*l*l+.235*l*l*l);let c=e.x,d=e.y,u=e.z,p=t.x-c,f=t.y-d,m=t.z-u;const g=c,_=d,y=u,w=n*s,v=p*p+f*f+m*m;if(v>w*w){const t=Math.sqrt(v);p=p/t*w,f=f/t*w,m=m/t*w}c=t.x-p,d=t.y-f,u=t.z-m;const b=(i.x+a*p)*r,x=(i.y+a*f)*r,E=(i.z+a*m)*r;i.x=(i.x-a*b)*h,i.y=(i.y-a*x)*h,i.z=(i.z-a*E)*h,o.x=c+(p+b)*h,o.y=d+(f+x)*h,o.z=u+(m+E)*h;const T=g-t.x,C=_-t.y,S=y-t.z;return T*(o.x-g)+C*(o.y-_)+S*(o.z-y)>0&&(o.x=g,o.y=_,o.z=y,i.x=(o.x-g)/r,i.y=(o.y-_)/r,i.z=(o.z-y)/r),o}function gi(t,e){e.set(0,0),t.forEach(t=>{e.x+=t.clientX,e.y+=t.clientY}),e.x/=t.length,e.y/=t.length}function _i(t,e){return!!ni(t)&&(console.warn(`${e} is not supported in OrthographicCamera`),!0)}class yi{constructor(){this._listeners={}}addEventListener(t,e){const i=this._listeners;void 0===i[t]&&(i[t]=[]),-1===i[t].indexOf(e)&&i[t].push(e)}hasEventListener(t,e){const i=this._listeners;return void 0!==i[t]&&-1!==i[t].indexOf(e)}removeEventListener(t,e){const i=this._listeners[t];if(void 0!==i){const t=i.indexOf(e);-1!==t&&i.splice(t,1)}}removeAllEventListeners(t){t?Array.isArray(this._listeners[t])&&(this._listeners[t].length=0):this._listeners={}}dispatchEvent(t){const e=this._listeners[t.type];if(void 0!==e){t.target=this;const i=e.slice(0);for(let e=0,s=i.length;e<s;e++)i[e].call(this,t)}}}var wi;const vi=1/8,bi=/Mac/.test(null===(wi=null===globalThis||void 0===globalThis?void 0:globalThis.navigator)||void 0===wi?void 0:wi.platform);let xi,Ei,Ti,Ci,Si,Ai,Oi,Pi,Ii,ki,Di,zi,Mi,Bi,Ni,Ui,Li,Ri,Fi,Vi,ji,Zi,Hi;class $i extends yi{static install(t){xi=t.THREE,Ei=Object.freeze(new xi.Vector3(0,0,0)),Ti=Object.freeze(new xi.Vector3(0,1,0)),Ci=Object.freeze(new xi.Vector3(0,0,1)),Si=new xi.Vector2,Ai=new xi.Vector3,Oi=new xi.Vector3,Pi=new xi.Vector3,Ii=new xi.Vector3,ki=new xi.Vector3,Di=new xi.Vector3,zi=new xi.Vector3,Mi=new xi.Vector3,Bi=new xi.Vector3,Ni=new xi.Spherical,Ui=new xi.Spherical,Li=new xi.Box3,Ri=new xi.Box3,Fi=new xi.Sphere,Vi=new xi.Quaternion,ji=new xi.Quaternion,Zi=new xi.Matrix4,Hi=new xi.Raycaster}static get ACTION(){return Je}set verticalDragToForward(t){console.warn("camera-controls: `verticalDragToForward` was removed. Use `mouseButtons.left = CameraControls.ACTION.SCREEN_PAN` instead.")}constructor(t,e){super(),this.minPolarAngle=0,this.maxPolarAngle=Math.PI,this.minAzimuthAngle=-1/0,this.maxAzimuthAngle=1/0,this.minDistance=Number.EPSILON,this.maxDistance=1/0,this.infinityDolly=!1,this.minZoom=.01,this.maxZoom=1/0,this.smoothTime=.25,this.draggingSmoothTime=.125,this.maxSpeed=1/0,this.azimuthRotateSpeed=1,this.polarRotateSpeed=1,this.dollySpeed=1,this.dollyDragInverted=!1,this.truckSpeed=2,this.dollyToCursor=!1,this.dragToOffset=!1,this.boundaryFriction=0,this.restThreshold=.01,this.colliderMeshes=[],this.cancel=()=>{},this._enabled=!0,this._state=Je.NONE,this._viewport=null,this._changedDolly=0,this._changedZoom=0,this._hasRested=!0,this._boundaryEnclosesCamera=!1,this._needsUpdate=!0,this._updatedLastTime=!1,this._elementRect=new DOMRect,this._isDragging=!1,this._dragNeedsUpdate=!0,this._activePointers=[],this._lockedPointer=null,this._interactiveArea=new DOMRect(0,0,1,1),this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._isUserControllingOffset=!1,this._isUserControllingZoom=!1,this._lastDollyDirection=ti,this._thetaVelocity={value:0},this._phiVelocity={value:0},this._radiusVelocity={value:0},this._targetVelocity=new xi.Vector3,this._focalOffsetVelocity=new xi.Vector3,this._zoomVelocity={value:0},this._truckInternal=(t,e,i,s)=>{let n,r;if(si(this._camera)){const i=Ai.copy(this._camera.position).sub(this._target),s=this._camera.getEffectiveFOV()*ai,o=i.length()*Math.tan(.5*s);n=this.truckSpeed*t*o/this._elementRect.height,r=this.truckSpeed*e*o/this._elementRect.height}else{if(!ni(this._camera))return;{const i=this._camera;n=this.truckSpeed*t*(i.right-i.left)/i.zoom/this._elementRect.width,r=this.truckSpeed*e*(i.top-i.bottom)/i.zoom/this._elementRect.height}}s?(i?this.setFocalOffset(this._focalOffsetEnd.x+n,this._focalOffsetEnd.y,this._focalOffsetEnd.z,!0):this.truck(n,0,!0),this.forward(-r,!0)):i?this.setFocalOffset(this._focalOffsetEnd.x+n,this._focalOffsetEnd.y+r,this._focalOffsetEnd.z,!0):this.truck(n,r,!0)},this._rotateInternal=(t,e)=>{const i=ri*this.azimuthRotateSpeed*t/this._elementRect.height,s=ri*this.polarRotateSpeed*e/this._elementRect.height;this.rotate(i,s,!0)},this._dollyInternal=(t,e,i)=>{const s=Math.pow(.95,-t*this.dollySpeed),n=this._sphericalEnd.radius,r=this._sphericalEnd.radius*s,o=li(r,this.minDistance,this.maxDistance),a=o-r;this.infinityDolly&&this.dollyToCursor?this._dollyToNoClamp(r,!0):this.infinityDolly&&!this.dollyToCursor?(this.dollyInFixed(a,!0),this._dollyToNoClamp(o,!0)):this._dollyToNoClamp(o,!0),this.dollyToCursor&&(this._changedDolly+=(this.infinityDolly?r:o)-n,this._dollyControlCoord.set(e,i)),this._lastDollyDirection=Math.sign(-t)},this._zoomInternal=(t,e,i)=>{const s=Math.pow(.95,t*this.dollySpeed),n=this._zoom,r=this._zoom*s;this.zoomTo(r,!0),this.dollyToCursor&&(this._changedZoom+=r-n,this._dollyControlCoord.set(e,i))},void 0===xi&&console.error("camera-controls: `THREE` is undefined. You must first run `CameraControls.install( { THREE: THREE } )`. Check the docs for further information."),this._camera=t,this._yAxisUpSpace=(new xi.Quaternion).setFromUnitVectors(this._camera.up,Ti),this._yAxisUpSpaceInverse=this._yAxisUpSpace.clone().invert(),this._state=Je.NONE,this._target=new xi.Vector3,this._targetEnd=this._target.clone(),this._focalOffset=new xi.Vector3,this._focalOffsetEnd=this._focalOffset.clone(),this._spherical=(new xi.Spherical).setFromVector3(Ai.copy(this._camera.position).applyQuaternion(this._yAxisUpSpace)),this._sphericalEnd=this._spherical.clone(),this._lastDistance=this._spherical.radius,this._zoom=this._camera.zoom,this._zoomEnd=this._zoom,this._lastZoom=this._zoom,this._nearPlaneCorners=[new xi.Vector3,new xi.Vector3,new xi.Vector3,new xi.Vector3],this._updateNearPlaneCorners(),this._boundary=new xi.Box3(new xi.Vector3(-1/0,-1/0,-1/0),new xi.Vector3(1/0,1/0,1/0)),this._cameraUp0=this._camera.up.clone(),this._target0=this._target.clone(),this._position0=this._camera.position.clone(),this._zoom0=this._zoom,this._focalOffset0=this._focalOffset.clone(),this._dollyControlCoord=new xi.Vector2,this.mouseButtons={left:Je.ROTATE,middle:Je.DOLLY,right:Je.TRUCK,wheel:si(this._camera)?Je.DOLLY:ni(this._camera)?Je.ZOOM:Je.NONE},this.touches={one:Je.TOUCH_ROTATE,two:si(this._camera)?Je.TOUCH_DOLLY_TRUCK:ni(this._camera)?Je.TOUCH_ZOOM_TRUCK:Je.NONE,three:Je.TOUCH_TRUCK};const i=new xi.Vector2,s=new xi.Vector2,n=new xi.Vector2,r=t=>{if(!this._enabled||!this._domElement)return;if(0!==this._interactiveArea.left||0!==this._interactiveArea.top||1!==this._interactiveArea.width||1!==this._interactiveArea.height){const e=this._domElement.getBoundingClientRect(),i=t.clientX/e.width,s=t.clientY/e.height;if(i<this._interactiveArea.left||i>this._interactiveArea.right||s<this._interactiveArea.top||s>this._interactiveArea.bottom)return}const e="mouse"!==t.pointerType?null:(t.buttons&Ge)===Ge?Ge:(t.buttons&Ke)===Ke?Ke:(t.buttons&Qe)===Qe?Qe:null;if(null!==e){const t=this._findPointerByMouseButton(e);t&&this._disposePointer(t)}if((t.buttons&Ge)===Ge&&this._lockedPointer)return;const i={pointerId:t.pointerId,clientX:t.clientX,clientY:t.clientY,deltaX:0,deltaY:0,mouseButton:e};this._activePointers.push(i),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.ownerDocument.addEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.addEventListener("pointerup",a),this._isDragging=!0,d(t)},o=t=>{t.cancelable&&t.preventDefault();const e=t.pointerId,i=this._lockedPointer||this._findPointerById(e);if(i){if(i.clientX=t.clientX,i.clientY=t.clientY,i.deltaX=t.movementX,i.deltaY=t.movementY,this._state=0,"touch"===t.pointerType)switch(this._activePointers.length){case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else(!this._isDragging&&this._lockedPointer||this._isDragging&&(t.buttons&Ge)===Ge)&&(this._state=this._state|this.mouseButtons.left),this._isDragging&&(t.buttons&Ke)===Ke&&(this._state=this._state|this.mouseButtons.middle),this._isDragging&&(t.buttons&Qe)===Qe&&(this._state=this._state|this.mouseButtons.right);u()}},a=t=>{const e=this._findPointerById(t.pointerId);if(!e||e!==this._lockedPointer){if(e&&this._disposePointer(e),"touch"===t.pointerType)switch(this._activePointers.length){case 0:this._state=Je.NONE;break;case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else this._state=Je.NONE;p()}};let l=-1;const h=t=>{if(!this._domElement)return;if(!this._enabled||this.mouseButtons.wheel===Je.NONE)return;if(0!==this._interactiveArea.left||0!==this._interactiveArea.top||1!==this._interactiveArea.width||1!==this._interactiveArea.height){const e=this._domElement.getBoundingClientRect(),i=t.clientX/e.width,s=t.clientY/e.height;if(i<this._interactiveArea.left||i>this._interactiveArea.right||s<this._interactiveArea.top||s>this._interactiveArea.bottom)return}if(t.preventDefault(),this.dollyToCursor||this.mouseButtons.wheel===Je.ROTATE||this.mouseButtons.wheel===Je.TRUCK){const t=performance.now();l-t<1e3&&this._getClientRect(this._elementRect),l=t}const e=bi?-1:-3,i=1===t.deltaMode||t.ctrlKey?t.deltaY/e:t.deltaY/(10*e),s=this.dollyToCursor?(t.clientX-this._elementRect.x)/this._elementRect.width*2-1:0,n=this.dollyToCursor?(t.clientY-this._elementRect.y)/this._elementRect.height*-2+1:0;switch(this.mouseButtons.wheel){case Je.ROTATE:this._rotateInternal(t.deltaX,t.deltaY),this._isUserControllingRotate=!0;break;case Je.TRUCK:this._truckInternal(t.deltaX,t.deltaY,!1,!1),this._isUserControllingTruck=!0;break;case Je.SCREEN_PAN:this._truckInternal(t.deltaX,t.deltaY,!1,!0),this._isUserControllingTruck=!0;break;case Je.OFFSET:this._truckInternal(t.deltaX,t.deltaY,!0,!1),this._isUserControllingOffset=!0;break;case Je.DOLLY:this._dollyInternal(-i,s,n),this._isUserControllingDolly=!0;break;case Je.ZOOM:this._zoomInternal(-i,s,n),this._isUserControllingZoom=!0}this.dispatchEvent({type:"control"})},c=t=>{if(this._domElement&&this._enabled){if(this.mouseButtons.right===$i.ACTION.NONE){const e=t instanceof PointerEvent?t.pointerId:0,i=this._findPointerById(e);return i&&this._disposePointer(i),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),void this._domElement.ownerDocument.removeEventListener("pointerup",a)}t.preventDefault()}},d=t=>{if(!this._enabled)return;gi(this._activePointers,Si),this._getClientRect(this._elementRect),i.copy(Si),s.copy(Si);if(this._activePointers.length>=2){const t=Si.x-this._activePointers[1].clientX,e=Si.y-this._activePointers[1].clientY,i=Math.sqrt(t*t+e*e);n.set(0,i);const r=.5*(this._activePointers[0].clientX+this._activePointers[1].clientX),o=.5*(this._activePointers[0].clientY+this._activePointers[1].clientY);s.set(r,o)}if(this._state=0,t)if("pointerType"in t&&"touch"===t.pointerType)switch(this._activePointers.length){case 1:this._state=this.touches.one;break;case 2:this._state=this.touches.two;break;case 3:this._state=this.touches.three}else this._lockedPointer||(t.buttons&Ge)!==Ge||(this._state=this._state|this.mouseButtons.left),(t.buttons&Ke)===Ke&&(this._state=this._state|this.mouseButtons.middle),(t.buttons&Qe)===Qe&&(this._state=this._state|this.mouseButtons.right);else this._lockedPointer&&(this._state=this._state|this.mouseButtons.left);(this._state&Je.ROTATE)!==Je.ROTATE&&(this._state&Je.TOUCH_ROTATE)!==Je.TOUCH_ROTATE&&(this._state&Je.TOUCH_DOLLY_ROTATE)!==Je.TOUCH_DOLLY_ROTATE&&(this._state&Je.TOUCH_ZOOM_ROTATE)!==Je.TOUCH_ZOOM_ROTATE||(this._sphericalEnd.theta=this._spherical.theta,this._sphericalEnd.phi=this._spherical.phi,this._thetaVelocity.value=0,this._phiVelocity.value=0),(this._state&Je.TRUCK)!==Je.TRUCK&&(this._state&Je.SCREEN_PAN)!==Je.SCREEN_PAN&&(this._state&Je.TOUCH_TRUCK)!==Je.TOUCH_TRUCK&&(this._state&Je.TOUCH_SCREEN_PAN)!==Je.TOUCH_SCREEN_PAN&&(this._state&Je.TOUCH_DOLLY_TRUCK)!==Je.TOUCH_DOLLY_TRUCK&&(this._state&Je.TOUCH_DOLLY_SCREEN_PAN)!==Je.TOUCH_DOLLY_SCREEN_PAN&&(this._state&Je.TOUCH_ZOOM_TRUCK)!==Je.TOUCH_ZOOM_TRUCK&&(this._state&Je.TOUCH_ZOOM_SCREEN_PAN)!==Je.TOUCH_DOLLY_SCREEN_PAN||(this._targetEnd.copy(this._target),this._targetVelocity.set(0,0,0)),(this._state&Je.DOLLY)!==Je.DOLLY&&(this._state&Je.TOUCH_DOLLY)!==Je.TOUCH_DOLLY&&(this._state&Je.TOUCH_DOLLY_TRUCK)!==Je.TOUCH_DOLLY_TRUCK&&(this._state&Je.TOUCH_DOLLY_SCREEN_PAN)!==Je.TOUCH_DOLLY_SCREEN_PAN&&(this._state&Je.TOUCH_DOLLY_OFFSET)!==Je.TOUCH_DOLLY_OFFSET&&(this._state&Je.TOUCH_DOLLY_ROTATE)!==Je.TOUCH_DOLLY_ROTATE||(this._sphericalEnd.radius=this._spherical.radius,this._radiusVelocity.value=0),(this._state&Je.ZOOM)!==Je.ZOOM&&(this._state&Je.TOUCH_ZOOM)!==Je.TOUCH_ZOOM&&(this._state&Je.TOUCH_ZOOM_TRUCK)!==Je.TOUCH_ZOOM_TRUCK&&(this._state&Je.TOUCH_ZOOM_SCREEN_PAN)!==Je.TOUCH_ZOOM_SCREEN_PAN&&(this._state&Je.TOUCH_ZOOM_OFFSET)!==Je.TOUCH_ZOOM_OFFSET&&(this._state&Je.TOUCH_ZOOM_ROTATE)!==Je.TOUCH_ZOOM_ROTATE||(this._zoomEnd=this._zoom,this._zoomVelocity.value=0),(this._state&Je.OFFSET)!==Je.OFFSET&&(this._state&Je.TOUCH_OFFSET)!==Je.TOUCH_OFFSET&&(this._state&Je.TOUCH_DOLLY_OFFSET)!==Je.TOUCH_DOLLY_OFFSET&&(this._state&Je.TOUCH_ZOOM_OFFSET)!==Je.TOUCH_ZOOM_OFFSET||(this._focalOffsetEnd.copy(this._focalOffset),this._focalOffsetVelocity.set(0,0,0)),this.dispatchEvent({type:"controlstart"})},u=()=>{if(!this._enabled||!this._dragNeedsUpdate)return;this._dragNeedsUpdate=!1,gi(this._activePointers,Si);const t=this._domElement&&this._domElement.ownerDocument.pointerLockElement===this._domElement?this._lockedPointer||this._activePointers[0]:null,e=t?-t.deltaX:s.x-Si.x,r=t?-t.deltaY:s.y-Si.y;if(s.copy(Si),(this._state&Je.ROTATE)!==Je.ROTATE&&(this._state&Je.TOUCH_ROTATE)!==Je.TOUCH_ROTATE&&(this._state&Je.TOUCH_DOLLY_ROTATE)!==Je.TOUCH_DOLLY_ROTATE&&(this._state&Je.TOUCH_ZOOM_ROTATE)!==Je.TOUCH_ZOOM_ROTATE||(this._rotateInternal(e,r),this._isUserControllingRotate=!0),(this._state&Je.DOLLY)===Je.DOLLY||(this._state&Je.ZOOM)===Je.ZOOM){const t=this.dollyToCursor?(i.x-this._elementRect.x)/this._elementRect.width*2-1:0,e=this.dollyToCursor?(i.y-this._elementRect.y)/this._elementRect.height*-2+1:0,s=this.dollyDragInverted?-1:1;(this._state&Je.DOLLY)===Je.DOLLY?(this._dollyInternal(s*r*vi,t,e),this._isUserControllingDolly=!0):(this._zoomInternal(s*r*vi,t,e),this._isUserControllingZoom=!0)}if((this._state&Je.TOUCH_DOLLY)===Je.TOUCH_DOLLY||(this._state&Je.TOUCH_ZOOM)===Je.TOUCH_ZOOM||(this._state&Je.TOUCH_DOLLY_TRUCK)===Je.TOUCH_DOLLY_TRUCK||(this._state&Je.TOUCH_ZOOM_TRUCK)===Je.TOUCH_ZOOM_TRUCK||(this._state&Je.TOUCH_DOLLY_SCREEN_PAN)===Je.TOUCH_DOLLY_SCREEN_PAN||(this._state&Je.TOUCH_ZOOM_SCREEN_PAN)===Je.TOUCH_ZOOM_SCREEN_PAN||(this._state&Je.TOUCH_DOLLY_OFFSET)===Je.TOUCH_DOLLY_OFFSET||(this._state&Je.TOUCH_ZOOM_OFFSET)===Je.TOUCH_ZOOM_OFFSET||(this._state&Je.TOUCH_DOLLY_ROTATE)===Je.TOUCH_DOLLY_ROTATE||(this._state&Je.TOUCH_ZOOM_ROTATE)===Je.TOUCH_ZOOM_ROTATE){const t=Si.x-this._activePointers[1].clientX,e=Si.y-this._activePointers[1].clientY,i=Math.sqrt(t*t+e*e),r=n.y-i;n.set(0,i);const o=this.dollyToCursor?(s.x-this._elementRect.x)/this._elementRect.width*2-1:0,a=this.dollyToCursor?(s.y-this._elementRect.y)/this._elementRect.height*-2+1:0;(this._state&Je.TOUCH_DOLLY)===Je.TOUCH_DOLLY||(this._state&Je.TOUCH_DOLLY_ROTATE)===Je.TOUCH_DOLLY_ROTATE||(this._state&Je.TOUCH_DOLLY_TRUCK)===Je.TOUCH_DOLLY_TRUCK||(this._state&Je.TOUCH_DOLLY_SCREEN_PAN)===Je.TOUCH_DOLLY_SCREEN_PAN||(this._state&Je.TOUCH_DOLLY_OFFSET)===Je.TOUCH_DOLLY_OFFSET?(this._dollyInternal(r*vi,o,a),this._isUserControllingDolly=!0):(this._zoomInternal(r*vi,o,a),this._isUserControllingZoom=!0)}(this._state&Je.TRUCK)!==Je.TRUCK&&(this._state&Je.TOUCH_TRUCK)!==Je.TOUCH_TRUCK&&(this._state&Je.TOUCH_DOLLY_TRUCK)!==Je.TOUCH_DOLLY_TRUCK&&(this._state&Je.TOUCH_ZOOM_TRUCK)!==Je.TOUCH_ZOOM_TRUCK||(this._truckInternal(e,r,!1,!1),this._isUserControllingTruck=!0),(this._state&Je.SCREEN_PAN)!==Je.SCREEN_PAN&&(this._state&Je.TOUCH_SCREEN_PAN)!==Je.TOUCH_SCREEN_PAN&&(this._state&Je.TOUCH_DOLLY_SCREEN_PAN)!==Je.TOUCH_DOLLY_SCREEN_PAN&&(this._state&Je.TOUCH_ZOOM_SCREEN_PAN)!==Je.TOUCH_ZOOM_SCREEN_PAN||(this._truckInternal(e,r,!1,!0),this._isUserControllingTruck=!0),(this._state&Je.OFFSET)!==Je.OFFSET&&(this._state&Je.TOUCH_OFFSET)!==Je.TOUCH_OFFSET&&(this._state&Je.TOUCH_DOLLY_OFFSET)!==Je.TOUCH_DOLLY_OFFSET&&(this._state&Je.TOUCH_ZOOM_OFFSET)!==Je.TOUCH_ZOOM_OFFSET||(this._truckInternal(e,r,!0,!1),this._isUserControllingOffset=!0),this.dispatchEvent({type:"control"})},p=()=>{gi(this._activePointers,Si),s.copy(Si),this._dragNeedsUpdate=!1,(0===this._activePointers.length||1===this._activePointers.length&&this._activePointers[0]===this._lockedPointer)&&(this._isDragging=!1),0===this._activePointers.length&&this._domElement&&(this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this.dispatchEvent({type:"controlend"}))};this.lockPointer=()=>{this._enabled&&this._domElement&&(this.cancel(),this._lockedPointer={pointerId:-1,clientX:0,clientY:0,deltaX:0,deltaY:0,mouseButton:null},this._activePointers.push(this._lockedPointer),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.requestPointerLock(),this._domElement.ownerDocument.addEventListener("pointerlockchange",f),this._domElement.ownerDocument.addEventListener("pointerlockerror",m),this._domElement.ownerDocument.addEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.addEventListener("pointerup",a),d())},this.unlockPointer=()=>{var t,e,i;null!==this._lockedPointer&&(this._disposePointer(this._lockedPointer),this._lockedPointer=null),null===(t=this._domElement)||void 0===t||t.ownerDocument.exitPointerLock(),null===(e=this._domElement)||void 0===e||e.ownerDocument.removeEventListener("pointerlockchange",f),null===(i=this._domElement)||void 0===i||i.ownerDocument.removeEventListener("pointerlockerror",m),this.cancel()};const f=()=>{this._domElement&&this._domElement.ownerDocument.pointerLockElement===this._domElement||this.unlockPointer()},m=()=>{this.unlockPointer()};this._addAllEventListeners=t=>{this._domElement=t,this._domElement.style.touchAction="none",this._domElement.style.userSelect="none",this._domElement.style.webkitUserSelect="none",this._domElement.addEventListener("pointerdown",r),this._domElement.addEventListener("pointercancel",a),this._domElement.addEventListener("wheel",h,{passive:!1}),this._domElement.addEventListener("contextmenu",c)},this._removeAllEventListeners=()=>{this._domElement&&(this._domElement.style.touchAction="",this._domElement.style.userSelect="",this._domElement.style.webkitUserSelect="",this._domElement.removeEventListener("pointerdown",r),this._domElement.removeEventListener("pointercancel",a),this._domElement.removeEventListener("wheel",h,{passive:!1}),this._domElement.removeEventListener("contextmenu",c),this._domElement.ownerDocument.removeEventListener("pointermove",o,{passive:!1}),this._domElement.ownerDocument.removeEventListener("pointerup",a),this._domElement.ownerDocument.removeEventListener("pointerlockchange",f),this._domElement.ownerDocument.removeEventListener("pointerlockerror",m))},this.cancel=()=>{this._state!==Je.NONE&&(this._state=Je.NONE,this._activePointers.length=0,p())},e&&this.connect(e),this.update(0)}get camera(){return this._camera}set camera(t){this._camera=t,this.updateCameraUp(),this._camera.updateProjectionMatrix(),this._updateNearPlaneCorners(),this._needsUpdate=!0}get enabled(){return this._enabled}set enabled(t){this._enabled=t,this._domElement&&(t?(this._domElement.style.touchAction="none",this._domElement.style.userSelect="none",this._domElement.style.webkitUserSelect="none"):(this.cancel(),this._domElement.style.touchAction="",this._domElement.style.userSelect="",this._domElement.style.webkitUserSelect=""))}get active(){return!this._hasRested}get currentAction(){return this._state}get distance(){return this._spherical.radius}set distance(t){this._spherical.radius===t&&this._sphericalEnd.radius===t||(this._spherical.radius=t,this._sphericalEnd.radius=t,this._needsUpdate=!0)}get azimuthAngle(){return this._spherical.theta}set azimuthAngle(t){this._spherical.theta===t&&this._sphericalEnd.theta===t||(this._spherical.theta=t,this._sphericalEnd.theta=t,this._needsUpdate=!0)}get polarAngle(){return this._spherical.phi}set polarAngle(t){this._spherical.phi===t&&this._sphericalEnd.phi===t||(this._spherical.phi=t,this._sphericalEnd.phi=t,this._needsUpdate=!0)}get boundaryEnclosesCamera(){return this._boundaryEnclosesCamera}set boundaryEnclosesCamera(t){this._boundaryEnclosesCamera=t,this._needsUpdate=!0}set interactiveArea(t){this._interactiveArea.width=li(t.width,0,1),this._interactiveArea.height=li(t.height,0,1),this._interactiveArea.x=li(t.x,0,1-this._interactiveArea.width),this._interactiveArea.y=li(t.y,0,1-this._interactiveArea.height)}addEventListener(t,e){super.addEventListener(t,e)}removeEventListener(t,e){super.removeEventListener(t,e)}rotate(t,e,i=!1){return this.rotateTo(this._sphericalEnd.theta+t,this._sphericalEnd.phi+e,i)}rotateAzimuthTo(t,e=!1){return this.rotateTo(t,this._sphericalEnd.phi,e)}rotatePolarTo(t,e=!1){return this.rotateTo(this._sphericalEnd.theta,t,e)}rotateTo(t,e,i=!1){this._isUserControllingRotate=!1;const s=li(t,this.minAzimuthAngle,this.maxAzimuthAngle),n=li(e,this.minPolarAngle,this.maxPolarAngle);this._sphericalEnd.theta=s,this._sphericalEnd.phi=n,this._sphericalEnd.makeSafe(),this._needsUpdate=!0,i||(this._spherical.theta=this._sphericalEnd.theta,this._spherical.phi=this._sphericalEnd.phi);const r=!i||ci(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ci(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold);return this._createOnRestPromise(r)}dolly(t,e=!1){return this.dollyTo(this._sphericalEnd.radius-t,e)}dollyTo(t,e=!1){return this._isUserControllingDolly=!1,this._lastDollyDirection=ti,this._changedDolly=0,this._dollyToNoClamp(li(t,this.minDistance,this.maxDistance),e)}_dollyToNoClamp(t,e=!1){const i=this._sphericalEnd.radius;if(this.colliderMeshes.length>=1){const e=this._collisionTest(),s=ci(e,this._spherical.radius);if(!(i>t)&&s)return Promise.resolve();this._sphericalEnd.radius=Math.min(t,e)}else this._sphericalEnd.radius=t;this._needsUpdate=!0,e||(this._spherical.radius=this._sphericalEnd.radius);const s=!e||ci(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(s)}dollyInFixed(t,e=!1){this._targetEnd.add(this._getCameraDirection(Ii).multiplyScalar(t)),e||this._target.copy(this._targetEnd);const i=!e||ci(this._target.x,this._targetEnd.x,this.restThreshold)&&ci(this._target.y,this._targetEnd.y,this.restThreshold)&&ci(this._target.z,this._targetEnd.z,this.restThreshold);return this._createOnRestPromise(i)}zoom(t,e=!1){return this.zoomTo(this._zoomEnd+t,e)}zoomTo(t,e=!1){this._isUserControllingZoom=!1,this._zoomEnd=li(t,this.minZoom,this.maxZoom),this._needsUpdate=!0,e||(this._zoom=this._zoomEnd);const i=!e||ci(this._zoom,this._zoomEnd,this.restThreshold);return this._changedZoom=0,this._createOnRestPromise(i)}pan(t,e,i=!1){return console.warn("`pan` has been renamed to `truck`"),this.truck(t,e,i)}truck(t,e,i=!1){this._camera.updateMatrix(),ki.setFromMatrixColumn(this._camera.matrix,0),Di.setFromMatrixColumn(this._camera.matrix,1),ki.multiplyScalar(t),Di.multiplyScalar(-e);const s=Ai.copy(ki).add(Di),n=Oi.copy(this._targetEnd).add(s);return this.moveTo(n.x,n.y,n.z,i)}forward(t,e=!1){Ai.setFromMatrixColumn(this._camera.matrix,0),Ai.crossVectors(this._camera.up,Ai),Ai.multiplyScalar(t);const i=Oi.copy(this._targetEnd).add(Ai);return this.moveTo(i.x,i.y,i.z,e)}elevate(t,e=!1){return Ai.copy(this._camera.up).multiplyScalar(t),this.moveTo(this._targetEnd.x+Ai.x,this._targetEnd.y+Ai.y,this._targetEnd.z+Ai.z,e)}moveTo(t,e,i,s=!1){this._isUserControllingTruck=!1;const n=Ai.set(t,e,i).sub(this._targetEnd);this._encloseToBoundary(this._targetEnd,n,this.boundaryFriction),this._needsUpdate=!0,s||this._target.copy(this._targetEnd);const r=!s||ci(this._target.x,this._targetEnd.x,this.restThreshold)&&ci(this._target.y,this._targetEnd.y,this.restThreshold)&&ci(this._target.z,this._targetEnd.z,this.restThreshold);return this._createOnRestPromise(r)}lookInDirectionOf(t,e,i,s=!1){const n=Ai.set(t,e,i).sub(this._targetEnd).normalize().multiplyScalar(-this._sphericalEnd.radius).add(this._targetEnd);return this.setPosition(n.x,n.y,n.z,s)}fitToBox(t,e,{cover:i=!1,paddingLeft:s=0,paddingRight:n=0,paddingBottom:r=0,paddingTop:o=0}={}){const a=[],l=t.isBox3?Li.copy(t):Li.setFromObject(t);l.isEmpty()&&(console.warn("camera-controls: fitTo() cannot be used with an empty box. Aborting"),Promise.resolve());const h=di(this._sphericalEnd.theta,oi),c=di(this._sphericalEnd.phi,oi);a.push(this.rotateTo(h,c,e));const d=Ai.setFromSpherical(this._sphericalEnd).normalize(),u=Vi.setFromUnitVectors(d,Ci),p=ci(Math.abs(d.y),1);p&&u.multiply(ji.setFromAxisAngle(Ti,h)),u.multiply(this._yAxisUpSpaceInverse);const f=Ri.makeEmpty();Oi.copy(l.min).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.min).setX(l.max.x).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.min).setY(l.max.y).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.max).setZ(l.min.z).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.min).setZ(l.max.z).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.max).setY(l.min.y).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.max).setX(l.min.x).applyQuaternion(u),f.expandByPoint(Oi),Oi.copy(l.max).applyQuaternion(u),f.expandByPoint(Oi),f.min.x-=s,f.min.y-=r,f.max.x+=n,f.max.y+=o,u.setFromUnitVectors(Ci,d),p&&u.premultiply(ji.invert()),u.premultiply(this._yAxisUpSpace);const m=f.getSize(Ai),g=f.getCenter(Oi).applyQuaternion(u);if(si(this._camera)){const t=this.getDistanceToFitBox(m.x,m.y,m.z,i);a.push(this.moveTo(g.x,g.y,g.z,e)),a.push(this.dollyTo(t,e)),a.push(this.setFocalOffset(0,0,0,e))}else if(ni(this._camera)){const t=this._camera,s=t.right-t.left,n=t.top-t.bottom,r=i?Math.max(s/m.x,n/m.y):Math.min(s/m.x,n/m.y);a.push(this.moveTo(g.x,g.y,g.z,e)),a.push(this.zoomTo(r,e)),a.push(this.setFocalOffset(0,0,0,e))}return Promise.all(a)}fitToSphere(t,e){const i=[],s="isObject3D"in t?$i.createBoundingSphere(t,Fi):Fi.copy(t);if(i.push(this.moveTo(s.center.x,s.center.y,s.center.z,e)),si(this._camera)){const t=this.getDistanceToFitSphere(s.radius);i.push(this.dollyTo(t,e))}else if(ni(this._camera)){const t=this._camera.right-this._camera.left,n=this._camera.top-this._camera.bottom,r=2*s.radius,o=Math.min(t/r,n/r);i.push(this.zoomTo(o,e))}return i.push(this.setFocalOffset(0,0,0,e)),Promise.all(i)}setLookAt(t,e,i,s,n,r,o=!1){this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._lastDollyDirection=ti,this._changedDolly=0;const a=Oi.set(s,n,r),l=Ai.set(t,e,i);this._targetEnd.copy(a),this._sphericalEnd.setFromVector3(l.sub(a).applyQuaternion(this._yAxisUpSpace)),this.normalizeRotations(),this._needsUpdate=!0,o||(this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd));const h=!o||ci(this._target.x,this._targetEnd.x,this.restThreshold)&&ci(this._target.y,this._targetEnd.y,this.restThreshold)&&ci(this._target.z,this._targetEnd.z,this.restThreshold)&&ci(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ci(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&ci(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(h)}lerpLookAt(t,e,i,s,n,r,o,a,l,h,c,d,u,p=!1){this._isUserControllingRotate=!1,this._isUserControllingDolly=!1,this._isUserControllingTruck=!1,this._lastDollyDirection=ti,this._changedDolly=0;const f=Ai.set(s,n,r),m=Oi.set(t,e,i);Ni.setFromVector3(m.sub(f).applyQuaternion(this._yAxisUpSpace));const g=Pi.set(h,c,d),_=Oi.set(o,a,l);Ui.setFromVector3(_.sub(g).applyQuaternion(this._yAxisUpSpace)),this._targetEnd.copy(f.lerp(g,u));const y=Ui.theta-Ni.theta,w=Ui.phi-Ni.phi,v=Ui.radius-Ni.radius;this._sphericalEnd.set(Ni.radius+v*u,Ni.phi+w*u,Ni.theta+y*u),this.normalizeRotations(),this._needsUpdate=!0,p||(this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd));const b=!p||ci(this._target.x,this._targetEnd.x,this.restThreshold)&&ci(this._target.y,this._targetEnd.y,this.restThreshold)&&ci(this._target.z,this._targetEnd.z,this.restThreshold)&&ci(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&ci(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&ci(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(b)}setPosition(t,e,i,s=!1){return this.setLookAt(t,e,i,this._targetEnd.x,this._targetEnd.y,this._targetEnd.z,s)}setTarget(t,e,i,s=!1){const n=this.getPosition(Ai),r=this.setLookAt(n.x,n.y,n.z,t,e,i,s);return this._sphericalEnd.phi=li(this._sphericalEnd.phi,this.minPolarAngle,this.maxPolarAngle),r}setFocalOffset(t,e,i,s=!1){this._isUserControllingOffset=!1,this._focalOffsetEnd.set(t,e,i),this._needsUpdate=!0,s||this._focalOffset.copy(this._focalOffsetEnd);const n=!s||ci(this._focalOffset.x,this._focalOffsetEnd.x,this.restThreshold)&&ci(this._focalOffset.y,this._focalOffsetEnd.y,this.restThreshold)&&ci(this._focalOffset.z,this._focalOffsetEnd.z,this.restThreshold);return this._createOnRestPromise(n)}setOrbitPoint(t,e,i){this._camera.updateMatrixWorld(),ki.setFromMatrixColumn(this._camera.matrixWorldInverse,0),Di.setFromMatrixColumn(this._camera.matrixWorldInverse,1),zi.setFromMatrixColumn(this._camera.matrixWorldInverse,2);const s=Ai.set(t,e,i),n=s.distanceTo(this._camera.position),r=s.sub(this._camera.position);ki.multiplyScalar(r.x),Di.multiplyScalar(r.y),zi.multiplyScalar(r.z),Ai.copy(ki).add(Di).add(zi),Ai.z=Ai.z+n,this.dollyTo(n,!1),this.setFocalOffset(-Ai.x,Ai.y,-Ai.z,!1),this.moveTo(t,e,i,!1)}setBoundary(t){if(!t)return this._boundary.min.set(-1/0,-1/0,-1/0),this._boundary.max.set(1/0,1/0,1/0),void(this._needsUpdate=!0);this._boundary.copy(t),this._boundary.clampPoint(this._targetEnd,this._targetEnd),this._needsUpdate=!0}setViewport(t,e,i,s){null!==t?(this._viewport=this._viewport||new xi.Vector4,"number"==typeof t?this._viewport.set(t,e,i,s):this._viewport.copy(t)):this._viewport=null}getDistanceToFitBox(t,e,i,s=!1){if(_i(this._camera,"getDistanceToFitBox"))return this._spherical.radius;const n=t/e,r=this._camera.getEffectiveFOV()*ai,o=this._camera.aspect;return.5*((s?n>o:n<o)?e:t/o)/Math.tan(.5*r)+.5*i}getDistanceToFitSphere(t){if(_i(this._camera,"getDistanceToFitSphere"))return this._spherical.radius;const e=this._camera.getEffectiveFOV()*ai,i=2*Math.atan(Math.tan(.5*e)*this._camera.aspect),s=1<this._camera.aspect?e:i;return t/Math.sin(.5*s)}getTarget(t,e=!0){return(t&&t.isVector3?t:new xi.Vector3).copy(e?this._targetEnd:this._target)}getPosition(t,e=!0){return(t&&t.isVector3?t:new xi.Vector3).setFromSpherical(e?this._sphericalEnd:this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(e?this._targetEnd:this._target)}getSpherical(t,e=!0){return(t||new xi.Spherical).copy(e?this._sphericalEnd:this._spherical)}getFocalOffset(t,e=!0){return(t&&t.isVector3?t:new xi.Vector3).copy(e?this._focalOffsetEnd:this._focalOffset)}normalizeRotations(){this._sphericalEnd.theta=this._sphericalEnd.theta%ri,this._sphericalEnd.theta<0&&(this._sphericalEnd.theta+=ri),this._spherical.theta+=ri*Math.round((this._sphericalEnd.theta-this._spherical.theta)/ri)}stop(){this._focalOffset.copy(this._focalOffsetEnd),this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd),this._zoom=this._zoomEnd}reset(t=!1){if(!ci(this._camera.up.x,this._cameraUp0.x)||!ci(this._camera.up.y,this._cameraUp0.y)||!ci(this._camera.up.z,this._cameraUp0.z)){this._camera.up.copy(this._cameraUp0);const t=this.getPosition(Ai);this.updateCameraUp(),this.setPosition(t.x,t.y,t.z)}const e=[this.setLookAt(this._position0.x,this._position0.y,this._position0.z,this._target0.x,this._target0.y,this._target0.z,t),this.setFocalOffset(this._focalOffset0.x,this._focalOffset0.y,this._focalOffset0.z,t),this.zoomTo(this._zoom0,t)];return Promise.all(e)}saveState(){this._cameraUp0.copy(this._camera.up),this.getTarget(this._target0),this.getPosition(this._position0),this._zoom0=this._zoom,this._focalOffset0.copy(this._focalOffset)}updateCameraUp(){this._yAxisUpSpace.setFromUnitVectors(this._camera.up,Ti),this._yAxisUpSpaceInverse.copy(this._yAxisUpSpace).invert()}applyCameraUp(){const t=Ai.subVectors(this._target,this._camera.position).normalize(),e=Oi.crossVectors(t,this._camera.up);this._camera.up.crossVectors(e,t).normalize(),this._camera.updateMatrixWorld();const i=this.getPosition(Ai);this.updateCameraUp(),this.setPosition(i.x,i.y,i.z)}update(t){const e=this._sphericalEnd.theta-this._spherical.theta,i=this._sphericalEnd.phi-this._spherical.phi,s=this._sphericalEnd.radius-this._spherical.radius,n=Mi.subVectors(this._targetEnd,this._target),r=Bi.subVectors(this._focalOffsetEnd,this._focalOffset),o=this._zoomEnd-this._zoom;if(hi(e))this._thetaVelocity.value=0,this._spherical.theta=this._sphericalEnd.theta;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.theta=fi(this._spherical.theta,this._sphericalEnd.theta,this._thetaVelocity,e,1/0,t),this._needsUpdate=!0}if(hi(i))this._phiVelocity.value=0,this._spherical.phi=this._sphericalEnd.phi;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.phi=fi(this._spherical.phi,this._sphericalEnd.phi,this._phiVelocity,e,1/0,t),this._needsUpdate=!0}if(hi(s))this._radiusVelocity.value=0,this._spherical.radius=this._sphericalEnd.radius;else{const e=this._isUserControllingDolly?this.draggingSmoothTime:this.smoothTime;this._spherical.radius=fi(this._spherical.radius,this._sphericalEnd.radius,this._radiusVelocity,e,this.maxSpeed,t),this._needsUpdate=!0}if(hi(n.x)&&hi(n.y)&&hi(n.z))this._targetVelocity.set(0,0,0),this._target.copy(this._targetEnd);else{const e=this._isUserControllingTruck?this.draggingSmoothTime:this.smoothTime;mi(this._target,this._targetEnd,this._targetVelocity,e,this.maxSpeed,t,this._target),this._needsUpdate=!0}if(hi(r.x)&&hi(r.y)&&hi(r.z))this._focalOffsetVelocity.set(0,0,0),this._focalOffset.copy(this._focalOffsetEnd);else{const e=this._isUserControllingOffset?this.draggingSmoothTime:this.smoothTime;mi(this._focalOffset,this._focalOffsetEnd,this._focalOffsetVelocity,e,this.maxSpeed,t,this._focalOffset),this._needsUpdate=!0}if(hi(o))this._zoomVelocity.value=0,this._zoom=this._zoomEnd;else{const e=this._isUserControllingZoom?this.draggingSmoothTime:this.smoothTime;this._zoom=fi(this._zoom,this._zoomEnd,this._zoomVelocity,e,1/0,t)}if(this.dollyToCursor)if(si(this._camera)&&0!==this._changedDolly){const t=this._spherical.radius-this._lastDistance,e=this._camera,i=this._getCameraDirection(Ii),s=Ai.copy(i).cross(e.up).normalize();0===s.lengthSq()&&(s.x=1);const n=Oi.crossVectors(s,i),r=this._sphericalEnd.radius*Math.tan(e.getEffectiveFOV()*ai*.5),o=(this._sphericalEnd.radius-t-this._sphericalEnd.radius)/this._sphericalEnd.radius,a=Pi.copy(this._targetEnd).add(s.multiplyScalar(this._dollyControlCoord.x*r*e.aspect)).add(n.multiplyScalar(this._dollyControlCoord.y*r)),l=Ai.copy(this._targetEnd).lerp(a,o),h=this._lastDollyDirection===ei&&this._spherical.radius<=this.minDistance,c=this._lastDollyDirection===ii&&this.maxDistance<=this._spherical.radius;if(this.infinityDolly&&(h||c)){this._sphericalEnd.radius-=t,this._spherical.radius-=t;const e=Oi.copy(i).multiplyScalar(-t);l.add(e)}this._boundary.clampPoint(l,l);const d=Oi.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedDolly-=t,hi(this._changedDolly)&&(this._changedDolly=0)}else if(ni(this._camera)&&0!==this._changedZoom){const t=this._zoom-this._lastZoom,e=this._camera,i=Ai.set(this._dollyControlCoord.x,this._dollyControlCoord.y,(e.near+e.far)/(e.near-e.far)).unproject(e),s=Oi.set(0,0,-1).applyQuaternion(e.quaternion),n=Pi.copy(i).add(s.multiplyScalar(-i.dot(e.up))),r=-(this._zoom-t-this._zoom)/this._zoom,o=this._getCameraDirection(Ii),a=this._targetEnd.dot(o),l=Ai.copy(this._targetEnd).lerp(n,r),h=l.dot(o),c=o.multiplyScalar(h-a);l.sub(c),this._boundary.clampPoint(l,l);const d=Oi.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedZoom-=t,hi(this._changedZoom)&&(this._changedZoom=0)}this._camera.zoom!==this._zoom&&(this._camera.zoom=this._zoom,this._camera.updateProjectionMatrix(),this._updateNearPlaneCorners(),this._needsUpdate=!0),this._dragNeedsUpdate=!0;const a=this._collisionTest();this._spherical.radius=Math.min(this._spherical.radius,a),this._spherical.makeSafe(),this._camera.position.setFromSpherical(this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(this._target),this._camera.lookAt(this._target);(!hi(this._focalOffset.x)||!hi(this._focalOffset.y)||!hi(this._focalOffset.z))&&(ki.setFromMatrixColumn(this._camera.matrix,0),Di.setFromMatrixColumn(this._camera.matrix,1),zi.setFromMatrixColumn(this._camera.matrix,2),ki.multiplyScalar(this._focalOffset.x),Di.multiplyScalar(-this._focalOffset.y),zi.multiplyScalar(this._focalOffset.z),Ai.copy(ki).add(Di).add(zi),this._camera.position.add(Ai),this._camera.updateMatrixWorld()),this._boundaryEnclosesCamera&&this._encloseToBoundary(this._camera.position.copy(this._target),Ai.setFromSpherical(this._spherical).applyQuaternion(this._yAxisUpSpaceInverse),1);const l=this._needsUpdate;return l&&!this._updatedLastTime?(this._hasRested=!1,this.dispatchEvent({type:"wake"}),this.dispatchEvent({type:"update"})):l?(this.dispatchEvent({type:"update"}),hi(e,this.restThreshold)&&hi(i,this.restThreshold)&&hi(s,this.restThreshold)&&hi(n.x,this.restThreshold)&&hi(n.y,this.restThreshold)&&hi(n.z,this.restThreshold)&&hi(r.x,this.restThreshold)&&hi(r.y,this.restThreshold)&&hi(r.z,this.restThreshold)&&hi(o,this.restThreshold)&&!this._hasRested&&(this._hasRested=!0,this.dispatchEvent({type:"rest"}))):!l&&this._updatedLastTime&&this.dispatchEvent({type:"sleep"}),this._lastDistance=this._spherical.radius,this._lastZoom=this._zoom,this._updatedLastTime=l,this._needsUpdate=!1,l}toJSON(){return JSON.stringify({enabled:this._enabled,minDistance:this.minDistance,maxDistance:ui(this.maxDistance),minZoom:this.minZoom,maxZoom:ui(this.maxZoom),minPolarAngle:this.minPolarAngle,maxPolarAngle:ui(this.maxPolarAngle),minAzimuthAngle:ui(this.minAzimuthAngle),maxAzimuthAngle:ui(this.maxAzimuthAngle),smoothTime:this.smoothTime,draggingSmoothTime:this.draggingSmoothTime,dollySpeed:this.dollySpeed,truckSpeed:this.truckSpeed,dollyToCursor:this.dollyToCursor,target:this._targetEnd.toArray(),position:Ai.setFromSpherical(this._sphericalEnd).add(this._targetEnd).toArray(),zoom:this._zoomEnd,focalOffset:this._focalOffsetEnd.toArray(),target0:this._target0.toArray(),position0:this._position0.toArray(),zoom0:this._zoom0,focalOffset0:this._focalOffset0.toArray()})}fromJSON(t,e=!1){const i=JSON.parse(t);this.enabled=i.enabled,this.minDistance=i.minDistance,this.maxDistance=pi(i.maxDistance),this.minZoom=i.minZoom,this.maxZoom=pi(i.maxZoom),this.minPolarAngle=i.minPolarAngle,this.maxPolarAngle=pi(i.maxPolarAngle),this.minAzimuthAngle=pi(i.minAzimuthAngle),this.maxAzimuthAngle=pi(i.maxAzimuthAngle),this.smoothTime=i.smoothTime,this.draggingSmoothTime=i.draggingSmoothTime,this.dollySpeed=i.dollySpeed,this.truckSpeed=i.truckSpeed,this.dollyToCursor=i.dollyToCursor,this._target0.fromArray(i.target0),this._position0.fromArray(i.position0),this._zoom0=i.zoom0,this._focalOffset0.fromArray(i.focalOffset0),this.moveTo(i.target[0],i.target[1],i.target[2],e),Ni.setFromVector3(Ai.fromArray(i.position).sub(this._targetEnd).applyQuaternion(this._yAxisUpSpace)),this.rotateTo(Ni.theta,Ni.phi,e),this.dollyTo(Ni.radius,e),this.zoomTo(i.zoom,e),this.setFocalOffset(i.focalOffset[0],i.focalOffset[1],i.focalOffset[2],e),this._needsUpdate=!0}connect(t){this._domElement?console.warn("camera-controls is already connected."):(t.setAttribute("data-camera-controls-version","2.10.1"),this._addAllEventListeners(t),this._getClientRect(this._elementRect))}disconnect(){this.cancel(),this._removeAllEventListeners(),this._domElement&&(this._domElement.removeAttribute("data-camera-controls-version"),this._domElement=void 0)}dispose(){this.removeAllEventListeners(),this.disconnect()}_getTargetDirection(t){return t.setFromSpherical(this._spherical).divideScalar(this._spherical.radius).applyQuaternion(this._yAxisUpSpaceInverse)}_getCameraDirection(t){return this._getTargetDirection(t).negate()}_findPointerById(t){return this._activePointers.find(e=>e.pointerId===t)}_findPointerByMouseButton(t){return this._activePointers.find(e=>e.mouseButton===t)}_disposePointer(t){this._activePointers.splice(this._activePointers.indexOf(t),1)}_encloseToBoundary(t,e,i){const s=e.lengthSq();if(0===s)return t;const n=Oi.copy(e).add(t),r=this._boundary.clampPoint(n,Pi).sub(n),o=r.lengthSq();if(0===o)return t.add(e);if(o===s)return t;if(0===i)return t.add(e).add(r);{const s=1+i*o/e.dot(r);return t.add(Oi.copy(e).multiplyScalar(s)).add(r.multiplyScalar(1-i))}}_updateNearPlaneCorners(){if(si(this._camera)){const t=this._camera,e=t.near,i=t.getEffectiveFOV()*ai,s=Math.tan(.5*i)*e,n=s*t.aspect;this._nearPlaneCorners[0].set(-n,-s,0),this._nearPlaneCorners[1].set(n,-s,0),this._nearPlaneCorners[2].set(n,s,0),this._nearPlaneCorners[3].set(-n,s,0)}else if(ni(this._camera)){const t=this._camera,e=1/t.zoom,i=t.left*e,s=t.right*e,n=t.top*e,r=t.bottom*e;this._nearPlaneCorners[0].set(i,n,0),this._nearPlaneCorners[1].set(s,n,0),this._nearPlaneCorners[2].set(s,r,0),this._nearPlaneCorners[3].set(i,r,0)}}_collisionTest(){let t=1/0;if(!(this.colliderMeshes.length>=1))return t;if(_i(this._camera,"_collisionTest"))return t;const e=this._getTargetDirection(Ii);Zi.lookAt(Ei,e,this._camera.up);for(let i=0;i<4;i++){const s=Oi.copy(this._nearPlaneCorners[i]);s.applyMatrix4(Zi);const n=Pi.addVectors(this._target,s);Hi.set(n,e),Hi.far=this._spherical.radius+1;const r=Hi.intersectObjects(this.colliderMeshes);0!==r.length&&r[0].distance<t&&(t=r[0].distance)}return t}_getClientRect(t){if(!this._domElement)return;const e=this._domElement.getBoundingClientRect();return t.x=e.left,t.y=e.top,this._viewport?(t.x+=this._viewport.x,t.y+=e.height-this._viewport.w-this._viewport.y,t.width=this._viewport.z,t.height=this._viewport.w):(t.width=e.width,t.height=e.height),t}_createOnRestPromise(t){return t?Promise.resolve():(this._hasRested=!1,this.dispatchEvent({type:"transitionstart"}),new Promise(t=>{const e=()=>{this.removeEventListener("rest",e),t()};this.addEventListener("rest",e)}))}_addAllEventListeners(t){}_removeAllEventListeners(){}get dampingFactor(){return console.warn(".dampingFactor has been deprecated. use smoothTime (in seconds) instead."),0}set dampingFactor(t){console.warn(".dampingFactor has been deprecated. use smoothTime (in seconds) instead.")}get draggingDampingFactor(){return console.warn(".draggingDampingFactor has been deprecated. use draggingSmoothTime (in seconds) instead."),0}set draggingDampingFactor(t){console.warn(".draggingDampingFactor has been deprecated. use draggingSmoothTime (in seconds) instead.")}static createBoundingSphere(t,e=new xi.Sphere){const i=e,s=i.center;Li.makeEmpty(),t.traverseVisible(t=>{t.isMesh&&Li.expandByObject(t)}),Li.getCenter(s);let n=0;return t.traverseVisible(t=>{if(!t.isMesh)return;const e=t;if(!e.geometry)return;const i=e.geometry.clone();i.applyMatrix4(e.matrixWorld);const r=i.attributes.position;for(let t=0,e=r.count;t<e;t++)Ai.fromBufferAttribute(r,t),n=Math.max(n,s.distanceToSquared(Ai))}),i.radius=Math.sqrt(n),i}}class Yi extends j{constructor(t){super(t),e(this,"onBeforeUpdate",new U),e(this,"onAfterUpdate",new U),e(this,"onAspectUpdated",new U),e(this,"onDisposed",new U),e(this,"three"),e(this,"_allControls",new Map),e(this,"updateAspect",()=>{var t;if(this.currentWorld&&this.currentWorld.renderer)if(this.three instanceof r.OrthographicCamera)this.onAspectUpdated.trigger();else if(null==(t=this.currentWorld.renderer)?void 0:t.isResizeable()){const t=this.currentWorld.renderer.getSize();this.three.aspect=t.width/t.height,this.three.updateProjectionMatrix(),this.onAspectUpdated.trigger()}}),this.three=this.setupCamera(),this.setupEvents(!0),this.worlds.onItemSet.add(({value:t})=>{const e=this.newCameraControls();this._allControls.set(t.uuid,e)}),this.worlds.onBeforeDelete.add(({value:t})=>{const e=this._allControls.get(t.uuid);e&&(e.dispose(),this._allControls.delete(t.uuid))})}get controls(){if(!this.currentWorld)throw new Error("This camera needs a world to work!");const t=this._allControls.get(this.currentWorld.uuid);if(!t)throw new Error("Controls not found!");return t}get enabled(){return null!==this.currentWorld&&this.controls.enabled}set enabled(t){null!==this.currentWorld&&(this.controls.enabled=t)}set currentWorld(t){if(super.currentWorld=t,!t)return;this.worlds.get(t.uuid)||this.worlds.set(t.uuid,t)}get currentWorld(){return this._currentWorld}dispose(){this.setupEvents(!1),this.onAspectUpdated.reset(),this.onBeforeUpdate.reset(),this.onAfterUpdate.reset(),this.three.removeFromParent(),this.onDisposed.trigger(),this.onDisposed.reset();for(const[t,e]of this._allControls)e.dispose();this.worlds.clear()}async fitToItems(t){const e=await this.getItemsBounding(t);await this.controls.fitToSphere(e,!0)}async setOrbitToItems(t){const e=await this.getItemsBounding(t);this.controls.setOrbitPoint(e.center.x,e.center.y,e.center.z)}update(t){this.enabled&&(this.onBeforeUpdate.trigger(this),this.controls.update(t),this.onAfterUpdate.trigger(this))}async getItemsBounding(t){const e=this.components.get(Ae),i=this.components.get(Me);i.list.clear();const s=new r.Sphere;if(t)await i.addFromModelIdMap(t);else for(const[,t]of e.list)i.list.add(t.box);return i.get().getBoundingSphere(s),i.list.clear(),s}setupCamera(){const t=window.innerWidth/window.innerHeight,e=new r.PerspectiveCamera(60,t,1,1e3);return e.position.set(50,50,50),e.lookAt(new r.Vector3(0,0,0)),e}newCameraControls(){if(!this.currentWorld)throw new Error("This camera needs a world to work!");if(!this.currentWorld.renderer)throw new Error("This camera needs a renderer to work!");$i.install({THREE:Yi.getSubsetOfThree()});const{domElement:t}=this.currentWorld.renderer.three,e=new $i(this.three,t);return e.smoothTime=.2,e.dollyToCursor=!0,e.infinityDolly=!0,e.minDistance=6,e}setupEvents(t){t?window.addEventListener("resize",this.updateAspect):window.removeEventListener("resize",this.updateAspect)}static getSubsetOfThree(){return{MOUSE:r.MOUSE,Vector2:r.Vector2,Vector3:r.Vector3,Vector4:r.Vector4,Quaternion:r.Quaternion,Matrix4:r.Matrix4,Spherical:r.Spherical,Box3:r.Box3,Sphere:r.Sphere,Raycaster:r.Raycaster,MathUtils:r.MathUtils}}}const Wi=class t extends F{constructor(i){super(i),e(this,"onAfterUpdate",new U),e(this,"onBeforeUpdate",new U),e(this,"onDisposed",new U),e(this,"list",new we),e(this,"enabled",!0),i.add(t.uuid,this)}create(){const t=new Ze(this.components),e=t.uuid;if(this.list.has(e))throw new Error("There is already a world with this name!");return this.list.set(e,t),t}delete(t){if(!this.list.has(t.uuid))throw new Error("The provided world is not found in the list!");this.list.delete(t.uuid),t.dispose()}dispose(){this.enabled=!1;for(const[t,e]of this.list)e.dispose();this.list.clear(),this.onDisposed.trigger()}update(t){if(this.enabled)for(const[e,i]of this.list)i.update(t)}};e(Wi,"uuid","fdb61dc4-2ec1-4966-b83d-54ea795fad4a");let Xi=Wi;class qi extends xe{constructor(){super(...arguments),e(this,"_config",{visible:{value:!0,type:"Boolean"},color:{value:new r.Color,type:"Color"},primarySize:{type:"Number",interpolable:!0,value:1,min:0,max:1e3},secondarySize:{type:"Number",interpolable:!0,value:10,min:0,max:1e3},distance:{type:"Number",interpolable:!0,value:500,min:0,max:500}})}get visible(){return this._config.visible.value}set visible(t){this._config.visible.value=t,this._component.visible=t}get color(){return this._config.color.value}set color(t){this._config.color.value=t,this._component.material.uniforms.uColor.value=t,this._component.material.uniformsNeedUpdate=!0}get primarySize(){return this._config.primarySize.value}set primarySize(t){this._config.primarySize.value=t,this._component.material.uniforms.uSize1.value=t,this._component.material.uniformsNeedUpdate=!0}get secondarySize(){return this._config.secondarySize.value}set secondarySize(t){this._config.secondarySize.value=t,this._component.material.uniforms.uSize2.value=t,this._component.material.uniformsNeedUpdate=!0}get distance(){return this._config.distance.value}set distance(t){this._config.distance.value=t,this._component.material.uniforms.uDistance.value=t,this._component.material.uniformsNeedUpdate=!0}}class Gi{constructor(t,i){e(this,"onDisposed",new U),e(this,"onSetup",new U),e(this,"isSetup",!1),e(this,"world"),e(this,"components"),e(this,"config"),e(this,"_defaultConfig",{visible:!0,color:new r.Color(12303291),primarySize:1,secondarySize:10,distance:500}),e(this,"three"),e(this,"_fade",3),e(this,"updateZoom",()=>{this.world.camera instanceof Yi&&(this.material.uniforms.uZoom.value=this.world.camera.three.zoom)}),this.world=i;const{color:s,primarySize:n,secondarySize:o,distance:a}=this._defaultConfig;this.components=t,this.config=new qi(this,this.components,"Grid");const l=new r.PlaneGeometry(2,2,1,1),h=new r.ShaderMaterial({side:r.DoubleSide,uniforms:{uSize1:{value:n},uSize2:{value:o},uColor:{value:s},uDistance:{value:a},uFade:{value:this._fade},uZoom:{value:1}},transparent:!0,vertexShader:"\n \n varying vec3 worldPosition;\n \n uniform float uDistance;\n \n void main() {\n \n vec3 pos = position.xzy * uDistance;\n pos.xz += cameraPosition.xz;\n \n worldPosition = pos;\n \n gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);\n \n }\n ",fragmentShader:"\n \n varying vec3 worldPosition;\n \n uniform float uZoom;\n uniform float uFade;\n uniform float uSize1;\n uniform float uSize2;\n uniform vec3 uColor;\n uniform float uDistance;\n \n \n \n float getGrid(float size) {\n \n vec2 r = worldPosition.xz / size;\n \n \n vec2 grid = abs(fract(r - 0.5) - 0.5) / fwidth(r);\n float line = min(grid.x, grid.y);\n \n \n return 1.0 - min(line, 1.0);\n }\n \n void main() {\n \n \n float d = 1.0 - min(distance(cameraPosition.xz, worldPosition.xz) / uDistance, 1.0);\n \n float g1 = getGrid(uSize1);\n float g2 = getGrid(uSize2);\n \n // Ortho camera fades the grid away when zooming out\n float minZoom = step(0.2, uZoom);\n float zoomFactor = pow(min(uZoom, 1.), 2.) * minZoom;\n \n gl_FragColor = vec4(uColor.rgb, mix(g2, g1, g1) * pow(d, uFade));\n gl_FragColor.a = mix(0.5 * gl_FragColor.a, gl_FragColor.a, g2) * zoomFactor;\n \n if ( gl_FragColor.a <= 0.0 ) discard;\n \n \n }\n \n ",extensions:{derivatives:!0}});this.three=new r.Mesh(l,h),this.three.frustumCulled=!1,i.scene.three.add(this.three),this.setupEvents(!0)}get visible(){return this.three.visible}set visible(t){if(this.three.visible=t,t){this.world.scene.three.add(this.three)}else this.three.removeFromParent()}get material(){return this.three.material}get fade(){return 3===this._fade}set fade(t){this._fade=t?3:0,this.material.uniforms.uFade.value=this._fade}setup(t){const e={...this._defaultConfig,...t};this.config.visible=!0,this.config.color=e.color,this.config.primarySize=e.primarySize,this.config.secondarySize=e.secondarySize,this.config.distance=e.distance,this.isSetup=!0,this.onSetup.trigger()}dispose(){this.setupEvents(!1);this.components.get(Te).list.delete(this.config.uuid);this.components.get($).destroy(this.three),this.onDisposed.trigger(),this.onDisposed.reset(),this.world=null,this.components=null}setupEvents(t){if(this.world.isDisposing)return;if(!(this.world.camera instanceof Yi))return;const e=this.world.camera.controls;t?e.addEventListener("update",this.updateZoom):e.removeEventListener("update",this.updateZoom)}}const Qi=class t extends F{constructor(i){super(i),e(this,"list",new Map),e(this,"onDisposed",new U),e(this,"enabled",!0),i.add(t.uuid,this)}create(t){if(this.list.has(t.uuid))throw new Error("This world already has a grid!");const e=new Gi(this.components,t);return this.list.set(t.uuid,e),t.onDisposed.add(()=>{this.delete(t)}),e}delete(t){const e=this.list.get(t.uuid);e&&e.dispose(),this.list.delete(t.uuid)}dispose(){for(const[t,e]of this.list)e.dispose();this.list.clear(),this.onDisposed.trigger(),this.onDisposed.reset()}};e(Qi,"uuid","d1e814d5-b81c-4452-87a2-f039375e0489");let Ki=Qi;const Ji=1.25,ts=65535,es=Math.pow(2,-24),is=Symbol("SKIP_GENERATION");function ss(t){return function(t){return t.index?t.index.count:t.attributes.position.count}(t)/3}function ns(t,e){if(!t.index){const i=t.attributes.position.count,s=function(t,e=ArrayBuffer){return t>65535?new Uint32Array(new e(4*t)):new Uint16Array(new e(2*t))}(i,e.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer);t.setIndex(new o(s,1));for(let t=0;t<i;t++)s[t]=t}}function rs(t){const e=ss(t),i=t.drawRange,s=i.start/3,n=(i.start+i.count)/3,r=Math.max(0,s),o=Math.min(e,n)-r;return[{offset:Math.floor(r),count:Math.floor(o)}]}function os(t){if(!t.groups||!t.groups.length)return rs(t);const e=[],i=new Set,s=t.drawRange,n=s.start/3,r=(s.start+s.count)/3;for(const e of t.groups){const t=e.start/3,s=(e.start+e.count)/3;i.add(Math.max(n,t)),i.add(Math.min(r,s))}const o=Array.from(i.values()).sort((t,e)=>t-e);for(let t=0;t<o.length-1;t++){const i=o[t],s=o[t+1];e.push({offset:Math.floor(i),count:Math.floor(s-i)})}return e}function as(t,e,i){return i.min.x=e[t],i.min.y=e[t+1],i.min.z=e[t+2],i.max.x=e[t+3],i.max.y=e[t+4],i.max.z=e[t+5],i}function ls(t){let e=-1,i=-1/0;for(let s=0;s<3;s++){const n=t[s+3]-t[s];n>i&&(i=n,e=s)}return e}function hs(t,e){e.set(t)}function cs(t,e,i){let s,n;for(let r=0;r<3;r++){const o=r+3;s=t[r],n=e[r],i[r]=s<n?s:n,s=t[o],n=e[o],i[o]=s>n?s:n}}function ds(t,e,i){for(let s=0;s<3;s++){const n=e[t+2*s],r=e[t+2*s+1],o=n-r,a=n+r;o<i[s]&&(i[s]=o),a>i[s+3]&&(i[s+3]=a)}}function us(t){const e=t[3]-t[0],i=t[4]-t[1],s=t[5]-t[2];return 2*(e*i+i*s+s*e)}function ps(t,e,i,s,n=null){let r=1/0,o=1/0,a=1/0,l=-1/0,h=-1/0,c=-1/0,d=1/0,u=1/0,p=1/0,f=-1/0,m=-1/0,g=-1/0;const _=null!==n;for(let s=6*e,n=6*(e+i);s<n;s+=6){const e=t[s+0],i=t[s+1],n=e-i,y=e+i;n<r&&(r=n),y>l&&(l=y),_&&e<d&&(d=e),_&&e>f&&(f=e);const w=t[s+2],v=t[s+3],b=w-v,x=w+v;b<o&&(o=b),x>h&&(h=x),_&&w<u&&(u=w),_&&w>m&&(m=w);const E=t[s+4],T=t[s+5],C=E-T,S=E+T;C<a&&(a=C),S>c&&(c=S),_&&E<p&&(p=E),_&&E>g&&(g=E)}s[0]=r,s[1]=o,s[2]=a,s[3]=l,s[4]=h,s[5]=c,_&&(n[0]=d,n[1]=u,n[2]=p,n[3]=f,n[4]=m,n[5]=g)}const fs=32,ms=(t,e)=>t.candidate-e.candidate,gs=new Array(fs).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),_s=new Float32Array(6);class ys{constructor(){}}function ws(t,e,i,s,n,r){let o=s,a=s+n-1;const l=r.pos,h=2*r.axis;for(;;){for(;o<=a&&i[6*o+h]<l;)o++;for(;o<=a&&i[6*a+h]>=l;)a--;if(!(o<a))return o;for(let t=0;t<3;t++){let i=e[3*o+t];e[3*o+t]=e[3*a+t],e[3*a+t]=i}for(let t=0;t<6;t++){let e=i[6*o+t];i[6*o+t]=i[6*a+t],i[6*a+t]=e}o++,a--}}function vs(t,e,i,s,n,r){let o=s,a=s+n-1;const l=r.pos,h=2*r.axis;for(;;){for(;o<=a&&i[6*o+h]<l;)o++;for(;o<=a&&i[6*a+h]>=l;)a--;if(!(o<a))return o;{let e=t[o];t[o]=t[a],t[a]=e;for(let t=0;t<6;t++){let e=i[6*o+t];i[6*o+t]=i[6*a+t],i[6*a+t]=e}o++,a--}}}function bs(t,e){const i=t.geometry,s=i.index?i.index.array:null,n=e.maxDepth,r=e.verbose,o=e.maxLeafTris,a=e.strategy,l=e.onProgress,h=ss(i),c=t._indirectBuffer;let d=!1;const u=new Float32Array(6),p=new Float32Array(6),f=function(t,e){var i;(i=e)[0]=i[1]=i[2]=1/0,i[3]=i[4]=i[5]=-1/0;const s=t.attributes.position,n=t.index?t.index.array:null,r=ss(t),o=new Float32Array(6*r),a=s.normalized,l=s.array,h=s.offset||0;let c=3;s.isInterleavedBufferAttribute&&(c=s.data.stride);const d=["getX","getY","getZ"];for(let t=0;t<r;t++){const i=3*t,r=6*t;let u=i+0,p=i+1,f=i+2;n&&(u=n[u],p=n[p],f=n[f]),a||(u=u*c+h,p=p*c+h,f=f*c+h);for(let t=0;t<3;t++){let i,n,h;a?(i=s[d[t]](u),n=s[d[t]](p),h=s[d[t]](f)):(i=l[u+t],n=l[p+t],h=l[f+t]);let c=i;n<c&&(c=n),h<c&&(c=h);let m=i;n>m&&(m=n),h>m&&(m=h);const g=(m-c)/2,_=2*t;o[r+_+0]=c+g,o[r+_+1]=g+(Math.abs(c)+g)*es,c<e[t]&&(e[t]=c),m>e[t+3]&&(e[t+3]=m)}}return o}(i,u),m=e.indirect?vs:ws,g=[],_=e.indirect?rs(i):os(i);if(1===_.length){const t=_[0],e=new ys;e.boundingData=u,function(t,e,i,s){let n=1/0,r=1/0,o=1/0,a=-1/0,l=-1/0,h=-1/0;for(let s=6*e,c=6*(e+i);s<c;s+=6){const e=t[s+0];e<n&&(n=e),e>a&&(a=e);const i=t[s+2];i<r&&(r=i),i>l&&(l=i);const c=t[s+4];c<o&&(o=c),c>h&&(h=c)}s[0]=n,s[1]=r,s[2]=o,s[3]=a,s[4]=l,s[5]=h}(f,t.offset,t.count,p),w(e,t.offset,t.count,p),g.push(e)}else for(let t of _){const e=new ys;e.boundingData=new Float32Array(6),ps(f,t.offset,t.count,e.boundingData,p),w(e,t.offset,t.count,p),g.push(e)}return g;function y(t){l&&l(t/h)}function w(t,e,l,h=null,u=0){if(!d&&u>=n&&(d=!0,r&&(console.warn(`MeshBVH: Max depth of ${n} reached when generating BVH. Consider increasing maxDepth.`),console.warn(i))),l<=o||u>=n)return y(e+l),t.offset=e,t.count=l,t;const g=function(t,e,i,s,n,r){let o=-1,a=0;if(0===r)o=ls(e),-1!==o&&(a=(e[o]+e[o+3])/2);else if(1===r)o=ls(t),-1!==o&&(a=function(t,e,i,s){let n=0;for(let r=e,o=e+i;r<o;r++)n+=t[6*r+2*s];return n/i}(i,s,n,o));else if(2===r){const r=us(t);let l=Ji*n;const h=6*s,c=6*(s+n);for(let t=0;t<3;t++){const s=e[t],d=(e[t+3]-s)/fs;if(n<8){const e=[...gs];e.length=n;let s=0;for(let n=h;n<c;n+=6,s++){const r=e[s];r.candidate=i[n+2*t],r.count=0;const{bounds:o,leftCacheBounds:a,rightCacheBounds:l}=r;for(let t=0;t<3;t++)l[t]=1/0,l[t+3]=-1/0,a[t]=1/0,a[t+3]=-1/0,o[t]=1/0,o[t+3]=-1/0;ds(n,i,o)}e.sort(ms);let d=n;for(let t=0;t<d;t++){const i=e[t];for(;t+1<d&&e[t+1].candidate===i.candidate;)e.splice(t+1,1),d--}for(let s=h;s<c;s+=6){const n=i[s+2*t];for(let t=0;t<d;t++){const r=e[t];n>=r.candidate?ds(s,i,r.rightCacheBounds):(ds(s,i,r.leftCacheBounds),r.count++)}}for(let i=0;i<d;i++){const s=e[i],h=s.count,c=n-s.count,d=s.leftCacheBounds,u=s.rightCacheBounds;let p=0;0!==h&&(p=us(d)/r);let f=0;0!==c&&(f=us(u)/r);const m=1+Ji*(p*h+f*c);m<l&&(o=t,l=m,a=s.candidate)}}else{for(let t=0;t<fs;t++){const e=gs[t];e.count=0,e.candidate=s+d+t*d;const i=e.bounds;for(let t=0;t<3;t++)i[t]=1/0,i[t+3]=-1/0}for(let e=h;e<c;e+=6){let n=~~((i[e+2*t]-s)/d);n>=fs&&(n=31);const r=gs[n];r.count++,ds(e,i,r.bounds)}const e=gs[31];hs(e.bounds,e.rightCacheBounds);for(let t=30;t>=0;t--){const e=gs[t],i=gs[t+1];cs(e.bounds,i.rightCacheBounds,e.rightCacheBounds)}let u=0;for(let e=0;e<31;e++){const i=gs[e],s=i.count,h=i.bounds,c=gs[e+1].rightCacheBounds;0!==s&&(0===u?hs(h,_s):cs(h,_s,_s)),u+=s;let d=0,p=0;0!==u&&(d=us(_s)/r);const f=n-u;0!==f&&(p=us(c)/r);const m=1+Ji*(d*u+p*f);m<l&&(o=t,l=m,a=i.candidate)}}}}else console.warn(`MeshBVH: Invalid build strategy value ${r} used.`);return{axis:o,pos:a}}(t.boundingData,h,f,e,l,a);if(-1===g.axis)return y(e+l),t.offset=e,t.count=l,t;const _=m(c,s,f,e,l,g);if(_===e||_===e+l)y(e+l),t.offset=e,t.count=l;else{t.splitAxis=g.axis;const i=new ys,s=e,n=_-e;t.left=i,i.boundingData=new Float32Array(6),ps(f,s,n,i.boundingData,p),w(i,s,n,p,u+1);const r=new ys,o=_,a=l-n;t.right=r,r.boundingData=new Float32Array(6),ps(f,o,a,r.boundingData,p),w(r,o,a,p,u+1)}return t}}function xs(t,e){const i=t.geometry;e.indirect&&(t._indirectBuffer=function(t,e){const i=(t.index?t.index.count:t.attributes.position.count)/3,s=i>65536,n=s?4:2,r=e?new SharedArrayBuffer(i*n):new ArrayBuffer(i*n),o=s?new Uint32Array(r):new Uint16Array(r);for(let t=0,e=o.length;t<e;t++)o[t]=t;return o}(i,e.useSharedArrayBuffer),function(t){if(0===t.groups.length)return!1;const e=ss(t),i=os(t).sort((t,e)=>t.offset-e.offset),s=i[i.length-1];s.count=Math.min(e-s.offset,s.count);let n=0;return i.forEach(({count:t})=>n+=t),e!==n}(i)&&!e.verbose&&console.warn('MeshBVH: Provided geometry contains groups that do not fully span the vertex contents while using the "indirect" option. BVH may incorrectly report intersections on unrendered portions of the geometry.')),t._indirectBuffer||ns(i,e);const s=bs(t,e);let n,r,o;const a=[],l=e.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer;for(let t=0;t<s.length;t++){const e=s[t];const i=new l(32*h(e));n=new Float32Array(i),r=new Uint32Array(i),o=new Uint16Array(i),c(0,e),a.push(i)}return void(t._roots=a);function h(t){return t.count?1:1+h(t.left)+h(t.right)}function c(t,e){const i=t/4,s=t/2,a=!!e.count,l=e.boundingData;for(let t=0;t<6;t++)n[i+t]=l[t];if(a){const n=e.offset,a=e.count;return r[i+6]=n,o[s+14]=a,o[s+15]=ts,t+32}{const s=e.left,n=e.right,o=e.splitAxis;let a;if(a=c(t+32,s),a/4>Math.pow(2,32))throw new Error("MeshBVH: Cannot store child pointer greater than 32 bits.");return r[i+6]=a/4,a=c(a,n),r[i+7]=o,a}}}class Es{constructor(){this.min=1/0,this.max=-1/0}setFromPointsField(t,e){let i=1/0,s=-1/0;for(let n=0,r=t.length;n<r;n++){const r=t[n][e];i=r<i?r:i,s=r>s?r:s}this.min=i,this.max=s}setFromPoints(t,e){let i=1/0,s=-1/0;for(let n=0,r=e.length;n<r;n++){const r=e[n],o=t.dot(r);i=o<i?o:i,s=o>s?o:s}this.min=i,this.max=s}isSeparated(t){return this.min>t.max||t.min>this.max}}Es.prototype.setFromBox=function(){const t=new a;return function(e,i){const s=i.min,n=i.max;let r=1/0,o=-1/0;for(let i=0;i<=1;i++)for(let a=0;a<=1;a++)for(let l=0;l<=1;l++){t.x=s.x*i+n.x*(1-i),t.y=s.y*a+n.y*(1-a),t.z=s.z*l+n.z*(1-l);const h=e.dot(t);r=Math.min(h,r),o=Math.max(h,o)}this.min=r,this.max=o}}();const Ts=function(){const t=new a,e=new a,i=new a;return function(s,n,r){const o=s.start,a=t,l=n.start,h=e;i.subVectors(o,l),t.subVectors(s.end,s.start),e.subVectors(n.end,n.start);const c=i.dot(h),d=h.dot(a),u=h.dot(h),p=i.dot(a),f=a.dot(a)*u-d*d;let m,g;m=0!==f?(c*d-p*u)/f:0,g=(c+m*d)/u,r.x=m,r.y=g}}(),Cs=function(){const t=new c,e=new a,i=new a;return function(s,n,r,o){Ts(s,n,t);let a=t.x,l=t.y;if(a>=0&&a<=1&&l>=0&&l<=1)return s.at(a,r),void n.at(l,o);if(a>=0&&a<=1)return l<0?n.at(0,o):n.at(1,o),void s.closestPointToPoint(o,!0,r);if(l>=0&&l<=1)return a<0?s.at(0,r):s.at(1,r),void n.closestPointToPoint(r,!0,o);{let t,h;t=a<0?s.start:s.end,h=l<0?n.start:n.end;const c=e,d=i;return s.closestPointToPoint(h,!0,e),n.closestPointToPoint(t,!0,i),c.distanceToSquared(h)<=d.distanceToSquared(t)?(r.copy(c),void o.copy(h)):(r.copy(t),void o.copy(d))}}}(),Ss=function(){const t=new a,e=new a,i=new l,s=new h;return function(n,r){const{radius:o,center:a}=n,{a:l,b:h,c:c}=r;s.start=l,s.end=h;if(s.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;s.start=l,s.end=c;if(s.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;s.start=h,s.end=c;if(s.closestPointToPoint(a,!0,t).distanceTo(a)<=o)return!0;const d=r.getPlane(i);if(Math.abs(d.distanceToPoint(a))<=o){const t=d.projectPoint(a,e);if(r.containsPoint(t))return!0}return!1}}();function As(t){return Math.abs(t)<1e-15}class Os extends d{constructor(...t){super(...t),this.isExtendedTriangle=!0,this.satAxes=new Array(4).fill().map(()=>new a),this.satBounds=new Array(4).fill().map(()=>new Es),this.points=[this.a,this.b,this.c],this.sphere=new u,this.plane=new l,this.needsUpdate=!0}intersectsSphere(t){return Ss(t,this)}update(){const t=this.a,e=this.b,i=this.c,s=this.points,n=this.satAxes,r=this.satBounds,o=n[0],a=r[0];this.getNormal(o),a.setFromPoints(o,s);const l=n[1],h=r[1];l.subVectors(t,e),h.setFromPoints(l,s);const c=n[2],d=r[2];c.subVectors(e,i),d.setFromPoints(c,s);const u=n[3],p=r[3];u.subVectors(i,t),p.setFromPoints(u,s),this.sphere.setFromPoints(this.points),this.plane.setFromNormalAndCoplanarPoint(o,t),this.needsUpdate=!1}}Os.prototype.closestPointToSegment=function(){const t=new a,e=new a,i=new h;return function(s,n=null,r=null){const{start:o,end:a}=s,l=this.points;let h,c=1/0;for(let o=0;o<3;o++){const a=(o+1)%3;i.start.copy(l[o]),i.end.copy(l[a]),Cs(i,s,t,e),h=t.distanceToSquared(e),h<c&&(c=h,n&&n.copy(t),r&&r.copy(e))}return this.closestPointToPoint(o,t),h=o.distanceToSquared(t),h<c&&(c=h,n&&n.copy(t),r&&r.copy(o)),this.closestPointToPoint(a,t),h=a.distanceToSquared(t),h<c&&(c=h,n&&n.copy(t),r&&r.copy(a)),Math.sqrt(c)}}(),Os.prototype.intersectsTriangle=function(){const t=new Os,e=new Array(3),i=new Array(3),s=new Es,n=new Es,r=new a,o=new a,l=new a,c=new a,d=new a,u=new h,p=new h,f=new h,m=new a;function g(t,e,i){const s=t.points;let n=0,r=-1;for(let t=0;t<3;t++){const{start:a,end:l}=u;a.copy(s[t]),l.copy(s[(t+1)%3]),u.delta(o);const h=As(e.distanceToPoint(a));if(As(e.normal.dot(o))&&h){i.copy(u),n=2;break}const c=e.intersectLine(u,m);if(!c&&h&&m.copy(a),(c||h)&&!As(m.distanceTo(l))){if(n<=1){(1===n?i.start:i.end).copy(m),h&&(r=n)}else if(n>=2){(1===r?i.start:i.end).copy(m),n=2;break}if(n++,2===n&&-1===r)break}}return n}return function(o,a=null,h=!1){this.needsUpdate&&this.update(),o.isExtendedTriangle?o.needsUpdate&&o.update():(t.copy(o),t.update(),o=t);const u=this.plane,m=o.plane;if(Math.abs(u.normal.dot(m.normal))>1-1e-10){const t=this.satBounds,l=this.satAxes;i[0]=o.a,i[1]=o.b,i[2]=o.c;for(let e=0;e<4;e++){const n=t[e],r=l[e];if(s.setFromPoints(r,i),n.isSeparated(s))return!1}const c=o.satBounds,d=o.satAxes;e[0]=this.a,e[1]=this.b,e[2]=this.c;for(let t=0;t<4;t++){const i=c[t],n=d[t];if(s.setFromPoints(n,e),i.isSeparated(s))return!1}for(let t=0;t<4;t++){const o=l[t];for(let t=0;t<4;t++){const a=d[t];if(r.crossVectors(o,a),s.setFromPoints(r,e),n.setFromPoints(r,i),s.isSeparated(n))return!1}}return a&&(h||console.warn("ExtendedTriangle.intersectsTriangle: Triangles are coplanar which does not support an output edge. Setting edge to 0, 0, 0."),a.start.set(0,0,0),a.end.set(0,0,0)),!0}{const t=g(this,m,p);if(1===t&&o.containsPoint(p.end))return a&&(a.start.copy(p.end),a.end.copy(p.end)),!0;if(2!==t)return!1;const e=g(o,u,f);if(1===e&&this.containsPoint(f.end))return a&&(a.start.copy(f.end),a.end.copy(f.end)),!0;if(2!==e)return!1;if(p.delta(l),f.delta(c),l.dot(c)<0){let t=f.start;f.start=f.end,f.end=t}const i=p.start.dot(l),s=p.end.dot(l),n=f.start.dot(l),r=f.end.dot(l);return(i===r||n===s||s<n!==i<r)&&(a&&(d.subVectors(p.start,f.start),d.dot(l)>0?a.start.copy(p.start):a.start.copy(f.start),d.subVectors(p.end,f.end),d.dot(l)<0?a.end.copy(p.end):a.end.copy(f.end)),!0)}}}(),Os.prototype.distanceToPoint=function(){const t=new a;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),Os.prototype.distanceToTriangle=function(){const t=new a,e=new a,i=["a","b","c"],s=new h,n=new h;return function(r,o=null,a=null){const l=o||a?s:null;if(this.intersectsTriangle(r,l))return(o||a)&&(o&&l.getCenter(o),a&&l.getCenter(a)),0;let h=1/0;for(let e=0;e<3;e++){let s;const n=i[e],l=r[n];this.closestPointToPoint(l,t),s=l.distanceToSquared(t),s<h&&(h=s,o&&o.copy(t),a&&a.copy(l));const c=this[n];r.closestPointToPoint(c,t),s=c.distanceToSquared(t),s<h&&(h=s,o&&o.copy(c),a&&a.copy(t))}for(let l=0;l<3;l++){const c=i[l],d=i[(l+1)%3];s.set(this[c],this[d]);for(let l=0;l<3;l++){const c=i[l],d=i[(l+1)%3];n.set(r[c],r[d]),Cs(s,n,t,e);const u=t.distanceToSquared(e);u<h&&(h=u,o&&o.copy(t),a&&a.copy(e))}}return Math.sqrt(h)}}();class Ps{constructor(t,e,i){this.isOrientedBox=!0,this.min=new a,this.max=new a,this.matrix=new p,this.invMatrix=new p,this.points=new Array(8).fill().map(()=>new a),this.satAxes=new Array(3).fill().map(()=>new a),this.satBounds=new Array(3).fill().map(()=>new Es),this.alignedSatBounds=new Array(3).fill().map(()=>new Es),this.needsUpdate=!1,t&&this.min.copy(t),e&&this.max.copy(e),i&&this.matrix.copy(i)}set(t,e,i){this.min.copy(t),this.max.copy(e),this.matrix.copy(i),this.needsUpdate=!0}copy(t){this.min.copy(t.min),this.max.copy(t.max),this.matrix.copy(t.matrix),this.needsUpdate=!0}}Ps.prototype.update=function(){return function(){const t=this.matrix,e=this.min,i=this.max,s=this.points;for(let n=0;n<=1;n++)for(let r=0;r<=1;r++)for(let o=0;o<=1;o++){const a=s[1*n|2*r|4*o];a.x=n?i.x:e.x,a.y=r?i.y:e.y,a.z=o?i.z:e.z,a.applyMatrix4(t)}const n=this.satBounds,r=this.satAxes,o=s[0];for(let t=0;t<3;t++){const e=r[t],i=n[t],a=s[1<<t];e.subVectors(o,a),i.setFromPoints(e,s)}const a=this.alignedSatBounds;a[0].setFromPointsField(s,"x"),a[1].setFromPointsField(s,"y"),a[2].setFromPointsField(s,"z"),this.invMatrix.copy(this.matrix).invert(),this.needsUpdate=!1}}(),Ps.prototype.intersectsBox=function(){const t=new Es;return function(e){this.needsUpdate&&this.update();const i=e.min,s=e.max,n=this.satBounds,r=this.satAxes,o=this.alignedSatBounds;if(t.min=i.x,t.max=s.x,o[0].isSeparated(t))return!1;if(t.min=i.y,t.max=s.y,o[1].isSeparated(t))return!1;if(t.min=i.z,t.max=s.z,o[2].isSeparated(t))return!1;for(let i=0;i<3;i++){const s=r[i],o=n[i];if(t.setFromBox(s,e),o.isSeparated(t))return!1}return!0}}(),Ps.prototype.intersectsTriangle=function(){const t=new Os,e=new Array(3),i=new Es,s=new Es,n=new a;return function(r){this.needsUpdate&&this.update(),r.isExtendedTriangle?r.needsUpdate&&r.update():(t.copy(r),t.update(),r=t);const o=this.satBounds,a=this.satAxes;e[0]=r.a,e[1]=r.b,e[2]=r.c;for(let t=0;t<3;t++){const s=o[t],n=a[t];if(i.setFromPoints(n,e),s.isSeparated(i))return!1}const l=r.satBounds,h=r.satAxes,c=this.points;for(let t=0;t<3;t++){const e=l[t],s=h[t];if(i.setFromPoints(s,c),e.isSeparated(i))return!1}for(let t=0;t<3;t++){const r=a[t];for(let t=0;t<4;t++){const o=h[t];if(n.crossVectors(r,o),i.setFromPoints(n,e),s.setFromPoints(n,c),i.isSeparated(s))return!1}}return!0}}(),Ps.prototype.closestPointToPoint=function(){return function(t,e){return this.needsUpdate&&this.update(),e.copy(t).applyMatrix4(this.invMatrix).clamp(this.min,this.max).applyMatrix4(this.matrix),e}}(),Ps.prototype.distanceToPoint=function(){const t=new a;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),Ps.prototype.distanceToBox=function(){const t=["x","y","z"],e=new Array(12).fill().map(()=>new h),i=new Array(12).fill().map(()=>new h),s=new a,n=new a;return function(r,o=0,a=null,l=null){if(this.needsUpdate&&this.update(),this.intersectsBox(r))return(a||l)&&(r.getCenter(n),this.closestPointToPoint(n,s),r.closestPointToPoint(s,n),a&&a.copy(s),l&&l.copy(n)),0;const h=o*o,c=r.min,d=r.max,u=this.points;let p=1/0;for(let t=0;t<8;t++){const e=u[t];n.copy(e).clamp(c,d);const i=e.distanceToSquared(n);if(i<p&&(p=i,a&&a.copy(e),l&&l.copy(n),i<h))return Math.sqrt(i)}let f=0;for(let s=0;s<3;s++)for(let n=0;n<=1;n++)for(let r=0;r<=1;r++){const o=(s+1)%3,a=(s+2)%3,l=1<<s|n<<o|r<<a,h=u[n<<o|r<<a],p=u[l];e[f].set(h,p);const m=t[s],g=t[o],_=t[a],y=i[f],w=y.start,v=y.end;w[m]=c[m],w[g]=n?c[g]:d[g],w[_]=r?c[_]:d[g],v[m]=d[m],v[g]=n?c[g]:d[g],v[_]=r?c[_]:d[g],f++}for(let t=0;t<=1;t++)for(let e=0;e<=1;e++)for(let i=0;i<=1;i++){n.x=t?d.x:c.x,n.y=e?d.y:c.y,n.z=i?d.z:c.z,this.closestPointToPoint(n,s);const r=n.distanceToSquared(s);if(r<p&&(p=r,a&&a.copy(s),l&&l.copy(n),r<h))return Math.sqrt(r)}for(let t=0;t<12;t++){const r=e[t];for(let t=0;t<12;t++){const e=i[t];Cs(r,e,s,n);const o=s.distanceToSquared(n);if(o<p&&(p=o,a&&a.copy(s),l&&l.copy(n),o<h))return Math.sqrt(o)}}return Math.sqrt(p)}}();class Is{constructor(t){this._getNewPrimitive=t,this._primitives=[]}getPrimitive(){const t=this._primitives;return 0===t.length?this._getNewPrimitive():t.pop()}releasePrimitive(t){this._primitives.push(t)}}class ks extends Is{constructor(){super(()=>new Os)}}const Ds=new ks;function zs(t,e){return 65535===e[t+15]}function Ms(t,e){return e[t+6]}function Bs(t,e){return e[t+14]}function Ns(t){return t+8}function Us(t,e){return e[t+6]}function Ls(t,e){return e[t+7]}function Rs(t){return t}const Fs=new class{constructor(){this.float32Array=null,this.uint16Array=null,this.uint32Array=null;const t=[];let e=null;this.setBuffer=i=>{e&&t.push(e),e=i,this.float32Array=new Float32Array(i),this.uint16Array=new Uint16Array(i),this.uint32Array=new Uint32Array(i)},this.clearBuffer=()=>{e=null,this.float32Array=null,this.uint16Array=null,this.uint32Array=null,0!==t.length&&this.setBuffer(t.pop())}}};let Vs,js;const Zs=[],Hs=new Is(()=>new f);function $s(t,e,i,s,n,r){Vs=Hs.getPrimitive(),js=Hs.getPrimitive(),Zs.push(Vs,js),Fs.setBuffer(t._roots[e]);const o=Ys(0,t.geometry,i,s,n,r);Fs.clearBuffer(),Hs.releasePrimitive(Vs),Hs.releasePrimitive(js),Zs.pop(),Zs.pop();const a=Zs.length;return a>0&&(js=Zs[a-1],Vs=Zs[a-2]),o}function Ys(t,e,i,s,n=null,r=0,o=0){const{float32Array:a,uint16Array:l,uint32Array:h}=Fs;let c=2*t;if(zs(c,l)){const e=Ms(t,h),i=Bs(c,l);return as(t,a,Vs),s(e,i,!1,o,r+t,Vs)}{let c=function(t){const{uint16Array:e,uint32Array:i}=Fs;let s=2*t;for(;!zs(s,e);)s=2*(t=Ns(t));return Ms(t,i)},d=function(t){const{uint16Array:e,uint32Array:i}=Fs;let s=2*t;for(;!zs(s,e);)s=2*(t=Us(t,i));return Ms(t,i)+Bs(s,e)};const u=Ns(t),p=Us(t,h);let f,m,g,_,y=u,w=p;if(n&&(g=Vs,_=js,as(y,a,g),as(w,a,_),f=n(g),m=n(_),m<f)){y=p,w=u;const t=f;f=m,m=t,g=_}g||(g=Vs,as(y,a,g));const v=i(g,zs(2*y,l),f,o+1,r+y);let b;if(2===v){const t=c(y);b=s(t,d(y)-t,!0,o+1,r+y,g)}else b=v&&Ys(y,e,i,s,n,r,o+1);if(b)return!0;_=js,as(w,a,_);const x=i(_,zs(2*w,l),m,o+1,r+w);let E;if(2===x){const t=c(w);E=s(t,d(w)-t,!0,o+1,r+w,_)}else E=x&&Ys(w,e,i,s,n,r,o+1);return!!E}}const Ws=new a,Xs=new a;const qs=new a,Gs=new a,Qs=new a,Ks=new c,Js=new c,tn=new c,en=new a,sn=new a,nn=new a,rn=new a;function on(t,e,i,s,n,r,o,l,h){qs.fromBufferAttribute(e,r),Gs.fromBufferAttribute(e,o),Qs.fromBufferAttribute(e,l);const u=function(t,e,i,s,n,r){let o;return o=r===m?t.intersectTriangle(s,i,e,!0,n):t.intersectTriangle(e,i,s,r!==g,n),null===o?null:{distance:t.origin.distanceTo(n),point:n.clone()}}(t,qs,Gs,Qs,rn,h);if(u){s&&(Ks.fromBufferAttribute(s,r),Js.fromBufferAttribute(s,o),tn.fromBufferAttribute(s,l),u.uv=d.getInterpolation(rn,qs,Gs,Qs,Ks,Js,tn,new c)),n&&(Ks.fromBufferAttribute(n,r),Js.fromBufferAttribute(n,o),tn.fromBufferAttribute(n,l),u.uv1=d.getInterpolation(rn,qs,Gs,Qs,Ks,Js,tn,new c)),i&&(en.fromBufferAttribute(i,r),sn.fromBufferAttribute(i,o),nn.fromBufferAttribute(i,l),u.normal=d.getInterpolation(rn,qs,Gs,Qs,en,sn,nn,new a),u.normal.dot(t.direction)>0&&u.normal.multiplyScalar(-1));const e={a:r,b:o,c:l,normal:new a,materialIndex:0};d.getNormal(qs,Gs,Qs,e.normal),u.face=e,u.faceIndex=r}return u}function an(t,e,i,s,n){const r=3*s;let o=r+0,a=r+1,l=r+2;const h=t.index;t.index&&(o=h.getX(o),a=h.getX(a),l=h.getX(l));const{position:c,normal:d,uv:u,uv1:p}=t.attributes,f=on(i,c,d,u,p,o,a,l,e);return f?(f.faceIndex=s,n&&n.push(f),f):null}function ln(t,e,i,s){const n=t.a,r=t.b,o=t.c;let a=e,l=e+1,h=e+2;i&&(a=i.getX(a),l=i.getX(l),h=i.getX(h)),n.x=s.getX(a),n.y=s.getY(a),n.z=s.getZ(a),r.x=s.getX(l),r.y=s.getY(l),r.z=s.getZ(l),o.x=s.getX(h),o.y=s.getY(h),o.z=s.getZ(h)}function hn(t,e,i,s,n,r,o){const{geometry:a}=i,{index:l}=a,h=a.attributes.position;for(let i=t,a=e+t;i<a;i++){let t;if(t=i,ln(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}function cn(t,e=null){e&&Array.isArray(e)&&(e=new Set(e));const i=t.geometry,s=i.index?i.index.array:null,n=i.attributes.position;let r,o,a,l,h=0;const c=t._roots;for(let t=0,e=c.length;t<e;t++)r=c[t],o=new Uint32Array(r),a=new Uint16Array(r),l=new Float32Array(r),d(0,h),h+=r.byteLength;function d(t,i,r=!1){const h=2*t;if(a[h+15]===ts){const e=o[t+6];let i=1/0,r=1/0,c=1/0,d=-1/0,u=-1/0,p=-1/0;for(let t=3*e,o=3*(e+a[h+14]);t<o;t++){let e=s[t];const o=n.getX(e),a=n.getY(e),l=n.getZ(e);o<i&&(i=o),o>d&&(d=o),a<r&&(r=a),a>u&&(u=a),l<c&&(c=l),l>p&&(p=l)}return(l[t+0]!==i||l[t+1]!==r||l[t+2]!==c||l[t+3]!==d||l[t+4]!==u||l[t+5]!==p)&&(l[t+0]=i,l[t+1]=r,l[t+2]=c,l[t+3]=d,l[t+4]=u,l[t+5]=p,!0)}{const s=t+8,n=o[t+6],a=s+i,h=n+i;let c=r,u=!1,p=!1;e?c||(u=e.has(a),p=e.has(h),c=!u&&!p):(u=!0,p=!0);const f=c||p;let m=!1;(c||u)&&(m=d(s,i,c));let g=!1;f&&(g=d(n,i,c));const _=m||g;if(_)for(let e=0;e<3;e++){const i=s+e,r=n+e,o=l[i],a=l[i+3],h=l[r],c=l[r+3];l[t+e]=o<h?o:h,l[t+e+3]=a>c?a:c}return _}}}const dn=new f;function un(t,e,i,s){return as(t,e,dn),i.intersectBox(dn,s)}function pn(t,e,i,s,n,r,o){const{geometry:a}=i,{index:l}=a,h=a.attributes.position;for(let a=t,c=e+t;a<c;a++){let t;if(t=i.resolveTriangleIndex(a),ln(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}const fn=new a;function mn(t,e,i,s,n){Fs.setBuffer(t._roots[e]),gn(0,t,i,s,n),Fs.clearBuffer()}function gn(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=Fs,l=2*t;if(zs(l,o)){!function(t,e,i,s,n,r){const{geometry:o,_indirectBuffer:a}=t;for(let t=s,a=s+n;t<a;t++)an(o,e,i,t,r)}(e,i,s,Ms(t,a),Bs(l,o),n)}else{const o=Ns(t);un(o,r,s,fn)&&gn(o,e,i,s,n);const l=Us(t,a);un(l,r,s,fn)&&gn(l,e,i,s,n)}}const _n=new a,yn=["x","y","z"];function wn(t,e,i,s){Fs.setBuffer(t._roots[e]);const n=vn(0,t,i,s);return Fs.clearBuffer(),n}function vn(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=Fs;let a=2*t;if(zs(a,r)){return function(t,e,i,s,n){const{geometry:r,_indirectBuffer:o}=t;let a=1/0,l=null;for(let t=s,o=s+n;t<o;t++){let s;s=an(r,e,i,t),s&&s.distance<a&&(l=s,a=s.distance)}return l}(e,i,s,Ms(t,o),Bs(a,r))}{const r=Ls(t,o),a=yn[r],l=s.direction[a]>=0;let h,c;l?(h=Ns(t),c=Us(t,o)):(h=Us(t,o),c=Ns(t));const d=un(h,n,s,_n)?vn(h,e,i,s):null;if(d){const t=d.point[a];if(l?t<=n[c+r]:t>=n[c+r+3])return d}const u=un(c,n,s,_n)?vn(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const bn=new f,xn=new Os,En=new Os,Tn=new p,Cn=new Ps,Sn=new Ps;function An(t,e,i,s){Fs.setBuffer(t._roots[e]);const n=On(0,t,i,s);return Fs.clearBuffer(),n}function On(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=Fs;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),Cn.set(i.boundingBox.min,i.boundingBox.max,s),n=Cn);if(!zs(l,o)){const o=t+8,l=a[t+6];as(o,r,bn);if(n.intersectsBox(bn)&&On(o,e,i,s,n))return!0;as(l,r,bn);return!!(n.intersectsBox(bn)&&On(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=Ms(t,a),f=Bs(l,o);if(Tn.copy(s).invert(),i.boundsTree){as(t,r,Sn),Sn.matrix.copy(Tn),Sn.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>Sn.intersectsBox(t),intersectsTriangle:t=>{t.a.applyMatrix4(s),t.b.applyMatrix4(s),t.c.applyMatrix4(s),t.needsUpdate=!0;for(let e=3*p,i=3*(f+p);e<i;e+=3)if(ln(En,e,h,c),En.needsUpdate=!0,t.intersectsTriangle(En))return!0;return!1}})}for(let t=3*p,e=3*(f+p);t<e;t+=3){ln(xn,t,h,c),xn.a.applyMatrix4(Tn),xn.b.applyMatrix4(Tn),xn.c.applyMatrix4(Tn),xn.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(ln(En,t,d,u),En.needsUpdate=!0,xn.intersectsTriangle(En))return!0}}}const Pn=new p,In=new Ps,kn=new Ps,Dn=new a,zn=new a,Mn=new a,Bn=new a;function Nn(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),In.set(e.boundingBox.min,e.boundingBox.max,i),In.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=Ds.getPrimitive(),p=Ds.getPrimitive();let f=Dn,m=zn,g=null,_=null;n&&(g=Mn,_=Bn);let y=1/0,w=null,v=null;return Pn.copy(i).invert(),kn.matrix.copy(Pn),t.shapecast({boundsTraverseOrder:t=>In.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(kn.min.copy(t.min),kn.max.copy(t.max),kn.needsUpdate=!0),!0),intersectsRange:(t,s)=>{if(e.boundsTree){return e.boundsTree.shapecast({boundsTraverseOrder:t=>kn.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o,intersectsRange:(e,n)=>{for(let o=e,a=e+n;o<a;o++){ln(p,3*o,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=t,i=t+s;e<i;e++){ln(u,3*e,h,l),u.needsUpdate=!0;const t=u.distanceToTriangle(p,f,g);if(t<y&&(m.copy(f),_&&_.copy(g),y=t,w=e,v=o),t<r)return!0}}}})}for(let n=0,o=ss(e);n<o;n++){ln(p,3*n,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=t,i=t+s;e<i;e++){ln(u,3*e,h,l),u.needsUpdate=!0;const t=u.distanceToTriangle(p,f,g);if(t<y&&(m.copy(f),_&&_.copy(g),y=t,w=e,v=n),t<r)return!0}}}}),Ds.releasePrimitive(u),Ds.releasePrimitive(p),y===1/0?null:(s.point?s.point.copy(m):s.point=m.clone(),s.distance=y,s.faceIndex=w,n&&(n.point?n.point.copy(_):n.point=_.clone(),n.point.applyMatrix4(Pn),m.applyMatrix4(Pn),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}function Un(t,e=null){e&&Array.isArray(e)&&(e=new Set(e));const i=t.geometry,s=i.index?i.index.array:null,n=i.attributes.position;let r,o,a,l,h=0;const c=t._roots;for(let t=0,e=c.length;t<e;t++)r=c[t],o=new Uint32Array(r),a=new Uint16Array(r),l=new Float32Array(r),d(0,h),h+=r.byteLength;function d(i,r,h=!1){const c=2*i;if(a[c+15]===ts){const e=o[i+6];let r=1/0,h=1/0,d=1/0,u=-1/0,p=-1/0,f=-1/0;for(let i=e,o=e+a[c+14];i<o;i++){const e=3*t.resolveTriangleIndex(i);for(let t=0;t<3;t++){let i=e+t;i=s?s[i]:i;const o=n.getX(i),a=n.getY(i),l=n.getZ(i);o<r&&(r=o),o>u&&(u=o),a<h&&(h=a),a>p&&(p=a),l<d&&(d=l),l>f&&(f=l)}}return(l[i+0]!==r||l[i+1]!==h||l[i+2]!==d||l[i+3]!==u||l[i+4]!==p||l[i+5]!==f)&&(l[i+0]=r,l[i+1]=h,l[i+2]=d,l[i+3]=u,l[i+4]=p,l[i+5]=f,!0)}{const t=i+8,s=o[i+6],n=t+r,a=s+r;let c=h,u=!1,p=!1;e?c||(u=e.has(n),p=e.has(a),c=!u&&!p):(u=!0,p=!0);const f=c||p;let m=!1;(c||u)&&(m=d(t,r,c));let g=!1;f&&(g=d(s,r,c));const _=m||g;if(_)for(let e=0;e<3;e++){const n=t+e,r=s+e,o=l[n],a=l[n+3],h=l[r],c=l[r+3];l[i+e]=o<h?o:h,l[i+e+3]=a>c?a:c}return _}}}const Ln=new a;function Rn(t,e,i,s,n){Fs.setBuffer(t._roots[e]),Fn(0,t,i,s,n),Fs.clearBuffer()}function Fn(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=Fs,l=2*t;if(zs(l,o)){!function(t,e,i,s,n,r){const{geometry:o,_indirectBuffer:a}=t;for(let t=s,l=s+n;t<l;t++)an(o,e,i,a?a[t]:t,r)}(e,i,s,Ms(t,a),Bs(l,o),n)}else{const o=Ns(t);un(o,r,s,Ln)&&Fn(o,e,i,s,n);const l=Us(t,a);un(l,r,s,Ln)&&Fn(l,e,i,s,n)}}const Vn=new a,jn=["x","y","z"];function Zn(t,e,i,s){Fs.setBuffer(t._roots[e]);const n=Hn(0,t,i,s);return Fs.clearBuffer(),n}function Hn(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=Fs;let a=2*t;if(zs(a,r)){return function(t,e,i,s,n){const{geometry:r,_indirectBuffer:o}=t;let a=1/0,l=null;for(let t=s,h=s+n;t<h;t++){let s;s=an(r,e,i,o?o[t]:t),s&&s.distance<a&&(l=s,a=s.distance)}return l}(e,i,s,Ms(t,o),Bs(a,r))}{const r=Ls(t,o),a=jn[r],l=s.direction[a]>=0;let h,c;l?(h=Ns(t),c=Us(t,o)):(h=Us(t,o),c=Ns(t));const d=un(h,n,s,Vn)?Hn(h,e,i,s):null;if(d){const t=d.point[a];if(l?t<=n[c+r]:t>=n[c+r+3])return d}const u=un(c,n,s,Vn)?Hn(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const $n=new f,Yn=new Os,Wn=new Os,Xn=new p,qn=new Ps,Gn=new Ps;function Qn(t,e,i,s){Fs.setBuffer(t._roots[e]);const n=Kn(0,t,i,s);return Fs.clearBuffer(),n}function Kn(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=Fs;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),qn.set(i.boundingBox.min,i.boundingBox.max,s),n=qn);if(!zs(l,o)){const o=t+8,l=a[t+6];as(o,r,$n);if(n.intersectsBox($n)&&Kn(o,e,i,s,n))return!0;as(l,r,$n);return!!(n.intersectsBox($n)&&Kn(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=Ms(t,a),f=Bs(l,o);if(Xn.copy(s).invert(),i.boundsTree){as(t,r,Gn),Gn.matrix.copy(Xn),Gn.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>Gn.intersectsBox(t),intersectsTriangle:t=>{t.a.applyMatrix4(s),t.b.applyMatrix4(s),t.c.applyMatrix4(s),t.needsUpdate=!0;for(let i=p,s=f+p;i<s;i++)if(ln(Wn,3*e.resolveTriangleIndex(i),h,c),Wn.needsUpdate=!0,t.intersectsTriangle(Wn))return!0;return!1}})}for(let t=p,i=f+p;t<i;t++){const i=e.resolveTriangleIndex(t);ln(Yn,3*i,h,c),Yn.a.applyMatrix4(Xn),Yn.b.applyMatrix4(Xn),Yn.c.applyMatrix4(Xn),Yn.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(ln(Wn,t,d,u),Wn.needsUpdate=!0,Yn.intersectsTriangle(Wn))return!0}}}const Jn=new p,tr=new Ps,er=new Ps,ir=new a,sr=new a,nr=new a,rr=new a;function or(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),tr.set(e.boundingBox.min,e.boundingBox.max,i),tr.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=Ds.getPrimitive(),p=Ds.getPrimitive();let f=ir,m=sr,g=null,_=null;n&&(g=nr,_=rr);let y=1/0,w=null,v=null;return Jn.copy(i).invert(),er.matrix.copy(Jn),t.shapecast({boundsTraverseOrder:t=>tr.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(er.min.copy(t.min),er.max.copy(t.max),er.needsUpdate=!0),!0),intersectsRange:(s,n)=>{if(e.boundsTree){const a=e.boundsTree;return a.shapecast({boundsTraverseOrder:t=>er.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o,intersectsRange:(e,o)=>{for(let b=e,x=e+o;b<x;b++){const e=a.resolveTriangleIndex(b);ln(p,3*e,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=s,i=s+n;e<i;e++){const i=t.resolveTriangleIndex(e);ln(u,3*i,h,l),u.needsUpdate=!0;const s=u.distanceToTriangle(p,f,g);if(s<y&&(m.copy(f),_&&_.copy(g),y=s,w=e,v=b),s<r)return!0}}}})}for(let o=0,a=ss(e);o<a;o++){ln(p,3*o,d,c),p.a.applyMatrix4(i),p.b.applyMatrix4(i),p.c.applyMatrix4(i),p.needsUpdate=!0;for(let e=s,i=s+n;e<i;e++){const i=t.resolveTriangleIndex(e);ln(u,3*i,h,l),u.needsUpdate=!0;const s=u.distanceToTriangle(p,f,g);if(s<y&&(m.copy(f),_&&_.copy(g),y=s,w=e,v=o),s<r)return!0}}}}),Ds.releasePrimitive(u),Ds.releasePrimitive(p),y===1/0?null:(s.point?s.point.copy(m):s.point=m.clone(),s.distance=y,s.faceIndex=w,n&&(n.point?n.point.copy(_):n.point=_.clone(),n.point.applyMatrix4(Jn),m.applyMatrix4(Jn),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}const ar=new Fs.constructor,lr=new Fs.constructor,hr=new Is(()=>new f),cr=new f,dr=new f,ur=new f,pr=new f;let fr=!1;function mr(t,e,i,s,n,r=0,o=0,a=0,l=0,h=null,c=!1){let d,u;c?(d=lr,u=ar):(d=ar,u=lr);const p=d.float32Array,f=d.uint32Array,m=d.uint16Array,g=u.float32Array,_=u.uint32Array,y=u.uint16Array,w=2*e,v=zs(2*t,m),b=zs(w,y);let x=!1;if(b&&v)x=c?n(Ms(e,_),Bs(2*e,y),Ms(t,f),Bs(2*t,m),l,o+e,a,r+t):n(Ms(t,f),Bs(2*t,m),Ms(e,_),Bs(2*e,y),a,r+t,l,o+e);else if(b){const h=hr.getPrimitive();as(e,g,h),h.applyMatrix4(i);const d=Ns(t),u=Us(t,f);as(d,p,cr),as(u,p,dr);const m=h.intersectsBox(cr),_=h.intersectsBox(dr);x=m&&mr(e,d,s,i,n,o,r,l,a+1,h,!c)||_&&mr(e,u,s,i,n,o,r,l,a+1,h,!c),hr.releasePrimitive(h)}else{const d=Ns(e),u=Us(e,_);as(d,g,ur),as(u,g,pr);const m=h.intersectsBox(ur),y=h.intersectsBox(pr);if(m&&y)x=mr(t,d,i,s,n,r,o,a,l+1,h,c)||mr(t,u,i,s,n,r,o,a,l+1,h,c);else if(m)if(v)x=mr(t,d,i,s,n,r,o,a,l+1,h,c);else{const e=hr.getPrimitive();e.copy(ur).applyMatrix4(i);const h=Ns(t),u=Us(t,f);as(h,p,cr),as(u,p,dr);const m=e.intersectsBox(cr),g=e.intersectsBox(dr);x=m&&mr(d,h,s,i,n,o,r,l,a+1,e,!c)||g&&mr(d,u,s,i,n,o,r,l,a+1,e,!c),hr.releasePrimitive(e)}else if(y)if(v)x=mr(t,u,i,s,n,r,o,a,l+1,h,c);else{const e=hr.getPrimitive();e.copy(pr).applyMatrix4(i);const h=Ns(t),d=Us(t,f);as(h,p,cr),as(d,p,dr);const m=e.intersectsBox(cr),g=e.intersectsBox(dr);x=m&&mr(u,h,s,i,n,o,r,l,a+1,e,!c)||g&&mr(u,d,s,i,n,o,r,l,a+1,e,!c),hr.releasePrimitive(e)}}return x}const gr=new Ps,_r=new f;class yr{static serialize(t,e={}){e={cloneBuffers:!0,...e};const i=t.geometry,s=t._roots,n=t._indirectBuffer,r=i.getIndex();let o;return o=e.cloneBuffers?{roots:s.map(t=>t.slice()),index:r.array.slice(),indirectBuffer:n?n.slice():null}:{roots:s,index:r.array,indirectBuffer:n},o}static deserialize(t,e,i={}){i={setIndex:!0,indirect:Boolean(t.indirectBuffer),...i};const{index:s,roots:n,indirectBuffer:r}=t,a=new yr(e,{...i,[is]:!0});if(a._roots=n,a._indirectBuffer=r||null,i.setIndex){const i=e.getIndex();if(null===i){const i=new o(t.index,1,!1);e.setIndex(i)}else i.array!==s&&(i.array.set(s),i.needsUpdate=!0)}return a}get indirect(){return!!this._indirectBuffer}constructor(t,e={}){if(!t.isBufferGeometry)throw new Error("MeshBVH: Only BufferGeometries are supported.");if(t.index&&t.index.isInterleavedBufferAttribute)throw new Error("MeshBVH: InterleavedBufferAttribute is not supported for the index attribute.");if((e=Object.assign({strategy:0,maxDepth:40,maxLeafTris:10,verbose:!0,useSharedArrayBuffer:!1,setBoundingBox:!0,onProgress:null,indirect:!1,[is]:!1},e)).useSharedArrayBuffer&&"undefined"==typeof SharedArrayBuffer)throw new Error("MeshBVH: SharedArrayBuffer is not available.");this.geometry=t,this._roots=null,this._indirectBuffer=null,e[is]||(xs(this,e),!t.boundingBox&&e.setBoundingBox&&(t.boundingBox=this.getBoundingBox(new f)));const{_indirectBuffer:i}=this;this.resolveTriangleIndex=e.indirect?t=>i[t]:t=>t}refit(t=null){return(this.indirect?Un:cn)(this,t)}traverse(t,e=0){const i=this._roots[e],s=new Uint32Array(i),n=new Uint16Array(i);!function e(r,o=0){const a=2*r,l=n[a+15]===ts;if(l){const e=s[r+6],h=n[a+14];t(o,l,new Float32Array(i,4*r,6),e,h)}else{const n=r+8,a=s[r+6],h=s[r+7];t(o,l,new Float32Array(i,4*r,6),h)||(e(n,o+1),e(a,o+1))}}(0)}raycast(t,e=_){const i=this._roots,s=this.geometry,n=[],r=e.isMaterial,o=Array.isArray(e),a=s.groups,l=r?e.side:e,h=this.indirect?Rn:mn;for(let s=0,r=i.length;s<r;s++){const i=o?e[a[s].materialIndex].side:l,r=n.length;if(h(this,s,i,t,n),o){const t=a[s].materialIndex;for(let e=r,i=n.length;e<i;e++)n[e].face.materialIndex=t}}return n}raycastFirst(t,e=_){const i=this._roots,s=this.geometry,n=e.isMaterial,r=Array.isArray(e);let o=null;const a=s.groups,l=n?e.side:e,h=this.indirect?Zn:wn;for(let s=0,n=i.length;s<n;s++){const i=h(this,s,r?e[a[s].materialIndex].side:l,t);null!=i&&(null==o||i.distance<o.distance)&&(o=i,r&&(i.face.materialIndex=a[s].materialIndex))}return o}intersectsGeometry(t,e){let i=!1;const s=this._roots,n=this.indirect?Qn:An;for(let r=0,o=s.length;r<o&&(i=n(this,r,t,e),!i);r++);return i}shapecast(t){const e=Ds.getPrimitive(),i=this.indirect?pn:hn;let{boundsTraverseOrder:s,intersectsBounds:n,intersectsRange:r,intersectsTriangle:o}=t;if(r&&o){const t=r;r=(s,n,r,a,l)=>!!t(s,n,r,a,l)||i(s,n,this,o,r,a,e)}else r||(r=o?(t,s,n,r)=>i(t,s,this,o,n,r,e):(t,e,i)=>i);let a=!1,l=0;const h=this._roots;for(let t=0,e=h.length;t<e;t++){const e=h[t];if(a=$s(this,t,n,r,s,l),a)break;l+=e.byteLength}return Ds.releasePrimitive(e),a}bvhcast(t,e,i){let{intersectsRanges:s,intersectsTriangles:n}=i;const r=Ds.getPrimitive(),o=this.geometry.index,a=this.geometry.attributes.position,l=this.indirect?t=>{const e=this.resolveTriangleIndex(t);ln(r,3*e,o,a)}:t=>{ln(r,3*t,o,a)},h=Ds.getPrimitive(),c=t.geometry.index,d=t.geometry.attributes.position,u=t.indirect?e=>{const i=t.resolveTriangleIndex(e);ln(h,3*i,c,d)}:t=>{ln(h,3*t,c,d)};if(n){const t=(t,i,s,o,a,c,d,p)=>{for(let f=s,m=s+o;f<m;f++){u(f),h.a.applyMatrix4(e),h.b.applyMatrix4(e),h.c.applyMatrix4(e),h.needsUpdate=!0;for(let e=t,s=t+i;e<s;e++)if(l(e),r.needsUpdate=!0,n(r,h,e,f,a,c,d,p))return!0}return!1};if(s){const e=s;s=function(i,s,n,r,o,a,l,h){return!!e(i,s,n,r,o,a,l,h)||t(i,s,n,r,o,a,l,h)}}else s=t}return function(t,e,i,s){if(fr)throw new Error("MeshBVH: Recursive calls to bvhcast not supported.");fr=!0;const n=t._roots,r=e._roots;let o,a=0,l=0;const h=(new p).copy(i).invert();for(let t=0,e=n.length;t<e;t++){ar.setBuffer(n[t]),l=0;const e=hr.getPrimitive();as(Rs(0),ar.float32Array,e),e.applyMatrix4(h);for(let n=0,c=r.length;n<c&&(lr.setBuffer(r[t]),o=mr(0,0,i,h,s,a,l,0,0,e),lr.clearBuffer(),l+=r[n].length,!o);n++);if(hr.releasePrimitive(e),ar.clearBuffer(),a+=n[t].length,o)break}return fr=!1,o}(this,t,e,s)}intersectsBox(t,e){return gr.set(t.min,t.max,e),gr.needsUpdate=!0,this.shapecast({intersectsBounds:t=>gr.intersectsBox(t),intersectsTriangle:t=>gr.intersectsTriangle(t)})}intersectsSphere(t){return this.shapecast({intersectsBounds:e=>t.intersectsBox(e),intersectsTriangle:e=>e.intersectsSphere(t)})}closestPointToGeometry(t,e,i={},s={},n=0,r=1/0){return(this.indirect?or:Nn)(this,t,e,i,s,n,r)}closestPointToPoint(t,e={},i=0,s=1/0){return function(t,e,i={},s=0,n=1/0){const r=s*s,o=n*n;let a=1/0,l=null;if(t.shapecast({boundsTraverseOrder:t=>(Ws.copy(e).clamp(t.min,t.max),Ws.distanceToSquared(e)),intersectsBounds:(t,e,i)=>i<a&&i<o,intersectsTriangle:(t,i)=>{t.closestPointToPoint(e,Ws);const s=e.distanceToSquared(Ws);return s<a&&(Xs.copy(Ws),a=s,l=i),s<r}}),a===1/0)return null;const h=Math.sqrt(a);return i.point?i.point.copy(Xs):i.point=Xs.clone(),i.distance=h,i.faceIndex=l,i}(this,t,e,i,s)}getBoundingBox(t){t.makeEmpty();return this._roots.forEach(e=>{as(0,new Float32Array(e),_r),t.union(_r)}),t}}function wr(t,e,i){return null===t?null:(t.point.applyMatrix4(e.matrixWorld),t.distance=t.point.distanceTo(i.ray.origin),t.object=e,t.distance<i.near||t.distance>i.far?null:t)}const vr=new y,br=new p,xr=w.prototype.raycast;function Er(t,e){if(this.geometry.boundsTree){if(void 0===this.material)return;br.copy(this.matrixWorld).invert(),vr.copy(t.ray).applyMatrix4(br);const i=this.geometry.boundsTree;if(!0===t.firstHitOnly){const s=wr(i.raycastFirst(vr,this.material),this,t);s&&e.push(s)}else{const s=i.raycast(vr,this.material);for(let i=0,n=s.length;i<n;i++){const n=wr(s[i],this,t);n&&e.push(n)}}}else xr.call(this,t,e)}function Tr(t){return this.boundsTree=new yr(this,t),this.boundsTree}function Cr(){this.boundsTree=null}const Sr=class t{constructor(){e(this,"onDisposed",new U),e(this,"list",new s),e(this,"enabled",!1),e(this,"_clock"),e(this,"onInit",new U),e(this,"update",()=>{if(!this.enabled)return;const t=this._clock.getDelta();for(const[e,i]of this.list)i.enabled&&i.isUpdateable()&&i.update(t);requestAnimationFrame(this.update)}),this._clock=new r.Clock,t.setupBVH()}add(t,e){if(this.list.has(t))throw new Error("You're trying to add a component that already exists in the components instance. Use Components.get() instead.");q.validate(t),this.list.set(t,e)}get(t){const e=t.uuid;if(!this.list.has(e)){const i=new t(this);return i.isDisposeable()&&i.onDisposed.add(()=>this.list.delete(e)),this.list.has(e)||this.add(e,i),i}return this.list.get(e)}init(){this.enabled=!0;for(const[t,e]of this.list.entries())e.enabled=!0;this._clock.start(),this.update(),this.onInit.trigger()}dispose(){let t;this.enabled=!1;for(const[e,i]of this.list)i.enabled=!1,e!==Ae.uuid?i.isDisposeable()&&i.dispose():t=i;null==t||t.dispose(),this._clock.stop(),this.onDisposed.trigger()}static setupBVH(){r.BufferGeometry.prototype.computeBoundsTree=Tr,r.BufferGeometry.prototype.disposeBoundsTree=Cr,r.Mesh.prototype.raycast=Er}};e(Sr,"release","2.4.3");let Ar=Sr;class Or{constructor(t){e(this,"enabled",!1),e(this,"id","FirstPerson"),this.camera=t}set(t){if(this.enabled=t,t){if("Perspective"!==this.camera.projection.current)return void this.camera.set("Orbit");this.setupFirstPersonCamera()}}setupFirstPersonCamera(){const t=this.camera.controls,e=new r.Vector3;t.distance--,t.getPosition(e),t.minDistance=1,t.maxDistance=1,t.distance=1,t.moveTo(e.x,e.y,e.z),t.truckSpeed=50,t.mouseButtons.wheel=$i.ACTION.DOLLY,t.touches.two=$i.ACTION.TOUCH_ZOOM_TRUCK}}class Pr{constructor(t){e(this,"enabled",!0),e(this,"id","Orbit"),this.camera=t,this.activateOrbitControls()}set(t){this.enabled=t,t&&this.activateOrbitControls()}activateOrbitControls(){const t=this.camera.controls;t.minDistance=1,t.maxDistance=300;const e=new r.Vector3;t.getPosition(e);const i=e.length();t.distance=i,t.truckSpeed=2;const{rotation:s}=this.camera.three,n=new r.Vector3(0,0,-1).applyEuler(s),o=e.addScaledVector(n,i);t.moveTo(o.x,o.y,o.z)}}class Ir{constructor(t){e(this,"enabled",!1),e(this,"id","Plan"),e(this,"mouseAction1"),e(this,"mouseAction2"),e(this,"mouseInitialized",!1),e(this,"defaultAzimuthSpeed"),e(this,"defaultPolarSpeed"),this.camera=t,this.defaultAzimuthSpeed=t.controls.azimuthRotateSpeed,this.defaultPolarSpeed=t.controls.polarRotateSpeed}set(t){this.enabled=t;const e=this.camera.controls;e.azimuthRotateSpeed=t?0:this.defaultAzimuthSpeed,e.polarRotateSpeed=t?0:this.defaultPolarSpeed,this.mouseInitialized||(this.mouseAction1=e.touches.one,this.mouseAction2=e.touches.two,this.mouseInitialized=!0),t?(e.mouseButtons.left=$i.ACTION.TRUCK,e.touches.one=$i.ACTION.TOUCH_TRUCK,e.touches.two=$i.ACTION.TOUCH_ZOOM):(e.mouseButtons.left=$i.ACTION.ROTATE,e.touches.one=this.mouseAction1,e.touches.two=this.mouseAction2)}}class kr{constructor(t){e(this,"onChanged",new U),e(this,"current","Perspective"),e(this,"camera"),e(this,"matchOrthoDistanceEnabled",!1),e(this,"_component"),e(this,"_previousDistance",-1),this._component=t,this.camera=t.three}async set(t){this.current!==t&&("Orthographic"===t?this.setOrthoCamera():await this.setPerspectiveCamera(),this.onChanged.trigger(this.camera))}async toggle(){const t="Perspective"===this.current?"Orthographic":"Perspective";await this.set(t)}setOrthoCamera(){if(null===this._component.mode)return;if("FirstPerson"===this._component.mode.id)return;this._previousDistance=this._component.controls.distance,this._component.controls.distance=200;const t=this.getPerspectiveDims();if(!t)return;const{width:e,height:i}=t;this.setupOrthoCamera(i,e),this.camera=this._component.threeOrtho,this.current="Orthographic"}getPerspectiveDims(){const t=this._component.currentWorld;if(!t||!t.renderer)return null;const e=new r.Vector3;this._component.threePersp.getWorldDirection(e);const i=new r.Vector3;this._component.controls.getTarget(i);const s=i.clone().sub(this._component.threePersp.position).dot(e),n=t.renderer.getSize(),o=n.x/n.y,a=this._component.threePersp,l=2*s*Math.atan(a.fov*(Math.PI/180)/2);return{width:l*o,height:l}}setupOrthoCamera(t,e){this._component.controls.mouseButtons.wheel=$i.ACTION.ZOOM,this._component.controls.mouseButtons.middle=$i.ACTION.ZOOM;const i=this._component.threePersp,s=this._component.threeOrtho;s.zoom=1,s.left=e/-2,s.right=e/2,s.top=t/2,s.bottom=t/-2,s.updateProjectionMatrix(),s.position.copy(i.position),s.quaternion.copy(i.quaternion),this._component.controls.camera=s}getDistance(){const t=this._component.threePersp,e=this._component.threeOrtho;return(e.top-e.bottom)/e.zoom/(2*Math.atan(t.fov*(Math.PI/180)/2))}async setPerspectiveCamera(){this._component.controls.mouseButtons.wheel=$i.ACTION.DOLLY,this._component.controls.mouseButtons.middle=$i.ACTION.DOLLY;const t=this._component.threePersp,e=this._component.threeOrtho;t.position.copy(e.position),t.quaternion.copy(e.quaternion),this._component.controls.mouseButtons.wheel=$i.ACTION.DOLLY,this.matchOrthoDistanceEnabled?this._component.controls.distance=this.getDistance():this._component.controls.distance=this._previousDistance,await this._component.controls.zoomTo(1),t.updateProjectionMatrix(),this._component.controls.camera=t,this.camera=t,this.current="Perspective"}}class Dr extends Yi{constructor(t){super(t),e(this,"projection"),e(this,"threeOrtho"),e(this,"threePersp"),e(this,"_userInputButtons",{}),e(this,"_frustumSize",50),e(this,"_navigationModes",new Map),e(this,"_mode",null),e(this,"previousSize",null),this.threePersp=this.three,this.threeOrtho=this.newOrthoCamera(),this.projection=new kr(this),this.onAspectUpdated.add(()=>{this.setOrthoPerspCameraAspect()}),this.projection.onChanged.add(t=>{this.three=t,this.updateAspect()}),this.worlds.onItemSet.add(()=>{this._navigationModes.clear(),this._navigationModes.set("Orbit",new Pr(this)),this._navigationModes.set("FirstPerson",new Or(this)),this._navigationModes.set("Plan",new Ir(this)),this._mode=this._navigationModes.get("Orbit"),this.mode.set(!0,{preventTargetAdjustment:!0}),this.currentWorld&&this.currentWorld.renderer&&(this.previousSize=this.currentWorld.renderer.getSize().clone())}),this.worlds.onItemDeleted.add(()=>{this._navigationModes.clear()})}get mode(){if(!this._mode)throw new Error("Mode not found, camera not initialized");return this._mode}dispose(){super.dispose(),this.threeOrtho.removeFromParent()}set(t){if(null!==this.mode&&this.mode.id!==t){if(this.mode.set(!1),!this._navigationModes.has(t))throw new Error("The specified mode does not exist!");this._mode=this._navigationModes.get(t),this.mode.set(!0)}}async fit(t,e=1.5){if(!this.enabled)return;const i=Number.MAX_VALUE,s=Number.MIN_VALUE,n=new r.Vector3(i,i,i),o=new r.Vector3(s,s,s);for(const e of t){const t=(new r.Box3).setFromObject(e);t.min.x<n.x&&(n.x=t.min.x),t.min.y<n.y&&(n.y=t.min.y),t.min.z<n.z&&(n.z=t.min.z),t.max.x>o.x&&(o.x=t.max.x),t.max.y>o.y&&(o.y=t.max.y),t.max.z>o.z&&(o.z=t.max.z)}const a=new r.Box3(n,o),l=this.components.get(Ae);if(l.initialized)for(const[,t]of l.list){const e=t.box;e.min.x<n.x&&(n.x=e.min.x),e.min.y<n.y&&(n.y=e.min.y),e.min.z<n.z&&(n.z=e.min.z),e.max.x>o.x&&(o.x=e.max.x),e.max.y>o.y&&(o.y=e.max.y),e.max.z>o.z&&(o.z=e.max.z)}const h=new r.Vector3;a.getSize(h);const c=new r.Vector3;a.getCenter(c);const d=Math.max(h.x,h.y,h.z)*e,u=new r.Sphere(c,d);await this.controls.fitToSphere(u,!0)}setUserInput(t){t?this.enableUserInput():this.disableUserInput()}addCustomNavigationMode(t){this._navigationModes.set(t.id,t)}disableUserInput(){this._userInputButtons.left=this.controls.mouseButtons.left,this._userInputButtons.right=this.controls.mouseButtons.right,this._userInputButtons.middle=this.controls.mouseButtons.middle,this._userInputButtons.wheel=this.controls.mouseButtons.wheel,this.controls.mouseButtons.left=0,this.controls.mouseButtons.right=0,this.controls.mouseButtons.middle=0,this.controls.mouseButtons.wheel=0}enableUserInput(){0!==Object.keys(this._userInputButtons).length&&(this.controls.mouseButtons.left=this._userInputButtons.left,this.controls.mouseButtons.right=this._userInputButtons.right,this.controls.mouseButtons.middle=this._userInputButtons.middle,this.controls.mouseButtons.wheel=this._userInputButtons.wheel)}newOrthoCamera(){const t=window.innerWidth/window.innerHeight;return new r.OrthographicCamera(this._frustumSize*t/-2,this._frustumSize*t/2,this._frustumSize/2,this._frustumSize/-2,.1,1e3)}setOrthoPerspCameraAspect(){if(!this.currentWorld||!this.currentWorld.renderer)return;if(!this.previousSize)return;const t=this.currentWorld.renderer.getSize(),e=this.threeOrtho.top,i=this.threeOrtho.right,s=e*(t.y/this.previousSize.y),n=i*(t.x/this.previousSize.x);this.threeOrtho.left=-n,this.threeOrtho.right=n,this.threeOrtho.top=s,this.threeOrtho.bottom=-s,this.threeOrtho.updateProjectionMatrix(),this.previousSize.copy(t)}}function zr(t){throw new Error('Could not dynamically require "'+t+'". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.')}var Mr={exports:{}};
8
8
  /*!
9
9
 
10
10
  JSZip v3.10.1 - A JavaScript class for generating and reading zip files
package/dist/index.mjs CHANGED
@@ -11969,6 +11969,14 @@ class OrthoPerspectiveCamera extends SimpleCamera {
11969
11969
  this.disableUserInput();
11970
11970
  }
11971
11971
  }
11972
+ /**
11973
+ * Adds a custom {@link NavigationMode} to the camera that can be used using the {@link OrthoPerspectiveCamera.set} method.
11974
+ *
11975
+ * @param mode - The custom {@link NavigationMode} to add.
11976
+ */
11977
+ addCustomNavigationMode(mode) {
11978
+ this._navigationModes.set(mode.id, mode);
11979
+ }
11972
11980
  disableUserInput() {
11973
11981
  this._userInputButtons.left = this.controls.mouseButtons.left;
11974
11982
  this._userInputButtons.right = this.controls.mouseButtons.right;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@thatopen/components",
3
3
  "description": "Collection of core functionalities to author BIM apps.",
4
- "version": "3.2.6",
4
+ "version": "3.2.7",
5
5
  "author": "That Open Company",
6
6
  "contributors": [
7
7
  "Antonio Gonzalez Viegas (https://github.com/agviegas)",