@thatopen/components 3.1.0 → 3.1.2

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.
@@ -5,7 +5,7 @@
5
5
  * (c) 2017 @yomotsu
6
6
  * Released under the MIT License.
7
7
  */
8
- const _e=1,ye=2,we=4,ve=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}),be=0,xe=1,Ee=-1;function Te(t){return t.isPerspectiveCamera}function Ce(t){return t.isOrthographicCamera}const Se=2*Math.PI,Oe=Math.PI/2,Ae=Math.PI/180;function Pe(t,e,i){return Math.max(e,Math.min(i,t))}function Ie(t,e=1e-5){return Math.abs(t)<e}function De(t,e,i=1e-5){return Ie(t-e,i)}function ke(t,e){return Math.round(t/e)*e}function Me(t){return isFinite(t)?t:t<0?-Number.MAX_VALUE:Number.MAX_VALUE}function ze(t){return Math.abs(t)<Number.MAX_VALUE?t:t*(1/0)}function Be(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=Pe(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 Ne(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 Ue(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 Le(t,e){return!!Ce(t)&&(console.warn(`${e} is not supported in OrthographicCamera`),!0)}class Re{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 Ve;const Fe=1/8,je=/Mac/.test(null===(Ve=null===globalThis||void 0===globalThis?void 0:globalThis.navigator)||void 0===Ve?void 0:Ve.platform);let He,Ze,$e,We,Ye,Ge,qe,Xe,Qe,Ke,Je,ti,ei,ii,si,ni,ri,oi,ai,li,hi,ci,di;class ui extends Re{static install(t){He=t.THREE,Ze=Object.freeze(new He.Vector3(0,0,0)),$e=Object.freeze(new He.Vector3(0,1,0)),We=Object.freeze(new He.Vector3(0,0,1)),Ye=new He.Vector2,Ge=new He.Vector3,qe=new He.Vector3,Xe=new He.Vector3,Qe=new He.Vector3,Ke=new He.Vector3,Je=new He.Vector3,ti=new He.Vector3,ei=new He.Vector3,ii=new He.Vector3,si=new He.Spherical,ni=new He.Spherical,ri=new He.Box3,oi=new He.Box3,ai=new He.Sphere,li=new He.Quaternion,hi=new He.Quaternion,ci=new He.Matrix4,di=new He.Raycaster}static get ACTION(){return ve}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=ve.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=be,this._thetaVelocity={value:0},this._phiVelocity={value:0},this._radiusVelocity={value:0},this._targetVelocity=new He.Vector3,this._focalOffsetVelocity=new He.Vector3,this._zoomVelocity={value:0},this._truckInternal=(t,e,i,s)=>{let n,r;if(Te(this._camera)){const i=Ge.copy(this._camera.position).sub(this._target),s=this._camera.getEffectiveFOV()*Ae,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(!Ce(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=Se*this.azimuthRotateSpeed*t/this._elementRect.height,s=Se*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=Pe(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===He&&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 He.Quaternion).setFromUnitVectors(this._camera.up,$e),this._yAxisUpSpaceInverse=this._yAxisUpSpace.clone().invert(),this._state=ve.NONE,this._target=new He.Vector3,this._targetEnd=this._target.clone(),this._focalOffset=new He.Vector3,this._focalOffsetEnd=this._focalOffset.clone(),this._spherical=(new He.Spherical).setFromVector3(Ge.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 He.Vector3,new He.Vector3,new He.Vector3,new He.Vector3],this._updateNearPlaneCorners(),this._boundary=new He.Box3(new He.Vector3(-1/0,-1/0,-1/0),new He.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 He.Vector2,this.mouseButtons={left:ve.ROTATE,middle:ve.DOLLY,right:ve.TRUCK,wheel:Te(this._camera)?ve.DOLLY:Ce(this._camera)?ve.ZOOM:ve.NONE},this.touches={one:ve.TOUCH_ROTATE,two:Te(this._camera)?ve.TOUCH_DOLLY_TRUCK:Ce(this._camera)?ve.TOUCH_ZOOM_TRUCK:ve.NONE,three:ve.TOUCH_TRUCK};const i=new He.Vector2,s=new He.Vector2,n=new He.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&_e)===_e?_e:(t.buttons&we)===we?we:(t.buttons&ye)===ye?ye:null;if(null!==e){const t=this._findPointerByMouseButton(e);t&&this._disposePointer(t)}if((t.buttons&_e)===_e&&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&_e)===_e)&&(this._state=this._state|this.mouseButtons.left),this._isDragging&&(t.buttons&we)===we&&(this._state=this._state|this.mouseButtons.middle),this._isDragging&&(t.buttons&ye)===ye&&(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=ve.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=ve.NONE;p()}};let l=-1;const h=t=>{if(!this._domElement)return;if(!this._enabled||this.mouseButtons.wheel===ve.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===ve.ROTATE||this.mouseButtons.wheel===ve.TRUCK){const t=performance.now();l-t<1e3&&this._getClientRect(this._elementRect),l=t}const e=je?-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 ve.ROTATE:this._rotateInternal(t.deltaX,t.deltaY),this._isUserControllingRotate=!0;break;case ve.TRUCK:this._truckInternal(t.deltaX,t.deltaY,!1,!1),this._isUserControllingTruck=!0;break;case ve.SCREEN_PAN:this._truckInternal(t.deltaX,t.deltaY,!1,!0),this._isUserControllingTruck=!0;break;case ve.OFFSET:this._truckInternal(t.deltaX,t.deltaY,!0,!1),this._isUserControllingOffset=!0;break;case ve.DOLLY:this._dollyInternal(-i,s,n),this._isUserControllingDolly=!0;break;case ve.ZOOM:this._zoomInternal(-i,s,n),this._isUserControllingZoom=!0}this.dispatchEvent({type:"control"})},c=t=>{if(this._domElement&&this._enabled){if(this.mouseButtons.right===ui.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;Ue(this._activePointers,Ye),this._getClientRect(this._elementRect),i.copy(Ye),s.copy(Ye);if(this._activePointers.length>=2){const t=Ye.x-this._activePointers[1].clientX,e=Ye.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&_e)!==_e||(this._state=this._state|this.mouseButtons.left),(t.buttons&we)===we&&(this._state=this._state|this.mouseButtons.middle),(t.buttons&ye)===ye&&(this._state=this._state|this.mouseButtons.right);else this._lockedPointer&&(this._state=this._state|this.mouseButtons.left);(this._state&ve.ROTATE)!==ve.ROTATE&&(this._state&ve.TOUCH_ROTATE)!==ve.TOUCH_ROTATE&&(this._state&ve.TOUCH_DOLLY_ROTATE)!==ve.TOUCH_DOLLY_ROTATE&&(this._state&ve.TOUCH_ZOOM_ROTATE)!==ve.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&ve.TRUCK)!==ve.TRUCK&&(this._state&ve.SCREEN_PAN)!==ve.SCREEN_PAN&&(this._state&ve.TOUCH_TRUCK)!==ve.TOUCH_TRUCK&&(this._state&ve.TOUCH_SCREEN_PAN)!==ve.TOUCH_SCREEN_PAN&&(this._state&ve.TOUCH_DOLLY_TRUCK)!==ve.TOUCH_DOLLY_TRUCK&&(this._state&ve.TOUCH_DOLLY_SCREEN_PAN)!==ve.TOUCH_DOLLY_SCREEN_PAN&&(this._state&ve.TOUCH_ZOOM_TRUCK)!==ve.TOUCH_ZOOM_TRUCK&&(this._state&ve.TOUCH_ZOOM_SCREEN_PAN)!==ve.TOUCH_DOLLY_SCREEN_PAN||(this._targetEnd.copy(this._target),this._targetVelocity.set(0,0,0)),(this._state&ve.DOLLY)!==ve.DOLLY&&(this._state&ve.TOUCH_DOLLY)!==ve.TOUCH_DOLLY&&(this._state&ve.TOUCH_DOLLY_TRUCK)!==ve.TOUCH_DOLLY_TRUCK&&(this._state&ve.TOUCH_DOLLY_SCREEN_PAN)!==ve.TOUCH_DOLLY_SCREEN_PAN&&(this._state&ve.TOUCH_DOLLY_OFFSET)!==ve.TOUCH_DOLLY_OFFSET&&(this._state&ve.TOUCH_DOLLY_ROTATE)!==ve.TOUCH_DOLLY_ROTATE||(this._sphericalEnd.radius=this._spherical.radius,this._radiusVelocity.value=0),(this._state&ve.ZOOM)!==ve.ZOOM&&(this._state&ve.TOUCH_ZOOM)!==ve.TOUCH_ZOOM&&(this._state&ve.TOUCH_ZOOM_TRUCK)!==ve.TOUCH_ZOOM_TRUCK&&(this._state&ve.TOUCH_ZOOM_SCREEN_PAN)!==ve.TOUCH_ZOOM_SCREEN_PAN&&(this._state&ve.TOUCH_ZOOM_OFFSET)!==ve.TOUCH_ZOOM_OFFSET&&(this._state&ve.TOUCH_ZOOM_ROTATE)!==ve.TOUCH_ZOOM_ROTATE||(this._zoomEnd=this._zoom,this._zoomVelocity.value=0),(this._state&ve.OFFSET)!==ve.OFFSET&&(this._state&ve.TOUCH_OFFSET)!==ve.TOUCH_OFFSET&&(this._state&ve.TOUCH_DOLLY_OFFSET)!==ve.TOUCH_DOLLY_OFFSET&&(this._state&ve.TOUCH_ZOOM_OFFSET)!==ve.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,Ue(this._activePointers,Ye);const t=this._domElement&&this._domElement.ownerDocument.pointerLockElement===this._domElement?this._lockedPointer||this._activePointers[0]:null,e=t?-t.deltaX:s.x-Ye.x,r=t?-t.deltaY:s.y-Ye.y;if(s.copy(Ye),(this._state&ve.ROTATE)!==ve.ROTATE&&(this._state&ve.TOUCH_ROTATE)!==ve.TOUCH_ROTATE&&(this._state&ve.TOUCH_DOLLY_ROTATE)!==ve.TOUCH_DOLLY_ROTATE&&(this._state&ve.TOUCH_ZOOM_ROTATE)!==ve.TOUCH_ZOOM_ROTATE||(this._rotateInternal(e,r),this._isUserControllingRotate=!0),(this._state&ve.DOLLY)===ve.DOLLY||(this._state&ve.ZOOM)===ve.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&ve.DOLLY)===ve.DOLLY?(this._dollyInternal(s*r*Fe,t,e),this._isUserControllingDolly=!0):(this._zoomInternal(s*r*Fe,t,e),this._isUserControllingZoom=!0)}if((this._state&ve.TOUCH_DOLLY)===ve.TOUCH_DOLLY||(this._state&ve.TOUCH_ZOOM)===ve.TOUCH_ZOOM||(this._state&ve.TOUCH_DOLLY_TRUCK)===ve.TOUCH_DOLLY_TRUCK||(this._state&ve.TOUCH_ZOOM_TRUCK)===ve.TOUCH_ZOOM_TRUCK||(this._state&ve.TOUCH_DOLLY_SCREEN_PAN)===ve.TOUCH_DOLLY_SCREEN_PAN||(this._state&ve.TOUCH_ZOOM_SCREEN_PAN)===ve.TOUCH_ZOOM_SCREEN_PAN||(this._state&ve.TOUCH_DOLLY_OFFSET)===ve.TOUCH_DOLLY_OFFSET||(this._state&ve.TOUCH_ZOOM_OFFSET)===ve.TOUCH_ZOOM_OFFSET||(this._state&ve.TOUCH_DOLLY_ROTATE)===ve.TOUCH_DOLLY_ROTATE||(this._state&ve.TOUCH_ZOOM_ROTATE)===ve.TOUCH_ZOOM_ROTATE){const t=Ye.x-this._activePointers[1].clientX,e=Ye.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&ve.TOUCH_DOLLY)===ve.TOUCH_DOLLY||(this._state&ve.TOUCH_DOLLY_ROTATE)===ve.TOUCH_DOLLY_ROTATE||(this._state&ve.TOUCH_DOLLY_TRUCK)===ve.TOUCH_DOLLY_TRUCK||(this._state&ve.TOUCH_DOLLY_SCREEN_PAN)===ve.TOUCH_DOLLY_SCREEN_PAN||(this._state&ve.TOUCH_DOLLY_OFFSET)===ve.TOUCH_DOLLY_OFFSET?(this._dollyInternal(r*Fe,o,a),this._isUserControllingDolly=!0):(this._zoomInternal(r*Fe,o,a),this._isUserControllingZoom=!0)}(this._state&ve.TRUCK)!==ve.TRUCK&&(this._state&ve.TOUCH_TRUCK)!==ve.TOUCH_TRUCK&&(this._state&ve.TOUCH_DOLLY_TRUCK)!==ve.TOUCH_DOLLY_TRUCK&&(this._state&ve.TOUCH_ZOOM_TRUCK)!==ve.TOUCH_ZOOM_TRUCK||(this._truckInternal(e,r,!1,!1),this._isUserControllingTruck=!0),(this._state&ve.SCREEN_PAN)!==ve.SCREEN_PAN&&(this._state&ve.TOUCH_SCREEN_PAN)!==ve.TOUCH_SCREEN_PAN&&(this._state&ve.TOUCH_DOLLY_SCREEN_PAN)!==ve.TOUCH_DOLLY_SCREEN_PAN&&(this._state&ve.TOUCH_ZOOM_SCREEN_PAN)!==ve.TOUCH_ZOOM_SCREEN_PAN||(this._truckInternal(e,r,!1,!0),this._isUserControllingTruck=!0),(this._state&ve.OFFSET)!==ve.OFFSET&&(this._state&ve.TOUCH_OFFSET)!==ve.TOUCH_OFFSET&&(this._state&ve.TOUCH_DOLLY_OFFSET)!==ve.TOUCH_DOLLY_OFFSET&&(this._state&ve.TOUCH_ZOOM_OFFSET)!==ve.TOUCH_ZOOM_OFFSET||(this._truckInternal(e,r,!0,!1),this._isUserControllingOffset=!0),this.dispatchEvent({type:"control"})},p=()=>{Ue(this._activePointers,Ye),s.copy(Ye),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!==ve.NONE&&(this._state=ve.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=Pe(t.width,0,1),this._interactiveArea.height=Pe(t.height,0,1),this._interactiveArea.x=Pe(t.x,0,1-this._interactiveArea.width),this._interactiveArea.y=Pe(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=Pe(t,this.minAzimuthAngle,this.maxAzimuthAngle),n=Pe(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||De(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&De(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=be,this._changedDolly=0,this._dollyToNoClamp(Pe(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=De(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||De(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(s)}dollyInFixed(t,e=!1){this._targetEnd.add(this._getCameraDirection(Qe).multiplyScalar(t)),e||this._target.copy(this._targetEnd);const i=!e||De(this._target.x,this._targetEnd.x,this.restThreshold)&&De(this._target.y,this._targetEnd.y,this.restThreshold)&&De(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=Pe(t,this.minZoom,this.maxZoom),this._needsUpdate=!0,e||(this._zoom=this._zoomEnd);const i=!e||De(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(),Ke.setFromMatrixColumn(this._camera.matrix,0),Je.setFromMatrixColumn(this._camera.matrix,1),Ke.multiplyScalar(t),Je.multiplyScalar(-e);const s=Ge.copy(Ke).add(Je),n=qe.copy(this._targetEnd).add(s);return this.moveTo(n.x,n.y,n.z,i)}forward(t,e=!1){Ge.setFromMatrixColumn(this._camera.matrix,0),Ge.crossVectors(this._camera.up,Ge),Ge.multiplyScalar(t);const i=qe.copy(this._targetEnd).add(Ge);return this.moveTo(i.x,i.y,i.z,e)}elevate(t,e=!1){return Ge.copy(this._camera.up).multiplyScalar(t),this.moveTo(this._targetEnd.x+Ge.x,this._targetEnd.y+Ge.y,this._targetEnd.z+Ge.z,e)}moveTo(t,e,i,s=!1){this._isUserControllingTruck=!1;const n=Ge.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||De(this._target.x,this._targetEnd.x,this.restThreshold)&&De(this._target.y,this._targetEnd.y,this.restThreshold)&&De(this._target.z,this._targetEnd.z,this.restThreshold);return this._createOnRestPromise(r)}lookInDirectionOf(t,e,i,s=!1){const n=Ge.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?ri.copy(t):ri.setFromObject(t);l.isEmpty()&&(console.warn("camera-controls: fitTo() cannot be used with an empty box. Aborting"),Promise.resolve());const h=ke(this._sphericalEnd.theta,Oe),c=ke(this._sphericalEnd.phi,Oe);a.push(this.rotateTo(h,c,e));const d=Ge.setFromSpherical(this._sphericalEnd).normalize(),u=li.setFromUnitVectors(d,We),p=De(Math.abs(d.y),1);p&&u.multiply(hi.setFromAxisAngle($e,h)),u.multiply(this._yAxisUpSpaceInverse);const f=oi.makeEmpty();qe.copy(l.min).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.min).setX(l.max.x).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.min).setY(l.max.y).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.max).setZ(l.min.z).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.min).setZ(l.max.z).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.max).setY(l.min.y).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.max).setX(l.min.x).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.max).applyQuaternion(u),f.expandByPoint(qe),f.min.x-=s,f.min.y-=r,f.max.x+=n,f.max.y+=o,u.setFromUnitVectors(We,d),p&&u.premultiply(hi.invert()),u.premultiply(this._yAxisUpSpace);const m=f.getSize(Ge),g=f.getCenter(qe).applyQuaternion(u);if(Te(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(Ce(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?ui.createBoundingSphere(t,ai):ai.copy(t);if(i.push(this.moveTo(s.center.x,s.center.y,s.center.z,e)),Te(this._camera)){const t=this.getDistanceToFitSphere(s.radius);i.push(this.dollyTo(t,e))}else if(Ce(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=be,this._changedDolly=0;const a=qe.set(s,n,r),l=Ge.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||De(this._target.x,this._targetEnd.x,this.restThreshold)&&De(this._target.y,this._targetEnd.y,this.restThreshold)&&De(this._target.z,this._targetEnd.z,this.restThreshold)&&De(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&De(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&De(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=be,this._changedDolly=0;const f=Ge.set(s,n,r),m=qe.set(t,e,i);si.setFromVector3(m.sub(f).applyQuaternion(this._yAxisUpSpace));const g=Xe.set(h,c,d),_=qe.set(o,a,l);ni.setFromVector3(_.sub(g).applyQuaternion(this._yAxisUpSpace)),this._targetEnd.copy(f.lerp(g,u));const y=ni.theta-si.theta,w=ni.phi-si.phi,v=ni.radius-si.radius;this._sphericalEnd.set(si.radius+v*u,si.phi+w*u,si.theta+y*u),this.normalizeRotations(),this._needsUpdate=!0,p||(this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd));const b=!p||De(this._target.x,this._targetEnd.x,this.restThreshold)&&De(this._target.y,this._targetEnd.y,this.restThreshold)&&De(this._target.z,this._targetEnd.z,this.restThreshold)&&De(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&De(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&De(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(Ge),r=this.setLookAt(n.x,n.y,n.z,t,e,i,s);return this._sphericalEnd.phi=Pe(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||De(this._focalOffset.x,this._focalOffsetEnd.x,this.restThreshold)&&De(this._focalOffset.y,this._focalOffsetEnd.y,this.restThreshold)&&De(this._focalOffset.z,this._focalOffsetEnd.z,this.restThreshold);return this._createOnRestPromise(n)}setOrbitPoint(t,e,i){this._camera.updateMatrixWorld(),Ke.setFromMatrixColumn(this._camera.matrixWorldInverse,0),Je.setFromMatrixColumn(this._camera.matrixWorldInverse,1),ti.setFromMatrixColumn(this._camera.matrixWorldInverse,2);const s=Ge.set(t,e,i),n=s.distanceTo(this._camera.position),r=s.sub(this._camera.position);Ke.multiplyScalar(r.x),Je.multiplyScalar(r.y),ti.multiplyScalar(r.z),Ge.copy(Ke).add(Je).add(ti),Ge.z=Ge.z+n,this.dollyTo(n,!1),this.setFocalOffset(-Ge.x,Ge.y,-Ge.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 He.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(Le(this._camera,"getDistanceToFitBox"))return this._spherical.radius;const n=t/e,r=this._camera.getEffectiveFOV()*Ae,o=this._camera.aspect;return.5*((s?n>o:n<o)?e:t/o)/Math.tan(.5*r)+.5*i}getDistanceToFitSphere(t){if(Le(this._camera,"getDistanceToFitSphere"))return this._spherical.radius;const e=this._camera.getEffectiveFOV()*Ae,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 He.Vector3).copy(e?this._targetEnd:this._target)}getPosition(t,e=!0){return(t&&t.isVector3?t:new He.Vector3).setFromSpherical(e?this._sphericalEnd:this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(e?this._targetEnd:this._target)}getSpherical(t,e=!0){return(t||new He.Spherical).copy(e?this._sphericalEnd:this._spherical)}getFocalOffset(t,e=!0){return(t&&t.isVector3?t:new He.Vector3).copy(e?this._focalOffsetEnd:this._focalOffset)}normalizeRotations(){this._sphericalEnd.theta=this._sphericalEnd.theta%Se,this._sphericalEnd.theta<0&&(this._sphericalEnd.theta+=Se),this._spherical.theta+=Se*Math.round((this._sphericalEnd.theta-this._spherical.theta)/Se)}stop(){this._focalOffset.copy(this._focalOffsetEnd),this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd),this._zoom=this._zoomEnd}reset(t=!1){if(!De(this._camera.up.x,this._cameraUp0.x)||!De(this._camera.up.y,this._cameraUp0.y)||!De(this._camera.up.z,this._cameraUp0.z)){this._camera.up.copy(this._cameraUp0);const t=this.getPosition(Ge);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,$e),this._yAxisUpSpaceInverse.copy(this._yAxisUpSpace).invert()}applyCameraUp(){const t=Ge.subVectors(this._target,this._camera.position).normalize(),e=qe.crossVectors(t,this._camera.up);this._camera.up.crossVectors(e,t).normalize(),this._camera.updateMatrixWorld();const i=this.getPosition(Ge);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=ei.subVectors(this._targetEnd,this._target),r=ii.subVectors(this._focalOffsetEnd,this._focalOffset),o=this._zoomEnd-this._zoom;if(Ie(e))this._thetaVelocity.value=0,this._spherical.theta=this._sphericalEnd.theta;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.theta=Be(this._spherical.theta,this._sphericalEnd.theta,this._thetaVelocity,e,1/0,t),this._needsUpdate=!0}if(Ie(i))this._phiVelocity.value=0,this._spherical.phi=this._sphericalEnd.phi;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.phi=Be(this._spherical.phi,this._sphericalEnd.phi,this._phiVelocity,e,1/0,t),this._needsUpdate=!0}if(Ie(s))this._radiusVelocity.value=0,this._spherical.radius=this._sphericalEnd.radius;else{const e=this._isUserControllingDolly?this.draggingSmoothTime:this.smoothTime;this._spherical.radius=Be(this._spherical.radius,this._sphericalEnd.radius,this._radiusVelocity,e,this.maxSpeed,t),this._needsUpdate=!0}if(Ie(n.x)&&Ie(n.y)&&Ie(n.z))this._targetVelocity.set(0,0,0),this._target.copy(this._targetEnd);else{const e=this._isUserControllingTruck?this.draggingSmoothTime:this.smoothTime;Ne(this._target,this._targetEnd,this._targetVelocity,e,this.maxSpeed,t,this._target),this._needsUpdate=!0}if(Ie(r.x)&&Ie(r.y)&&Ie(r.z))this._focalOffsetVelocity.set(0,0,0),this._focalOffset.copy(this._focalOffsetEnd);else{const e=this._isUserControllingOffset?this.draggingSmoothTime:this.smoothTime;Ne(this._focalOffset,this._focalOffsetEnd,this._focalOffsetVelocity,e,this.maxSpeed,t,this._focalOffset),this._needsUpdate=!0}if(Ie(o))this._zoomVelocity.value=0,this._zoom=this._zoomEnd;else{const e=this._isUserControllingZoom?this.draggingSmoothTime:this.smoothTime;this._zoom=Be(this._zoom,this._zoomEnd,this._zoomVelocity,e,1/0,t)}if(this.dollyToCursor)if(Te(this._camera)&&0!==this._changedDolly){const t=this._spherical.radius-this._lastDistance,e=this._camera,i=this._getCameraDirection(Qe),s=Ge.copy(i).cross(e.up).normalize();0===s.lengthSq()&&(s.x=1);const n=qe.crossVectors(s,i),r=this._sphericalEnd.radius*Math.tan(e.getEffectiveFOV()*Ae*.5),o=(this._sphericalEnd.radius-t-this._sphericalEnd.radius)/this._sphericalEnd.radius,a=Xe.copy(this._targetEnd).add(s.multiplyScalar(this._dollyControlCoord.x*r*e.aspect)).add(n.multiplyScalar(this._dollyControlCoord.y*r)),l=Ge.copy(this._targetEnd).lerp(a,o),h=this._lastDollyDirection===xe&&this._spherical.radius<=this.minDistance,c=this._lastDollyDirection===Ee&&this.maxDistance<=this._spherical.radius;if(this.infinityDolly&&(h||c)){this._sphericalEnd.radius-=t,this._spherical.radius-=t;const e=qe.copy(i).multiplyScalar(-t);l.add(e)}this._boundary.clampPoint(l,l);const d=qe.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedDolly-=t,Ie(this._changedDolly)&&(this._changedDolly=0)}else if(Ce(this._camera)&&0!==this._changedZoom){const t=this._zoom-this._lastZoom,e=this._camera,i=Ge.set(this._dollyControlCoord.x,this._dollyControlCoord.y,(e.near+e.far)/(e.near-e.far)).unproject(e),s=qe.set(0,0,-1).applyQuaternion(e.quaternion),n=Xe.copy(i).add(s.multiplyScalar(-i.dot(e.up))),r=-(this._zoom-t-this._zoom)/this._zoom,o=this._getCameraDirection(Qe),a=this._targetEnd.dot(o),l=Ge.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=qe.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedZoom-=t,Ie(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);(!Ie(this._focalOffset.x)||!Ie(this._focalOffset.y)||!Ie(this._focalOffset.z))&&(Ke.setFromMatrixColumn(this._camera.matrix,0),Je.setFromMatrixColumn(this._camera.matrix,1),ti.setFromMatrixColumn(this._camera.matrix,2),Ke.multiplyScalar(this._focalOffset.x),Je.multiplyScalar(-this._focalOffset.y),ti.multiplyScalar(this._focalOffset.z),Ge.copy(Ke).add(Je).add(ti),this._camera.position.add(Ge),this._camera.updateMatrixWorld()),this._boundaryEnclosesCamera&&this._encloseToBoundary(this._camera.position.copy(this._target),Ge.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"}),Ie(e,this.restThreshold)&&Ie(i,this.restThreshold)&&Ie(s,this.restThreshold)&&Ie(n.x,this.restThreshold)&&Ie(n.y,this.restThreshold)&&Ie(n.z,this.restThreshold)&&Ie(r.x,this.restThreshold)&&Ie(r.y,this.restThreshold)&&Ie(r.z,this.restThreshold)&&Ie(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:Me(this.maxDistance),minZoom:this.minZoom,maxZoom:Me(this.maxZoom),minPolarAngle:this.minPolarAngle,maxPolarAngle:Me(this.maxPolarAngle),minAzimuthAngle:Me(this.minAzimuthAngle),maxAzimuthAngle:Me(this.maxAzimuthAngle),smoothTime:this.smoothTime,draggingSmoothTime:this.draggingSmoothTime,dollySpeed:this.dollySpeed,truckSpeed:this.truckSpeed,dollyToCursor:this.dollyToCursor,target:this._targetEnd.toArray(),position:Ge.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=ze(i.maxDistance),this.minZoom=i.minZoom,this.maxZoom=ze(i.maxZoom),this.minPolarAngle=i.minPolarAngle,this.maxPolarAngle=ze(i.maxPolarAngle),this.minAzimuthAngle=ze(i.minAzimuthAngle),this.maxAzimuthAngle=ze(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),si.setFromVector3(Ge.fromArray(i.position).sub(this._targetEnd).applyQuaternion(this._yAxisUpSpace)),this.rotateTo(si.theta,si.phi,e),this.dollyTo(si.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=qe.copy(e).add(t),r=this._boundary.clampPoint(n,Xe).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(qe.copy(e).multiplyScalar(s)).add(r.multiplyScalar(1-i))}}_updateNearPlaneCorners(){if(Te(this._camera)){const t=this._camera,e=t.near,i=t.getEffectiveFOV()*Ae,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(Ce(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(Le(this._camera,"_collisionTest"))return t;const e=this._getTargetDirection(Qe);ci.lookAt(Ze,e,this._camera.up);for(let i=0;i<4;i++){const s=qe.copy(this._nearPlaneCorners[i]);s.applyMatrix4(ci);const n=Xe.addVectors(this._target,s);di.set(n,e),di.far=this._spherical.radius+1;const r=di.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 He.Sphere){const i=e,s=i.center;ri.makeEmpty(),t.traverseVisible(t=>{t.isMesh&&ri.expandByObject(t)}),ri.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++)Ge.fromBufferAttribute(r,t),n=Math.max(n,s.distanceToSquared(Ge))}),i.radius=Math.sqrt(n),i}}class pi 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!");ui.install({THREE:pi.getSubsetOfThree()});const{domElement:t}=this.currentWorld.renderer.three,e=new ui(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 fi=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(fi,"uuid","fdb61dc4-2ec1-4966-b83d-54ea795fad4a");let mi=fi;class gi 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 _i{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 pi&&(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 gi(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 pi))return;const e=this.world.camera.controls;t?e.addEventListener("update",this.updateZoom):e.removeEventListener("update",this.updateZoom)}}const yi=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 _i(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(yi,"uuid","d1e814d5-b81c-4452-87a2-f039375e0489");let wi=yi;const vi=1.25,bi=65535,xi=Math.pow(2,-24),Ei=Symbol("SKIP_GENERATION");function Ti(t){return function(t){return t.index?t.index.count:t.attributes.position.count}(t)/3}function Ci(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 Si(t){const e=Ti(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 Si(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 Ai(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 Pi(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 Ii(t,e){e.set(t)}function Di(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 ki(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 Mi(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 zi(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 Bi=32,Ni=(t,e)=>t.candidate-e.candidate,Ui=new Array(Bi).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),Li=new Float32Array(6);class Ri{constructor(){}}function Vi(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 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;{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 ji(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=Ti(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=Ti(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)*xi,c<e[t]&&(e[t]=c),m>e[t+3]&&(e[t+3]=m)}}return o}(i,u),m=e.indirect?Fi:Vi,g=[],_=e.indirect?Si(i):Oi(i);if(1===_.length){const t=_[0],e=new Ri;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 Ri;e.boundingData=new Float32Array(6),zi(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=Pi(e),-1!==o&&(a=(e[o]+e[o+3])/2);else if(1===r)o=Pi(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=Mi(t);let l=vi*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)/Bi;if(n<8){const e=[...Ui];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;ki(n,i,o)}e.sort(Ni);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?ki(s,i,r.rightCacheBounds):(ki(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=Mi(d)/r);let f=0;0!==c&&(f=Mi(u)/r);const m=1+vi*(p*h+f*c);m<l&&(o=t,l=m,a=s.candidate)}}else{for(let t=0;t<Bi;t++){const e=Ui[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>=Bi&&(n=31);const r=Ui[n];r.count++,ki(e,i,r.bounds)}const e=Ui[31];Ii(e.bounds,e.rightCacheBounds);for(let t=30;t>=0;t--){const e=Ui[t],i=Ui[t+1];Di(e.bounds,i.rightCacheBounds,e.rightCacheBounds)}let u=0;for(let e=0;e<31;e++){const i=Ui[e],s=i.count,h=i.bounds,c=Ui[e+1].rightCacheBounds;0!==s&&(0===u?Ii(h,Li):Di(h,Li,Li)),u+=s;let d=0,p=0;0!==u&&(d=Mi(Li)/r);const f=n-u;0!==f&&(p=Mi(c)/r);const m=1+vi*(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 Ri,s=e,n=_-e;t.left=i,i.boundingData=new Float32Array(6),zi(f,s,n,i.boundingData,p),w(i,s,n,p,u+1);const r=new Ri,o=_,a=l-n;t.right=r,r.boundingData=new Float32Array(6),zi(f,o,a,r.boundingData,p),w(r,o,a,p,u+1)}return t}}function Hi(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=Ti(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||Ci(i,e);const s=ji(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]=bi,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 Zi{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}}Zi.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 $i=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}}(),Wi=function(){const t=new s.Vector2,e=new s.Vector3,i=new s.Vector3;return function(s,n,r,o){$i(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))}}}(),Yi=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 Gi(t){return Math.abs(t)<1e-15}class qi 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 Zi),this.points=[this.a,this.b,this.c],this.sphere=new s.Sphere,this.plane=new s.Plane,this.needsUpdate=!0}intersectsSphere(t){return Yi(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}}qi.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]),Wi(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)}}(),qi.prototype.intersectsTriangle=function(){const t=new qi,e=new Array(3),i=new Array(3),n=new Zi,r=new Zi,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=Gi(e.distanceToPoint(o));if(Gi(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)&&!Gi(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)}}}(),qi.prototype.distanceToPoint=function(){const t=new s.Vector3;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),qi.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]),Wi(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 Xi{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 Zi),this.alignedSatBounds=new Array(3).fill().map(()=>new Zi),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}}Xi.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}}(),Xi.prototype.intersectsBox=function(){const t=new Zi;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}}(),Xi.prototype.intersectsTriangle=function(){const t=new qi,e=new Array(3),i=new Zi,n=new Zi,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}}(),Xi.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}}(),Xi.prototype.distanceToPoint=function(){const t=new s.Vector3;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),Xi.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];Wi(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 Qi{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 Ki extends Qi{constructor(){super(()=>new qi)}}const Ji=new Ki;function ts(t,e){return 65535===e[t+15]}function es(t,e){return e[t+6]}function is(t,e){return e[t+14]}function ss(t){return t+8}function ns(t,e){return e[t+6]}function rs(t,e){return e[t+7]}function os(t){return t}const as=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 ls,hs;const cs=[],ds=new Qi(()=>new s.Box3);function us(t,e,i,s,n,r){ls=ds.getPrimitive(),hs=ds.getPrimitive(),cs.push(ls,hs),as.setBuffer(t._roots[e]);const o=ps(0,t.geometry,i,s,n,r);as.clearBuffer(),ds.releasePrimitive(ls),ds.releasePrimitive(hs),cs.pop(),cs.pop();const a=cs.length;return a>0&&(hs=cs[a-1],ls=cs[a-2]),o}function ps(t,e,i,s,n=null,r=0,o=0){const{float32Array:a,uint16Array:l,uint32Array:h}=as;let c=2*t;if(ts(c,l)){const e=es(t,h),i=is(c,l);return Ai(t,a,ls),s(e,i,!1,o,r+t,ls)}{let c=function(t){const{uint16Array:e,uint32Array:i}=as;let s=2*t;for(;!ts(s,e);)s=2*(t=ss(t));return es(t,i)},d=function(t){const{uint16Array:e,uint32Array:i}=as;let s=2*t;for(;!ts(s,e);)s=2*(t=ns(t,i));return es(t,i)+is(s,e)};const u=ss(t),p=ns(t,h);let f,m,g,_,y=u,w=p;if(n&&(g=ls,_=hs,Ai(y,a,g),Ai(w,a,_),f=n(g),m=n(_),m<f)){y=p,w=u;const t=f;f=m,m=t,g=_}g||(g=ls,Ai(y,a,g));const v=i(g,ts(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&&ps(y,e,i,s,n,r,o+1);if(b)return!0;_=hs,Ai(w,a,_);const x=i(_,ts(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&&ps(w,e,i,s,n,r,o+1);return!!E}}const fs=new s.Vector3,ms=new s.Vector3;const gs=new s.Vector3,_s=new s.Vector3,ys=new s.Vector3,ws=new s.Vector2,vs=new s.Vector2,bs=new s.Vector2,xs=new s.Vector3,Es=new s.Vector3,Ts=new s.Vector3,Cs=new s.Vector3;function Ss(t,e,i,n,r,o,a,l,h){gs.fromBufferAttribute(e,o),_s.fromBufferAttribute(e,a),ys.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,gs,_s,ys,Cs,h);if(c){n&&(ws.fromBufferAttribute(n,o),vs.fromBufferAttribute(n,a),bs.fromBufferAttribute(n,l),c.uv=s.Triangle.getInterpolation(Cs,gs,_s,ys,ws,vs,bs,new s.Vector2)),r&&(ws.fromBufferAttribute(r,o),vs.fromBufferAttribute(r,a),bs.fromBufferAttribute(r,l),c.uv1=s.Triangle.getInterpolation(Cs,gs,_s,ys,ws,vs,bs,new s.Vector2)),i&&(xs.fromBufferAttribute(i,o),Es.fromBufferAttribute(i,a),Ts.fromBufferAttribute(i,l),c.normal=s.Triangle.getInterpolation(Cs,gs,_s,ys,xs,Es,Ts,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(gs,_s,ys,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=Ss(i,c,d,u,p,o,a,l,e);return f?(f.faceIndex=s,n&&n.push(f),f):null}function As(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 Ps(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,As(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}function Is(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]===bi){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 Ds=new s.Box3;function ks(t,e,i,s){return Ai(t,e,Ds),i.intersectBox(Ds,s)}function Ms(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),As(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}const zs=new s.Vector3;function Bs(t,e,i,s,n){as.setBuffer(t._roots[e]),Ns(0,t,i,s,n),as.clearBuffer()}function Ns(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=as,l=2*t;if(ts(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,es(t,a),is(l,o),n)}else{const o=ss(t);ks(o,r,s,zs)&&Ns(o,e,i,s,n);const l=ns(t,a);ks(l,r,s,zs)&&Ns(l,e,i,s,n)}}const Us=new s.Vector3,Ls=["x","y","z"];function Rs(t,e,i,s){as.setBuffer(t._roots[e]);const n=Vs(0,t,i,s);return as.clearBuffer(),n}function Vs(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=as;let a=2*t;if(ts(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,es(t,o),is(a,r))}{const r=rs(t,o),a=Ls[r],l=s.direction[a]>=0;let h,c;l?(h=ss(t),c=ns(t,o)):(h=ns(t,o),c=ss(t));const d=ks(h,n,s,Us)?Vs(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=ks(c,n,s,Us)?Vs(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const Fs=new s.Box3,js=new qi,Hs=new qi,Zs=new s.Matrix4,$s=new Xi,Ws=new Xi;function Ys(t,e,i,s){as.setBuffer(t._roots[e]);const n=Gs(0,t,i,s);return as.clearBuffer(),n}function Gs(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=as;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),$s.set(i.boundingBox.min,i.boundingBox.max,s),n=$s);if(!ts(l,o)){const o=t+8,l=a[t+6];Ai(o,r,Fs);if(n.intersectsBox(Fs)&&Gs(o,e,i,s,n))return!0;Ai(l,r,Fs);return!!(n.intersectsBox(Fs)&&Gs(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=es(t,a),f=is(l,o);if(Zs.copy(s).invert(),i.boundsTree){Ai(t,r,Ws),Ws.matrix.copy(Zs),Ws.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>Ws.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(As(Hs,e,h,c),Hs.needsUpdate=!0,t.intersectsTriangle(Hs))return!0;return!1}})}for(let t=3*p,e=3*(f+p);t<e;t+=3){As(js,t,h,c),js.a.applyMatrix4(Zs),js.b.applyMatrix4(Zs),js.c.applyMatrix4(Zs),js.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(As(Hs,t,d,u),Hs.needsUpdate=!0,js.intersectsTriangle(Hs))return!0}}}const qs=new s.Matrix4,Xs=new Xi,Qs=new Xi,Ks=new s.Vector3,Js=new s.Vector3,tn=new s.Vector3,en=new s.Vector3;function sn(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),Xs.set(e.boundingBox.min,e.boundingBox.max,i),Xs.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=Ji.getPrimitive(),p=Ji.getPrimitive();let f=Ks,m=Js,g=null,_=null;n&&(g=tn,_=en);let y=1/0,w=null,v=null;return qs.copy(i).invert(),Qs.matrix.copy(qs),t.shapecast({boundsTraverseOrder:t=>Xs.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(Qs.min.copy(t.min),Qs.max.copy(t.max),Qs.needsUpdate=!0),!0),intersectsRange:(t,s)=>{if(e.boundsTree){return e.boundsTree.shapecast({boundsTraverseOrder:t=>Qs.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o,intersectsRange:(e,n)=>{for(let o=e,a=e+n;o<a;o++){As(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++){As(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=Ti(e);n<o;n++){As(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++){As(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}}}}),Ji.releasePrimitive(u),Ji.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(qs),m.applyMatrix4(qs),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}function nn(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]===bi){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 rn=new s.Vector3;function on(t,e,i,s,n){as.setBuffer(t._roots[e]),an(0,t,i,s,n),as.clearBuffer()}function an(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=as,l=2*t;if(ts(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,es(t,a),is(l,o),n)}else{const o=ss(t);ks(o,r,s,rn)&&an(o,e,i,s,n);const l=ns(t,a);ks(l,r,s,rn)&&an(l,e,i,s,n)}}const ln=new s.Vector3,hn=["x","y","z"];function cn(t,e,i,s){as.setBuffer(t._roots[e]);const n=dn(0,t,i,s);return as.clearBuffer(),n}function dn(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=as;let a=2*t;if(ts(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,es(t,o),is(a,r))}{const r=rs(t,o),a=hn[r],l=s.direction[a]>=0;let h,c;l?(h=ss(t),c=ns(t,o)):(h=ns(t,o),c=ss(t));const d=ks(h,n,s,ln)?dn(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=ks(c,n,s,ln)?dn(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const un=new s.Box3,pn=new qi,fn=new qi,mn=new s.Matrix4,gn=new Xi,_n=new Xi;function yn(t,e,i,s){as.setBuffer(t._roots[e]);const n=wn(0,t,i,s);return as.clearBuffer(),n}function wn(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=as;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),gn.set(i.boundingBox.min,i.boundingBox.max,s),n=gn);if(!ts(l,o)){const o=t+8,l=a[t+6];Ai(o,r,un);if(n.intersectsBox(un)&&wn(o,e,i,s,n))return!0;Ai(l,r,un);return!!(n.intersectsBox(un)&&wn(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=es(t,a),f=is(l,o);if(mn.copy(s).invert(),i.boundsTree){Ai(t,r,_n),_n.matrix.copy(mn),_n.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>_n.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(As(fn,3*e.resolveTriangleIndex(i),h,c),fn.needsUpdate=!0,t.intersectsTriangle(fn))return!0;return!1}})}for(let t=p,i=f+p;t<i;t++){const i=e.resolveTriangleIndex(t);As(pn,3*i,h,c),pn.a.applyMatrix4(mn),pn.b.applyMatrix4(mn),pn.c.applyMatrix4(mn),pn.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(As(fn,t,d,u),fn.needsUpdate=!0,pn.intersectsTriangle(fn))return!0}}}const vn=new s.Matrix4,bn=new Xi,xn=new Xi,En=new s.Vector3,Tn=new s.Vector3,Cn=new s.Vector3,Sn=new s.Vector3;function On(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),bn.set(e.boundingBox.min,e.boundingBox.max,i),bn.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=Ji.getPrimitive(),p=Ji.getPrimitive();let f=En,m=Tn,g=null,_=null;n&&(g=Cn,_=Sn);let y=1/0,w=null,v=null;return vn.copy(i).invert(),xn.matrix.copy(vn),t.shapecast({boundsTraverseOrder:t=>bn.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(xn.min.copy(t.min),xn.max.copy(t.max),xn.needsUpdate=!0),!0),intersectsRange:(s,n)=>{if(e.boundsTree){const a=e.boundsTree;return a.shapecast({boundsTraverseOrder:t=>xn.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);As(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);As(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=Ti(e);o<a;o++){As(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);As(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}}}}),Ji.releasePrimitive(u),Ji.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(vn),m.applyMatrix4(vn),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}const An=new as.constructor,Pn=new as.constructor,In=new Qi(()=>new s.Box3),Dn=new s.Box3,kn=new s.Box3,Mn=new s.Box3,zn=new s.Box3;let Bn=!1;function Nn(t,e,i,s,n,r=0,o=0,a=0,l=0,h=null,c=!1){let d,u;c?(d=Pn,u=An):(d=An,u=Pn);const p=d.float32Array,f=d.uint32Array,m=d.uint16Array,g=u.float32Array,_=u.uint32Array,y=u.uint16Array,w=2*e,v=ts(2*t,m),b=ts(w,y);let x=!1;if(b&&v)x=c?n(es(e,_),is(2*e,y),es(t,f),is(2*t,m),l,o+e,a,r+t):n(es(t,f),is(2*t,m),es(e,_),is(2*e,y),a,r+t,l,o+e);else if(b){const h=In.getPrimitive();Ai(e,g,h),h.applyMatrix4(i);const d=ss(t),u=ns(t,f);Ai(d,p,Dn),Ai(u,p,kn);const m=h.intersectsBox(Dn),_=h.intersectsBox(kn);x=m&&Nn(e,d,s,i,n,o,r,l,a+1,h,!c)||_&&Nn(e,u,s,i,n,o,r,l,a+1,h,!c),In.releasePrimitive(h)}else{const d=ss(e),u=ns(e,_);Ai(d,g,Mn),Ai(u,g,zn);const m=h.intersectsBox(Mn),y=h.intersectsBox(zn);if(m&&y)x=Nn(t,d,i,s,n,r,o,a,l+1,h,c)||Nn(t,u,i,s,n,r,o,a,l+1,h,c);else if(m)if(v)x=Nn(t,d,i,s,n,r,o,a,l+1,h,c);else{const e=In.getPrimitive();e.copy(Mn).applyMatrix4(i);const h=ss(t),u=ns(t,f);Ai(h,p,Dn),Ai(u,p,kn);const m=e.intersectsBox(Dn),g=e.intersectsBox(kn);x=m&&Nn(d,h,s,i,n,o,r,l,a+1,e,!c)||g&&Nn(d,u,s,i,n,o,r,l,a+1,e,!c),In.releasePrimitive(e)}else if(y)if(v)x=Nn(t,u,i,s,n,r,o,a,l+1,h,c);else{const e=In.getPrimitive();e.copy(zn).applyMatrix4(i);const h=ss(t),d=ns(t,f);Ai(h,p,Dn),Ai(d,p,kn);const m=e.intersectsBox(Dn),g=e.intersectsBox(kn);x=m&&Nn(u,h,s,i,n,o,r,l,a+1,e,!c)||g&&Nn(u,d,s,i,n,o,r,l,a+1,e,!c),In.releasePrimitive(e)}}return x}const Un=new Xi,Ln=new s.Box3;class Rn{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 Rn(e,{...i,[Ei]:!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,[Ei]:!1},e)).useSharedArrayBuffer&&"undefined"==typeof SharedArrayBuffer)throw new Error("MeshBVH: SharedArrayBuffer is not available.");this.geometry=t,this._roots=null,this._indirectBuffer=null,e[Ei]||(Hi(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?nn:Is)(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]===bi;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?on:Bs;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?cn:Rs;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?yn:Ys;for(let r=0,o=s.length;r<o&&(i=n(this,r,t,e),!i);r++);return i}shapecast(t){const e=Ji.getPrimitive(),i=this.indirect?Ms:Ps;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=us(this,t,n,r,s,l),a)break;l+=e.byteLength}return Ji.releasePrimitive(e),a}bvhcast(t,e,i){let{intersectsRanges:n,intersectsTriangles:r}=i;const o=Ji.getPrimitive(),a=this.geometry.index,l=this.geometry.attributes.position,h=this.indirect?t=>{const e=this.resolveTriangleIndex(t);As(o,3*e,a,l)}:t=>{As(o,3*t,a,l)},c=Ji.getPrimitive(),d=t.geometry.index,u=t.geometry.attributes.position,p=t.indirect?e=>{const i=t.resolveTriangleIndex(e);As(c,3*i,d,u)}:t=>{As(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(Bn)throw new Error("MeshBVH: Recursive calls to bvhcast not supported.");Bn=!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++){An.setBuffer(r[t]),h=0;const e=In.getPrimitive();Ai(os(0),An.float32Array,e),e.applyMatrix4(c);for(let s=0,r=o.length;s<r&&(Pn.setBuffer(o[t]),a=Nn(0,0,i,c,n,l,h,0,0,e),Pn.clearBuffer(),h+=o[s].length,!a);s++);if(In.releasePrimitive(e),An.clearBuffer(),l+=r[t].length,a)break}return Bn=!1,a}(this,t,e,n)}intersectsBox(t,e){return Un.set(t.min,t.max,e),Un.needsUpdate=!0,this.shapecast({intersectsBounds:t=>Un.intersectsBox(t),intersectsTriangle:t=>Un.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:sn)(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=>(fs.copy(e).clamp(t.min,t.max),fs.distanceToSquared(e)),intersectsBounds:(t,e,i)=>i<a&&i<o,intersectsTriangle:(t,i)=>{t.closestPointToPoint(e,fs);const s=e.distanceToSquared(fs);return s<a&&(ms.copy(fs),a=s,l=i),s<r}}),a===1/0)return null;const h=Math.sqrt(a);return i.point?i.point.copy(ms):i.point=ms.clone(),i.distance=h,i.faceIndex=l,i}(this,t,e,i,s)}getBoundingBox(t){t.makeEmpty();return this._roots.forEach(e=>{Ai(0,new Float32Array(e),Ln),t.union(Ln)}),t}}function Vn(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 Fn=new s.Ray,jn=new s.Matrix4,Hn=s.Mesh.prototype.raycast;function Zn(t,e){if(this.geometry.boundsTree){if(void 0===this.material)return;jn.copy(this.matrixWorld).invert(),Fn.copy(t.ray).applyMatrix4(jn);const i=this.geometry.boundsTree;if(!0===t.firstHitOnly){const s=Vn(i.raycastFirst(Fn,this.material),this,t);s&&e.push(s)}else{const s=i.raycast(Fn,this.material);for(let i=0,n=s.length;i<n;i++){const n=Vn(s[i],this,t);n&&e.push(n)}}}else Hn.call(this,t,e)}function $n(t){return this.boundsTree=new Rn(this,t),this.boundsTree}function Wn(){this.boundsTree=null}const Yn=class t{constructor(){e(this,"onDisposed",new h),e(this,"list",new Map),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=$n,a.BufferGeometry.prototype.disposeBoundsTree=Wn,a.Mesh.prototype.raycast=Zn}};e(Yn,"release","2.4.3");let Gn=Yn;class qn{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=ui.ACTION.DOLLY,t.touches.two=ui.ACTION.TOUCH_ZOOM_TRUCK}}class Xn{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 Qn{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=ui.ACTION.TRUCK,e.touches.one=ui.ACTION.TOUCH_TRUCK,e.touches.two=ui.ACTION.TOUCH_ZOOM):(e.mouseButtons.left=ui.ACTION.ROTATE,e.touches.one=this.mouseAction1,e.touches.two=this.mouseAction2)}}class Kn{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=ui.ACTION.ZOOM,this._component.controls.mouseButtons.middle=ui.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=ui.ACTION.DOLLY,this._component.controls.mouseButtons.middle=ui.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=ui.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 Jn extends pi{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 Kn(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 Xn(this)),this._navigationModes.set("FirstPerson",new qn(this)),this._navigationModes.set("Plan",new Qn(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 tr(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 er={exports:{}};
8
+ const _e=1,ye=2,we=4,ve=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}),be=0,xe=1,Ee=-1;function Te(t){return t.isPerspectiveCamera}function Ce(t){return t.isOrthographicCamera}const Se=2*Math.PI,Oe=Math.PI/2,Ae=Math.PI/180;function Pe(t,e,i){return Math.max(e,Math.min(i,t))}function Ie(t,e=1e-5){return Math.abs(t)<e}function De(t,e,i=1e-5){return Ie(t-e,i)}function ke(t,e){return Math.round(t/e)*e}function Me(t){return isFinite(t)?t:t<0?-Number.MAX_VALUE:Number.MAX_VALUE}function ze(t){return Math.abs(t)<Number.MAX_VALUE?t:t*(1/0)}function Be(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=Pe(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 Ne(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 Ue(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 Le(t,e){return!!Ce(t)&&(console.warn(`${e} is not supported in OrthographicCamera`),!0)}class Re{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 Ve;const Fe=1/8,je=/Mac/.test(null===(Ve=null===globalThis||void 0===globalThis?void 0:globalThis.navigator)||void 0===Ve?void 0:Ve.platform);let He,Ze,$e,We,Ye,Ge,qe,Xe,Qe,Ke,Je,ti,ei,ii,si,ni,ri,oi,ai,li,hi,ci,di;class ui extends Re{static install(t){He=t.THREE,Ze=Object.freeze(new He.Vector3(0,0,0)),$e=Object.freeze(new He.Vector3(0,1,0)),We=Object.freeze(new He.Vector3(0,0,1)),Ye=new He.Vector2,Ge=new He.Vector3,qe=new He.Vector3,Xe=new He.Vector3,Qe=new He.Vector3,Ke=new He.Vector3,Je=new He.Vector3,ti=new He.Vector3,ei=new He.Vector3,ii=new He.Vector3,si=new He.Spherical,ni=new He.Spherical,ri=new He.Box3,oi=new He.Box3,ai=new He.Sphere,li=new He.Quaternion,hi=new He.Quaternion,ci=new He.Matrix4,di=new He.Raycaster}static get ACTION(){return ve}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=ve.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=be,this._thetaVelocity={value:0},this._phiVelocity={value:0},this._radiusVelocity={value:0},this._targetVelocity=new He.Vector3,this._focalOffsetVelocity=new He.Vector3,this._zoomVelocity={value:0},this._truckInternal=(t,e,i,s)=>{let n,r;if(Te(this._camera)){const i=Ge.copy(this._camera.position).sub(this._target),s=this._camera.getEffectiveFOV()*Ae,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(!Ce(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=Se*this.azimuthRotateSpeed*t/this._elementRect.height,s=Se*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=Pe(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===He&&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 He.Quaternion).setFromUnitVectors(this._camera.up,$e),this._yAxisUpSpaceInverse=this._yAxisUpSpace.clone().invert(),this._state=ve.NONE,this._target=new He.Vector3,this._targetEnd=this._target.clone(),this._focalOffset=new He.Vector3,this._focalOffsetEnd=this._focalOffset.clone(),this._spherical=(new He.Spherical).setFromVector3(Ge.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 He.Vector3,new He.Vector3,new He.Vector3,new He.Vector3],this._updateNearPlaneCorners(),this._boundary=new He.Box3(new He.Vector3(-1/0,-1/0,-1/0),new He.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 He.Vector2,this.mouseButtons={left:ve.ROTATE,middle:ve.DOLLY,right:ve.TRUCK,wheel:Te(this._camera)?ve.DOLLY:Ce(this._camera)?ve.ZOOM:ve.NONE},this.touches={one:ve.TOUCH_ROTATE,two:Te(this._camera)?ve.TOUCH_DOLLY_TRUCK:Ce(this._camera)?ve.TOUCH_ZOOM_TRUCK:ve.NONE,three:ve.TOUCH_TRUCK};const i=new He.Vector2,s=new He.Vector2,n=new He.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&_e)===_e?_e:(t.buttons&we)===we?we:(t.buttons&ye)===ye?ye:null;if(null!==e){const t=this._findPointerByMouseButton(e);t&&this._disposePointer(t)}if((t.buttons&_e)===_e&&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&_e)===_e)&&(this._state=this._state|this.mouseButtons.left),this._isDragging&&(t.buttons&we)===we&&(this._state=this._state|this.mouseButtons.middle),this._isDragging&&(t.buttons&ye)===ye&&(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=ve.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=ve.NONE;p()}};let l=-1;const h=t=>{if(!this._domElement)return;if(!this._enabled||this.mouseButtons.wheel===ve.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===ve.ROTATE||this.mouseButtons.wheel===ve.TRUCK){const t=performance.now();l-t<1e3&&this._getClientRect(this._elementRect),l=t}const e=je?-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 ve.ROTATE:this._rotateInternal(t.deltaX,t.deltaY),this._isUserControllingRotate=!0;break;case ve.TRUCK:this._truckInternal(t.deltaX,t.deltaY,!1,!1),this._isUserControllingTruck=!0;break;case ve.SCREEN_PAN:this._truckInternal(t.deltaX,t.deltaY,!1,!0),this._isUserControllingTruck=!0;break;case ve.OFFSET:this._truckInternal(t.deltaX,t.deltaY,!0,!1),this._isUserControllingOffset=!0;break;case ve.DOLLY:this._dollyInternal(-i,s,n),this._isUserControllingDolly=!0;break;case ve.ZOOM:this._zoomInternal(-i,s,n),this._isUserControllingZoom=!0}this.dispatchEvent({type:"control"})},c=t=>{if(this._domElement&&this._enabled){if(this.mouseButtons.right===ui.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;Ue(this._activePointers,Ye),this._getClientRect(this._elementRect),i.copy(Ye),s.copy(Ye);if(this._activePointers.length>=2){const t=Ye.x-this._activePointers[1].clientX,e=Ye.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&_e)!==_e||(this._state=this._state|this.mouseButtons.left),(t.buttons&we)===we&&(this._state=this._state|this.mouseButtons.middle),(t.buttons&ye)===ye&&(this._state=this._state|this.mouseButtons.right);else this._lockedPointer&&(this._state=this._state|this.mouseButtons.left);(this._state&ve.ROTATE)!==ve.ROTATE&&(this._state&ve.TOUCH_ROTATE)!==ve.TOUCH_ROTATE&&(this._state&ve.TOUCH_DOLLY_ROTATE)!==ve.TOUCH_DOLLY_ROTATE&&(this._state&ve.TOUCH_ZOOM_ROTATE)!==ve.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&ve.TRUCK)!==ve.TRUCK&&(this._state&ve.SCREEN_PAN)!==ve.SCREEN_PAN&&(this._state&ve.TOUCH_TRUCK)!==ve.TOUCH_TRUCK&&(this._state&ve.TOUCH_SCREEN_PAN)!==ve.TOUCH_SCREEN_PAN&&(this._state&ve.TOUCH_DOLLY_TRUCK)!==ve.TOUCH_DOLLY_TRUCK&&(this._state&ve.TOUCH_DOLLY_SCREEN_PAN)!==ve.TOUCH_DOLLY_SCREEN_PAN&&(this._state&ve.TOUCH_ZOOM_TRUCK)!==ve.TOUCH_ZOOM_TRUCK&&(this._state&ve.TOUCH_ZOOM_SCREEN_PAN)!==ve.TOUCH_DOLLY_SCREEN_PAN||(this._targetEnd.copy(this._target),this._targetVelocity.set(0,0,0)),(this._state&ve.DOLLY)!==ve.DOLLY&&(this._state&ve.TOUCH_DOLLY)!==ve.TOUCH_DOLLY&&(this._state&ve.TOUCH_DOLLY_TRUCK)!==ve.TOUCH_DOLLY_TRUCK&&(this._state&ve.TOUCH_DOLLY_SCREEN_PAN)!==ve.TOUCH_DOLLY_SCREEN_PAN&&(this._state&ve.TOUCH_DOLLY_OFFSET)!==ve.TOUCH_DOLLY_OFFSET&&(this._state&ve.TOUCH_DOLLY_ROTATE)!==ve.TOUCH_DOLLY_ROTATE||(this._sphericalEnd.radius=this._spherical.radius,this._radiusVelocity.value=0),(this._state&ve.ZOOM)!==ve.ZOOM&&(this._state&ve.TOUCH_ZOOM)!==ve.TOUCH_ZOOM&&(this._state&ve.TOUCH_ZOOM_TRUCK)!==ve.TOUCH_ZOOM_TRUCK&&(this._state&ve.TOUCH_ZOOM_SCREEN_PAN)!==ve.TOUCH_ZOOM_SCREEN_PAN&&(this._state&ve.TOUCH_ZOOM_OFFSET)!==ve.TOUCH_ZOOM_OFFSET&&(this._state&ve.TOUCH_ZOOM_ROTATE)!==ve.TOUCH_ZOOM_ROTATE||(this._zoomEnd=this._zoom,this._zoomVelocity.value=0),(this._state&ve.OFFSET)!==ve.OFFSET&&(this._state&ve.TOUCH_OFFSET)!==ve.TOUCH_OFFSET&&(this._state&ve.TOUCH_DOLLY_OFFSET)!==ve.TOUCH_DOLLY_OFFSET&&(this._state&ve.TOUCH_ZOOM_OFFSET)!==ve.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,Ue(this._activePointers,Ye);const t=this._domElement&&this._domElement.ownerDocument.pointerLockElement===this._domElement?this._lockedPointer||this._activePointers[0]:null,e=t?-t.deltaX:s.x-Ye.x,r=t?-t.deltaY:s.y-Ye.y;if(s.copy(Ye),(this._state&ve.ROTATE)!==ve.ROTATE&&(this._state&ve.TOUCH_ROTATE)!==ve.TOUCH_ROTATE&&(this._state&ve.TOUCH_DOLLY_ROTATE)!==ve.TOUCH_DOLLY_ROTATE&&(this._state&ve.TOUCH_ZOOM_ROTATE)!==ve.TOUCH_ZOOM_ROTATE||(this._rotateInternal(e,r),this._isUserControllingRotate=!0),(this._state&ve.DOLLY)===ve.DOLLY||(this._state&ve.ZOOM)===ve.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&ve.DOLLY)===ve.DOLLY?(this._dollyInternal(s*r*Fe,t,e),this._isUserControllingDolly=!0):(this._zoomInternal(s*r*Fe,t,e),this._isUserControllingZoom=!0)}if((this._state&ve.TOUCH_DOLLY)===ve.TOUCH_DOLLY||(this._state&ve.TOUCH_ZOOM)===ve.TOUCH_ZOOM||(this._state&ve.TOUCH_DOLLY_TRUCK)===ve.TOUCH_DOLLY_TRUCK||(this._state&ve.TOUCH_ZOOM_TRUCK)===ve.TOUCH_ZOOM_TRUCK||(this._state&ve.TOUCH_DOLLY_SCREEN_PAN)===ve.TOUCH_DOLLY_SCREEN_PAN||(this._state&ve.TOUCH_ZOOM_SCREEN_PAN)===ve.TOUCH_ZOOM_SCREEN_PAN||(this._state&ve.TOUCH_DOLLY_OFFSET)===ve.TOUCH_DOLLY_OFFSET||(this._state&ve.TOUCH_ZOOM_OFFSET)===ve.TOUCH_ZOOM_OFFSET||(this._state&ve.TOUCH_DOLLY_ROTATE)===ve.TOUCH_DOLLY_ROTATE||(this._state&ve.TOUCH_ZOOM_ROTATE)===ve.TOUCH_ZOOM_ROTATE){const t=Ye.x-this._activePointers[1].clientX,e=Ye.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&ve.TOUCH_DOLLY)===ve.TOUCH_DOLLY||(this._state&ve.TOUCH_DOLLY_ROTATE)===ve.TOUCH_DOLLY_ROTATE||(this._state&ve.TOUCH_DOLLY_TRUCK)===ve.TOUCH_DOLLY_TRUCK||(this._state&ve.TOUCH_DOLLY_SCREEN_PAN)===ve.TOUCH_DOLLY_SCREEN_PAN||(this._state&ve.TOUCH_DOLLY_OFFSET)===ve.TOUCH_DOLLY_OFFSET?(this._dollyInternal(r*Fe,o,a),this._isUserControllingDolly=!0):(this._zoomInternal(r*Fe,o,a),this._isUserControllingZoom=!0)}(this._state&ve.TRUCK)!==ve.TRUCK&&(this._state&ve.TOUCH_TRUCK)!==ve.TOUCH_TRUCK&&(this._state&ve.TOUCH_DOLLY_TRUCK)!==ve.TOUCH_DOLLY_TRUCK&&(this._state&ve.TOUCH_ZOOM_TRUCK)!==ve.TOUCH_ZOOM_TRUCK||(this._truckInternal(e,r,!1,!1),this._isUserControllingTruck=!0),(this._state&ve.SCREEN_PAN)!==ve.SCREEN_PAN&&(this._state&ve.TOUCH_SCREEN_PAN)!==ve.TOUCH_SCREEN_PAN&&(this._state&ve.TOUCH_DOLLY_SCREEN_PAN)!==ve.TOUCH_DOLLY_SCREEN_PAN&&(this._state&ve.TOUCH_ZOOM_SCREEN_PAN)!==ve.TOUCH_ZOOM_SCREEN_PAN||(this._truckInternal(e,r,!1,!0),this._isUserControllingTruck=!0),(this._state&ve.OFFSET)!==ve.OFFSET&&(this._state&ve.TOUCH_OFFSET)!==ve.TOUCH_OFFSET&&(this._state&ve.TOUCH_DOLLY_OFFSET)!==ve.TOUCH_DOLLY_OFFSET&&(this._state&ve.TOUCH_ZOOM_OFFSET)!==ve.TOUCH_ZOOM_OFFSET||(this._truckInternal(e,r,!0,!1),this._isUserControllingOffset=!0),this.dispatchEvent({type:"control"})},p=()=>{Ue(this._activePointers,Ye),s.copy(Ye),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!==ve.NONE&&(this._state=ve.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=Pe(t.width,0,1),this._interactiveArea.height=Pe(t.height,0,1),this._interactiveArea.x=Pe(t.x,0,1-this._interactiveArea.width),this._interactiveArea.y=Pe(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=Pe(t,this.minAzimuthAngle,this.maxAzimuthAngle),n=Pe(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||De(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&De(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=be,this._changedDolly=0,this._dollyToNoClamp(Pe(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=De(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||De(this._spherical.radius,this._sphericalEnd.radius,this.restThreshold);return this._createOnRestPromise(s)}dollyInFixed(t,e=!1){this._targetEnd.add(this._getCameraDirection(Qe).multiplyScalar(t)),e||this._target.copy(this._targetEnd);const i=!e||De(this._target.x,this._targetEnd.x,this.restThreshold)&&De(this._target.y,this._targetEnd.y,this.restThreshold)&&De(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=Pe(t,this.minZoom,this.maxZoom),this._needsUpdate=!0,e||(this._zoom=this._zoomEnd);const i=!e||De(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(),Ke.setFromMatrixColumn(this._camera.matrix,0),Je.setFromMatrixColumn(this._camera.matrix,1),Ke.multiplyScalar(t),Je.multiplyScalar(-e);const s=Ge.copy(Ke).add(Je),n=qe.copy(this._targetEnd).add(s);return this.moveTo(n.x,n.y,n.z,i)}forward(t,e=!1){Ge.setFromMatrixColumn(this._camera.matrix,0),Ge.crossVectors(this._camera.up,Ge),Ge.multiplyScalar(t);const i=qe.copy(this._targetEnd).add(Ge);return this.moveTo(i.x,i.y,i.z,e)}elevate(t,e=!1){return Ge.copy(this._camera.up).multiplyScalar(t),this.moveTo(this._targetEnd.x+Ge.x,this._targetEnd.y+Ge.y,this._targetEnd.z+Ge.z,e)}moveTo(t,e,i,s=!1){this._isUserControllingTruck=!1;const n=Ge.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||De(this._target.x,this._targetEnd.x,this.restThreshold)&&De(this._target.y,this._targetEnd.y,this.restThreshold)&&De(this._target.z,this._targetEnd.z,this.restThreshold);return this._createOnRestPromise(r)}lookInDirectionOf(t,e,i,s=!1){const n=Ge.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?ri.copy(t):ri.setFromObject(t);l.isEmpty()&&(console.warn("camera-controls: fitTo() cannot be used with an empty box. Aborting"),Promise.resolve());const h=ke(this._sphericalEnd.theta,Oe),c=ke(this._sphericalEnd.phi,Oe);a.push(this.rotateTo(h,c,e));const d=Ge.setFromSpherical(this._sphericalEnd).normalize(),u=li.setFromUnitVectors(d,We),p=De(Math.abs(d.y),1);p&&u.multiply(hi.setFromAxisAngle($e,h)),u.multiply(this._yAxisUpSpaceInverse);const f=oi.makeEmpty();qe.copy(l.min).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.min).setX(l.max.x).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.min).setY(l.max.y).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.max).setZ(l.min.z).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.min).setZ(l.max.z).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.max).setY(l.min.y).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.max).setX(l.min.x).applyQuaternion(u),f.expandByPoint(qe),qe.copy(l.max).applyQuaternion(u),f.expandByPoint(qe),f.min.x-=s,f.min.y-=r,f.max.x+=n,f.max.y+=o,u.setFromUnitVectors(We,d),p&&u.premultiply(hi.invert()),u.premultiply(this._yAxisUpSpace);const m=f.getSize(Ge),g=f.getCenter(qe).applyQuaternion(u);if(Te(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(Ce(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?ui.createBoundingSphere(t,ai):ai.copy(t);if(i.push(this.moveTo(s.center.x,s.center.y,s.center.z,e)),Te(this._camera)){const t=this.getDistanceToFitSphere(s.radius);i.push(this.dollyTo(t,e))}else if(Ce(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=be,this._changedDolly=0;const a=qe.set(s,n,r),l=Ge.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||De(this._target.x,this._targetEnd.x,this.restThreshold)&&De(this._target.y,this._targetEnd.y,this.restThreshold)&&De(this._target.z,this._targetEnd.z,this.restThreshold)&&De(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&De(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&De(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=be,this._changedDolly=0;const f=Ge.set(s,n,r),m=qe.set(t,e,i);si.setFromVector3(m.sub(f).applyQuaternion(this._yAxisUpSpace));const g=Xe.set(h,c,d),_=qe.set(o,a,l);ni.setFromVector3(_.sub(g).applyQuaternion(this._yAxisUpSpace)),this._targetEnd.copy(f.lerp(g,u));const y=ni.theta-si.theta,w=ni.phi-si.phi,v=ni.radius-si.radius;this._sphericalEnd.set(si.radius+v*u,si.phi+w*u,si.theta+y*u),this.normalizeRotations(),this._needsUpdate=!0,p||(this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd));const b=!p||De(this._target.x,this._targetEnd.x,this.restThreshold)&&De(this._target.y,this._targetEnd.y,this.restThreshold)&&De(this._target.z,this._targetEnd.z,this.restThreshold)&&De(this._spherical.theta,this._sphericalEnd.theta,this.restThreshold)&&De(this._spherical.phi,this._sphericalEnd.phi,this.restThreshold)&&De(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(Ge),r=this.setLookAt(n.x,n.y,n.z,t,e,i,s);return this._sphericalEnd.phi=Pe(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||De(this._focalOffset.x,this._focalOffsetEnd.x,this.restThreshold)&&De(this._focalOffset.y,this._focalOffsetEnd.y,this.restThreshold)&&De(this._focalOffset.z,this._focalOffsetEnd.z,this.restThreshold);return this._createOnRestPromise(n)}setOrbitPoint(t,e,i){this._camera.updateMatrixWorld(),Ke.setFromMatrixColumn(this._camera.matrixWorldInverse,0),Je.setFromMatrixColumn(this._camera.matrixWorldInverse,1),ti.setFromMatrixColumn(this._camera.matrixWorldInverse,2);const s=Ge.set(t,e,i),n=s.distanceTo(this._camera.position),r=s.sub(this._camera.position);Ke.multiplyScalar(r.x),Je.multiplyScalar(r.y),ti.multiplyScalar(r.z),Ge.copy(Ke).add(Je).add(ti),Ge.z=Ge.z+n,this.dollyTo(n,!1),this.setFocalOffset(-Ge.x,Ge.y,-Ge.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 He.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(Le(this._camera,"getDistanceToFitBox"))return this._spherical.radius;const n=t/e,r=this._camera.getEffectiveFOV()*Ae,o=this._camera.aspect;return.5*((s?n>o:n<o)?e:t/o)/Math.tan(.5*r)+.5*i}getDistanceToFitSphere(t){if(Le(this._camera,"getDistanceToFitSphere"))return this._spherical.radius;const e=this._camera.getEffectiveFOV()*Ae,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 He.Vector3).copy(e?this._targetEnd:this._target)}getPosition(t,e=!0){return(t&&t.isVector3?t:new He.Vector3).setFromSpherical(e?this._sphericalEnd:this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(e?this._targetEnd:this._target)}getSpherical(t,e=!0){return(t||new He.Spherical).copy(e?this._sphericalEnd:this._spherical)}getFocalOffset(t,e=!0){return(t&&t.isVector3?t:new He.Vector3).copy(e?this._focalOffsetEnd:this._focalOffset)}normalizeRotations(){this._sphericalEnd.theta=this._sphericalEnd.theta%Se,this._sphericalEnd.theta<0&&(this._sphericalEnd.theta+=Se),this._spherical.theta+=Se*Math.round((this._sphericalEnd.theta-this._spherical.theta)/Se)}stop(){this._focalOffset.copy(this._focalOffsetEnd),this._target.copy(this._targetEnd),this._spherical.copy(this._sphericalEnd),this._zoom=this._zoomEnd}reset(t=!1){if(!De(this._camera.up.x,this._cameraUp0.x)||!De(this._camera.up.y,this._cameraUp0.y)||!De(this._camera.up.z,this._cameraUp0.z)){this._camera.up.copy(this._cameraUp0);const t=this.getPosition(Ge);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,$e),this._yAxisUpSpaceInverse.copy(this._yAxisUpSpace).invert()}applyCameraUp(){const t=Ge.subVectors(this._target,this._camera.position).normalize(),e=qe.crossVectors(t,this._camera.up);this._camera.up.crossVectors(e,t).normalize(),this._camera.updateMatrixWorld();const i=this.getPosition(Ge);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=ei.subVectors(this._targetEnd,this._target),r=ii.subVectors(this._focalOffsetEnd,this._focalOffset),o=this._zoomEnd-this._zoom;if(Ie(e))this._thetaVelocity.value=0,this._spherical.theta=this._sphericalEnd.theta;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.theta=Be(this._spherical.theta,this._sphericalEnd.theta,this._thetaVelocity,e,1/0,t),this._needsUpdate=!0}if(Ie(i))this._phiVelocity.value=0,this._spherical.phi=this._sphericalEnd.phi;else{const e=this._isUserControllingRotate?this.draggingSmoothTime:this.smoothTime;this._spherical.phi=Be(this._spherical.phi,this._sphericalEnd.phi,this._phiVelocity,e,1/0,t),this._needsUpdate=!0}if(Ie(s))this._radiusVelocity.value=0,this._spherical.radius=this._sphericalEnd.radius;else{const e=this._isUserControllingDolly?this.draggingSmoothTime:this.smoothTime;this._spherical.radius=Be(this._spherical.radius,this._sphericalEnd.radius,this._radiusVelocity,e,this.maxSpeed,t),this._needsUpdate=!0}if(Ie(n.x)&&Ie(n.y)&&Ie(n.z))this._targetVelocity.set(0,0,0),this._target.copy(this._targetEnd);else{const e=this._isUserControllingTruck?this.draggingSmoothTime:this.smoothTime;Ne(this._target,this._targetEnd,this._targetVelocity,e,this.maxSpeed,t,this._target),this._needsUpdate=!0}if(Ie(r.x)&&Ie(r.y)&&Ie(r.z))this._focalOffsetVelocity.set(0,0,0),this._focalOffset.copy(this._focalOffsetEnd);else{const e=this._isUserControllingOffset?this.draggingSmoothTime:this.smoothTime;Ne(this._focalOffset,this._focalOffsetEnd,this._focalOffsetVelocity,e,this.maxSpeed,t,this._focalOffset),this._needsUpdate=!0}if(Ie(o))this._zoomVelocity.value=0,this._zoom=this._zoomEnd;else{const e=this._isUserControllingZoom?this.draggingSmoothTime:this.smoothTime;this._zoom=Be(this._zoom,this._zoomEnd,this._zoomVelocity,e,1/0,t)}if(this.dollyToCursor)if(Te(this._camera)&&0!==this._changedDolly){const t=this._spherical.radius-this._lastDistance,e=this._camera,i=this._getCameraDirection(Qe),s=Ge.copy(i).cross(e.up).normalize();0===s.lengthSq()&&(s.x=1);const n=qe.crossVectors(s,i),r=this._sphericalEnd.radius*Math.tan(e.getEffectiveFOV()*Ae*.5),o=(this._sphericalEnd.radius-t-this._sphericalEnd.radius)/this._sphericalEnd.radius,a=Xe.copy(this._targetEnd).add(s.multiplyScalar(this._dollyControlCoord.x*r*e.aspect)).add(n.multiplyScalar(this._dollyControlCoord.y*r)),l=Ge.copy(this._targetEnd).lerp(a,o),h=this._lastDollyDirection===xe&&this._spherical.radius<=this.minDistance,c=this._lastDollyDirection===Ee&&this.maxDistance<=this._spherical.radius;if(this.infinityDolly&&(h||c)){this._sphericalEnd.radius-=t,this._spherical.radius-=t;const e=qe.copy(i).multiplyScalar(-t);l.add(e)}this._boundary.clampPoint(l,l);const d=qe.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedDolly-=t,Ie(this._changedDolly)&&(this._changedDolly=0)}else if(Ce(this._camera)&&0!==this._changedZoom){const t=this._zoom-this._lastZoom,e=this._camera,i=Ge.set(this._dollyControlCoord.x,this._dollyControlCoord.y,(e.near+e.far)/(e.near-e.far)).unproject(e),s=qe.set(0,0,-1).applyQuaternion(e.quaternion),n=Xe.copy(i).add(s.multiplyScalar(-i.dot(e.up))),r=-(this._zoom-t-this._zoom)/this._zoom,o=this._getCameraDirection(Qe),a=this._targetEnd.dot(o),l=Ge.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=qe.subVectors(l,this._targetEnd);this._targetEnd.copy(l),this._target.add(d),this._changedZoom-=t,Ie(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);(!Ie(this._focalOffset.x)||!Ie(this._focalOffset.y)||!Ie(this._focalOffset.z))&&(Ke.setFromMatrixColumn(this._camera.matrix,0),Je.setFromMatrixColumn(this._camera.matrix,1),ti.setFromMatrixColumn(this._camera.matrix,2),Ke.multiplyScalar(this._focalOffset.x),Je.multiplyScalar(-this._focalOffset.y),ti.multiplyScalar(this._focalOffset.z),Ge.copy(Ke).add(Je).add(ti),this._camera.position.add(Ge),this._camera.updateMatrixWorld()),this._boundaryEnclosesCamera&&this._encloseToBoundary(this._camera.position.copy(this._target),Ge.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"}),Ie(e,this.restThreshold)&&Ie(i,this.restThreshold)&&Ie(s,this.restThreshold)&&Ie(n.x,this.restThreshold)&&Ie(n.y,this.restThreshold)&&Ie(n.z,this.restThreshold)&&Ie(r.x,this.restThreshold)&&Ie(r.y,this.restThreshold)&&Ie(r.z,this.restThreshold)&&Ie(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:Me(this.maxDistance),minZoom:this.minZoom,maxZoom:Me(this.maxZoom),minPolarAngle:this.minPolarAngle,maxPolarAngle:Me(this.maxPolarAngle),minAzimuthAngle:Me(this.minAzimuthAngle),maxAzimuthAngle:Me(this.maxAzimuthAngle),smoothTime:this.smoothTime,draggingSmoothTime:this.draggingSmoothTime,dollySpeed:this.dollySpeed,truckSpeed:this.truckSpeed,dollyToCursor:this.dollyToCursor,target:this._targetEnd.toArray(),position:Ge.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=ze(i.maxDistance),this.minZoom=i.minZoom,this.maxZoom=ze(i.maxZoom),this.minPolarAngle=i.minPolarAngle,this.maxPolarAngle=ze(i.maxPolarAngle),this.minAzimuthAngle=ze(i.minAzimuthAngle),this.maxAzimuthAngle=ze(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),si.setFromVector3(Ge.fromArray(i.position).sub(this._targetEnd).applyQuaternion(this._yAxisUpSpace)),this.rotateTo(si.theta,si.phi,e),this.dollyTo(si.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=qe.copy(e).add(t),r=this._boundary.clampPoint(n,Xe).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(qe.copy(e).multiplyScalar(s)).add(r.multiplyScalar(1-i))}}_updateNearPlaneCorners(){if(Te(this._camera)){const t=this._camera,e=t.near,i=t.getEffectiveFOV()*Ae,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(Ce(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(Le(this._camera,"_collisionTest"))return t;const e=this._getTargetDirection(Qe);ci.lookAt(Ze,e,this._camera.up);for(let i=0;i<4;i++){const s=qe.copy(this._nearPlaneCorners[i]);s.applyMatrix4(ci);const n=Xe.addVectors(this._target,s);di.set(n,e),di.far=this._spherical.radius+1;const r=di.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 He.Sphere){const i=e,s=i.center;ri.makeEmpty(),t.traverseVisible(t=>{t.isMesh&&ri.expandByObject(t)}),ri.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++)Ge.fromBufferAttribute(r,t),n=Math.max(n,s.distanceToSquared(Ge))}),i.radius=Math.sqrt(n),i}}class pi 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!");ui.install({THREE:pi.getSubsetOfThree()});const{domElement:t}=this.currentWorld.renderer.three,e=new ui(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 fi=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(fi,"uuid","fdb61dc4-2ec1-4966-b83d-54ea795fad4a");let mi=fi;class gi 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 _i{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 pi&&(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 gi(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 pi))return;const e=this.world.camera.controls;t?e.addEventListener("update",this.updateZoom):e.removeEventListener("update",this.updateZoom)}}const yi=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 _i(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(yi,"uuid","d1e814d5-b81c-4452-87a2-f039375e0489");let wi=yi;const vi=1.25,bi=65535,xi=Math.pow(2,-24),Ei=Symbol("SKIP_GENERATION");function Ti(t){return function(t){return t.index?t.index.count:t.attributes.position.count}(t)/3}function Ci(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 Si(t){const e=Ti(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 Si(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 Ai(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 Pi(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 Ii(t,e){e.set(t)}function Di(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 ki(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 Mi(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 zi(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 Bi=32,Ni=(t,e)=>t.candidate-e.candidate,Ui=new Array(Bi).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),Li=new Float32Array(6);class Ri{constructor(){}}function Vi(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 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;{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 ji(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=Ti(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=Ti(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)*xi,c<e[t]&&(e[t]=c),m>e[t+3]&&(e[t+3]=m)}}return o}(i,u),m=e.indirect?Fi:Vi,g=[],_=e.indirect?Si(i):Oi(i);if(1===_.length){const t=_[0],e=new Ri;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 Ri;e.boundingData=new Float32Array(6),zi(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=Pi(e),-1!==o&&(a=(e[o]+e[o+3])/2);else if(1===r)o=Pi(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=Mi(t);let l=vi*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)/Bi;if(n<8){const e=[...Ui];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;ki(n,i,o)}e.sort(Ni);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?ki(s,i,r.rightCacheBounds):(ki(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=Mi(d)/r);let f=0;0!==c&&(f=Mi(u)/r);const m=1+vi*(p*h+f*c);m<l&&(o=t,l=m,a=s.candidate)}}else{for(let t=0;t<Bi;t++){const e=Ui[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>=Bi&&(n=31);const r=Ui[n];r.count++,ki(e,i,r.bounds)}const e=Ui[31];Ii(e.bounds,e.rightCacheBounds);for(let t=30;t>=0;t--){const e=Ui[t],i=Ui[t+1];Di(e.bounds,i.rightCacheBounds,e.rightCacheBounds)}let u=0;for(let e=0;e<31;e++){const i=Ui[e],s=i.count,h=i.bounds,c=Ui[e+1].rightCacheBounds;0!==s&&(0===u?Ii(h,Li):Di(h,Li,Li)),u+=s;let d=0,p=0;0!==u&&(d=Mi(Li)/r);const f=n-u;0!==f&&(p=Mi(c)/r);const m=1+vi*(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 Ri,s=e,n=_-e;t.left=i,i.boundingData=new Float32Array(6),zi(f,s,n,i.boundingData,p),w(i,s,n,p,u+1);const r=new Ri,o=_,a=l-n;t.right=r,r.boundingData=new Float32Array(6),zi(f,o,a,r.boundingData,p),w(r,o,a,p,u+1)}return t}}function Hi(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=Ti(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||Ci(i,e);const s=ji(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]=bi,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 Zi{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}}Zi.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 $i=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}}(),Wi=function(){const t=new s.Vector2,e=new s.Vector3,i=new s.Vector3;return function(s,n,r,o){$i(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))}}}(),Yi=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 Gi(t){return Math.abs(t)<1e-15}class qi 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 Zi),this.points=[this.a,this.b,this.c],this.sphere=new s.Sphere,this.plane=new s.Plane,this.needsUpdate=!0}intersectsSphere(t){return Yi(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}}qi.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]),Wi(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)}}(),qi.prototype.intersectsTriangle=function(){const t=new qi,e=new Array(3),i=new Array(3),n=new Zi,r=new Zi,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=Gi(e.distanceToPoint(o));if(Gi(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)&&!Gi(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)}}}(),qi.prototype.distanceToPoint=function(){const t=new s.Vector3;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),qi.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]),Wi(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 Xi{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 Zi),this.alignedSatBounds=new Array(3).fill().map(()=>new Zi),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}}Xi.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}}(),Xi.prototype.intersectsBox=function(){const t=new Zi;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}}(),Xi.prototype.intersectsTriangle=function(){const t=new qi,e=new Array(3),i=new Zi,n=new Zi,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}}(),Xi.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}}(),Xi.prototype.distanceToPoint=function(){const t=new s.Vector3;return function(e){return this.closestPointToPoint(e,t),e.distanceTo(t)}}(),Xi.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];Wi(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 Qi{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 Ki extends Qi{constructor(){super(()=>new qi)}}const Ji=new Ki;function ts(t,e){return 65535===e[t+15]}function es(t,e){return e[t+6]}function is(t,e){return e[t+14]}function ss(t){return t+8}function ns(t,e){return e[t+6]}function rs(t,e){return e[t+7]}function os(t){return t}const as=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 ls,hs;const cs=[],ds=new Qi(()=>new s.Box3);function us(t,e,i,s,n,r){ls=ds.getPrimitive(),hs=ds.getPrimitive(),cs.push(ls,hs),as.setBuffer(t._roots[e]);const o=ps(0,t.geometry,i,s,n,r);as.clearBuffer(),ds.releasePrimitive(ls),ds.releasePrimitive(hs),cs.pop(),cs.pop();const a=cs.length;return a>0&&(hs=cs[a-1],ls=cs[a-2]),o}function ps(t,e,i,s,n=null,r=0,o=0){const{float32Array:a,uint16Array:l,uint32Array:h}=as;let c=2*t;if(ts(c,l)){const e=es(t,h),i=is(c,l);return Ai(t,a,ls),s(e,i,!1,o,r+t,ls)}{let c=function(t){const{uint16Array:e,uint32Array:i}=as;let s=2*t;for(;!ts(s,e);)s=2*(t=ss(t));return es(t,i)},d=function(t){const{uint16Array:e,uint32Array:i}=as;let s=2*t;for(;!ts(s,e);)s=2*(t=ns(t,i));return es(t,i)+is(s,e)};const u=ss(t),p=ns(t,h);let f,m,g,_,y=u,w=p;if(n&&(g=ls,_=hs,Ai(y,a,g),Ai(w,a,_),f=n(g),m=n(_),m<f)){y=p,w=u;const t=f;f=m,m=t,g=_}g||(g=ls,Ai(y,a,g));const v=i(g,ts(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&&ps(y,e,i,s,n,r,o+1);if(b)return!0;_=hs,Ai(w,a,_);const x=i(_,ts(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&&ps(w,e,i,s,n,r,o+1);return!!E}}const fs=new s.Vector3,ms=new s.Vector3;const gs=new s.Vector3,_s=new s.Vector3,ys=new s.Vector3,ws=new s.Vector2,vs=new s.Vector2,bs=new s.Vector2,xs=new s.Vector3,Es=new s.Vector3,Ts=new s.Vector3,Cs=new s.Vector3;function Ss(t,e,i,n,r,o,a,l,h){gs.fromBufferAttribute(e,o),_s.fromBufferAttribute(e,a),ys.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,gs,_s,ys,Cs,h);if(c){n&&(ws.fromBufferAttribute(n,o),vs.fromBufferAttribute(n,a),bs.fromBufferAttribute(n,l),c.uv=s.Triangle.getInterpolation(Cs,gs,_s,ys,ws,vs,bs,new s.Vector2)),r&&(ws.fromBufferAttribute(r,o),vs.fromBufferAttribute(r,a),bs.fromBufferAttribute(r,l),c.uv1=s.Triangle.getInterpolation(Cs,gs,_s,ys,ws,vs,bs,new s.Vector2)),i&&(xs.fromBufferAttribute(i,o),Es.fromBufferAttribute(i,a),Ts.fromBufferAttribute(i,l),c.normal=s.Triangle.getInterpolation(Cs,gs,_s,ys,xs,Es,Ts,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(gs,_s,ys,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=Ss(i,c,d,u,p,o,a,l,e);return f?(f.faceIndex=s,n&&n.push(f),f):null}function As(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 Ps(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,As(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}function Is(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]===bi){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 Ds=new s.Box3;function ks(t,e,i,s){return Ai(t,e,Ds),i.intersectBox(Ds,s)}function Ms(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),As(o,3*t,l,h),o.needsUpdate=!0,s(o,t,n,r))return!0}return!1}const zs=new s.Vector3;function Bs(t,e,i,s,n){as.setBuffer(t._roots[e]),Ns(0,t,i,s,n),as.clearBuffer()}function Ns(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=as,l=2*t;if(ts(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,es(t,a),is(l,o),n)}else{const o=ss(t);ks(o,r,s,zs)&&Ns(o,e,i,s,n);const l=ns(t,a);ks(l,r,s,zs)&&Ns(l,e,i,s,n)}}const Us=new s.Vector3,Ls=["x","y","z"];function Rs(t,e,i,s){as.setBuffer(t._roots[e]);const n=Vs(0,t,i,s);return as.clearBuffer(),n}function Vs(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=as;let a=2*t;if(ts(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,es(t,o),is(a,r))}{const r=rs(t,o),a=Ls[r],l=s.direction[a]>=0;let h,c;l?(h=ss(t),c=ns(t,o)):(h=ns(t,o),c=ss(t));const d=ks(h,n,s,Us)?Vs(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=ks(c,n,s,Us)?Vs(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const Fs=new s.Box3,js=new qi,Hs=new qi,Zs=new s.Matrix4,$s=new Xi,Ws=new Xi;function Ys(t,e,i,s){as.setBuffer(t._roots[e]);const n=Gs(0,t,i,s);return as.clearBuffer(),n}function Gs(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=as;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),$s.set(i.boundingBox.min,i.boundingBox.max,s),n=$s);if(!ts(l,o)){const o=t+8,l=a[t+6];Ai(o,r,Fs);if(n.intersectsBox(Fs)&&Gs(o,e,i,s,n))return!0;Ai(l,r,Fs);return!!(n.intersectsBox(Fs)&&Gs(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=es(t,a),f=is(l,o);if(Zs.copy(s).invert(),i.boundsTree){Ai(t,r,Ws),Ws.matrix.copy(Zs),Ws.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>Ws.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(As(Hs,e,h,c),Hs.needsUpdate=!0,t.intersectsTriangle(Hs))return!0;return!1}})}for(let t=3*p,e=3*(f+p);t<e;t+=3){As(js,t,h,c),js.a.applyMatrix4(Zs),js.b.applyMatrix4(Zs),js.c.applyMatrix4(Zs),js.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(As(Hs,t,d,u),Hs.needsUpdate=!0,js.intersectsTriangle(Hs))return!0}}}const qs=new s.Matrix4,Xs=new Xi,Qs=new Xi,Ks=new s.Vector3,Js=new s.Vector3,tn=new s.Vector3,en=new s.Vector3;function sn(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),Xs.set(e.boundingBox.min,e.boundingBox.max,i),Xs.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=Ji.getPrimitive(),p=Ji.getPrimitive();let f=Ks,m=Js,g=null,_=null;n&&(g=tn,_=en);let y=1/0,w=null,v=null;return qs.copy(i).invert(),Qs.matrix.copy(qs),t.shapecast({boundsTraverseOrder:t=>Xs.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(Qs.min.copy(t.min),Qs.max.copy(t.max),Qs.needsUpdate=!0),!0),intersectsRange:(t,s)=>{if(e.boundsTree){return e.boundsTree.shapecast({boundsTraverseOrder:t=>Qs.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o,intersectsRange:(e,n)=>{for(let o=e,a=e+n;o<a;o++){As(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++){As(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=Ti(e);n<o;n++){As(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++){As(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}}}}),Ji.releasePrimitive(u),Ji.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(qs),m.applyMatrix4(qs),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}function nn(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]===bi){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 rn=new s.Vector3;function on(t,e,i,s,n){as.setBuffer(t._roots[e]),an(0,t,i,s,n),as.clearBuffer()}function an(t,e,i,s,n){const{float32Array:r,uint16Array:o,uint32Array:a}=as,l=2*t;if(ts(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,es(t,a),is(l,o),n)}else{const o=ss(t);ks(o,r,s,rn)&&an(o,e,i,s,n);const l=ns(t,a);ks(l,r,s,rn)&&an(l,e,i,s,n)}}const ln=new s.Vector3,hn=["x","y","z"];function cn(t,e,i,s){as.setBuffer(t._roots[e]);const n=dn(0,t,i,s);return as.clearBuffer(),n}function dn(t,e,i,s){const{float32Array:n,uint16Array:r,uint32Array:o}=as;let a=2*t;if(ts(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,es(t,o),is(a,r))}{const r=rs(t,o),a=hn[r],l=s.direction[a]>=0;let h,c;l?(h=ss(t),c=ns(t,o)):(h=ns(t,o),c=ss(t));const d=ks(h,n,s,ln)?dn(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=ks(c,n,s,ln)?dn(c,e,i,s):null;return d&&u?d.distance<=u.distance?d:u:d||u||null}}const un=new s.Box3,pn=new qi,fn=new qi,mn=new s.Matrix4,gn=new Xi,_n=new Xi;function yn(t,e,i,s){as.setBuffer(t._roots[e]);const n=wn(0,t,i,s);return as.clearBuffer(),n}function wn(t,e,i,s,n=null){const{float32Array:r,uint16Array:o,uint32Array:a}=as;let l=2*t;null===n&&(i.boundingBox||i.computeBoundingBox(),gn.set(i.boundingBox.min,i.boundingBox.max,s),n=gn);if(!ts(l,o)){const o=t+8,l=a[t+6];Ai(o,r,un);if(n.intersectsBox(un)&&wn(o,e,i,s,n))return!0;Ai(l,r,un);return!!(n.intersectsBox(un)&&wn(l,e,i,s,n))}{const n=e.geometry,h=n.index,c=n.attributes.position,d=i.index,u=i.attributes.position,p=es(t,a),f=is(l,o);if(mn.copy(s).invert(),i.boundsTree){Ai(t,r,_n),_n.matrix.copy(mn),_n.needsUpdate=!0;return i.boundsTree.shapecast({intersectsBounds:t=>_n.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(As(fn,3*e.resolveTriangleIndex(i),h,c),fn.needsUpdate=!0,t.intersectsTriangle(fn))return!0;return!1}})}for(let t=p,i=f+p;t<i;t++){const i=e.resolveTriangleIndex(t);As(pn,3*i,h,c),pn.a.applyMatrix4(mn),pn.b.applyMatrix4(mn),pn.c.applyMatrix4(mn),pn.needsUpdate=!0;for(let t=0,e=d.count;t<e;t+=3)if(As(fn,t,d,u),fn.needsUpdate=!0,pn.intersectsTriangle(fn))return!0}}}const vn=new s.Matrix4,bn=new Xi,xn=new Xi,En=new s.Vector3,Tn=new s.Vector3,Cn=new s.Vector3,Sn=new s.Vector3;function On(t,e,i,s={},n={},r=0,o=1/0){e.boundingBox||e.computeBoundingBox(),bn.set(e.boundingBox.min,e.boundingBox.max,i),bn.needsUpdate=!0;const a=t.geometry,l=a.attributes.position,h=a.index,c=e.attributes.position,d=e.index,u=Ji.getPrimitive(),p=Ji.getPrimitive();let f=En,m=Tn,g=null,_=null;n&&(g=Cn,_=Sn);let y=1/0,w=null,v=null;return vn.copy(i).invert(),xn.matrix.copy(vn),t.shapecast({boundsTraverseOrder:t=>bn.distanceToBox(t),intersectsBounds:(t,e,i)=>i<y&&i<o&&(e&&(xn.min.copy(t.min),xn.max.copy(t.max),xn.needsUpdate=!0),!0),intersectsRange:(s,n)=>{if(e.boundsTree){const a=e.boundsTree;return a.shapecast({boundsTraverseOrder:t=>xn.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);As(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);As(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=Ti(e);o<a;o++){As(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);As(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}}}}),Ji.releasePrimitive(u),Ji.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(vn),m.applyMatrix4(vn),n.distance=m.sub(n.point).length(),n.faceIndex=v),s)}const An=new as.constructor,Pn=new as.constructor,In=new Qi(()=>new s.Box3),Dn=new s.Box3,kn=new s.Box3,Mn=new s.Box3,zn=new s.Box3;let Bn=!1;function Nn(t,e,i,s,n,r=0,o=0,a=0,l=0,h=null,c=!1){let d,u;c?(d=Pn,u=An):(d=An,u=Pn);const p=d.float32Array,f=d.uint32Array,m=d.uint16Array,g=u.float32Array,_=u.uint32Array,y=u.uint16Array,w=2*e,v=ts(2*t,m),b=ts(w,y);let x=!1;if(b&&v)x=c?n(es(e,_),is(2*e,y),es(t,f),is(2*t,m),l,o+e,a,r+t):n(es(t,f),is(2*t,m),es(e,_),is(2*e,y),a,r+t,l,o+e);else if(b){const h=In.getPrimitive();Ai(e,g,h),h.applyMatrix4(i);const d=ss(t),u=ns(t,f);Ai(d,p,Dn),Ai(u,p,kn);const m=h.intersectsBox(Dn),_=h.intersectsBox(kn);x=m&&Nn(e,d,s,i,n,o,r,l,a+1,h,!c)||_&&Nn(e,u,s,i,n,o,r,l,a+1,h,!c),In.releasePrimitive(h)}else{const d=ss(e),u=ns(e,_);Ai(d,g,Mn),Ai(u,g,zn);const m=h.intersectsBox(Mn),y=h.intersectsBox(zn);if(m&&y)x=Nn(t,d,i,s,n,r,o,a,l+1,h,c)||Nn(t,u,i,s,n,r,o,a,l+1,h,c);else if(m)if(v)x=Nn(t,d,i,s,n,r,o,a,l+1,h,c);else{const e=In.getPrimitive();e.copy(Mn).applyMatrix4(i);const h=ss(t),u=ns(t,f);Ai(h,p,Dn),Ai(u,p,kn);const m=e.intersectsBox(Dn),g=e.intersectsBox(kn);x=m&&Nn(d,h,s,i,n,o,r,l,a+1,e,!c)||g&&Nn(d,u,s,i,n,o,r,l,a+1,e,!c),In.releasePrimitive(e)}else if(y)if(v)x=Nn(t,u,i,s,n,r,o,a,l+1,h,c);else{const e=In.getPrimitive();e.copy(zn).applyMatrix4(i);const h=ss(t),d=ns(t,f);Ai(h,p,Dn),Ai(d,p,kn);const m=e.intersectsBox(Dn),g=e.intersectsBox(kn);x=m&&Nn(u,h,s,i,n,o,r,l,a+1,e,!c)||g&&Nn(u,d,s,i,n,o,r,l,a+1,e,!c),In.releasePrimitive(e)}}return x}const Un=new Xi,Ln=new s.Box3;class Rn{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 Rn(e,{...i,[Ei]:!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,[Ei]:!1},e)).useSharedArrayBuffer&&"undefined"==typeof SharedArrayBuffer)throw new Error("MeshBVH: SharedArrayBuffer is not available.");this.geometry=t,this._roots=null,this._indirectBuffer=null,e[Ei]||(Hi(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?nn:Is)(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]===bi;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?on:Bs;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?cn:Rs;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?yn:Ys;for(let r=0,o=s.length;r<o&&(i=n(this,r,t,e),!i);r++);return i}shapecast(t){const e=Ji.getPrimitive(),i=this.indirect?Ms:Ps;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=us(this,t,n,r,s,l),a)break;l+=e.byteLength}return Ji.releasePrimitive(e),a}bvhcast(t,e,i){let{intersectsRanges:n,intersectsTriangles:r}=i;const o=Ji.getPrimitive(),a=this.geometry.index,l=this.geometry.attributes.position,h=this.indirect?t=>{const e=this.resolveTriangleIndex(t);As(o,3*e,a,l)}:t=>{As(o,3*t,a,l)},c=Ji.getPrimitive(),d=t.geometry.index,u=t.geometry.attributes.position,p=t.indirect?e=>{const i=t.resolveTriangleIndex(e);As(c,3*i,d,u)}:t=>{As(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(Bn)throw new Error("MeshBVH: Recursive calls to bvhcast not supported.");Bn=!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++){An.setBuffer(r[t]),h=0;const e=In.getPrimitive();Ai(os(0),An.float32Array,e),e.applyMatrix4(c);for(let s=0,r=o.length;s<r&&(Pn.setBuffer(o[t]),a=Nn(0,0,i,c,n,l,h,0,0,e),Pn.clearBuffer(),h+=o[s].length,!a);s++);if(In.releasePrimitive(e),An.clearBuffer(),l+=r[t].length,a)break}return Bn=!1,a}(this,t,e,n)}intersectsBox(t,e){return Un.set(t.min,t.max,e),Un.needsUpdate=!0,this.shapecast({intersectsBounds:t=>Un.intersectsBox(t),intersectsTriangle:t=>Un.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:sn)(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=>(fs.copy(e).clamp(t.min,t.max),fs.distanceToSquared(e)),intersectsBounds:(t,e,i)=>i<a&&i<o,intersectsTriangle:(t,i)=>{t.closestPointToPoint(e,fs);const s=e.distanceToSquared(fs);return s<a&&(ms.copy(fs),a=s,l=i),s<r}}),a===1/0)return null;const h=Math.sqrt(a);return i.point?i.point.copy(ms):i.point=ms.clone(),i.distance=h,i.faceIndex=l,i}(this,t,e,i,s)}getBoundingBox(t){t.makeEmpty();return this._roots.forEach(e=>{Ai(0,new Float32Array(e),Ln),t.union(Ln)}),t}}function Vn(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 Fn=new s.Ray,jn=new s.Matrix4,Hn=s.Mesh.prototype.raycast;function Zn(t,e){if(this.geometry.boundsTree){if(void 0===this.material)return;jn.copy(this.matrixWorld).invert(),Fn.copy(t.ray).applyMatrix4(jn);const i=this.geometry.boundsTree;if(!0===t.firstHitOnly){const s=Vn(i.raycastFirst(Fn,this.material),this,t);s&&e.push(s)}else{const s=i.raycast(Fn,this.material);for(let i=0,n=s.length;i<n;i++){const n=Vn(s[i],this,t);n&&e.push(n)}}}else Hn.call(this,t,e)}function $n(t){return this.boundsTree=new Rn(this,t),this.boundsTree}function Wn(){this.boundsTree=null}const Yn=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=$n,a.BufferGeometry.prototype.disposeBoundsTree=Wn,a.Mesh.prototype.raycast=Zn}};e(Yn,"release","2.4.3");let Gn=Yn;class qn{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=ui.ACTION.DOLLY,t.touches.two=ui.ACTION.TOUCH_ZOOM_TRUCK}}class Xn{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 Qn{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=ui.ACTION.TRUCK,e.touches.one=ui.ACTION.TOUCH_TRUCK,e.touches.two=ui.ACTION.TOUCH_ZOOM):(e.mouseButtons.left=ui.ACTION.ROTATE,e.touches.one=this.mouseAction1,e.touches.two=this.mouseAction2)}}class Kn{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=ui.ACTION.ZOOM,this._component.controls.mouseButtons.middle=ui.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=ui.ACTION.DOLLY,this._component.controls.mouseButtons.middle=ui.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=ui.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 Jn extends pi{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 Kn(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 Xn(this)),this._navigationModes.set("FirstPerson",new qn(this)),this._navigationModes.set("Plan",new Qn(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 tr(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 er={exports:{}};
9
9
  /*!
10
10
 
11
11
  JSZip v3.10.1 - A JavaScript class for generating and reading zip files