@konfigure/sdk 0.2.0 → 0.3.0

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.
@@ -0,0 +1,482 @@
1
+ import {a,b}from'./chunk-XUSV3KOD.js';import {Vector3,Vector2,Plane,Line3,Triangle,Matrix4,Box3,Ray,BufferAttribute,FrontSide,BatchedMesh,Points,LineLoop,LineSegments,Line,Mesh,DataTexture,NearestFilter,UnsignedIntType,IntType,FloatType,RGBAFormat,RGIntegerFormat,BackSide,DoubleSide,Sphere,RGFormat,RedFormat,RGBAIntegerFormat,RedIntegerFormat,UnsignedByteType,UnsignedShortType,ByteType,ShortType,REVISION}from'three';var yt=Math.pow(2,-24),Dt=Symbol("SKIP_GENERATION"),Gt={strategy:0,maxDepth:40,maxLeafSize:10,useSharedArrayBuffer:false,setBoundingBox:true,onProgress:null,indirect:false,verbose:true,range:null,[Dt]:false};var K=class{static{a(this,"SeparatingAxisBounds");}constructor(){this.min=1/0,this.max=-1/0;}setFromPointsField(t,e){let n=1/0,o=-1/0;for(let c=0,s=t.length;c<s;c++){let r=t[c][e];n=r<n?r:n,o=r>o?r:o;}this.min=n,this.max=o;}setFromPoints(t,e){let n=1/0,o=-1/0;for(let c=0,s=e.length;c<s;c++){let a=e[c],r=t.dot(a);n=r<n?r:n,o=r>o?r:o;}this.min=n,this.max=o;}isSeparated(t){return this.min>t.max||t.min>this.max}};K.prototype.setFromBox=(function(){let i=new Vector3;return a(function(e,n){let o=n.min,c=n.max,s=1/0,a=-1/0;for(let r=0;r<=1;r++)for(let p=0;p<=1;p++)for(let u=0;u<=1;u++){i.x=o.x*r+c.x*(1-r),i.y=o.y*p+c.y*(1-p),i.z=o.z*u+c.z*(1-u);let f=e.dot(i);s=Math.min(f,s),a=Math.max(f,a);}this.min=s,this.max=a;},"setFromBox")})();var Bi=(function(){let i=new Vector3,t=new Vector3,e=new Vector3;return a(function(o,c,s){let a=o.start,r=i,p=c.start,u=t;e.subVectors(a,p),i.subVectors(o.end,o.start),t.subVectors(c.end,c.start);let f=e.dot(u),l=u.dot(r),d=u.dot(u),b=e.dot(r),v=r.dot(r)*d-l*l,T,m;v!==0?T=(f*l-b*d)/v:T=0,m=(f+T*l)/d,s.x=T,s.y=m;},"closestPointLineToLine")})(),Nt=(function(){let i=new Vector2,t=new Vector3,e=new Vector3;return a(function(o,c,s,a){Bi(o,c,i);let r=i.x,p=i.y;if(r>=0&&r<=1&&p>=0&&p<=1){o.at(r,s),c.at(p,a);return}else if(r>=0&&r<=1){p<0?c.at(0,a):c.at(1,a),o.closestPointToPoint(a,true,s);return}else if(p>=0&&p<=1){r<0?o.at(0,s):o.at(1,s),c.closestPointToPoint(s,true,a);return}else {let u;r<0?u=o.start:u=o.end;let f;p<0?f=c.start:f=c.end;let l=t,d=e;if(o.closestPointToPoint(f,true,t),c.closestPointToPoint(u,true,e),l.distanceToSquared(f)<=d.distanceToSquared(u)){s.copy(l),a.copy(f);return}else {s.copy(u),a.copy(d);return}}},"closestPointsSegmentToSegment")})(),rn=(function(){let i=new Vector3,t=new Vector3,e=new Plane,n=new Line3;return a(function(c,s){let{radius:a,center:r}=c,{a:p,b:u,c:f}=s;if(n.start=p,n.end=u,n.closestPointToPoint(r,true,i).distanceTo(r)<=a||(n.start=p,n.end=f,n.closestPointToPoint(r,true,i).distanceTo(r)<=a)||(n.start=u,n.end=f,n.closestPointToPoint(r,true,i).distanceTo(r)<=a))return true;let A=s.getPlane(e);if(Math.abs(A.distanceToPoint(r))<=a){let T=A.projectPoint(r,t);if(s.containsPoint(T))return true}return false},"sphereIntersectTriangle")})();var Ii=["x","y","z"],et=1e-15,an=et*et;function Z(i){return Math.abs(i)<et}a(Z,"isNearZero");var X=class extends Triangle{static{a(this,"ExtendedTriangle");}constructor(...t){super(...t),this.isExtendedTriangle=true,this.satAxes=new Array(4).fill().map(()=>new Vector3),this.satBounds=new Array(4).fill().map(()=>new K),this.points=[this.a,this.b,this.c],this.plane=new Plane,this.isDegenerateIntoSegment=false,this.isDegenerateIntoPoint=false,this.degenerateSegment=new Line3,this.needsUpdate=true;}intersectsSphere(t){return rn(t,this)}update(){let t=this.a,e=this.b,n=this.c,o=this.points,c=this.satAxes,s=this.satBounds,a=c[0],r=s[0];this.getNormal(a),r.setFromPoints(a,o);let p=c[1],u=s[1];p.subVectors(t,e),u.setFromPoints(p,o);let f=c[2],l=s[2];f.subVectors(e,n),l.setFromPoints(f,o);let d=c[3],b=s[3];d.subVectors(n,t),b.setFromPoints(d,o);let A=p.length(),v=f.length(),T=d.length();this.isDegenerateIntoPoint=false,this.isDegenerateIntoSegment=false,A<et?v<et||T<et?this.isDegenerateIntoPoint=true:(this.isDegenerateIntoSegment=true,this.degenerateSegment.start.copy(t),this.degenerateSegment.end.copy(n)):v<et?T<et?this.isDegenerateIntoPoint=true:(this.isDegenerateIntoSegment=true,this.degenerateSegment.start.copy(e),this.degenerateSegment.end.copy(t)):T<et&&(this.isDegenerateIntoSegment=true,this.degenerateSegment.start.copy(n),this.degenerateSegment.end.copy(e)),this.plane.setFromNormalAndCoplanarPoint(a,t),this.needsUpdate=false;}};X.prototype.closestPointToSegment=(function(){let i=new Vector3,t=new Vector3,e=new Line3;return a(function(o,c=null,s=null){let{start:a,end:r}=o,p=this.points,u,f=1/0;for(let l=0;l<3;l++){let d=(l+1)%3;e.start.copy(p[l]),e.end.copy(p[d]),Nt(e,o,i,t),u=i.distanceToSquared(t),u<f&&(f=u,c&&c.copy(i),s&&s.copy(t));}return this.closestPointToPoint(a,i),u=a.distanceToSquared(i),u<f&&(f=u,c&&c.copy(i),s&&s.copy(a)),this.closestPointToPoint(r,i),u=r.distanceToSquared(i),u<f&&(f=u,c&&c.copy(i),s&&s.copy(r)),Math.sqrt(f)},"distanceToSegment")})();X.prototype.intersectsTriangle=(function(){let i=new X,t=new K,e=new K,n=new Vector3,o=new Vector3,c=new Vector3,s=new Vector3,a$1=new Line3,r=new Line3,p=new Vector3,u=new Vector2,f=new Vector2;function l(g,x,h,B){let w=n;!g.isDegenerateIntoPoint&&!g.isDegenerateIntoSegment?w.copy(g.plane.normal):w.copy(x.plane.normal);let S=g.satBounds,P=g.satAxes;for(let I=1;I<4;I++){let D=S[I],N=P[I];if(t.setFromPoints(N,x.points),D.isSeparated(t)||(s.copy(w).cross(N),t.setFromPoints(s,g.points),e.setFromPoints(s,x.points),t.isSeparated(e)))return false}let _=x.satBounds,E=x.satAxes;for(let I=1;I<4;I++){let D=_[I],N=E[I];if(t.setFromPoints(N,g.points),D.isSeparated(t)||(s.crossVectors(w,N),t.setFromPoints(s,g.points),e.setFromPoints(s,x.points),t.isSeparated(e)))return false}return h&&(B||console.warn("ExtendedTriangle.intersectsTriangle: Triangles are coplanar which does not support an output edge. Setting edge to 0, 0, 0."),h.start.set(0,0,0),h.end.set(0,0,0)),true}a(l,"coplanarIntersectsTriangle");function d(g,x,h,B,w,S,P,_,E,I,D){let N=P/(P-_);I.x=B+(w-B)*N,D.start.subVectors(x,g).multiplyScalar(N).add(g),N=P/(P-E),I.y=B+(S-B)*N,D.end.subVectors(h,g).multiplyScalar(N).add(g);}a(d,"findSingleBounds");function b(g,x,h,B,w,S,P,_,E,I,D){if(w>0)d(g.c,g.a,g.b,B,x,h,E,P,_,I,D);else if(S>0)d(g.b,g.a,g.c,h,x,B,_,P,E,I,D);else if(_*E>0||P!=0)d(g.a,g.b,g.c,x,h,B,P,_,E,I,D);else if(_!=0)d(g.b,g.a,g.c,h,x,B,_,P,E,I,D);else if(E!=0)d(g.c,g.a,g.b,B,x,h,E,P,_,I,D);else return true;return false}a(b,"findIntersectionLineBounds");function A(g,x,h,B){let w=x.degenerateSegment,S=g.plane.distanceToPoint(w.start),P=g.plane.distanceToPoint(w.end);return Z(S)?Z(P)?l(g,x,h,B):(h&&(h.start.copy(w.start),h.end.copy(w.start)),g.containsPoint(w.start)):Z(P)?(h&&(h.start.copy(w.end),h.end.copy(w.end)),g.containsPoint(w.end)):g.plane.intersectLine(w,n)!=null?(h&&(h.start.copy(n),h.end.copy(n)),g.containsPoint(n)):false}a(A,"intersectTriangleSegment");function v(g,x,h){let B=x.a;return Z(g.plane.distanceToPoint(B))&&g.containsPoint(B)?(h&&(h.start.copy(B),h.end.copy(B)),true):false}a(v,"intersectTrianglePoint");function T(g,x,h){let B=g.degenerateSegment,w=x.a;return B.closestPointToPoint(w,true,n),w.distanceToSquared(n)<an?(h&&(h.start.copy(w),h.end.copy(w)),true):false}a(T,"intersectSegmentPoint");function m(g,x,h,B){if(g.isDegenerateIntoSegment)if(x.isDegenerateIntoSegment){let w=g.degenerateSegment,S=x.degenerateSegment,P=o,_=c;w.delta(P),S.delta(_);let E=n.subVectors(S.start,w.start),I=P.x*_.y-P.y*_.x;if(Z(I))return false;let D=(E.x*_.y-E.y*_.x)/I,N=-(P.x*E.y-P.y*E.x)/I;if(D<0||D>1||N<0||N>1)return false;let F=w.start.z+P.z*D,L=S.start.z+_.z*N;return Z(F-L)?(h&&(h.start.copy(w.start).addScaledVector(P,D),h.end.copy(w.start).addScaledVector(P,D)),true):false}else return x.isDegenerateIntoPoint?T(g,x,h):A(x,g,h,B);else {if(g.isDegenerateIntoPoint)return x.isDegenerateIntoPoint?x.a.distanceToSquared(g.a)<an?(h&&(h.start.copy(g.a),h.end.copy(g.a)),true):false:x.isDegenerateIntoSegment?T(x,g,h):v(x,g,h);if(x.isDegenerateIntoPoint)return v(g,x,h);if(x.isDegenerateIntoSegment)return A(g,x,h,B)}}return a(m,"handleDegenerateCases"),a(function(x,h=null,B=false){this.needsUpdate&&this.update(),x.isExtendedTriangle?x.needsUpdate&&x.update():(i.copy(x),i.update(),x=i);let w=m(this,x,h,B);if(w!==void 0)return w;let S=this.plane,P=x.plane,_=P.distanceToPoint(this.a),E=P.distanceToPoint(this.b),I=P.distanceToPoint(this.c);Z(_)&&(_=0),Z(E)&&(E=0),Z(I)&&(I=0);let D=_*E,N=_*I;if(D>0&&N>0)return false;let F=S.distanceToPoint(x.a),L=S.distanceToPoint(x.b),j=S.distanceToPoint(x.c);Z(F)&&(F=0),Z(L)&&(L=0),Z(j)&&(j=0);let nn=F*L,on=F*j;if(nn>0&&on>0)return false;o.copy(S.normal),c.copy(P.normal);let ve=o.cross(c),Ae=0,we=Math.abs(ve.x),sn=Math.abs(ve.y);sn>we&&(we=sn,Ae=1),Math.abs(ve.z)>we&&(Ae=2);let mt=Ii[Ae],di=this.a[mt],mi=this.b[mt],yi=this.c[mt],hi=x.a[mt],xi=x.b[mt],gi=x.c[mt];if(b(this,di,mi,yi,D,N,_,E,I,u,a$1))return l(this,x,h,B);if(b(x,hi,xi,gi,nn,on,F,L,j,f,r))return l(this,x,h,B);if(u.y<u.x){let Be=u.y;u.y=u.x,u.x=Be,p.copy(a$1.start),a$1.start.copy(a$1.end),a$1.end.copy(p);}if(f.y<f.x){let Be=f.y;f.y=f.x,f.x=Be,p.copy(r.start),r.start.copy(r.end),r.end.copy(p);}return u.y<f.x||f.y<u.x?false:(h&&(f.x>u.x?h.start.copy(r.start):h.start.copy(a$1.start),f.y<u.y?h.end.copy(r.end):h.end.copy(a$1.end)),true)},"intersectsTriangle")})();X.prototype.distanceToPoint=(function(){let i=new Vector3;return a(function(e){return this.closestPointToPoint(e,i),e.distanceTo(i)},"distanceToPoint")})();X.prototype.distanceToTriangle=(function(){let i=new Vector3,t=new Vector3,e=["a","b","c"],n=new Line3,o=new Line3;return a(function(s,a=null,r=null){let p=a||r?n:null;if(this.intersectsTriangle(s,p))return (a||r)&&(a&&p.getCenter(a),r&&p.getCenter(r)),0;let u=1/0;for(let f=0;f<3;f++){let l,d=e[f],b=s[d];this.closestPointToPoint(b,i),l=b.distanceToSquared(i),l<u&&(u=l,a&&a.copy(i),r&&r.copy(b));let A=this[d];s.closestPointToPoint(A,i),l=A.distanceToSquared(i),l<u&&(u=l,a&&a.copy(A),r&&r.copy(i));}for(let f=0;f<3;f++){let l=e[f],d=e[(f+1)%3];n.set(this[l],this[d]);for(let b=0;b<3;b++){let A=e[b],v=e[(b+1)%3];o.set(s[A],s[v]),Nt(n,o,i,t);let T=i.distanceToSquared(t);T<u&&(u=T,a&&a.copy(i),r&&r.copy(t));}}return Math.sqrt(u)},"distanceToTriangle")})();var k=class{static{a(this,"OrientedBox");}constructor(t,e,n){this.isOrientedBox=true,this.min=new Vector3,this.max=new Vector3,this.matrix=new Matrix4,this.invMatrix=new Matrix4,this.points=new Array(8).fill().map(()=>new Vector3),this.satAxes=new Array(3).fill().map(()=>new Vector3),this.satBounds=new Array(3).fill().map(()=>new K),this.alignedSatBounds=new Array(3).fill().map(()=>new K),this.needsUpdate=false,t&&this.min.copy(t),e&&this.max.copy(e),n&&this.matrix.copy(n);}set(t,e,n){this.min.copy(t),this.max.copy(e),this.matrix.copy(n),this.needsUpdate=true;}copy(t){this.min.copy(t.min),this.max.copy(t.max),this.matrix.copy(t.matrix),this.needsUpdate=true;}};k.prototype.update=(function(){return a(function(){let t=this.matrix,e=this.min,n=this.max,o=this.points;for(let p=0;p<=1;p++)for(let u=0;u<=1;u++)for(let f=0;f<=1;f++){let l=1*p|2*u|4*f,d=o[l];d.x=p?n.x:e.x,d.y=u?n.y:e.y,d.z=f?n.z:e.z,d.applyMatrix4(t);}let c=this.satBounds,s=this.satAxes,a=o[0];for(let p=0;p<3;p++){let u=s[p],f=c[p],l=1<<p,d=o[l];u.subVectors(a,d),f.setFromPoints(u,o);}let r=this.alignedSatBounds;r[0].setFromPointsField(o,"x"),r[1].setFromPointsField(o,"y"),r[2].setFromPointsField(o,"z"),this.invMatrix.copy(this.matrix).invert(),this.needsUpdate=false;},"update")})();k.prototype.intersectsBox=(function(){let i=new K;return a(function(e){this.needsUpdate&&this.update();let n=e.min,o=e.max,c=this.satBounds,s=this.satAxes,a=this.alignedSatBounds;if(i.min=n.x,i.max=o.x,a[0].isSeparated(i)||(i.min=n.y,i.max=o.y,a[1].isSeparated(i))||(i.min=n.z,i.max=o.z,a[2].isSeparated(i)))return false;for(let r=0;r<3;r++){let p=s[r],u=c[r];if(i.setFromBox(p,e),u.isSeparated(i))return false}return true},"intersectsBox")})();k.prototype.intersectsTriangle=(function(){let i=new X,t=new Array(3),e=new K,n=new K,o=new Vector3;return a(function(s){this.needsUpdate&&this.update(),s.isExtendedTriangle?s.needsUpdate&&s.update():(i.copy(s),i.update(),s=i);let a=this.satBounds,r=this.satAxes;t[0]=s.a,t[1]=s.b,t[2]=s.c;for(let l=0;l<3;l++){let d=a[l],b=r[l];if(e.setFromPoints(b,t),d.isSeparated(e))return false}let p=s.satBounds,u=s.satAxes,f=this.points;for(let l=0;l<3;l++){let d=p[l],b=u[l];if(e.setFromPoints(b,f),d.isSeparated(e))return false}for(let l=0;l<3;l++){let d=r[l];for(let b=0;b<4;b++){let A=u[b];if(o.crossVectors(d,A),e.setFromPoints(o,t),n.setFromPoints(o,f),e.isSeparated(n))return false}}return true},"intersectsTriangle")})();k.prototype.closestPointToPoint=(function(){return a(function(t,e){return this.needsUpdate&&this.update(),e.copy(t).applyMatrix4(this.invMatrix).clamp(this.min,this.max).applyMatrix4(this.matrix),e},"closestPointToPoint")})();k.prototype.distanceToPoint=(function(){let i=new Vector3;return a(function(e){return this.closestPointToPoint(e,i),e.distanceTo(i)},"distanceToPoint")})();k.prototype.distanceToBox=(function(){let i=["x","y","z"],t=new Array(12).fill().map(()=>new Line3),e=new Array(12).fill().map(()=>new Line3),n=new Vector3,o=new Vector3;return a(function(s,a=0,r=null,p=null){if(this.needsUpdate&&this.update(),this.intersectsBox(s))return (r||p)&&(s.getCenter(o),this.closestPointToPoint(o,n),s.closestPointToPoint(n,o),r&&r.copy(n),p&&p.copy(o)),0;let u=a*a,f=s.min,l=s.max,d=this.points,b=1/0;for(let v=0;v<8;v++){let T=d[v];o.copy(T).clamp(f,l);let m=T.distanceToSquared(o);if(m<b&&(b=m,r&&r.copy(T),p&&p.copy(o),m<u))return Math.sqrt(m)}let A=0;for(let v=0;v<3;v++)for(let T=0;T<=1;T++)for(let m=0;m<=1;m++){let g=(v+1)%3,x=(v+2)%3,h=T<<g|m<<x,B=1<<v|T<<g|m<<x,w=d[h],S=d[B];t[A].set(w,S);let _=i[v],E=i[g],I=i[x],D=e[A],N=D.start,F=D.end;N[_]=f[_],N[E]=T?f[E]:l[E],N[I]=m?f[I]:l[E],F[_]=l[_],F[E]=T?f[E]:l[E],F[I]=m?f[I]:l[E],A++;}for(let v=0;v<=1;v++)for(let T=0;T<=1;T++)for(let m=0;m<=1;m++){o.x=v?l.x:f.x,o.y=T?l.y:f.y,o.z=m?l.z:f.z,this.closestPointToPoint(o,n);let g=o.distanceToSquared(n);if(g<b&&(b=g,r&&r.copy(n),p&&p.copy(o),g<u))return Math.sqrt(g)}for(let v=0;v<12;v++){let T=t[v];for(let m=0;m<12;m++){let g=e[m];Nt(T,g,n,o);let x=n.distanceToSquared(o);if(x<b&&(b=x,r&&r.copy(n),p&&p.copy(o),x<u))return Math.sqrt(x)}}return Math.sqrt(b)},"distanceToBox")})();var ot=class{static{a(this,"PrimitivePool");}constructor(t){this._getNewPrimitive=t,this._primitives=[];}getPrimitive(){let t=this._primitives;return t.length===0?this._getNewPrimitive():t.pop()}releasePrimitive(t){this._primitives.push(t);}};var _e=class extends ot{static{a(this,"ExtendedTrianglePoolBase");}constructor(){super(()=>new X);}},W=new _e;var Ft=new Vector3,Se=new Vector3;function pn(i,t,e={},n=0,o=1/0){let c=n*n,s=o*o,a$1=1/0,r=null;if(i.shapecast({boundsTraverseOrder:a(u=>(Ft.copy(t).clamp(u.min,u.max),Ft.distanceToSquared(t)),"boundsTraverseOrder"),intersectsBounds:a((u,f,l)=>l<a$1&&l<s,"intersectsBounds"),intersectsTriangle:a((u,f)=>{u.closestPointToPoint(t,Ft);let l=t.distanceToSquared(Ft);return l<a$1&&(Se.copy(Ft),a$1=l,r=f),l<c},"intersectsTriangle")}),a$1===1/0)return null;let p=Math.sqrt(a$1);return e.point?e.point.copy(Se):e.point=Se.clone(),e.distance=p,e.faceIndex=r,e}a(pn,"closestPointToPoint");function M(i,t){return t[i+15]===65535}a(M,"IS_LEAF");function C(i,t){return t[i+6]}a(C,"OFFSET");function U(i,t){return t[i+14]}a(U,"COUNT");function O(i){return i+8}a(O,"LEFT_NODE");function z(i,t){let e=t[i+6];return i+e*8}a(z,"RIGHT_NODE");function st(i,t){return t[i+7]}a(st,"SPLIT_AXIS");var Xt=parseInt(REVISION)>=169,Ni=parseInt(REVISION)<=161,ut=new Vector3,pt=new Vector3,dt=new Vector3,Yt=new Vector2,Wt=new Vector2,Kt=new Vector2,dn=new Vector3,mn=new Vector3,yn=new Vector3,Rt=new Vector3;function Fi(i,t,e,n,o,c,s,a){let r;if(c===BackSide?r=i.intersectTriangle(n,e,t,true,o):r=i.intersectTriangle(t,e,n,c!==DoubleSide,o),r===null)return null;let p=i.origin.distanceTo(o);return p<s||p>a?null:{distance:p,point:o.clone()}}a(Fi,"checkIntersection");function hn(i,t,e,n,o,c,s,a,r,p,u){ut.fromBufferAttribute(t,c),pt.fromBufferAttribute(t,s),dt.fromBufferAttribute(t,a);let f=Fi(i,ut,pt,dt,Rt,r,p,u);if(f){if(n){Yt.fromBufferAttribute(n,c),Wt.fromBufferAttribute(n,s),Kt.fromBufferAttribute(n,a),f.uv=new Vector2;let d=Triangle.getInterpolation(Rt,ut,pt,dt,Yt,Wt,Kt,f.uv);Xt||(f.uv=d);}if(o){Yt.fromBufferAttribute(o,c),Wt.fromBufferAttribute(o,s),Kt.fromBufferAttribute(o,a),f.uv1=new Vector2;let d=Triangle.getInterpolation(Rt,ut,pt,dt,Yt,Wt,Kt,f.uv1);Xt||(f.uv1=d),Ni&&(f.uv2=f.uv1);}if(e){dn.fromBufferAttribute(e,c),mn.fromBufferAttribute(e,s),yn.fromBufferAttribute(e,a),f.normal=new Vector3;let d=Triangle.getInterpolation(Rt,ut,pt,dt,dn,mn,yn,f.normal);f.normal.dot(i.direction)>0&&f.normal.multiplyScalar(-1),Xt||(f.normal=d);}let l={a:c,b:s,c:a,normal:new Vector3,materialIndex:0};if(Triangle.getNormal(ut,pt,dt,l.normal),f.face=l,f.faceIndex=c,Xt){let d=new Vector3;Triangle.getBarycoord(Rt,ut,pt,dt,d),f.barycoord=d;}}return f}a(hn,"checkBufferGeometryIntersection");function xn(i){return i&&i.isMaterial?i.side:i}a(xn,"getSide");function xt(i,t,e,n,o,c,s){let a=n*3,r=a+0,p=a+1,u=a+2,{index:f,groups:l}=i;i.index&&(r=f.getX(r),p=f.getX(p),u=f.getX(u));let{position:d,normal:b,uv:A,uv1:v}=i.attributes;if(Array.isArray(t)){let T=n*3;for(let m=0,g=l.length;m<g;m++){let{start:x,count:h,materialIndex:B}=l[m];if(T>=x&&T<x+h){let w=xn(t[B]),S=hn(e,d,b,A,v,r,p,u,w,c,s);if(S)if(S.faceIndex=n,S.face.materialIndex=B,o)o.push(S);else return S}}}else {let T=xn(t),m=hn(e,d,b,A,v,r,p,u,T,c,s);if(m)if(m.faceIndex=n,m.face.materialIndex=0,o)o.push(m);else return m}return null}a(xt,"intersectTri");function V(i,t,e,n){let o=i.a,c=i.b,s=i.c,a=t,r=t+1,p=t+2;e&&(a=e.getX(a),r=e.getX(r),p=e.getX(p)),o.x=n.getX(a),o.y=n.getY(a),o.z=n.getZ(a),c.x=n.getX(r),c.y=n.getY(r),c.z=n.getZ(r),s.x=n.getX(p),s.y=n.getY(p),s.z=n.getZ(p);}a(V,"setTriangle");function Tn(i,t,e,n,o,c,s,a){let{geometry:r,_indirectBuffer:p}=i;for(let u=n,f=n+o;u<f;u++)xt(r,t,e,u,c,s,a);}a(Tn,"intersectTris");function bn(i,t,e,n,o,c,s){let{geometry:a,_indirectBuffer:r}=i,p=1/0,u=null;for(let f=n,l=n+o;f<l;f++){let d;d=xt(a,t,e,f,null,c,s),d&&d.distance<p&&(u=d,p=d.distance);}return u}a(bn,"intersectClosestTri");function vn(i,t,e,n,o,c,s){let{geometry:a}=e,{index:r}=a,p=a.attributes.position;for(let u=i,f=t+i;u<f;u++){let l;if(l=u,V(s,l*3,r,p),s.needsUpdate=true,n(s,l,o,c))return true}return false}a(vn,"iterateOverTriangles");function An(i,t=null){t&&Array.isArray(t)&&(t=new Set(t));let e=i.geometry,n=e.index?e.index.array:null,o=e.attributes.position,c,s,a$1,r,p=0,u=i._roots;for(let l=0,d=u.length;l<d;l++)c=u[l],s=new Uint32Array(c),a$1=new Uint16Array(c),r=new Float32Array(c),f(0,p),p+=c.byteLength;function f(l,d,b=false){let A=l*2;if(M(A,a$1)){let v=C(l,s),T=U(A,a$1),m=1/0,g=1/0,x=1/0,h=-1/0,B=-1/0,w=-1/0;for(let S=3*v,P=3*(v+T);S<P;S++){let _=n[S],E=o.getX(_),I=o.getY(_),D=o.getZ(_);E<m&&(m=E),E>h&&(h=E),I<g&&(g=I),I>B&&(B=I),D<x&&(x=D),D>w&&(w=D);}return r[l+0]!==m||r[l+1]!==g||r[l+2]!==x||r[l+3]!==h||r[l+4]!==B||r[l+5]!==w?(r[l+0]=m,r[l+1]=g,r[l+2]=x,r[l+3]=h,r[l+4]=B,r[l+5]=w,true):false}else {let v=O(l),T=z(l,s),m=b,g=false,x=false;if(t){if(!m){let _=v/8+d/32,E=T/8+d/32;g=t.has(_),x=t.has(E),m=!g&&!x;}}else g=true,x=true;let h=m||g,B=m||x,w=false;h&&(w=f(v,d,m));let S=false;B&&(S=f(T,d,m));let P=w||S;if(P)for(let _=0;_<3;_++){let E=v+_,I=T+_,D=r[E],N=r[E+3],F=r[I],L=r[I+3];r[l+_]=D<F?D:F,r[l+_+3]=N>L?N:L;}return P}}a(f,"_traverse");}a(An,"refit");function $(i,t,e,n,o){let c,s,a,r,p,u,f=1/e.direction.x,l=1/e.direction.y,d=1/e.direction.z,b=e.origin.x,A=e.origin.y,v=e.origin.z,T=t[i],m=t[i+3],g=t[i+1],x=t[i+3+1],h=t[i+2],B=t[i+3+2];return f>=0?(c=(T-b)*f,s=(m-b)*f):(c=(m-b)*f,s=(T-b)*f),l>=0?(a=(g-A)*l,r=(x-A)*l):(a=(x-A)*l,r=(g-A)*l),c>r||a>s||((a>c||isNaN(c))&&(c=a),(r<s||isNaN(s))&&(s=r),d>=0?(p=(h-v)*d,u=(B-v)*d):(p=(B-v)*d,u=(h-v)*d),c>u||p>s)?false:((p>c||c!==c)&&(c=p),(u<s||s!==s)&&(s=u),c<=o&&s>=n)}a($,"intersectRay");var Ie=class{static{a(this,"_BufferStack");}constructor(){this.float32Array=null,this.uint16Array=null,this.uint32Array=null;let t=[],e=null;this.setBuffer=n=>{e&&t.push(e),e=n,this.float32Array=new Float32Array(n),this.uint16Array=new Uint16Array(n),this.uint32Array=new Uint32Array(n);},this.clearBuffer=()=>{e=null,this.float32Array=null,this.uint16Array=null,this.uint32Array=null,t.length!==0&&this.setBuffer(t.pop());};}},R=new Ie;function wn(i,t,e,n,o,c,s,a){let{geometry:r,_indirectBuffer:p}=i;for(let u=n,f=n+o;u<f;u++){let l=p?p[u]:u;xt(r,t,e,l,c,s,a);}}a(wn,"intersectTris_indirect");function Bn(i,t,e,n,o,c,s){let{geometry:a,_indirectBuffer:r}=i,p=1/0,u=null;for(let f=n,l=n+o;f<l;f++){let d;d=xt(a,t,e,r?r[f]:f,null,c,s),d&&d.distance<p&&(u=d,p=d.distance);}return u}a(Bn,"intersectClosestTri_indirect");function _n(i,t,e,n,o,c,s){let{geometry:a}=e,{index:r}=a,p=a.attributes.position;for(let u=i,f=t+i;u<f;u++){let l;if(l=e.resolveTriangleIndex(u),V(s,l*3,r,p),s.needsUpdate=true,n(s,l,o,c))return true}return false}a(_n,"iterateOverTriangles_indirect");function Sn(i,t,e,n,o,c,s){R.setBuffer(i._roots[t]),Pe(0,i,e,n,o,c,s),R.clearBuffer();}a(Sn,"raycast");function Pe(i,t,e,n,o,c,s){let{float32Array:a,uint16Array:r,uint32Array:p}=R,u=i*2;if(M(u,r)){let l=C(i,p),d=U(u,r);Tn(t,e,n,l,d,o,c,s);}else {let l=O(i);$(l,a,n,c,s)&&Pe(l,t,e,n,o,c,s);let d=z(i,p);$(d,a,n,c,s)&&Pe(d,t,e,n,o,c,s);}}a(Pe,"_raycast");var Mi=["x","y","z"];function In(i,t,e,n,o,c){R.setBuffer(i._roots[t]);let s=Ee(0,i,e,n,o,c);return R.clearBuffer(),s}a(In,"raycastFirst");function Ee(i,t,e,n,o,c){let{float32Array:s,uint16Array:a,uint32Array:r}=R,p=i*2;if(M(p,a)){let f=C(i,r),l=U(p,a);return bn(t,e,n,f,l,o,c)}else {let f=st(i,r),l=Mi[f],b=n.direction[l]>=0,A,v;b?(A=O(i),v=z(i,r)):(A=z(i,r),v=O(i));let m=$(A,s,n,o,c)?Ee(A,t,e,n,o,c):null;if(m){let h=m.point[l];if(b?h<=s[v+f]:h>=s[v+f+3])return m}let x=$(v,s,n,o,c)?Ee(v,t,e,n,o,c):null;return m&&x?m.distance<=x.distance?m:x:m||x||null}}a(Ee,"_raycastFirst");function H(i,t,e){return e.min.x=t[i],e.min.y=t[i+1],e.min.z=t[i+2],e.max.x=t[i+3],e.max.y=t[i+4],e.max.z=t[i+5],e}a(H,"arrayToBox");function De(i){let t=-1,e=-1/0;for(let n=0;n<3;n++){let o=i[n+3]-i[n];o>e&&(e=o,t=n);}return t}a(De,"getLongestEdgeIndex");function Ne(i,t){t.set(i);}a(Ne,"copyBounds");function Fe(i,t,e){let n,o;for(let c=0;c<3;c++){let s=c+3;n=i[c],o=t[c],e[c]=n<o?n:o,n=i[s],o=t[s],e[s]=n>o?n:o;}}a(Fe,"unionBounds");function Ct(i,t,e){for(let n=0;n<3;n++){let o=t[i+2*n],c=t[i+2*n+1],s=o-c,a=o+c;s<e[n]&&(e[n]=s),a>e[n+3]&&(e[n+3]=a);}}a(Ct,"expandByPrimitiveBounds");function gt(i){let t=i[3]-i[0],e=i[4]-i[1],n=i[5]-i[2];return 2*(t*e+e*n+n*t)}a(gt,"computeSurfaceArea");function Ot(i){return i.index?i.index.count:i.attributes.position.count}a(Ot,"getVertexCount");function rt(i){return Ot(i)/3}a(rt,"getTriCount");function Me(i,t=ArrayBuffer){return i>65535?new Uint32Array(new t(4*i)):new Uint16Array(new t(2*i))}a(Me,"getIndexArray");function Pn(i,t){if(!i.index){let e=i.attributes.position.count,n=t.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer,o=Me(e,n);i.setIndex(new BufferAttribute(o,1));for(let c=0;c<e;c++)o[c]=c;}}a(Pn,"ensureIndex");function Li(i,t,e){let n=Ot(i)/e,o=t||i.drawRange,c=o.start/e,s=(o.start+o.count)/e,a=Math.max(0,c),r=Math.min(n,s)-a;return {offset:Math.floor(a),count:Math.floor(r)}}a(Li,"getFullPrimitiveRange");function Ci(i,t){return i.groups.map(e=>({offset:e.start/t,count:e.count/t}))}a(Ci,"getPrimitiveGroupRanges");function Re(i,t,e){let n=Li(i,t,e),o=Ci(i,e);if(!o.length)return [n];let c=[],s=n.offset,a=n.offset+n.count,r=Ot(i)/e,p=[];for(let l of o){let{offset:d,count:b}=l,A=d,v=isFinite(b)?b:r-d,T=d+v;A<a&&T>s&&(p.push({pos:Math.max(s,A),isStart:true}),p.push({pos:Math.min(a,T),isStart:false}));}p.sort((l,d)=>l.pos!==d.pos?l.pos-d.pos:l.type==="end"?-1:1);let u=0,f=null;for(let l of p){let d=l.pos;u!==0&&d!==f&&c.push({offset:f,count:d-f}),u+=l.isStart?1:-1,f=d;}return c}a(Re,"getRootPrimitiveRanges");var Zt=new Box3,Tt=new X,bt=new X,zt=new Matrix4,En=new k,$t=new k;function Dn(i,t,e,n){R.setBuffer(i._roots[t]);let o=Le(0,i,e,n);return R.clearBuffer(),o}a(Dn,"intersectsGeometry");function Le(i,t,e,n,o=null){let{float32Array:c,uint16Array:s,uint32Array:a$1}=R,r=i*2;if(o===null&&(e.boundingBox||e.computeBoundingBox(),En.set(e.boundingBox.min,e.boundingBox.max,n),o=En),M(r,s)){let u=t.geometry,f=u.index,l=u.attributes.position,d=e.index,b=e.attributes.position,A=C(i,a$1),v=U(r,s);if(zt.copy(n).invert(),e.boundsTree)return H(i,c,$t),$t.matrix.copy(zt),$t.needsUpdate=true,e.boundsTree.shapecast({intersectsBounds:a(m=>$t.intersectsBox(m),"intersectsBounds"),intersectsTriangle:a(m=>{m.a.applyMatrix4(n),m.b.applyMatrix4(n),m.c.applyMatrix4(n),m.needsUpdate=true;for(let g=A*3,x=(v+A)*3;g<x;g+=3)if(V(bt,g,f,l),bt.needsUpdate=true,m.intersectsTriangle(bt))return true;return false},"intersectsTriangle")});{let T=rt(e);for(let m=A*3,g=(v+A)*3;m<g;m+=3){V(Tt,m,f,l),Tt.a.applyMatrix4(zt),Tt.b.applyMatrix4(zt),Tt.c.applyMatrix4(zt),Tt.needsUpdate=true;for(let x=0,h=T*3;x<h;x+=3)if(V(bt,x,d,b),bt.needsUpdate=true,Tt.intersectsTriangle(bt))return true}}}else {let u=O(i),f=z(i,a$1);return H(u,c,Zt),!!(o.intersectsBox(Zt)&&Le(u,t,e,n,o)||(H(f,c,Zt),o.intersectsBox(Zt)&&Le(f,t,e,n,o)))}}a(Le,"_intersectsGeometry");var Jt=new Matrix4,Ce=new k,Ut=new k,Vi=new Vector3,Hi=new Vector3,ki=new Vector3,qi=new Vector3;function Nn(i,t,e,n={},o={},c=0,s=1/0){t.boundingBox||t.computeBoundingBox(),Ce.set(t.boundingBox.min,t.boundingBox.max,e),Ce.needsUpdate=true;let a$1=i.geometry,r=a$1.attributes.position,p=a$1.index,u=t.attributes.position,f=t.index,l=W.getPrimitive(),d=W.getPrimitive(),b=Vi,A=Hi,v=null,T=null;o&&(v=ki,T=qi);let m=1/0,g=null,x=null;return Jt.copy(e).invert(),Ut.matrix.copy(Jt),i.shapecast({boundsTraverseOrder:a(h=>Ce.distanceToBox(h),"boundsTraverseOrder"),intersectsBounds:a((h,B,w)=>w<m&&w<s?(B&&(Ut.min.copy(h.min),Ut.max.copy(h.max),Ut.needsUpdate=true),true):false,"intersectsBounds"),intersectsRange:a((h,B)=>{if(t.boundsTree)return t.boundsTree.shapecast({boundsTraverseOrder:a(S=>Ut.distanceToBox(S),"boundsTraverseOrder"),intersectsBounds:a((S,P,_)=>_<m&&_<s,"intersectsBounds"),intersectsRange:a((S,P)=>{for(let _=S,E=S+P;_<E;_++){V(d,3*_,f,u),d.a.applyMatrix4(e),d.b.applyMatrix4(e),d.c.applyMatrix4(e),d.needsUpdate=true;for(let I=h,D=h+B;I<D;I++){V(l,3*I,p,r),l.needsUpdate=true;let N=l.distanceToTriangle(d,b,v);if(N<m&&(A.copy(b),T&&T.copy(v),m=N,g=I,x=_),N<c)return true}}},"intersectsRange")});{let w=rt(t);for(let S=0,P=w;S<P;S++){V(d,3*S,f,u),d.a.applyMatrix4(e),d.b.applyMatrix4(e),d.c.applyMatrix4(e),d.needsUpdate=true;for(let _=h,E=h+B;_<E;_++){V(l,3*_,p,r),l.needsUpdate=true;let I=l.distanceToTriangle(d,b,v);if(I<m&&(A.copy(b),T&&T.copy(v),m=I,g=_,x=S),I<c)return true}}}},"intersectsRange")}),W.releasePrimitive(l),W.releasePrimitive(d),m===1/0?null:(n.point?n.point.copy(A):n.point=A.clone(),n.distance=m,n.faceIndex=g,o&&(o.point?o.point.copy(T):o.point=T.clone(),o.point.applyMatrix4(Jt),A.applyMatrix4(Jt),o.distance=A.sub(o.point).length(),o.faceIndex=x),n)}a(Nn,"closestPointToGeometry");function Fn(i,t=null){t&&Array.isArray(t)&&(t=new Set(t));let e=i.geometry,n=e.index?e.index.array:null,o=e.attributes.position,c,s,a$1,r,p=0,u=i._roots;for(let l=0,d=u.length;l<d;l++)c=u[l],s=new Uint32Array(c),a$1=new Uint16Array(c),r=new Float32Array(c),f(0,p),p+=c.byteLength;function f(l,d,b=false){let A=l*2;if(M(A,a$1)){let v=C(l,s),T=U(A,a$1),m=1/0,g=1/0,x=1/0,h=-1/0,B=-1/0,w=-1/0;for(let S=v,P=v+T;S<P;S++){let _=3*i.resolveTriangleIndex(S);for(let E=0;E<3;E++){let I=_+E;I=n?n[I]:I;let D=o.getX(I),N=o.getY(I),F=o.getZ(I);D<m&&(m=D),D>h&&(h=D),N<g&&(g=N),N>B&&(B=N),F<x&&(x=F),F>w&&(w=F);}}return r[l+0]!==m||r[l+1]!==g||r[l+2]!==x||r[l+3]!==h||r[l+4]!==B||r[l+5]!==w?(r[l+0]=m,r[l+1]=g,r[l+2]=x,r[l+3]=h,r[l+4]=B,r[l+5]=w,true):false}else {let v=O(l),T=z(l,s),m=b,g=false,x=false;if(t){if(!m){let _=v/8+d/32,E=T/8+d/32;g=t.has(_),x=t.has(E),m=!g&&!x;}}else g=true,x=true;let h=m||g,B=m||x,w=false;h&&(w=f(v,d,m));let S=false;B&&(S=f(T,d,m));let P=w||S;if(P)for(let _=0;_<3;_++){let E=v+_,I=T+_,D=r[E],N=r[E+3],F=r[I],L=r[I+3];r[l+_]=D<F?D:F,r[l+_+3]=N>L?N:L;}return P}}a(f,"_traverse");}a(Fn,"refit_indirect");function Mn(i,t,e,n,o,c,s){R.setBuffer(i._roots[t]),Oe(0,i,e,n,o,c,s),R.clearBuffer();}a(Mn,"raycast_indirect");function Oe(i,t,e,n,o,c,s){let{float32Array:a,uint16Array:r,uint32Array:p}=R,u=i*2;if(M(u,r)){let l=C(i,p),d=U(u,r);wn(t,e,n,l,d,o,c,s);}else {let l=O(i);$(l,a,n,c,s)&&Oe(l,t,e,n,o,c,s);let d=z(i,p);$(d,a,n,c,s)&&Oe(d,t,e,n,o,c,s);}}a(Oe,"_raycast");var Gi=["x","y","z"];function Rn(i,t,e,n,o,c){R.setBuffer(i._roots[t]);let s=ze(0,i,e,n,o,c);return R.clearBuffer(),s}a(Rn,"raycastFirst_indirect");function ze(i,t,e,n,o,c){let{float32Array:s,uint16Array:a,uint32Array:r}=R,p=i*2;if(M(p,a)){let f=C(i,r),l=U(p,a);return Bn(t,e,n,f,l,o,c)}else {let f=st(i,r),l=Gi[f],b=n.direction[l]>=0,A,v;b?(A=O(i),v=z(i,r)):(A=z(i,r),v=O(i));let m=$(A,s,n,o,c)?ze(A,t,e,n,o,c):null;if(m){let h=m.point[l];if(b?h<=s[v+f]:h>=s[v+f+3])return m}let x=$(v,s,n,o,c)?ze(v,t,e,n,o,c):null;return m&&x?m.distance<=x.distance?m:x:m||x||null}}a(ze,"_raycastFirst");var te=new Box3,vt=new X,At=new X,Vt=new Matrix4,Ln=new k,ee=new k;function Cn(i,t,e,n){R.setBuffer(i._roots[t]);let o=Ue(0,i,e,n);return R.clearBuffer(),o}a(Cn,"intersectsGeometry_indirect");function Ue(i,t,e,n,o=null){let{float32Array:c,uint16Array:s,uint32Array:a$1}=R,r=i*2;if(o===null&&(e.boundingBox||e.computeBoundingBox(),Ln.set(e.boundingBox.min,e.boundingBox.max,n),o=Ln),M(r,s)){let u=t.geometry,f=u.index,l=u.attributes.position,d=e.index,b=e.attributes.position,A=C(i,a$1),v=U(r,s);if(Vt.copy(n).invert(),e.boundsTree)return H(i,c,ee),ee.matrix.copy(Vt),ee.needsUpdate=true,e.boundsTree.shapecast({intersectsBounds:a(m=>ee.intersectsBox(m),"intersectsBounds"),intersectsTriangle:a(m=>{m.a.applyMatrix4(n),m.b.applyMatrix4(n),m.c.applyMatrix4(n),m.needsUpdate=true;for(let g=A,x=v+A;g<x;g++)if(V(At,3*t.resolveTriangleIndex(g),f,l),At.needsUpdate=true,m.intersectsTriangle(At))return true;return false},"intersectsTriangle")});{let T=rt(e);for(let m=A,g=v+A;m<g;m++){let x=t.resolveTriangleIndex(m);V(vt,3*x,f,l),vt.a.applyMatrix4(Vt),vt.b.applyMatrix4(Vt),vt.c.applyMatrix4(Vt),vt.needsUpdate=true;for(let h=0,B=T*3;h<B;h+=3)if(V(At,h,d,b),At.needsUpdate=true,vt.intersectsTriangle(At))return true}}}else {let u=O(i),f=z(i,a$1);return H(u,c,te),!!(o.intersectsBox(te)&&Ue(u,t,e,n,o)||(H(f,c,te),o.intersectsBox(te)&&Ue(f,t,e,n,o)))}}a(Ue,"_intersectsGeometry");var ne=new Matrix4,Ve=new k,Ht=new k,Ki=new Vector3,Zi=new Vector3,$i=new Vector3,ji=new Vector3;function On(i,t,e,n={},o={},c=0,s=1/0){t.boundingBox||t.computeBoundingBox(),Ve.set(t.boundingBox.min,t.boundingBox.max,e),Ve.needsUpdate=true;let a$1=i.geometry,r=a$1.attributes.position,p=a$1.index,u=t.attributes.position,f=t.index,l=W.getPrimitive(),d=W.getPrimitive(),b=Ki,A=Zi,v=null,T=null;o&&(v=$i,T=ji);let m=1/0,g=null,x=null;return ne.copy(e).invert(),Ht.matrix.copy(ne),i.shapecast({boundsTraverseOrder:a(h=>Ve.distanceToBox(h),"boundsTraverseOrder"),intersectsBounds:a((h,B,w)=>w<m&&w<s?(B&&(Ht.min.copy(h.min),Ht.max.copy(h.max),Ht.needsUpdate=true),true):false,"intersectsBounds"),intersectsRange:a((h,B)=>{if(t.boundsTree){let w=t.boundsTree;return w.shapecast({boundsTraverseOrder:a(S=>Ht.distanceToBox(S),"boundsTraverseOrder"),intersectsBounds:a((S,P,_)=>_<m&&_<s,"intersectsBounds"),intersectsRange:a((S,P)=>{for(let _=S,E=S+P;_<E;_++){let I=w.resolveTriangleIndex(_);V(d,3*I,f,u),d.a.applyMatrix4(e),d.b.applyMatrix4(e),d.c.applyMatrix4(e),d.needsUpdate=true;for(let D=h,N=h+B;D<N;D++){let F=i.resolveTriangleIndex(D);V(l,3*F,p,r),l.needsUpdate=true;let L=l.distanceToTriangle(d,b,v);if(L<m&&(A.copy(b),T&&T.copy(v),m=L,g=D,x=_),L<c)return true}}},"intersectsRange")})}else {let w=rt(t);for(let S=0,P=w;S<P;S++){V(d,3*S,f,u),d.a.applyMatrix4(e),d.b.applyMatrix4(e),d.c.applyMatrix4(e),d.needsUpdate=true;for(let _=h,E=h+B;_<E;_++){let I=i.resolveTriangleIndex(_);V(l,3*I,p,r),l.needsUpdate=true;let D=l.distanceToTriangle(d,b,v);if(D<m&&(A.copy(b),T&&T.copy(v),m=D,g=_,x=S),D<c)return true}}}},"intersectsRange")}),W.releasePrimitive(l),W.releasePrimitive(d),m===1/0?null:(n.point?n.point.copy(A):n.point=A.clone(),n.distance=m,n.faceIndex=g,o&&(o.point?o.point.copy(T):o.point=T.clone(),o.point.applyMatrix4(ne),A.applyMatrix4(ne),o.distance=A.sub(o.point).length(),o.faceIndex=x),n)}a(On,"closestPointToGeometry_indirect");function He(i,t,e){return i===null?null:(i.point.applyMatrix4(t.matrixWorld),i.distance=i.point.distanceTo(e.ray.origin),i.object=t,i)}a(He,"convertRaycastIntersect");function zn(){return typeof SharedArrayBuffer<"u"}a(zn,"isSharedArrayBufferSupported");function oe(i,t,e,n,o){let c=1/0,s=1/0,a=1/0,r=-1/0,p=-1/0,u=-1/0,f=1/0,l=1/0,d=1/0,b=-1/0,A=-1/0,v=-1/0,T=i.offset||0;for(let m=(t-T)*6,g=(t+e-T)*6;m<g;m+=6){let x=i[m+0],h=i[m+1],B=x-h,w=x+h;B<c&&(c=B),w>r&&(r=w),x<f&&(f=x),x>b&&(b=x);let S=i[m+2],P=i[m+3],_=S-P,E=S+P;_<s&&(s=_),E>p&&(p=E),S<l&&(l=S),S>A&&(A=S);let I=i[m+4],D=i[m+5],N=I-D,F=I+D;N<a&&(a=N),F>u&&(u=F),I<d&&(d=I),I>v&&(v=I);}n[0]=c,n[1]=s,n[2]=a,n[3]=r,n[4]=p,n[5]=u,o[0]=f,o[1]=l,o[2]=d,o[3]=b,o[4]=A,o[5]=v;}a(oe,"getBounds");var nt=32,Qi=a((i,t)=>i.candidate-t.candidate,"binsSort"),ct=new Array(nt).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),se=new Float32Array(6);function Vn(i,t,e,n,o,c){let s=-1,a=0;if(c===0)s=De(t),s!==-1&&(a=(t[s]+t[s+3])/2);else if(c===1)s=De(i),s!==-1&&(a=to(e,n,o,s));else if(c===2){let r=gt(i),p=1.25*o,u=e.offset||0,f=(n-u)*6,l=(n+o-u)*6;for(let d=0;d<3;d++){let b=t[d],T=(t[d+3]-b)/nt;if(o<nt/4){let m=[...ct];m.length=o;let g=0;for(let h=f;h<l;h+=6,g++){let B=m[g];B.candidate=e[h+2*d],B.count=0;let{bounds:w,leftCacheBounds:S,rightCacheBounds:P}=B;for(let _=0;_<3;_++)P[_]=1/0,P[_+3]=-1/0,S[_]=1/0,S[_+3]=-1/0,w[_]=1/0,w[_+3]=-1/0;Ct(h,e,w);}m.sort(Qi);let x=o;for(let h=0;h<x;h++){let B=m[h];for(;h+1<x&&m[h+1].candidate===B.candidate;)m.splice(h+1,1),x--;}for(let h=f;h<l;h+=6){let B=e[h+2*d];for(let w=0;w<x;w++){let S=m[w];B>=S.candidate?Ct(h,e,S.rightCacheBounds):(Ct(h,e,S.leftCacheBounds),S.count++);}}for(let h=0;h<x;h++){let B=m[h],w=B.count,S=o-B.count,P=B.leftCacheBounds,_=B.rightCacheBounds,E=0;w!==0&&(E=gt(P)/r);let I=0;S!==0&&(I=gt(_)/r);let D=1+1.25*(E*w+I*S);D<p&&(s=d,p=D,a=B.candidate);}}else {for(let x=0;x<nt;x++){let h=ct[x];h.count=0,h.candidate=b+T+x*T;let B=h.bounds;for(let w=0;w<3;w++)B[w]=1/0,B[w+3]=-1/0;}for(let x=f;x<l;x+=6){let w=~~((e[x+2*d]-b)/T);w>=nt&&(w=nt-1);let S=ct[w];S.count++,Ct(x,e,S.bounds);}let m=ct[nt-1];Ne(m.bounds,m.rightCacheBounds);for(let x=nt-2;x>=0;x--){let h=ct[x],B=ct[x+1];Fe(h.bounds,B.rightCacheBounds,h.rightCacheBounds);}let g=0;for(let x=0;x<nt-1;x++){let h=ct[x],B=h.count,w=h.bounds,P=ct[x+1].rightCacheBounds;B!==0&&(g===0?Ne(w,se):Fe(w,se,se)),g+=B;let _=0,E=0;g!==0&&(_=gt(se)/r);let I=o-g;I!==0&&(E=gt(P)/r);let D=1+1.25*(_*g+E*I);D<p&&(s=d,p=D,a=h.candidate);}}}}else console.warn(`BVH: Invalid build strategy value ${c} used.`);return {axis:s,pos:a}}a(Vn,"getOptimalSplit");function to(i,t,e,n){let o=0,c=i.offset;for(let s=t,a=t+e;s<a;s++)o+=i[(s-c)*6+n*2];return o/e}a(to,"getAverage");var wt=class{static{a(this,"BVHNode");}constructor(){this.boundingData=new Float32Array(6);}};function Hn(i,t,e,n,o,c){let s=n,a=n+o-1,r=c.pos,p=c.axis*2,u=e.offset||0;for(;;){for(;s<=a&&e[(s-u)*6+p]<r;)s++;for(;s<=a&&e[(a-u)*6+p]>=r;)a--;if(s<a){for(let f=0;f<t;f++){let l=i[s*t+f];i[s*t+f]=i[a*t+f],i[a*t+f]=l;}for(let f=0;f<6;f++){let l=s-u,d=a-u,b=e[l*6+f];e[l*6+f]=e[d*6+f],e[d*6+f]=b;}s++,a--;}else return s}}a(Hn,"partition");var kn,re,ke,qn,eo=Math.pow(2,32);function ce(i){return "count"in i?1:1+ce(i.left)+ce(i.right)}a(ce,"countNodes");function Gn(i,t,e){return kn=new Float32Array(e),re=new Uint32Array(e),ke=new Uint16Array(e),qn=new Uint8Array(e),qe(i,t)}a(Gn,"populateBuffer");function qe(i,t){let e=i/4,n=i/2,o="count"in t,c=t.boundingData;for(let s=0;s<6;s++)kn[e+s]=c[s];if(o)return t.buffer?(qn.set(new Uint8Array(t.buffer),i),i+t.buffer.byteLength):(re[e+6]=t.offset,ke[n+14]=t.count,ke[n+15]=65535,i+32);{let{left:s,right:a,splitAxis:r}=t,p=i+32,u=qe(p,s),f=i/32,d=u/32-f;if(d>eo)throw new Error("MeshBVH: Cannot store relative child node offset greater than 32 bits.");return re[e+6]=d,re[e+7]=r,qe(u,a)}}a(qe,"_populateBuffer");function no(i,t,e,n,o){let{maxDepth:c,verbose:s,maxLeafSize:a$1,strategy:r,onProgress:p}=o,u=i.primitiveBuffer,f=i.primitiveBufferStride,l=new Float32Array(6),d=false,b=new wt;return oe(t,e,n,b.boundingData,l),v(b,e,n,l),b;function A(T){p&&p(T/n);}function v(T,m,g,x=null,h=0){if(!d&&h>=c&&(d=true,s&&console.warn(`BVH: Max depth of ${c} reached when generating BVH. Consider increasing maxDepth.`)),g<=a$1||h>=c)return A(m+g),T.offset=m,T.count=g,T;let B=Vn(T.boundingData,x,t,m,g,r);if(B.axis===-1)return A(m+g),T.offset=m,T.count=g,T;let w=Hn(u,f,t,m,g,B);if(w===m||w===m+g)A(m+g),T.offset=m,T.count=g;else {T.splitAxis=B.axis;let S=new wt,P=m,_=w-m;T.left=S,oe(t,P,_,S.boundingData,l),v(S,P,_,l,h+1);let E=new wt,I=w,D=g-_;T.right=E,oe(t,I,D,E.boundingData,l),v(E,I,D,l,h+1);}return T}}a(no,"buildTree");function Xn(i,t){let e=t.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer,n=i.getRootRanges(t.range),o=n[0],c=n[n.length-1],s={offset:o.offset,count:c.offset+c.count-o.offset},a=new Float32Array(6*s.count);a.offset=s.offset,i.computePrimitiveBounds(s.offset,s.count,a),i._roots=n.map(r=>{let p=no(i,a,r.offset,r.count,t),u=ce(p),f=new e(32*u);return Gn(0,p,f),f});}a(Xn,"buildPackedTree");var at,_t,Bt=[],ae=new ot(()=>new Box3);function Yn(i,t,e,n,o,c){at=ae.getPrimitive(),_t=ae.getPrimitive(),Bt.push(at,_t),R.setBuffer(i._roots[t]);let s=Ge(0,i.geometry,e,n,o,c);R.clearBuffer(),ae.releasePrimitive(at),ae.releasePrimitive(_t),Bt.pop(),Bt.pop();let a=Bt.length;return a>0&&(_t=Bt[a-1],at=Bt[a-2]),s}a(Yn,"shapecast");function Ge(i,t,e,n,o=null,c=0,s=0){let{float32Array:a$1,uint16Array:r,uint32Array:p}=R,u=i*2;if(M(u,r)){let b=C(i,p),A=U(u,r);return H(i,a$1,at),n(b,A,false,s,c+i/8,at)}else {let I=function(N){let{uint16Array:F,uint32Array:L}=R,j=N*2;for(;!M(j,F);)N=O(N),j=N*2;return C(N,L)},D=function(N){let{uint16Array:F,uint32Array:L}=R,j=N*2;for(;!M(j,F);)N=z(N,L),j=N*2;return C(N,L)+U(j,F)};a(I,"getLeftOffset"),a(D,"getRightEndOffset");let b=O(i),A=z(i,p),v=b,T=A,m,g,x,h;if(o&&(x=at,h=_t,H(v,a$1,x),H(T,a$1,h),m=o(x),g=o(h),g<m)){v=A,T=b;let N=m;m=g,g=N,x=h;}x||(x=at,H(v,a$1,x));let B=M(v*2,r),w=e(x,B,m,s+1,c+v/8),S;if(w===2){let N=I(v),L=D(v)-N;S=n(N,L,true,s+1,c+v/8,x);}else S=w&&Ge(v,t,e,n,o,c,s+1);if(S)return true;h=_t,H(T,a$1,h);let P=M(T*2,r),_=e(h,P,g,s+1,c+T/8),E;if(_===2){let N=I(T),L=D(T)-N;E=n(N,L,true,s+1,c+T/8,h);}else E=_&&Ge(T,t,e,n,o,c,s+1);return !!E}}a(Ge,"shapecastTraverse");var kt=new R.constructor,le=new R.constructor,lt=new ot(()=>new Box3),St=new Box3,It=new Box3,Xe=new Box3,Ye=new Box3,We=false;function Wn(i,t,e,n){if(We)throw new Error("MeshBVH: Recursive calls to bvhcast not supported.");We=true;let o=i._roots,c=t._roots,s,a=0,r=0,p=new Matrix4().copy(e).invert();for(let u=0,f=o.length;u<f;u++){kt.setBuffer(o[u]),r=0;let l=lt.getPrimitive();H(0,kt.float32Array,l),l.applyMatrix4(p);for(let d=0,b=c.length;d<b&&(le.setBuffer(c[d]),s=Q(0,0,e,p,n,a,r,0,0,l),le.clearBuffer(),r+=c[d].byteLength/32,!s);d++);if(lt.releasePrimitive(l),kt.clearBuffer(),a+=o[u].byteLength/32,s)break}return We=false,s}a(Wn,"bvhcast");function Q(i,t,e,n,o,c=0,s=0,a=0,r=0,p=null,u=false){let f,l;u?(f=le,l=kt):(f=kt,l=le);let d=f.float32Array,b=f.uint32Array,A=f.uint16Array,v=l.float32Array,T=l.uint32Array,m=l.uint16Array,g=i*2,x=t*2,h=M(g,A),B=M(x,m),w=false;if(B&&h)u?w=o(C(t,T),U(t*2,m),C(i,b),U(i*2,A),r,s+t/8,a,c+i/8):w=o(C(i,b),U(i*2,A),C(t,T),U(t*2,m),a,c+i/8,r,s+t/8);else if(B){let S=lt.getPrimitive();H(t,v,S),S.applyMatrix4(e);let P=O(i),_=z(i,b);H(P,d,St),H(_,d,It);let E=S.intersectsBox(St),I=S.intersectsBox(It);w=E&&Q(t,P,n,e,o,s,c,r,a+1,S,!u)||I&&Q(t,_,n,e,o,s,c,r,a+1,S,!u),lt.releasePrimitive(S);}else {let S=O(t),P=z(t,T);H(S,v,Xe),H(P,v,Ye);let _=p.intersectsBox(Xe),E=p.intersectsBox(Ye);if(_&&E)w=Q(i,S,e,n,o,c,s,a,r+1,p,u)||Q(i,P,e,n,o,c,s,a,r+1,p,u);else if(_)if(h)w=Q(i,S,e,n,o,c,s,a,r+1,p,u);else {let I=lt.getPrimitive();I.copy(Xe).applyMatrix4(e);let D=O(i),N=z(i,b);H(D,d,St),H(N,d,It);let F=I.intersectsBox(St),L=I.intersectsBox(It);w=F&&Q(S,D,n,e,o,s,c,r,a+1,I,!u)||L&&Q(S,N,n,e,o,s,c,r,a+1,I,!u),lt.releasePrimitive(I);}else if(E)if(h)w=Q(i,P,e,n,o,c,s,a,r+1,p,u);else {let I=lt.getPrimitive();I.copy(Ye).applyMatrix4(e);let D=O(i),N=z(i,b);H(D,d,St),H(N,d,It);let F=I.intersectsBox(St),L=I.intersectsBox(It);w=F&&Q(P,D,n,e,o,s,c,r,a+1,I,!u)||L&&Q(P,N,n,e,o,s,c,r,a+1,I,!u),lt.releasePrimitive(I);}}return w}a(Q,"_traverse");var Kn=new Box3,Pt=new Float32Array(6),fe=class{static{a(this,"BVH");}constructor(){this._roots=null,this.primitiveBuffer=null,this.primitiveBufferStride=null;}init(t){t={...Gt,...t},Xn(this,t);}getRootRanges(){throw new Error("BVH: getRootRanges() not implemented")}writePrimitiveBounds(){throw new Error("BVH: writePrimitiveBounds() not implemented")}writePrimitiveRangeBounds(t,e,n,o){let c=1/0,s=1/0,a=1/0,r=-1/0,p=-1/0,u=-1/0;for(let f=t,l=t+e;f<l;f++){this.writePrimitiveBounds(f,Pt,0);let[d,b,A,v,T,m]=Pt;d<c&&(c=d),v>r&&(r=v),b<s&&(s=b),T>p&&(p=T),A<a&&(a=A),m>u&&(u=m);}return n[o+0]=c,n[o+1]=s,n[o+2]=a,n[o+3]=r,n[o+4]=p,n[o+5]=u,n}computePrimitiveBounds(t,e,n){let o=n.offset||0;for(let c=t,s=t+e;c<s;c++){this.writePrimitiveBounds(c,Pt,0);let[a,r,p,u,f,l]=Pt,d=(a+u)/2,b=(r+f)/2,A=(p+l)/2,v=(u-a)/2,T=(f-r)/2,m=(l-p)/2,g=(c-o)*6;n[g+0]=d,n[g+1]=v+(Math.abs(d)+v)*yt,n[g+2]=b,n[g+3]=T+(Math.abs(b)+T)*yt,n[g+4]=A,n[g+5]=m+(Math.abs(A)+m)*yt;}return n}shiftPrimitiveOffsets(t){let e=this._indirectBuffer;if(e)for(let n=0,o=e.length;n<o;n++)e[n]+=t;else {let n=this._roots;for(let o=0;o<n.length;o++){let c=n[o],s=new Uint32Array(c),a=new Uint16Array(c),r=c.byteLength/32;for(let p=0;p<r;p++){let u=8*p,f=2*u;M(f,a)&&(s[u+6]+=t);}}}}traverse(t,e=0){let n=this._roots[e],o=new Uint32Array(n),c=new Uint16Array(n);s(0);function s(a,r=0){let p=a*2,u=M(p,c);if(u){let f=o[a+6],l=c[p+14];t(r,u,new Float32Array(n,a*4,6),f,l);}else {let f=O(a),l=z(a,o),d=st(a,o);t(r,u,new Float32Array(n,a*4,6),d)||(s(f,r+1),s(l,r+1));}}a(s,"_traverse");}refit(){let t=this._roots;for(let e=0,n=t.length;e<n;e++){let o=t[e],c=new Uint32Array(o),s=new Uint16Array(o),a=new Float32Array(o),r=o.byteLength/32;for(let p=r-1;p>=0;p--){let u=p*8,f=u*2;if(M(f,s)){let d=C(u,c),b=U(f,s);this.writePrimitiveRangeBounds(d,b,Pt,0),a.set(Pt,u);}else {let d=O(u),b=z(u,c);for(let A=0;A<3;A++){let v=a[d+A],T=a[d+A+3],m=a[b+A],g=a[b+A+3];a[u+A]=v<m?v:m,a[u+A+3]=T>g?T:g;}}}}}getBoundingBox(t){return t.makeEmpty(),this._roots.forEach(n=>{H(0,new Float32Array(n),Kn),t.union(Kn);}),t}shapecast(t){let{boundsTraverseOrder:e,intersectsBounds:n,intersectsRange:o,intersectsPrimitive:c,scratchPrimitive:s,iterate:a$1}=t;if(o&&c){let f=o;o=a((l,d,b,A,v)=>f(l,d,b,A,v)?true:a$1(l,d,this,c,b,A,s),"intersectsRange");}else o||(c?o=a((f,l,d,b)=>a$1(f,l,this,c,d,b,s),"intersectsRange"):o=a((f,l,d)=>d,"intersectsRange"));let r=false,p=0,u=this._roots;for(let f=0,l=u.length;f<l;f++){let d=u[f];if(r=Yn(this,f,n,o,e,p),r)break;p+=d.byteLength/32;}return r}bvhcast(t,e,n){let{intersectsRanges:o}=n;return Wn(this,t,e,o)}};function ao(i,t){let e=i[i.length-1],n=e.offset+e.count>2**16,o=i.reduce((p,u)=>p+u.count,0),c=n?4:2,s=t?new SharedArrayBuffer(o*c):new ArrayBuffer(o*c),a=n?new Uint32Array(s):new Uint16Array(s),r=0;for(let p=0;p<i.length;p++){let{offset:u,count:f}=i[p];for(let l=0;l<f;l++)a[r+l]=u+l;r+=f;}return a}a(ao,"generateIndirectBuffer");var ue=class extends fe{static{a(this,"GeometryBVH");}get indirect(){return !!this._indirectBuffer}get primitiveStride(){return null}get primitiveBufferStride(){return this.indirect?1:this.primitiveStride}set primitiveBufferStride(t){}get primitiveBuffer(){return this.indirect?this._indirectBuffer:this.geometry.index.array}set primitiveBuffer(t){}constructor(t,e={}){if(t.isBufferGeometry){if(t.index&&t.index.isInterleavedBufferAttribute)throw new Error("BVH: InterleavedBufferAttribute is not supported for the index attribute.")}else throw new Error("BVH: Only BufferGeometries are supported.");if(e.useSharedArrayBuffer&&!zn())throw new Error("BVH: SharedArrayBuffer is not available.");super(),this.geometry=t,this.resolvePrimitiveIndex=e.indirect?n=>this._indirectBuffer[n]:n=>n,this.primitiveBuffer=null,this.primitiveBufferStride=null,this._indirectBuffer=null,e={...Gt,...e},e[Dt]||this.init(e);}init(t){let{geometry:e,primitiveStride:n}=this;if(t.indirect){let o=Re(e,t.range,n),c=ao(o,t.useSharedArrayBuffer);this._indirectBuffer=c;}else Pn(e,t);super.init(t),!e.boundingBox&&t.setBoundingBox&&(e.boundingBox=this.getBoundingBox(new Box3));}getRootRanges(t){return this.indirect?[{offset:0,count:this._indirectBuffer.length}]:Re(this.geometry,t,this.primitiveStride)}raycastObject3D(){throw new Error("BVH: raycastObject3D() not implemented")}};var pe=new k,de=new Ray,$n=new Vector3,jn=new Matrix4,Jn=new Vector3,Ke=["getX","getY","getZ"],Qn=class i extends ue{static{a(this,"MeshBVH");}static serialize(t,e={}){e={cloneBuffers:true,...e};let n=t.geometry,o=t._roots,c=t._indirectBuffer,s=n.getIndex(),a={version:1,roots:null,index:null,indirectBuffer:null};return e.cloneBuffers?(a.roots=o.map(r=>r.slice()),a.index=s?s.array.slice():null,a.indirectBuffer=c?c.slice():null):(a.roots=o,a.index=s?s.array:null,a.indirectBuffer=c),a}static deserialize(t,e,n={}){n={setIndex:true,indirect:!!t.indirectBuffer,...n};let{index:o,roots:c,indirectBuffer:s}=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."),r(c));let a=new i(e,{...n,[Dt]:true});if(a._roots=c,a._indirectBuffer=s||null,n.setIndex){let p=e.getIndex();if(p===null){let u=new BufferAttribute(t.index,1,false);e.setIndex(u);}else p.array!==o&&(p.array.set(o),p.needsUpdate=true);}return a;function r(p){for(let u=0;u<p.length;u++){let f=p[u],l=new Uint32Array(f),d=new Uint16Array(f);for(let b=0,A=f.byteLength/32;b<A;b++){let v=8*b,T=2*v;M(T,d)||(l[v+6]=l[v+6]/8-b);}}}}get primitiveStride(){return 3}get resolveTriangleIndex(){return this.resolvePrimitiveIndex}constructor(t,e={}){e.maxLeafTris&&(console.warn('MeshBVH: "maxLeafTris" option has been deprecated. Use maxLeafSize, instead.'),e={...e,maxLeafSize:e.maxLeafTris}),super(t,e);}shiftTriangleOffsets(t){return super.shiftPrimitiveOffsets(t)}writePrimitiveBounds(t,e,n){let o=this.geometry,c=this._indirectBuffer,s=o.attributes.position,a=o.index?o.index.array:null,p=(c?c[t]:t)*3,u=p+0,f=p+1,l=p+2;a&&(u=a[u],f=a[f],l=a[l]);for(let d=0;d<3;d++){let b=s[Ke[d]](u),A=s[Ke[d]](f),v=s[Ke[d]](l),T=b;A<T&&(T=A),v<T&&(T=v);let m=b;A>m&&(m=A),v>m&&(m=v),e[n+d]=T,e[n+d+3]=m;}return e}computePrimitiveBounds(t,e,n){let o=this.geometry,c=this._indirectBuffer,s=o.attributes.position,a=o.index?o.index.array:null,r=s.normalized;if(t<0||e+t-n.offset>n.length/6)throw new Error("MeshBVH: compute triangle bounds range is invalid.");let p=s.array,u=s.offset||0,f=3;s.isInterleavedBufferAttribute&&(f=s.data.stride);let l=["getX","getY","getZ"],d=n.offset;for(let b=t,A=t+e;b<A;b++){let T=(c?c[b]:b)*3,m=(b-d)*6,g=T+0,x=T+1,h=T+2;a&&(g=a[g],x=a[x],h=a[h]),r||(g=g*f+u,x=x*f+u,h=h*f+u);for(let B=0;B<3;B++){let w,S,P;r?(w=s[l[B]](g),S=s[l[B]](x),P=s[l[B]](h)):(w=p[g+B],S=p[x+B],P=p[h+B]);let _=w;S<_&&(_=S),P<_&&(_=P);let E=w;S>E&&(E=S),P>E&&(E=P);let I=(E-_)/2,D=B*2;n[m+D+0]=_+I,n[m+D+1]=I+(Math.abs(_)+I)*yt;}}return n}raycastObject3D(t,e,n=[]){let{material:o}=t;if(o===void 0)return;jn.copy(t.matrixWorld).invert(),de.copy(e.ray).applyMatrix4(jn),Jn.setFromMatrixScale(t.matrixWorld),$n.copy(de.direction).multiply(Jn);let c=$n.length(),s=e.near/c,a=e.far/c;if(e.firstHitOnly===true){let r=this.raycastFirst(de,o,s,a);r=He(r,t,e),r&&n.push(r);}else {let r=this.raycast(de,o,s,a);for(let p=0,u=r.length;p<u;p++){let f=He(r[p],t,e);f&&n.push(f);}}return n}refit(t=null){return (this.indirect?Fn:An)(this,t)}raycast(t,e=FrontSide,n=0,o=1/0){let c=this._roots,s=[],a=this.indirect?Mn:Sn;for(let r=0,p=c.length;r<p;r++)a(this,r,e,t,s,n,o);return s}raycastFirst(t,e=FrontSide,n=0,o=1/0){let c=this._roots,s=null,a=this.indirect?Rn:In;for(let r=0,p=c.length;r<p;r++){let u=a(this,r,e,t,n,o);u!=null&&(s==null||u.distance<s.distance)&&(s=u);}return s}intersectsGeometry(t,e){let n=false,o=this._roots,c=this.indirect?Cn:Dn;for(let s=0,a=o.length;s<a&&(n=c(this,s,t,e),!n);s++);return n}shapecast(t){let e=W.getPrimitive(),n=super.shapecast({...t,intersectsPrimitive:t.intersectsTriangle,scratchPrimitive:e,iterate:this.indirect?_n:vn});return W.releasePrimitive(e),n}bvhcast(t,e,n){let{intersectsRanges:o,intersectsTriangles:c}=n,s=W.getPrimitive(),a$1=this.geometry.index,r=this.geometry.attributes.position,p=this.indirect?b=>{let A=this.resolveTriangleIndex(b);V(s,A*3,a$1,r);}:b=>{V(s,b*3,a$1,r);},u=W.getPrimitive(),f=t.geometry.index,l=t.geometry.attributes.position,d=t.indirect?b=>{let A=t.resolveTriangleIndex(b);V(u,A*3,f,l);}:b=>{V(u,b*3,f,l);};if(c){if(!(t instanceof i))throw new Error('MeshBVH: "intersectsTriangles" callback can only be used with another MeshBVH.');let b=a((A,v,T,m,g,x,h,B)=>{for(let w=T,S=T+m;w<S;w++){d(w),u.a.applyMatrix4(e),u.b.applyMatrix4(e),u.c.applyMatrix4(e),u.needsUpdate=true;for(let P=A,_=A+v;P<_;P++)if(p(P),s.needsUpdate=true,c(s,u,P,w,g,x,h,B))return true}return false},"iterateOverDoubleTriangles");if(o){let A=o;o=a(function(v,T,m,g,x,h,B,w){return A(v,T,m,g,x,h,B,w)?true:b(v,T,m,g,x,h,B,w)},"intersectsRanges");}else o=b;}return super.bvhcast(t,e,{intersectsRanges:o})}intersectsBox(t,e){return pe.set(t.min,t.max,e),pe.needsUpdate=true,this.shapecast({intersectsBounds:a(n=>pe.intersectsBox(n),"intersectsBounds"),intersectsTriangle:a(n=>pe.intersectsTriangle(n),"intersectsTriangle")})}intersectsSphere(t){return this.shapecast({intersectsBounds:a(e=>t.intersectsBox(e),"intersectsBounds"),intersectsTriangle:a(e=>e.intersectsSphere(t),"intersectsTriangle")})}closestPointToGeometry(t,e,n={},o={},c=0,s=1/0){return (this.indirect?On:Nn)(this,t,e,n,o,c,s)}closestPointToPoint(t,e={},n=0,o=1/0){return pn(this,t,e,n,o)}};var Et={Mesh:Mesh.prototype.raycast,Line:Line.prototype.raycast,LineSegments:LineSegments.prototype.raycast,LineLoop:LineLoop.prototype.raycast,Points:Points.prototype.raycast,BatchedMesh:BatchedMesh.prototype.raycast},Y=new Mesh,me=[];function xa(i,t){if(this.isBatchedMesh)ho.call(this,i,t);else {let{geometry:e}=this;if(e.boundsTree)e.boundsTree.raycastObject3D(this,i,t);else {let n;if(this instanceof Mesh)n=Et.Mesh;else if(this instanceof LineSegments)n=Et.LineSegments;else if(this instanceof LineLoop)n=Et.LineLoop;else if(this instanceof Line)n=Et.Line;else if(this instanceof Points)n=Et.Points;else throw new Error("BVH: Fallback raycast function not found.");n.call(this,i,t);}}}a(xa,"acceleratedRaycast");function ho(i,t){if(this.boundsTrees){let e=this.boundsTrees,n=this._drawInfo||this._instanceInfo,o=this._drawRanges||this._geometryInfo,c=this.matrixWorld;Y.material=this.material,Y.geometry=this.geometry;let s=Y.geometry.boundsTree,a=Y.geometry.drawRange;Y.geometry.boundingSphere===null&&(Y.geometry.boundingSphere=new Sphere);for(let r=0,p=n.length;r<p;r++){if(!this.getVisibleAt(r))continue;let u=n[r].geometryIndex;if(Y.geometry.boundsTree=e[u],this.getMatrixAt(r,Y.matrixWorld).premultiply(c),!Y.geometry.boundsTree){this.getBoundingBoxAt(u,Y.geometry.boundingBox),this.getBoundingSphereAt(u,Y.geometry.boundingSphere);let f=o[u];Y.geometry.setDrawRange(f.start,f.count);}Y.raycast(i,me);for(let f=0,l=me.length;f<l;f++){let d=me[f];d.object=this,d.batchId=r,t.push(d);}me.length=0;}Y.geometry.boundsTree=s,Y.geometry.drawRange=a,Y.material=null,Y.geometry=null;}else Et.BatchedMesh.call(this,i,t);}a(ho,"acceleratedBatchedMeshRaycast");function Bo(i){switch(i){case 1:return "R";case 2:return "RG";case 3:return "RGBA";case 4:return "RGBA"}throw new Error}a(Bo,"countToStringFormat");function _o(i){switch(i){case 1:return RedFormat;case 2:return RGFormat;case 3:return RGBAFormat;case 4:return RGBAFormat}}a(_o,"countToFormat");function ai(i){switch(i){case 1:return RedIntegerFormat;case 2:return RGIntegerFormat;case 3:return RGBAIntegerFormat;case 4:return RGBAIntegerFormat}}a(ai,"countToIntFormat");var xe=class extends DataTexture{static{a(this,"VertexAttributeTexture");}constructor(){super(),this.minFilter=NearestFilter,this.magFilter=NearestFilter,this.generateMipmaps=false,this.overrideItemSize=null,this._forcedType=null;}updateFrom(t){let e=this.overrideItemSize,n=t.itemSize,o=t.count;if(e!==null){if(n*o%e!==0)throw new Error("VertexAttributeTexture: overrideItemSize must divide evenly into buffer length.");t.itemSize=e,t.count=o*n/e;}let c=t.itemSize,s=t.count,a=t.normalized,r=t.array.constructor,p=r.BYTES_PER_ELEMENT,u=this._forcedType,f=c;if(u===null)switch(r){case Float32Array:u=FloatType;break;case Uint8Array:case Uint16Array:case Uint32Array:u=UnsignedIntType;break;case Int8Array:case Int16Array:case Int32Array:u=IntType;break}let l,d,b,A,v=Bo(c);switch(u){case FloatType:b=1,d=_o(c),a&&p===1?(A=r,v+="8",r===Uint8Array?l=UnsignedByteType:(l=ByteType,v+="_SNORM")):(A=Float32Array,v+="32F",l=FloatType);break;case IntType:v+=p*8+"I",b=a?Math.pow(2,r.BYTES_PER_ELEMENT*8-1):1,d=ai(c),p===1?(A=Int8Array,l=ByteType):p===2?(A=Int16Array,l=ShortType):(A=Int32Array,l=IntType);break;case UnsignedIntType:v+=p*8+"UI",b=a?Math.pow(2,r.BYTES_PER_ELEMENT*8-1):1,d=ai(c),p===1?(A=Uint8Array,l=UnsignedByteType):p===2?(A=Uint16Array,l=UnsignedShortType):(A=Uint32Array,l=UnsignedIntType);break}f===3&&(d===RGBAFormat||d===RGBAIntegerFormat)&&(f=4);let T=Math.ceil(Math.sqrt(s))||1,m=f*T*T,g=new A(m),x=t.normalized;t.normalized=false;for(let h=0;h<s;h++){let B=f*h;g[B]=t.getX(h)/b,c>=2&&(g[B+1]=t.getY(h)/b),c>=3&&(g[B+2]=t.getZ(h)/b,f===4&&(g[B+3]=1)),c>=4&&(g[B+3]=t.getW(h)/b);}t.normalized=x,this.internalFormat=v,this.format=d,this.type=l,this.image.width=T,this.image.height=T,this.image.data=g,this.needsUpdate=true,this.dispose(),t.itemSize=n,t.count=o;}},ge=class extends xe{static{a(this,"UIntVertexAttributeTexture");}constructor(){super(),this._forcedType=UnsignedIntType;}};var Te=class extends xe{static{a(this,"FloatVertexAttributeTexture");}constructor(){super(),this._forcedType=FloatType;}};var fi=class{static{a(this,"MeshBVHUniformStruct");}constructor(){this.index=new ge,this.position=new Te,this.bvhBounds=new DataTexture,this.bvhContents=new DataTexture,this._cachedIndexAttr=null,this.index.overrideItemSize=3;}updateFrom(t){let{geometry:e}=t;if(Fo(t,this.bvhBounds,this.bvhContents),this.position.updateFrom(e.attributes.position),t.indirect){let n=t._indirectBuffer;if(this._cachedIndexAttr===null||this._cachedIndexAttr.count!==n.length)if(e.index)this._cachedIndexAttr=e.index.clone();else {let o=Me(Ot(e));this._cachedIndexAttr=new BufferAttribute(o,1,false);}No(e,n,this._cachedIndexAttr),this.index.updateFrom(this._cachedIndexAttr);}else this.index.updateFrom(e.index);}dispose(){let{index:t,position:e,bvhBounds:n,bvhContents:o}=this;t&&t.dispose(),e&&e.dispose(),n&&n.dispose(),o&&o.dispose();}};function No(i,t,e){let n=e.array,o=i.index?i.index.array:null;for(let c=0,s=t.length;c<s;c++){let a=3*c,r=3*t[c];for(let p=0;p<3;p++)n[a+p]=o?o[r+p]:r+p;}}a(No,"dereferenceIndex");function Fo(i,t,e){let n=i._roots;if(n.length!==1)throw new Error("MeshBVHUniformStruct: Multi-root BVHs not supported.");let o=n[0],c=new Uint16Array(o),s=new Uint32Array(o),a=new Float32Array(o),r=o.byteLength/32,p=2*Math.ceil(Math.sqrt(r/2)),u=new Float32Array(4*p*p),f=Math.ceil(Math.sqrt(r)),l=new Uint32Array(2*f*f);for(let d=0;d<r;d++){let b=d*32/4,A=b*2,v=b;for(let T=0;T<3;T++)u[8*d+0+T]=a[v+0+T],u[8*d+4+T]=a[v+3+T];if(M(A,c)){let T=U(A,c),m=C(b,s),g=-65536|T;l[d*2+0]=g,l[d*2+1]=m;}else {let T=s[b+6],m=st(b,s);l[d*2+0]=m,l[d*2+1]=T;}}t.image.data=u,t.image.width=p,t.image.height=p,t.format=RGBAFormat,t.type=FloatType,t.internalFormat="RGBA32F",t.minFilter=NearestFilter,t.magFilter=NearestFilter,t.generateMipmaps=false,t.needsUpdate=true,t.dispose(),e.image.data=l,e.image.width=f,e.image.height=f,e.format=RGIntegerFormat,e.type=UnsignedIntType,e.internalFormat="RG32UI",e.minFilter=NearestFilter,e.magFilter=NearestFilter,e.generateMipmaps=false,e.needsUpdate=true,e.dispose();}a(Fo,"bvhToTextures");var en={};b(en,{bvh_distance_functions:()=>ui,bvh_ray_functions:()=>tn,bvh_struct_definitions:()=>pi,common_functions:()=>Qe});var Qe=`
2
+
3
+ // A stack of uint32 indices can can store the indices for
4
+ // a perfectly balanced tree with a depth up to 31. Lower stack
5
+ // depth gets higher performance.
6
+ //
7
+ // However not all trees are balanced. Best value to set this to
8
+ // is the trees max depth.
9
+ #ifndef BVH_STACK_DEPTH
10
+ #define BVH_STACK_DEPTH 60
11
+ #endif
12
+
13
+ #ifndef INFINITY
14
+ #define INFINITY 1e20
15
+ #endif
16
+
17
+ // Utilities
18
+ uvec4 uTexelFetch1D( usampler2D tex, uint index ) {
19
+
20
+ uint width = uint( textureSize( tex, 0 ).x );
21
+ uvec2 uv;
22
+ uv.x = index % width;
23
+ uv.y = index / width;
24
+
25
+ return texelFetch( tex, ivec2( uv ), 0 );
26
+
27
+ }
28
+
29
+ ivec4 iTexelFetch1D( isampler2D tex, uint index ) {
30
+
31
+ uint width = uint( textureSize( tex, 0 ).x );
32
+ uvec2 uv;
33
+ uv.x = index % width;
34
+ uv.y = index / width;
35
+
36
+ return texelFetch( tex, ivec2( uv ), 0 );
37
+
38
+ }
39
+
40
+ vec4 texelFetch1D( sampler2D tex, uint index ) {
41
+
42
+ uint width = uint( textureSize( tex, 0 ).x );
43
+ uvec2 uv;
44
+ uv.x = index % width;
45
+ uv.y = index / width;
46
+
47
+ return texelFetch( tex, ivec2( uv ), 0 );
48
+
49
+ }
50
+
51
+ vec4 textureSampleBarycoord( sampler2D tex, vec3 barycoord, uvec3 faceIndices ) {
52
+
53
+ return
54
+ barycoord.x * texelFetch1D( tex, faceIndices.x ) +
55
+ barycoord.y * texelFetch1D( tex, faceIndices.y ) +
56
+ barycoord.z * texelFetch1D( tex, faceIndices.z );
57
+
58
+ }
59
+
60
+ void ndcToCameraRay(
61
+ vec2 coord, mat4 cameraWorld, mat4 invProjectionMatrix,
62
+ out vec3 rayOrigin, out vec3 rayDirection
63
+ ) {
64
+
65
+ // get camera look direction and near plane for camera clipping
66
+ vec4 lookDirection = cameraWorld * vec4( 0.0, 0.0, - 1.0, 0.0 );
67
+ vec4 nearVector = invProjectionMatrix * vec4( 0.0, 0.0, - 1.0, 1.0 );
68
+ float near = abs( nearVector.z / nearVector.w );
69
+
70
+ // get the camera direction and position from camera matrices
71
+ vec4 origin = cameraWorld * vec4( 0.0, 0.0, 0.0, 1.0 );
72
+ vec4 direction = invProjectionMatrix * vec4( coord, 0.5, 1.0 );
73
+ direction /= direction.w;
74
+ direction = cameraWorld * direction - origin;
75
+
76
+ // slide the origin along the ray until it sits at the near clip plane position
77
+ origin.xyz += direction.xyz * near / dot( direction, lookDirection );
78
+
79
+ rayOrigin = origin.xyz;
80
+ rayDirection = direction.xyz;
81
+
82
+ }
83
+ `;var ui=`
84
+
85
+ float dot2( vec3 v ) {
86
+
87
+ return dot( v, v );
88
+
89
+ }
90
+
91
+ // https://www.shadertoy.com/view/ttfGWl
92
+ vec3 closestPointToTriangle( vec3 p, vec3 v0, vec3 v1, vec3 v2, out vec3 barycoord ) {
93
+
94
+ vec3 v10 = v1 - v0;
95
+ vec3 v21 = v2 - v1;
96
+ vec3 v02 = v0 - v2;
97
+
98
+ vec3 p0 = p - v0;
99
+ vec3 p1 = p - v1;
100
+ vec3 p2 = p - v2;
101
+
102
+ vec3 nor = cross( v10, v02 );
103
+
104
+ // method 2, in barycentric space
105
+ vec3 q = cross( nor, p0 );
106
+ float d = 1.0 / dot2( nor );
107
+ float u = d * dot( q, v02 );
108
+ float v = d * dot( q, v10 );
109
+ float w = 1.0 - u - v;
110
+
111
+ if( u < 0.0 ) {
112
+
113
+ w = clamp( dot( p2, v02 ) / dot2( v02 ), 0.0, 1.0 );
114
+ u = 0.0;
115
+ v = 1.0 - w;
116
+
117
+ } else if( v < 0.0 ) {
118
+
119
+ u = clamp( dot( p0, v10 ) / dot2( v10 ), 0.0, 1.0 );
120
+ v = 0.0;
121
+ w = 1.0 - u;
122
+
123
+ } else if( w < 0.0 ) {
124
+
125
+ v = clamp( dot( p1, v21 ) / dot2( v21 ), 0.0, 1.0 );
126
+ w = 0.0;
127
+ u = 1.0 - v;
128
+
129
+ }
130
+
131
+ barycoord = vec3( u, v, w );
132
+ return u * v1 + v * v2 + w * v0;
133
+
134
+ }
135
+
136
+ float distanceToTriangles(
137
+ // geometry info and triangle range
138
+ sampler2D positionAttr, usampler2D indexAttr, uint offset, uint count,
139
+
140
+ // point and cut off range
141
+ vec3 point, float closestDistanceSquared,
142
+
143
+ // outputs
144
+ inout uvec4 faceIndices, inout vec3 faceNormal, inout vec3 barycoord, inout float side, inout vec3 outPoint
145
+ ) {
146
+
147
+ bool found = false;
148
+ vec3 localBarycoord;
149
+ for ( uint i = offset, l = offset + count; i < l; i ++ ) {
150
+
151
+ uvec3 indices = uTexelFetch1D( indexAttr, i ).xyz;
152
+ vec3 a = texelFetch1D( positionAttr, indices.x ).rgb;
153
+ vec3 b = texelFetch1D( positionAttr, indices.y ).rgb;
154
+ vec3 c = texelFetch1D( positionAttr, indices.z ).rgb;
155
+
156
+ // get the closest point and barycoord
157
+ vec3 closestPoint = closestPointToTriangle( point, a, b, c, localBarycoord );
158
+ vec3 delta = point - closestPoint;
159
+ float sqDist = dot2( delta );
160
+ if ( sqDist < closestDistanceSquared ) {
161
+
162
+ // set the output results
163
+ closestDistanceSquared = sqDist;
164
+ faceIndices = uvec4( indices.xyz, i );
165
+ faceNormal = normalize( cross( a - b, b - c ) );
166
+ barycoord = localBarycoord;
167
+ outPoint = closestPoint;
168
+ side = sign( dot( faceNormal, delta ) );
169
+
170
+ }
171
+
172
+ }
173
+
174
+ return closestDistanceSquared;
175
+
176
+ }
177
+
178
+ float distanceSqToBounds( vec3 point, vec3 boundsMin, vec3 boundsMax ) {
179
+
180
+ vec3 clampedPoint = clamp( point, boundsMin, boundsMax );
181
+ vec3 delta = point - clampedPoint;
182
+ return dot( delta, delta );
183
+
184
+ }
185
+
186
+ float distanceSqToBVHNodeBoundsPoint( vec3 point, sampler2D bvhBounds, uint currNodeIndex ) {
187
+
188
+ uint cni2 = currNodeIndex * 2u;
189
+ vec3 boundsMin = texelFetch1D( bvhBounds, cni2 ).xyz;
190
+ vec3 boundsMax = texelFetch1D( bvhBounds, cni2 + 1u ).xyz;
191
+ return distanceSqToBounds( point, boundsMin, boundsMax );
192
+
193
+ }
194
+
195
+ // use a macro to hide the fact that we need to expand the struct into separate fields
196
+ #define bvhClosestPointToPoint( bvh, point, maxDistance, faceIndices, faceNormal, barycoord, side, outPoint ) _bvhClosestPointToPoint( bvh.position, bvh.index, bvh.bvhBounds, bvh.bvhContents, point, maxDistance, faceIndices, faceNormal, barycoord, side, outPoint )
197
+
198
+ float _bvhClosestPointToPoint(
199
+ // bvh info
200
+ sampler2D bvh_position, usampler2D bvh_index, sampler2D bvh_bvhBounds, usampler2D bvh_bvhContents,
201
+
202
+ // point to check
203
+ vec3 point, float maxDistance,
204
+
205
+ // output variables
206
+ inout uvec4 faceIndices, inout vec3 faceNormal, inout vec3 barycoord,
207
+ inout float side, inout vec3 outPoint
208
+ ) {
209
+
210
+ // stack needs to be twice as long as the deepest tree we expect because
211
+ // we push both the left and right child onto the stack every traversal
212
+ int pointer = 0;
213
+ uint stack[ BVH_STACK_DEPTH ];
214
+ stack[ 0 ] = 0u;
215
+
216
+ float closestDistanceSquared = maxDistance * maxDistance;
217
+ bool found = false;
218
+ while ( pointer > - 1 && pointer < BVH_STACK_DEPTH ) {
219
+
220
+ uint currNodeIndex = stack[ pointer ];
221
+ pointer --;
222
+
223
+ // check if we intersect the current bounds
224
+ float boundsHitDistance = distanceSqToBVHNodeBoundsPoint( point, bvh_bvhBounds, currNodeIndex );
225
+ if ( boundsHitDistance > closestDistanceSquared ) {
226
+
227
+ continue;
228
+
229
+ }
230
+
231
+ uvec2 boundsInfo = uTexelFetch1D( bvh_bvhContents, currNodeIndex ).xy;
232
+ bool isLeaf = bool( boundsInfo.x & 0xffff0000u );
233
+ if ( isLeaf ) {
234
+
235
+ uint count = boundsInfo.x & 0x0000ffffu;
236
+ uint offset = boundsInfo.y;
237
+ closestDistanceSquared = distanceToTriangles(
238
+ bvh_position, bvh_index, offset, count, point, closestDistanceSquared,
239
+
240
+ // outputs
241
+ faceIndices, faceNormal, barycoord, side, outPoint
242
+ );
243
+
244
+ } else {
245
+
246
+ uint leftIndex = currNodeIndex + 1u;
247
+ uint splitAxis = boundsInfo.x & 0x0000ffffu;
248
+ uint rightIndex = currNodeIndex + boundsInfo.y;
249
+ bool leftToRight = distanceSqToBVHNodeBoundsPoint( point, bvh_bvhBounds, leftIndex ) < distanceSqToBVHNodeBoundsPoint( point, bvh_bvhBounds, rightIndex );//rayDirection[ splitAxis ] >= 0.0;
250
+ uint c1 = leftToRight ? leftIndex : rightIndex;
251
+ uint c2 = leftToRight ? rightIndex : leftIndex;
252
+
253
+ // set c2 in the stack so we traverse it later. We need to keep track of a pointer in
254
+ // the stack while we traverse. The second pointer added is the one that will be
255
+ // traversed first
256
+ pointer ++;
257
+ stack[ pointer ] = c2;
258
+ pointer ++;
259
+ stack[ pointer ] = c1;
260
+
261
+ }
262
+
263
+ }
264
+
265
+ return sqrt( closestDistanceSquared );
266
+
267
+ }
268
+ `;var tn=`
269
+
270
+ #ifndef TRI_INTERSECT_EPSILON
271
+ #define TRI_INTERSECT_EPSILON 1e-5
272
+ #endif
273
+
274
+ // Raycasting
275
+ bool intersectsBounds( vec3 rayOrigin, vec3 rayDirection, vec3 boundsMin, vec3 boundsMax, out float dist ) {
276
+
277
+ // https://www.reddit.com/r/opengl/comments/8ntzz5/fast_glsl_ray_box_intersection/
278
+ // https://tavianator.com/2011/ray_box.html
279
+ vec3 invDir = 1.0 / rayDirection;
280
+
281
+ // find intersection distances for each plane
282
+ vec3 tMinPlane = invDir * ( boundsMin - rayOrigin );
283
+ vec3 tMaxPlane = invDir * ( boundsMax - rayOrigin );
284
+
285
+ // get the min and max distances from each intersection
286
+ vec3 tMinHit = min( tMaxPlane, tMinPlane );
287
+ vec3 tMaxHit = max( tMaxPlane, tMinPlane );
288
+
289
+ // get the furthest hit distance
290
+ vec2 t = max( tMinHit.xx, tMinHit.yz );
291
+ float t0 = max( t.x, t.y );
292
+
293
+ // get the minimum hit distance
294
+ t = min( tMaxHit.xx, tMaxHit.yz );
295
+ float t1 = min( t.x, t.y );
296
+
297
+ // set distance to 0.0 if the ray starts inside the box
298
+ dist = max( t0, 0.0 );
299
+
300
+ return t1 >= dist;
301
+
302
+ }
303
+
304
+ bool intersectsTriangle(
305
+ vec3 rayOrigin, vec3 rayDirection, vec3 a, vec3 b, vec3 c,
306
+ out vec3 barycoord, out vec3 norm, out float dist, out float side
307
+ ) {
308
+
309
+ // https://stackoverflow.com/questions/42740765/intersection-between-line-and-triangle-in-3d
310
+ vec3 edge1 = b - a;
311
+ vec3 edge2 = c - a;
312
+ norm = cross( edge1, edge2 );
313
+
314
+ float det = - dot( rayDirection, norm );
315
+ float invdet = 1.0 / det;
316
+
317
+ vec3 AO = rayOrigin - a;
318
+ vec3 DAO = cross( AO, rayDirection );
319
+
320
+ vec4 uvt;
321
+ uvt.x = dot( edge2, DAO ) * invdet;
322
+ uvt.y = - dot( edge1, DAO ) * invdet;
323
+ uvt.z = dot( AO, norm ) * invdet;
324
+ uvt.w = 1.0 - uvt.x - uvt.y;
325
+
326
+ // set the hit information
327
+ barycoord = uvt.wxy; // arranged in A, B, C order
328
+ dist = uvt.z;
329
+ side = sign( det );
330
+ norm = side * normalize( norm );
331
+
332
+ // add an epsilon to avoid misses between triangles
333
+ uvt += vec4( TRI_INTERSECT_EPSILON );
334
+
335
+ return all( greaterThanEqual( uvt, vec4( 0.0 ) ) );
336
+
337
+ }
338
+
339
+ bool intersectTriangles(
340
+ // geometry info and triangle range
341
+ sampler2D positionAttr, usampler2D indexAttr, uint offset, uint count,
342
+
343
+ // ray
344
+ vec3 rayOrigin, vec3 rayDirection,
345
+
346
+ // outputs
347
+ inout float minDistance, inout uvec4 faceIndices, inout vec3 faceNormal, inout vec3 barycoord,
348
+ inout float side, inout float dist
349
+ ) {
350
+
351
+ bool found = false;
352
+ vec3 localBarycoord, localNormal;
353
+ float localDist, localSide;
354
+ for ( uint i = offset, l = offset + count; i < l; i ++ ) {
355
+
356
+ uvec3 indices = uTexelFetch1D( indexAttr, i ).xyz;
357
+ vec3 a = texelFetch1D( positionAttr, indices.x ).rgb;
358
+ vec3 b = texelFetch1D( positionAttr, indices.y ).rgb;
359
+ vec3 c = texelFetch1D( positionAttr, indices.z ).rgb;
360
+
361
+ if (
362
+ intersectsTriangle( rayOrigin, rayDirection, a, b, c, localBarycoord, localNormal, localDist, localSide )
363
+ && localDist < minDistance
364
+ ) {
365
+
366
+ found = true;
367
+ minDistance = localDist;
368
+
369
+ faceIndices = uvec4( indices.xyz, i );
370
+ faceNormal = localNormal;
371
+
372
+ side = localSide;
373
+ barycoord = localBarycoord;
374
+ dist = localDist;
375
+
376
+ }
377
+
378
+ }
379
+
380
+ return found;
381
+
382
+ }
383
+
384
+ bool intersectsBVHNodeBounds( vec3 rayOrigin, vec3 rayDirection, sampler2D bvhBounds, uint currNodeIndex, out float dist ) {
385
+
386
+ uint cni2 = currNodeIndex * 2u;
387
+ vec3 boundsMin = texelFetch1D( bvhBounds, cni2 ).xyz;
388
+ vec3 boundsMax = texelFetch1D( bvhBounds, cni2 + 1u ).xyz;
389
+ return intersectsBounds( rayOrigin, rayDirection, boundsMin, boundsMax, dist );
390
+
391
+ }
392
+
393
+ // use a macro to hide the fact that we need to expand the struct into separate fields
394
+ #define bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist ) _bvhIntersectFirstHit( bvh.position, bvh.index, bvh.bvhBounds, bvh.bvhContents, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist )
395
+
396
+ bool _bvhIntersectFirstHit(
397
+ // bvh info
398
+ sampler2D bvh_position, usampler2D bvh_index, sampler2D bvh_bvhBounds, usampler2D bvh_bvhContents,
399
+
400
+ // ray
401
+ vec3 rayOrigin, vec3 rayDirection,
402
+
403
+ // output variables split into separate variables due to output precision
404
+ inout uvec4 faceIndices, inout vec3 faceNormal, inout vec3 barycoord,
405
+ inout float side, inout float dist
406
+ ) {
407
+
408
+ // stack needs to be twice as long as the deepest tree we expect because
409
+ // we push both the left and right child onto the stack every traversal
410
+ int pointer = 0;
411
+ uint stack[ BVH_STACK_DEPTH ];
412
+ stack[ 0 ] = 0u;
413
+
414
+ float triangleDistance = INFINITY;
415
+ bool found = false;
416
+ while ( pointer > - 1 && pointer < BVH_STACK_DEPTH ) {
417
+
418
+ uint currNodeIndex = stack[ pointer ];
419
+ pointer --;
420
+
421
+ // check if we intersect the current bounds
422
+ float boundsHitDistance;
423
+ if (
424
+ ! intersectsBVHNodeBounds( rayOrigin, rayDirection, bvh_bvhBounds, currNodeIndex, boundsHitDistance )
425
+ || boundsHitDistance > triangleDistance
426
+ ) {
427
+
428
+ continue;
429
+
430
+ }
431
+
432
+ uvec2 boundsInfo = uTexelFetch1D( bvh_bvhContents, currNodeIndex ).xy;
433
+ bool isLeaf = bool( boundsInfo.x & 0xffff0000u );
434
+
435
+ if ( isLeaf ) {
436
+
437
+ uint count = boundsInfo.x & 0x0000ffffu;
438
+ uint offset = boundsInfo.y;
439
+
440
+ found = intersectTriangles(
441
+ bvh_position, bvh_index, offset, count,
442
+ rayOrigin, rayDirection, triangleDistance,
443
+ faceIndices, faceNormal, barycoord, side, dist
444
+ ) || found;
445
+
446
+ } else {
447
+
448
+ uint leftIndex = currNodeIndex + 1u;
449
+ uint splitAxis = boundsInfo.x & 0x0000ffffu;
450
+ uint rightIndex = currNodeIndex + boundsInfo.y;
451
+
452
+ bool leftToRight = rayDirection[ splitAxis ] >= 0.0;
453
+ uint c1 = leftToRight ? leftIndex : rightIndex;
454
+ uint c2 = leftToRight ? rightIndex : leftIndex;
455
+
456
+ // set c2 in the stack so we traverse it later. We need to keep track of a pointer in
457
+ // the stack while we traverse. The second pointer added is the one that will be
458
+ // traversed first
459
+ pointer ++;
460
+ stack[ pointer ] = c2;
461
+
462
+ pointer ++;
463
+ stack[ pointer ] = c1;
464
+
465
+ }
466
+
467
+ }
468
+
469
+ return found;
470
+
471
+ }
472
+ `;var pi=`
473
+ struct BVH {
474
+
475
+ usampler2D index;
476
+ sampler2D position;
477
+
478
+ sampler2D bvhBounds;
479
+ usampler2D bvhContents;
480
+
481
+ };
482
+ `;export{Qn as a,xa as b,ge as c,Te as d,fi as e,en as f};