import{Ar as e,En as t,Ft as n,Gn as r,Mt as i,Pt as a,S as o,Xt as s,Yt as c,Zt as l,_ as u,_n as d,bn as f,jr as p,jt as m,kr as h,lr as g,m as _,x as v,yr as y}from"./three@0.169.19.js";var b=1.25,x=65535;x<<16;var S=2**-24,C=Symbol(`SKIP_GENERATION`),w={strategy:0,maxDepth:40,maxLeafSize:10,useSharedArrayBuffer:!1,setBoundingBox:!0,onProgress:null,indirect:!1,verbose:!0,range:null,[C]:!1};function T(e,t,n){return n.min.x=t[e],n.min.y=t[e+1],n.min.z=t[e+2],n.max.x=t[e+3],n.max.y=t[e+4],n.max.z=t[e+5],n}function ee(e){let t=-1,n=-1/0;for(let r=0;r<3;r++){let i=e[r+3]-e[r];i>n&&(n=i,t=r)}return t}function te(e,t){t.set(e)}function ne(e,t,n){let r,i;for(let a=0;a<3;a++){let o=a+3;r=e[a],i=t[a],n[a]=ri?r:i}}function E(e,t,n){for(let r=0;r<3;r++){let i=t[e+2*r],a=t[e+2*r+1],o=i-a,s=i+a;on[r+3]&&(n[r+3]=s)}}function D(e){let t=e[3]-e[0],n=e[4]-e[1],r=e[5]-e[2];return 2*(t*n+n*r+r*t)}function O(e,t){return t[e+15]===x}function k(e,t){return t[e+6]}function A(e,t){return t[e+14]}function j(e){return e+8}function M(e,t){return e+t[e+6]*8}function re(e,t){return t[e+7]}function N(e){return e}function ie(e,t,n,r,i){let a=1/0,o=1/0,s=1/0,c=-1/0,l=-1/0,u=-1/0,d=1/0,f=1/0,p=1/0,m=-1/0,h=-1/0,g=-1/0,_=e.offset||0;for(let r=(t-_)*6,i=(t+n-_)*6;rc&&(c=_),tm&&(m=t);let v=e[r+2],y=e[r+3],b=v-y,x=v+y;bl&&(l=x),vh&&(h=v);let S=e[r+4],C=e[r+5],w=S-C,T=S+C;wu&&(u=T),Sg&&(g=S)}r[0]=a,r[1]=o,r[2]=s,r[3]=c,r[4]=l,r[5]=u,i[0]=d,i[1]=f,i[2]=p,i[3]=m,i[4]=h,i[5]=g}var P=32,ae=(e,t)=>e.candidate-t.candidate,F=Array(P).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),oe=new Float32Array(6);function se(e,t,n,r,i,a){let o=-1,s=0;if(a===0)o=ee(t),o!==-1&&(s=(t[o]+t[o+3])/2);else if(a===1)o=ee(e),o!==-1&&(s=ce(n,r,i,o));else if(a===2){let a=D(e),c=b*i,l=n.offset||0,u=(r-l)*6,d=(r+i-l)*6;for(let e=0;e<3;e++){let r=t[e],l=(t[e+3]-r)/P;if(i

=a.candidate?E(r,n,a.rightCacheBounds):(E(r,n,a.leftCacheBounds),a.count++)}}for(let n=0;n=P&&(i=P-1);let a=F[i];a.count++,E(t,n,a.bounds)}let t=F[P-1];te(t.bounds,t.rightCacheBounds);for(let e=P-2;e>=0;e--){let t=F[e],n=F[e+1];ne(t.bounds,n.rightCacheBounds,t.rightCacheBounds)}let f=0;for(let t=0;t=c;)s--;if(ohe)throw Error(`MeshBVH: Cannot store relative child node offset greater than 32 bits.`);return fe[n+6]=c,fe[n+7]=a,ve(o,i)}}function ye(e,t,n,r,i){let{maxDepth:a,verbose:o,maxLeafSize:s,strategy:c,onProgress:l}=i,u=e.primitiveBuffer,d=e.primitiveBufferStride,f=new Float32Array(6),p=!1,m=new le;return ie(t,n,r,m.boundingData,f),g(m,n,r,f),m;function h(e){l&&l(e/r)}function g(e,n,r,i=null,l=0){if(!p&&l>=a&&(p=!0,o&&console.warn(`BVH: Max depth of ${a} reached when generating BVH. Consider increasing maxDepth.`)),r<=s||l>=a)return h(n+r),e.offset=n,e.count=r,e;let m=se(e.boundingData,i,t,n,r,c);if(m.axis===-1)return h(n+r),e.offset=n,e.count=r,e;let _=ue(u,d,t,n,r,m);if(_===n||_===n+r)h(n+r),e.offset=n,e.count=r;else{e.splitAxis=m.axis;let i=new le,a=n,o=_-n;e.left=i,ie(t,a,o,i.boundingData,f),g(i,a,o,f,l+1);let s=new le,c=_,u=r-o;e.right=s,ie(t,c,u,s.boundingData,f),g(s,c,u,f,l+1)}return e}}function be(e,t){let n=t.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer,r=e.getRootRanges(t.range),i=r[0],a=r[r.length-1],o={offset:i.offset,count:a.offset+a.count-i.offset},s=new Float32Array(6*o.count);s.offset=o.offset,e.computePrimitiveBounds(o.offset,o.count,s),e._roots=r.map(r=>{let i=ye(e,s,r.offset,r.count,t),a=new n(32*ge(i));return _e(0,i,a),a})}var xe=class{constructor(e){this._getNewPrimitive=e,this._primitives=[]}getPrimitive(){let e=this._primitives;return e.length===0?this._getNewPrimitive():e.pop()}releasePrimitive(e){this._primitives.push(e)}},I=new class{constructor(){this.float32Array=null,this.uint16Array=null,this.uint32Array=null;let e=[],t=null;this.setBuffer=n=>{t&&e.push(t),t=n,this.float32Array=new Float32Array(n),this.uint16Array=new Uint16Array(n),this.uint32Array=new Uint32Array(n)},this.clearBuffer=()=>{t=null,this.float32Array=null,this.uint16Array=null,this.uint32Array=null,e.length!==0&&this.setBuffer(e.pop())}}},L,Se,Ce=[],we=new xe(()=>new u);function Te(e,t,n,r,i,a){L=we.getPrimitive(),Se=we.getPrimitive(),Ce.push(L,Se),I.setBuffer(e._roots[t]);let o=Ee(0,e.geometry,n,r,i,a);I.clearBuffer(),we.releasePrimitive(L),we.releasePrimitive(Se),Ce.pop(),Ce.pop();let s=Ce.length;return s>0&&(Se=Ce[s-1],L=Ce[s-2]),o}function Ee(e,t,n,r,i=null,a=0,o=0){let{float32Array:s,uint16Array:c,uint32Array:l}=I,u=e*2;if(O(u,c)){let t=k(e,l),n=A(u,c);return T(N(e),s,L),r(t,n,!1,o,a+e/8,L)}else{let u=j(e),d=M(e,l),f=u,p=d,m,h,g,_;if(i&&(g=L,_=Se,T(N(f),s,g),T(N(p),s,_),m=i(g),h=i(_),hnew u),ke=new u,Ae=new u,je=new u,Me=new u,Ne=!1;function Pe(e,t,n,r){if(Ne)throw Error(`MeshBVH: Recursive calls to bvhcast not supported.`);Ne=!0;let i=e._roots,a=t._roots,o,c=0,l=0,u=new s().copy(n).invert();for(let e=0,t=i.length;es&&(s=u),tc&&(c=d),rl&&(l=f)}return n[r+0]=i,n[r+1]=a,n[r+2]=o,n[r+3]=s,n[r+4]=c,n[r+5]=l,n}computePrimitiveBounds(e,t,n){let r=n.offset||0;for(let i=e,a=e+t;i=0;e--){let t=e*8,n=t*2;if(O(n,i)){let e=k(t,r),o=A(n,i);this.writePrimitiveRangeBounds(e,o,Ie,0),a.set(Ie,t)}else{let e=j(t),n=M(t,r);for(let r=0;r<3;r++){let i=a[e+r],o=a[e+r+3],s=a[n+r],c=a[n+r+3];a[t+r]=ic?o:c}}}}}getBoundingBox(e){return e.makeEmpty(),this._roots.forEach(t=>{T(0,new Float32Array(t),Fe),e.union(Fe)}),e}shapecast(e){let{boundsTraverseOrder:t,intersectsBounds:n,intersectsRange:r,intersectsPrimitive:i,scratchPrimitive:a,iterate:o}=e;if(r&&i){let e=r;r=(t,n,r,s,c)=>e(t,n,r,s,c)?!0:o(t,n,this,i,r,s,a)}else r||=i?(e,t,n,r)=>o(e,t,this,i,n,r,a):(e,t,n)=>n;let s=!1,c=0,l=this._roots;for(let e=0,i=l.length;e65535?new Uint32Array(new t(4*e)):new Uint16Array(new t(2*e))}function He(e,t){if(!e.index){let n=e.attributes.position.count,r=Ve(n,t.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer);e.setIndex(new v(r,1));for(let e=0;e({offset:e.start/t,count:e.count/t}))}function Ge(e,t,n){let r=Ue(e,t,n),i=We(e,n);if(!i.length)return[r];let a=[],o=r.offset,s=r.offset+r.count,c=ze(e)/n,l=[];for(let e of i){let{offset:t,count:n}=e,r=t,i=t+(isFinite(n)?n:c-t);ro&&(l.push({pos:Math.max(o,r),isStart:!0}),l.push({pos:Math.min(s,i),isStart:!1}))}l.sort((e,t)=>e.pos===t.pos?e.type===`end`?-1:1:e.pos-t.pos);let u=0,d=null;for(let e of l){let t=e.pos;u!==0&&t!==d&&a.push({offset:d,count:t-d}),u+=e.isStart?1:-1,d=t}return a}function Ke(e,t){let n=e[e.length-1],r=n.offset+n.count>2**16,i=e.reduce((e,t)=>e+t.count,0),a=r?4:2,o=t?new SharedArrayBuffer(i*a):new ArrayBuffer(i*a),s=r?new Uint32Array(o):new Uint16Array(o),c=0;for(let t=0;tthis._indirectBuffer[e]:e=>e,this.primitiveBuffer=null,this.primitiveBufferStride=null,this._indirectBuffer=null,t={...w,...t},t[C]||this.init(t)}init(e){let{geometry:t,primitiveStride:n}=this;e.indirect?this._indirectBuffer=Ke(Ge(t,e.range,n),e.useSharedArrayBuffer):He(t,e),super.init(e),!t.boundingBox&&e.setBoundingBox&&(t.boundingBox=this.getBoundingBox(new u))}getRootRanges(e){return this.indirect?[{offset:0,count:this._indirectBuffer.length}]:Ge(this.geometry,e,this.primitiveStride)}raycastObject3D(){throw Error(`BVH: raycastObject3D() not implemented`)}},B=class{constructor(){this.min=1/0,this.max=-1/0}setFromPointsField(e,t){let n=1/0,r=-1/0;for(let i=0,a=e.length;ir?a:r}this.min=n,this.max=r}setFromPoints(e,t){let n=1/0,r=-1/0;for(let i=0,a=t.length;ir?o:r}this.min=n,this.max=r}isSeparated(e){return this.min>e.max||e.min>this.max}};B.prototype.setFromBox=(function(){let t=new e;return function(e,n){let r=n.min,i=n.max,a=1/0,o=-1/0;for(let n=0;n<=1;n++)for(let s=0;s<=1;s++)for(let c=0;c<=1;c++){t.x=r.x*n+i.x*(1-n),t.y=r.y*s+i.y*(1-s),t.z=r.z*c+i.z*(1-c);let l=e.dot(t);a=Math.min(l,a),o=Math.max(l,o)}this.min=a,this.max=o}})(),(function(){let e=new B;return function(t,n){let r=t.points,i=t.satAxes,a=t.satBounds,o=n.points,s=n.satAxes,c=n.satBounds;for(let t=0;t<3;t++){let n=a[t],r=i[t];if(e.setFromPoints(r,o),n.isSeparated(e))return!1}for(let t=0;t<3;t++){let n=c[t],i=s[t];if(e.setFromPoints(i,r),n.isSeparated(e))return!1}}})();var Je=(function(){let t=new e,n=new e,r=new e;return function(e,i,a){let o=e.start,s=t,c=i.start,l=n;r.subVectors(o,c),t.subVectors(e.end,e.start),n.subVectors(i.end,i.start);let u=r.dot(l),d=l.dot(s),f=l.dot(l),p=r.dot(s),m=s.dot(s)*f-d*d,h,g;h=m===0?0:(u*d-p*f)/m,g=(u+h*d)/f,a.x=h,a.y=g}})(),Ye=(function(){let t=new h,n=new e,r=new e;return function(e,i,a,o){Je(e,i,t);let s=t.x,c=t.y;if(s>=0&&s<=1&&c>=0&&c<=1){e.at(s,a),i.at(c,o);return}else if(s>=0&&s<=1){c<0?i.at(0,o):i.at(1,o),e.closestPointToPoint(o,!0,a);return}else if(c>=0&&c<=1){s<0?e.at(0,a):e.at(1,a),i.closestPointToPoint(a,!0,o);return}else{let t;t=s<0?e.start:e.end;let l;l=c<0?i.start:i.end;let u=n,d=r;if(e.closestPointToPoint(l,!0,n),i.closestPointToPoint(t,!0,r),u.distanceToSquared(l)<=d.distanceToSquared(t)){a.copy(u),o.copy(l);return}else{a.copy(t),o.copy(d);return}}}})(),Xe=(function(){let t=new e,n=new e,r=new d,a=new i;return function(e,i){let{radius:o,center:s}=e,{a:c,b:l,c:u}=i;if(a.start=c,a.end=l,a.closestPointToPoint(s,!0,t).distanceTo(s)<=o||(a.start=c,a.end=u,a.closestPointToPoint(s,!0,t).distanceTo(s)<=o)||(a.start=l,a.end=u,a.closestPointToPoint(s,!0,t).distanceTo(s)<=o))return!0;let d=i.getPlane(r);if(Math.abs(d.distanceToPoint(s))<=o){let e=d.projectPoint(s,n);if(i.containsPoint(e))return!0}return!1}})(),Ze=[`x`,`y`,`z`],V=1e-15,Qe=V*V;function H(e){return Math.abs(e)new e),this.satBounds=[,,,,].fill().map(()=>new B),this.points=[this.a,this.b,this.c],this.plane=new d,this.isDegenerateIntoSegment=!1,this.isDegenerateIntoPoint=!1,this.degenerateSegment=new i,this.needsUpdate=!0}intersectsSphere(e){return Xe(e,this)}update(){let e=this.a,t=this.b,n=this.c,r=this.points,i=this.satAxes,a=this.satBounds,o=i[0],s=a[0];this.getNormal(o),s.setFromPoints(o,r);let c=i[1],l=a[1];c.subVectors(e,t),l.setFromPoints(c,r);let u=i[2],d=a[2];u.subVectors(t,n),d.setFromPoints(u,r);let f=i[3],p=a[3];f.subVectors(n,e),p.setFromPoints(f,r);let m=c.length(),h=u.length(),g=f.length();this.isDegenerateIntoPoint=!1,this.isDegenerateIntoSegment=!1,m0)g(e.c,e.a,e.b,r,t,n,c,o,s,l,u);else if(a>0)g(e.b,e.a,e.c,n,t,r,s,o,c,l,u);else if(s*c>0||o!=0)g(e.a,e.b,e.c,t,n,r,o,s,c,l,u);else if(s!=0)g(e.b,e.a,e.c,n,t,r,s,o,c,l,u);else if(c!=0)g(e.c,e.a,e.b,r,t,n,c,o,s,l,u);else return!0;return!1}function v(e,t,n,r){let i=t.degenerateSegment,o=e.plane.distanceToPoint(i.start),s=e.plane.distanceToPoint(i.end);return H(o)?H(s)?m(e,t,n,r):(n&&(n.start.copy(i.start),n.end.copy(i.start)),e.containsPoint(i.start)):H(s)?(n&&(n.start.copy(i.end),n.end.copy(i.end)),e.containsPoint(i.end)):e.plane.intersectLine(i,a)==null?!1:(n&&(n.start.copy(a),n.end.copy(a)),e.containsPoint(a))}function y(e,t,n){let r=t.a;return H(e.plane.distanceToPoint(r))&&e.containsPoint(r)?(n&&(n.start.copy(r),n.end.copy(r)),!0):!1}function b(e,t,n){let r=e.degenerateSegment,i=t.a;return r.closestPointToPoint(i,!0,a),i.distanceToSquared(a)1||p<0||p>1?!1:H(r.start.z+c.z*f-(i.start.z+l.z*p))?(n&&(n.start.copy(r.start).addScaledVector(c,f),n.end.copy(r.start).addScaledVector(c,f)),!0):!1}else if(t.isDegenerateIntoPoint)return b(e,t,n);else return v(t,e,n,r);else if(e.isDegenerateIntoPoint)return t.isDegenerateIntoPoint?t.a.distanceToSquared(e.a)0&&b>0)return!1;let S=a.distanceToPoint(e.a),C=a.distanceToPoint(e.b),w=a.distanceToPoint(e.c);H(S)&&(S=0),H(C)&&(C=0),H(w)&&(w=0);let T=S*C,ee=S*w;if(T>0&&ee>0)return!1;o.copy(a.normal),s.copy(c.normal);let te=o.cross(s),ne=0,E=Math.abs(te.x),D=Math.abs(te.y);D>E&&(E=D,ne=1),Math.abs(te.z)>E&&(ne=2);let O=Ze[ne],k=this.a[O],A=this.b[O],j=this.c[O],M=e.a[O],re=e.b[O],N=e.c[O];if(_(this,k,A,j,y,b,h,g,v,f,l)||_(e,M,re,N,T,ee,S,C,w,p,u))return m(this,e,n,r);if(f.yf.x?n.start.copy(u.start):n.start.copy(l.start),p.ynew e),this.satAxes=[,,,].fill().map(()=>new e),this.satBounds=[,,,].fill().map(()=>new B),this.alignedSatBounds=[,,,].fill().map(()=>new B),this.needsUpdate=!1,t&&this.min.copy(t),n&&this.max.copy(n),r&&this.matrix.copy(r)}set(e,t,n){this.min.copy(e),this.max.copy(t),this.matrix.copy(n),this.needsUpdate=!0}copy(e){this.min.copy(e.min),this.max.copy(e.max),this.matrix.copy(e.matrix),this.needsUpdate=!0}};W.prototype.update=(function(){return function(){let e=this.matrix,t=this.min,n=this.max,r=this.points;for(let i=0;i<=1;i++)for(let a=0;a<=1;a++)for(let o=0;o<=1;o++){let s=r[1*i|2*a|4*o];s.x=i?n.x:t.x,s.y=a?n.y:t.y,s.z=o?n.z:t.z,s.applyMatrix4(e)}let i=this.satBounds,a=this.satAxes,o=r[0];for(let e=0;e<3;e++){let t=a[e],n=i[e],s=r[1<new i),r=Array(12).fill().map(()=>new i),a=new e,o=new e;return function(e,i=0,s=null,c=null){if(this.needsUpdate&&this.update(),this.intersectsBox(e))return(s||c)&&(e.getCenter(o),this.closestPointToPoint(o,a),e.closestPointToPoint(a,o),s&&s.copy(a),c&&c.copy(o)),0;let l=i*i,u=e.min,d=e.max,f=this.points,p=1/0;for(let e=0;e<8;e++){let t=f[e];o.copy(t).clamp(u,d);let n=t.distanceToSquared(o);if(nnew U)}},$e=new e,et=new e;function tt(e,t,n={},r=0,i=1/0){let a=r*r,o=i*i,s=1/0,c=null;if(e.shapecast({boundsTraverseOrder:e=>($e.copy(t).clamp(e.min,e.max),$e.distanceToSquared(t)),intersectsBounds:(e,t,n)=>n{e.closestPointToPoint(t,$e);let r=t.distanceToSquared($e);return r=169,rt=parseInt(t)<=161,K=new e,q=new e,J=new e,it=new h,at=new h,ot=new h,st=new e,ct=new e,lt=new e,ut=new e;function dt(e,t,n,r,i,a,o,s){let c;if(c=a===1?e.intersectTriangle(r,n,t,!0,i):e.intersectTriangle(t,n,r,a!==2,i),c===null)return null;let l=e.origin.distanceTo(i);return ls?null:{distance:l,point:i.clone()}}function ft(t,n,r,i,a,o,s,c,l,u,d){K.fromBufferAttribute(n,o),q.fromBufferAttribute(n,s),J.fromBufferAttribute(n,c);let f=dt(t,K,q,J,ut,l,u,d);if(f){if(i){it.fromBufferAttribute(i,o),at.fromBufferAttribute(i,s),ot.fromBufferAttribute(i,c),f.uv=new h;let e=y.getInterpolation(ut,K,q,J,it,at,ot,f.uv);nt||(f.uv=e)}if(a){it.fromBufferAttribute(a,o),at.fromBufferAttribute(a,s),ot.fromBufferAttribute(a,c),f.uv1=new h;let e=y.getInterpolation(ut,K,q,J,it,at,ot,f.uv1);nt||(f.uv1=e),rt&&(f.uv2=f.uv1)}if(r){st.fromBufferAttribute(r,o),ct.fromBufferAttribute(r,s),lt.fromBufferAttribute(r,c),f.normal=new e;let n=y.getInterpolation(ut,K,q,J,st,ct,lt,f.normal);f.normal.dot(t.direction)>0&&f.normal.multiplyScalar(-1),nt||(f.normal=n)}let n={a:o,b:s,c,normal:new e,materialIndex:0};if(y.getNormal(K,q,J,n.normal),f.face=n,f.faceIndex=o,nt){let t=new e;y.getBarycoord(ut,K,q,J,t),f.barycoord=t}}return f}function pt(e){return e&&e.isMaterial?e.side:e}function mt(e,t,n,r,i,a,o){let s=r*3,c=s+0,l=s+1,u=s+2,{index:d,groups:f}=e;e.index&&(c=d.getX(c),l=d.getX(l),u=d.getX(u));let{position:p,normal:m,uv:h,uv1:g}=e.attributes;if(Array.isArray(t)){let e=r*3;for(let s=0,d=f.length;s=d&&ef&&(f=n),op&&(p=o),sm&&(m=s)}return c[e+0]!==a||c[e+1]!==u||c[e+2]!==d||c[e+3]!==f||c[e+4]!==p||c[e+5]!==m?(c[e+0]=a,c[e+1]=u,c[e+2]=d,c[e+3]=f,c[e+4]=p,c[e+5]=m,!0):!1}else{let r=j(e),i=M(e,o),s=a,l=!1,u=!1;if(t){if(!s){let e=r/8+n/32,a=i/8+n/32;l=t.has(e),u=t.has(a),s=!l&&!u}}else l=!0,u=!0;let f=s||l,p=s||u,m=!1;f&&(m=d(r,n,s));let h=!1;p&&(h=d(i,n,s));let g=m||h;if(g)for(let t=0;t<3;t++){let n=r+t,a=i+t,o=c[n],s=c[n+3],l=c[a],u=c[a+3];c[e+t]=ou?s:u}return g}}}function X(e,t,n,r,i){let a,o,s,c,l,u,d=1/n.direction.x,f=1/n.direction.y,p=1/n.direction.z,m=n.origin.x,h=n.origin.y,g=n.origin.z,_=t[e],v=t[e+3],y=t[e+1],b=t[e+3+1],x=t[e+2],S=t[e+3+2];return d>=0?(a=(_-m)*d,o=(v-m)*d):(a=(v-m)*d,o=(_-m)*d),f>=0?(s=(y-h)*f,c=(b-h)*f):(s=(b-h)*f,c=(y-h)*f),a>c||s>o||((s>a||isNaN(a))&&(a=s),(c=0?(l=(x-g)*p,u=(S-g)*p):(l=(S-g)*p,u=(x-g)*p),a>u||l>o)?!1:((l>a||a!==a)&&(a=l),(u=r)}function yt(e,t,n,r,i,a,o,s){let{geometry:c,_indirectBuffer:l}=e;for(let e=r,u=r+i;e=0,d,f;u?(d=j(e),f=M(e,c)):(d=M(e,c),f=j(e));let p=X(d,o,r,i,a)?Et(d,t,n,r,i,a):null;if(p){let e=p.point[l];if(u?e<=o[f+s]:e>=o[f+s+3])return p}let m=X(f,o,r,i,a)?Et(f,t,n,r,i,a):null;return p&&m?p.distance<=m.distance?p:m:p||m||null}}var Dt=new u,Ot=new U,kt=new U,At=new s,jt=new W,Mt=new W;function Nt(e,t,n,r){I.setBuffer(e._roots[t]);let i=Pt(0,e,n,r);return I.clearBuffer(),i}function Pt(e,t,n,r,i=null){let{float32Array:a,uint16Array:o,uint32Array:s}=I,c=e*2;if(i===null&&(n.boundingBox||n.computeBoundingBox(),jt.set(n.boundingBox.min,n.boundingBox.max,r),i=jt),O(c,o)){let i=t.geometry,l=i.index,u=i.attributes.position,d=n.index,f=n.attributes.position,p=k(e,s),m=A(c,o);if(At.copy(r).invert(),n.boundsTree)return T(N(e),a,Mt),Mt.matrix.copy(At),Mt.needsUpdate=!0,n.boundsTree.shapecast({intersectsBounds:e=>Mt.intersectsBox(e),intersectsTriangle:e=>{e.a.applyMatrix4(r),e.b.applyMatrix4(r),e.c.applyMatrix4(r),e.needsUpdate=!0;for(let t=p*3,n=(m+p)*3;tIt.distanceToBox(e),intersectsBounds:(e,t,n)=>n{if(t.boundsTree)return t.boundsTree.shapecast({boundsTraverseOrder:e=>Lt.distanceToBox(e),intersectsBounds:(e,t,n)=>n{for(let o=t,s=t+i;op&&(p=a),om&&(m=o),sh&&(h=s)}}return c[n+0]!==l||c[n+1]!==d||c[n+2]!==f||c[n+3]!==p||c[n+4]!==m||c[n+5]!==h?(c[n+0]=l,c[n+1]=d,c[n+2]=f,c[n+3]=p,c[n+4]=m,c[n+5]=h,!0):!1}else{let e=j(n),r=M(n,o),i=l,s=!1,u=!1;if(t){if(!i){let n=e/8+a/32,o=r/8+a/32;s=t.has(n),u=t.has(o),i=!s&&!u}}else s=!0,u=!0;let f=i||s,p=i||u,m=!1;f&&(m=d(e,a,i));let h=!1;p&&(h=d(r,a,i));let g=m||h;if(g)for(let t=0;t<3;t++){let i=e+t,a=r+t,o=c[i],s=c[i+3],l=c[a],u=c[a+3];c[n+t]=ou?s:u}return g}}}function Wt(e,t,n,r,i,a,o){I.setBuffer(e._roots[t]),Gt(0,e,n,r,i,a,o),I.clearBuffer()}function Gt(e,t,n,r,i,a,o){let{float32Array:s,uint16Array:c,uint32Array:l}=I,u=e*2;if(O(u,c))yt(t,n,r,k(e,l),A(u,c),i,a,o);else{let c=j(e);X(c,s,r,a,o)&&Gt(c,t,n,r,i,a,o);let u=M(e,l);X(u,s,r,a,o)&&Gt(u,t,n,r,i,a,o)}}var Kt=[`x`,`y`,`z`];function qt(e,t,n,r,i,a){I.setBuffer(e._roots[t]);let o=Jt(0,e,n,r,i,a);return I.clearBuffer(),o}function Jt(e,t,n,r,i,a){let{float32Array:o,uint16Array:s,uint32Array:c}=I,l=e*2;if(O(l,s))return bt(t,n,r,k(e,c),A(l,s),i,a);{let s=re(e,c),l=Kt[s],u=r.direction[l]>=0,d,f;u?(d=j(e),f=M(e,c)):(d=M(e,c),f=j(e));let p=X(d,o,r,i,a)?Jt(d,t,n,r,i,a):null;if(p){let e=p.point[l];if(u?e<=o[f+s]:e>=o[f+s+3])return p}let m=X(f,o,r,i,a)?Jt(f,t,n,r,i,a):null;return p&&m?p.distance<=m.distance?p:m:p||m||null}}var Yt=new u,Xt=new U,Zt=new U,Qt=new s,$t=new W,en=new W;function tn(e,t,n,r){I.setBuffer(e._roots[t]);let i=nn(0,e,n,r);return I.clearBuffer(),i}function nn(e,t,n,r,i=null){let{float32Array:a,uint16Array:o,uint32Array:s}=I,c=e*2;if(i===null&&(n.boundingBox||n.computeBoundingBox(),$t.set(n.boundingBox.min,n.boundingBox.max,r),i=$t),O(c,o)){let i=t.geometry,l=i.index,u=i.attributes.position,d=n.index,f=n.attributes.position,p=k(e,s),m=A(c,o);if(Qt.copy(r).invert(),n.boundsTree)return T(N(e),a,en),en.matrix.copy(Qt),en.needsUpdate=!0,n.boundsTree.shapecast({intersectsBounds:e=>en.intersectsBox(e),intersectsTriangle:e=>{e.a.applyMatrix4(r),e.b.applyMatrix4(r),e.c.applyMatrix4(r),e.needsUpdate=!0;for(let n=p,r=m+p;nan.distanceToBox(e),intersectsBounds:(e,t,n)=>n{if(t.boundsTree){let s=t.boundsTree;return s.shapecast({boundsTraverseOrder:e=>on.distanceToBox(e),intersectsBounds:(e,t,n)=>n{for(let x=t,S=t+o;xe.slice()),o.index=a?a.array.slice():null,o.indirectBuffer=i?i.slice():null):(o.roots=r,o.index=a?a.array:null,o.indirectBuffer=i),o}static deserialize(t,n,r={}){r={setIndex:!0,indirect:!!t.indirectBuffer,...r};let{index:i,roots:a,indirectBuffer:o}=t;t.version||(console.warn(`MeshBVH.deserialize: Serialization format has been changed and will be fixed up. It is recommended to regenerate any stored serialized data.`),c(a));let s=new e(n,{...r,[C]:!0});if(s._roots=a,s._indirectBuffer=o||null,r.setIndex){let e=n.getIndex();if(e===null){let e=new v(t.index,1,!1);n.setIndex(e)}else e.array!==i&&(e.array.set(i),e.needsUpdate=!0)}return s;function c(e){for(let t=0;td&&(d=i),o>d&&(d=o),t[n+e]=s,t[n+e+3]=d}return t}computePrimitiveBounds(e,t,n){let r=this.geometry,i=this._indirectBuffer,a=r.attributes.position,o=r.index?r.index.array:null,s=a.normalized;if(e<0||t+e-n.offset>n.length/6)throw Error(`MeshBVH: compute triangle bounds range is invalid.`);let c=a.array,l=a.offset||0,u=3;a.isInterleavedBufferAttribute&&(u=a.data.stride);let d=[`getX`,`getY`,`getZ`],f=n.offset;for(let r=e,p=e+t;ru&&(u=i),o>u&&(u=o);let f=(u-l)/2,g=e*2;n[t+g+0]=l+f,n[t+g+1]=f+(Math.abs(l)+f)*S}}return n}raycastObject3D(e,t,n=[]){let{material:r}=e;if(r===void 0)return;gn.copy(e.matrixWorld).invert(),mn.copy(t.ray).applyMatrix4(gn),_n.setFromMatrixScale(e.matrixWorld),hn.copy(mn.direction).multiply(_n);let i=hn.length(),a=t.near/i,o=t.far/i;if(t.firstHitOnly===!0){let i=this.raycastFirst(mn,r,a,o);i=fn(i,e,t),i&&n.push(i)}else{let i=this.raycast(mn,r,a,o);for(let r=0,a=i.length;r{Y(o,this.resolveTriangleIndex(e)*3,s,c)}:e=>{Y(o,e*3,s,c)},u=G.getPrimitive(),d=t.geometry.index,f=t.geometry.attributes.position,p=t.indirect?e=>{Y(u,t.resolveTriangleIndex(e)*3,d,f)}:e=>{Y(u,e*3,d,f)};if(a){if(!(t instanceof e))throw Error(`MeshBVH: "intersectsTriangles" callback can only be used with another MeshBVH.`);let r=(e,t,r,i,s,c,d,f)=>{for(let m=r,h=r+i;mpn.intersectsBox(e),intersectsTriangle:e=>pn.intersectsTriangle(e)})}intersectsSphere(e){return this.shapecast({intersectsBounds:t=>e.intersectsBox(t),intersectsTriangle:t=>t.intersectsSphere(e)})}closestPointToGeometry(e,t,n={},r={},i=0,a=1/0){return(this.indirect?dn:Ht)(this,e,t,n,r,i,a)}closestPointToPoint(e,t={},n=0,r=1/0){return tt(this,e,t,n,r)}},bn={Mesh:l.prototype.raycast,Line:m.prototype.raycast,LineSegments:n.prototype.raycast,LineLoop:a.prototype.raycast,Points:f.prototype.raycast,BatchedMesh:_.prototype.raycast},Z=new l,xn=[];function Sn(e,t){if(this.isBatchedMesh)Cn.call(this,e,t);else{let{geometry:r}=this;if(r.boundsTree)r.boundsTree.raycastObject3D(this,e,t);else{let r;if(this instanceof l)r=bn.Mesh;else if(this instanceof n)r=bn.LineSegments;else if(this instanceof a)r=bn.LineLoop;else if(this instanceof m)r=bn.Line;else if(this instanceof f)r=bn.Points;else throw Error(`BVH: Fallback raycast function not found.`);r.call(this,e,t)}}}function Cn(e,t){if(this.boundsTrees){let n=this.boundsTrees,r=this._drawInfo||this._instanceInfo,i=this._drawRanges||this._geometryInfo,a=this.matrixWorld;Z.material=this.material,Z.geometry=this.geometry;let o=Z.geometry.boundsTree,s=Z.geometry.drawRange;Z.geometry.boundingSphere===null&&(Z.geometry.boundingSphere=new g);for(let o=0,s=r.length;o=2&&t.setY(a,e.getY(i)),r>=3&&t.setZ(a,e.getZ(i)),r>=4&&t.setW(a,e.getW(i))}}else{let r=t.array,i=r.constructor,a=r.BYTES_PER_ELEMENT*e.itemSize*n;new i(r.buffer,a,e.array.length).set(e.array)}}function In(e,t,n){let r=e.elements,i=t.elements;for(let e=0,t=i.length;e{e.traverseVisible(e=>{e.isMesh&&t.push(e)})}),this.meshes=t,this.useGroups=!0,this.applyWorldTransforms=!0,this.attributes=[`position`,`normal`,`color`,`tangent`,`uv`,`uv2`],this._intermediateGeometry=Array(t.length).fill().map(()=>new o),this._diffMap=new WeakMap}getMaterials(){let e=[];return this.meshes.forEach(t=>{Array.isArray(t.material)?e.push(...t.material):e.push(t.material)}),e}generate(e=new o){let t=[],{meshes:n,useGroups:r,_intermediateGeometry:i,_diffMap:a}=this;for(let e=0,r=n.length;e{throw e.message?Error(`${this.name}: Could not create Web Worker with error "${e.message}"`):Error(`${this.name}: Could not create Web Worker.`)}}runTask(){}generate(...e){if(this.running)throw Error(`GenerateMeshBVHWorker: Already running job.`);if(this.worker===null)throw Error(`GenerateMeshBVHWorker: Worker has been disposed.`);this.running=!0;let t=this.runTask(this.worker,...e);return t.finally(()=>{this.running=!1}),t}dispose(){this.worker.terminate(),this.worker=null}};export{yn as a,qe as c,C as d,wn as i,Ke as l,Un as n,W as o,Sn as r,U as s,Wn as t,Le as u};