scad-gltf 0.4.0 → 0.5.1

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.
@@ -1,4 +1,4 @@
1
- import{$ as e,A as t,B as n,C as r,D as i,E as a,F as o,G as s,H as c,I as l,J as u,K as d,L as f,M as p,N as m,O as h,P as g,Q as _,R as v,S as y,T as b,U as x,V as S,W as C,X as ee,Y as te,Z as ne,_ as w,a as re,at as ie,b as ae,c as oe,ct as se,d as ce,dt as le,et as ue,f as de,ft as fe,g as pe,h as me,ht as he,i as ge,it as _e,j as ve,k as ye,l as be,lt as T,m as xe,mt as Se,n as Ce,nt as we,o as Te,ot as Ee,p as De,pt as Oe,q as ke,r as Ae,rt as je,s as Me,st as Ne,t as Pe,tt as Fe,u as Ie,ut as E,v as Le,w as Re,x as ze,y as Be,z as Ve}from"./OutputPass-C3DZe46t.js";import{i as He,n as Ue,r as We,t as Ge}from"./prompt-ui-BjZlMzjn.js";var Ke=1.25,qe=65535,Je=-65536,Ye=2**-24,Xe=Symbol(`SKIP_GENERATION`),Ze={strategy:0,maxDepth:40,targetLeafSize:10,useSharedArrayBuffer:!1,setBoundingBox:!0,onProgress:null,indirect:!1,verbose:!0,range:null,[Xe]:!1};function D(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 Qe(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 $e(e,t){t.set(e)}function et(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]=r<i?r:i,r=e[o],i=t[o],n[o]=r>i?r:i}}function tt(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;o<n[r]&&(n[r]=o),s>n[r+3]&&(n[r+3]=s)}}function nt(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]===qe}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 rt(e,t){return t[e+7]}function N(e){return e}function it(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;r<i;r+=6){let t=e[r+0],n=e[r+1],i=t-n,_=t+n;i<a&&(a=i),_>c&&(c=_),t<d&&(d=t),t>m&&(m=t);let v=e[r+2],y=e[r+3],b=v-y,x=v+y;b<o&&(o=b),x>l&&(l=x),v<f&&(f=v),v>h&&(h=v);let S=e[r+4],C=e[r+5],ee=S-C,te=S+C;ee<s&&(s=ee),te>u&&(u=te),S<p&&(p=S),S>g&&(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 at=32,ot=(e,t)=>e.candidate-t.candidate,st=Array(at).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),ct=new Float32Array(6);function lt(e,t,n,r,i,a){let o=-1,s=0;if(a===0)o=Qe(t),o!==-1&&(s=(t[o]+t[o+3])/2);else if(a===1)o=Qe(e),o!==-1&&(s=ut(n,r,i,o));else if(a===2){let a=nt(e),c=Ke*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)/at;if(i<at/4){let t=[...st];t.length=i;let r=0;for(let i=u;i<d;i+=6,r++){let a=t[r];a.candidate=n[i+2*e],a.count=0;let{bounds:o,leftCacheBounds:s,rightCacheBounds:c}=a;for(let e=0;e<3;e++)c[e]=1/0,c[e+3]=-1/0,s[e]=1/0,s[e+3]=-1/0,o[e]=1/0,o[e+3]=-1/0;tt(i,n,o)}t.sort(ot);let l=i;for(let e=0;e<l;e++){let n=t[e];for(;e+1<l&&t[e+1].candidate===n.candidate;)t.splice(e+1,1),l--}for(let r=u;r<d;r+=6){let i=n[r+2*e];for(let e=0;e<l;e++){let a=t[e];i>=a.candidate?tt(r,n,a.rightCacheBounds):(tt(r,n,a.leftCacheBounds),a.count++)}}for(let n=0;n<l;n++){let r=t[n],l=r.count,u=i-r.count,d=r.leftCacheBounds,f=r.rightCacheBounds,p=0;l!==0&&(p=nt(d)/a);let m=0;u!==0&&(m=nt(f)/a);let h=1+Ke*(p*l+m*u);h<c&&(o=e,c=h,s=r.candidate)}}else{for(let e=0;e<at;e++){let t=st[e];t.count=0,t.candidate=r+l+e*l;let n=t.bounds;for(let e=0;e<3;e++)n[e]=1/0,n[e+3]=-1/0}for(let t=u;t<d;t+=6){let i=~~((n[t+2*e]-r)/l);i>=at&&(i=31);let a=st[i];a.count++,tt(t,n,a.bounds)}let t=st[31];$e(t.bounds,t.rightCacheBounds);for(let e=30;e>=0;e--){let t=st[e],n=st[e+1];et(t.bounds,n.rightCacheBounds,t.rightCacheBounds)}let f=0;for(let t=0;t<31;t++){let n=st[t],r=n.count,l=n.bounds,u=st[t+1].rightCacheBounds;r!==0&&(f===0?$e(l,ct):et(l,ct,ct)),f+=r;let d=0,p=0;f!==0&&(d=nt(ct)/a);let m=i-f;m!==0&&(p=nt(u)/a);let h=1+Ke*(d*f+p*m);h<c&&(o=e,c=h,s=n.candidate)}}}}else console.warn(`BVH: Invalid build strategy value ${a} used.`);return{axis:o,pos:s}}function ut(e,t,n,r){let i=0,a=e.offset;for(let o=t,s=t+n;o<s;o++)i+=e[(o-a)*6+r*2];return i/n}var dt=class{constructor(){this.boundingData=new Float32Array(6)}};function ft(e,t,n,r,i,a){let o=r,s=r+i-1,c=a.pos,l=a.axis*2,u=n.offset||0;for(;;){for(;o<=s&&n[(o-u)*6+l]<c;)o++;for(;o<=s&&n[(s-u)*6+l]>=c;)s--;if(o<s){for(let n=0;n<t;n++){let r=e[o*t+n];e[o*t+n]=e[s*t+n],e[s*t+n]=r}for(let e=0;e<6;e++){let t=o-u,r=s-u,i=n[t*6+e];n[t*6+e]=n[r*6+e],n[r*6+e]=i}o++,s--}else return o}}var pt,mt,ht,gt,_t=2**32;function vt(e){return`count`in e?1:1+vt(e.left)+vt(e.right)}function yt(e,t,n){return pt=new Float32Array(n),mt=new Uint32Array(n),ht=new Uint16Array(n),gt=new Uint8Array(n),bt(e,t)}function bt(e,t){let n=e/4,r=e/2,i=`count`in t,a=t.boundingData;for(let e=0;e<6;e++)pt[n+e]=a[e];if(i)return t.buffer?(gt.set(new Uint8Array(t.buffer),e),e+t.buffer.byteLength):(mt[n+6]=t.offset,ht[r+14]=t.count,ht[r+15]=qe,e+32);{let{left:r,right:i,splitAxis:a}=t,o=bt(e+32,r),s=e/32,c=o/32-s;if(c>_t)throw Error(`MeshBVH: Cannot store relative child node offset greater than 32 bits.`);return mt[n+6]=c,mt[n+7]=a,bt(o,i)}}function xt(e,t,n,r,i,a){let{maxDepth:o,verbose:s,targetLeafSize:c,_strictLeafSize:l=1/0,strategy:u,onProgress:d}=i,f=e.primitiveBuffer,p=e.primitiveBufferStride,m=new Float32Array(6),h=!1,g=new dt;return it(t,n,r,g.boundingData,m),v(g,n,r,m),g;function _(e){d&&d((e-a.offset)/a.count)}function v(e,n,r,i=null,a=0){!h&&a>=o&&(h=!0,s&&console.warn(`BVH: Max depth of ${o} reached when generating BVH. Consider increasing maxDepth.`));let d=r>l;if(r<=c&&!d||a>=o)return _(n+r),e.offset=n,e.count=r,e;let g=lt(e.boundingData,i,t,n,r,u),y=g.axis===-1?-1:ft(f,p,t,n,r,g);if(g.axis===-1||y===n||y===n+r){if(!d)return _(n+r),e.offset=n,e.count=r,e;g.axis=Math.max(0,Qe(e.boundingData)),y=n+Math.max(1,Math.floor(r/2))}e.splitAxis=g.axis;let b=new dt,x=n,S=y-n;e.left=b,it(t,x,S,b.boundingData,m),v(b,x,S,m,a+1);let C=new dt,ee=y,te=r-S;return e.right=C,it(t,ee,te,C.boundingData,m),v(C,ee,te,m,a+1),e}}function St(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=xt(e,s,r.offset,r.count,t,o),a=vt(i),c=new n(32*a);return yt(0,i,c),c})}var Ct=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)}},P=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())}}},wt,Tt,Et=[],Dt=new Ct(()=>new De);function Ot(e,t,n,r,i,a){wt=Dt.getPrimitive(),Tt=Dt.getPrimitive(),Et.push(wt,Tt),P.setBuffer(e._roots[t]);let o=kt(0,e.geometry,n,r,i,a);P.clearBuffer(),Dt.releasePrimitive(wt),Dt.releasePrimitive(Tt),Et.pop(),Et.pop();let s=Et.length;return s>0&&(Tt=Et[s-1],wt=Et[s-2]),o}function kt(e,t,n,r,i=null,a=0,o=0){let{float32Array:s,uint16Array:c,uint32Array:l}=P,u=e*2;if(O(u,c)){let t=k(e,l),n=A(u,c);return D(N(e),s,wt),r(t,n,!1,o,a+e/8,wt)}{let u=j(e),d=M(e,l),f=u,p=d,m,h,g,_;if(i&&(g=wt,_=Tt,D(N(f),s,g),D(N(p),s,_),m=i(g),h=i(_),h<m)){f=d,p=u;let e=m;m=h,h=e,g=_}g||(g=wt,D(N(f),s,g));let v=O(f*2,c),y=n(g,v,m,o+1,a+f/8),b;if(y===2){let e=ee(f);b=r(e,te(f)-e,!0,o+1,a+f/8,g)}else b=y&&kt(f,t,n,r,i,a,o+1);if(b)return!0;_=Tt,D(N(p),s,_);let x=O(p*2,c),S=n(_,x,h,o+1,a+p/8),C;if(S===2){let e=ee(p);C=r(e,te(p)-e,!0,o+1,a+p/8,_)}else C=S&&kt(p,t,n,r,i,a,o+1);return!!C;function ee(e){let{uint16Array:t,uint32Array:n}=P,r=e*2;for(;!O(r,t);)e=j(e),r=e*2;return k(e,n)}function te(e){let{uint16Array:t,uint32Array:n}=P,r=e*2;for(;!O(r,t);)e=M(e,n),r=e*2;return k(e,n)+A(r,t)}}}var At=new P.constructor,jt=new P.constructor,Mt=new Ct(()=>new De),Nt=new De,Pt=new De,Ft=new De,It=new De,Lt=!1;function Rt(e,t,n,r){if(Lt)throw Error(`MeshBVH: Recursive calls to bvhcast not supported.`);Lt=!0;let i=e._roots,a=t._roots,o,s=0,c=0,l=new f().copy(n).invert();for(let e=0,t=i.length;e<t;e++){At.setBuffer(i[e]),c=0;let t=Mt.getPrimitive();D(N(0),At.float32Array,t),t.applyMatrix4(l);for(let e=0,i=a.length;e<i&&(jt.setBuffer(a[e]),o=zt(0,0,n,l,r,s,c,0,0,t),jt.clearBuffer(),c+=a[e].byteLength/32,!o);e++);if(Mt.releasePrimitive(t),At.clearBuffer(),s+=i[e].byteLength/32,o)break}return Lt=!1,o}function zt(e,t,n,r,i,a=0,o=0,s=0,c=0,l=null,u=!1){let d,f;u?(d=jt,f=At):(d=At,f=jt);let p=d.float32Array,m=d.uint32Array,h=d.uint16Array,g=f.float32Array,_=f.uint32Array,v=f.uint16Array,y=e*2,b=t*2,x=O(y,h),S=O(b,v),C=!1;if(S&&x)C=u?i(k(t,_),A(t*2,v),k(e,m),A(e*2,h),c,o+t/8,s,a+e/8):i(k(e,m),A(e*2,h),k(t,_),A(t*2,v),s,a+e/8,c,o+t/8);else if(S){let l=Mt.getPrimitive();D(N(t),g,l),l.applyMatrix4(n);let d=j(e),f=M(e,m);D(N(d),p,Nt),D(N(f),p,Pt);let h=l.intersectsBox(Nt),_=l.intersectsBox(Pt);C=h&&zt(t,d,r,n,i,o,a,c,s+1,l,!u)||_&&zt(t,f,r,n,i,o,a,c,s+1,l,!u),Mt.releasePrimitive(l)}else{let d=j(t),f=M(t,_);D(N(d),g,Ft),D(N(f),g,It);let h=l.intersectsBox(Ft),v=l.intersectsBox(It);if(h&&v)C=zt(e,d,n,r,i,a,o,s,c+1,l,u)||zt(e,f,n,r,i,a,o,s,c+1,l,u);else if(h){if(x)C=zt(e,d,n,r,i,a,o,s,c+1,l,u);else{let t=Mt.getPrimitive();t.copy(Ft).applyMatrix4(n);let l=j(e),f=M(e,m);D(N(l),p,Nt),D(N(f),p,Pt);let h=t.intersectsBox(Nt),g=t.intersectsBox(Pt);C=h&&zt(d,l,r,n,i,o,a,c,s+1,t,!u)||g&&zt(d,f,r,n,i,o,a,c,s+1,t,!u),Mt.releasePrimitive(t)}}else if(v){if(x)C=zt(e,f,n,r,i,a,o,s,c+1,l,u);else{let t=Mt.getPrimitive();t.copy(It).applyMatrix4(n);let l=j(e),d=M(e,m);D(N(l),p,Nt),D(N(d),p,Pt);let h=t.intersectsBox(Nt),g=t.intersectsBox(Pt);C=h&&zt(f,l,r,n,i,o,a,c,s+1,t,!u)||g&&zt(f,d,r,n,i,o,a,c,s+1,t,!u),Mt.releasePrimitive(t)}}}return C}var Bt=new class{constructor(){let e=null,t=null,n=null,r=!1;this.root=null,this.buffer=null,this.uint32Array=null,this.uint16Array=null,this.setBVH=(i,a)=>{if(r)throw Error(`BVHTraversalHelper: cannot call setBVH during an active traversal.`);this.root=a,this.buffer=e=i._roots[a],this.uint16Array=n=new Uint16Array(e),this.uint32Array=t=new Uint32Array(e)},this.reset=()=>{this.root=null,this.buffer=e=null,this.uint16Array=n=null,this.uint32Array=t=null},this.getRangeStart=e=>{let r=e*2;for(;!O(r,n);)e=j(e),r=e*2;return k(e,t)},this.getRangeEnd=e=>{let r=e*2;for(;!O(r,n);)e=M(e,t),r=e*2;return k(e,t)+A(r,n)};let i=(e,r,a)=>{let o=O(r*2,n);if(!e(a,o,r)&&!o){let n=j(r),o=M(r,t);i(e,n,a+1),i(e,o,a+1)}};this.traverseBuffer=e=>{if(r)throw Error(`BVHTraversalHelper: cannot start a traversal during an active traversal.`);r=!0;try{i(e,0,0)}finally{r=!1}},this.traverse=r=>{this.traverseBuffer((i,a,o)=>{if(a){let s=o*2,c=t[o+6],l=n[s+14];return r(i,a,new Float32Array(e,o*4,6),c,l)}{let n=rt(o,t);return r(i,a,new Float32Array(e,o*4,6),n)}})}}},Vt=new De,Ht=new Float32Array(6),Ut=class{constructor(){this._roots=null,this.primitiveBuffer=null,this.primitiveBufferStride=null}init(e){e={...Ze,...e},`maxLeafSize`in e&&(console.warn(`BVH: "maxLeafSize" option has been deprecated. Use "targetLeafSize", instead.`),e={...e,targetLeafSize:e.maxLeafSize}),St(this,e)}getRootRanges(){throw Error(`BVH: getRootRanges() not implemented`)}writePrimitiveBounds(){throw Error(`BVH: writePrimitiveBounds() not implemented`)}writePrimitiveRangeBounds(e,t,n,r){let i=1/0,a=1/0,o=1/0,s=-1/0,c=-1/0,l=-1/0;for(let n=e,r=e+t;n<r;n++){this.writePrimitiveBounds(n,Ht,0);let[e,t,r,u,d,f]=Ht;e<i&&(i=e),u>s&&(s=u),t<a&&(a=t),d>c&&(c=d),r<o&&(o=r),f>l&&(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<a;i++){this.writePrimitiveBounds(i,Ht,0);let[e,t,a,o,s,c]=Ht,l=(e+o)/2,u=(t+s)/2,d=(a+c)/2,f=(o-e)/2,p=(s-t)/2,m=(c-a)/2,h=(i-r)*6;n[h+0]=l,n[h+1]=f+(Math.abs(l)+f)*Ye,n[h+2]=u,n[h+3]=p+(Math.abs(u)+p)*Ye,n[h+4]=d,n[h+5]=m+(Math.abs(d)+m)*Ye}return n}shiftPrimitiveOffsets(e){let t=this._indirectBuffer;if(t)for(let n=0,r=t.length;n<r;n++)t[n]+=e;else{let t=this._roots;for(let n=0;n<t.length;n++){let r=t[n],i=new Uint32Array(r),a=new Uint16Array(r),o=r.byteLength/32;for(let t=0;t<o;t++){let n=8*t;O(2*n,a)&&(i[n+6]+=e)}}}}traverse(e,t=0){Bt.setBVH(this,t),Bt.traverse(e),Bt.reset()}refit(){let e=this._roots;for(let t=0,n=e.length;t<n;t++){let n=e[t],r=new Uint32Array(n),i=new Uint16Array(n),a=new Float32Array(n),o=n.byteLength/32;for(let e=o-1;e>=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,Ht,0),a.set(Ht,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]=i<s?i:s,a[t+r+3]=o>c?o:c}}}}}getBoundingBox(e){return e.makeEmpty(),this._roots.forEach(t=>{D(0,new Float32Array(t),Vt),e.union(Vt)}),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;e<i;e++){let i=l[e];if(s=Ot(this,e,n,r,t,c),s)break;c+=i.byteLength/32}return s}bvhcast(e,t,n){let{intersectsRanges:r}=n;return Rt(this,e,t,r)}};function Wt(){return typeof SharedArrayBuffer<`u`}function Gt(e){return e.index?e.index.count:e.attributes.position.count}function Kt(e){return Gt(e)/3}function qt(e,t=ArrayBuffer){return e>65535?new Uint32Array(new t(4*e)):new Uint16Array(new t(2*e))}function Jt(e,t){if(!e.index){let n=e.attributes.position.count,r=qt(n,t.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer);e.setIndex(new xe(r,1));for(let e=0;e<n;e++)r[e]=e}}function Yt(e,t,n){let r=Gt(e)/n,i=t||e.drawRange,a=i.start/n,o=(i.start+i.count)/n,s=Math.max(0,a),c=Math.min(r,o)-s;return{offset:Math.floor(s),count:Math.floor(c)}}function Xt(e,t){return e.groups.map(e=>({offset:e.start/t,count:e.count/t}))}function Zt(e,t,n){let r=Yt(e,t,n),i=Xt(e,n);if(!i.length)return[r];let a=[],o=r.offset,s=r.offset+r.count,c=Gt(e)/n,l=[];for(let e of i){let{offset:t,count:n}=e,r=t,i=t+(isFinite(n)?n:c-t);r<s&&i>o&&(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 Qt(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;t<e.length;t++){let{offset:n,count:r}=e[t];for(let e=0;e<r;e++)s[c+e]=n+e;c+=r}return s}var $t=class extends Ut{get indirect(){return!!this._indirectBuffer}get primitiveStride(){return null}get primitiveBufferStride(){return this.indirect?1:this.primitiveStride}set primitiveBufferStride(e){}get primitiveBuffer(){return this.indirect?this._indirectBuffer:this.geometry.index.array}set primitiveBuffer(e){}constructor(e,t={}){if(!e.isBufferGeometry)throw Error(`BVH: Only BufferGeometries are supported.`);if(e.index&&e.index.isInterleavedBufferAttribute)throw Error(`BVH: InterleavedBufferAttribute is not supported for the index attribute.`);if(t.useSharedArrayBuffer&&!Wt())throw Error(`BVH: SharedArrayBuffer is not available.`);super(),this.geometry=e,this.resolvePrimitiveIndex=t.indirect?e=>this._indirectBuffer[e]:e=>e,this.primitiveBuffer=null,this.primitiveBufferStride=null,this._indirectBuffer=null,t={...Ze,...t},t[Xe]||this.init(t)}init(e){let{geometry:t,primitiveStride:n}=this;if(e.indirect){let r=Qt(Zt(t,e.range,n),e.useSharedArrayBuffer);this._indirectBuffer=r}else Jt(t,e);super.init(e),!t.boundingBox&&e.setBoundingBox&&(t.boundingBox=this.getBoundingBox(new De))}getRootRanges(e){return this.indirect?[{offset:0,count:this._indirectBuffer.length}]:Zt(this.geometry,e,this.primitiveStride)}raycastObject3D(){throw Error(`BVH: raycastObject3D() not implemented`)}},en=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;i<a;i++){let a=e[i][t];n=a<n?a:n,r=a>r?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;i<a;i++){let a=t[i],o=e.dot(a);n=o<n?o:n,r=o>r?o:r}this.min=n,this.max=r}isSeparated(e){return this.min>e.max||e.min>this.max}};en.prototype.setFromBox=(function(){let e=new E;return function(t,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++){e.x=r.x*n+i.x*(1-n),e.y=r.y*s+i.y*(1-s),e.z=r.z*c+i.z*(1-c);let l=t.dot(e);a=Math.min(l,a),o=Math.max(l,o)}this.min=a,this.max=o}})();var tn=(function(){let e=new E,t=new E,n=new E;return function(r,i,a){let o=r.start,s=e,c=i.start,l=t;n.subVectors(o,c),e.subVectors(r.end,r.start),t.subVectors(i.end,i.start);let u=n.dot(l),d=l.dot(s),f=l.dot(l),p=n.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}})(),nn=(function(){let e=new T,t=new E,n=new E;return function(r,i,a,o){tn(r,i,e);let s=e.x,c=e.y;if(s>=0&&s<=1&&c>=0&&c<=1){r.at(s,a),i.at(c,o);return}if(s>=0&&s<=1){c<0?i.at(0,o):i.at(1,o),r.closestPointToPoint(o,!0,a);return}if(c>=0&&c<=1){s<0?r.at(0,a):r.at(1,a),i.closestPointToPoint(a,!0,o);return}{let e;e=s<0?r.start:r.end;let l;l=c<0?i.start:i.end;let u=t,d=n;if(r.closestPointToPoint(l,!0,t),i.closestPointToPoint(e,!0,n),u.distanceToSquared(l)<=d.distanceToSquared(e)){a.copy(u),o.copy(l);return}a.copy(e),o.copy(d);return}}})(),rn=(function(){let e=new E,t=new E,n=new x,r=new p;return function(i,a){let{radius:o,center:s}=i,{a:c,b:l,c:u}=a;if(r.start=c,r.end=l,r.closestPointToPoint(s,!0,e).distanceTo(s)<=o||(r.start=c,r.end=u,r.closestPointToPoint(s,!0,e).distanceTo(s)<=o)||(r.start=l,r.end=u,r.closestPointToPoint(s,!0,e).distanceTo(s)<=o))return!0;let d=a.getPlane(n);if(Math.abs(d.distanceToPoint(s))<=o){let e=d.projectPoint(s,t);if(a.containsPoint(e))return!0}return!1}})(),an=[`x`,`y`,`z`],on=1e-15,sn=on*on;function cn(e){return Math.abs(e)<on}var ln=class extends ie{constructor(...e){super(...e),this.isExtendedTriangle=!0,this.satAxes=[,,,,].fill().map(()=>new E),this.satBounds=[,,,,].fill().map(()=>new en),this.points=[this.a,this.b,this.c],this.plane=new x,this.isDegenerateIntoSegment=!1,this.isDegenerateIntoPoint=!1,this.degenerateSegment=new p,this.needsUpdate=!0}intersectsSphere(e){return rn(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,m<on?h<on||g<on?this.isDegenerateIntoPoint=!0:(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(e),this.degenerateSegment.end.copy(n)):h<on?g<on?this.isDegenerateIntoPoint=!0:(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(t),this.degenerateSegment.end.copy(e)):g<on&&(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(n),this.degenerateSegment.end.copy(t)),this.plane.setFromNormalAndCoplanarPoint(o,e),this.needsUpdate=!1}};ln.prototype.closestPointToSegment=(function(){let e=new E,t=new E,n=new p;return function(r,i=null,a=null){let{start:o,end:s}=r,c=this.points,l,u=1/0;for(let o=0;o<3;o++){let s=(o+1)%3;n.start.copy(c[o]),n.end.copy(c[s]),nn(n,r,e,t),l=e.distanceToSquared(t),l<u&&(u=l,i&&i.copy(e),a&&a.copy(t))}return this.closestPointToPoint(o,e),l=o.distanceToSquared(e),l<u&&(u=l,i&&i.copy(e),a&&a.copy(o)),this.closestPointToPoint(s,e),l=s.distanceToSquared(e),l<u&&(u=l,i&&i.copy(e),a&&a.copy(s)),Math.sqrt(u)}})(),ln.prototype.intersectsTriangle=(function(){let e=new ln,t=new en,n=new en,r=new E,i=new E,a=new E,o=new E,s=new p,c=new p,l=new E,u=new T,d=new T;function f(e,i,a,s){let c=r;!e.isDegenerateIntoPoint&&!e.isDegenerateIntoSegment?c.copy(e.plane.normal):c.copy(i.plane.normal);let l=e.satBounds,u=e.satAxes;for(let r=1;r<4;r++){let a=l[r],s=u[r];if(t.setFromPoints(s,i.points),a.isSeparated(t)||(o.copy(c).cross(s),t.setFromPoints(o,e.points),n.setFromPoints(o,i.points),t.isSeparated(n)))return!1}let d=i.satBounds,f=i.satAxes;for(let r=1;r<4;r++){let a=d[r],s=f[r];if(t.setFromPoints(s,e.points),a.isSeparated(t)||(o.crossVectors(c,s),t.setFromPoints(o,e.points),n.setFromPoints(o,i.points),t.isSeparated(n)))return!1}return a&&(s||console.warn(`ExtendedTriangle.intersectsTriangle: Triangles are coplanar which does not support an output edge. Setting edge to 0, 0, 0.`),a.start.set(0,0,0),a.end.set(0,0,0)),!0}function m(e,t,n,r,i,a,o,s,c,l,u){let d=o/(o-s);l.x=r+(i-r)*d,u.start.subVectors(t,e).multiplyScalar(d).add(e),d=o/(o-c),l.y=r+(a-r)*d,u.end.subVectors(n,e).multiplyScalar(d).add(e)}function h(e,t,n,r,i,a,o,s,c,l,u){if(i>0)m(e.c,e.a,e.b,r,t,n,c,o,s,l,u);else if(a>0)m(e.b,e.a,e.c,n,t,r,s,o,c,l,u);else if(s*c>0||o!=0)m(e.a,e.b,e.c,t,n,r,o,s,c,l,u);else if(s!=0)m(e.b,e.a,e.c,n,t,r,s,o,c,l,u);else if(c!=0)m(e.c,e.a,e.b,r,t,n,c,o,s,l,u);else return!0;return!1}function g(e,t,n,i){let a=t.degenerateSegment,o=e.plane.distanceToPoint(a.start),s=e.plane.distanceToPoint(a.end);return cn(o)?cn(s)?f(e,t,n,i):(n&&(n.start.copy(a.start),n.end.copy(a.start)),e.containsPoint(a.start)):cn(s)?(n&&(n.start.copy(a.end),n.end.copy(a.end)),e.containsPoint(a.end)):e.plane.intersectLine(a,r)!=null&&(n&&(n.start.copy(r),n.end.copy(r)),e.containsPoint(r))}function _(e,t,n){let r=t.a;return cn(e.plane.distanceToPoint(r))&&e.containsPoint(r)?(n&&(n.start.copy(r),n.end.copy(r)),!0):!1}function v(e,t,n){let i=e.degenerateSegment,a=t.a;return i.closestPointToPoint(a,!0,r),a.distanceToSquared(r)<sn&&(n&&(n.start.copy(a),n.end.copy(a)),!0)}function y(e,t,n,o){if(e.isDegenerateIntoSegment){if(t.isDegenerateIntoSegment){let o=e.degenerateSegment,s=t.degenerateSegment,c=i,l=a;o.delta(c),s.delta(l);let u=r.subVectors(s.start,o.start),d=c.x*l.y-c.y*l.x;if(cn(d))return!1;let f=(u.x*l.y-u.y*l.x)/d,p=-(c.x*u.y-c.y*u.x)/d;return f<0||f>1||p<0||p>1?!1:cn(o.start.z+c.z*f-(s.start.z+l.z*p))?(n&&(n.start.copy(o.start).addScaledVector(c,f),n.end.copy(o.start).addScaledVector(c,f)),!0):!1}return t.isDegenerateIntoPoint?v(e,t,n):g(t,e,n,o)}if(e.isDegenerateIntoPoint)return t.isDegenerateIntoPoint?t.a.distanceToSquared(e.a)<sn&&(n&&(n.start.copy(e.a),n.end.copy(e.a)),!0):t.isDegenerateIntoSegment?v(t,e,n):_(t,e,n);if(t.isDegenerateIntoPoint)return _(e,t,n);if(t.isDegenerateIntoSegment)return g(e,t,n,o)}return function(t,n=null,r=!1){this.needsUpdate&&this.update(),t.isExtendedTriangle?t.needsUpdate&&t.update():(e.copy(t),e.update(),t=e);let o=y(this,t,n,r);if(o!==void 0)return o;let p=this.plane,m=t.plane,g=m.distanceToPoint(this.a),_=m.distanceToPoint(this.b),v=m.distanceToPoint(this.c);cn(g)&&(g=0),cn(_)&&(_=0),cn(v)&&(v=0);let b=g*_,x=g*v;if(b>0&&x>0)return!1;let S=p.distanceToPoint(t.a),C=p.distanceToPoint(t.b),ee=p.distanceToPoint(t.c);cn(S)&&(S=0),cn(C)&&(C=0),cn(ee)&&(ee=0);let te=S*C,ne=S*ee;if(te>0&&ne>0)return!1;i.copy(p.normal),a.copy(m.normal);let w=i.cross(a),re=0,ie=Math.abs(w.x),ae=Math.abs(w.y);ae>ie&&(ie=ae,re=1),Math.abs(w.z)>ie&&(re=2);let oe=an[re],se=this.a[oe],ce=this.b[oe],le=this.c[oe],ue=t.a[oe],de=t.b[oe],fe=t.c[oe];if(h(this,se,ce,le,b,x,g,_,v,u,s)||h(t,ue,de,fe,te,ne,S,C,ee,d,c))return f(this,t,n,r);if(u.y<u.x){let e=u.y;u.y=u.x,u.x=e,l.copy(s.start),s.start.copy(s.end),s.end.copy(l)}if(d.y<d.x){let e=d.y;d.y=d.x,d.x=e,l.copy(c.start),c.start.copy(c.end),c.end.copy(l)}return u.y<d.x||d.y<u.x?!1:(n&&(d.x>u.x?n.start.copy(c.start):n.start.copy(s.start),d.y<u.y?n.end.copy(c.end):n.end.copy(s.end)),!0)}})(),ln.prototype.distanceToPoint=(function(){let e=new E;return function(t){return this.closestPointToPoint(t,e),t.distanceTo(e)}})(),ln.prototype.distanceToTriangle=(function(){let e=new E,t=new E,n=[`a`,`b`,`c`],r=new p,i=new p;return function(a,o=null,s=null){let c=o||s?r:null;if(this.intersectsTriangle(a,c,!0))return(o||s)&&(o&&c.getCenter(o),s&&c.getCenter(s)),0;let l=1/0;for(let t=0;t<3;t++){let r,i=n[t],c=a[i];this.closestPointToPoint(c,e),r=c.distanceToSquared(e),r<l&&(l=r,o&&o.copy(e),s&&s.copy(c));let u=this[i];a.closestPointToPoint(u,e),r=u.distanceToSquared(e),r<l&&(l=r,o&&o.copy(u),s&&s.copy(e))}for(let c=0;c<3;c++){let u=n[c],d=n[(c+1)%3];r.set(this[u],this[d]);for(let c=0;c<3;c++){let u=n[c],d=n[(c+1)%3];i.set(a[u],a[d]),nn(r,i,e,t);let f=e.distanceToSquared(t);f<l&&(l=f,o&&o.copy(e),s&&s.copy(t))}}return Math.sqrt(l)}})();var F=class{constructor(e,t,n){this.isOrientedBox=!0,this.min=new E,this.max=new E,this.matrix=new f,this.invMatrix=new f,this.points=Array(8).fill().map(()=>new E),this.satAxes=[,,,].fill().map(()=>new E),this.satBounds=[,,,].fill().map(()=>new en),this.alignedSatBounds=[,,,].fill().map(()=>new en),this.needsUpdate=!1,e&&this.min.copy(e),t&&this.max.copy(t),n&&this.matrix.copy(n)}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}};F.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<<e];t.subVectors(o,s),n.setFromPoints(t,r)}let s=this.alignedSatBounds;s[0].setFromPointsField(r,`x`),s[1].setFromPointsField(r,`y`),s[2].setFromPointsField(r,`z`),this.invMatrix.copy(this.matrix).invert(),this.needsUpdate=!1}})(),F.prototype.intersectsBox=(function(){let e=new en;return function(t){this.needsUpdate&&this.update();let n=t.min,r=t.max,i=this.satBounds,a=this.satAxes,o=this.alignedSatBounds;if(e.min=n.x,e.max=r.x,o[0].isSeparated(e)||(e.min=n.y,e.max=r.y,o[1].isSeparated(e))||(e.min=n.z,e.max=r.z,o[2].isSeparated(e)))return!1;for(let n=0;n<3;n++){let r=a[n],o=i[n];if(e.setFromBox(r,t),o.isSeparated(e))return!1}return!0}})(),F.prototype.intersectsTriangle=(function(){let e=new ln,t=[,,,],n=new en,r=new en,i=new E;return function(a){this.needsUpdate&&this.update(),a.isExtendedTriangle?a.needsUpdate&&a.update():(e.copy(a),e.update(),a=e);let o=this.satBounds,s=this.satAxes;t[0]=a.a,t[1]=a.b,t[2]=a.c;for(let e=0;e<3;e++){let r=o[e],i=s[e];if(n.setFromPoints(i,t),r.isSeparated(n))return!1}let c=a.satBounds,l=a.satAxes,u=this.points;for(let e=0;e<3;e++){let t=c[e],r=l[e];if(n.setFromPoints(r,u),t.isSeparated(n))return!1}for(let e=0;e<3;e++){let a=s[e];for(let e=0;e<4;e++){let o=l[e];if(i.crossVectors(a,o),n.setFromPoints(i,t),r.setFromPoints(i,u),n.isSeparated(r))return!1}}return!0}})(),F.prototype.closestPointToPoint=(function(){return function(e,t){return this.needsUpdate&&this.update(),t.copy(e).applyMatrix4(this.invMatrix).clamp(this.min,this.max).applyMatrix4(this.matrix),t}})(),F.prototype.distanceToPoint=(function(){let e=new E;return function(t){return this.closestPointToPoint(t,e),t.distanceTo(e)}})(),F.prototype.distanceToBox=(function(){let e=[`x`,`y`,`z`],t=Array(12).fill().map(()=>new p),n=Array(12).fill().map(()=>new p),r=new E,i=new E;return function(a,o=0,s=null,c=null){if(this.needsUpdate&&this.update(),this.intersectsBox(a))return(s||c)&&(a.getCenter(i),this.closestPointToPoint(i,r),a.closestPointToPoint(r,i),s&&s.copy(r),c&&c.copy(i)),0;let l=o*o,u=a.min,d=a.max,f=this.points,p=1/0;for(let e=0;e<8;e++){let t=f[e];i.copy(t).clamp(u,d);let n=t.distanceToSquared(i);if(n<p&&(p=n,s&&s.copy(t),c&&c.copy(i),n<l))return Math.sqrt(n)}let m=0;for(let r=0;r<3;r++)for(let i=0;i<=1;i++)for(let a=0;a<=1;a++){let o=(r+1)%3,s=(r+2)%3,c=i<<o|a<<s,l=1<<r|i<<o|a<<s,p=f[c],h=f[l];t[m].set(p,h);let g=e[r],_=e[o],v=e[s],y=n[m],b=y.start,x=y.end;b[g]=u[g],b[_]=i?u[_]:d[_],b[v]=a?u[v]:d[_],x[g]=d[g],x[_]=i?u[_]:d[_],x[v]=a?u[v]:d[_],m++}for(let e=0;e<=1;e++)for(let t=0;t<=1;t++)for(let n=0;n<=1;n++){i.x=e?d.x:u.x,i.y=t?d.y:u.y,i.z=n?d.z:u.z,this.closestPointToPoint(i,r);let a=i.distanceToSquared(r);if(a<p&&(p=a,s&&s.copy(r),c&&c.copy(i),a<l))return Math.sqrt(a)}for(let e=0;e<12;e++){let a=t[e];for(let e=0;e<12;e++){let t=n[e];nn(a,t,r,i);let o=r.distanceToSquared(i);if(o<p&&(p=o,s&&s.copy(r),c&&c.copy(i),o<l))return Math.sqrt(o)}}return Math.sqrt(p)}})();var un=new class extends Ct{constructor(){super(()=>new ln)}},dn=new E,fn=new E;function pn(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=>(dn.copy(t).clamp(e.min,e.max),dn.distanceToSquared(t)),intersectsBounds:(e,t,n)=>n<s&&n<o,intersectsTriangle:(e,n)=>{e.closestPointToPoint(t,dn);let r=t.distanceToSquared(dn);return r<s&&(fn.copy(dn),s=r,c=n),r<a}}),s===1/0)return null;let l=Math.sqrt(s);return n.point?n.point.copy(fn):n.point=fn.clone(),n.distance=l,n.faceIndex=c,n}var mn=!0,hn=new E,gn=new E,_n=new E,vn=new T,yn=new T,bn=new T,xn=new E,Sn=new E,Cn=new E,wn=new E;function Tn(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 l<o||l>s?null:{distance:l,point:i.clone()}}function En(e,t,n,r,i,a,o,s,c,l,u){hn.fromBufferAttribute(t,a),gn.fromBufferAttribute(t,o),_n.fromBufferAttribute(t,s);let d=Tn(e,hn,gn,_n,wn,c,l,u);if(d){if(r){vn.fromBufferAttribute(r,a),yn.fromBufferAttribute(r,o),bn.fromBufferAttribute(r,s),d.uv=new T;let e=ie.getInterpolation(wn,hn,gn,_n,vn,yn,bn,d.uv);mn||(d.uv=e)}if(i){vn.fromBufferAttribute(i,a),yn.fromBufferAttribute(i,o),bn.fromBufferAttribute(i,s),d.uv1=new T;let e=ie.getInterpolation(wn,hn,gn,_n,vn,yn,bn,d.uv1);mn||(d.uv1=e)}if(n){xn.fromBufferAttribute(n,a),Sn.fromBufferAttribute(n,o),Cn.fromBufferAttribute(n,s),d.normal=new E;let t=ie.getInterpolation(wn,hn,gn,_n,xn,Sn,Cn,d.normal);d.normal.dot(e.direction)>0&&d.normal.multiplyScalar(-1),mn||(d.normal=t)}let t={a,b:o,c:s,normal:new E,materialIndex:0};if(ie.getNormal(hn,gn,_n,t.normal),d.face=t,d.faceIndex=a,mn){let e=new E;ie.getBarycoord(wn,hn,gn,_n,e),d.barycoord=e}}return d}function Dn(e){return e&&e.isMaterial?e.side:e}function On(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;s++){let{start:d,count:_,materialIndex:v}=f[s];if(e>=d&&e<d+_){let e=Dn(t[v]),s=En(n,p,m,h,g,c,l,u,e,a,o);if(s){if(s.faceIndex=r,s.face.materialIndex=v,i)i.push(s);else return s}}}}else{let e=Dn(t),s=En(n,p,m,h,g,c,l,u,e,a,o);if(s){if(s.faceIndex=r,s.face.materialIndex=0,i)i.push(s);else return s}}return null}function I(e,t,n,r){let i=e.a,a=e.b,o=e.c,s=t,c=t+1,l=t+2;n&&(s=n.getX(s),c=n.getX(c),l=n.getX(l)),i.x=r.getX(s),i.y=r.getY(s),i.z=r.getZ(s),a.x=r.getX(c),a.y=r.getY(c),a.z=r.getZ(c),o.x=r.getX(l),o.y=r.getY(l),o.z=r.getZ(l)}function kn(e,t,n,r,i,a,o,s){let{geometry:c,_indirectBuffer:l}=e;for(let e=r,l=r+i;e<l;e++)On(c,t,n,e,a,o,s)}function An(e,t,n,r,i,a,o){let{geometry:s,_indirectBuffer:c}=e,l=1/0,u=null;for(let e=r,c=r+i;e<c;e++){let r;r=On(s,t,n,e,null,a,o),r&&r.distance<l&&(u=r,l=r.distance)}return u}function jn(e,t,n,r,i,a,o){let{geometry:s}=n,{index:c}=s,l=s.attributes.position;for(let n=e,s=t+e;n<s;n++){let e;if(e=n,I(o,e*3,c,l),o.needsUpdate=!0,r(o,e,i,a))return!0}return!1}function Mn(e,t=null){t&&Array.isArray(t)&&(t=new Set(t));let n=e.geometry,r=n.index?n.index.array:null,i=n.attributes.position,a,o,s,c,l=0,u=e._roots;for(let e=0,t=u.length;e<t;e++)a=u[e],o=new Uint32Array(a),s=new Uint16Array(a),c=new Float32Array(a),d(0,l),l+=a.byteLength;function d(e,n,a=!1){let l=e*2;if(O(l,s)){let t=k(e,o),n=A(l,s),a=1/0,u=1/0,d=1/0,f=-1/0,p=-1/0,m=-1/0;for(let e=3*t,o=3*(t+n);e<o;e++){let t=r[e],n=i.getX(t),o=i.getY(t),s=i.getZ(t);n<a&&(a=n),n>f&&(f=n),o<u&&(u=o),o>p&&(p=o),s<d&&(d=s),s>m&&(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}{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]=o<l?o:l,c[e+t+3]=s>u?s:u}return g}}}function Nn(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<o||isNaN(o))&&(o=c),p>=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<o||o!==o)&&(o=u),a<=i&&o>=r)}function Pn(e,t,n,r,i,a,o,s){let{geometry:c,_indirectBuffer:l}=e;for(let e=r,u=r+i;e<u;e++)On(c,t,n,l?l[e]:e,a,o,s)}function Fn(e,t,n,r,i,a,o){let{geometry:s,_indirectBuffer:c}=e,l=1/0,u=null;for(let e=r,d=r+i;e<d;e++){let r;r=On(s,t,n,c?c[e]:e,null,a,o),r&&r.distance<l&&(u=r,l=r.distance)}return u}function In(e,t,n,r,i,a,o){let{geometry:s}=n,{index:c}=s,l=s.attributes.position;for(let s=e,u=t+e;s<u;s++){let e;if(e=n.resolveTriangleIndex(s),I(o,e*3,c,l),o.needsUpdate=!0,r(o,e,i,a))return!0}return!1}function Ln(e,t,n,r,i,a,o){P.setBuffer(e._roots[t]),Rn(0,e,n,r,i,a,o),P.clearBuffer()}function Rn(e,t,n,r,i,a,o){let{float32Array:s,uint16Array:c,uint32Array:l}=P,u=e*2;if(O(u,c))kn(t,n,r,k(e,l),A(u,c),i,a,o);else{let c=j(e);Nn(c,s,r,a,o)&&Rn(c,t,n,r,i,a,o);let u=M(e,l);Nn(u,s,r,a,o)&&Rn(u,t,n,r,i,a,o)}}var zn=[`x`,`y`,`z`];function Bn(e,t,n,r,i,a){P.setBuffer(e._roots[t]);let o=Vn(0,e,n,r,i,a);return P.clearBuffer(),o}function Vn(e,t,n,r,i,a){let{float32Array:o,uint16Array:s,uint32Array:c}=P,l=e*2;if(O(l,s))return An(t,n,r,k(e,c),A(l,s),i,a);{let s=rt(e,c),l=zn[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=Nn(d,o,r,i,a)?Vn(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=Nn(f,o,r,i,a)?Vn(f,t,n,r,i,a):null;return p&&m?p.distance<=m.distance?p:m:p||m||null}}var Hn=new De,Un=new ln,Wn=new ln,Gn=new f,Kn=new F,qn=new F;function Jn(e,t,n,r){P.setBuffer(e._roots[t]);let i=Yn(0,e,n,r);return P.clearBuffer(),i}function Yn(e,t,n,r,i=null){let{float32Array:a,uint16Array:o,uint32Array:s}=P,c=e*2;if(i===null&&(n.boundingBox||n.computeBoundingBox(),Kn.set(n.boundingBox.min,n.boundingBox.max,r),i=Kn),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(Gn.copy(r).invert(),n.boundsTree)return D(N(e),a,qn),qn.matrix.copy(Gn),qn.needsUpdate=!0,n.boundsTree.shapecast({intersectsBounds:e=>qn.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;t<n;t+=3)if(I(Wn,t,l,u),Wn.needsUpdate=!0,e.intersectsTriangle(Wn))return!0;return!1}});{let e=Kt(n);for(let t=p*3,n=(m+p)*3;t<n;t+=3){I(Un,t,l,u),Un.a.applyMatrix4(Gn),Un.b.applyMatrix4(Gn),Un.c.applyMatrix4(Gn),Un.needsUpdate=!0;for(let t=0,n=e*3;t<n;t+=3)if(I(Wn,t,d,f),Wn.needsUpdate=!0,Un.intersectsTriangle(Wn))return!0}}}else{let o=j(e),c=M(e,s);return D(N(o),a,Hn),!!(i.intersectsBox(Hn)&&Yn(o,t,n,r,i)||(D(N(c),a,Hn),i.intersectsBox(Hn)&&Yn(c,t,n,r,i)))}}var Xn=new f,Zn=new F,Qn=new F,$n=new E,er=new E,tr=new E,nr=new E;function rr(e,t,n,r={},i={},a=0,o=1/0){t.boundingBox||t.computeBoundingBox(),Zn.set(t.boundingBox.min,t.boundingBox.max,n),Zn.needsUpdate=!0;let s=e.geometry,c=s.attributes.position,l=s.index,u=t.attributes.position,d=t.index,f=un.getPrimitive(),p=un.getPrimitive(),m=$n,h=er,g=null,_=null;i&&(g=tr,_=nr);let v=1/0,y=null,b=null;return Xn.copy(n).invert(),Qn.matrix.copy(Xn),e.shapecast({boundsTraverseOrder:e=>Zn.distanceToBox(e),intersectsBounds:(e,t,n)=>n<v&&n<o&&(t&&(Qn.min.copy(e.min),Qn.max.copy(e.max),Qn.needsUpdate=!0),!0),intersectsRange:(e,r)=>{if(t.boundsTree)return t.boundsTree.shapecast({boundsTraverseOrder:e=>Qn.distanceToBox(e),intersectsBounds:(e,t,n)=>n<v&&n<o,intersectsRange:(t,i)=>{for(let o=t,s=t+i;o<s;o++){I(p,3*o,d,u),p.a.applyMatrix4(n),p.b.applyMatrix4(n),p.c.applyMatrix4(n),p.needsUpdate=!0;for(let t=e,n=e+r;t<n;t++){I(f,3*t,l,c),f.needsUpdate=!0;let e=f.distanceToTriangle(p,m,g);if(e<v&&(h.copy(m),_&&_.copy(g),v=e,y=t,b=o),e<a)return!0}}}});{let i=Kt(t);for(let t=0,o=i;t<o;t++){I(p,3*t,d,u),p.a.applyMatrix4(n),p.b.applyMatrix4(n),p.c.applyMatrix4(n),p.needsUpdate=!0;for(let n=e,i=e+r;n<i;n++){I(f,3*n,l,c),f.needsUpdate=!0;let e=f.distanceToTriangle(p,m,g);if(e<v&&(h.copy(m),_&&_.copy(g),v=e,y=n,b=t),e<a)return!0}}}}}),un.releasePrimitive(f),un.releasePrimitive(p),v===1/0?null:(r.point?r.point.copy(h):r.point=h.clone(),r.distance=v,r.faceIndex=y,i&&(i.point?i.point.copy(_):i.point=_.clone(),i.point.applyMatrix4(Xn),h.applyMatrix4(Xn),i.distance=h.sub(i.point).length(),i.faceIndex=b),r)}function ir(e,t=null){t&&Array.isArray(t)&&(t=new Set(t));let n=e.geometry,r=n.index?n.index.array:null,i=n.attributes.position,a,o,s,c,l=0,u=e._roots;for(let e=0,t=u.length;e<t;e++)a=u[e],o=new Uint32Array(a),s=new Uint16Array(a),c=new Float32Array(a),d(0,l),l+=a.byteLength;function d(n,a,l=!1){let u=n*2;if(O(u,s)){let t=k(n,o),a=A(u,s),l=1/0,d=1/0,f=1/0,p=-1/0,m=-1/0,h=-1/0;for(let n=t,o=t+a;n<o;n++){let t=3*e.resolveTriangleIndex(n);for(let e=0;e<3;e++){let n=t+e;n=r?r[n]:n;let a=i.getX(n),o=i.getY(n),s=i.getZ(n);a<l&&(l=a),a>p&&(p=a),o<d&&(d=o),o>m&&(m=o),s<f&&(f=s),s>h&&(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}{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]=o<l?o:l,c[n+t+3]=s>u?s:u}return g}}}function ar(e,t,n,r,i,a,o){P.setBuffer(e._roots[t]),or(0,e,n,r,i,a,o),P.clearBuffer()}function or(e,t,n,r,i,a,o){let{float32Array:s,uint16Array:c,uint32Array:l}=P,u=e*2;if(O(u,c))Pn(t,n,r,k(e,l),A(u,c),i,a,o);else{let c=j(e);Nn(c,s,r,a,o)&&or(c,t,n,r,i,a,o);let u=M(e,l);Nn(u,s,r,a,o)&&or(u,t,n,r,i,a,o)}}var sr=[`x`,`y`,`z`];function cr(e,t,n,r,i,a){P.setBuffer(e._roots[t]);let o=lr(0,e,n,r,i,a);return P.clearBuffer(),o}function lr(e,t,n,r,i,a){let{float32Array:o,uint16Array:s,uint32Array:c}=P,l=e*2;if(O(l,s))return Fn(t,n,r,k(e,c),A(l,s),i,a);{let s=rt(e,c),l=sr[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=Nn(d,o,r,i,a)?lr(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=Nn(f,o,r,i,a)?lr(f,t,n,r,i,a):null;return p&&m?p.distance<=m.distance?p:m:p||m||null}}var ur=new De,dr=new ln,fr=new ln,pr=new f,mr=new F,hr=new F;function gr(e,t,n,r){P.setBuffer(e._roots[t]);let i=_r(0,e,n,r);return P.clearBuffer(),i}function _r(e,t,n,r,i=null){let{float32Array:a,uint16Array:o,uint32Array:s}=P,c=e*2;if(i===null&&(n.boundingBox||n.computeBoundingBox(),mr.set(n.boundingBox.min,n.boundingBox.max,r),i=mr),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(pr.copy(r).invert(),n.boundsTree)return D(N(e),a,hr),hr.matrix.copy(pr),hr.needsUpdate=!0,n.boundsTree.shapecast({intersectsBounds:e=>hr.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;n<r;n++)if(I(fr,3*t.resolveTriangleIndex(n),l,u),fr.needsUpdate=!0,e.intersectsTriangle(fr))return!0;return!1}});{let e=Kt(n);for(let n=p,r=m+p;n<r;n++){I(dr,3*t.resolveTriangleIndex(n),l,u),dr.a.applyMatrix4(pr),dr.b.applyMatrix4(pr),dr.c.applyMatrix4(pr),dr.needsUpdate=!0;for(let t=0,n=e*3;t<n;t+=3)if(I(fr,t,d,f),fr.needsUpdate=!0,dr.intersectsTriangle(fr))return!0}}}else{let o=j(e),c=M(e,s);return D(N(o),a,ur),!!(i.intersectsBox(ur)&&_r(o,t,n,r,i)||(D(N(c),a,ur),i.intersectsBox(ur)&&_r(c,t,n,r,i)))}}var vr=new f,yr=new F,br=new F,xr=new E,Sr=new E,Cr=new E,wr=new E;function Tr(e,t,n,r={},i={},a=0,o=1/0){t.boundingBox||t.computeBoundingBox(),yr.set(t.boundingBox.min,t.boundingBox.max,n),yr.needsUpdate=!0;let s=e.geometry,c=s.attributes.position,l=s.index,u=t.attributes.position,d=t.index,f=un.getPrimitive(),p=un.getPrimitive(),m=xr,h=Sr,g=null,_=null;i&&(g=Cr,_=wr);let v=1/0,y=null,b=null;return vr.copy(n).invert(),br.matrix.copy(vr),e.shapecast({boundsTraverseOrder:e=>yr.distanceToBox(e),intersectsBounds:(e,t,n)=>n<v&&n<o&&(t&&(br.min.copy(e.min),br.max.copy(e.max),br.needsUpdate=!0),!0),intersectsRange:(r,i)=>{if(t.boundsTree){let s=t.boundsTree;return s.shapecast({boundsTraverseOrder:e=>br.distanceToBox(e),intersectsBounds:(e,t,n)=>n<v&&n<o,intersectsRange:(t,o)=>{for(let x=t,S=t+o;x<S;x++){let t=s.resolveTriangleIndex(x);I(p,3*t,d,u),p.a.applyMatrix4(n),p.b.applyMatrix4(n),p.c.applyMatrix4(n),p.needsUpdate=!0;for(let t=r,n=r+i;t<n;t++){let n=e.resolveTriangleIndex(t);I(f,3*n,l,c),f.needsUpdate=!0;let r=f.distanceToTriangle(p,m,g);if(r<v&&(h.copy(m),_&&_.copy(g),v=r,y=t,b=x),r<a)return!0}}}})}{let o=Kt(t);for(let t=0,s=o;t<s;t++){I(p,3*t,d,u),p.a.applyMatrix4(n),p.b.applyMatrix4(n),p.c.applyMatrix4(n),p.needsUpdate=!0;for(let n=r,o=r+i;n<o;n++){let r=e.resolveTriangleIndex(n);I(f,3*r,l,c),f.needsUpdate=!0;let i=f.distanceToTriangle(p,m,g);if(i<v&&(h.copy(m),_&&_.copy(g),v=i,y=n,b=t),i<a)return!0}}}}}),un.releasePrimitive(f),un.releasePrimitive(p),v===1/0?null:(r.point?r.point.copy(h):r.point=h.clone(),r.distance=v,r.faceIndex=y,i&&(i.point?i.point.copy(_):i.point=_.clone(),i.point.applyMatrix4(vr),h.applyMatrix4(vr),i.distance=h.sub(i.point).length(),i.faceIndex=b),r)}function Er(e,t,n){return e===null?null:(e.point.applyMatrix4(t.matrixWorld),e.distance=e.point.distanceTo(n.ray.origin),e.object=t,e)}var Dr=new F,Or=new ee,kr=new E,Ar=new f,jr=new E,Mr=[`getX`,`getY`,`getZ`],Nr=class e extends $t{static serialize(e,t={}){t={cloneBuffers:!0,...t};let n=e.geometry,r=e._roots,i=e._indirectBuffer,a=n.getIndex(),o={version:1,roots:null,index:null,indirectBuffer:null};return t.cloneBuffers?(o.roots=r.map(e=>e.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,[Xe]:!0});if(s._roots=a,s._indirectBuffer=o||null,r.setIndex){let e=n.getIndex();if(e===null){let e=new xe(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;t<e.length;t++){let n=e[t],r=new Uint32Array(n),i=new Uint16Array(n);for(let e=0,t=n.byteLength/32;e<t;e++){let t=8*e;O(2*t,i)||(r[t+6]=r[t+6]/8-e)}}}}get primitiveStride(){return 3}get resolveTriangleIndex(){return this.resolvePrimitiveIndex}constructor(e,t={}){t.maxLeafTris&&(console.warn(`MeshBVH: "maxLeafTris" option has been deprecated. Use "targetLeafSize", instead.`),t={...t,targetLeafSize:t.maxLeafTris}),super(e,t)}shiftTriangleOffsets(e){return super.shiftPrimitiveOffsets(e)}writePrimitiveBounds(e,t,n){let r=this.geometry,i=this._indirectBuffer,a=r.attributes.position,o=r.index?r.index.array:null,s=(i?i[e]:e)*3,c=s+0,l=s+1,u=s+2;o&&(c=o[c],l=o[l],u=o[u]);for(let e=0;e<3;e++){let r=a[Mr[e]](c),i=a[Mr[e]](l),o=a[Mr[e]](u),s=r;i<s&&(s=i),o<s&&(s=o);let d=r;i>d&&(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;r<p;r++){let e=(i?i[r]:r)*3,t=(r-f)*6,p=e+0,m=e+1,h=e+2;o&&(p=o[p],m=o[m],h=o[h]),s||(p=p*u+l,m=m*u+l,h=h*u+l);for(let e=0;e<3;e++){let r,i,o;s?(r=a[d[e]](p),i=a[d[e]](m),o=a[d[e]](h)):(r=c[p+e],i=c[m+e],o=c[h+e]);let l=r;i<l&&(l=i),o<l&&(l=o);let u=r;i>u&&(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)*Ye}}return n}raycastObject3D(e,t,n=[]){let{material:r}=e;if(r===void 0)return;Ar.copy(e.matrixWorld).invert(),Or.copy(t.ray).applyMatrix4(Ar),jr.setFromMatrixScale(e.matrixWorld),kr.copy(Or.direction).multiply(jr);let i=kr.length(),a=t.near/i,o=t.far/i;if(t.firstHitOnly===!0){let i=this.raycastFirst(Or,r,a,o);i=Er(i,e,t),i&&n.push(i)}else{let i=this.raycast(Or,r,a,o);for(let r=0,a=i.length;r<a;r++){let a=Er(i[r],e,t);a&&n.push(a)}}return n}refit(e=null){return(this.indirect?ir:Mn)(this,e)}raycast(e,t=0,n=0,r=1/0){let i=this._roots,a=[],o=this.indirect?ar:Ln;for(let s=0,c=i.length;s<c;s++)o(this,s,t,e,a,n,r);return a}raycastFirst(e,t=0,n=0,r=1/0){let i=this._roots,a=null,o=this.indirect?cr:Bn;for(let s=0,c=i.length;s<c;s++){let i=o(this,s,t,e,n,r);i!=null&&(a==null||i.distance<a.distance)&&(a=i)}return a}intersectsGeometry(e,t){let n=!1,r=this._roots,i=this.indirect?gr:Jn;for(let a=0,o=r.length;a<o&&(n=i(this,a,e,t),!n);a++);return n}shapecast(e){let t=un.getPrimitive(),n=super.shapecast({...e,intersectsPrimitive:e.intersectsTriangle,scratchPrimitive:t,iterate:this.indirect?In:jn});return un.releasePrimitive(t),n}bvhcast(t,n,r){let{intersectsRanges:i,intersectsTriangles:a}=r,o=un.getPrimitive(),s=this.geometry.index,c=this.geometry.attributes.position,l=this.indirect?e=>{let t=this.resolveTriangleIndex(e);I(o,t*3,s,c)}:e=>{I(o,e*3,s,c)},u=un.getPrimitive(),d=t.geometry.index,f=t.geometry.attributes.position,p=t.indirect?e=>{let n=t.resolveTriangleIndex(e);I(u,n*3,d,f)}:e=>{I(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;m<h;m++){p(m),u.a.applyMatrix4(n),u.b.applyMatrix4(n),u.c.applyMatrix4(n),u.needsUpdate=!0;for(let n=e,r=e+t;n<r;n++)if(l(n),o.needsUpdate=!0,a(o,u,n,m,s,c,d,f))return!0}return!1};if(i){let e=i;i=function(t,n,i,a,o,s,c,l){return e(t,n,i,a,o,s,c,l)?!0:r(t,n,i,a,o,s,c,l)}}else i=r}return super.bvhcast(t,n,{intersectsRanges:i})}intersectsBox(e,t){return Dr.set(e.min,e.max,t),Dr.needsUpdate=!0,this.shapecast({intersectsBounds:e=>Dr.intersectsBox(e),intersectsTriangle:e=>Dr.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?Tr:rr)(this,e,t,n,r,i,a)}closestPointToPoint(e,t={},n=0,r=1/0){return pn(this,e,t,n,r)}};function Pr(e){switch(e){case 1:return`R`;case 2:return`RG`;case 3:return`RGBA`;case 4:return`RGBA`}throw Error()}function Fr(e){switch(e){case 1:return ne;case 2:return u;case 3:return d;case 4:return d}}function Ir(e){switch(e){case 1:return _;case 2:return te;case 3:return ke;case 4:return ke}}var Lr=class extends ze{constructor(){super(),this.minFilter=S,this.magFilter=S,this.generateMipmaps=!1,this.overrideItemSize=null,this._forcedType=null}updateFrom(e){let t=this.overrideItemSize,n=e.itemSize,r=e.count;if(t!==null){if(n*r%t!==0)throw Error(`VertexAttributeTexture: overrideItemSize must divide evenly into buffer length.`);e.itemSize=t,e.count=r*n/t}let i=e.itemSize,a=e.count,o=e.normalized,s=e.array.constructor,c=s.BYTES_PER_ELEMENT,l=this._forcedType,u=i;if(l===null)switch(s){case Float32Array:l=b;break;case Uint8Array:case Uint16Array:case Uint32Array:l=Ne;break;case Int8Array:case Int16Array:case Int32Array:l=ve}let d,f,p,m,h=Pr(i);switch(l){case b:p=1,f=Fr(i),o&&c===1?(m=s,h+=`8`,s===Uint8Array?d=Ee:(d=pe,h+=`_SNORM`)):(m=Float32Array,h+=`32F`,d=b);break;case ve:h+=c*8+`I`,p=o?2**(s.BYTES_PER_ELEMENT*8-1):1,f=Ir(i),c===1?(m=Int8Array,d=pe):c===2?(m=Int16Array,d=we):(m=Int32Array,d=ve);break;case Ne:h+=c*8+`UI`,p=o?2**(s.BYTES_PER_ELEMENT*8-1):1,f=Ir(i),c===1?(m=Uint8Array,d=Ee):c===2?(m=Uint16Array,d=se):(m=Uint32Array,d=Ne)}u===3&&(f===1023||f===1033)&&(u=4);let g=Math.ceil(Math.sqrt(a))||1,_=u*g*g,v=new m(_),y=e.normalized;e.normalized=!1;for(let t=0;t<a;t++){let n=u*t;v[n]=e.getX(t)/p,i>=2&&(v[n+1]=e.getY(t)/p),i>=3&&(v[n+2]=e.getZ(t)/p,u===4&&(v[n+3]=1)),i>=4&&(v[n+3]=e.getW(t)/p)}e.normalized=y,this.internalFormat=h,this.format=f,this.type=d,this.image.width=g,this.image.height=g,this.image.data=v,this.needsUpdate=!0,this.dispose(),e.itemSize=n,e.count=r}},Rr=class extends Lr{constructor(){super(),this._forcedType=Ne}},zr=class extends Lr{constructor(){super(),this._forcedType=b}},Br=class{constructor(){this.index=new Rr,this.position=new zr,this.bvhBounds=new ze,this.bvhContents=new ze,this._cachedIndexAttr=null,this.index.overrideItemSize=3}updateFrom(e){let{geometry:t}=e;if(Hr(e,this.bvhBounds,this.bvhContents),this.position.updateFrom(t.attributes.position),e.indirect){let n=e._indirectBuffer;if(this._cachedIndexAttr===null||this._cachedIndexAttr.count!==n.length){if(t.index)this._cachedIndexAttr=t.index.clone();else{let e=qt(Gt(t));this._cachedIndexAttr=new xe(e,1,!1)}}Vr(t,n,this._cachedIndexAttr),this.index.updateFrom(this._cachedIndexAttr)}else this.index.updateFrom(t.index)}dispose(){let{index:e,position:t,bvhBounds:n,bvhContents:r}=this;e&&e.dispose(),t&&t.dispose(),n&&n.dispose(),r&&r.dispose()}};function Vr(e,t,n){let r=n.array,i=e.index?e.index.array:null;for(let e=0,n=t.length;e<n;e++){let n=3*e,a=3*t[e];for(let e=0;e<3;e++)r[n+e]=i?i[a+e]:a+e}}function Hr(e,t,n){let r=e._roots;if(r.length!==1)throw Error(`MeshBVHUniformStruct: Multi-root BVHs not supported.`);let i=r[0],a=new Uint16Array(i),o=new Uint32Array(i),s=new Float32Array(i),c=i.byteLength/32,l=2*Math.ceil(Math.sqrt(c/2)),u=new Float32Array(4*l*l),f=Math.ceil(Math.sqrt(c)),p=new Uint32Array(2*f*f);for(let e=0;e<c;e++){let t=e*32/4,n=t*2,r=N(t);for(let t=0;t<3;t++)u[8*e+0+t]=s[r+0+t],u[8*e+4+t]=s[r+3+t];if(O(n,a)){let r=A(n,a),i=k(t,o),s=Je|r;p[e*2+0]=s,p[e*2+1]=i}else{let n=o[t+6],r=rt(t,o);p[e*2+0]=r,p[e*2+1]=n}}t.image.data=u,t.image.width=l,t.image.height=l,t.format=d,t.type=b,t.internalFormat=`RGBA32F`,t.minFilter=S,t.magFilter=S,t.generateMipmaps=!1,t.needsUpdate=!0,t.dispose(),n.image.data=p,n.image.width=f,n.image.height=f,n.format=te,n.type=Ne,n.internalFormat=`RG32UI`,n.minFilter=S,n.magFilter=S,n.generateMipmaps=!1,n.needsUpdate=!0,n.dispose()}var Ur=`
1
+ import{$ as e,A as t,B as n,C as r,Ct as i,D as a,E as o,Et as s,F as c,G as l,H as u,I as d,J as f,K as p,L as m,M as h,N as g,O as _,P as v,Q as y,R as b,S as x,St as S,T as C,Tt as ee,U as w,V as te,W as ne,X as re,Y as ie,Z as T,_ as ae,_t as oe,a as se,at as ce,b as le,bt as E,c as ue,ct as de,d as fe,dt as pe,et as me,f as he,ft as ge,g as _e,gt as ve,h as ye,ht as be,i as xe,it as Se,j as Ce,k as we,l as Te,lt as Ee,m as De,mt as Oe,n as ke,nt as D,o as Ae,ot as je,p as Me,pt as Ne,q as O,r as Pe,rt as Fe,s as Ie,st as Le,t as Re,tt as ze,u as Be,ut as Ve,v as He,vt as Ue,w as We,wt as Ge,x as Ke,xt as k,y as qe,yt as Je,z as Ye}from"./shared-CDFEFgsW.js";import{i as Xe,n as Ze,r as Qe,t as $e}from"./prompt-ui-zw7U8lsl.js";var et=1.25,tt=65535,nt=-65536,rt=2**-24,it=Symbol(`SKIP_GENERATION`),at={strategy:0,maxDepth:40,targetLeafSize:10,useSharedArrayBuffer:!1,setBoundingBox:!0,onProgress:null,indirect:!1,verbose:!0,range:null,[it]:!1};function A(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 ot(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 st(e,t){t.set(e)}function ct(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]=r<i?r:i,r=e[o],i=t[o],n[o]=r>i?r:i}}function lt(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;o<n[r]&&(n[r]=o),s>n[r+3]&&(n[r+3]=s)}}function ut(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 j(e,t){return t[e+15]===tt}function M(e,t){return t[e+6]}function N(e,t){return t[e+14]}function P(e){return e+8}function F(e,t){return e+t[e+6]*8}function dt(e,t){return t[e+7]}function I(e){return e}function ft(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;r<i;r+=6){let t=e[r+0],n=e[r+1],i=t-n,_=t+n;i<a&&(a=i),_>c&&(c=_),t<d&&(d=t),t>m&&(m=t);let v=e[r+2],y=e[r+3],b=v-y,x=v+y;b<o&&(o=b),x>l&&(l=x),v<f&&(f=v),v>h&&(h=v);let S=e[r+4],C=e[r+5],ee=S-C,w=S+C;ee<s&&(s=ee),w>u&&(u=w),S<p&&(p=S),S>g&&(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 pt=32,mt=(e,t)=>e.candidate-t.candidate,ht=Array(pt).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),gt=new Float32Array(6);function _t(e,t,n,r,i,a){let o=-1,s=0;if(a===0)o=ot(t),o!==-1&&(s=(t[o]+t[o+3])/2);else if(a===1)o=ot(e),o!==-1&&(s=vt(n,r,i,o));else if(a===2){let a=ut(e),c=et*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)/pt;if(i<pt/4){let t=[...ht];t.length=i;let r=0;for(let i=u;i<d;i+=6,r++){let a=t[r];a.candidate=n[i+2*e],a.count=0;let{bounds:o,leftCacheBounds:s,rightCacheBounds:c}=a;for(let e=0;e<3;e++)c[e]=1/0,c[e+3]=-1/0,s[e]=1/0,s[e+3]=-1/0,o[e]=1/0,o[e+3]=-1/0;lt(i,n,o)}t.sort(mt);let l=i;for(let e=0;e<l;e++){let n=t[e];for(;e+1<l&&t[e+1].candidate===n.candidate;)t.splice(e+1,1),l--}for(let r=u;r<d;r+=6){let i=n[r+2*e];for(let e=0;e<l;e++){let a=t[e];i>=a.candidate?lt(r,n,a.rightCacheBounds):(lt(r,n,a.leftCacheBounds),a.count++)}}for(let n=0;n<l;n++){let r=t[n],l=r.count,u=i-r.count,d=r.leftCacheBounds,f=r.rightCacheBounds,p=0;l!==0&&(p=ut(d)/a);let m=0;u!==0&&(m=ut(f)/a);let h=1+et*(p*l+m*u);h<c&&(o=e,c=h,s=r.candidate)}}else{for(let e=0;e<pt;e++){let t=ht[e];t.count=0,t.candidate=r+l+e*l;let n=t.bounds;for(let e=0;e<3;e++)n[e]=1/0,n[e+3]=-1/0}for(let t=u;t<d;t+=6){let i=~~((n[t+2*e]-r)/l);i>=pt&&(i=31);let a=ht[i];a.count++,lt(t,n,a.bounds)}let t=ht[31];st(t.bounds,t.rightCacheBounds);for(let e=30;e>=0;e--){let t=ht[e],n=ht[e+1];ct(t.bounds,n.rightCacheBounds,t.rightCacheBounds)}let f=0;for(let t=0;t<31;t++){let n=ht[t],r=n.count,l=n.bounds,u=ht[t+1].rightCacheBounds;r!==0&&(f===0?st(l,gt):ct(l,gt,gt)),f+=r;let d=0,p=0;f!==0&&(d=ut(gt)/a);let m=i-f;m!==0&&(p=ut(u)/a);let h=1+et*(d*f+p*m);h<c&&(o=e,c=h,s=n.candidate)}}}}else console.warn(`BVH: Invalid build strategy value ${a} used.`);return{axis:o,pos:s}}function vt(e,t,n,r){let i=0,a=e.offset;for(let o=t,s=t+n;o<s;o++)i+=e[(o-a)*6+r*2];return i/n}var yt=class{constructor(){this.boundingData=new Float32Array(6)}};function bt(e,t,n,r,i,a){let o=r,s=r+i-1,c=a.pos,l=a.axis*2,u=n.offset||0;for(;;){for(;o<=s&&n[(o-u)*6+l]<c;)o++;for(;o<=s&&n[(s-u)*6+l]>=c;)s--;if(o<s){for(let n=0;n<t;n++){let r=e[o*t+n];e[o*t+n]=e[s*t+n],e[s*t+n]=r}for(let e=0;e<6;e++){let t=o-u,r=s-u,i=n[t*6+e];n[t*6+e]=n[r*6+e],n[r*6+e]=i}o++,s--}else return o}}var xt,St,Ct,wt,Tt=2**32;function Et(e){return`count`in e?1:1+Et(e.left)+Et(e.right)}function Dt(e,t,n){return xt=new Float32Array(n),St=new Uint32Array(n),Ct=new Uint16Array(n),wt=new Uint8Array(n),Ot(e,t)}function Ot(e,t){let n=e/4,r=e/2,i=`count`in t,a=t.boundingData;for(let e=0;e<6;e++)xt[n+e]=a[e];if(i)return t.buffer?(wt.set(new Uint8Array(t.buffer),e),e+t.buffer.byteLength):(St[n+6]=t.offset,Ct[r+14]=t.count,Ct[r+15]=tt,e+32);{let{left:r,right:i,splitAxis:a}=t,o=Ot(e+32,r),s=e/32,c=o/32-s;if(c>Tt)throw Error(`MeshBVH: Cannot store relative child node offset greater than 32 bits.`);return St[n+6]=c,St[n+7]=a,Ot(o,i)}}function kt(e,t,n,r,i,a){let{maxDepth:o,verbose:s,targetLeafSize:c,_strictLeafSize:l=1/0,strategy:u,onProgress:d}=i,f=e.primitiveBuffer,p=e.primitiveBufferStride,m=new Float32Array(6),h=!1,g=new yt;return ft(t,n,r,g.boundingData,m),v(g,n,r,m),g;function _(e){d&&d((e-a.offset)/a.count)}function v(e,n,r,i=null,a=0){!h&&a>=o&&(h=!0,s&&console.warn(`BVH: Max depth of ${o} reached when generating BVH. Consider increasing maxDepth.`));let d=r>l;if(r<=c&&!d||a>=o)return _(n+r),e.offset=n,e.count=r,e;let g=_t(e.boundingData,i,t,n,r,u),y=g.axis===-1?-1:bt(f,p,t,n,r,g);if(g.axis===-1||y===n||y===n+r){if(!d)return _(n+r),e.offset=n,e.count=r,e;g.axis=Math.max(0,ot(e.boundingData)),y=n+Math.max(1,Math.floor(r/2))}e.splitAxis=g.axis;let b=new yt,x=n,S=y-n;e.left=b,ft(t,x,S,b.boundingData,m),v(b,x,S,m,a+1);let C=new yt,ee=y,w=r-S;return e.right=C,ft(t,ee,w,C.boundingData,m),v(C,ee,w,m,a+1),e}}function At(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=kt(e,s,r.offset,r.count,t,o),a=Et(i),c=new n(32*a);return Dt(0,i,c),c})}var jt=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)}},L=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())}}},Mt,Nt,Pt=[],Ft=new jt(()=>new We);function It(e,t,n,r,i,a){Mt=Ft.getPrimitive(),Nt=Ft.getPrimitive(),Pt.push(Mt,Nt),L.setBuffer(e._roots[t]);let o=Lt(0,e.geometry,n,r,i,a);L.clearBuffer(),Ft.releasePrimitive(Mt),Ft.releasePrimitive(Nt),Pt.pop(),Pt.pop();let s=Pt.length;return s>0&&(Nt=Pt[s-1],Mt=Pt[s-2]),o}function Lt(e,t,n,r,i=null,a=0,o=0){let{float32Array:s,uint16Array:c,uint32Array:l}=L,u=e*2;if(j(u,c)){let t=M(e,l),n=N(u,c);return A(I(e),s,Mt),r(t,n,!1,o,a+e/8,Mt)}{let u=P(e),d=F(e,l),f=u,p=d,m,h,g,_;if(i&&(g=Mt,_=Nt,A(I(f),s,g),A(I(p),s,_),m=i(g),h=i(_),h<m)){f=d,p=u;let e=m;m=h,h=e,g=_}g||(g=Mt,A(I(f),s,g));let v=j(f*2,c),y=n(g,v,m,o+1,a+f/8),b;if(y===2){let e=ee(f);b=r(e,w(f)-e,!0,o+1,a+f/8,g)}else b=y&&Lt(f,t,n,r,i,a,o+1);if(b)return!0;_=Nt,A(I(p),s,_);let x=j(p*2,c),S=n(_,x,h,o+1,a+p/8),C;if(S===2){let e=ee(p);C=r(e,w(p)-e,!0,o+1,a+p/8,_)}else C=S&&Lt(p,t,n,r,i,a,o+1);return!!C;function ee(e){let{uint16Array:t,uint32Array:n}=L,r=e*2;for(;!j(r,t);)e=P(e),r=e*2;return M(e,n)}function w(e){let{uint16Array:t,uint32Array:n}=L,r=e*2;for(;!j(r,t);)e=F(e,n),r=e*2;return M(e,n)+N(r,t)}}}var Rt=new L.constructor,zt=new L.constructor,Bt=new jt(()=>new We),Vt=new We,Ht=new We,Ut=new We,Wt=new We,Gt=!1;function Kt(e,t,n,r){if(Gt)throw Error(`MeshBVH: Recursive calls to bvhcast not supported.`);Gt=!0;let i=e._roots,a=t._roots,o,s=0,c=0,l=new O().copy(n).invert();for(let e=0,t=i.length;e<t;e++){Rt.setBuffer(i[e]),c=0;let t=Bt.getPrimitive();A(I(0),Rt.float32Array,t),t.applyMatrix4(l);for(let e=0,i=a.length;e<i&&(zt.setBuffer(a[e]),o=qt(0,0,n,l,r,s,c,0,0,t),zt.clearBuffer(),c+=a[e].byteLength/32,!o);e++);if(Bt.releasePrimitive(t),Rt.clearBuffer(),s+=i[e].byteLength/32,o)break}return Gt=!1,o}function qt(e,t,n,r,i,a=0,o=0,s=0,c=0,l=null,u=!1){let d,f;u?(d=zt,f=Rt):(d=Rt,f=zt);let p=d.float32Array,m=d.uint32Array,h=d.uint16Array,g=f.float32Array,_=f.uint32Array,v=f.uint16Array,y=e*2,b=t*2,x=j(y,h),S=j(b,v),C=!1;if(S&&x)C=u?i(M(t,_),N(t*2,v),M(e,m),N(e*2,h),c,o+t/8,s,a+e/8):i(M(e,m),N(e*2,h),M(t,_),N(t*2,v),s,a+e/8,c,o+t/8);else if(S){let l=Bt.getPrimitive();A(I(t),g,l),l.applyMatrix4(n);let d=P(e),f=F(e,m);A(I(d),p,Vt),A(I(f),p,Ht);let h=l.intersectsBox(Vt),_=l.intersectsBox(Ht);C=h&&qt(t,d,r,n,i,o,a,c,s+1,l,!u)||_&&qt(t,f,r,n,i,o,a,c,s+1,l,!u),Bt.releasePrimitive(l)}else{let d=P(t),f=F(t,_);A(I(d),g,Ut),A(I(f),g,Wt);let h=l.intersectsBox(Ut),v=l.intersectsBox(Wt);if(h&&v)C=qt(e,d,n,r,i,a,o,s,c+1,l,u)||qt(e,f,n,r,i,a,o,s,c+1,l,u);else if(h){if(x)C=qt(e,d,n,r,i,a,o,s,c+1,l,u);else{let t=Bt.getPrimitive();t.copy(Ut).applyMatrix4(n);let l=P(e),f=F(e,m);A(I(l),p,Vt),A(I(f),p,Ht);let h=t.intersectsBox(Vt),g=t.intersectsBox(Ht);C=h&&qt(d,l,r,n,i,o,a,c,s+1,t,!u)||g&&qt(d,f,r,n,i,o,a,c,s+1,t,!u),Bt.releasePrimitive(t)}}else if(v){if(x)C=qt(e,f,n,r,i,a,o,s,c+1,l,u);else{let t=Bt.getPrimitive();t.copy(Wt).applyMatrix4(n);let l=P(e),d=F(e,m);A(I(l),p,Vt),A(I(d),p,Ht);let h=t.intersectsBox(Vt),g=t.intersectsBox(Ht);C=h&&qt(f,l,r,n,i,o,a,c,s+1,t,!u)||g&&qt(f,d,r,n,i,o,a,c,s+1,t,!u),Bt.releasePrimitive(t)}}}return C}var Jt=new class{constructor(){let e=null,t=null,n=null,r=!1;this.root=null,this.buffer=null,this.uint32Array=null,this.uint16Array=null,this.setBVH=(i,a)=>{if(r)throw Error(`BVHTraversalHelper: cannot call setBVH during an active traversal.`);this.root=a,this.buffer=e=i._roots[a],this.uint16Array=n=new Uint16Array(e),this.uint32Array=t=new Uint32Array(e)},this.reset=()=>{this.root=null,this.buffer=e=null,this.uint16Array=n=null,this.uint32Array=t=null},this.getRangeStart=e=>{let r=e*2;for(;!j(r,n);)e=P(e),r=e*2;return M(e,t)},this.getRangeEnd=e=>{let r=e*2;for(;!j(r,n);)e=F(e,t),r=e*2;return M(e,t)+N(r,n)};let i=(e,r,a)=>{let o=j(r*2,n);if(!e(a,o,r)&&!o){let n=P(r),o=F(r,t);i(e,n,a+1),i(e,o,a+1)}};this.traverseBuffer=e=>{if(r)throw Error(`BVHTraversalHelper: cannot start a traversal during an active traversal.`);r=!0;try{i(e,0,0)}finally{r=!1}},this.traverse=r=>{this.traverseBuffer((i,a,o)=>{if(a){let s=o*2,c=t[o+6],l=n[s+14];return r(i,a,new Float32Array(e,o*4,6),c,l)}{let n=dt(o,t);return r(i,a,new Float32Array(e,o*4,6),n)}})}}},Yt=new We,Xt=new Float32Array(6),Zt=class{constructor(){this._roots=null,this.primitiveBuffer=null,this.primitiveBufferStride=null}init(e){e={...at,...e},`maxLeafSize`in e&&(console.warn(`BVH: "maxLeafSize" option has been deprecated. Use "targetLeafSize", instead.`),e={...e,targetLeafSize:e.maxLeafSize}),At(this,e)}getRootRanges(){throw Error(`BVH: getRootRanges() not implemented`)}writePrimitiveBounds(){throw Error(`BVH: writePrimitiveBounds() not implemented`)}writePrimitiveRangeBounds(e,t,n,r){let i=1/0,a=1/0,o=1/0,s=-1/0,c=-1/0,l=-1/0;for(let n=e,r=e+t;n<r;n++){this.writePrimitiveBounds(n,Xt,0);let[e,t,r,u,d,f]=Xt;e<i&&(i=e),u>s&&(s=u),t<a&&(a=t),d>c&&(c=d),r<o&&(o=r),f>l&&(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<a;i++){this.writePrimitiveBounds(i,Xt,0);let[e,t,a,o,s,c]=Xt,l=(e+o)/2,u=(t+s)/2,d=(a+c)/2,f=(o-e)/2,p=(s-t)/2,m=(c-a)/2,h=(i-r)*6;n[h+0]=l,n[h+1]=f+(Math.abs(l)+f)*rt,n[h+2]=u,n[h+3]=p+(Math.abs(u)+p)*rt,n[h+4]=d,n[h+5]=m+(Math.abs(d)+m)*rt}return n}shiftPrimitiveOffsets(e){let t=this._indirectBuffer;if(t)for(let n=0,r=t.length;n<r;n++)t[n]+=e;else{let t=this._roots;for(let n=0;n<t.length;n++){let r=t[n],i=new Uint32Array(r),a=new Uint16Array(r),o=r.byteLength/32;for(let t=0;t<o;t++){let n=8*t;j(2*n,a)&&(i[n+6]+=e)}}}}traverse(e,t=0){Jt.setBVH(this,t),Jt.traverse(e),Jt.reset()}refit(){let e=this._roots;for(let t=0,n=e.length;t<n;t++){let n=e[t],r=new Uint32Array(n),i=new Uint16Array(n),a=new Float32Array(n),o=n.byteLength/32;for(let e=o-1;e>=0;e--){let t=e*8,n=t*2;if(j(n,i)){let e=M(t,r),o=N(n,i);this.writePrimitiveRangeBounds(e,o,Xt,0),a.set(Xt,t)}else{let e=P(t),n=F(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]=i<s?i:s,a[t+r+3]=o>c?o:c}}}}}getBoundingBox(e){return e.makeEmpty(),this._roots.forEach(t=>{A(0,new Float32Array(t),Yt),e.union(Yt)}),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;e<i;e++){let i=l[e];if(s=It(this,e,n,r,t,c),s)break;c+=i.byteLength/32}return s}bvhcast(e,t,n){let{intersectsRanges:r}=n;return Kt(this,e,t,r)}};function Qt(){return typeof SharedArrayBuffer<`u`}function $t(e){return e.index?e.index.count:e.attributes.position.count}function en(e){return $t(e)/3}function tn(e,t=ArrayBuffer){return e>65535?new Uint32Array(new t(4*e)):new Uint16Array(new t(2*e))}function nn(e,t){if(!e.index){let n=e.attributes.position.count,r=tn(n,t.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer);e.setIndex(new C(r,1));for(let e=0;e<n;e++)r[e]=e}}function rn(e,t,n){let r=$t(e)/n,i=t||e.drawRange,a=i.start/n,o=(i.start+i.count)/n,s=Math.max(0,a),c=Math.min(r,o)-s;return{offset:Math.floor(s),count:Math.floor(c)}}function an(e,t){return e.groups.map(e=>({offset:e.start/t,count:e.count/t}))}function on(e,t,n){let r=rn(e,t,n),i=an(e,n);if(!i.length)return[r];let a=[],o=r.offset,s=r.offset+r.count,c=$t(e)/n,l=[];for(let e of i){let{offset:t,count:n}=e,r=t,i=t+(isFinite(n)?n:c-t);r<s&&i>o&&(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 sn(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;t<e.length;t++){let{offset:n,count:r}=e[t];for(let e=0;e<r;e++)s[c+e]=n+e;c+=r}return s}var cn=class extends Zt{get indirect(){return!!this._indirectBuffer}get primitiveStride(){return null}get primitiveBufferStride(){return this.indirect?1:this.primitiveStride}set primitiveBufferStride(e){}get primitiveBuffer(){return this.indirect?this._indirectBuffer:this.geometry.index.array}set primitiveBuffer(e){}constructor(e,t={}){if(!e.isBufferGeometry)throw Error(`BVH: Only BufferGeometries are supported.`);if(e.index&&e.index.isInterleavedBufferAttribute)throw Error(`BVH: InterleavedBufferAttribute is not supported for the index attribute.`);if(t.useSharedArrayBuffer&&!Qt())throw Error(`BVH: SharedArrayBuffer is not available.`);super(),this.geometry=e,this.resolvePrimitiveIndex=t.indirect?e=>this._indirectBuffer[e]:e=>e,this.primitiveBuffer=null,this.primitiveBufferStride=null,this._indirectBuffer=null,t={...at,...t},t[it]||this.init(t)}init(e){let{geometry:t,primitiveStride:n}=this;if(e.indirect){let r=sn(on(t,e.range,n),e.useSharedArrayBuffer);this._indirectBuffer=r}else nn(t,e);super.init(e),!t.boundingBox&&e.setBoundingBox&&(t.boundingBox=this.getBoundingBox(new We))}getRootRanges(e){return this.indirect?[{offset:0,count:this._indirectBuffer.length}]:on(this.geometry,e,this.primitiveStride)}raycastObject3D(){throw Error(`BVH: raycastObject3D() not implemented`)}},ln=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;i<a;i++){let a=e[i][t];n=a<n?a:n,r=a>r?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;i<a;i++){let a=t[i],o=e.dot(a);n=o<n?o:n,r=o>r?o:r}this.min=n,this.max=r}isSeparated(e){return this.min>e.max||e.min>this.max}};ln.prototype.setFromBox=(function(){let e=new k;return function(t,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++){e.x=r.x*n+i.x*(1-n),e.y=r.y*s+i.y*(1-s),e.z=r.z*c+i.z*(1-c);let l=t.dot(e);a=Math.min(l,a),o=Math.max(l,o)}this.min=a,this.max=o}})();var un=(function(){let e=new k,t=new k,n=new k;return function(r,i,a){let o=r.start,s=e,c=i.start,l=t;n.subVectors(o,c),e.subVectors(r.end,r.start),t.subVectors(i.end,i.start);let u=n.dot(l),d=l.dot(s),f=l.dot(l),p=n.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}})(),dn=(function(){let e=new E,t=new k,n=new k;return function(r,i,a,o){un(r,i,e);let s=e.x,c=e.y;if(s>=0&&s<=1&&c>=0&&c<=1){r.at(s,a),i.at(c,o);return}if(s>=0&&s<=1){c<0?i.at(0,o):i.at(1,o),r.closestPointToPoint(o,!0,a);return}if(c>=0&&c<=1){s<0?r.at(0,a):r.at(1,a),i.closestPointToPoint(a,!0,o);return}{let e;e=s<0?r.start:r.end;let l;l=c<0?i.start:i.end;let u=t,d=n;if(r.closestPointToPoint(l,!0,t),i.closestPointToPoint(e,!0,n),u.distanceToSquared(l)<=d.distanceToSquared(e)){a.copy(u),o.copy(l);return}a.copy(e),o.copy(d);return}}})(),fn=(function(){let t=new k,n=new k,r=new e,i=new u;return function(e,a){let{radius:o,center:s}=e,{a:c,b:l,c:u}=a;if(i.start=c,i.end=l,i.closestPointToPoint(s,!0,t).distanceTo(s)<=o||(i.start=c,i.end=u,i.closestPointToPoint(s,!0,t).distanceTo(s)<=o)||(i.start=l,i.end=u,i.closestPointToPoint(s,!0,t).distanceTo(s)<=o))return!0;let d=a.getPlane(r);if(Math.abs(d.distanceToPoint(s))<=o){let e=d.projectPoint(s,n);if(a.containsPoint(e))return!0}return!1}})(),pn=[`x`,`y`,`z`],mn=1e-15,hn=mn*mn;function gn(e){return Math.abs(e)<mn}var _n=class extends be{constructor(...t){super(...t),this.isExtendedTriangle=!0,this.satAxes=[,,,,].fill().map(()=>new k),this.satBounds=[,,,,].fill().map(()=>new ln),this.points=[this.a,this.b,this.c],this.plane=new e,this.isDegenerateIntoSegment=!1,this.isDegenerateIntoPoint=!1,this.degenerateSegment=new u,this.needsUpdate=!0}intersectsSphere(e){return fn(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,m<mn?h<mn||g<mn?this.isDegenerateIntoPoint=!0:(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(e),this.degenerateSegment.end.copy(n)):h<mn?g<mn?this.isDegenerateIntoPoint=!0:(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(t),this.degenerateSegment.end.copy(e)):g<mn&&(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(n),this.degenerateSegment.end.copy(t)),this.plane.setFromNormalAndCoplanarPoint(o,e),this.needsUpdate=!1}};_n.prototype.closestPointToSegment=(function(){let e=new k,t=new k,n=new u;return function(r,i=null,a=null){let{start:o,end:s}=r,c=this.points,l,u=1/0;for(let o=0;o<3;o++){let s=(o+1)%3;n.start.copy(c[o]),n.end.copy(c[s]),dn(n,r,e,t),l=e.distanceToSquared(t),l<u&&(u=l,i&&i.copy(e),a&&a.copy(t))}return this.closestPointToPoint(o,e),l=o.distanceToSquared(e),l<u&&(u=l,i&&i.copy(e),a&&a.copy(o)),this.closestPointToPoint(s,e),l=s.distanceToSquared(e),l<u&&(u=l,i&&i.copy(e),a&&a.copy(s)),Math.sqrt(u)}})(),_n.prototype.intersectsTriangle=(function(){let e=new _n,t=new ln,n=new ln,r=new k,i=new k,a=new k,o=new k,s=new u,c=new u,l=new k,d=new E,f=new E;function p(e,i,a,s){let c=r;!e.isDegenerateIntoPoint&&!e.isDegenerateIntoSegment?c.copy(e.plane.normal):c.copy(i.plane.normal);let l=e.satBounds,u=e.satAxes;for(let r=1;r<4;r++){let a=l[r],s=u[r];if(t.setFromPoints(s,i.points),a.isSeparated(t)||(o.copy(c).cross(s),t.setFromPoints(o,e.points),n.setFromPoints(o,i.points),t.isSeparated(n)))return!1}let d=i.satBounds,f=i.satAxes;for(let r=1;r<4;r++){let a=d[r],s=f[r];if(t.setFromPoints(s,e.points),a.isSeparated(t)||(o.crossVectors(c,s),t.setFromPoints(o,e.points),n.setFromPoints(o,i.points),t.isSeparated(n)))return!1}return a&&(s||console.warn(`ExtendedTriangle.intersectsTriangle: Triangles are coplanar which does not support an output edge. Setting edge to 0, 0, 0.`),a.start.set(0,0,0),a.end.set(0,0,0)),!0}function m(e,t,n,r,i,a,o,s,c,l,u){let d=o/(o-s);l.x=r+(i-r)*d,u.start.subVectors(t,e).multiplyScalar(d).add(e),d=o/(o-c),l.y=r+(a-r)*d,u.end.subVectors(n,e).multiplyScalar(d).add(e)}function h(e,t,n,r,i,a,o,s,c,l,u){if(i>0)m(e.c,e.a,e.b,r,t,n,c,o,s,l,u);else if(a>0)m(e.b,e.a,e.c,n,t,r,s,o,c,l,u);else if(s*c>0||o!=0)m(e.a,e.b,e.c,t,n,r,o,s,c,l,u);else if(s!=0)m(e.b,e.a,e.c,n,t,r,s,o,c,l,u);else if(c!=0)m(e.c,e.a,e.b,r,t,n,c,o,s,l,u);else return!0;return!1}function g(e,t,n,i){let a=t.degenerateSegment,o=e.plane.distanceToPoint(a.start),s=e.plane.distanceToPoint(a.end);return gn(o)?gn(s)?p(e,t,n,i):(n&&(n.start.copy(a.start),n.end.copy(a.start)),e.containsPoint(a.start)):gn(s)?(n&&(n.start.copy(a.end),n.end.copy(a.end)),e.containsPoint(a.end)):e.plane.intersectLine(a,r)!=null&&(n&&(n.start.copy(r),n.end.copy(r)),e.containsPoint(r))}function _(e,t,n){let r=t.a;return gn(e.plane.distanceToPoint(r))&&e.containsPoint(r)?(n&&(n.start.copy(r),n.end.copy(r)),!0):!1}function v(e,t,n){let i=e.degenerateSegment,a=t.a;return i.closestPointToPoint(a,!0,r),a.distanceToSquared(r)<hn&&(n&&(n.start.copy(a),n.end.copy(a)),!0)}function y(e,t,n,o){if(e.isDegenerateIntoSegment){if(t.isDegenerateIntoSegment){let o=e.degenerateSegment,s=t.degenerateSegment,c=i,l=a;o.delta(c),s.delta(l);let u=r.subVectors(s.start,o.start),d=c.x*l.y-c.y*l.x;if(gn(d))return!1;let f=(u.x*l.y-u.y*l.x)/d,p=-(c.x*u.y-c.y*u.x)/d;return f<0||f>1||p<0||p>1?!1:gn(o.start.z+c.z*f-(s.start.z+l.z*p))?(n&&(n.start.copy(o.start).addScaledVector(c,f),n.end.copy(o.start).addScaledVector(c,f)),!0):!1}return t.isDegenerateIntoPoint?v(e,t,n):g(t,e,n,o)}if(e.isDegenerateIntoPoint)return t.isDegenerateIntoPoint?t.a.distanceToSquared(e.a)<hn&&(n&&(n.start.copy(e.a),n.end.copy(e.a)),!0):t.isDegenerateIntoSegment?v(t,e,n):_(t,e,n);if(t.isDegenerateIntoPoint)return _(e,t,n);if(t.isDegenerateIntoSegment)return g(e,t,n,o)}return function(t,n=null,r=!1){this.needsUpdate&&this.update(),t.isExtendedTriangle?t.needsUpdate&&t.update():(e.copy(t),e.update(),t=e);let o=y(this,t,n,r);if(o!==void 0)return o;let u=this.plane,m=t.plane,g=m.distanceToPoint(this.a),_=m.distanceToPoint(this.b),v=m.distanceToPoint(this.c);gn(g)&&(g=0),gn(_)&&(_=0),gn(v)&&(v=0);let b=g*_,x=g*v;if(b>0&&x>0)return!1;let S=u.distanceToPoint(t.a),C=u.distanceToPoint(t.b),ee=u.distanceToPoint(t.c);gn(S)&&(S=0),gn(C)&&(C=0),gn(ee)&&(ee=0);let w=S*C,te=S*ee;if(w>0&&te>0)return!1;i.copy(u.normal),a.copy(m.normal);let ne=i.cross(a),re=0,ie=Math.abs(ne.x),T=Math.abs(ne.y);T>ie&&(ie=T,re=1),Math.abs(ne.z)>ie&&(re=2);let ae=pn[re],oe=this.a[ae],se=this.b[ae],ce=this.c[ae],le=t.a[ae],E=t.b[ae],ue=t.c[ae];if(h(this,oe,se,ce,b,x,g,_,v,d,s)||h(t,le,E,ue,w,te,S,C,ee,f,c))return p(this,t,n,r);if(d.y<d.x){let e=d.y;d.y=d.x,d.x=e,l.copy(s.start),s.start.copy(s.end),s.end.copy(l)}if(f.y<f.x){let e=f.y;f.y=f.x,f.x=e,l.copy(c.start),c.start.copy(c.end),c.end.copy(l)}return d.y<f.x||f.y<d.x?!1:(n&&(f.x>d.x?n.start.copy(c.start):n.start.copy(s.start),f.y<d.y?n.end.copy(c.end):n.end.copy(s.end)),!0)}})(),_n.prototype.distanceToPoint=(function(){let e=new k;return function(t){return this.closestPointToPoint(t,e),t.distanceTo(e)}})(),_n.prototype.distanceToTriangle=(function(){let e=new k,t=new k,n=[`a`,`b`,`c`],r=new u,i=new u;return function(a,o=null,s=null){let c=o||s?r:null;if(this.intersectsTriangle(a,c,!0))return(o||s)&&(o&&c.getCenter(o),s&&c.getCenter(s)),0;let l=1/0;for(let t=0;t<3;t++){let r,i=n[t],c=a[i];this.closestPointToPoint(c,e),r=c.distanceToSquared(e),r<l&&(l=r,o&&o.copy(e),s&&s.copy(c));let u=this[i];a.closestPointToPoint(u,e),r=u.distanceToSquared(e),r<l&&(l=r,o&&o.copy(u),s&&s.copy(e))}for(let c=0;c<3;c++){let u=n[c],d=n[(c+1)%3];r.set(this[u],this[d]);for(let c=0;c<3;c++){let u=n[c],d=n[(c+1)%3];i.set(a[u],a[d]),dn(r,i,e,t);let f=e.distanceToSquared(t);f<l&&(l=f,o&&o.copy(e),s&&s.copy(t))}}return Math.sqrt(l)}})();var vn=class{constructor(e,t,n){this.isOrientedBox=!0,this.min=new k,this.max=new k,this.matrix=new O,this.invMatrix=new O,this.points=Array(8).fill().map(()=>new k),this.satAxes=[,,,].fill().map(()=>new k),this.satBounds=[,,,].fill().map(()=>new ln),this.alignedSatBounds=[,,,].fill().map(()=>new ln),this.needsUpdate=!1,e&&this.min.copy(e),t&&this.max.copy(t),n&&this.matrix.copy(n)}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}};vn.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<<e];t.subVectors(o,s),n.setFromPoints(t,r)}let s=this.alignedSatBounds;s[0].setFromPointsField(r,`x`),s[1].setFromPointsField(r,`y`),s[2].setFromPointsField(r,`z`),this.invMatrix.copy(this.matrix).invert(),this.needsUpdate=!1}})(),vn.prototype.intersectsBox=(function(){let e=new ln;return function(t){this.needsUpdate&&this.update();let n=t.min,r=t.max,i=this.satBounds,a=this.satAxes,o=this.alignedSatBounds;if(e.min=n.x,e.max=r.x,o[0].isSeparated(e)||(e.min=n.y,e.max=r.y,o[1].isSeparated(e))||(e.min=n.z,e.max=r.z,o[2].isSeparated(e)))return!1;for(let n=0;n<3;n++){let r=a[n],o=i[n];if(e.setFromBox(r,t),o.isSeparated(e))return!1}return!0}})(),vn.prototype.intersectsTriangle=(function(){let e=new _n,t=[,,,],n=new ln,r=new ln,i=new k;return function(a){this.needsUpdate&&this.update(),a.isExtendedTriangle?a.needsUpdate&&a.update():(e.copy(a),e.update(),a=e);let o=this.satBounds,s=this.satAxes;t[0]=a.a,t[1]=a.b,t[2]=a.c;for(let e=0;e<3;e++){let r=o[e],i=s[e];if(n.setFromPoints(i,t),r.isSeparated(n))return!1}let c=a.satBounds,l=a.satAxes,u=this.points;for(let e=0;e<3;e++){let t=c[e],r=l[e];if(n.setFromPoints(r,u),t.isSeparated(n))return!1}for(let e=0;e<3;e++){let a=s[e];for(let e=0;e<4;e++){let o=l[e];if(i.crossVectors(a,o),n.setFromPoints(i,t),r.setFromPoints(i,u),n.isSeparated(r))return!1}}return!0}})(),vn.prototype.closestPointToPoint=(function(){return function(e,t){return this.needsUpdate&&this.update(),t.copy(e).applyMatrix4(this.invMatrix).clamp(this.min,this.max).applyMatrix4(this.matrix),t}})(),vn.prototype.distanceToPoint=(function(){let e=new k;return function(t){return this.closestPointToPoint(t,e),t.distanceTo(e)}})(),vn.prototype.distanceToBox=(function(){let e=[`x`,`y`,`z`],t=Array(12).fill().map(()=>new u),n=Array(12).fill().map(()=>new u),r=new k,i=new k;return function(a,o=0,s=null,c=null){if(this.needsUpdate&&this.update(),this.intersectsBox(a))return(s||c)&&(a.getCenter(i),this.closestPointToPoint(i,r),a.closestPointToPoint(r,i),s&&s.copy(r),c&&c.copy(i)),0;let l=o*o,u=a.min,d=a.max,f=this.points,p=1/0;for(let e=0;e<8;e++){let t=f[e];i.copy(t).clamp(u,d);let n=t.distanceToSquared(i);if(n<p&&(p=n,s&&s.copy(t),c&&c.copy(i),n<l))return Math.sqrt(n)}let m=0;for(let r=0;r<3;r++)for(let i=0;i<=1;i++)for(let a=0;a<=1;a++){let o=(r+1)%3,s=(r+2)%3,c=i<<o|a<<s,l=1<<r|i<<o|a<<s,p=f[c],h=f[l];t[m].set(p,h);let g=e[r],_=e[o],v=e[s],y=n[m],b=y.start,x=y.end;b[g]=u[g],b[_]=i?u[_]:d[_],b[v]=a?u[v]:d[_],x[g]=d[g],x[_]=i?u[_]:d[_],x[v]=a?u[v]:d[_],m++}for(let e=0;e<=1;e++)for(let t=0;t<=1;t++)for(let n=0;n<=1;n++){i.x=e?d.x:u.x,i.y=t?d.y:u.y,i.z=n?d.z:u.z,this.closestPointToPoint(i,r);let a=i.distanceToSquared(r);if(a<p&&(p=a,s&&s.copy(r),c&&c.copy(i),a<l))return Math.sqrt(a)}for(let e=0;e<12;e++){let a=t[e];for(let e=0;e<12;e++){let t=n[e];dn(a,t,r,i);let o=r.distanceToSquared(i);if(o<p&&(p=o,s&&s.copy(r),c&&c.copy(i),o<l))return Math.sqrt(o)}}return Math.sqrt(p)}})();var yn=new class extends jt{constructor(){super(()=>new _n)}},bn=new k,xn=new k;function Sn(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=>(bn.copy(t).clamp(e.min,e.max),bn.distanceToSquared(t)),intersectsBounds:(e,t,n)=>n<s&&n<o,intersectsTriangle:(e,n)=>{e.closestPointToPoint(t,bn);let r=t.distanceToSquared(bn);return r<s&&(xn.copy(bn),s=r,c=n),r<a}}),s===1/0)return null;let l=Math.sqrt(s);return n.point?n.point.copy(xn):n.point=xn.clone(),n.distance=l,n.faceIndex=c,n}var Cn=!0,wn=new k,Tn=new k,En=new k,Dn=new E,On=new E,kn=new E,An=new k,jn=new k,Mn=new k,Nn=new k;function Pn(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 l<o||l>s?null:{distance:l,point:i.clone()}}function Fn(e,t,n,r,i,a,o,s,c,l,u){wn.fromBufferAttribute(t,a),Tn.fromBufferAttribute(t,o),En.fromBufferAttribute(t,s);let d=Pn(e,wn,Tn,En,Nn,c,l,u);if(d){if(r){Dn.fromBufferAttribute(r,a),On.fromBufferAttribute(r,o),kn.fromBufferAttribute(r,s),d.uv=new E;let e=be.getInterpolation(Nn,wn,Tn,En,Dn,On,kn,d.uv);Cn||(d.uv=e)}if(i){Dn.fromBufferAttribute(i,a),On.fromBufferAttribute(i,o),kn.fromBufferAttribute(i,s),d.uv1=new E;let e=be.getInterpolation(Nn,wn,Tn,En,Dn,On,kn,d.uv1);Cn||(d.uv1=e)}if(n){An.fromBufferAttribute(n,a),jn.fromBufferAttribute(n,o),Mn.fromBufferAttribute(n,s),d.normal=new k;let t=be.getInterpolation(Nn,wn,Tn,En,An,jn,Mn,d.normal);d.normal.dot(e.direction)>0&&d.normal.multiplyScalar(-1),Cn||(d.normal=t)}let t={a,b:o,c:s,normal:new k,materialIndex:0};if(be.getNormal(wn,Tn,En,t.normal),d.face=t,d.faceIndex=a,Cn){let e=new k;be.getBarycoord(Nn,wn,Tn,En,e),d.barycoord=e}}return d}function In(e){return e&&e.isMaterial?e.side:e}function Ln(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;s++){let{start:d,count:_,materialIndex:v}=f[s];if(e>=d&&e<d+_){let e=In(t[v]),s=Fn(n,p,m,h,g,c,l,u,e,a,o);if(s){if(s.faceIndex=r,s.face.materialIndex=v,i)i.push(s);else return s}}}}else{let e=In(t),s=Fn(n,p,m,h,g,c,l,u,e,a,o);if(s){if(s.faceIndex=r,s.face.materialIndex=0,i)i.push(s);else return s}}return null}function R(e,t,n,r){let i=e.a,a=e.b,o=e.c,s=t,c=t+1,l=t+2;n&&(s=n.getX(s),c=n.getX(c),l=n.getX(l)),i.x=r.getX(s),i.y=r.getY(s),i.z=r.getZ(s),a.x=r.getX(c),a.y=r.getY(c),a.z=r.getZ(c),o.x=r.getX(l),o.y=r.getY(l),o.z=r.getZ(l)}function Rn(e,t,n,r,i,a,o,s){let{geometry:c,_indirectBuffer:l}=e;for(let e=r,l=r+i;e<l;e++)Ln(c,t,n,e,a,o,s)}function zn(e,t,n,r,i,a,o){let{geometry:s,_indirectBuffer:c}=e,l=1/0,u=null;for(let e=r,c=r+i;e<c;e++){let r;r=Ln(s,t,n,e,null,a,o),r&&r.distance<l&&(u=r,l=r.distance)}return u}function Bn(e,t,n,r,i,a,o){let{geometry:s}=n,{index:c}=s,l=s.attributes.position;for(let n=e,s=t+e;n<s;n++){let e;if(e=n,R(o,e*3,c,l),o.needsUpdate=!0,r(o,e,i,a))return!0}return!1}function Vn(e,t=null){t&&Array.isArray(t)&&(t=new Set(t));let n=e.geometry,r=n.index?n.index.array:null,i=n.attributes.position,a,o,s,c,l=0,u=e._roots;for(let e=0,t=u.length;e<t;e++)a=u[e],o=new Uint32Array(a),s=new Uint16Array(a),c=new Float32Array(a),d(0,l),l+=a.byteLength;function d(e,n,a=!1){let l=e*2;if(j(l,s)){let t=M(e,o),n=N(l,s),a=1/0,u=1/0,d=1/0,f=-1/0,p=-1/0,m=-1/0;for(let e=3*t,o=3*(t+n);e<o;e++){let t=r[e],n=i.getX(t),o=i.getY(t),s=i.getZ(t);n<a&&(a=n),n>f&&(f=n),o<u&&(u=o),o>p&&(p=o),s<d&&(d=s),s>m&&(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}{let r=P(e),i=F(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]=o<l?o:l,c[e+t+3]=s>u?s:u}return g}}}function Hn(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<o||isNaN(o))&&(o=c),p>=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<o||o!==o)&&(o=u),a<=i&&o>=r)}function Un(e,t,n,r,i,a,o,s){let{geometry:c,_indirectBuffer:l}=e;for(let e=r,u=r+i;e<u;e++)Ln(c,t,n,l?l[e]:e,a,o,s)}function Wn(e,t,n,r,i,a,o){let{geometry:s,_indirectBuffer:c}=e,l=1/0,u=null;for(let e=r,d=r+i;e<d;e++){let r;r=Ln(s,t,n,c?c[e]:e,null,a,o),r&&r.distance<l&&(u=r,l=r.distance)}return u}function Gn(e,t,n,r,i,a,o){let{geometry:s}=n,{index:c}=s,l=s.attributes.position;for(let s=e,u=t+e;s<u;s++){let e;if(e=n.resolveTriangleIndex(s),R(o,e*3,c,l),o.needsUpdate=!0,r(o,e,i,a))return!0}return!1}function Kn(e,t,n,r,i,a,o){L.setBuffer(e._roots[t]),qn(0,e,n,r,i,a,o),L.clearBuffer()}function qn(e,t,n,r,i,a,o){let{float32Array:s,uint16Array:c,uint32Array:l}=L,u=e*2;if(j(u,c))Rn(t,n,r,M(e,l),N(u,c),i,a,o);else{let c=P(e);Hn(c,s,r,a,o)&&qn(c,t,n,r,i,a,o);let u=F(e,l);Hn(u,s,r,a,o)&&qn(u,t,n,r,i,a,o)}}var Jn=[`x`,`y`,`z`];function Yn(e,t,n,r,i,a){L.setBuffer(e._roots[t]);let o=Xn(0,e,n,r,i,a);return L.clearBuffer(),o}function Xn(e,t,n,r,i,a){let{float32Array:o,uint16Array:s,uint32Array:c}=L,l=e*2;if(j(l,s))return zn(t,n,r,M(e,c),N(l,s),i,a);{let s=dt(e,c),l=Jn[s],u=r.direction[l]>=0,d,f;u?(d=P(e),f=F(e,c)):(d=F(e,c),f=P(e));let p=Hn(d,o,r,i,a)?Xn(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=Hn(f,o,r,i,a)?Xn(f,t,n,r,i,a):null;return p&&m?p.distance<=m.distance?p:m:p||m||null}}var Zn=new We,Qn=new _n,$n=new _n,er=new O,tr=new vn,nr=new vn;function rr(e,t,n,r){L.setBuffer(e._roots[t]);let i=ir(0,e,n,r);return L.clearBuffer(),i}function ir(e,t,n,r,i=null){let{float32Array:a,uint16Array:o,uint32Array:s}=L,c=e*2;if(i===null&&(n.boundingBox||n.computeBoundingBox(),tr.set(n.boundingBox.min,n.boundingBox.max,r),i=tr),j(c,o)){let i=t.geometry,l=i.index,u=i.attributes.position,d=n.index,f=n.attributes.position,p=M(e,s),m=N(c,o);if(er.copy(r).invert(),n.boundsTree)return A(I(e),a,nr),nr.matrix.copy(er),nr.needsUpdate=!0,n.boundsTree.shapecast({intersectsBounds:e=>nr.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;t<n;t+=3)if(R($n,t,l,u),$n.needsUpdate=!0,e.intersectsTriangle($n))return!0;return!1}});{let e=en(n);for(let t=p*3,n=(m+p)*3;t<n;t+=3){R(Qn,t,l,u),Qn.a.applyMatrix4(er),Qn.b.applyMatrix4(er),Qn.c.applyMatrix4(er),Qn.needsUpdate=!0;for(let t=0,n=e*3;t<n;t+=3)if(R($n,t,d,f),$n.needsUpdate=!0,Qn.intersectsTriangle($n))return!0}}}else{let o=P(e),c=F(e,s);return A(I(o),a,Zn),!!(i.intersectsBox(Zn)&&ir(o,t,n,r,i)||(A(I(c),a,Zn),i.intersectsBox(Zn)&&ir(c,t,n,r,i)))}}var ar=new O,or=new vn,sr=new vn,cr=new k,lr=new k,ur=new k,dr=new k;function fr(e,t,n,r={},i={},a=0,o=1/0){t.boundingBox||t.computeBoundingBox(),or.set(t.boundingBox.min,t.boundingBox.max,n),or.needsUpdate=!0;let s=e.geometry,c=s.attributes.position,l=s.index,u=t.attributes.position,d=t.index,f=yn.getPrimitive(),p=yn.getPrimitive(),m=cr,h=lr,g=null,_=null;i&&(g=ur,_=dr);let v=1/0,y=null,b=null;return ar.copy(n).invert(),sr.matrix.copy(ar),e.shapecast({boundsTraverseOrder:e=>or.distanceToBox(e),intersectsBounds:(e,t,n)=>n<v&&n<o&&(t&&(sr.min.copy(e.min),sr.max.copy(e.max),sr.needsUpdate=!0),!0),intersectsRange:(e,r)=>{if(t.boundsTree)return t.boundsTree.shapecast({boundsTraverseOrder:e=>sr.distanceToBox(e),intersectsBounds:(e,t,n)=>n<v&&n<o,intersectsRange:(t,i)=>{for(let o=t,s=t+i;o<s;o++){R(p,3*o,d,u),p.a.applyMatrix4(n),p.b.applyMatrix4(n),p.c.applyMatrix4(n),p.needsUpdate=!0;for(let t=e,n=e+r;t<n;t++){R(f,3*t,l,c),f.needsUpdate=!0;let e=f.distanceToTriangle(p,m,g);if(e<v&&(h.copy(m),_&&_.copy(g),v=e,y=t,b=o),e<a)return!0}}}});{let i=en(t);for(let t=0,o=i;t<o;t++){R(p,3*t,d,u),p.a.applyMatrix4(n),p.b.applyMatrix4(n),p.c.applyMatrix4(n),p.needsUpdate=!0;for(let n=e,i=e+r;n<i;n++){R(f,3*n,l,c),f.needsUpdate=!0;let e=f.distanceToTriangle(p,m,g);if(e<v&&(h.copy(m),_&&_.copy(g),v=e,y=n,b=t),e<a)return!0}}}}}),yn.releasePrimitive(f),yn.releasePrimitive(p),v===1/0?null:(r.point?r.point.copy(h):r.point=h.clone(),r.distance=v,r.faceIndex=y,i&&(i.point?i.point.copy(_):i.point=_.clone(),i.point.applyMatrix4(ar),h.applyMatrix4(ar),i.distance=h.sub(i.point).length(),i.faceIndex=b),r)}function pr(e,t=null){t&&Array.isArray(t)&&(t=new Set(t));let n=e.geometry,r=n.index?n.index.array:null,i=n.attributes.position,a,o,s,c,l=0,u=e._roots;for(let e=0,t=u.length;e<t;e++)a=u[e],o=new Uint32Array(a),s=new Uint16Array(a),c=new Float32Array(a),d(0,l),l+=a.byteLength;function d(n,a,l=!1){let u=n*2;if(j(u,s)){let t=M(n,o),a=N(u,s),l=1/0,d=1/0,f=1/0,p=-1/0,m=-1/0,h=-1/0;for(let n=t,o=t+a;n<o;n++){let t=3*e.resolveTriangleIndex(n);for(let e=0;e<3;e++){let n=t+e;n=r?r[n]:n;let a=i.getX(n),o=i.getY(n),s=i.getZ(n);a<l&&(l=a),a>p&&(p=a),o<d&&(d=o),o>m&&(m=o),s<f&&(f=s),s>h&&(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}{let e=P(n),r=F(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]=o<l?o:l,c[n+t+3]=s>u?s:u}return g}}}function mr(e,t,n,r,i,a,o){L.setBuffer(e._roots[t]),hr(0,e,n,r,i,a,o),L.clearBuffer()}function hr(e,t,n,r,i,a,o){let{float32Array:s,uint16Array:c,uint32Array:l}=L,u=e*2;if(j(u,c))Un(t,n,r,M(e,l),N(u,c),i,a,o);else{let c=P(e);Hn(c,s,r,a,o)&&hr(c,t,n,r,i,a,o);let u=F(e,l);Hn(u,s,r,a,o)&&hr(u,t,n,r,i,a,o)}}var gr=[`x`,`y`,`z`];function _r(e,t,n,r,i,a){L.setBuffer(e._roots[t]);let o=vr(0,e,n,r,i,a);return L.clearBuffer(),o}function vr(e,t,n,r,i,a){let{float32Array:o,uint16Array:s,uint32Array:c}=L,l=e*2;if(j(l,s))return Wn(t,n,r,M(e,c),N(l,s),i,a);{let s=dt(e,c),l=gr[s],u=r.direction[l]>=0,d,f;u?(d=P(e),f=F(e,c)):(d=F(e,c),f=P(e));let p=Hn(d,o,r,i,a)?vr(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=Hn(f,o,r,i,a)?vr(f,t,n,r,i,a):null;return p&&m?p.distance<=m.distance?p:m:p||m||null}}var yr=new We,br=new _n,xr=new _n,Sr=new O,Cr=new vn,wr=new vn;function Tr(e,t,n,r){L.setBuffer(e._roots[t]);let i=Er(0,e,n,r);return L.clearBuffer(),i}function Er(e,t,n,r,i=null){let{float32Array:a,uint16Array:o,uint32Array:s}=L,c=e*2;if(i===null&&(n.boundingBox||n.computeBoundingBox(),Cr.set(n.boundingBox.min,n.boundingBox.max,r),i=Cr),j(c,o)){let i=t.geometry,l=i.index,u=i.attributes.position,d=n.index,f=n.attributes.position,p=M(e,s),m=N(c,o);if(Sr.copy(r).invert(),n.boundsTree)return A(I(e),a,wr),wr.matrix.copy(Sr),wr.needsUpdate=!0,n.boundsTree.shapecast({intersectsBounds:e=>wr.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;n<r;n++)if(R(xr,3*t.resolveTriangleIndex(n),l,u),xr.needsUpdate=!0,e.intersectsTriangle(xr))return!0;return!1}});{let e=en(n);for(let n=p,r=m+p;n<r;n++){R(br,3*t.resolveTriangleIndex(n),l,u),br.a.applyMatrix4(Sr),br.b.applyMatrix4(Sr),br.c.applyMatrix4(Sr),br.needsUpdate=!0;for(let t=0,n=e*3;t<n;t+=3)if(R(xr,t,d,f),xr.needsUpdate=!0,br.intersectsTriangle(xr))return!0}}}else{let o=P(e),c=F(e,s);return A(I(o),a,yr),!!(i.intersectsBox(yr)&&Er(o,t,n,r,i)||(A(I(c),a,yr),i.intersectsBox(yr)&&Er(c,t,n,r,i)))}}var Dr=new O,Or=new vn,kr=new vn,Ar=new k,jr=new k,Mr=new k,Nr=new k;function Pr(e,t,n,r={},i={},a=0,o=1/0){t.boundingBox||t.computeBoundingBox(),Or.set(t.boundingBox.min,t.boundingBox.max,n),Or.needsUpdate=!0;let s=e.geometry,c=s.attributes.position,l=s.index,u=t.attributes.position,d=t.index,f=yn.getPrimitive(),p=yn.getPrimitive(),m=Ar,h=jr,g=null,_=null;i&&(g=Mr,_=Nr);let v=1/0,y=null,b=null;return Dr.copy(n).invert(),kr.matrix.copy(Dr),e.shapecast({boundsTraverseOrder:e=>Or.distanceToBox(e),intersectsBounds:(e,t,n)=>n<v&&n<o&&(t&&(kr.min.copy(e.min),kr.max.copy(e.max),kr.needsUpdate=!0),!0),intersectsRange:(r,i)=>{if(t.boundsTree){let s=t.boundsTree;return s.shapecast({boundsTraverseOrder:e=>kr.distanceToBox(e),intersectsBounds:(e,t,n)=>n<v&&n<o,intersectsRange:(t,o)=>{for(let x=t,S=t+o;x<S;x++){let t=s.resolveTriangleIndex(x);R(p,3*t,d,u),p.a.applyMatrix4(n),p.b.applyMatrix4(n),p.c.applyMatrix4(n),p.needsUpdate=!0;for(let t=r,n=r+i;t<n;t++){let n=e.resolveTriangleIndex(t);R(f,3*n,l,c),f.needsUpdate=!0;let r=f.distanceToTriangle(p,m,g);if(r<v&&(h.copy(m),_&&_.copy(g),v=r,y=t,b=x),r<a)return!0}}}})}{let o=en(t);for(let t=0,s=o;t<s;t++){R(p,3*t,d,u),p.a.applyMatrix4(n),p.b.applyMatrix4(n),p.c.applyMatrix4(n),p.needsUpdate=!0;for(let n=r,o=r+i;n<o;n++){let r=e.resolveTriangleIndex(n);R(f,3*r,l,c),f.needsUpdate=!0;let i=f.distanceToTriangle(p,m,g);if(i<v&&(h.copy(m),_&&_.copy(g),v=i,y=n,b=t),i<a)return!0}}}}}),yn.releasePrimitive(f),yn.releasePrimitive(p),v===1/0?null:(r.point?r.point.copy(h):r.point=h.clone(),r.distance=v,r.faceIndex=y,i&&(i.point?i.point.copy(_):i.point=_.clone(),i.point.applyMatrix4(Dr),h.applyMatrix4(Dr),i.distance=h.sub(i.point).length(),i.faceIndex=b),r)}function Fr(e,t,n){return e===null?null:(e.point.applyMatrix4(t.matrixWorld),e.distance=e.point.distanceTo(n.ray.origin),e.object=t,e)}var Ir=new vn,Lr=new je,Rr=new k,zr=new O,Br=new k,Vr=[`getX`,`getY`,`getZ`],Hr=class e extends cn{static serialize(e,t={}){t={cloneBuffers:!0,...t};let n=e.geometry,r=e._roots,i=e._indirectBuffer,a=n.getIndex(),o={version:1,roots:null,index:null,indirectBuffer:null};return t.cloneBuffers?(o.roots=r.map(e=>e.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,[it]:!0});if(s._roots=a,s._indirectBuffer=o||null,r.setIndex){let e=n.getIndex();if(e===null){let e=new C(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;t<e.length;t++){let n=e[t],r=new Uint32Array(n),i=new Uint16Array(n);for(let e=0,t=n.byteLength/32;e<t;e++){let t=8*e;j(2*t,i)||(r[t+6]=r[t+6]/8-e)}}}}get primitiveStride(){return 3}get resolveTriangleIndex(){return this.resolvePrimitiveIndex}constructor(e,t={}){t.maxLeafTris&&(console.warn(`MeshBVH: "maxLeafTris" option has been deprecated. Use "targetLeafSize", instead.`),t={...t,targetLeafSize:t.maxLeafTris}),super(e,t)}shiftTriangleOffsets(e){return super.shiftPrimitiveOffsets(e)}writePrimitiveBounds(e,t,n){let r=this.geometry,i=this._indirectBuffer,a=r.attributes.position,o=r.index?r.index.array:null,s=(i?i[e]:e)*3,c=s+0,l=s+1,u=s+2;o&&(c=o[c],l=o[l],u=o[u]);for(let e=0;e<3;e++){let r=a[Vr[e]](c),i=a[Vr[e]](l),o=a[Vr[e]](u),s=r;i<s&&(s=i),o<s&&(s=o);let d=r;i>d&&(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;r<p;r++){let e=(i?i[r]:r)*3,t=(r-f)*6,p=e+0,m=e+1,h=e+2;o&&(p=o[p],m=o[m],h=o[h]),s||(p=p*u+l,m=m*u+l,h=h*u+l);for(let e=0;e<3;e++){let r,i,o;s?(r=a[d[e]](p),i=a[d[e]](m),o=a[d[e]](h)):(r=c[p+e],i=c[m+e],o=c[h+e]);let l=r;i<l&&(l=i),o<l&&(l=o);let u=r;i>u&&(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)*rt}}return n}raycastObject3D(e,t,n=[]){let{material:r}=e;if(r===void 0)return;zr.copy(e.matrixWorld).invert(),Lr.copy(t.ray).applyMatrix4(zr),Br.setFromMatrixScale(e.matrixWorld),Rr.copy(Lr.direction).multiply(Br);let i=Rr.length(),a=t.near/i,o=t.far/i;if(t.firstHitOnly===!0){let i=this.raycastFirst(Lr,r,a,o);i=Fr(i,e,t),i&&n.push(i)}else{let i=this.raycast(Lr,r,a,o);for(let r=0,a=i.length;r<a;r++){let a=Fr(i[r],e,t);a&&n.push(a)}}return n}refit(e=null){return(this.indirect?pr:Vn)(this,e)}raycast(e,t=0,n=0,r=1/0){let i=this._roots,a=[],o=this.indirect?mr:Kn;for(let s=0,c=i.length;s<c;s++)o(this,s,t,e,a,n,r);return a}raycastFirst(e,t=0,n=0,r=1/0){let i=this._roots,a=null,o=this.indirect?_r:Yn;for(let s=0,c=i.length;s<c;s++){let i=o(this,s,t,e,n,r);i!=null&&(a==null||i.distance<a.distance)&&(a=i)}return a}intersectsGeometry(e,t){let n=!1,r=this._roots,i=this.indirect?Tr:rr;for(let a=0,o=r.length;a<o&&(n=i(this,a,e,t),!n);a++);return n}shapecast(e){let t=yn.getPrimitive(),n=super.shapecast({...e,intersectsPrimitive:e.intersectsTriangle,scratchPrimitive:t,iterate:this.indirect?Gn:Bn});return yn.releasePrimitive(t),n}bvhcast(t,n,r){let{intersectsRanges:i,intersectsTriangles:a}=r,o=yn.getPrimitive(),s=this.geometry.index,c=this.geometry.attributes.position,l=this.indirect?e=>{let t=this.resolveTriangleIndex(e);R(o,t*3,s,c)}:e=>{R(o,e*3,s,c)},u=yn.getPrimitive(),d=t.geometry.index,f=t.geometry.attributes.position,p=t.indirect?e=>{let n=t.resolveTriangleIndex(e);R(u,n*3,d,f)}:e=>{R(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;m<h;m++){p(m),u.a.applyMatrix4(n),u.b.applyMatrix4(n),u.c.applyMatrix4(n),u.needsUpdate=!0;for(let n=e,r=e+t;n<r;n++)if(l(n),o.needsUpdate=!0,a(o,u,n,m,s,c,d,f))return!0}return!1};if(i){let e=i;i=function(t,n,i,a,o,s,c,l){return e(t,n,i,a,o,s,c,l)?!0:r(t,n,i,a,o,s,c,l)}}else i=r}return super.bvhcast(t,n,{intersectsRanges:i})}intersectsBox(e,t){return Ir.set(e.min,e.max,t),Ir.needsUpdate=!0,this.shapecast({intersectsBounds:e=>Ir.intersectsBox(e),intersectsTriangle:e=>Ir.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?Pr:fr)(this,e,t,n,r,i,a)}closestPointToPoint(e,t={},n=0,r=1/0){return Sn(this,e,t,n,r)}};function Ur(e){switch(e){case 1:return`R`;case 2:return`RG`;case 3:return`RGBA`;case 4:return`RGBA`}throw Error()}function Wr(e){switch(e){case 1:return Le;case 2:return Se;case 3:return D;case 4:return D}}function Gr(e){switch(e){case 1:return de;case 2:return ce;case 3:return Fe;case 4:return Fe}}var Kr=class extends h{constructor(){super(),this.minFilter=T,this.magFilter=T,this.generateMipmaps=!1,this.overrideItemSize=null,this._forcedType=null}updateFrom(e){let t=this.overrideItemSize,n=e.itemSize,r=e.count;if(t!==null){if(n*r%t!==0)throw Error(`VertexAttributeTexture: overrideItemSize must divide evenly into buffer length.`);e.itemSize=t,e.count=r*n/t}let i=e.itemSize,o=e.count,s=e.normalized,c=e.array.constructor,l=c.BYTES_PER_ELEMENT,u=this._forcedType,f=i;if(u===null)switch(c){case Float32Array:u=d;break;case Uint8Array:case Uint16Array:case Uint32Array:u=Ue;break;case Int8Array:case Int16Array:case Int32Array:u=te}let p,m,h,g,_=Ur(i);switch(u){case d:h=1,m=Wr(i),s&&l===1?(g=c,_+=`8`,c===Uint8Array?p=oe:(p=a,_+=`_SNORM`)):(g=Float32Array,_+=`32F`,p=d);break;case te:_+=l*8+`I`,h=s?2**(c.BYTES_PER_ELEMENT*8-1):1,m=Gr(i),l===1?(g=Int8Array,p=a):l===2?(g=Int16Array,p=ge):(g=Int32Array,p=te);break;case Ue:_+=l*8+`UI`,h=s?2**(c.BYTES_PER_ELEMENT*8-1):1,m=Gr(i),l===1?(g=Uint8Array,p=oe):l===2?(g=Uint16Array,p=Je):(g=Uint32Array,p=Ue)}f===3&&(m===1023||m===1033)&&(f=4);let v=Math.ceil(Math.sqrt(o))||1,y=f*v*v,b=new g(y),x=e.normalized;e.normalized=!1;for(let t=0;t<o;t++){let n=f*t;b[n]=e.getX(t)/h,i>=2&&(b[n+1]=e.getY(t)/h),i>=3&&(b[n+2]=e.getZ(t)/h,f===4&&(b[n+3]=1)),i>=4&&(b[n+3]=e.getW(t)/h)}e.normalized=x,this.internalFormat=_,this.format=m,this.type=p,this.image.width=v,this.image.height=v,this.image.data=b,this.needsUpdate=!0,this.dispose(),e.itemSize=n,e.count=r}},qr=class extends Kr{constructor(){super(),this._forcedType=Ue}},Jr=class extends Kr{constructor(){super(),this._forcedType=d}},Yr=class{constructor(){this.index=new qr,this.position=new Jr,this.bvhBounds=new h,this.bvhContents=new h,this._cachedIndexAttr=null,this.index.overrideItemSize=3}updateFrom(e){let{geometry:t}=e;if(Zr(e,this.bvhBounds,this.bvhContents),this.position.updateFrom(t.attributes.position),e.indirect){let n=e._indirectBuffer;if(this._cachedIndexAttr===null||this._cachedIndexAttr.count!==n.length){if(t.index)this._cachedIndexAttr=t.index.clone();else{let e=tn($t(t));this._cachedIndexAttr=new C(e,1,!1)}}Xr(t,n,this._cachedIndexAttr),this.index.updateFrom(this._cachedIndexAttr)}else this.index.updateFrom(t.index)}dispose(){let{index:e,position:t,bvhBounds:n,bvhContents:r}=this;e&&e.dispose(),t&&t.dispose(),n&&n.dispose(),r&&r.dispose()}};function Xr(e,t,n){let r=n.array,i=e.index?e.index.array:null;for(let e=0,n=t.length;e<n;e++){let n=3*e,a=3*t[e];for(let e=0;e<3;e++)r[n+e]=i?i[a+e]:a+e}}function Zr(e,t,n){let r=e._roots;if(r.length!==1)throw Error(`MeshBVHUniformStruct: Multi-root BVHs not supported.`);let i=r[0],a=new Uint16Array(i),o=new Uint32Array(i),s=new Float32Array(i),c=i.byteLength/32,l=2*Math.ceil(Math.sqrt(c/2)),u=new Float32Array(4*l*l),f=Math.ceil(Math.sqrt(c)),p=new Uint32Array(2*f*f);for(let e=0;e<c;e++){let t=e*32/4,n=t*2,r=I(t);for(let t=0;t<3;t++)u[8*e+0+t]=s[r+0+t],u[8*e+4+t]=s[r+3+t];if(j(n,a)){let r=N(n,a),i=M(t,o),s=nt|r;p[e*2+0]=s,p[e*2+1]=i}else{let n=o[t+6],r=dt(t,o);p[e*2+0]=r,p[e*2+1]=n}}t.image.data=u,t.image.width=l,t.image.height=l,t.format=D,t.type=d,t.internalFormat=`RGBA32F`,t.minFilter=T,t.magFilter=T,t.generateMipmaps=!1,t.needsUpdate=!0,t.dispose(),n.image.data=p,n.image.width=f,n.image.height=f,n.format=ce,n.type=Ue,n.internalFormat=`RG32UI`,n.minFilter=T,n.magFilter=T,n.generateMipmaps=!1,n.needsUpdate=!0,n.dispose()}var Qr=`
2
2
 
3
3
  // A stack of uint32 indices can can store the indices for
4
4
  // a perfectly balanced tree with a depth up to 31. Lower stack
@@ -80,7 +80,7 @@ void ndcToCameraRay(
80
80
  rayDirection = direction.xyz;
81
81
 
82
82
  }
83
- `,Wr=`
83
+ `,$r=`
84
84
 
85
85
  #ifndef TRI_INTERSECT_EPSILON
86
86
  #define TRI_INTERSECT_EPSILON 1e-5
@@ -284,7 +284,7 @@ bool _bvhIntersectFirstHit(
284
284
  return found;
285
285
 
286
286
  }
287
- `,Gr=`
287
+ `,ei=`
288
288
  struct BVH {
289
289
 
290
290
  usampler2D index;
@@ -294,7 +294,7 @@ struct BVH {
294
294
  usampler2D bvhContents;
295
295
 
296
296
  };
297
- `;function Kr(e,t,n=0){if(e.isInterleavedBufferAttribute){let r=e.itemSize;for(let i=0,a=e.count;i<a;i++){let a=i+n;t.setX(a,e.getX(i)),r>=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 qr(e,t=null){let n=e.array.constructor,r=e.normalized,i=e.itemSize,a=t===null?e.count:t;return new xe(new n(i*a),i,r)}function Jr(e,t){if(!e&&!t)return!0;if(!!e!=!!t)return!1;let n=e.count===t.count,r=e.normalized===t.normalized,i=e.array.constructor===t.array.constructor,a=e.itemSize===t.itemSize;return!(!n||!r||!i||!a)}function Yr(e){let t=e[0].index!==null,n=new Set(Object.keys(e[0].attributes));if(!e[0].getAttribute(`position`))throw Error(`StaticGeometryGenerator: position attribute is required.`);for(let r=0;r<e.length;++r){let i=e[r],a=0;if(t!==(i.index!==null))throw Error(`StaticGeometryGenerator: All geometries must have compatible attributes; make sure index attribute exists among all geometries, or in none of them.`);for(let e in i.attributes){if(!n.has(e))throw Error(`StaticGeometryGenerator: All geometries must have compatible attributes; make sure "`+e+`" attribute exists among all geometries, or in none of them.`);a++}if(a!==n.size)throw Error(`StaticGeometryGenerator: All geometries must have the same number of attributes.`)}}function Xr(e){let t=0;for(let n=0,r=e.length;n<r;n++)t+=e[n].getIndex().count;return t}function Zr(e){let t=0;for(let n=0,r=e.length;n<r;n++)t+=e[n].getAttribute(`position`).count;return t}function Qr(e,t,n){e.index&&e.index.count!==t&&e.setIndex(null);let r=e.attributes;for(let t in r)r[t].count!==n&&e.deleteAttribute(t)}function $r(e,t={},n=new me){let{useGroups:r=!1,forceUpdate:i=!1,skipAssigningAttributes:a=[],overwriteIndex:o=!0}=t;Yr(e);let s=e[0].index!==null,c=s?Xr(e):-1,l=Zr(e);if(Qr(n,c,l),r){let t=0;for(let r=0,i=e.length;r<i;r++){let i=e[r],a;a=s?i.getIndex().count:i.getAttribute(`position`).count,n.addGroup(t,a,r),t+=a}}if(s){let t=!1;if(n.index||(n.setIndex(new xe(new Uint32Array(c),1,!1)),t=!0),t||o){let r=0,o=0,s=n.getIndex();for(let n=0,c=e.length;n<c;n++){let c=e[n],l=c.getIndex();if(i||t||!a[n])for(let e=0;e<l.count;++e)s.setX(r+e,l.getX(e)+o);r+=l.count,o+=c.getAttribute(`position`).count}}}let u=Object.keys(e[0].attributes);for(let t=0,r=u.length;t<r;t++){let r=!1,o=u[t];if(!n.getAttribute(o)){let t=e[0].getAttribute(o);n.setAttribute(o,qr(t,l)),r=!0}let s=0,c=n.getAttribute(o);for(let t=0,n=e.length;t<n;t++){let n=e[t],l=!i&&!r&&a[t],u=n.getAttribute(o);if(!l){if(o===`color`&&c.itemSize!==u.itemSize)for(let e=s,t=u.count;e<t;e++)u.setXYZW(e,c.getX(e),c.getY(e),c.getZ(e),1);else Kr(u,c,s)}s+=u.count}}}function ei(e,t,n){let r=e.index,i=e.attributes.position.count,a=r?r.count:i,o=e.groups;o.length===0&&(o=[{count:a,start:0,materialIndex:0}]);let s=e.getAttribute(`materialIndex`);if(!s||s.count!==i){let t;t=n.length<=255?new Uint8Array(i):new Uint16Array(i),s=new xe(t,1,!1),e.deleteAttribute(`materialIndex`),e.setAttribute(`materialIndex`,s)}let c=s.array;for(let e=0;e<o.length;e++){let i=o[e],s=i.start,l=i.count,u=Math.min(l,a-s),d=Array.isArray(t)?t[i.materialIndex]:t,f=n.indexOf(d);for(let e=0;e<u;e++){let t=s+e;r&&(t=r.getX(t)),c[t]=f}}}function ti(e,t){if(!e.index){let t=e.attributes.position.count,n=Array(t);for(let e=0;e<t;e++)n[e]=e;e.setIndex(n)}if(!e.attributes.normal&&t&&t.includes(`normal`)&&e.computeVertexNormals(),!e.attributes.uv&&t&&t.includes(`uv`)){let t=e.attributes.position.count;e.setAttribute(`uv`,new xe(new Float32Array(t*2),2,!1))}if(!e.attributes.uv2&&t&&t.includes(`uv2`)){let t=e.attributes.position.count;e.setAttribute(`uv2`,new xe(new Float32Array(t*2),2,!1))}if(!e.attributes.tangent&&t&&t.includes(`tangent`)){if(e.attributes.uv&&e.attributes.normal)e.computeTangents();else{let t=e.attributes.position.count;e.setAttribute(`tangent`,new xe(new Float32Array(t*4),4,!1))}}if(!e.attributes.color&&t&&t.includes(`color`)){let t=e.attributes.position.count,n=new Float32Array(t*4);n.fill(1),e.setAttribute(`color`,new xe(n,4))}}function ni(e){let t=0;if(e.byteLength!==0){let n=new Uint8Array(e);for(let r=0;r<e.byteLength;r++){let e=n[r];t=(t<<5)-t+e,t|=0}}return t}function ri(e){let t=e.uuid,n=Object.values(e.attributes);e.index&&(n.push(e.index),t+=`index|${e.index.version}`);let r=Object.keys(n).sort();for(let e of r){let r=n[e];t+=`${e}_${r.version}|`}return t}function ii(e){let t=e.skeleton;return t?(t.boneTexture||t.computeBoneTexture(),`${ni(t.boneTexture.image.data.buffer)}_${t.boneTexture.uuid}`):null}var ai=class{constructor(e=null){this.matrixWorld=new f,this.geometryHash=null,this.skeletonHash=null,this.primitiveCount=-1,e!==null&&this.updateFrom(e)}updateFrom(e){let t=e.geometry,n=(t.index?t.index.count:t.attributes.position.count)/3;this.matrixWorld.copy(e.matrixWorld),this.geometryHash=ri(t),this.primitiveCount=n,this.skeletonHash=ii(e)}didChange(e){let t=e.geometry,n=(t.index?t.index.count:t.attributes.position.count)/3;return!(this.matrixWorld.equals(e.matrixWorld)&&this.geometryHash===ri(t)&&this.skeletonHash===ii(e)&&this.primitiveCount===n)}},oi=new E,si=new E,ci=new E,li=new le,ui=new E,di=new E,fi=new le,pi=new le,mi=new f,hi=new f;function gi(e,t,n){let r=e.skeleton,i=e.geometry,a=r.bones,o=r.boneInverses;fi.fromBufferAttribute(i.attributes.skinIndex,t),pi.fromBufferAttribute(i.attributes.skinWeight,t),mi.elements.fill(0);for(let e=0;e<4;e++){let t=pi.getComponent(e);if(t!==0){let n=fi.getComponent(e);hi.multiplyMatrices(a[n].matrixWorld,o[n]),vi(mi,hi,t)}}return mi.multiply(e.bindMatrix).premultiply(e.bindMatrixInverse),n.transformDirection(mi),n}function _i(e,t,n,r,i){ui.set(0,0,0);for(let a=0,o=e.length;a<o;a++){let o=t[a],s=e[a];o!==0&&(di.fromBufferAttribute(s,r),n?ui.addScaledVector(di,o):ui.addScaledVector(di.sub(i),o))}i.add(ui)}function vi(e,t,n){let r=e.elements,i=t.elements;for(let e=0,t=i.length;e<t;e++)r[e]+=i[e]*n}function yi(e){let{index:t,attributes:n}=e;if(t)for(let e=0,n=t.count;e<n;e+=3){let n=t.getX(e),r=t.getX(e+2);t.setX(e,r),t.setX(e+2,n)}else for(let e in n){let t=n[e],r=t.itemSize;for(let e=0,n=t.count;e<n;e+=3)for(let n=0;n<r;n++){let r=t.getComponent(e,n),i=t.getComponent(e+2,n);t.setComponent(e,n,i),t.setComponent(e+2,n,r)}}return e}function bi(e,t={},n=new me){t={applyWorldTransforms:!0,attributes:[],...t};let r=e.geometry,i=t.applyWorldTransforms,a=t.attributes.includes(`normal`),o=t.attributes.includes(`tangent`),s=r.attributes,c=n.attributes;for(let e in n.attributes)(!t.attributes.includes(e)||!(e in r.attributes))&&n.deleteAttribute(e);!n.index&&r.index&&(n.index=r.index.clone()),c.position||n.setAttribute(`position`,qr(s.position)),a&&!c.normal&&s.normal&&n.setAttribute(`normal`,qr(s.normal)),o&&!c.tangent&&s.tangent&&n.setAttribute(`tangent`,qr(s.tangent)),Jr(r.index,n.index),Jr(s.position,c.position),a&&Jr(s.normal,c.normal),o&&Jr(s.tangent,c.tangent);let u=s.position,d=a?s.normal:null,f=o?s.tangent:null,p=r.morphAttributes.position,m=r.morphAttributes.normal,h=r.morphAttributes.tangent,g=r.morphTargetsRelative,_=e.morphTargetInfluences,v=new l;v.getNormalMatrix(e.matrixWorld),r.index&&n.index.array.set(r.index.array);for(let t=0,n=s.position.count;t<n;t++)oi.fromBufferAttribute(u,t),d&&si.fromBufferAttribute(d,t),f&&(li.fromBufferAttribute(f,t),ci.fromBufferAttribute(f,t)),_&&(p&&_i(p,_,g,t,oi),m&&_i(m,_,g,t,si),h&&_i(h,_,g,t,ci)),e.isSkinnedMesh&&(e.applyBoneTransform(t,oi),d&&gi(e,t,si),f&&gi(e,t,ci)),i&&oi.applyMatrix4(e.matrixWorld),c.position.setXYZ(t,oi.x,oi.y,oi.z),d&&(i&&si.applyNormalMatrix(v),c.normal.setXYZ(t,si.x,si.y,si.z)),f&&(i&&ci.transformDirection(e.matrixWorld),c.tangent.setXYZW(t,ci.x,ci.y,ci.z,li.w));for(let e in t.attributes){let r=t.attributes[e];r!==`position`&&r!==`tangent`&&r!==`normal`&&r in s&&(c[r]||n.setAttribute(r,qr(s[r])),Jr(s[r],c[r]),Kr(s[r],c[r]))}return e.matrixWorld.determinant()<0&&yi(n),n}var xi=class extends me{constructor(){super(),this.version=0,this.hash=null,this._diff=new ai}isCompatible(e,t){let n=e.geometry;for(let e=0;e<t.length;e++){let r=t[e],i=n.attributes[r],a=this.attributes[r];if(i&&!Jr(i,a))return!1}return!0}updateFrom(e,t){let n=this._diff;return n.didChange(e)?(bi(e,t,this),n.updateFrom(e),this.version++,this.hash=`${this.uuid}_${this.version}`,!0):!1}};function Si(e,t){for(let n=0,r=e.length;n<r;n++)e[n].traverseVisible(e=>{e.isMesh&&t(e)})}function Ci(e){let t=[];for(let n=0,r=e.length;n<r;n++){let r=e[n];Array.isArray(r.material)?t.push(...r.material):t.push(r.material)}return t}function wi(e,t,n){if(e.length===0){t.setIndex(null);let e=t.attributes;for(let n in e)t.deleteAttribute(n);for(let e in n.attributes)t.setAttribute(n.attributes[e],new xe(new Float32Array,4,!1))}else $r(e,n,t);for(let e in t.attributes)t.attributes[e].needsUpdate=!0}var Ti=class{constructor(e){this.objects=null,this.useGroups=!0,this.applyWorldTransforms=!0,this.generateMissingAttributes=!0,this.overwriteIndex=!0,this.attributes=[`position`,`normal`,`color`,`tangent`,`uv`,`uv2`],this._intermediateGeometry=new Map,this._geometryMergeSets=new WeakMap,this._mergeOrder=[],this._dummyMesh=null,this.setObjects(e||[])}_getDummyMesh(){if(!this._dummyMesh){let e=new Ve,t=new me;t.setAttribute(`position`,new xe(new Float32Array(9),3)),this._dummyMesh=new v(t,e)}return this._dummyMesh}_getMeshes(){let e=[];return Si(this.objects,t=>{e.push(t)}),e.sort((e,t)=>e.uuid>t.uuid?1:e.uuid<t.uuid?-1:0),e.length===0&&e.push(this._getDummyMesh()),e}_updateIntermediateGeometries(){let{_intermediateGeometry:e}=this,t=this._getMeshes(),n=new Set(e.keys()),r={attributes:this.attributes,applyWorldTransforms:this.applyWorldTransforms};for(let i=0,a=t.length;i<a;i++){let a=t[i],o=a.uuid;n.delete(o);let s=e.get(o);(!s||!s.isCompatible(a,this.attributes))&&(s&&s.dispose(),s=new xi,e.set(o,s)),s.updateFrom(a,r)&&this.generateMissingAttributes&&ti(s,this.attributes)}n.forEach(t=>{e.delete(t)})}setObjects(e){this.objects=Array.isArray(e)?[...e]:[e]}generate(e=new me){let{useGroups:t,overwriteIndex:n,_intermediateGeometry:r,_geometryMergeSets:i}=this,a=this._getMeshes(),o=[],s=[],c=i.get(e)||[];this._updateIntermediateGeometries();let l=!1;a.length!==c.length&&(l=!0);for(let e=0,t=a.length;e<t;e++){let t=a[e],n=r.get(t.uuid);s.push(n);let i=c[e];!i||i.uuid!==n.uuid?(o.push(!1),l=!0):i.version===n.version?o.push(!0):o.push(!1)}wi(s,e,{useGroups:t,forceUpdate:l,skipAssigningAttributes:o,overwriteIndex:n}),l&&e.dispose(),i.set(e,s.map(e=>({version:e.version,uuid:e.uuid})));let u=0;return l?u=2:o.includes(!1)&&(u=1),{changeType:u,materials:Ci(a),geometry:e}}};function Ei(e){let t=new Set;for(let n=0,r=e.length;n<r;n++){let r=e[n];for(let e in r){let n=r[e];n&&n.isTexture&&t.add(n)}}return Array.from(t)}function Di(e){let t=[],n=new Set;for(let r=0,i=e.length;r<i;r++)e[r].traverse(e=>{e.visible&&(e.isRectAreaLight||e.isSpotLight||e.isPointLight||e.isDirectionalLight)&&(t.push(e),e.iesMap&&n.add(e.iesMap))});return{lights:t,iesTextures:Array.from(n).sort((e,t)=>e.uuid<t.uuid?1:e.uuid>t.uuid?-1:0)}}var Oi=class{get initialized(){return!!this.bvh}constructor(e){this.bvhOptions={},this.attributes=[`position`,`normal`,`tangent`,`color`,`uv`,`uv2`],this.generateBVH=!0,this.bvh=null,this.geometry=new me,this.staticGeometryGenerator=new Ti(e),this._bvhWorker=null,this._pendingGenerate=null,this._buildAsync=!1,this._materialUuids=null}setObjects(e){this.staticGeometryGenerator.setObjects(e)}setBVHWorker(e){this._bvhWorker=e}async generateAsync(e=null){if(!this._bvhWorker)throw Error(`PathTracingSceneGenerator: "setBVHWorker" must be called before "generateAsync" can be called.`);if(this.bvh instanceof Promise)return this._pendingGenerate||=new Promise(async()=>(await this.bvh,this._pendingGenerate=null,this.generateAsync(e))),this._pendingGenerate;{this._buildAsync=!0;let t=this.generate(e);return this._buildAsync=!1,t.bvh=this.bvh=await t.bvh,t}}generate(e=null){let{staticGeometryGenerator:t,geometry:n,attributes:r}=this,i=t.objects;t.attributes=r,i.forEach(e=>{e.traverse(e=>{e.isSkinnedMesh&&e.skeleton&&e.skeleton.update()})});let a=t.generate(n),o=a.materials,s=a.changeType!==0||this._materialUuids===null||this._materialUuids.length!==length;if(!s){for(let e=0,t=o.length;e<t;e++)if(o[e].uuid!==this._materialUuids[e]){s=!0;break}}let c=Ei(o),{lights:l,iesTextures:u}=Di(i);if(s&&(ei(n,o,o),this._materialUuids=o.map(e=>e.uuid)),this.generateBVH){if(this.bvh instanceof Promise)throw Error(`PathTracingSceneGenerator: BVH is already building asynchronously.`);if(a.changeType===2){let t={strategy:2,maxLeafTris:1,indirect:!0,onProgress:e,...this.bvhOptions};this.bvh=this._buildAsync?this._bvhWorker.generate(n,t):new Nr(n,t)}else a.changeType===1&&this.bvh.refit()}return{bvhChanged:a.changeType!==0,bvh:this.bvh,needsMaterialIndexUpdate:s,lights:l,iesTextures:u,geometry:n,materials:o,textures:c,objects:i}}},ki=class extends Fe{set needsUpdate(e){super.needsUpdate=!0,this.dispatchEvent({type:`recompilation`})}constructor(e){super(e);for(let e in this.uniforms)Object.defineProperty(this,e,{get(){return this.uniforms[e].value},set(t){this.uniforms[e].value=t}})}setDefine(e,t=void 0){if(t==null){if(e in this.defines)return delete this.defines[e],this.needsUpdate=!0,!0}else if(this.defines[e]!==t)return this.defines[e]=t,this.needsUpdate=!0,!0;return!1}},Ai=class extends ki{constructor(e){super({blending:0,uniforms:{target1:{value:null},target2:{value:null},opacity:{value:1}},vertexShader:`
297
+ `;function ti(e,t,n=0){if(e.isInterleavedBufferAttribute){let r=e.itemSize;for(let i=0,a=e.count;i<a;i++){let a=i+n;t.setX(a,e.getX(i)),r>=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 ni(e,t=null){let n=e.array.constructor,r=e.normalized,i=e.itemSize,a=t===null?e.count:t;return new C(new n(i*a),i,r)}function ri(e,t){if(!e&&!t)return!0;if(!!e!=!!t)return!1;let n=e.count===t.count,r=e.normalized===t.normalized,i=e.array.constructor===t.array.constructor,a=e.itemSize===t.itemSize;return!(!n||!r||!i||!a)}function ii(e){let t=e[0].index!==null,n=new Set(Object.keys(e[0].attributes));if(!e[0].getAttribute(`position`))throw Error(`StaticGeometryGenerator: position attribute is required.`);for(let r=0;r<e.length;++r){let i=e[r],a=0;if(t!==(i.index!==null))throw Error(`StaticGeometryGenerator: All geometries must have compatible attributes; make sure index attribute exists among all geometries, or in none of them.`);for(let e in i.attributes){if(!n.has(e))throw Error(`StaticGeometryGenerator: All geometries must have compatible attributes; make sure "`+e+`" attribute exists among all geometries, or in none of them.`);a++}if(a!==n.size)throw Error(`StaticGeometryGenerator: All geometries must have the same number of attributes.`)}}function ai(e){let t=0;for(let n=0,r=e.length;n<r;n++)t+=e[n].getIndex().count;return t}function oi(e){let t=0;for(let n=0,r=e.length;n<r;n++)t+=e[n].getAttribute(`position`).count;return t}function si(e,t,n){e.index&&e.index.count!==t&&e.setIndex(null);let r=e.attributes;for(let t in r)r[t].count!==n&&e.deleteAttribute(t)}function ci(e,t={},n=new o){let{useGroups:r=!1,forceUpdate:i=!1,skipAssigningAttributes:a=[],overwriteIndex:s=!0}=t;ii(e);let c=e[0].index!==null,l=c?ai(e):-1,u=oi(e);if(si(n,l,u),r){let t=0;for(let r=0,i=e.length;r<i;r++){let i=e[r],a;a=c?i.getIndex().count:i.getAttribute(`position`).count,n.addGroup(t,a,r),t+=a}}if(c){let t=!1;if(n.index||(n.setIndex(new C(new Uint32Array(l),1,!1)),t=!0),t||s){let r=0,o=0,s=n.getIndex();for(let n=0,c=e.length;n<c;n++){let c=e[n],l=c.getIndex();if(i||t||!a[n])for(let e=0;e<l.count;++e)s.setX(r+e,l.getX(e)+o);r+=l.count,o+=c.getAttribute(`position`).count}}}let d=Object.keys(e[0].attributes);for(let t=0,r=d.length;t<r;t++){let r=!1,o=d[t];if(!n.getAttribute(o)){let t=e[0].getAttribute(o);n.setAttribute(o,ni(t,u)),r=!0}let s=0,c=n.getAttribute(o);for(let t=0,n=e.length;t<n;t++){let n=e[t],l=!i&&!r&&a[t],u=n.getAttribute(o);if(!l){if(o===`color`&&c.itemSize!==u.itemSize)for(let e=s,t=u.count;e<t;e++)u.setXYZW(e,c.getX(e),c.getY(e),c.getZ(e),1);else ti(u,c,s)}s+=u.count}}}function li(e,t,n){let r=e.index,i=e.attributes.position.count,a=r?r.count:i,o=e.groups;o.length===0&&(o=[{count:a,start:0,materialIndex:0}]);let s=e.getAttribute(`materialIndex`);if(!s||s.count!==i){let t;t=n.length<=255?new Uint8Array(i):new Uint16Array(i),s=new C(t,1,!1),e.deleteAttribute(`materialIndex`),e.setAttribute(`materialIndex`,s)}let c=s.array;for(let e=0;e<o.length;e++){let i=o[e],s=i.start,l=i.count,u=Math.min(l,a-s),d=Array.isArray(t)?t[i.materialIndex]:t,f=n.indexOf(d);for(let e=0;e<u;e++){let t=s+e;r&&(t=r.getX(t)),c[t]=f}}}function ui(e,t){if(!e.index){let t=e.attributes.position.count,n=Array(t);for(let e=0;e<t;e++)n[e]=e;e.setIndex(n)}if(!e.attributes.normal&&t&&t.includes(`normal`)&&e.computeVertexNormals(),!e.attributes.uv&&t&&t.includes(`uv`)){let t=e.attributes.position.count;e.setAttribute(`uv`,new C(new Float32Array(t*2),2,!1))}if(!e.attributes.uv2&&t&&t.includes(`uv2`)){let t=e.attributes.position.count;e.setAttribute(`uv2`,new C(new Float32Array(t*2),2,!1))}if(!e.attributes.tangent&&t&&t.includes(`tangent`)){if(e.attributes.uv&&e.attributes.normal)e.computeTangents();else{let t=e.attributes.position.count;e.setAttribute(`tangent`,new C(new Float32Array(t*4),4,!1))}}if(!e.attributes.color&&t&&t.includes(`color`)){let t=e.attributes.position.count,n=new Float32Array(t*4);n.fill(1),e.setAttribute(`color`,new C(n,4))}}function di(e){let t=0;if(e.byteLength!==0){let n=new Uint8Array(e);for(let r=0;r<e.byteLength;r++){let e=n[r];t=(t<<5)-t+e,t|=0}}return t}function fi(e){let t=e.uuid,n=Object.values(e.attributes);e.index&&(n.push(e.index),t+=`index|${e.index.version}`);let r=Object.keys(n).sort();for(let e of r){let r=n[e];t+=`${e}_${r.version}|`}return t}function pi(e){let t=e.skeleton;return t?(t.boneTexture||t.computeBoneTexture(),`${di(t.boneTexture.image.data.buffer)}_${t.boneTexture.uuid}`):null}var mi=class{constructor(e=null){this.matrixWorld=new O,this.geometryHash=null,this.skeletonHash=null,this.primitiveCount=-1,e!==null&&this.updateFrom(e)}updateFrom(e){let t=e.geometry,n=(t.index?t.index.count:t.attributes.position.count)/3;this.matrixWorld.copy(e.matrixWorld),this.geometryHash=fi(t),this.primitiveCount=n,this.skeletonHash=pi(e)}didChange(e){let t=e.geometry,n=(t.index?t.index.count:t.attributes.position.count)/3;return!(this.matrixWorld.equals(e.matrixWorld)&&this.geometryHash===fi(t)&&this.skeletonHash===pi(e)&&this.primitiveCount===n)}},hi=new k,gi=new k,_i=new k,vi=new S,yi=new k,bi=new k,xi=new S,Si=new S,Ci=new O,wi=new O;function Ti(e,t,n){let r=e.skeleton,i=e.geometry,a=r.bones,o=r.boneInverses;xi.fromBufferAttribute(i.attributes.skinIndex,t),Si.fromBufferAttribute(i.attributes.skinWeight,t),Ci.elements.fill(0);for(let e=0;e<4;e++){let t=Si.getComponent(e);if(t!==0){let n=xi.getComponent(e);wi.multiplyMatrices(a[n].matrixWorld,o[n]),Di(Ci,wi,t)}}return Ci.multiply(e.bindMatrix).premultiply(e.bindMatrixInverse),n.transformDirection(Ci),n}function Ei(e,t,n,r,i){yi.set(0,0,0);for(let a=0,o=e.length;a<o;a++){let o=t[a],s=e[a];o!==0&&(bi.fromBufferAttribute(s,r),n?yi.addScaledVector(bi,o):yi.addScaledVector(bi.sub(i),o))}i.add(yi)}function Di(e,t,n){let r=e.elements,i=t.elements;for(let e=0,t=i.length;e<t;e++)r[e]+=i[e]*n}function Oi(e){let{index:t,attributes:n}=e;if(t)for(let e=0,n=t.count;e<n;e+=3){let n=t.getX(e),r=t.getX(e+2);t.setX(e,r),t.setX(e+2,n)}else for(let e in n){let t=n[e],r=t.itemSize;for(let e=0,n=t.count;e<n;e+=3)for(let n=0;n<r;n++){let r=t.getComponent(e,n),i=t.getComponent(e+2,n);t.setComponent(e,n,i),t.setComponent(e+2,n,r)}}return e}function ki(e,t={},n=new o){t={applyWorldTransforms:!0,attributes:[],...t};let r=e.geometry,i=t.applyWorldTransforms,a=t.attributes.includes(`normal`),s=t.attributes.includes(`tangent`),c=r.attributes,l=n.attributes;for(let e in n.attributes)(!t.attributes.includes(e)||!(e in r.attributes))&&n.deleteAttribute(e);!n.index&&r.index&&(n.index=r.index.clone()),l.position||n.setAttribute(`position`,ni(c.position)),a&&!l.normal&&c.normal&&n.setAttribute(`normal`,ni(c.normal)),s&&!l.tangent&&c.tangent&&n.setAttribute(`tangent`,ni(c.tangent)),ri(r.index,n.index),ri(c.position,l.position),a&&ri(c.normal,l.normal),s&&ri(c.tangent,l.tangent);let u=c.position,d=a?c.normal:null,f=s?c.tangent:null,m=r.morphAttributes.position,h=r.morphAttributes.normal,g=r.morphAttributes.tangent,_=r.morphTargetsRelative,v=e.morphTargetInfluences,y=new p;y.getNormalMatrix(e.matrixWorld),r.index&&n.index.array.set(r.index.array);for(let t=0,n=c.position.count;t<n;t++)hi.fromBufferAttribute(u,t),d&&gi.fromBufferAttribute(d,t),f&&(vi.fromBufferAttribute(f,t),_i.fromBufferAttribute(f,t)),v&&(m&&Ei(m,v,_,t,hi),h&&Ei(h,v,_,t,gi),g&&Ei(g,v,_,t,_i)),e.isSkinnedMesh&&(e.applyBoneTransform(t,hi),d&&Ti(e,t,gi),f&&Ti(e,t,_i)),i&&hi.applyMatrix4(e.matrixWorld),l.position.setXYZ(t,hi.x,hi.y,hi.z),d&&(i&&gi.applyNormalMatrix(y),l.normal.setXYZ(t,gi.x,gi.y,gi.z)),f&&(i&&_i.transformDirection(e.matrixWorld),l.tangent.setXYZW(t,_i.x,_i.y,_i.z,vi.w));for(let e in t.attributes){let r=t.attributes[e];r!==`position`&&r!==`tangent`&&r!==`normal`&&r in c&&(l[r]||n.setAttribute(r,ni(c[r])),ri(c[r],l[r]),ti(c[r],l[r]))}return e.matrixWorld.determinant()<0&&Oi(n),n}var Ai=class extends o{constructor(){super(),this.version=0,this.hash=null,this._diff=new mi}isCompatible(e,t){let n=e.geometry;for(let e=0;e<t.length;e++){let r=t[e],i=n.attributes[r],a=this.attributes[r];if(i&&!ri(i,a))return!1}return!0}updateFrom(e,t){let n=this._diff;return n.didChange(e)?(ki(e,t,this),n.updateFrom(e),this.version++,this.hash=`${this.uuid}_${this.version}`,!0):!1}};function ji(e,t){for(let n=0,r=e.length;n<r;n++)e[n].traverseVisible(e=>{e.isMesh&&t(e)})}function Mi(e){let t=[];for(let n=0,r=e.length;n<r;n++){let r=e[n];Array.isArray(r.material)?t.push(...r.material):t.push(r.material)}return t}function Ni(e,t,n){if(e.length===0){t.setIndex(null);let e=t.attributes;for(let n in e)t.deleteAttribute(n);for(let e in n.attributes)t.setAttribute(n.attributes[e],new C(new Float32Array,4,!1))}else ci(e,n,t);for(let e in t.attributes)t.attributes[e].needsUpdate=!0}var Pi=class{constructor(e){this.objects=null,this.useGroups=!0,this.applyWorldTransforms=!0,this.generateMissingAttributes=!0,this.overwriteIndex=!0,this.attributes=[`position`,`normal`,`color`,`tangent`,`uv`,`uv2`],this._intermediateGeometry=new Map,this._geometryMergeSets=new WeakMap,this._mergeOrder=[],this._dummyMesh=null,this.setObjects(e||[])}_getDummyMesh(){if(!this._dummyMesh){let e=new ie,t=new o;t.setAttribute(`position`,new C(new Float32Array(9),3)),this._dummyMesh=new f(t,e)}return this._dummyMesh}_getMeshes(){let e=[];return ji(this.objects,t=>{e.push(t)}),e.sort((e,t)=>e.uuid>t.uuid?1:e.uuid<t.uuid?-1:0),e.length===0&&e.push(this._getDummyMesh()),e}_updateIntermediateGeometries(){let{_intermediateGeometry:e}=this,t=this._getMeshes(),n=new Set(e.keys()),r={attributes:this.attributes,applyWorldTransforms:this.applyWorldTransforms};for(let i=0,a=t.length;i<a;i++){let a=t[i],o=a.uuid;n.delete(o);let s=e.get(o);(!s||!s.isCompatible(a,this.attributes))&&(s&&s.dispose(),s=new Ai,e.set(o,s)),s.updateFrom(a,r)&&this.generateMissingAttributes&&ui(s,this.attributes)}n.forEach(t=>{e.delete(t)})}setObjects(e){this.objects=Array.isArray(e)?[...e]:[e]}generate(e=new o){let{useGroups:t,overwriteIndex:n,_intermediateGeometry:r,_geometryMergeSets:i}=this,a=this._getMeshes(),s=[],c=[],l=i.get(e)||[];this._updateIntermediateGeometries();let u=!1;a.length!==l.length&&(u=!0);for(let e=0,t=a.length;e<t;e++){let t=a[e],n=r.get(t.uuid);c.push(n);let i=l[e];!i||i.uuid!==n.uuid?(s.push(!1),u=!0):i.version===n.version?s.push(!0):s.push(!1)}Ni(c,e,{useGroups:t,forceUpdate:u,skipAssigningAttributes:s,overwriteIndex:n}),u&&e.dispose(),i.set(e,c.map(e=>({version:e.version,uuid:e.uuid})));let d=0;return u?d=2:s.includes(!1)&&(d=1),{changeType:d,materials:Mi(a),geometry:e}}};function Fi(e){let t=new Set;for(let n=0,r=e.length;n<r;n++){let r=e[n];for(let e in r){let n=r[e];n&&n.isTexture&&t.add(n)}}return Array.from(t)}function Ii(e){let t=[],n=new Set;for(let r=0,i=e.length;r<i;r++)e[r].traverse(e=>{e.visible&&(e.isRectAreaLight||e.isSpotLight||e.isPointLight||e.isDirectionalLight)&&(t.push(e),e.iesMap&&n.add(e.iesMap))});return{lights:t,iesTextures:Array.from(n).sort((e,t)=>e.uuid<t.uuid?1:e.uuid>t.uuid?-1:0)}}var Li=class{get initialized(){return!!this.bvh}constructor(e){this.bvhOptions={},this.attributes=[`position`,`normal`,`tangent`,`color`,`uv`,`uv2`],this.generateBVH=!0,this.bvh=null,this.geometry=new o,this.staticGeometryGenerator=new Pi(e),this._bvhWorker=null,this._pendingGenerate=null,this._buildAsync=!1,this._materialUuids=null}setObjects(e){this.staticGeometryGenerator.setObjects(e)}setBVHWorker(e){this._bvhWorker=e}async generateAsync(e=null){if(!this._bvhWorker)throw Error(`PathTracingSceneGenerator: "setBVHWorker" must be called before "generateAsync" can be called.`);if(this.bvh instanceof Promise)return this._pendingGenerate||=new Promise(async()=>(await this.bvh,this._pendingGenerate=null,this.generateAsync(e))),this._pendingGenerate;{this._buildAsync=!0;let t=this.generate(e);return this._buildAsync=!1,t.bvh=this.bvh=await t.bvh,t}}generate(e=null){let{staticGeometryGenerator:t,geometry:n,attributes:r}=this,i=t.objects;t.attributes=r,i.forEach(e=>{e.traverse(e=>{e.isSkinnedMesh&&e.skeleton&&e.skeleton.update()})});let a=t.generate(n),o=a.materials,s=a.changeType!==0||this._materialUuids===null||this._materialUuids.length!==length;if(!s){for(let e=0,t=o.length;e<t;e++)if(o[e].uuid!==this._materialUuids[e]){s=!0;break}}let c=Fi(o),{lights:l,iesTextures:u}=Ii(i);if(s&&(li(n,o,o),this._materialUuids=o.map(e=>e.uuid)),this.generateBVH){if(this.bvh instanceof Promise)throw Error(`PathTracingSceneGenerator: BVH is already building asynchronously.`);if(a.changeType===2){let t={strategy:2,maxLeafTris:1,indirect:!0,onProgress:e,...this.bvhOptions};this.bvh=this._buildAsync?this._bvhWorker.generate(n,t):new Hr(n,t)}else a.changeType===1&&this.bvh.refit()}return{bvhChanged:a.changeType!==0,bvh:this.bvh,needsMaterialIndexUpdate:s,lights:l,iesTextures:u,geometry:n,materials:o,textures:c,objects:i}}},Ri=class extends pe{set needsUpdate(e){super.needsUpdate=!0,this.dispatchEvent({type:`recompilation`})}constructor(e){super(e);for(let e in this.uniforms)Object.defineProperty(this,e,{get(){return this.uniforms[e].value},set(t){this.uniforms[e].value=t}})}setDefine(e,t=void 0){if(t==null){if(e in this.defines)return delete this.defines[e],this.needsUpdate=!0,!0}else if(this.defines[e]!==t)return this.defines[e]=t,this.needsUpdate=!0,!0;return!1}},zi=class extends Ri{constructor(e){super({blending:0,uniforms:{target1:{value:null},target2:{value:null},opacity:{value:1}},vertexShader:`
298
298
 
299
299
  varying vec2 vUv;
300
300
 
@@ -331,7 +331,7 @@ struct BVH {
331
331
 
332
332
  }
333
333
 
334
- }`}),this.setValues(e)}};function ji(e=1){let t=`uint`;return e>1&&(t=`uvec`+e),`
334
+ }`}),this.setValues(e)}};function Bi(e=1){let t=`uint`;return e>1&&(t=`uvec`+e),`
335
335
  ${t} sobolReverseBits( ${t} x ) {
336
336
 
337
337
  x = ( ( ( x & 0xaaaaaaaau ) >> 1 ) | ( ( x & 0x55555555u ) << 1 ) );
@@ -366,7 +366,7 @@ struct BVH {
366
366
  return x;
367
367
 
368
368
  }
369
- `}function Mi(e=1){let t=`uint`,n=`float`,r=``,i=`.r`,a=`1u`;return e>1&&(t=`uvec`+e,n=`vec`+e,r=e+``,e===2?(i=`.rg`,a=`uvec2( 1u, 2u )`):e===3?(i=`.rgb`,a=`uvec3( 1u, 2u, 3u )`):(i=``,a=`uvec4( 1u, 2u, 3u, 4u )`)),`
369
+ `}function Vi(e=1){let t=`uint`,n=`float`,r=``,i=`.r`,a=`1u`;return e>1&&(t=`uvec`+e,n=`vec`+e,r=e+``,e===2?(i=`.rg`,a=`uvec2( 1u, 2u )`):e===3?(i=`.rgb`,a=`uvec3( 1u, 2u, 3u )`):(i=``,a=`uvec4( 1u, 2u, 3u, 4u )`)),`
370
370
 
371
371
  ${n} sobol${r}( int effect ) {
372
372
 
@@ -384,16 +384,16 @@ struct BVH {
384
384
  return SOBOL_FACTOR * ${n}( result >> 8 );
385
385
 
386
386
  }
387
- `}var Ni=`
387
+ `}var Hi=`
388
388
 
389
389
  // Utils
390
390
  const float SOBOL_FACTOR = 1.0 / 16777216.0;
391
391
  const uint SOBOL_MAX_POINTS = 256u * 256u;
392
392
 
393
- ${ji(1)}
394
- ${ji(2)}
395
- ${ji(3)}
396
- ${ji(4)}
393
+ ${Bi(1)}
394
+ ${Bi(2)}
395
+ ${Bi(3)}
396
+ ${Bi(4)}
397
397
 
398
398
  uint sobolHash( uint x ) {
399
399
 
@@ -407,7 +407,7 @@ struct BVH {
407
407
 
408
408
  }
409
409
 
410
- `,Pi=`
410
+ `,Ui=`
411
411
 
412
412
  const uint SOBOL_DIRECTIONS_1[ 32 ] = uint[ 32 ](
413
413
  0x80000000u, 0xc0000000u, 0xa0000000u, 0xf0000000u,
@@ -485,7 +485,7 @@ struct BVH {
485
485
 
486
486
  }
487
487
 
488
- `,Fi=`
488
+ `,Wi=`
489
489
 
490
490
  // Seeds
491
491
  uniform sampler2D sobolTexture;
@@ -523,12 +523,12 @@ struct BVH {
523
523
 
524
524
  }
525
525
 
526
- ${Mi(1)}
527
- ${Mi(2)}
528
- ${Mi(3)}
529
- ${Mi(4)}
526
+ ${Vi(1)}
527
+ ${Vi(2)}
528
+ ${Vi(3)}
529
+ ${Vi(4)}
530
530
 
531
- `,Ii=class extends ki{constructor(){super({blending:0,uniforms:{resolution:{value:new T}},vertexShader:`
531
+ `,Gi=class extends Ri{constructor(){super({blending:0,uniforms:{resolution:{value:new E}},vertexShader:`
532
532
 
533
533
  varying vec2 vUv;
534
534
  void main() {
@@ -539,8 +539,8 @@ struct BVH {
539
539
  }
540
540
  `,fragmentShader:`
541
541
 
542
- ${Ni}
543
- ${Pi}
542
+ ${Hi}
543
+ ${Ui}
544
544
 
545
545
  varying vec2 vUv;
546
546
  uniform vec2 resolution;
@@ -550,7 +550,7 @@ struct BVH {
550
550
  gl_FragColor = generateSobolPoint( index );
551
551
 
552
552
  }
553
- `})}},Li=class{generate(e,t=256){let n=new Oe(t,t,{type:b,format:d,minFilter:S,magFilter:S,generateMipmaps:!1}),r=e.getRenderTarget();e.setRenderTarget(n);let i=new oe(new Ii);return i.material.resolution.set(t,t),i.render(e),e.setRenderTarget(r),i.dispose(),n}},Ri=class extends c{set bokehSize(e){this.fStop=this.getFocalLength()/e}get bokehSize(){return this.getFocalLength()/this.fStop}constructor(...e){super(...e),this.fStop=1.4,this.apertureBlades=0,this.apertureRotation=0,this.focusDistance=25,this.anamorphicRatio=1}copy(e,t){return super.copy(e,t),this.fStop=e.fStop,this.apertureBlades=e.apertureBlades,this.apertureRotation=e.apertureRotation,this.focusDistance=e.focusDistance,this.anamorphicRatio=e.anamorphicRatio,this}},zi=class{constructor(){this.bokehSize=0,this.apertureBlades=0,this.apertureRotation=0,this.focusDistance=10,this.anamorphicRatio=1}updateFrom(e){e instanceof Ri?(this.bokehSize=e.bokehSize,this.apertureBlades=e.apertureBlades,this.apertureRotation=e.apertureRotation,this.focusDistance=e.focusDistance,this.anamorphicRatio=e.anamorphicRatio):(this.bokehSize=0,this.apertureRotation=0,this.apertureBlades=0,this.focusDistance=10,this.anamorphicRatio=1)}};function Bi(e){let t=new Uint16Array(e.length);for(let n=0,i=e.length;n<i;++n)t[n]=r.toHalfFloat(e[n]);return t}function Vi(e,t,n=0,r=e.length){let i=n,a=n+r-1;for(;i<a;){let n=i+a>>1;e[n]<t?i=n+1:a=n}return i-n}function Hi(e,t,n){return .2126*e+.7152*t+.0722*n}function Ui(e,t=ye){let n=e.clone();n.source=new je({...n.image});let{width:i,height:a,data:o}=n.image,s=o;if(n.type!==t){s=t===1016?new Uint16Array(o.length):new Float32Array(o.length);let e;e=o instanceof Int8Array||o instanceof Int16Array||o instanceof Int32Array?2**(8*o.BYTES_PER_ELEMENT-1)-1:2**(8*o.BYTES_PER_ELEMENT)-1;for(let i=0,a=o.length;i<a;i++){let a=o[i];n.type===1016&&(a=r.fromHalfFloat(o[i])),n.type!==1015&&n.type!==1016&&(a/=e),t===1016&&(s[i]=r.toHalfFloat(a))}n.image.data=s,n.type=t}if(n.flipY){let e=s;s=s.slice();for(let t=0;t<a;t++)for(let n=0;n<i;n++){let r=a-t-1,o=4*(t*i+n),c=4*(r*i+n);s[c+0]=e[o+0],s[c+1]=e[o+1],s[c+2]=e[o+2],s[c+3]=e[o+3]}n.flipY=!1,n.image.data=s}return n}var Wi=class{constructor(){let t=new ze(Bi(new Float32Array([0,0,0,0])),1,1);t.type=ye,t.format=d,t.minFilter=m,t.magFilter=m,t.wrapS=e,t.wrapT=e,t.generateMipmaps=!1,t.needsUpdate=!0;let n=new ze(Bi(new Float32Array([0,1])),1,2);n.type=ye,n.format=ne,n.minFilter=m,n.magFilter=m,n.generateMipmaps=!1,n.needsUpdate=!0;let r=new ze(Bi(new Float32Array([0,0,1,1])),2,2);r.type=ye,r.format=ne,r.minFilter=m,r.magFilter=m,r.generateMipmaps=!1,r.needsUpdate=!0,this.map=t,this.marginalWeights=n,this.conditionalWeights=r,this.totalSum=0}dispose(){this.marginalWeights.dispose(),this.conditionalWeights.dispose(),this.map.dispose()}updateFrom(t){let n=Ui(t);n.wrapS=e,n.wrapT=w;let{width:i,height:a,data:o}=n.image,s=new Float32Array(i*a),c=new Float32Array(i*a),l=new Float32Array(a),u=new Float32Array(a),d=0,f=0;for(let e=0;e<a;e++){let t=0;for(let n=0;n<i;n++){let a=e*i+n,l=Hi(r.fromHalfFloat(o[4*a+0]),r.fromHalfFloat(o[4*a+1]),r.fromHalfFloat(o[4*a+2]));t+=l,d+=l,s[a]=l,c[a]=t}if(t!==0)for(let n=e*i,r=e*i+i;n<r;n++)s[n]/=t,c[n]/=t;f+=t,l[e]=t,u[e]=f}if(f!==0)for(let e=0,t=l.length;e<t;e++)l[e]/=f,u[e]/=f;let p=new Uint16Array(a),m=new Uint16Array(i*a);for(let e=0;e<a;e++){let t=Vi(u,(e+1)/a);p[e]=r.toHalfFloat((t+.5)/a)}for(let e=0;e<a;e++)for(let t=0;t<i;t++){let n=e*i+t,a=Vi(c,(t+1)/i,e*i,i);m[n]=r.toHalfFloat((a+.5)/i)}this.dispose();let{marginalWeights:h,conditionalWeights:g}=this;h.image={width:a,height:1,data:p},h.needsUpdate=!0,g.image={width:i,height:a,data:m},g.needsUpdate=!0,this.totalSum=d,this.map=n}},Gi=6,Ki=0,qi=1,Ji=2,Yi=3,Xi=4,Zi=new E,L=new E,Qi=new f,$i=new s,ea=new E,ta=new E,na=new E(0,1,0),ra=class{constructor(){let e=new ze(new Float32Array(4),1,1);e.format=d,e.type=b,e.wrapS=w,e.wrapT=w,e.generateMipmaps=!1,e.minFilter=S,e.magFilter=S,this.tex=e,this.count=0}updateFrom(e,t=[]){let n=this.tex,r=Math.max(e.length*Gi,1),i=Math.ceil(Math.sqrt(r));n.image.width!==i&&(n.dispose(),n.image.data=new Float32Array(i*i*4),n.image.width=i,n.image.height=i);let a=n.image.data;for(let n=0,r=e.length;n<r;n++){let r=e[n],i=n*Gi*4,o=0;for(let e=0;e<24;e++)a[i+e]=0;r.getWorldPosition(L),a[i+o++]=L.x,a[i+o++]=L.y,a[i+o++]=L.z;let s=Ki;if(r.isRectAreaLight&&r.isCircular?s=qi:r.isSpotLight?s=Ji:r.isDirectionalLight?s=Yi:r.isPointLight&&(s=Xi),a[i+o++]=s,a[i+o++]=r.color.r,a[i+o++]=r.color.g,a[i+o++]=r.color.b,a[i+o++]=r.intensity,r.getWorldQuaternion($i),r.isRectAreaLight)Zi.set(r.width,0,0).applyQuaternion($i),a[i+o++]=Zi.x,a[i+o++]=Zi.y,a[i+o++]=Zi.z,o++,L.set(0,r.height,0).applyQuaternion($i),a[i+o++]=L.x,a[i+o++]=L.y,a[i+o++]=L.z,a[i+o++]=Zi.cross(L).length()*(r.isCircular?Math.PI/4:1);else if(r.isSpotLight){let e=r.radius||0;ea.setFromMatrixPosition(r.matrixWorld),ta.setFromMatrixPosition(r.target.matrixWorld),Qi.lookAt(ea,ta,na),$i.setFromRotationMatrix(Qi),Zi.set(1,0,0).applyQuaternion($i),a[i+o++]=Zi.x,a[i+o++]=Zi.y,a[i+o++]=Zi.z,o++,L.set(0,1,0).applyQuaternion($i),a[i+o++]=L.x,a[i+o++]=L.y,a[i+o++]=L.z,a[i+o++]=Math.PI*e*e,a[i+o++]=e,a[i+o++]=r.decay,a[i+o++]=r.distance,a[i+o++]=Math.cos(r.angle),a[i+o++]=Math.cos(r.angle*(1-r.penumbra)),a[i+o++]=r.iesMap?t.indexOf(r.iesMap):-1}else if(r.isPointLight){let e=Zi.setFromMatrixPosition(r.matrixWorld);a[i+o++]=e.x,a[i+o++]=e.y,a[i+o++]=e.z,o++,o+=4,o+=1,a[i+o++]=r.decay,a[i+o++]=r.distance}else if(r.isDirectionalLight){let e=Zi.setFromMatrixPosition(r.matrixWorld),t=L.setFromMatrixPosition(r.target.matrixWorld);ta.subVectors(e,t).normalize(),a[i+o++]=ta.x,a[i+o++]=ta.y,a[i+o++]=ta.z}}this.count=e.length;let o=ni(a.buffer);return this.hash!==o&&(this.hash=o,n.needsUpdate=!0,!0)}};function ia(e,t,n,r,i){if(t>r)throw Error();let a=e.length/t,o=e.constructor.BYTES_PER_ELEMENT*8,s=1;switch(e.constructor){case Uint8Array:case Uint16Array:case Uint32Array:s=2**o-1;break;case Int8Array:case Int16Array:case Int32Array:s=2**(o-1)-1}for(let o=0;o<a;o++){let a=4*o,c=t*o;for(let o=0;o<r;o++)n[i+a+o]=t>=o+1?e[c+o]/s:0}}var aa=class extends ae{constructor(){super(),this._textures=[],this.type=b,this.format=d,this.internalFormat=`RGBA32F`}updateAttribute(e,t){let n=this._textures[e];n.updateFrom(t);let r=n.image,i=this.image;if(r.width!==i.width||r.height!==i.height)throw Error(`FloatAttributeTextureArray: Attribute must be the same dimensions when updating single layer.`);let{width:a,height:o,data:s}=i,c=a*o*4*e,l=t.itemSize;l===3&&(l=4),ia(n.image.data,l,s,4,c),this.dispose(),this.needsUpdate=!0}setAttributes(e){let t=e[0].count,n=e.length;for(let r=0,i=n;r<i;r++)if(e[r].count!==t)throw Error(`FloatAttributeTextureArray: All attributes must have the same item count.`);let r=this._textures;for(;r.length<n;){let e=new zr;r.push(e)}for(;r.length>n;)r.pop();for(let t=0,i=n;t<i;t++)r[t].updateFrom(e[t]);let i=r[0].image,a=this.image;(i.width!==a.width||i.height!==a.height||i.depth!==n)&&(a.width=i.width,a.height=i.height,a.depth=n,a.data=new Float32Array(a.width*a.height*a.depth*4));let{data:o,width:s,height:c}=a;for(let t=0,i=n;t<i;t++){let n=r[t],i=s*c*4*t,a=e[t].itemSize;a===3&&(a=4),ia(n.image.data,a,o,4,i)}this.dispose(),this.needsUpdate=!0}},oa=class extends aa{updateNormalAttribute(e){this.updateAttribute(0,e)}updateTangentAttribute(e){this.updateAttribute(1,e)}updateUvAttribute(e){this.updateAttribute(2,e)}updateColorAttribute(e){this.updateAttribute(3,e)}updateFrom(e,t,n,r){this.setAttributes([e,t,n,r])}};function sa(e,t){return e.uuid<t.uuid?1:e.uuid>t.uuid?-1:0}function ca(e){return`${e.source.uuid}:${e.colorSpace}`}function la(e){let t=new Set,n=[];for(let r=0,i=e.length;r<i;r++){let i=e[r],a=ca(i);t.has(a)||(t.add(a),n.push(i))}return n}function ua(e){let t=e.map(e=>e.iesMap||null).filter(e=>e),n=new Set(t);return Array.from(n).sort(sa)}function da(e){let t=new Set;for(let n=0,r=e.length;n<r;n++){let r=e[n];for(let e in r){let n=r[e];n&&n.isTexture&&t.add(n)}}return la(Array.from(t)).sort(sa)}function fa(e){let t=[];return e.traverse(e=>{e.visible&&(e.isRectAreaLight||e.isSpotLight||e.isPointLight||e.isDirectionalLight)&&t.push(e)}),t.sort(sa)}var pa=188,ma=class{constructor(){this._features={}}isUsed(e){return e in this._features}setUsed(e,t=!0){t===!1?delete this._features[e]:this._features[e]=!0}reset(){this._features={}}},ha=class extends ze{constructor(){super(new Float32Array(4),1,1),this.format=d,this.type=b,this.wrapS=w,this.wrapT=w,this.minFilter=S,this.magFilter=S,this.generateMipmaps=!1,this.features=new ma}updateFrom(e,t){function n(e,t,n=-1){if(t in e&&e[t]){let n=ca(e[t]);return u[n]}return n}function r(e,t,n){return t in e?e[t]:n}function i(e,t,n,r){let i=e[t]&&e[t].isTexture?e[t]:null;if(i){i.matrixAutoUpdate&&i.updateMatrix();let e=i.matrix.elements,t=0;n[r+t++]=e[0],n[r+t++]=e[3],n[r+t++]=e[6],t++,n[r+t++]=e[1],n[r+t++]=e[4],n[r+t++]=e[7],t++}return 8}let a=0,o=e.length*47,s=Math.ceil(Math.sqrt(o))||1,{image:c,features:l}=this,u={};for(let e=0,n=t.length;e<n;e++)u[ca(t[e])]=e;c.width!==s&&(this.dispose(),c.data=new Float32Array(s*s*4),c.width=s,c.height=s);let d=c.data;l.reset();for(let t=0,o=e.length;t<o;t++){let o=e[t];if(o.isFogVolumeMaterial){l.setUsed(`FOG`);for(let e=0;e<pa;e++)d[a+e]=0;d[a+0+0]=o.color.r,d[a+0+1]=o.color.g,d[a+0+2]=o.color.b,d[a+8+3]=r(o,`emissiveIntensity`,0),d[a+12+0]=o.emissive.r,d[a+12+1]=o.emissive.g,d[a+12+2]=o.emissive.b,d[a+52+1]=o.density,d[a+52+3]=0,d[a+56+2]=4,a+=pa;continue}d[a++]=o.color.r,d[a++]=o.color.g,d[a++]=o.color.b,d[a++]=n(o,`map`),d[a++]=r(o,`metalness`,0),d[a++]=n(o,`metalnessMap`),d[a++]=r(o,`roughness`,0),d[a++]=n(o,`roughnessMap`),d[a++]=r(o,`ior`,1.5),d[a++]=r(o,`transmission`,0),d[a++]=n(o,`transmissionMap`),d[a++]=r(o,`emissiveIntensity`,0),`emissive`in o?(d[a++]=o.emissive.r,d[a++]=o.emissive.g,d[a++]=o.emissive.b):(d[a++]=0,d[a++]=0,d[a++]=0),d[a++]=n(o,`emissiveMap`),d[a++]=n(o,`normalMap`),`normalScale`in o?(d[a++]=o.normalScale.x,d[a++]=o.normalScale.y):(d[a++]=1,d[a++]=1),d[a++]=r(o,`clearcoat`,0),d[a++]=n(o,`clearcoatMap`),d[a++]=r(o,`clearcoatRoughness`,0),d[a++]=n(o,`clearcoatRoughnessMap`),d[a++]=n(o,`clearcoatNormalMap`),`clearcoatNormalScale`in o?(d[a++]=o.clearcoatNormalScale.x,d[a++]=o.clearcoatNormalScale.y):(d[a++]=1,d[a++]=1),a++,d[a++]=r(o,`sheen`,0),`sheenColor`in o?(d[a++]=o.sheenColor.r,d[a++]=o.sheenColor.g,d[a++]=o.sheenColor.b):(d[a++]=0,d[a++]=0,d[a++]=0),d[a++]=n(o,`sheenColorMap`),d[a++]=r(o,`sheenRoughness`,0),d[a++]=n(o,`sheenRoughnessMap`),d[a++]=n(o,`iridescenceMap`),d[a++]=n(o,`iridescenceThicknessMap`),d[a++]=r(o,`iridescence`,0),d[a++]=r(o,`iridescenceIOR`,1.3);let s=r(o,`iridescenceThicknessRange`,[100,400]);d[a++]=s[0],d[a++]=s[1],`specularColor`in o?(d[a++]=o.specularColor.r,d[a++]=o.specularColor.g,d[a++]=o.specularColor.b):(d[a++]=1,d[a++]=1,d[a++]=1),d[a++]=n(o,`specularColorMap`),d[a++]=r(o,`specularIntensity`,1),d[a++]=n(o,`specularIntensityMap`);let c=r(o,`thickness`,0)===0&&r(o,`attenuationDistance`,1/0)===1/0;if(d[a++]=Number(c),a++,`attenuationColor`in o?(d[a++]=o.attenuationColor.r,d[a++]=o.attenuationColor.g,d[a++]=o.attenuationColor.b):(d[a++]=1,d[a++]=1,d[a++]=1),d[a++]=r(o,`attenuationDistance`,1/0),d[a++]=n(o,`alphaMap`),d[a++]=o.opacity,d[a++]=o.alphaTest,!c&&o.transmission>0)d[a++]=0;else switch(o.side){case 0:d[a++]=1;break;case 1:d[a++]=-1;break;case 2:d[a++]=0}d[a++]=Number(r(o,`matte`,!1)),d[a++]=Number(r(o,`castShadow`,!0)),d[a++]=Number(o.vertexColors)|Number(o.flatShading)<<1,d[a++]=Number(o.transparent),a+=i(o,`map`,d,a),a+=i(o,`metalnessMap`,d,a),a+=i(o,`roughnessMap`,d,a),a+=i(o,`transmissionMap`,d,a),a+=i(o,`emissiveMap`,d,a),a+=i(o,`normalMap`,d,a),a+=i(o,`clearcoatMap`,d,a),a+=i(o,`clearcoatNormalMap`,d,a),a+=i(o,`clearcoatRoughnessMap`,d,a),a+=i(o,`sheenColorMap`,d,a),a+=i(o,`sheenRoughnessMap`,d,a),a+=i(o,`iridescenceMap`,d,a),a+=i(o,`iridescenceThicknessMap`,d,a),a+=i(o,`specularColorMap`,d,a),a+=i(o,`specularIntensityMap`,d,a),a+=i(o,`alphaMap`,d,a)}let f=ni(d.buffer);return this.hash!==f&&(this.hash=f,this.needsUpdate=!0,!0)}},ga=new Be;function _a(e){return e?`${e.uuid}:${e.version}`:null}function va(e,t){for(let n in t)n in e&&(e[n]=t[n])}var ya=class extends fe{constructor(t,n,r){let i={format:d,type:Ee,minFilter:m,magFilter:m,wrapS:e,wrapT:e,generateMipmaps:!1,...r};super(t,n,1,i),va(this.texture,i),this.texture.setTextures=(...e)=>{this.setTextures(...e)},this.hashes=[null];let a=new oe(new ba);this.fsQuad=a}setTextures(e,t,n=this.width,r=this.height){let i=e.getRenderTarget(),a=e.toneMapping,o=e.getClearAlpha();e.getClearColor(ga);let s=t.length||1;(n!==this.width||r!==this.height||this.depth!==s)&&(this.setSize(n,r,s),this.hashes=Array(s).fill(null)),e.setClearColor(0,0),e.toneMapping=0;let c=this.fsQuad,l=this.hashes,u=!1;for(let n=0,r=s;n<r;n++){let r=t[n],i=_a(r);r&&(l[n]!==i||r.isWebGLRenderTarget)&&(r.matrixAutoUpdate=!1,r.matrix.identity(),c.material.map=r,e.setRenderTarget(this,n),c.render(e),r.updateMatrix(),r.matrixAutoUpdate=!0,l[n]=i,u=!0)}return c.material.map=null,e.setClearColor(ga,o),e.setRenderTarget(i),e.toneMapping=a,u}dispose(){super.dispose(),this.fsQuad.dispose()}},ba=class extends Fe{get map(){return this.uniforms.map.value}set map(e){this.uniforms.map.value=e}constructor(){super({uniforms:{map:{value:null}},vertexShader:`
553
+ `})}},Ki=class{generate(e,t=256){let n=new Ge(t,t,{type:d,format:D,minFilter:T,magFilter:T,generateMipmaps:!1}),r=e.getRenderTarget();e.setRenderTarget(n);let i=new He(new Gi);return i.material.resolution.set(t,t),i.render(e),e.setRenderTarget(r),i.dispose(),n}},qi=class extends y{set bokehSize(e){this.fStop=this.getFocalLength()/e}get bokehSize(){return this.getFocalLength()/this.fStop}constructor(...e){super(...e),this.fStop=1.4,this.apertureBlades=0,this.apertureRotation=0,this.focusDistance=25,this.anamorphicRatio=1}copy(e,t){return super.copy(e,t),this.fStop=e.fStop,this.apertureBlades=e.apertureBlades,this.apertureRotation=e.apertureRotation,this.focusDistance=e.focusDistance,this.anamorphicRatio=e.anamorphicRatio,this}},Ji=class{constructor(){this.bokehSize=0,this.apertureBlades=0,this.apertureRotation=0,this.focusDistance=10,this.anamorphicRatio=1}updateFrom(e){e instanceof qi?(this.bokehSize=e.bokehSize,this.apertureBlades=e.apertureBlades,this.apertureRotation=e.apertureRotation,this.focusDistance=e.focusDistance,this.anamorphicRatio=e.anamorphicRatio):(this.bokehSize=0,this.apertureRotation=0,this.apertureBlades=0,this.focusDistance=10,this.anamorphicRatio=1)}};function Yi(e){let t=new Uint16Array(e.length);for(let n=0,r=e.length;n<r;++n)t[n]=v.toHalfFloat(e[n]);return t}function Xi(e,t,n=0,r=e.length){let i=n,a=n+r-1;for(;i<a;){let n=i+a>>1;e[n]<t?i=n+1:a=n}return i-n}function Zi(e,t,n){return .2126*e+.7152*t+.0722*n}function Qi(e,t=Ye){let n=e.clone();n.source=new Ne({...n.image});let{width:r,height:i,data:a}=n.image,o=a;if(n.type!==t){o=t===1016?new Uint16Array(a.length):new Float32Array(a.length);let e;e=a instanceof Int8Array||a instanceof Int16Array||a instanceof Int32Array?2**(8*a.BYTES_PER_ELEMENT-1)-1:2**(8*a.BYTES_PER_ELEMENT)-1;for(let r=0,i=a.length;r<i;r++){let i=a[r];n.type===1016&&(i=v.fromHalfFloat(a[r])),n.type!==1015&&n.type!==1016&&(i/=e),t===1016&&(o[r]=v.toHalfFloat(i))}n.image.data=o,n.type=t}if(n.flipY){let e=o;o=o.slice();for(let t=0;t<i;t++)for(let n=0;n<r;n++){let a=i-t-1,s=4*(t*r+n),c=4*(a*r+n);o[c+0]=e[s+0],o[c+1]=e[s+1],o[c+2]=e[s+2],o[c+3]=e[s+3]}n.flipY=!1,n.image.data=o}return n}var $i=class{constructor(){let e=new h(Yi(new Float32Array([0,0,0,0])),1,1);e.type=Ye,e.format=D,e.minFilter=w,e.magFilter=w,e.wrapS=Ee,e.wrapT=Ee,e.generateMipmaps=!1,e.needsUpdate=!0;let t=new h(Yi(new Float32Array([0,1])),1,2);t.type=Ye,t.format=Le,t.minFilter=w,t.magFilter=w,t.generateMipmaps=!1,t.needsUpdate=!0;let n=new h(Yi(new Float32Array([0,0,1,1])),2,2);n.type=Ye,n.format=Le,n.minFilter=w,n.magFilter=w,n.generateMipmaps=!1,n.needsUpdate=!0,this.map=e,this.marginalWeights=t,this.conditionalWeights=n,this.totalSum=0}getPixelWeight(e,t,n){return Zi(e,t,n)}dispose(){this.marginalWeights.dispose(),this.conditionalWeights.dispose(),this.map.dispose()}updateFrom(e){let t=Qi(e);t.wrapS=Ee,t.wrapT=_;let{width:n,height:r,data:i}=t.image,a=new Float32Array(n*r),o=new Float32Array(n*r),s=new Float32Array(r),c=new Float32Array(r),l=0,u=0;for(let e=0;e<r;e++){let t=0;for(let s=0;s<n;s++){let c=e*n+s,u=v.fromHalfFloat(i[4*c+0]),d=v.fromHalfFloat(i[4*c+1]),f=v.fromHalfFloat(i[4*c+2]),p=this.getPixelWeight(u,d,f,e,r);t+=p,l+=p,a[c]=p,o[c]=t}if(t!==0)for(let r=e*n,i=e*n+n;r<i;r++)a[r]/=t,o[r]/=t;u+=t,s[e]=t,c[e]=u}if(u!==0)for(let e=0,t=s.length;e<t;e++)s[e]/=u,c[e]/=u;let d=new Uint16Array(r),f=new Uint16Array(n*r);for(let e=0;e<r;e++){let t=Xi(c,(e+1)/r);d[e]=v.toHalfFloat((t+.5)/r)}for(let e=0;e<r;e++)for(let t=0;t<n;t++){let r=e*n+t,i=Xi(o,(t+1)/n,e*n,n);f[r]=v.toHalfFloat((i+.5)/n)}this.dispose();let{marginalWeights:p,conditionalWeights:m}=this;p.image={width:r,height:1,data:d},p.needsUpdate=!0,m.image={width:n,height:r,data:f},m.needsUpdate=!0,this.totalSum=l,this.map=t}},ea=6,ta=0,na=1,ra=2,ia=3,aa=4,oa=new k,sa=new k,ca=new O,la=new ze,ua=new k,da=new k,fa=new k(0,1,0),pa=class{constructor(){let e=new h(new Float32Array(4),1,1);e.format=D,e.type=d,e.wrapS=_,e.wrapT=_,e.generateMipmaps=!1,e.minFilter=T,e.magFilter=T,this.tex=e,this.count=0}updateFrom(e,t=[]){let n=this.tex,r=Math.max(e.length*ea,1),i=Math.ceil(Math.sqrt(r));n.image.width!==i&&(n.dispose(),n.image.data=new Float32Array(i*i*4),n.image.width=i,n.image.height=i);let a=n.image.data;for(let n=0,r=e.length;n<r;n++){let r=e[n],i=n*ea*4,o=0;for(let e=0;e<24;e++)a[i+e]=0;r.getWorldPosition(sa),a[i+o++]=sa.x,a[i+o++]=sa.y,a[i+o++]=sa.z;let s=ta;if(r.isRectAreaLight&&r.isCircular?s=na:r.isSpotLight?s=ra:r.isDirectionalLight?s=ia:r.isPointLight&&(s=aa),a[i+o++]=s,a[i+o++]=r.color.r,a[i+o++]=r.color.g,a[i+o++]=r.color.b,a[i+o++]=r.intensity,r.getWorldQuaternion(la),r.isRectAreaLight)oa.set(r.width,0,0).applyQuaternion(la),a[i+o++]=oa.x,a[i+o++]=oa.y,a[i+o++]=oa.z,o++,sa.set(0,r.height,0).applyQuaternion(la),a[i+o++]=sa.x,a[i+o++]=sa.y,a[i+o++]=sa.z,a[i+o++]=oa.cross(sa).length()*(r.isCircular?Math.PI/4:1);else if(r.isSpotLight){let e=r.radius||0;ua.setFromMatrixPosition(r.matrixWorld),da.setFromMatrixPosition(r.target.matrixWorld),ca.lookAt(ua,da,fa),la.setFromRotationMatrix(ca),oa.set(1,0,0).applyQuaternion(la),a[i+o++]=oa.x,a[i+o++]=oa.y,a[i+o++]=oa.z,o++,sa.set(0,1,0).applyQuaternion(la),a[i+o++]=sa.x,a[i+o++]=sa.y,a[i+o++]=sa.z,a[i+o++]=Math.PI*e*e,a[i+o++]=e,a[i+o++]=r.decay,a[i+o++]=r.distance,a[i+o++]=Math.cos(r.angle),a[i+o++]=Math.cos(r.angle*(1-r.penumbra)),a[i+o++]=r.iesMap?t.indexOf(r.iesMap):-1}else if(r.isPointLight){let e=oa.setFromMatrixPosition(r.matrixWorld);a[i+o++]=e.x,a[i+o++]=e.y,a[i+o++]=e.z,o++,o+=4,o+=1,a[i+o++]=r.decay,a[i+o++]=r.distance}else if(r.isDirectionalLight){let e=oa.setFromMatrixPosition(r.matrixWorld),t=sa.setFromMatrixPosition(r.target.matrixWorld);da.subVectors(e,t).normalize(),a[i+o++]=da.x,a[i+o++]=da.y,a[i+o++]=da.z}}this.count=e.length;let o=di(a.buffer);return this.hash!==o&&(this.hash=o,n.needsUpdate=!0,!0)}};function ma(e,t,n,r,i){if(t>r)throw Error();let a=e.length/t,o=e.constructor.BYTES_PER_ELEMENT*8,s=1;switch(e.constructor){case Uint8Array:case Uint16Array:case Uint32Array:s=2**o-1;break;case Int8Array:case Int16Array:case Int32Array:s=2**(o-1)-1}for(let o=0;o<a;o++){let a=4*o,c=t*o;for(let o=0;o<r;o++)n[i+a+o]=t>=o+1?e[c+o]/s:0}}var ha=class extends Ce{constructor(){super(),this._textures=[],this.type=d,this.format=D,this.internalFormat=`RGBA32F`}updateAttribute(e,t){let n=this._textures[e];n.updateFrom(t);let r=n.image,i=this.image;if(r.width!==i.width||r.height!==i.height)throw Error(`FloatAttributeTextureArray: Attribute must be the same dimensions when updating single layer.`);let{width:a,height:o,data:s}=i,c=a*o*4*e,l=t.itemSize;l===3&&(l=4),ma(n.image.data,l,s,4,c),this.dispose(),this.needsUpdate=!0}setAttributes(e){let t=e[0].count,n=e.length;for(let r=0,i=n;r<i;r++)if(e[r].count!==t)throw Error(`FloatAttributeTextureArray: All attributes must have the same item count.`);let r=this._textures;for(;r.length<n;){let e=new Jr;r.push(e)}for(;r.length>n;)r.pop();for(let t=0,i=n;t<i;t++)r[t].updateFrom(e[t]);let i=r[0].image,a=this.image;(i.width!==a.width||i.height!==a.height||i.depth!==n)&&(a.width=i.width,a.height=i.height,a.depth=n,a.data=new Float32Array(a.width*a.height*a.depth*4));let{data:o,width:s,height:c}=a;for(let t=0,i=n;t<i;t++){let n=r[t],i=s*c*4*t,a=e[t].itemSize;a===3&&(a=4),ma(n.image.data,a,o,4,i)}this.dispose(),this.needsUpdate=!0}},ga=class extends ha{updateNormalAttribute(e){this.updateAttribute(0,e)}updateTangentAttribute(e){this.updateAttribute(1,e)}updateUvAttribute(e){this.updateAttribute(2,e)}updateColorAttribute(e){this.updateAttribute(3,e)}updateFrom(e,t,n,r){this.setAttributes([e,t,n,r])}};function _a(e,t){return e.uuid<t.uuid?1:e.uuid>t.uuid?-1:0}function va(e){return`${e.source.uuid}:${e.colorSpace}`}function ya(e){let t=new Set,n=[];for(let r=0,i=e.length;r<i;r++){let i=e[r],a=va(i);t.has(a)||(t.add(a),n.push(i))}return n}function ba(e){let t=e.map(e=>e.iesMap||null).filter(e=>e),n=new Set(t);return Array.from(n).sort(_a)}function xa(e){let t=new Set;for(let n=0,r=e.length;n<r;n++){let r=e[n];for(let e in r){let n=r[e];n&&n.isTexture&&t.add(n)}}return ya(Array.from(t)).sort(_a)}function Sa(e){let t=[];return e.traverseVisible(e=>{(e.isRectAreaLight||e.isSpotLight||e.isPointLight||e.isDirectionalLight)&&t.push(e)}),t.sort(_a)}var Ca=188,wa=class{constructor(){this._features={}}isUsed(e){return e in this._features}setUsed(e,t=!0){t===!1?delete this._features[e]:this._features[e]=!0}reset(){this._features={}}},Ta=class extends h{constructor(){super(new Float32Array(4),1,1),this.format=D,this.type=d,this.wrapS=_,this.wrapT=_,this.minFilter=T,this.magFilter=T,this.generateMipmaps=!1,this.features=new wa}updateFrom(e,t){function n(e,t,n=-1){if(t in e&&e[t]){let n=va(e[t]);return u[n]}return n}function r(e,t,n){return t in e?e[t]:n}function i(e,t,n,r){let i=e[t]&&e[t].isTexture?e[t]:null;if(i){i.matrixAutoUpdate&&i.updateMatrix();let e=i.matrix.elements,t=0;n[r+t++]=e[0],n[r+t++]=e[3],n[r+t++]=e[6],t++,n[r+t++]=e[1],n[r+t++]=e[4],n[r+t++]=e[7],t++}return 8}let a=0,o=e.length*47,s=Math.ceil(Math.sqrt(o))||1,{image:c,features:l}=this,u={};for(let e=0,n=t.length;e<n;e++)u[va(t[e])]=e;c.width!==s&&(this.dispose(),c.data=new Float32Array(s*s*4),c.width=s,c.height=s);let d=c.data;l.reset();for(let t=0,o=e.length;t<o;t++){let o=e[t];if(o.isFogVolumeMaterial){l.setUsed(`FOG`);for(let e=0;e<Ca;e++)d[a+e]=0;d[a+0+0]=o.color.r,d[a+0+1]=o.color.g,d[a+0+2]=o.color.b,d[a+8+3]=r(o,`emissiveIntensity`,0),d[a+12+0]=o.emissive.r,d[a+12+1]=o.emissive.g,d[a+12+2]=o.emissive.b,d[a+52+1]=o.density,d[a+52+3]=0,d[a+56+2]=4,a+=Ca;continue}d[a++]=o.color.r,d[a++]=o.color.g,d[a++]=o.color.b,d[a++]=n(o,`map`),d[a++]=r(o,`metalness`,0),d[a++]=n(o,`metalnessMap`),d[a++]=r(o,`roughness`,0),d[a++]=n(o,`roughnessMap`),d[a++]=r(o,`ior`,1.5),d[a++]=r(o,`transmission`,0),d[a++]=n(o,`transmissionMap`),d[a++]=r(o,`emissiveIntensity`,0),`emissive`in o?(d[a++]=o.emissive.r,d[a++]=o.emissive.g,d[a++]=o.emissive.b):(d[a++]=0,d[a++]=0,d[a++]=0),d[a++]=n(o,`emissiveMap`),d[a++]=n(o,`normalMap`),`normalScale`in o?(d[a++]=o.normalScale.x,d[a++]=o.normalScale.y):(d[a++]=1,d[a++]=1),d[a++]=r(o,`clearcoat`,0),d[a++]=n(o,`clearcoatMap`),d[a++]=r(o,`clearcoatRoughness`,0),d[a++]=n(o,`clearcoatRoughnessMap`),d[a++]=n(o,`clearcoatNormalMap`),`clearcoatNormalScale`in o?(d[a++]=o.clearcoatNormalScale.x,d[a++]=o.clearcoatNormalScale.y):(d[a++]=1,d[a++]=1),a++,d[a++]=r(o,`sheen`,0),`sheenColor`in o?(d[a++]=o.sheenColor.r,d[a++]=o.sheenColor.g,d[a++]=o.sheenColor.b):(d[a++]=0,d[a++]=0,d[a++]=0),d[a++]=n(o,`sheenColorMap`),d[a++]=r(o,`sheenRoughness`,0),d[a++]=n(o,`sheenRoughnessMap`),d[a++]=n(o,`iridescenceMap`),d[a++]=n(o,`iridescenceThicknessMap`),d[a++]=r(o,`iridescence`,0),d[a++]=r(o,`iridescenceIOR`,1.3);let s=r(o,`iridescenceThicknessRange`,[100,400]);d[a++]=s[0],d[a++]=s[1],`specularColor`in o?(d[a++]=o.specularColor.r,d[a++]=o.specularColor.g,d[a++]=o.specularColor.b):(d[a++]=1,d[a++]=1,d[a++]=1),d[a++]=n(o,`specularColorMap`),d[a++]=r(o,`specularIntensity`,1),d[a++]=n(o,`specularIntensityMap`);let c=r(o,`thickness`,0)===0&&r(o,`attenuationDistance`,1/0)===1/0;if(d[a++]=Number(c),a++,`attenuationColor`in o?(d[a++]=o.attenuationColor.r,d[a++]=o.attenuationColor.g,d[a++]=o.attenuationColor.b):(d[a++]=1,d[a++]=1,d[a++]=1),d[a++]=r(o,`attenuationDistance`,1/0),d[a++]=n(o,`alphaMap`),d[a++]=o.opacity,d[a++]=o.alphaTest,!c&&o.transmission>0)d[a++]=0;else switch(o.side){case 0:d[a++]=1;break;case 1:d[a++]=-1;break;case 2:d[a++]=0}d[a++]=Number(r(o,`matte`,!1)),d[a++]=Number(r(o,`castShadow`,!0)),d[a++]=Number(o.vertexColors)|Number(o.flatShading)<<1,d[a++]=Number(o.transparent),a+=i(o,`map`,d,a),a+=i(o,`metalnessMap`,d,a),a+=i(o,`roughnessMap`,d,a),a+=i(o,`transmissionMap`,d,a),a+=i(o,`emissiveMap`,d,a),a+=i(o,`normalMap`,d,a),a+=i(o,`clearcoatMap`,d,a),a+=i(o,`clearcoatNormalMap`,d,a),a+=i(o,`clearcoatRoughnessMap`,d,a),a+=i(o,`sheenColorMap`,d,a),a+=i(o,`sheenRoughnessMap`,d,a),a+=i(o,`iridescenceMap`,d,a),a+=i(o,`iridescenceThicknessMap`,d,a),a+=i(o,`specularColorMap`,d,a),a+=i(o,`specularIntensityMap`,d,a),a+=i(o,`alphaMap`,d,a)}let f=di(d.buffer);return this.hash!==f&&(this.hash=f,this.needsUpdate=!0,!0)}},Ea=new t;function Da(e){return e?`${e.uuid}:${e.version}`:null}function Oa(e,t){for(let n in t)n in e&&(e[n]=t[n])}var ka=class extends i{constructor(e,t,n){let r={format:D,type:oe,minFilter:w,magFilter:w,wrapS:Ee,wrapT:Ee,generateMipmaps:!1,...n};super(e,t,1,r),Oa(this.texture,r),this.texture.setTextures=(...e)=>{this.setTextures(...e)},this.hashes=[null];let i=new He(new Aa);this.fsQuad=i}setTextures(e,t,n=this.width,r=this.height){let i=e.getRenderTarget(),a=e.toneMapping,o=e.getClearAlpha();e.getClearColor(Ea);let s=t.length||1;(n!==this.width||r!==this.height||this.depth!==s)&&(this.setSize(n,r,s),this.hashes=Array(s).fill(null)),e.setClearColor(0,0),e.toneMapping=0;let c=this.fsQuad,l=this.hashes,u=!1;for(let n=0,r=s;n<r;n++){let r=t[n],i=Da(r);r&&(l[n]!==i||r.isWebGLRenderTarget)&&(r.matrixAutoUpdate=!1,r.matrix.identity(),c.material.map=r,e.setRenderTarget(this,n),c.render(e),r.updateMatrix(),r.matrixAutoUpdate=!0,l[n]=i,u=!0)}return c.material.map=null,e.setClearColor(Ea,o),e.setRenderTarget(i),e.toneMapping=a,u}dispose(){super.dispose(),this.fsQuad.dispose()}},Aa=class extends pe{get map(){return this.uniforms.map.value}set map(e){this.uniforms.map.value=e}constructor(){super({uniforms:{map:{value:null}},vertexShader:`
554
554
  varying vec2 vUv;
555
555
  void main() {
556
556
 
@@ -566,7 +566,7 @@ struct BVH {
566
566
  gl_FragColor = texture2D( map, vUv );
567
567
 
568
568
  }
569
- `})}};function xa(e,t=Math.random()){for(let n=e.length-1;n>0;n--){let r=Math.floor(t()*(n+1)),i=e[n];e[n]=e[r],e[r]=i}return e}var Sa=class{constructor(e,t,n=Math.random){let r=e**t,i=new Uint16Array(r),a=r;for(let e=0;e<r;e++)i[e]=e;this.samples=new Float32Array(t),this.strataCount=e,this.reset=function(){for(let e=0;e<r;e++)i[e]=e;a=0},this.reshuffle=function(){a=0},this.next=function(){let{samples:r}=this;a>=i.length&&(xa(i,n),this.reshuffle());let o=i[a++];for(let i=0;i<t;i++)r[i]=(o%e+n())/e,o=Math.floor(o/e);return r}}},Ca=class{constructor(e,t,n=Math.random){let r=0;for(let e of t)r+=e;let i=new Float32Array(r),a=[],o=0;for(let r of t){let t=new Sa(e,r,n);t.samples=new Float32Array(i.buffer,o,t.samples.length),o+=t.samples.length*4,a.push(t)}this.samples=i,this.strataCount=e,this.next=function(){for(let e of a)e.next();return i},this.reshuffle=function(){for(let e of a)e.reshuffle()},this.reset=function(){for(let e of a)e.reset()}}},wa=class{constructor(e=0){this.m=2147483648,this.a=1103515245,this.c=12345,this.seed=e}nextInt(){return this.seed=(this.a*this.seed+this.c)%this.m,this.seed}nextFloat(){return this.nextInt()/(this.m-1)}},Ta=class extends ze{constructor(e=1,t=1,n=8){super(new Float32Array(1),1,1,d,b),this.minFilter=S,this.magFilter=S,this.strata=n,this.sampler=null,this.generator=new wa,this.stableNoise=!1,this.random=()=>this.stableNoise?this.generator.nextFloat():Math.random(),this.init(e,t,n)}init(e=this.image.height,t=this.image.width,n=this.strata){let{image:r}=this;if(r.width===t&&r.height===e&&this.sampler!==null)return;let i=new Ca(n,Array(e*t).fill(4),this.random);r.width=t,r.height=e,r.data=i.samples,this.sampler=i,this.dispose(),this.next()}next(){this.sampler.next(),this.needsUpdate=!0}reset(){this.sampler.reset(),this.generator.seed=0}};function Ea(e,t=Math.random){for(let n=e.length-1;n>0;n--){let r=~~((t()-1e-6)*n),i=e[n];e[n]=e[r],e[r]=i}}function Da(e,t){e.fill(0);for(let n=0;n<t;n++)e[n]=1}var Oa=class{constructor(e){this.count=0,this.size=-1,this.sigma=-1,this.radius=-1,this.lookupTable=null,this.score=null,this.binaryPattern=null,this.resize(e),this.setSigma(1.5)}findVoid(){let{score:e,binaryPattern:t}=this,n=1/0,r=-1;for(let i=0,a=t.length;i<a;i++){if(t[i]!==0)continue;let a=e[i];a<n&&(n=a,r=i)}return r}findCluster(){let{score:e,binaryPattern:t}=this,n=-1/0,r=-1;for(let i=0,a=t.length;i<a;i++){if(t[i]!==1)continue;let a=e[i];a>n&&(n=a,r=i)}return r}setSigma(e){if(e===this.sigma)return;let t=~~(Math.sqrt(20*e**2)+1),n=2*t+1,r=new Float32Array(n*n),i=e*e;for(let e=-t;e<=t;e++)for(let a=-t;a<=t;a++){let o=(t+a)*n+e+t,s=e*e+a*a;r[o]=Math.E**(-s/(2*i))}this.lookupTable=r,this.sigma=e,this.radius=t}resize(e){this.size!==e&&(this.size=e,this.score=new Float32Array(e*e),this.binaryPattern=new Uint8Array(e*e))}invert(){let{binaryPattern:e,score:t,size:n}=this;t.fill(0);for(let t=0,r=e.length;t<r;t++)if(e[t]===0){let r=~~(t/n),i=t-r*n;this.updateScore(i,r,1),e[t]=1}else e[t]=0}updateScore(e,t,n){let{size:r,score:i,lookupTable:a}=this,o=this.radius,s=2*o+1;for(let c=-o;c<=o;c++)for(let l=-o;l<=o;l++){let u=a[(o+l)*s+c+o],d=e+c;d=d<0?r+d:d%r;let f=t+l;f=f<0?r+f:f%r;let p=f*r+d;i[p]+=n*u}}addPointIndex(e){this.binaryPattern[e]=1;let t=this.size,n=~~(e/t),r=e-n*t;this.updateScore(r,n,1),this.count++}removePointIndex(e){this.binaryPattern[e]=0;let t=this.size,n=~~(e/t),r=e-n*t;this.updateScore(r,n,-1),this.count--}copy(e){this.resize(e.size),this.score.set(e.score),this.binaryPattern.set(e.binaryPattern),this.setSigma(e.sigma),this.count=e.count}},ka=class{constructor(){this.random=Math.random,this.sigma=1.5,this.size=64,this.majorityPointsRatio=.1,this.samples=new Oa(1),this.savedSamples=new Oa(1)}generate(){let{samples:e,savedSamples:t,sigma:n,majorityPointsRatio:r,size:i}=this;e.resize(i),e.setSigma(n);let a=Math.floor(i*i*r),o=e.binaryPattern;Da(o,a),Ea(o,this.random);for(let t=0,n=o.length;t<n;t++)o[t]===1&&e.addPointIndex(t);for(;;){let t=e.findCluster();e.removePointIndex(t);let n=e.findVoid();if(t===n){e.addPointIndex(t);break}e.addPointIndex(n)}let s=new Uint32Array(i*i);t.copy(e);let c;for(c=e.count-1;c>=0;){let t=e.findCluster();e.removePointIndex(t),s[t]=c,c--}let l=i*i;for(c=t.count;c<l/2;){let e=t.findVoid();t.addPointIndex(e),s[e]=c,c++}for(t.invert();c<l;){let e=t.findCluster();t.removePointIndex(e),s[e]=c,c++}return{data:s,maxValue:l}}};function Aa(e){return e>=3?4:e}function ja(e){switch(e){case 1:return ne;case 2:return u;default:return d}}var Ma=class extends ze{constructor(e=64,t=1){super(new Float32Array(4),1,1,d,b),this.minFilter=S,this.magFilter=S,this.size=e,this.channels=t,this.update()}update(){let e=this.channels,t=this.size,n=new ka;n.channels=e,n.size=t;let r=Aa(e),i=ja(r);(this.image.width!==t||i!==this.format)&&(this.image.width=t,this.image.height=t,this.image.data=new Float32Array(t**2*r),this.format=i,this.dispose());let a=this.image.data;for(let t=0,i=e;t<i;t++){let e=n.generate(),i=e.data,o=e.maxValue;for(let e=0,n=i.length;e<n;e++){let n=i[e]/o;a[e*r+t]=n}}this.needsUpdate=!0}},Na=`
569
+ `})}};function ja(e,t=Math.random()){for(let n=e.length-1;n>0;n--){let r=Math.floor(t()*(n+1)),i=e[n];e[n]=e[r],e[r]=i}return e}var Ma=class{constructor(e,t,n=Math.random){let r=e**t,i=new Uint16Array(r),a=r;for(let e=0;e<r;e++)i[e]=e;this.samples=new Float32Array(t),this.strataCount=e,this.reset=function(){for(let e=0;e<r;e++)i[e]=e;a=0},this.reshuffle=function(){a=0},this.next=function(){let{samples:r}=this;a>=i.length&&(ja(i,n),this.reshuffle());let o=i[a++];for(let i=0;i<t;i++)r[i]=(o%e+n())/e,o=Math.floor(o/e);return r}}},Na=class{constructor(e,t,n=Math.random){let r=0;for(let e of t)r+=e;let i=new Float32Array(r),a=[],o=0;for(let r of t){let t=new Ma(e,r,n);t.samples=new Float32Array(i.buffer,o,t.samples.length),o+=t.samples.length*4,a.push(t)}this.samples=i,this.strataCount=e,this.next=function(){for(let e of a)e.next();return i},this.reshuffle=function(){for(let e of a)e.reshuffle()},this.reset=function(){for(let e of a)e.reset()}}},Pa=class{constructor(e=0){this.m=2147483648,this.a=1103515245,this.c=12345,this.seed=e}nextInt(){return this.seed=(this.a*this.seed+this.c)%this.m,this.seed}nextFloat(){return this.nextInt()/(this.m-1)}},Fa=class extends h{constructor(e=1,t=1,n=8){super(new Float32Array(1),1,1,D,d),this.minFilter=T,this.magFilter=T,this.strata=n,this.sampler=null,this.generator=new Pa,this.stableNoise=!1,this.random=()=>this.stableNoise?this.generator.nextFloat():Math.random(),this.init(e,t,n)}init(e=this.image.height,t=this.image.width,n=this.strata){let{image:r}=this;if(r.width===t&&r.height===e&&this.sampler!==null)return;let i=new Na(n,Array(e*t).fill(4),this.random);r.width=t,r.height=e,r.data=i.samples,this.sampler=i,this.dispose(),this.next()}next(){this.sampler.next(),this.needsUpdate=!0}reset(){this.sampler.reset(),this.generator.seed=0}};function Ia(e,t=Math.random){for(let n=e.length-1;n>0;n--){let r=~~((t()-1e-6)*n),i=e[n];e[n]=e[r],e[r]=i}}function La(e,t){e.fill(0);for(let n=0;n<t;n++)e[n]=1}var Ra=class{constructor(e){this.count=0,this.size=-1,this.sigma=-1,this.radius=-1,this.lookupTable=null,this.score=null,this.binaryPattern=null,this.resize(e),this.setSigma(1.5)}findVoid(){let{score:e,binaryPattern:t}=this,n=1/0,r=-1;for(let i=0,a=t.length;i<a;i++){if(t[i]!==0)continue;let a=e[i];a<n&&(n=a,r=i)}return r}findCluster(){let{score:e,binaryPattern:t}=this,n=-1/0,r=-1;for(let i=0,a=t.length;i<a;i++){if(t[i]!==1)continue;let a=e[i];a>n&&(n=a,r=i)}return r}setSigma(e){if(e===this.sigma)return;let t=~~(Math.sqrt(20*e**2)+1),n=2*t+1,r=new Float32Array(n*n),i=e*e;for(let e=-t;e<=t;e++)for(let a=-t;a<=t;a++){let o=(t+a)*n+e+t,s=e*e+a*a;r[o]=Math.E**(-s/(2*i))}this.lookupTable=r,this.sigma=e,this.radius=t}resize(e){this.size!==e&&(this.size=e,this.score=new Float32Array(e*e),this.binaryPattern=new Uint8Array(e*e))}invert(){let{binaryPattern:e,score:t,size:n}=this;t.fill(0);for(let t=0,r=e.length;t<r;t++)if(e[t]===0){let r=~~(t/n),i=t-r*n;this.updateScore(i,r,1),e[t]=1}else e[t]=0}updateScore(e,t,n){let{size:r,score:i,lookupTable:a}=this,o=this.radius,s=2*o+1;for(let c=-o;c<=o;c++)for(let l=-o;l<=o;l++){let u=a[(o+l)*s+c+o],d=e+c;d=d<0?r+d:d%r;let f=t+l;f=f<0?r+f:f%r;let p=f*r+d;i[p]+=n*u}}addPointIndex(e){this.binaryPattern[e]=1;let t=this.size,n=~~(e/t),r=e-n*t;this.updateScore(r,n,1),this.count++}removePointIndex(e){this.binaryPattern[e]=0;let t=this.size,n=~~(e/t),r=e-n*t;this.updateScore(r,n,-1),this.count--}copy(e){this.resize(e.size),this.score.set(e.score),this.binaryPattern.set(e.binaryPattern),this.setSigma(e.sigma),this.count=e.count}},za=class{constructor(){this.random=Math.random,this.sigma=1.5,this.size=64,this.majorityPointsRatio=.1,this.samples=new Ra(1),this.savedSamples=new Ra(1)}generate(){let{samples:e,savedSamples:t,sigma:n,majorityPointsRatio:r,size:i}=this;e.resize(i),e.setSigma(n);let a=Math.floor(i*i*r),o=e.binaryPattern;La(o,a),Ia(o,this.random);for(let t=0,n=o.length;t<n;t++)o[t]===1&&e.addPointIndex(t);for(;;){let t=e.findCluster();e.removePointIndex(t);let n=e.findVoid();if(t===n){e.addPointIndex(t);break}e.addPointIndex(n)}let s=new Uint32Array(i*i);t.copy(e);let c;for(c=e.count-1;c>=0;){let t=e.findCluster();e.removePointIndex(t),s[t]=c,c--}let l=i*i;for(c=t.count;c<l/2;){let e=t.findVoid();t.addPointIndex(e),s[e]=c,c++}for(t.invert();c<l;){let e=t.findCluster();t.removePointIndex(e),s[e]=c,c++}return{data:s,maxValue:l}}};function Ba(e){return e>=3?4:e}function Va(e){switch(e){case 1:return Le;case 2:return Se;default:return D}}var Ha=class extends h{constructor(e=64,t=1){super(new Float32Array(4),1,1,D,d),this.minFilter=T,this.magFilter=T,this.size=e,this.channels=t,this.update()}update(){let e=this.channels,t=this.size,n=new za;n.channels=e,n.size=t;let r=Ba(e),i=Va(r);(this.image.width!==t||i!==this.format)&&(this.image.width=t,this.image.height=t,this.image.data=new Float32Array(t**2*r),this.format=i,this.dispose());let a=this.image.data;for(let t=0,i=e;t<i;t++){let e=n.generate(),i=e.data,o=e.maxValue;for(let e=0,n=i.length;e<n;e++){let n=i[e]/o;a[e*r+t]=n}}this.needsUpdate=!0}},Ua=`
570
570
 
571
571
  struct PhysicalCamera {
572
572
 
@@ -578,7 +578,7 @@ struct BVH {
578
578
 
579
579
  };
580
580
 
581
- `,Pa=`
581
+ `,Wa=`
582
582
 
583
583
  struct EquirectHdrInfo {
584
584
 
@@ -590,7 +590,7 @@ struct BVH {
590
590
 
591
591
  };
592
592
 
593
- `,Fa=`
593
+ `,Ga=`
594
594
 
595
595
  #define RECT_AREA_LIGHT_TYPE 0
596
596
  #define CIRC_AREA_LIGHT_TYPE 1
@@ -676,7 +676,7 @@ struct BVH {
676
676
 
677
677
  }
678
678
 
679
- `,Ia=`
679
+ `,Ka=`
680
680
 
681
681
  struct Material {
682
682
 
@@ -885,7 +885,7 @@ struct BVH {
885
885
 
886
886
  }
887
887
 
888
- `,La=`
888
+ `,qa=`
889
889
 
890
890
  struct SurfaceRecord {
891
891
 
@@ -947,7 +947,7 @@ struct BVH {
947
947
  vec3 color;
948
948
  };
949
949
 
950
- `,Ra=`
950
+ `,Ja=`
951
951
 
952
952
  // samples the the given environment map in the given direction
953
953
  vec3 sampleEquirectColor( sampler2D envMap, vec3 direction ) {
@@ -1014,7 +1014,7 @@ struct BVH {
1014
1014
  return float( resolution.x * resolution.y ) * pdf * equirectDirectionPdf( direction );
1015
1015
 
1016
1016
  }
1017
- `,za=`
1017
+ `,Ya=`
1018
1018
 
1019
1019
  float getSpotAttenuation( const in float coneCosine, const in float penumbraCosine, const in float angleCosine ) {
1020
1020
 
@@ -1229,7 +1229,7 @@ struct BVH {
1229
1229
 
1230
1230
  }
1231
1231
 
1232
- `,Ba=`
1232
+ `,Xa=`
1233
1233
 
1234
1234
  vec3 sampleHemisphere( vec3 n, vec2 uv ) {
1235
1235
 
@@ -1314,7 +1314,7 @@ struct BVH {
1314
1314
  }
1315
1315
 
1316
1316
 
1317
- `,Va=`
1317
+ `,Za=`
1318
1318
 
1319
1319
  bool totalInternalReflection( float cosTheta, float eta ) {
1320
1320
 
@@ -1417,7 +1417,7 @@ struct BVH {
1417
1417
 
1418
1418
  }
1419
1419
 
1420
- `,Ha=`
1420
+ `,Qa=`
1421
1421
 
1422
1422
  // Fast arccos approximation used to remove banding artifacts caused by numerical errors in acos.
1423
1423
  // This is a cubic Lagrange interpolating polynomial for x = [-1, -1/2, 0, 1/2, 1].
@@ -1497,7 +1497,7 @@ struct BVH {
1497
1497
 
1498
1498
  }
1499
1499
 
1500
- `,Ua=`
1500
+ `,$a=`
1501
1501
 
1502
1502
  // Finds the point where the ray intersects the plane defined by u and v and checks if this point
1503
1503
  // falls in the bounds of the rectangle on that same plane.
@@ -1558,7 +1558,7 @@ struct BVH {
1558
1558
 
1559
1559
  }
1560
1560
 
1561
- `,Wa=`
1561
+ `,eo=`
1562
1562
 
1563
1563
  // add texel fetch functions for texture arrays
1564
1564
  vec4 texelFetch1D( sampler2DArray tex, int layer, uint index ) {
@@ -1581,7 +1581,7 @@ struct BVH {
1581
1581
 
1582
1582
  }
1583
1583
 
1584
- `,Ga=`
1584
+ `,to=`
1585
1585
 
1586
1586
  // TODO: possibly this should be renamed something related to material or path tracing logic
1587
1587
 
@@ -1696,7 +1696,7 @@ struct BVH {
1696
1696
  return x < 0.5 ? sqrt( 2.0 * x ) - 1.0 : 1.0 - sqrt( 2.0 - ( 2.0 * x ) );
1697
1697
 
1698
1698
  }
1699
- `,Ka=`
1699
+ `,no=`
1700
1700
 
1701
1701
  // https://www.shadertoy.com/view/wltcRS
1702
1702
  uvec4 WHITE_NOISE_SEED;
@@ -1752,7 +1752,7 @@ struct BVH {
1752
1752
  return vec4( WHITE_NOISE_SEED ) / float( 0xffffffffu );
1753
1753
 
1754
1754
  }
1755
- `,qa=`
1755
+ `,ro=`
1756
1756
 
1757
1757
  uniform sampler2D stratifiedTexture;
1758
1758
  uniform sampler2D stratifiedOffsetTexture;
@@ -1801,7 +1801,7 @@ struct BVH {
1801
1801
 
1802
1802
  }
1803
1803
 
1804
- `,Ja=`
1804
+ `,io=`
1805
1805
 
1806
1806
  // diffuse
1807
1807
  float diffuseEval( vec3 wo, vec3 wi, vec3 wh, SurfaceRecord surf, inout vec3 color ) {
@@ -2025,7 +2025,6 @@ struct BVH {
2025
2025
  // See equation (1) in http://www.aconty.com/pdf/s2017_pbs_imageworks_sheen.pdf
2026
2026
  vec3 color = surf.sheenColor;
2027
2027
  color *= D * G / ( 4.0 * abs( cosThetaO * cosThetaI ) );
2028
- color *= wi.z;
2029
2028
 
2030
2029
  return color;
2031
2030
 
@@ -2239,7 +2238,7 @@ struct BVH {
2239
2238
 
2240
2239
  }
2241
2240
 
2242
- `,Ya=`
2241
+ `,ao=`
2243
2242
 
2244
2243
  // returns the hit distance given the material density
2245
2244
  float intersectFogVolume( Material material, float u ) {
@@ -2260,7 +2259,7 @@ struct BVH {
2260
2259
 
2261
2260
  }
2262
2261
 
2263
- `,Xa=`
2262
+ `,oo=`
2264
2263
 
2265
2264
  // The GGX functions provide sampling and distribution information for normals as output so
2266
2265
  // in order to get probability of scatter direction the half vector must be computed and provided.
@@ -2361,7 +2360,7 @@ struct BVH {
2361
2360
 
2362
2361
  }
2363
2362
 
2364
- `,Za=`
2363
+ `,so=`
2365
2364
 
2366
2365
  // XYZ to sRGB color space
2367
2366
  const mat3 XYZ_TO_REC709 = mat3(
@@ -2495,7 +2494,7 @@ struct BVH {
2495
2494
 
2496
2495
  }
2497
2496
 
2498
- `,Qa=`
2497
+ `,co=`
2499
2498
 
2500
2499
  // See equation (2) in http://www.aconty.com/pdf/s2017_pbs_imageworks_sheen.pdf
2501
2500
  float velvetD( float cosThetaH, float roughness ) {
@@ -2592,7 +2591,7 @@ struct BVH {
2592
2591
 
2593
2592
  }
2594
2593
 
2595
- `,$a=`
2594
+ `,lo=`
2596
2595
 
2597
2596
  #ifndef FOG_CHECK_ITERATIONS
2598
2597
  #define FOG_CHECK_ITERATIONS 30
@@ -2653,7 +2652,7 @@ bool bvhIntersectFogVolumeHit(
2653
2652
 
2654
2653
  }
2655
2654
 
2656
- `,eo=`
2655
+ `,uo=`
2657
2656
 
2658
2657
  // step through multiple surface hits and accumulate color attenuation based on transmissive surfaces
2659
2658
  // returns true if a solid surface was hit
@@ -2831,7 +2830,7 @@ bool bvhIntersectFogVolumeHit(
2831
2830
 
2832
2831
  }
2833
2832
 
2834
- `,to=`
2833
+ `,fo=`
2835
2834
 
2836
2835
  vec3 ndcToRayOrigin( vec2 coord ) {
2837
2836
 
@@ -2914,7 +2913,7 @@ bool bvhIntersectFogVolumeHit(
2914
2913
 
2915
2914
  }
2916
2915
 
2917
- `,no=`
2916
+ `,po=`
2918
2917
 
2919
2918
  vec3 directLightContribution( vec3 worldWo, SurfaceRecord surf, RenderState state, vec3 rayOrigin ) {
2920
2919
 
@@ -3010,7 +3009,7 @@ bool bvhIntersectFogVolumeHit(
3010
3009
 
3011
3010
  }
3012
3011
 
3013
- `,ro=`
3012
+ `,mo=`
3014
3013
 
3015
3014
  #define SKIP_SURFACE 0
3016
3015
  #define HIT_SURFACE 1
@@ -3333,7 +3332,7 @@ bool bvhIntersectFogVolumeHit(
3333
3332
  return HIT_SURFACE;
3334
3333
 
3335
3334
  }
3336
- `,io=`
3335
+ `,ho=`
3337
3336
 
3338
3337
  struct Ray {
3339
3338
 
@@ -3382,7 +3381,7 @@ bool bvhIntersectFogVolumeHit(
3382
3381
 
3383
3382
  }
3384
3383
 
3385
- `,ao=`
3384
+ `,go=`
3386
3385
 
3387
3386
  #define NO_HIT 0
3388
3387
  #define SURFACE_HIT 1
@@ -3429,7 +3428,7 @@ bool bvhIntersectFogVolumeHit(
3429
3428
 
3430
3429
  }
3431
3430
 
3432
- `,oo=class extends ki{onBeforeRender(){this.setDefine(`FEATURE_DOF`,this.physicalCamera.bokehSize===0?0:1),this.setDefine(`FEATURE_BACKGROUND_MAP`,+!!this.backgroundMap),this.setDefine(`FEATURE_FOG`,+!!this.materials.features.isUsed(`FOG`))}constructor(e){super({transparent:!0,depthWrite:!1,defines:{FEATURE_MIS:1,FEATURE_RUSSIAN_ROULETTE:1,FEATURE_DOF:1,FEATURE_BACKGROUND_MAP:0,FEATURE_FOG:1,RANDOM_TYPE:2,CAMERA_TYPE:0,DEBUG_MODE:0,ATTR_NORMAL:0,ATTR_TANGENT:1,ATTR_UV:2,ATTR_COLOR:3,MATERIAL_PIXELS:47},uniforms:{resolution:{value:new T},opacity:{value:1},bounces:{value:10},transmissiveBounces:{value:10},filterGlossyFactor:{value:0},physicalCamera:{value:new zi},cameraWorldMatrix:{value:new f},invProjectionMatrix:{value:new f},bvh:{value:new Br},attributesArray:{value:new oa},materialIndexAttribute:{value:new Rr},materials:{value:new ha},textures:{value:new ya().texture},lights:{value:new ra},iesProfiles:{value:new ya(360,180,{type:ye,wrapS:w,wrapT:w}).texture},environmentIntensity:{value:1},environmentRotation:{value:new f},envMapInfo:{value:new Wi},backgroundBlur:{value:0},backgroundMap:{value:null},backgroundAlpha:{value:1},backgroundIntensity:{value:1},backgroundRotation:{value:new f},seed:{value:0},sobolTexture:{value:null},stratifiedTexture:{value:new Ta},stratifiedOffsetTexture:{value:new Ma(64,1)}},vertexShader:`
3431
+ `,_o=class extends Ri{onBeforeRender(){this.setDefine(`FEATURE_DOF`,this.physicalCamera.bokehSize===0?0:1),this.setDefine(`FEATURE_BACKGROUND_MAP`,+!!this.backgroundMap),this.setDefine(`FEATURE_FOG`,+!!this.materials.features.isUsed(`FOG`))}constructor(e){super({transparent:!0,depthWrite:!1,defines:{FEATURE_MIS:1,FEATURE_RUSSIAN_ROULETTE:1,FEATURE_DOF:1,FEATURE_BACKGROUND_MAP:0,FEATURE_FOG:1,RANDOM_TYPE:2,CAMERA_TYPE:0,DEBUG_MODE:0,ATTR_NORMAL:0,ATTR_TANGENT:1,ATTR_UV:2,ATTR_COLOR:3,MATERIAL_PIXELS:47},uniforms:{resolution:{value:new E},opacity:{value:1},bounces:{value:10},transmissiveBounces:{value:10},filterGlossyFactor:{value:0},physicalCamera:{value:new Ji},cameraWorldMatrix:{value:new O},invProjectionMatrix:{value:new O},bvh:{value:new Yr},attributesArray:{value:new ga},materialIndexAttribute:{value:new qr},materials:{value:new Ta},textures:{value:new ka().texture},lights:{value:new pa},iesProfiles:{value:new ka(360,180,{type:Ye,wrapS:_,wrapT:_}).texture},environmentIntensity:{value:1},environmentRotation:{value:new O},envMapInfo:{value:new $i},backgroundBlur:{value:0},backgroundMap:{value:null},backgroundAlpha:{value:1},backgroundIntensity:{value:1},backgroundRotation:{value:new O},seed:{value:0},sobolTexture:{value:null},stratifiedTexture:{value:new Fa},stratifiedOffsetTexture:{value:new Ha(64,1)}},vertexShader:`
3433
3432
 
3434
3433
  varying vec2 vUv;
3435
3434
  void main() {
@@ -3453,27 +3452,27 @@ bool bvhIntersectFogVolumeHit(
3453
3452
  #include <common>
3454
3453
 
3455
3454
  // bvh intersection
3456
- ${Ur}
3457
- ${Gr}
3458
- ${Wr}
3455
+ ${Qr}
3456
+ ${ei}
3457
+ ${$r}
3459
3458
 
3460
3459
  // uniform structs
3461
- ${Na}
3462
- ${Fa}
3463
- ${Pa}
3464
- ${Ia}
3465
- ${La}
3460
+ ${Ua}
3461
+ ${Ga}
3462
+ ${Wa}
3463
+ ${Ka}
3464
+ ${qa}
3466
3465
 
3467
3466
  // random
3468
3467
  #if RANDOM_TYPE == 2 // Stratified List
3469
3468
 
3470
- ${qa}
3469
+ ${ro}
3471
3470
 
3472
3471
  #elif RANDOM_TYPE == 1 // Sobol
3473
3472
 
3474
- ${Ka}
3475
- ${Ni}
3476
- ${Fi}
3473
+ ${no}
3474
+ ${Hi}
3475
+ ${Wi}
3477
3476
 
3478
3477
  #define rand(v) sobol(v)
3479
3478
  #define rand2(v) sobol2(v)
@@ -3482,7 +3481,7 @@ bool bvhIntersectFogVolumeHit(
3482
3481
 
3483
3482
  #else // PCG
3484
3483
 
3485
- ${Ka}
3484
+ ${no}
3486
3485
 
3487
3486
  // Using the sobol functions seems to break the the compiler on MacOS
3488
3487
  // - specifically the "sobolReverseBits" function.
@@ -3498,11 +3497,11 @@ bool bvhIntersectFogVolumeHit(
3498
3497
  #endif
3499
3498
 
3500
3499
  // common
3501
- ${Wa}
3502
- ${Va}
3503
- ${Ga}
3504
- ${Ha}
3505
- ${Ua}
3500
+ ${eo}
3501
+ ${Za}
3502
+ ${to}
3503
+ ${Qa}
3504
+ ${$a}
3506
3505
 
3507
3506
  // environment
3508
3507
  uniform EquirectHdrInfo envMapInfo;
@@ -3558,16 +3557,16 @@ bool bvhIntersectFogVolumeHit(
3558
3557
  float lightsDenom;
3559
3558
 
3560
3559
  // sampling
3561
- ${Ba}
3562
- ${Ra}
3563
- ${za}
3564
-
3565
- ${$a}
3566
3560
  ${Xa}
3567
- ${Qa}
3568
- ${Za}
3569
- ${Ya}
3570
3561
  ${Ja}
3562
+ ${Ya}
3563
+
3564
+ ${lo}
3565
+ ${oo}
3566
+ ${co}
3567
+ ${so}
3568
+ ${ao}
3569
+ ${io}
3571
3570
 
3572
3571
  float applyFilteredGlossy( float roughness, float accumulatedRoughness ) {
3573
3572
 
@@ -3599,12 +3598,12 @@ bool bvhIntersectFogVolumeHit(
3599
3598
 
3600
3599
  }
3601
3600
 
3602
- ${io}
3603
- ${to}
3604
- ${ao}
3605
- ${eo}
3606
- ${no}
3607
- ${ro}
3601
+ ${ho}
3602
+ ${fo}
3603
+ ${go}
3604
+ ${uo}
3605
+ ${po}
3606
+ ${mo}
3608
3607
 
3609
3608
  void main() {
3610
3609
 
@@ -3896,7 +3895,7 @@ bool bvhIntersectFogVolumeHit(
3896
3895
 
3897
3896
  }
3898
3897
 
3899
- `}),this.setValues(e)}};function*so(){let{_renderer:e,_fsQuad:t,_blendQuad:n,_primaryTarget:r,_blendTargets:i,_sobolTarget:a,_subframe:o,alpha:s,material:c}=this,l=new le,u=new le,d=n.material,[f,p]=i;for(;;){s?(d.opacity=this._opacityFactor/(this.samples+1),c.blending=0,c.opacity=1):(c.opacity=this._opacityFactor/(this.samples+1),c.blending=1);let[i,m,h,g]=o,_=r.width,v=r.height;c.resolution.set(_*h,v*g),c.sobolTexture=a.texture,c.stratifiedTexture.init(20,c.bounces+c.transmissiveBounces+5),c.stratifiedTexture.next(),c.seed++;let y=this.tiles.x||1,b=this.tiles.y||1,x=y*b,S=Math.ceil(_*h),C=Math.ceil(v*g),ee=Math.floor(i*_),te=Math.floor(m*v),ne=Math.ceil(S/y),w=Math.ceil(C/b);for(let i=0;i<b;i++)for(let a=0;a<y;a++){let o=e.getRenderTarget(),c=e.autoClear,m=e.getScissorTest();e.getScissor(l),e.getViewport(u);let h=a,g=i;if(!this.stableTiles){let e=this._currentTile%(y*b);h=e%y,g=~~(e/y),this._currentTile=e+1}let _=b-g-1;r.scissor.set(ee+h*ne,te+_*w,Math.min(ne,S-h*ne),Math.min(w,C-_*w)),r.viewport.set(ee,te,S,C),e.setRenderTarget(r),e.setScissorTest(!0),e.autoClear=!1,t.render(e),e.setViewport(u),e.setScissor(l),e.setScissorTest(m),e.setRenderTarget(o),e.autoClear=c,s&&(d.target1=f.texture,d.target2=r.texture,e.setRenderTarget(p),n.render(e),e.setRenderTarget(o)),this.samples+=1/x,a===y-1&&i===b-1&&(this.samples=Math.round(this.samples)),yield}[f,p]=[p,f]}}var co=new Be,lo=class{get material(){return this._fsQuad.material}set material(e){this._fsQuad.material.removeEventListener(`recompilation`,this._compileFunction),e.addEventListener(`recompilation`,this._compileFunction),this._fsQuad.material=e}get target(){return this._alpha?this._blendTargets[1]:this._primaryTarget}set alpha(e){this._alpha!==e&&(e||(this._blendTargets[0].dispose(),this._blendTargets[1].dispose()),this._alpha=e,this.reset())}get alpha(){return this._alpha}get isCompiling(){return!!this._compilePromise}constructor(e){this.camera=null,this.tiles=new T(3,3),this.stableNoise=!1,this.stableTiles=!0,this.samples=0,this._subframe=new le(0,0,1,1),this._opacityFactor=1,this._renderer=e,this._alpha=!1,this._fsQuad=new oe(new oo),this._blendQuad=new oe(new Ai),this._task=null,this._currentTile=0,this._compilePromise=null,this._sobolTarget=new Li().generate(e),this._primaryTarget=new Oe(1,1,{format:d,type:b,magFilter:S,minFilter:S}),this._blendTargets=[new Oe(1,1,{format:d,type:b,magFilter:S,minFilter:S}),new Oe(1,1,{format:d,type:b,magFilter:S,minFilter:S})],this._compileFunction=()=>{let e=this.compileMaterial(this._fsQuad._mesh);e.then(()=>{this._compilePromise===e&&(this._compilePromise=null)}),this._compilePromise=e},this.material.addEventListener(`recompilation`,this._compileFunction)}compileMaterial(){return this._renderer.compileAsync(this._fsQuad._mesh)}setCamera(e){let{material:t}=this;t.cameraWorldMatrix.copy(e.matrixWorld),t.invProjectionMatrix.copy(e.projectionMatrixInverse),t.physicalCamera.updateFrom(e);let n=0;e.projectionMatrix.elements[15]>0&&(n=1),e.isEquirectCamera&&(n=2),t.setDefine(`CAMERA_TYPE`,n),this.camera=e}setSize(e,t){e=Math.ceil(e),t=Math.ceil(t),(this._primaryTarget.width!==e||this._primaryTarget.height!==t)&&(this._primaryTarget.setSize(e,t),this._blendTargets[0].setSize(e,t),this._blendTargets[1].setSize(e,t),this.reset())}getSize(e){e.x=this._primaryTarget.width,e.y=this._primaryTarget.height}dispose(){this._primaryTarget.dispose(),this._blendTargets[0].dispose(),this._blendTargets[1].dispose(),this._sobolTarget.dispose(),this._fsQuad.dispose(),this._blendQuad.dispose(),this._task=null}reset(){let{_renderer:e,_primaryTarget:t,_blendTargets:n}=this,r=e.getRenderTarget(),i=e.getClearAlpha();e.getClearColor(co),e.setRenderTarget(t),e.setClearColor(0,0),e.clearColor(),e.setRenderTarget(n[0]),e.setClearColor(0,0),e.clearColor(),e.setRenderTarget(n[1]),e.setClearColor(0,0),e.clearColor(),e.setClearColor(co,i),e.setRenderTarget(r),this.samples=0,this._task=null,this.material.stratifiedTexture.stableNoise=this.stableNoise,this.stableNoise&&(this.material.seed=0,this.material.stratifiedTexture.reset())}update(){this.material.onBeforeRender(),!this.isCompiling&&(this._task||=so.call(this),this._task.next())}},uo=new T,fo=new T,po=new _e,mo=new Be,ho=class extends ze{constructor(t=512,n=512){super(new Float32Array(t*n*4),t,n,d,b,303,e,w,m,m),this.generationCallback=null}update(){this.dispose(),this.needsUpdate=!0;let{data:e,width:t,height:n}=this.image;for(let r=0;r<t;r++)for(let i=0;i<n;i++){fo.set(t,n),uo.set(r/t,i/n),uo.x-=.5,uo.y=1-uo.y,po.theta=uo.x*2*Math.PI,po.phi=uo.y*Math.PI,po.radius=1,this.generationCallback(po,uo,fo,mo);let a=4*(i*t+r);e[a+0]=mo.r,e[a+1]=mo.g,e[a+2]=mo.b,e[a+3]=1}}copy(e){return super.copy(e),this.generationCallback=e.generationCallback,this}},go=new E,_o=class extends ho{constructor(e=512){super(e,e),this.topColor=new Be().set(16777215),this.bottomColor=new Be().set(0),this.exponent=2,this.generationCallback=(e,t,n,r)=>{go.setFromSpherical(e);let i=go.y*.5+.5;r.lerpColors(this.bottomColor,this.topColor,i**this.exponent)}}copy(e){return super.copy(e),this.topColor.copy(e.topColor),this.bottomColor.copy(e.bottomColor),this}},vo=class extends Fe{get map(){return this.uniforms.map.value}set map(e){this.uniforms.map.value=e}get opacity(){return this.uniforms.opacity.value}set opacity(e){this.uniforms&&(this.uniforms.opacity.value=e)}constructor(e){super({uniforms:{map:{value:null},opacity:{value:1}},vertexShader:`
3898
+ `}),this.setValues(e)}};function*vo(){let{_renderer:e,_fsQuad:t,_blendQuad:n,_primaryTarget:r,_blendTargets:i,_sobolTarget:a,_subframe:o,alpha:s,material:c}=this,l=new S,u=new S,d=n.material,[f,p]=i;for(;;){s?(d.opacity=this._opacityFactor/(this.samples+1),c.blending=0,c.opacity=1):(c.opacity=this._opacityFactor/(this.samples+1),c.blending=1);let[i,m,h,g]=o,_=r.width,v=r.height;c.resolution.set(_*h,v*g),c.sobolTexture=a.texture,c.stratifiedTexture.init(20,c.bounces+c.transmissiveBounces+5),c.stratifiedTexture.next(),c.seed++;let y=this.tiles.x||1,b=this.tiles.y||1,x=y*b,S=Math.ceil(_*h),C=Math.ceil(v*g),ee=Math.floor(i*_),w=Math.floor(m*v),te=Math.ceil(S/y),ne=Math.ceil(C/b);for(let i=0;i<b;i++)for(let a=0;a<y;a++){let o=e.getRenderTarget(),c=e.autoClear,m=e.getScissorTest();e.getScissor(l),e.getViewport(u);let h=a,g=i;if(!this.stableTiles){let e=this._currentTile%(y*b);h=e%y,g=~~(e/y),this._currentTile=e+1}let _=b-g-1;r.scissor.set(ee+h*te,w+_*ne,Math.min(te,S-h*te),Math.min(ne,C-_*ne)),r.viewport.set(ee,w,S,C),e.setRenderTarget(r),e.setScissorTest(!0),e.autoClear=!1,t.render(e),e.setViewport(u),e.setScissor(l),e.setScissorTest(m),e.setRenderTarget(o),e.autoClear=c,s&&(d.target1=f.texture,d.target2=r.texture,e.setRenderTarget(p),n.render(e),e.setRenderTarget(o)),this.samples+=1/x,a===y-1&&i===b-1&&(this.samples=Math.round(this.samples)),yield}[f,p]=[p,f]}}var yo=new t,bo=class{get material(){return this._fsQuad.material}set material(e){this._fsQuad.material.removeEventListener(`recompilation`,this._compileFunction),e.addEventListener(`recompilation`,this._compileFunction),this._fsQuad.material=e}get target(){return this._alpha?this._blendTargets[1]:this._primaryTarget}set alpha(e){this._alpha!==e&&(e||(this._blendTargets[0].dispose(),this._blendTargets[1].dispose()),this._alpha=e,this.reset())}get alpha(){return this._alpha}get isCompiling(){return!!this._compilePromise}constructor(e){this.camera=null,this.tiles=new E(3,3),this.stableNoise=!1,this.stableTiles=!0,this.samples=0,this._subframe=new S(0,0,1,1),this._opacityFactor=1,this._renderer=e,this._alpha=!1,this._fsQuad=new He(new _o),this._blendQuad=new He(new zi),this._task=null,this._currentTile=0,this._compilePromise=null,this._sobolTarget=new Ki().generate(e),this._primaryTarget=new Ge(1,1,{format:D,type:d,magFilter:T,minFilter:T}),this._blendTargets=[new Ge(1,1,{format:D,type:d,magFilter:T,minFilter:T}),new Ge(1,1,{format:D,type:d,magFilter:T,minFilter:T})],this._compileFunction=()=>{let e=this.compileMaterial(this._fsQuad._mesh);e.then(()=>{this._compilePromise===e&&(this._compilePromise=null)}),this._compilePromise=e},this.material.addEventListener(`recompilation`,this._compileFunction)}compileMaterial(){return this._renderer.compileAsync(this._fsQuad._mesh)}setCamera(e){let{material:t}=this;t.cameraWorldMatrix.copy(e.matrixWorld),t.invProjectionMatrix.copy(e.projectionMatrixInverse),t.physicalCamera.updateFrom(e);let n=0;e.projectionMatrix.elements[15]>0&&(n=1),e.isEquirectCamera&&(n=2),t.setDefine(`CAMERA_TYPE`,n),this.camera=e}setSize(e,t){e=Math.ceil(e),t=Math.ceil(t),(this._primaryTarget.width!==e||this._primaryTarget.height!==t)&&(this._primaryTarget.setSize(e,t),this._blendTargets[0].setSize(e,t),this._blendTargets[1].setSize(e,t),this.reset())}getSize(e){e.x=this._primaryTarget.width,e.y=this._primaryTarget.height}dispose(){this._primaryTarget.dispose(),this._blendTargets[0].dispose(),this._blendTargets[1].dispose(),this._sobolTarget.dispose(),this._fsQuad.dispose(),this._blendQuad.dispose(),this._task=null}reset(){let{_renderer:e,_primaryTarget:t,_blendTargets:n}=this,r=e.getRenderTarget(),i=e.getClearAlpha();e.getClearColor(yo),e.setRenderTarget(t),e.setClearColor(0,0),e.clearColor(),e.setRenderTarget(n[0]),e.setClearColor(0,0),e.clearColor(),e.setRenderTarget(n[1]),e.setClearColor(0,0),e.clearColor(),e.setClearColor(yo,i),e.setRenderTarget(r),this.samples=0,this._task=null,this.material.stratifiedTexture.stableNoise=this.stableNoise,this.stableNoise&&(this.material.seed=0,this.material.stratifiedTexture.reset())}update(){this.material.onBeforeRender(),!this.isCompiling&&(this._task||=vo.call(this),this._task.next())}},xo=new E,So=new E,Co=new Oe,wo=new t,To=class extends h{constructor(e=512,t=512){super(new Uint16Array(e*t*4),e,t,D,Ye,303,Ee,_,w,w),this.generationCallback=null}update(){this.dispose(),this.needsUpdate=!0;let{data:e,width:t,height:n}=this.image;for(let r=0;r<t;r++)for(let i=0;i<n;i++){So.set(t,n),xo.set(r/t,i/n),xo.x-=.5,xo.y=1-xo.y,Co.theta=xo.x*2*Math.PI,Co.phi=xo.y*Math.PI,Co.radius=1,this.generationCallback(Co,xo,So,wo);let a=4*(i*t+r);e[a+0]=v.toHalfFloat(wo.r),e[a+1]=v.toHalfFloat(wo.g),e[a+2]=v.toHalfFloat(wo.b),e[a+3]=v.toHalfFloat(1)}}copy(e){return super.copy(e),this.generationCallback=e.generationCallback,this}},Eo=new k,Do=class extends To{constructor(e=512){super(e,e),this.topColor=new t().set(16777215),this.bottomColor=new t().set(0),this.exponent=2,this.generationCallback=(e,t,n,r)=>{Eo.setFromSpherical(e);let i=Eo.y*.5+.5;r.lerpColors(this.bottomColor,this.topColor,i**this.exponent)}}copy(e){return super.copy(e),this.topColor.copy(e.topColor),this.bottomColor.copy(e.bottomColor),this}},Oo=class extends pe{get map(){return this.uniforms.map.value}set map(e){this.uniforms.map.value=e}get opacity(){return this.uniforms.opacity.value}set opacity(e){this.uniforms&&(this.uniforms.opacity.value=e)}constructor(e){super({uniforms:{map:{value:null},opacity:{value:1}},vertexShader:`
3900
3899
  varying vec2 vUv;
3901
3900
  void main() {
3902
3901
 
@@ -3953,7 +3952,7 @@ bool bvhIntersectFogVolumeHit(
3953
3952
  #include <premultiplied_alpha_fragment>
3954
3953
 
3955
3954
  }
3956
- `}),this.setValues(e)}},yo=class extends Fe{constructor(){super({uniforms:{envMap:{value:null},flipEnvMap:{value:-1}},vertexShader:`
3955
+ `}),this.setValues(e)}},ko=class extends pe{constructor(){super({uniforms:{envMap:{value:null},flipEnvMap:{value:-1}},vertexShader:`
3957
3956
  varying vec2 vUv;
3958
3957
  void main() {
3959
3958
 
@@ -3970,7 +3969,7 @@ bool bvhIntersectFogVolumeHit(
3970
3969
  #include <common>
3971
3970
  #include <cube_uv_reflection_fragment>
3972
3971
 
3973
- ${Ga}
3972
+ ${to}
3974
3973
 
3975
3974
  void main() {
3976
3975
 
@@ -3978,11 +3977,124 @@ bool bvhIntersectFogVolumeHit(
3978
3977
  rayDirection.x *= flipEnvMap;
3979
3978
  gl_FragColor = textureCube( envMap, rayDirection );
3980
3979
 
3981
- }`}),this.depthWrite=!1,this.depthTest=!1}},bo=class{constructor(e){this._renderer=e,this._quad=new oe(new yo)}generate(t,n=null,i=null){if(!t.isCubeTexture)throw Error(`CubeToEquirectMaterial: Source can only be cube textures.`);let a=t.images[0],o=this._renderer,s=this._quad;n===null&&(n=4*a.height),i===null&&(i=2*a.height);let c=new Oe(n,i,{type:b,colorSpace:a.colorSpace}),l=a.height,u=Math.log2(l)-2,f=1/l,p=1/(3*Math.max(2**u,112));s.material.defines.CUBEUV_MAX_MIP=`${u}.0`,s.material.defines.CUBEUV_TEXEL_WIDTH=p,s.material.defines.CUBEUV_TEXEL_HEIGHT=f,s.material.uniforms.envMap.value=t,s.material.uniforms.flipEnvMap.value=t.isRenderTargetTexture?1:-1,s.material.needsUpdate=!0;let h=o.getRenderTarget(),_=o.autoClear;o.autoClear=!0,o.setRenderTarget(c),s.render(o),o.setRenderTarget(h),o.autoClear=_;let v=new Uint16Array(n*i*4),y=new Float32Array(n*i*4);o.readRenderTargetPixels(c,0,0,n,i,y),c.dispose();for(let e=0,t=y.length;e<t;e++)v[e]=r.toHalfFloat(y[e]);let x=new ze(v,n,i,d,ye);return x.minFilter=g,x.magFilter=m,x.wrapS=e,x.wrapT=e,x.mapping=303,x.needsUpdate=!0,x}dispose(){this._quad.dispose()}};function xo(e){return e.extensions.get(`EXT_float_blend`)}var So=new T,Co=class{get multipleImportanceSampling(){return!!this._pathTracer.material.defines.FEATURE_MIS}set multipleImportanceSampling(e){this._pathTracer.material.setDefine(`FEATURE_MIS`,+!!e)}get transmissiveBounces(){return this._pathTracer.material.transmissiveBounces}set transmissiveBounces(e){this._pathTracer.material.transmissiveBounces=e}get bounces(){return this._pathTracer.material.bounces}set bounces(e){this._pathTracer.material.bounces=e}get filterGlossyFactor(){return this._pathTracer.material.filterGlossyFactor}set filterGlossyFactor(e){this._pathTracer.material.filterGlossyFactor=e}get samples(){return this._pathTracer.samples}get target(){return this._pathTracer.target}get tiles(){return this._pathTracer.tiles}get stableNoise(){return this._pathTracer.stableNoise}set stableNoise(e){this._pathTracer.stableNoise=e}get isCompiling(){return!!this._pathTracer.isCompiling}constructor(e){this._renderer=e,this._generator=new Oi,this._pathTracer=new lo(e),this._queueReset=!1,this._clock=new Le,this._compilePromise=null,this._lowResPathTracer=new lo(e),this._lowResPathTracer.tiles.set(1,1),this._quad=new oe(new vo({map:null,transparent:!0,blending:0,premultipliedAlpha:e.getContextAttributes().premultipliedAlpha})),this._materials=null,this._previousEnvironment=null,this._previousBackground=null,this._internalBackground=null,this.renderDelay=100,this.minSamples=5,this.fadeDuration=500,this.enablePathTracing=!0,this.pausePathTracing=!1,this.dynamicLowRes=!1,this.lowResScale=.25,this.renderScale=1,this.synchronizeRenderSize=!0,this.rasterizeScene=!0,this.renderToCanvas=!0,this.textureSize=new T(1024,1024),this.rasterizeSceneCallback=(e,t)=>{this._renderer.render(e,t)},this.renderToCanvasCallback=(e,t,n)=>{let r=t.autoClear;t.autoClear=!1,n.render(t),t.autoClear=r},this.setScene(new ue,new c)}setBVHWorker(e){this._generator.setBVHWorker(e)}setScene(e,t,n={}){e.updateMatrixWorld(!0),t.updateMatrixWorld();let r=this._generator;if(r.setObjects(e),this._buildAsync)return r.generateAsync(n.onProgress).then(n=>this._updateFromResults(e,t,n));{let n=r.generate();return this._updateFromResults(e,t,n)}}setSceneAsync(...e){this._buildAsync=!0;let t=this.setScene(...e);return this._buildAsync=!1,t}setCamera(e){this.camera=e,this.updateCamera()}updateCamera(){let e=this.camera;e.updateMatrixWorld(),this._pathTracer.setCamera(e),this._lowResPathTracer.setCamera(e),this.reset()}updateMaterials(){let e=this._pathTracer.material,t=this._renderer,n=this._materials,r=this.textureSize,i=da(n);e.textures.setTextures(t,i,r.x,r.y),e.materials.updateFrom(n,i),this.reset()}updateLights(){let e=this.scene,t=this._renderer,n=this._pathTracer.material,r=fa(e),i=ua(r);n.lights.updateFrom(r,i),n.iesProfiles.setTextures(t,i),this.reset()}updateEnvironment(){let e=this.scene,t=this._pathTracer.material;if(this._internalBackground&&=(this._internalBackground.dispose(),null),t.backgroundBlur=e.backgroundBlurriness,t.backgroundIntensity=e.backgroundIntensity??1,t.backgroundRotation.makeRotationFromEuler(e.backgroundRotation).invert(),e.background===null)t.backgroundMap=null,t.backgroundAlpha=0;else if(e.background.isColor){this._colorBackground=this._colorBackground||new _o(16);let n=this._colorBackground;n.topColor.equals(e.background)||(n.topColor.set(e.background),n.bottomColor.set(e.background),n.update()),t.backgroundMap=n,t.backgroundAlpha=1}else if(e.background.isCubeTexture){if(e.background!==this._previousBackground){let n=new bo(this._renderer).generate(e.background);this._internalBackground=n,t.backgroundMap=n,t.backgroundAlpha=1}}else t.backgroundMap=e.background,t.backgroundAlpha=1;if(t.environmentIntensity=e.environment===null?0:e.environmentIntensity??1,t.environmentRotation.makeRotationFromEuler(e.environmentRotation).invert(),this._previousEnvironment!==e.environment&&e.environment!==null){if(e.environment.isCubeTexture){let n=new bo(this._renderer).generate(e.environment);t.envMapInfo.updateFrom(n)}else t.envMapInfo.updateFrom(e.environment)}this._previousEnvironment=e.environment,this._previousBackground=e.background,this.reset()}_updateFromResults(e,t,n){let{materials:r,geometry:i,bvh:a,bvhChanged:o,needsMaterialIndexUpdate:s}=n;this._materials=r;let c=this._pathTracer.material;return o&&(c.bvh.updateFrom(a),c.attributesArray.updateFrom(i.attributes.normal,i.attributes.tangent,i.attributes.uv,i.attributes.color)),s&&c.materialIndexAttribute.updateFrom(i.attributes.materialIndex),this._previousScene=e,this.scene=e,this.camera=t,this.updateCamera(),this.updateMaterials(),this.updateEnvironment(),this.updateLights(),n}renderSample(){let e=this._lowResPathTracer,t=this._pathTracer,n=this._renderer,r=this._clock,i=this._quad;this._updateScale(),this._queueReset&&(t.reset(),e.reset(),this._queueReset=!1,i.material.opacity=0,r.start());let a=r.getDelta()*1e3,o=r.getElapsedTime()*1e3;if(!this.pausePathTracing&&this.enablePathTracing&&this.renderDelay<=o&&!this.isCompiling&&t.update(),t.alpha=t.material.backgroundAlpha!==1||!xo(n),e.alpha=t.alpha,this.renderToCanvas){let n=this._renderer,r=this.minSamples;if(o>=this.renderDelay&&this.samples>=this.minSamples&&(this.fadeDuration===0?i.material.opacity=1:i.material.opacity=Math.min(i.material.opacity+a/this.fadeDuration,1)),!this.enablePathTracing||this.samples<r||i.material.opacity<1){if(this.dynamicLowRes&&!this.isCompiling){e.samples<1&&(e.material=t.material,e.update());let r=i.material.opacity;i.material.opacity=1-i.material.opacity,i.material.map=e.target.texture,i.render(n),i.material.opacity=r}(!this.dynamicLowRes&&this.rasterizeScene||this.dynamicLowRes&&this.isCompiling)&&this.rasterizeSceneCallback(this.scene,this.camera)}this.enablePathTracing&&i.material.opacity>0&&(i.material.opacity<1&&(i.material.blending=this.dynamicLowRes?2:1),i.material.map=t.target.texture,this.renderToCanvasCallback(t.target,n,i),i.material.blending=0)}}reset(){this._queueReset=!0,this._pathTracer.samples=0}dispose(){this._quad.dispose(),this._quad.material.dispose(),this._pathTracer.dispose()}_updateScale(){if(this.synchronizeRenderSize){this._renderer.getDrawingBufferSize(So);let e=Math.floor(this.renderScale*So.x),t=Math.floor(this.renderScale*So.y);if(this._pathTracer.getSize(So),So.x!==e||So.y!==t){let n=this.lowResScale;this._pathTracer.setSize(e,t),this._lowResPathTracer.setSize(Math.floor(e*n),Math.floor(t*n))}}}},wo=class extends y{constructor(e){super(e),this.type=ye}parse(e){let t=function(e,t){switch(e){case 1:throw Error(`THREE.HDRLoader: Read Error: `+(t||``));case 2:throw Error(`THREE.HDRLoader: Write Error: `+(t||``));case 3:throw Error(`THREE.HDRLoader: Bad File Format: `+(t||``));default:case 4:throw Error(`THREE.HDRLoader: Memory Error: `+(t||``))}},n=function(e,t,n){t||=1024;let r=e.pos,i=-1,a=0,o=``,s=String.fromCharCode.apply(null,new Uint16Array(e.subarray(r,r+128)));for(;0>(i=s.indexOf(`
3982
- `))&&a<t&&r<e.byteLength;)o+=s,a+=s.length,r+=128,s=String.fromCharCode.apply(null,new Uint16Array(e.subarray(r,r+128)));return-1<i&&(!1!==n&&(e.pos+=a+i+1),o+s.slice(0,i))},i=function(e){let r=/^#\?(\S+)/,i=/^\s*GAMMA\s*=\s*(\d+(\.\d+)?)\s*$/,a=/^\s*EXPOSURE\s*=\s*(\d+(\.\d+)?)\s*$/,o=/^\s*FORMAT=(\S+)\s*$/,s=/^\s*\-Y\s+(\d+)\s+\+X\s+(\d+)\s*$/,c={valid:0,string:``,comments:``,programtype:`RGBE`,format:``,gamma:1,exposure:1,width:0,height:0},l,u;for((e.pos>=e.byteLength||!(l=n(e)))&&t(1,`no header found`),(u=l.match(r))||t(3,`bad initial token`),c.valid|=1,c.programtype=u[1],c.string+=l+`
3980
+ }`}),this.depthWrite=!1,this.depthTest=!1}},Ao=class{constructor(e){this._renderer=e,this._quad=new He(new ko)}generate(e,t=null,n=null){if(!e.isCubeTexture)throw Error(`CubeToEquirectMaterial: Source can only be cube textures.`);let r=e.images[0],i=this._renderer,a=this._quad;t===null&&(t=4*r.height),n===null&&(n=2*r.height);let o=new Ge(t,n,{type:d,colorSpace:r.colorSpace}),s=r.height,c=Math.log2(s)-2,l=1/s,u=1/(3*Math.max(2**c,112));a.material.defines.CUBEUV_MAX_MIP=`${c}.0`,a.material.defines.CUBEUV_TEXEL_WIDTH=u,a.material.defines.CUBEUV_TEXEL_HEIGHT=l,a.material.uniforms.envMap.value=e,a.material.uniforms.flipEnvMap.value=e.isRenderTargetTexture?1:-1,a.material.needsUpdate=!0;let f=i.getRenderTarget(),p=i.autoClear;i.autoClear=!0,i.setRenderTarget(o),a.render(i),i.setRenderTarget(f),i.autoClear=p;let m=new Uint16Array(t*n*4),g=new Float32Array(t*n*4);i.readRenderTargetPixels(o,0,0,t,n,g),o.dispose();for(let e=0,t=g.length;e<t;e++)m[e]=v.toHalfFloat(g[e]);let _=new h(m,t,n,D,Ye);return _.minFilter=ne,_.magFilter=w,_.wrapS=Ee,_.wrapT=Ee,_.mapping=303,_.needsUpdate=!0,_}dispose(){this._quad.dispose()}};function jo(e){return e.extensions.get(`EXT_float_blend`)}var Mo=new E,No=class{get multipleImportanceSampling(){return!!this._pathTracer.material.defines.FEATURE_MIS}set multipleImportanceSampling(e){this._pathTracer.material.setDefine(`FEATURE_MIS`,+!!e)}get transmissiveBounces(){return this._pathTracer.material.transmissiveBounces}set transmissiveBounces(e){this._pathTracer.material.transmissiveBounces=e}get bounces(){return this._pathTracer.material.bounces}set bounces(e){this._pathTracer.material.bounces=e}get filterGlossyFactor(){return this._pathTracer.material.filterGlossyFactor}set filterGlossyFactor(e){this._pathTracer.material.filterGlossyFactor=e}get samples(){return this._pathTracer.samples}get target(){return this._pathTracer.target}get tiles(){return this._pathTracer.tiles}get stableNoise(){return this._pathTracer.stableNoise}set stableNoise(e){this._pathTracer.stableNoise=e}get isCompiling(){return!!this._pathTracer.isCompiling}constructor(e){console.warn(`WebGLPathTracer: This class has been deprecated and will be removed in a future release. Use "WebGPUPathTracer" instead.`),this._renderer=e,this._generator=new Li,this._pathTracer=new bo(e),this._queueReset=!1,this._clock=new we,this._compilePromise=null,this._lowResPathTracer=new bo(e),this._lowResPathTracer.tiles.set(1,1),this._quad=new He(new Oo({map:null,transparent:!0,blending:0,premultipliedAlpha:e.getContextAttributes().premultipliedAlpha})),this._materials=null,this._previousEnvironment=null,this._previousBackground=null,this._internalBackground=null,this.renderDelay=100,this.minSamples=5,this.fadeDuration=500,this.enablePathTracing=!0,this.pausePathTracing=!1,this.dynamicLowRes=!1,this.lowResScale=.25,this.renderScale=1,this.synchronizeRenderSize=!0,this.rasterizeScene=!0,this.renderToCanvas=!0,this.textureSize=new E(1024,1024),this.rasterizeSceneCallback=(e,t)=>{this._renderer.render(e,t)},this.renderToCanvasCallback=(e,t,n)=>{let r=t.autoClear;t.autoClear=!1,n.render(t),t.autoClear=r},this.setScene(new Ve,new y)}setBVHWorker(e){this._generator.setBVHWorker(e)}setScene(e,t,n={}){e.updateMatrixWorld(!0),t.updateMatrixWorld();let r=this._generator;if(r.setObjects(e),this._buildAsync)return r.generateAsync(n.onProgress).then(n=>this._updateFromResults(e,t,n));{let n=r.generate();return this._updateFromResults(e,t,n)}}setSceneAsync(...e){this._buildAsync=!0;let t=this.setScene(...e);return this._buildAsync=!1,t}setCamera(e){this.camera=e,this.updateCamera()}updateCamera(){let e=this.camera;e.updateMatrixWorld(),this._pathTracer.setCamera(e),this._lowResPathTracer.setCamera(e),this.reset()}updateMaterials(){let e=this._pathTracer.material,t=this._renderer,n=this._materials,r=this.textureSize,i=xa(n);e.textures.setTextures(t,i,r.x,r.y),e.materials.updateFrom(n,i),this.reset()}updateLights(){let e=this.scene,t=this._renderer,n=this._pathTracer.material,r=Sa(e),i=ba(r);n.lights.updateFrom(r,i),n.iesProfiles.setTextures(t,i),this.reset()}updateEnvironment(){let e=this.scene,t=this._pathTracer.material;if(this._internalBackground&&=(this._internalBackground.dispose(),null),t.backgroundBlur=e.backgroundBlurriness,t.backgroundIntensity=e.backgroundIntensity??1,t.backgroundRotation.makeRotationFromEuler(e.backgroundRotation).invert(),e.background===null)t.backgroundMap=null,t.backgroundAlpha=0;else if(e.background.isColor){this._colorBackground=this._colorBackground||new Do(16);let n=this._colorBackground;n.topColor.equals(e.background)||(n.topColor.set(e.background),n.bottomColor.set(e.background),n.update()),t.backgroundMap=n,t.backgroundAlpha=1}else if(e.background.isCubeTexture){if(e.background!==this._previousBackground){let n=new Ao(this._renderer).generate(e.background);this._internalBackground=n,t.backgroundMap=n,t.backgroundAlpha=1}}else t.backgroundMap=e.background,t.backgroundAlpha=1;if(t.environmentIntensity=e.environment===null?0:e.environmentIntensity??1,t.environmentRotation.makeRotationFromEuler(e.environmentRotation).invert(),this._previousEnvironment!==e.environment&&e.environment!==null){if(e.environment.isCubeTexture){let n=new Ao(this._renderer).generate(e.environment);t.envMapInfo.updateFrom(n)}else t.envMapInfo.updateFrom(e.environment)}this._previousEnvironment=e.environment,this._previousBackground=e.background,this.reset()}_updateFromResults(e,t,n){let{materials:r,geometry:i,bvh:a,bvhChanged:o,needsMaterialIndexUpdate:s}=n;this._materials=r;let c=this._pathTracer.material;return o&&(c.bvh.updateFrom(a),c.attributesArray.updateFrom(i.attributes.normal,i.attributes.tangent,i.attributes.uv,i.attributes.color)),s&&c.materialIndexAttribute.updateFrom(i.attributes.materialIndex),this._previousScene=e,this.scene=e,this.camera=t,this.updateCamera(),this.updateMaterials(),this.updateEnvironment(),this.updateLights(),n}renderSample(){let e=this._lowResPathTracer,t=this._pathTracer,n=this._renderer,r=this._clock,i=this._quad;this._updateScale(),this._queueReset&&(t.reset(),e.reset(),this._queueReset=!1,i.material.opacity=0,r.start());let a=r.getDelta()*1e3,o=r.getElapsedTime()*1e3;if(!this.pausePathTracing&&this.enablePathTracing&&this.renderDelay<=o&&!this.isCompiling&&t.update(),t.alpha=t.material.backgroundAlpha!==1||!jo(n),e.alpha=t.alpha,this.renderToCanvas){let n=this._renderer,r=this.minSamples;if(o>=this.renderDelay&&this.samples>=this.minSamples&&(this.fadeDuration===0?i.material.opacity=1:i.material.opacity=Math.min(i.material.opacity+a/this.fadeDuration,1)),!this.enablePathTracing||this.samples<r||i.material.opacity<1){if(this.dynamicLowRes&&!this.isCompiling){e.samples<1&&(e.material=t.material,e.update());let r=i.material.opacity;i.material.opacity=1-i.material.opacity,i.material.map=e.target.texture,i.render(n),i.material.opacity=r}(!this.dynamicLowRes&&this.rasterizeScene||this.dynamicLowRes&&this.isCompiling)&&this.rasterizeSceneCallback(this.scene,this.camera)}this.enablePathTracing&&i.material.opacity>0&&(i.material.opacity<1&&(i.material.blending=this.dynamicLowRes?2:1),i.material.map=t.target.texture,this.renderToCanvasCallback(t.target,n,i),i.material.blending=0)}}reset(){this._queueReset=!0,this._pathTracer.samples=0}dispose(){this._quad.dispose(),this._quad.material.dispose(),this._pathTracer.dispose()}_updateScale(){if(this.synchronizeRenderSize){this._renderer.getDrawingBufferSize(Mo);let e=Math.floor(this.renderScale*Mo.x),t=Math.floor(this.renderScale*Mo.y);if(this._pathTracer.getSize(Mo),Mo.x!==e||Mo.y!==t){let n=this.lowResScale;this._pathTracer.setSize(e,t),this._lowResPathTracer.setSize(Math.floor(e*n),Math.floor(t*n))}}}getRenderTime(){return this._clock.getElapsedTime()*1e3}},Po=class extends g{constructor(e){super(e),this.type=Ye}parse(e){let t=function(e,t){switch(e){case 1:throw Error(`THREE.HDRLoader: Read Error: `+(t||``));case 2:throw Error(`THREE.HDRLoader: Write Error: `+(t||``));case 3:throw Error(`THREE.HDRLoader: Bad File Format: `+(t||``));default:case 4:throw Error(`THREE.HDRLoader: Memory Error: `+(t||``))}},n=function(e,t,n){t||=1024;let r=e.pos,i=-1,a=0,o=``,s=String.fromCharCode.apply(null,new Uint16Array(e.subarray(r,r+128)));for(;0>(i=s.indexOf(`
3981
+ `))&&a<t&&r<e.byteLength;)o+=s,a+=s.length,r+=128,s=String.fromCharCode.apply(null,new Uint16Array(e.subarray(r,r+128)));return-1<i&&(!1!==n&&(e.pos+=a+i+1),o+s.slice(0,i))},r=function(e){let r=/^#\?(\S+)/,i=/^\s*GAMMA\s*=\s*(\d+(\.\d+)?)\s*$/,a=/^\s*EXPOSURE\s*=\s*(\d+(\.\d+)?)\s*$/,o=/^\s*FORMAT=(\S+)\s*$/,s=/^\s*\-Y\s+(\d+)\s+\+X\s+(\d+)\s*$/,c={valid:0,string:``,comments:``,programtype:`RGBE`,format:``,gamma:1,exposure:1,width:0,height:0},l,u;for((e.pos>=e.byteLength||!(l=n(e)))&&t(1,`no header found`),(u=l.match(r))||t(3,`bad initial token`),c.valid|=1,c.programtype=u[1],c.string+=l+`
3983
3982
  `;l=n(e),!1!==l;){if(c.string+=l+`
3984
3983
  `,l.charAt(0)===`#`){c.comments+=l+`
3985
- `;continue}if((u=l.match(i))&&(c.gamma=parseFloat(u[1])),(u=l.match(a))&&(c.exposure=parseFloat(u[1])),(u=l.match(o))&&(c.valid|=2,c.format=u[1]),(u=l.match(s))&&(c.valid|=4,c.height=parseInt(u[1],10),c.width=parseInt(u[2],10)),c.valid&2&&c.valid&4)break}return c.valid&2||t(3,`missing format specifier`),c.valid&4||t(3,`missing image size specifier`),c},a=function(e,n,r){let i=n;if(i<8||i>32767||e[0]!==2||e[1]!==2||e[2]&128)return new Uint8Array(e);i!==(e[2]<<8|e[3])&&t(3,`wrong scanline width`);let a=new Uint8Array(4*n*r);a.length||t(4,`unable to allocate buffer space`);let o=0,s=0,c=4*i,l=new Uint8Array(4),u=new Uint8Array(c),d=r;for(;d>0&&s<e.byteLength;){s+4>e.byteLength&&t(1),l[0]=e[s++],l[1]=e[s++],l[2]=e[s++],l[3]=e[s++],(l[0]!=2||l[1]!=2||(l[2]<<8|l[3])!=i)&&t(3,`bad rgbe scanline format`);let n=0,r;for(;n<c&&s<e.byteLength;){r=e[s++];let i=r>128;if(i&&(r-=128),(r===0||n+r>c)&&t(3,`bad scanline data`),i){let t=e[s++];for(let e=0;e<r;e++)u[n++]=t}else u.set(e.subarray(s,s+r),n),n+=r,s+=r}let f=i;for(let e=0;e<f;e++){let t=0;a[o]=u[e+t],t+=i,a[o+1]=u[e+t],t+=i,a[o+2]=u[e+t],t+=i,a[o+3]=u[e+t],o+=4}d--}return a},s=function(e,t,n,r){let i=2**(e[t+3]-128)/255;n[r+0]=e[t+0]*i,n[r+1]=e[t+1]*i,n[r+2]=e[t+2]*i,n[r+3]=1},c=function(e,t,n,i){let a=2**(e[t+3]-128)/255;n[i+0]=r.toHalfFloat(Math.min(e[t+0]*a,65504)),n[i+1]=r.toHalfFloat(Math.min(e[t+1]*a,65504)),n[i+2]=r.toHalfFloat(Math.min(e[t+2]*a,65504)),n[i+3]=r.toHalfFloat(1)},l=new Uint8Array(e);l.pos=0;let u=i(l),d=u.width,f=u.height,p=a(l.subarray(l.pos),d,f),h,g,_;switch(this.type){case b:_=p.length/4;let e=new Float32Array(_*4);for(let t=0;t<_;t++)s(p,t*4,e,t*4);h=e,g=b;break;case ye:_=p.length/4;let t=new Uint16Array(_*4);for(let e=0;e<_;e++)c(p,e*4,t,e*4);h=t,g=ye;break;default:throw Error(`THREE.HDRLoader: Unsupported type: `+this.type)}return{width:d,height:f,data:h,header:u.string,gamma:u.gamma,exposure:u.exposure,type:g,colorSpace:o,minFilter:m,magFilter:m,generateMipmaps:!1,flipY:!0}}setDataType(e){return this.type=e,this}},To=`// Global detail setting
3984
+ `;continue}if((u=l.match(i))&&(c.gamma=parseFloat(u[1])),(u=l.match(a))&&(c.exposure=parseFloat(u[1])),(u=l.match(o))&&(c.valid|=2,c.format=u[1]),(u=l.match(s))&&(c.valid|=4,c.height=parseInt(u[1],10),c.width=parseInt(u[2],10)),c.valid&2&&c.valid&4)break}return c.valid&2||t(3,`missing format specifier`),c.valid&4||t(3,`missing image size specifier`),c},i=function(e,n,r){let i=n;if(i<8||i>32767||e[0]!==2||e[1]!==2||e[2]&128)return new Uint8Array(e);i!==(e[2]<<8|e[3])&&t(3,`wrong scanline width`);let a=new Uint8Array(4*n*r);a.length||t(4,`unable to allocate buffer space`);let o=0,s=0,c=4*i,l=new Uint8Array(4),u=new Uint8Array(c),d=r;for(;d>0&&s<e.byteLength;){s+4>e.byteLength&&t(1),l[0]=e[s++],l[1]=e[s++],l[2]=e[s++],l[3]=e[s++],(l[0]!=2||l[1]!=2||(l[2]<<8|l[3])!=i)&&t(3,`bad rgbe scanline format`);let n=0,r;for(;n<c&&s<e.byteLength;){r=e[s++];let i=r>128;if(i&&(r-=128),(r===0||n+r>c)&&t(3,`bad scanline data`),i){let t=e[s++];for(let e=0;e<r;e++)u[n++]=t}else u.set(e.subarray(s,s+r),n),n+=r,s+=r}let f=i;for(let e=0;e<f;e++){let t=0;a[o]=u[e+t],t+=i,a[o+1]=u[e+t],t+=i,a[o+2]=u[e+t],t+=i,a[o+3]=u[e+t],o+=4}d--}return a},a=function(e,t,n,r){let i=2**(e[t+3]-128)/255;n[r+0]=e[t+0]*i,n[r+1]=e[t+1]*i,n[r+2]=e[t+2]*i,n[r+3]=1},o=function(e,t,n,r){let i=2**(e[t+3]-128)/255;n[r+0]=v.toHalfFloat(Math.min(e[t+0]*i,65504)),n[r+1]=v.toHalfFloat(Math.min(e[t+1]*i,65504)),n[r+2]=v.toHalfFloat(Math.min(e[t+2]*i,65504)),n[r+3]=v.toHalfFloat(1)},s=new Uint8Array(e);s.pos=0;let c=r(s),u=c.width,f=c.height,p=i(s.subarray(s.pos),u,f),m,h,g;switch(this.type){case d:g=p.length/4;let e=new Float32Array(g*4);for(let t=0;t<g;t++)a(p,t*4,e,t*4);m=e,h=d;break;case Ye:g=p.length/4;let t=new Uint16Array(g*4);for(let e=0;e<g;e++)o(p,e*4,t,e*4);m=t,h=Ye;break;default:throw Error(`THREE.HDRLoader: Unsupported type: `+this.type)}return{width:u,height:f,data:m,header:c.string,gamma:c.gamma,exposure:c.exposure,type:h,colorSpace:l,minFilter:w,magFilter:w,generateMipmaps:!1,flipY:!0}}setDataType(e){return this.type=e,this}},Fo={name:`LuminosityHighPassShader`,uniforms:{tDiffuse:{value:null},luminosityThreshold:{value:1},smoothWidth:{value:1},defaultColor:{value:new t(0)},defaultOpacity:{value:0}},vertexShader:`
3985
+
3986
+ varying vec2 vUv;
3987
+
3988
+ void main() {
3989
+
3990
+ vUv = uv;
3991
+
3992
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
3993
+
3994
+ }`,fragmentShader:`
3995
+
3996
+ uniform sampler2D tDiffuse;
3997
+ uniform vec3 defaultColor;
3998
+ uniform float defaultOpacity;
3999
+ uniform float luminosityThreshold;
4000
+ uniform float smoothWidth;
4001
+
4002
+ varying vec2 vUv;
4003
+
4004
+ void main() {
4005
+
4006
+ vec4 texel = texture2D( tDiffuse, vUv );
4007
+
4008
+ float v = luminance( texel.xyz );
4009
+
4010
+ vec4 outputColor = vec4( defaultColor.rgb, defaultOpacity );
4011
+
4012
+ float alpha = smoothstep( luminosityThreshold, luminosityThreshold + smoothWidth, v );
4013
+
4014
+ gl_FragColor = mix( outputColor, texel, alpha );
4015
+
4016
+ }`},Io=class e extends qe{constructor(e,n=1,r,i){super(),this.strength=n,this.radius=r,this.threshold=i,this.resolution=e===void 0?new E(256,256):new E(e.x,e.y),this.clearColor=new t(0,0,0),this.needsSwap=!1,this.renderTargetsHorizontal=[],this.renderTargetsVertical=[],this.nMips=5;let a=Math.round(this.resolution.x/2),o=Math.round(this.resolution.y/2);this.renderTargetBright=new Ge(a,o,{type:Ye,depthBuffer:!1}),this.renderTargetBright.texture.name=`UnrealBloomPass.bright`,this.renderTargetBright.texture.generateMipmaps=!1;for(let e=0;e<this.nMips;e++){let t=new Ge(a,o,{type:Ye,depthBuffer:!1});t.texture.name=`UnrealBloomPass.h`+e,t.texture.generateMipmaps=!1,this.renderTargetsHorizontal.push(t);let n=new Ge(a,o,{type:Ye,depthBuffer:!1});n.texture.name=`UnrealBloomPass.v`+e,n.texture.generateMipmaps=!1,this.renderTargetsVertical.push(n),a=Math.round(a/2),o=Math.round(o/2)}let s=Fo;this.highPassUniforms=ve.clone(s.uniforms),this.highPassUniforms.luminosityThreshold.value=i,this.highPassUniforms.smoothWidth.value=.01,this.materialHighPassFilter=new pe({uniforms:this.highPassUniforms,vertexShader:s.vertexShader,fragmentShader:s.fragmentShader}),this.separableBlurMaterials=[];let c=[6,10,14,18,22];a=Math.round(this.resolution.x/2),o=Math.round(this.resolution.y/2);for(let e=0;e<this.nMips;e++)this.separableBlurMaterials.push(this._getSeparableBlurMaterial(c[e])),this.separableBlurMaterials[e].uniforms.invSize.value=new E(1/a,1/o),a=Math.round(a/2),o=Math.round(o/2);this.compositeMaterial=this._getCompositeMaterial(this.nMips),this.compositeMaterial.uniforms.blurTexture1.value=this.renderTargetsVertical[0].texture,this.compositeMaterial.uniforms.blurTexture2.value=this.renderTargetsVertical[1].texture,this.compositeMaterial.uniforms.blurTexture3.value=this.renderTargetsVertical[2].texture,this.compositeMaterial.uniforms.blurTexture4.value=this.renderTargetsVertical[3].texture,this.compositeMaterial.uniforms.blurTexture5.value=this.renderTargetsVertical[4].texture,this.compositeMaterial.uniforms.bloomStrength.value=n,this.compositeMaterial.uniforms.bloomRadius.value=.1;let l=[1,.8,.6,.4,.2];this.compositeMaterial.uniforms.bloomFactors.value=l,this.bloomTintColors=[new k(1,1,1),new k(1,1,1),new k(1,1,1),new k(1,1,1),new k(1,1,1)],this.compositeMaterial.uniforms.bloomTintColors.value=this.bloomTintColors,this.copyUniforms=ve.clone(De.uniforms),this.blendMaterial=new pe({uniforms:this.copyUniforms,vertexShader:De.vertexShader,fragmentShader:De.fragmentShader,premultipliedAlpha:!0,blending:2,depthTest:!1,depthWrite:!1,transparent:!0}),this._oldClearColor=new t,this._oldClearAlpha=1,this._basic=new ie,this._fsQuad=new He(null)}dispose(){for(let e=0;e<this.renderTargetsHorizontal.length;e++)this.renderTargetsHorizontal[e].dispose();for(let e=0;e<this.renderTargetsVertical.length;e++)this.renderTargetsVertical[e].dispose();this.renderTargetBright.dispose();for(let e=0;e<this.separableBlurMaterials.length;e++)this.separableBlurMaterials[e].dispose();this.compositeMaterial.dispose(),this.blendMaterial.dispose(),this._basic.dispose(),this._fsQuad.dispose()}setSize(e,t){let n=Math.round(e/2),r=Math.round(t/2);this.renderTargetBright.setSize(n,r);for(let e=0;e<this.nMips;e++)this.renderTargetsHorizontal[e].setSize(n,r),this.renderTargetsVertical[e].setSize(n,r),this.separableBlurMaterials[e].uniforms.invSize.value=new E(1/n,1/r),n=Math.round(n/2),r=Math.round(r/2)}render(t,n,r,i,a){t.getClearColor(this._oldClearColor),this._oldClearAlpha=t.getClearAlpha();let o=t.autoClear;t.autoClear=!1,t.setClearColor(this.clearColor,0),a&&t.state.buffers.stencil.setTest(!1),this.renderToScreen&&(this._fsQuad.material=this._basic,this._basic.map=r.texture,t.setRenderTarget(null),t.clear(),this._fsQuad.render(t)),this.highPassUniforms.tDiffuse.value=r.texture,this.highPassUniforms.luminosityThreshold.value=this.threshold,this._fsQuad.material=this.materialHighPassFilter,t.setRenderTarget(this.renderTargetBright),t.clear(),this._fsQuad.render(t);let s=this.renderTargetBright;for(let n=0;n<this.nMips;n++)this._fsQuad.material=this.separableBlurMaterials[n],this.separableBlurMaterials[n].uniforms.colorTexture.value=s.texture,this.separableBlurMaterials[n].uniforms.direction.value=e.BlurDirectionX,t.setRenderTarget(this.renderTargetsHorizontal[n]),t.clear(),this._fsQuad.render(t),this.separableBlurMaterials[n].uniforms.colorTexture.value=this.renderTargetsHorizontal[n].texture,this.separableBlurMaterials[n].uniforms.direction.value=e.BlurDirectionY,t.setRenderTarget(this.renderTargetsVertical[n]),t.clear(),this._fsQuad.render(t),s=this.renderTargetsVertical[n];this._fsQuad.material=this.compositeMaterial,this.compositeMaterial.uniforms.bloomStrength.value=this.strength,this.compositeMaterial.uniforms.bloomRadius.value=this.radius,this.compositeMaterial.uniforms.bloomTintColors.value=this.bloomTintColors,t.setRenderTarget(this.renderTargetsHorizontal[0]),t.clear(),this._fsQuad.render(t),this._fsQuad.material=this.blendMaterial,this.copyUniforms.tDiffuse.value=this.renderTargetsHorizontal[0].texture,a&&t.state.buffers.stencil.setTest(!0),this.renderToScreen?(t.setRenderTarget(null),this._fsQuad.render(t)):(t.setRenderTarget(r),this._fsQuad.render(t)),t.setClearColor(this._oldClearColor,this._oldClearAlpha),t.autoClear=o}_getSeparableBlurMaterial(e){let t=[],n=e/3;for(let r=0;r<e;r++)t.push(.39894*Math.exp(-.5*r*r/(n*n))/n);let r=[],i=[];for(let n=1;n<e;n+=2){let a=t[n],o=n+1<e?t[n+1]:0,s=a+o;r.push((n*a+(n+1)*o)/s),i.push(s)}return new pe({defines:{KERNEL_PAIRS:r.length},uniforms:{colorTexture:{value:null},invSize:{value:new E(.5,.5)},direction:{value:new E(.5,.5)},centerWeight:{value:t[0]},gaussianOffsets:{value:r},gaussianWeights:{value:i}},vertexShader:`
4017
+
4018
+ varying vec2 vUv;
4019
+
4020
+ void main() {
4021
+
4022
+ vUv = uv;
4023
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
4024
+
4025
+ }`,fragmentShader:`
4026
+
4027
+ #include <common>
4028
+
4029
+ varying vec2 vUv;
4030
+
4031
+ uniform sampler2D colorTexture;
4032
+ uniform vec2 invSize;
4033
+ uniform vec2 direction;
4034
+ uniform float centerWeight;
4035
+ uniform float gaussianOffsets[KERNEL_PAIRS];
4036
+ uniform float gaussianWeights[KERNEL_PAIRS];
4037
+
4038
+ void main() {
4039
+
4040
+ vec3 diffuseSum = texture2D( colorTexture, vUv ).rgb * centerWeight;
4041
+
4042
+ for ( int i = 0; i < KERNEL_PAIRS; i ++ ) {
4043
+
4044
+ vec2 uvOffset = direction * invSize * gaussianOffsets[ i ];
4045
+ vec3 sample1 = texture2D( colorTexture, vUv + uvOffset ).rgb;
4046
+ vec3 sample2 = texture2D( colorTexture, vUv - uvOffset ).rgb;
4047
+ diffuseSum += ( sample1 + sample2 ) * gaussianWeights[ i ];
4048
+
4049
+ }
4050
+
4051
+ gl_FragColor = vec4( diffuseSum, 1.0 );
4052
+
4053
+ }`})}_getCompositeMaterial(e){return new pe({defines:{NUM_MIPS:e},uniforms:{blurTexture1:{value:null},blurTexture2:{value:null},blurTexture3:{value:null},blurTexture4:{value:null},blurTexture5:{value:null},bloomStrength:{value:1},bloomFactors:{value:null},bloomTintColors:{value:null},bloomRadius:{value:0}},vertexShader:`
4054
+
4055
+ varying vec2 vUv;
4056
+
4057
+ void main() {
4058
+
4059
+ vUv = uv;
4060
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
4061
+
4062
+ }`,fragmentShader:`
4063
+
4064
+ varying vec2 vUv;
4065
+
4066
+ uniform sampler2D blurTexture1;
4067
+ uniform sampler2D blurTexture2;
4068
+ uniform sampler2D blurTexture3;
4069
+ uniform sampler2D blurTexture4;
4070
+ uniform sampler2D blurTexture5;
4071
+ uniform float bloomStrength;
4072
+ uniform float bloomRadius;
4073
+ uniform float bloomFactors[NUM_MIPS];
4074
+ uniform vec3 bloomTintColors[NUM_MIPS];
4075
+
4076
+ float lerpBloomFactor( const in float factor ) {
4077
+
4078
+ float mirrorFactor = 1.2 - factor;
4079
+ return mix( factor, mirrorFactor, bloomRadius );
4080
+
4081
+ }
4082
+
4083
+ void main() {
4084
+
4085
+ // 3.0 for backwards compatibility with previous alpha-based intensity
4086
+ vec3 bloom = 3.0 * bloomStrength * (
4087
+ lerpBloomFactor( bloomFactors[ 0 ] ) * bloomTintColors[ 0 ] * texture2D( blurTexture1, vUv ).rgb +
4088
+ lerpBloomFactor( bloomFactors[ 1 ] ) * bloomTintColors[ 1 ] * texture2D( blurTexture2, vUv ).rgb +
4089
+ lerpBloomFactor( bloomFactors[ 2 ] ) * bloomTintColors[ 2 ] * texture2D( blurTexture3, vUv ).rgb +
4090
+ lerpBloomFactor( bloomFactors[ 3 ] ) * bloomTintColors[ 3 ] * texture2D( blurTexture4, vUv ).rgb +
4091
+ lerpBloomFactor( bloomFactors[ 4 ] ) * bloomTintColors[ 4 ] * texture2D( blurTexture5, vUv ).rgb
4092
+ );
4093
+
4094
+ float bloomAlpha = max( bloom.r, max( bloom.g, bloom.b ) );
4095
+ gl_FragColor = vec4( bloom, bloomAlpha );
4096
+
4097
+ }`})}};Io.BlurDirectionX=new E(1,0),Io.BlurDirectionY=new E(0,1);var Lo=`// Global detail setting
3986
4098
  $fn = 64;
3987
4099
 
3988
4100
  // Animation Data Dictionary
@@ -4108,7 +4220,7 @@ armature(animations=anim_data) {
4108
4220
  }
4109
4221
  }
4110
4222
  }
4111
- `,Eo=document.getElementById(`prompt-desc`),Do=document.getElementById(`copy-prompt-btn`),Oo=document.getElementById(`model-name-input`),R=document.getElementById(`editor`),ko=document.getElementById(`render-btn`),Ao=document.getElementById(`load-scad-btn`),jo=document.getElementById(`download-scad-btn`),Mo=document.getElementById(`export-gltf-btn`),No=document.getElementById(`capture-image-btn`),Po=document.getElementById(`share-btn`),Fo=document.getElementById(`auto-render-cb`),Io=document.getElementById(`minify-share-cb`),z=document.getElementById(`viewer`),Lo=document.getElementById(`right-panel`),Ro=document.getElementById(`backend-url`),zo=document.getElementById(`backend-connect-btn`),Bo=document.getElementById(`backend-ui`),Vo=document.getElementById(`backend-select`),Ho=document.getElementById(`backend-single-save-btn`),Uo=document.getElementById(`backend-delete-btn`),Wo=document.getElementById(`anim-controls-section`),Go=document.getElementById(`anim-play-btn`),Ko=document.getElementById(`anim-select`),qo=document.getElementById(`anim-slider`),Jo=document.getElementById(`prompt-ui-container`),Yo=document.getElementById(`show-grid-cb`),Xo=document.getElementById(`wireframe-cb`),Zo=document.getElementById(`fullscreen-btn`),Qo=document.getElementById(`screenshot-btn`),$o=document.getElementById(`camera-select`),es=``,B=null,ts=null,ns=[],rs=!1,is=null,V=null,as=!1,os=!1,ss=null,cs=[],ls=!1,us=!1,ds={isNew:!0,options:{},content:``},H=null,fs=!0,ps=!1,ms=[],U=null;function hs(e,t,n){if(e){if(e.isPerspectiveCamera)e.aspect=t/n,e.updateProjectionMatrix();else if(e.isOrthographicCamera){let r=t/n;e.userData.ymag||(e.userData.ymag=e.top),e.left=-e.userData.ymag*r,e.right=e.userData.ymag*r,e.top=e.userData.ymag,e.bottom=-e.userData.ymag,e.updateProjectionMatrix()}}}function gs(){return us?R.value:R.value||`// Global detail setting
4223
+ `,Ro=document.getElementById(`prompt-desc`),zo=document.getElementById(`copy-prompt-btn`),Bo=document.getElementById(`model-name-input`),z=document.getElementById(`editor`),Vo=document.getElementById(`render-btn`),Ho=document.getElementById(`load-scad-btn`),Uo=document.getElementById(`download-scad-btn`),Wo=document.getElementById(`export-gltf-btn`),Go=document.getElementById(`capture-image-btn`),Ko=document.getElementById(`share-btn`),qo=document.getElementById(`auto-render-cb`),Jo=document.getElementById(`minify-share-cb`),B=document.getElementById(`viewer`),Yo=document.getElementById(`right-panel`),Xo=document.getElementById(`backend-url`),Zo=document.getElementById(`backend-connect-btn`),Qo=document.getElementById(`backend-ui`),$o=document.getElementById(`backend-select`),es=document.getElementById(`backend-single-save-btn`),ts=document.getElementById(`backend-delete-btn`),ns=document.getElementById(`anim-controls-section`),rs=document.getElementById(`anim-play-btn`),is=document.getElementById(`anim-select`),as=document.getElementById(`anim-slider`),os=document.getElementById(`prompt-ui-container`),ss=document.getElementById(`show-grid-cb`),cs=document.getElementById(`wireframe-cb`),ls=document.getElementById(`bloom-cb`),V=document.getElementById(`lights-cb`),us=document.getElementById(`fullscreen-btn`),ds=document.getElementById(`screenshot-btn`),fs=document.getElementById(`camera-select`),ps=``,H=null,ms=null,hs=[],gs=!1,_s=null,U=null,vs=!1,ys=!1,bs=null,xs=[],Ss=!1,Cs=!1,ws={isNew:!0,options:{},content:``},W=null,Ts=!0,Es=!1,Ds=[],Os=[],G=null,ks=!1,As=1;function js(){return Cs?z.value:z.value||`// Global detail setting
4112
4224
  $fn = 64;
4113
4225
 
4114
4226
  // Animation Data Dictionary
@@ -4234,8 +4346,8 @@ armature(animations=anim_data) {
4234
4346
  }
4235
4347
  }
4236
4348
  }
4237
- `}function _s(){return Oo.value.trim()||`model`}Oo.addEventListener(`input`,Ms),Jo&&(Jo.innerHTML=Ge,We(Jo,`scad_editor_prompt_settings`)),Do.onclick=async()=>{let e=Eo.value.trim()||`an object`,t=Ue(Jo),n=He(e,t);try{await navigator.clipboard.writeText(n);let e=Do.innerText;Do.innerText=`✅ Copied!`,setTimeout(()=>{Do.innerText=e},2e3)}catch(e){alert(`Failed to copy clipboard: `+e)}};async function vs(e){let t={};if(!us)return t;let n=new Set;async function r(e){let i=/(?:include|use)\s*([<"])([^>"]+)([>"])/g,a;for(;(a=i.exec(e))!==null;){let e=a[2],i=e.split(/[/\\]/).pop();if(!n.has(e)){n.add(e);try{let n=await fetch(`${Os}/api/scads/${encodeURIComponent(i)}`);if(n.ok){let i=await n.json();t[e]=i.content,await r(i.content)}}catch{console.warn(`Failed to load dependency: ${e}`)}}}}return await r(e),t}async function ys(e){if(typeof e!=`string`)return;if(rs){is=e;return}if(e.trim()===``){Us();return}let t=e.match(/\/\*\s*Model Name:\s*(.*?)\s*\*\//i);if(t&&t[1]){let e=t[1].trim().replace(/\s+/g,`_`).replace(/[^a-zA-Z0-9_-]/g,``);e&&Oo.value!==e&&(Oo.value=e,Ms())}rs=!0;try{let t=await vs(e);ts=await Se(e,{wasmUrl:he,additionalFiles:t}),await Gs(ts)}catch(e){console.error(e)}finally{if(rs=!1,is!==null){let e=is;is=null,ys(e)}}}ko.onclick=()=>ys(gs());var bs;R.addEventListener(`input`,()=>{Ms(),clearTimeout(bs),Fo.checked&&(bs=setTimeout(()=>{ys(gs())},800))}),Fo.addEventListener(`change`,()=>{Fo.checked&&ys(gs())}),Ao.onclick=()=>{let e=document.createElement(`input`);e.type=`file`,e.accept=`.scad`,e.onchange=async e=>{if(e.target.files&&e.target.files.length>0)try{let t=await e.target.files[0].text();R.value=t,Ms(),ys(t)}catch(e){alert(`Failed to read file: `+e.message)}},e.click()};function xs(e,t){let n=URL.createObjectURL(e),r=document.createElement(`a`);r.href=n,r.download=t,document.body.appendChild(r),r.click(),document.body.removeChild(r),URL.revokeObjectURL(n)}jo.onclick=()=>{let e=gs();xs(new Blob([e],{type:`text/plain`}),`${_s()}.scad`)},Mo.onclick=()=>{ts&&xs(new Blob([ts],{type:`application/octet-stream`}),`${_s()}.glb`)};function Ss(){No.innerText=os?`🔴 Rec...`:H&&fs?`🎥 Video`:`📷 Image`}No.onclick=()=>{if(os){ls=!0,ss&&ss.state!==`inactive`&&ss.stop();return}if(H&&fs){if(typeof MediaRecorder<`u`){let e=q!==void 0&&J&&J.checked;H.time=0,V&&V.update(0),hs(U,512,512),K.setSize(512,512,!1),Ps.setSize(512,512),e&&q.updateCamera();let t=K.domElement,n=document.createElement(`canvas`);n.width=512,n.height=512;let r=n.getContext(`2d`),i=n.captureStream(30),a=25e5,o=`mp4`,s={mimeType:`video/mp4;codecs=avc1`,videoBitsPerSecond:a};MediaRecorder.isTypeSupported(s.mimeType)||(s={mimeType:`video/mp4`,videoBitsPerSecond:a},MediaRecorder.isTypeSupported(s.mimeType)||(o=`webm`,s={mimeType:`video/webm;codecs=vp9`,videoBitsPerSecond:a},MediaRecorder.isTypeSupported(s.mimeType)||(s={mimeType:`video/webm;codecs=vp8`,videoBitsPerSecond:a},MediaRecorder.isTypeSupported(s.mimeType)||(s={mimeType:`video/webm`,videoBitsPerSecond:a}))));let c=null;ls=!1,os=!0,Ss();let l=H.getClip().duration,u=Math.round(l*30),d=0,f=[],p=()=>{if(ls||d>=u){if(f.length===0){os=!1,hs(U,z.clientWidth,z.clientHeight),K.setSize(z.clientWidth,z.clientHeight),Ps.setSize(z.clientWidth,z.clientHeight),e&&q!==void 0&&(q.setScene(W,U),q.updateCamera()),Ss();return}ls=!1,ss=new MediaRecorder(i,s),cs=[],ss.ondataavailable=e=>{e.data.size>0&&cs.push(e.data)},ss.onstop=()=>{c&&clearTimeout(c),xs(new Blob(cs,{type:s.mimeType.split(`;`)[0]}),`${_s()}.${o}`),os=!1,hs(U,z.clientWidth,z.clientHeight),K.setSize(z.clientWidth,z.clientHeight),Ps.setSize(z.clientWidth,z.clientHeight),e&&q!==void 0&&(q.setScene(W,U),q.updateCamera()),Ss()},r.drawImage(f[0],0,0,512,512),ss.start();let t=0,n=performance.now(),a=()=>{if(ls){ss.stop();return}if(t<f.length){r.drawImage(f[t],0,0,512,512),t++;let e=n+1e3/30*t,i=Math.max(0,e-performance.now());c=setTimeout(a,i)}else c=setTimeout(()=>ss.stop(),50)};c=setTimeout(a,0);return}if(H.time=d/30,V&&V.update(0),e){Z!==void 0&&(Z.visible=!1),Y!==void 0&&(Y.visible=!1),Q!==void 0&&(Q.visible=!1),$!==void 0&&($.visible=!1),q.setScene(W,U);let e=()=>{if(ls){p();return}let n=0;for(;q.samples<100&&n<5;)q.renderSample(),n++;if(q.samples>=100){let e=document.createElement(`canvas`);e.width=512,e.height=512,e.getContext(`2d`).drawImage(t,0,0,512,512),f.push(e),d++,setTimeout(p,0)}else requestAnimationFrame(e)};e()}else{let e=!Yo||Yo.checked;Z!==void 0&&(Z.visible=!0),Y!==void 0&&(Y.visible=!0),Q!==void 0&&(Q.visible=e),$!==void 0&&($.visible=e),Ps.render();let n=document.createElement(`canvas`);n.width=512,n.height=512,n.getContext(`2d`).drawImage(t,0,0,512,512),f.push(n),d++,setTimeout(p,0)}};p()}else alert(`Video recording is not supported in this browser.`)}else as=!0};function Cs(e){let t=e.length%4;return t===2?e+`==`:t===3?e+`=`:e}async function ws(e){try{if(typeof CompressionStream<`u`){let t=new Blob([e]).stream().pipeThrough(new CompressionStream(`deflate-raw`)),n=await new Response(t).arrayBuffer(),r=new Uint8Array(n),i=``;for(let e=0;e<r.length;e++)i+=String.fromCharCode(r[e]);return`c`+btoa(i).replace(/\+/g,`-`).replace(/\//g,`_`).replace(/=+$/,``)}}catch(e){console.warn(`CompressionStream failed, falling back`,e)}return`u`+btoa(unescape(encodeURIComponent(e))).replace(/\+/g,`-`).replace(/\//g,`_`).replace(/=+$/,``)}async function Ts(e){if(!e)return null;let t=e.charAt(0),n=e.substring(1);if(n=Cs(n.replace(/-/g,`+`).replace(/_/g,`/`)),t===`c`)try{let e=atob(n),t=new Uint8Array(e.length);for(let n=0;n<e.length;n++)t[n]=e.charCodeAt(n);let r=new Blob([t]).stream().pipeThrough(new DecompressionStream(`deflate-raw`)),i=await new Response(r).arrayBuffer();return new TextDecoder().decode(i)}catch(e){console.warn(`DecompressionStream failed`,e)}else if(t===`u`)try{return decodeURIComponent(escape(atob(n)))}catch(e){console.warn(`Unescape failed`,e)}else try{return decodeURIComponent(escape(atob(Cs(e.replace(/-/g,`+`).replace(/_/g,`/`)))))}catch{return decodeURIComponent(e)}return null}Po.onclick=async()=>{let e=R.value.trim(),t=e,n=new URL(window.location.href),r=``;if(Io&&Io.checked){let t=``,n=!1,r=0;for(;r<e.length;)if(n)t+=e[r],e[r]===`"`&&e[r-1]!==`\\`&&(n=!1),r++;else if(e[r]===`"`)n=!0,t+=e[r],r++;else if(e[r]===`/`&&e[r+1]===`/`)for(r+=2;r<e.length&&e[r]!==`
4238
- `;)r++;else if(e[r]===`/`&&e[r+1]===`*`){for(r+=2;r<e.length&&(e[r]!==`*`||e[r+1]!==`/`);)r++;r+=2}else/\s/.test(e[r])?(t.length>0&&!/\s/.test(t[t.length-1])&&(t+=` `),r++):(t+=e[r],r++);e=t.trim()}if(!e||!us&&t===`// Global detail setting
4349
+ `}function Ms(){return Bo.value.trim()||`model`}Bo.addEventListener(`input`,Us),os&&(os.innerHTML=$e,Qe(os,`scad_editor_prompt_settings`)),zo.onclick=async()=>{let e=Ro.value.trim()||`an object`,t=Ze(os),n=Xe(e,t);try{await navigator.clipboard.writeText(n);let e=zo.innerText;zo.innerText=`✅ Copied!`,setTimeout(()=>{zo.innerText=e},2e3)}catch(e){alert(`Failed to copy clipboard: `+e)}};async function Ns(e){return Cs?Ie(e,zs,null):{}}async function Ps(e){if(typeof e!=`string`)return;if(gs){_s=e;return}if(e.trim()===``){nc();return}let t=e.match(/\/\*\s*Model Name:\s*(.*?)\s*\*\//i);if(t&&t[1]){let e=t[1].trim().replace(/\s+/g,`_`).replace(/[^a-zA-Z0-9_-]/g,``);e&&Bo.value!==e&&(Bo.value=e,Us())}gs=!0;try{let t=await Ns(e);ms=await ee(e,{wasmUrl:s,additionalFiles:t}),await rc(ms)}catch(e){console.error(e)}finally{if(gs=!1,_s!==null){let e=_s;_s=null,Ps(e)}}}Vo.onclick=()=>Ps(js());var Fs;z.addEventListener(`input`,()=>{Us(),clearTimeout(Fs),qo.checked&&(Fs=setTimeout(()=>{Ps(js())},800))}),qo.addEventListener(`change`,()=>{qo.checked&&Ps(js())}),Ho.onclick=()=>{let e=document.createElement(`input`);e.type=`file`,e.accept=`.scad`,e.onchange=async e=>{if(e.target.files&&e.target.files.length>0)try{let t=await e.target.files[0].text();z.value=t,Us(),Ps(t)}catch(e){alert(`Failed to read file: `+e.message)}},e.click()},Uo.onclick=()=>{let e=js();xe(new Blob([e],{type:`text/plain`}),`${Ms()}.scad`)},Wo.onclick=()=>{ms&&xe(new Blob([ms],{type:`application/octet-stream`}),`${Ms()}.glb`)};function Is(){Go.innerText=ys?`🔴 Rec...`:W&&Ts?`🎥 Video`:`📷 Image`}Go.onclick=()=>{if(ys){Ss=!0,bs&&bs.state!==`inactive`&&bs.stop();return}if(W&&Ts){if(typeof MediaRecorder<`u`){let e=J!==void 0&&Y&&Y.checked;W.time=0,U&&U.update(0),Be(G,512,512),q.setSize(512,512,!1),Ks.setSize(512,512),e&&J.updateCamera();let t=q.domElement,n=document.createElement(`canvas`);n.width=512,n.height=512;let r=n.getContext(`2d`),i=n.captureStream(30),a=25e5,o=`mp4`,s={mimeType:`video/mp4;codecs=avc1`,videoBitsPerSecond:a};MediaRecorder.isTypeSupported(s.mimeType)||(s={mimeType:`video/mp4`,videoBitsPerSecond:a},MediaRecorder.isTypeSupported(s.mimeType)||(o=`webm`,s={mimeType:`video/webm;codecs=vp9`,videoBitsPerSecond:a},MediaRecorder.isTypeSupported(s.mimeType)||(s={mimeType:`video/webm;codecs=vp8`,videoBitsPerSecond:a},MediaRecorder.isTypeSupported(s.mimeType)||(s={mimeType:`video/webm`,videoBitsPerSecond:a}))));let c=null;Ss=!1,ys=!0,Is();let l=W.getClip().duration,u=Math.round(l*30),d=0,f=[],p=()=>{if(Ss||d>=u){if(f.length===0){ys=!1,Be(G,B.clientWidth,B.clientHeight),q.setSize(B.clientWidth,B.clientHeight),Ks.setSize(B.clientWidth,B.clientHeight),e&&J!==void 0&&(J.setScene(K,G),J.updateCamera()),Is();return}Ss=!1,bs=new MediaRecorder(i,s),xs=[],bs.ondataavailable=e=>{e.data.size>0&&xs.push(e.data)},bs.onstop=()=>{c&&clearTimeout(c);let t=new Blob(xs,{type:s.mimeType.split(`;`)[0]});xe(t,`${Ms()}.${o}`),ys=!1,Be(G,B.clientWidth,B.clientHeight),q.setSize(B.clientWidth,B.clientHeight),Ks.setSize(B.clientWidth,B.clientHeight),e&&J!==void 0&&(J.setScene(K,G),J.updateCamera()),Is()},r.drawImage(f[0],0,0,512,512),bs.start();let t=0,n=performance.now(),a=()=>{if(Ss){bs.stop();return}if(t<f.length){r.drawImage(f[t],0,0,512,512),t++;let e=n+1e3/30*t,i=Math.max(0,e-performance.now());c=setTimeout(a,i)}else c=setTimeout(()=>bs.stop(),50)};c=setTimeout(a,0);return}if(W.time=d/30,U&&U.update(0),e){Z!==void 0&&(Z.visible=!1),X!==void 0&&(X.visible=!1),Q!==void 0&&(Q.visible=!1),$!==void 0&&($.visible=!1),J.setScene(K,G);let e=()=>{if(Ss){p();return}let n=0;for(;J.samples<100&&n<5;)J.renderSample(),n++;if(J.samples>=100){let e=document.createElement(`canvas`);e.width=512,e.height=512,e.getContext(`2d`).drawImage(t,0,0,512,512),f.push(e),d++,setTimeout(p,0)}else requestAnimationFrame(e)};e()}else{let e=!ss||ss.checked;Z!==void 0&&(Z.visible=!0),X!==void 0&&(X.visible=!ks||V&&!V.checked),Q!==void 0&&(Q.visible=e),$!==void 0&&($.visible=e),Ks.render();let n=document.createElement(`canvas`);n.width=512,n.height=512,n.getContext(`2d`).drawImage(t,0,0,512,512),f.push(n),d++,setTimeout(p,0)}};p()}else alert(`Video recording is not supported in this browser.`)}else vs=!0},Ko.onclick=async()=>{let e=z.value.trim(),t=e,n=new URL(window.location.href),r=``;if(Jo&&Jo.checked){let t=``,n=!1,r=0;for(;r<e.length;)if(n)t+=e[r],e[r]===`"`&&e[r-1]!==`\\`&&(n=!1),r++;else if(e[r]===`"`)n=!0,t+=e[r],r++;else if(e[r]===`/`&&e[r+1]===`/`)for(r+=2;r<e.length&&e[r]!==`
4350
+ `;)r++;else if(e[r]===`/`&&e[r+1]===`*`){for(r+=2;r<e.length&&(e[r]!==`*`||e[r+1]!==`/`);)r++;r+=2}else/\s/.test(e[r])?(t.length>0&&!/\s/.test(t[t.length-1])&&(t+=` `),r++):(t+=e[r],r++);e=t.trim()}if(!e||!Cs&&t===`// Global detail setting
4239
4351
  $fn = 64;
4240
4352
 
4241
4353
  // Animation Data Dictionary
@@ -4360,4 +4472,4 @@ armature(animations=anim_data) {
4360
4472
  }
4361
4473
  }
4362
4474
  }
4363
- }`)r=n.origin+n.pathname+n.search;else try{n.hash=await ws(e),r=n.toString()}catch{r=n.origin+n.pathname+n.search}window.history.replaceState(null,``,r);try{if(navigator.share){let e=Oo.value.trim(),t=e?e.replace(/[-_]/g,` `).replace(/\b\w/g,e=>e.toUpperCase()):`Scadify`;await navigator.share({title:t,url:r});let n=Po.innerText;Po.innerText=`✅ Shared!`,setTimeout(()=>Po.innerText=n,2e3)}else{await navigator.clipboard.writeText(r);let e=Po.innerText;Po.innerText=`✅ Copied Link!`,setTimeout(()=>Po.innerText=e,2e3)}}catch(e){if(e.name!==`AbortError`)try{await navigator.clipboard.writeText(r),Po.innerText=`✅ Copied Link!`,setTimeout(()=>Po.innerText=`🔗 Share`,2e3)}catch{alert(`Failed to share or copy link.`)}}};var Es=document.getElementById(`drag-overlay`);window.addEventListener(`dragenter`,e=>{e.preventDefault(),Es.classList.add(`active`)}),window.addEventListener(`dragover`,e=>{e.preventDefault(),Es.classList.add(`active`)}),Es.addEventListener(`dragleave`,e=>{e.preventDefault(),Es.classList.remove(`active`)}),window.addEventListener(`drop`,async e=>{if(e.preventDefault(),Es.classList.remove(`active`),e.dataTransfer.files&&e.dataTransfer.files.length>0){let t=e.dataTransfer.files[0],n=t.name.toLowerCase();if(n.endsWith(`.hdr`)){let e=URL.createObjectURL(t);new wo().load(e,e=>{e.mapping=303,W.environment=e,zs=e,J&&J.checked?(W.background=e,W.backgroundBlurriness=.5,W.backgroundIntensity=.3):(W.background=new Be(2236962),W.backgroundBlurriness=0,W.backgroundIntensity=1),q!==void 0&&(q.setScene(W,G),q.updateCamera())},void 0,e=>{console.warn(`Error loading HDR:`,e),alert(`Failed to load HDR file.`)})}else if(n.endsWith(`.scad`)||!t.type||t.type.includes(`text`))try{let e=await t.text();R.value=e,Ms(),ys(e)}catch(e){alert(`Failed to read file: `+e.message)}else alert(`Please drop a valid .scad or .hdr file.`)}});var Ds=null,Os=``;async function ks(e){let t=await fetch(`${e}/api/scads`);if(!t.ok)throw Error(`Failed to fetch files`);return(await t.json()).files}function As(){Vo.innerHTML=`<option value="">-- Create New Model --</option>`,Array.isArray(Ds)&&Ds.forEach((e,t)=>{let n=document.createElement(`option`);n.value=t,n.innerText=e,Vo.appendChild(n)}),Vo.value=es,Uo&&(Uo.style.display=es===``?`none`:`flex`)}function js(){let e=Oo.value.trim();return e&&=e.replace(/\.scad$/i,``).split(/[/\\]/).pop()+`.scad`,{filename:e}}function Ms(){if(Ds===null)return;let e=Vo.value===``,{filename:t}=js(),n=R.value,r=!1;e?(t&&t!==`.scad`||n.trim()!==``)&&(r=!0):n!==ds.content&&(r=!0),r?(Ho.style.display=`flex`,Ho.innerText=`Save`):Ho.style.display=`none`}async function Ns(e,t=!1){if(!e)return!1;try{return zo.innerText=`Connecting...`,Ds=await ks(e),Os=e,us=!0,zo.innerText=`Connected`,Bo.classList.add(`active`),R.placeholder=`Enter OpenSCAD code here...`,es=``,As(),ds={isNew:!0,content:``},Ms(),!0}catch(e){return t?console.warn(`Auto-connect failed:`,e.message):alert(`Connection failed: `+e.message),zo.innerText=`Connect`,Bo.classList.remove(`active`),us=!1,R.placeholder=To,!1}}zo.onclick=async()=>{await Ns(Ro.value.trim(),!1)&&ys(gs())},Vo.addEventListener(`change`,async()=>{if(Ho.style.display!==`none`&&!confirm(`You have unsaved changes. Are you sure you want to discard them and load another model?`)){Vo.value=es;return}let e=Vo.value;if(es=e,Uo&&(Uo.style.display=e===``?`none`:`flex`),e===``)Oo.value=``,R.value=``,ys(gs()),ds={isNew:!0,content:``},Ms();else{let t=Ds[e];Oo.value=t.replace(/\.scad$/i,``);try{let e=await fetch(`${Os}/api/scads/${encodeURIComponent(t)}`);if(!e.ok){let t=await e.json();throw Error(t.error||`Failed to load model`)}let n=await e.json();R.value=n.content,ys(gs()),ds={isNew:!1,content:n.content},Ms()}catch(e){alert(`Error loading model: `+e.message)}}}),Ho.onclick=async()=>{let{filename:e}=js();if(!e||e===`.scad`){let e=Ho.innerText;Ho.innerText=`⚠️ Name Required!`,Ho.style.borderColor=`#ff4444`,Oo.focus(),setTimeout(()=>{Ho.innerText=e,Ho.style.borderColor=``},2e3);return}if(Vo.value===``&&Ds&&Ds.includes(e)&&!confirm(`A model named "${e}" already exists. Are you sure you want to overwrite it?`))return;let t={filename:e,content:R.value};try{Ho.innerText=`Saving...`;let n=await fetch(`${Os}/api/scads`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(t)});if(!n.ok){let e=await n.json();throw Error(e.error||`Save failed`)}Ds=await ks(Os);let r=Ds.findIndex(t=>t===e);es=r>=0?r.toString():``,As(),ds={isNew:!1,content:R.value},Ms()}catch(e){Ms(),alert(`Error: `+e.message)}},Uo.onclick=async()=>{let e=Vo.value;if(e===``)return;let t=Ds[e];if(confirm(`Are you sure you want to delete "${t}"?`))try{Uo.innerText=`Deleting...`;let e=await fetch(`${Os}/api/scads/${encodeURIComponent(t)}`,{method:`DELETE`});if(!e.ok){let t=`Delete failed`;try{let n=await e.json();n.error&&(t=n.error)}catch{}throw Error(t)}Ds=await ks(Os),es=``,As(),Oo.value=``,R.value=``,ys(gs()),ds={isNew:!0,content:``},Ms(),Uo.innerText=`Delete`}catch(e){alert(`Error: `+e.message),Uo.innerText=`Delete`}};var W=new ue;W.background=new Be(2236962);var G=new c(60,z.clientWidth/z.clientHeight,.1,2e3);G.position.set(50,50,-50),U=G;var K=new Ie({antialias:!0,preserveDrawingBuffer:!0});K.setSize(z.clientWidth,z.clientHeight),K.setPixelRatio(Math.min(window.devicePixelRatio,2)),K.toneMapping=4,K.toneMappingExposure=1,K.shadowMap.enabled=!0,K.shadowMap.type=2,z.appendChild(K.domElement);var Ps=new ge(K),Fs=new Ae(W,U);Ps.addPass(Fs);var Is=new Ce(new T(z.clientWidth,z.clientHeight),.1,.1,2);Ps.addPass(Is);var Ls=new Pe;Ps.addPass(Ls);var q=new Co(K);q.bounces=10,q.transmissiveBounces=10,q.multipleImportanceSampling=!0,q.filterGlossyFactor=.2;var Rs=new Me(G,K.domElement);Rs.addEventListener(`change`,()=>{q!==void 0&&q.updateCamera()});var zs=null;new wo().load(`./aristea_wreck_puresky_2k.hdr`,e=>{e.mapping=303,W.environment=e,zs=e;let t=J&&J.checked,n=!Yo||Yo.checked;Z!==void 0&&(Z.visible=!t),Y!==void 0&&(Y.visible=!t),Q!==void 0&&(Q.visible=!t&&n),$!==void 0&&($.visible=!t&&n),t?(W.background=e,W.backgroundBlurriness=.5,W.backgroundIntensity=.3):(W.background=new Be(2236962),W.backgroundBlurriness=0,W.backgroundIntensity=1),q!==void 0&&(q.setScene(W,U),q.updateCamera())},void 0,e=>console.warn(`Error loading HDR:`,e));var J=document.getElementById(`path-tracing-cb`);J&&J.addEventListener(`change`,()=>{let e=J.checked,t=!Yo||Yo.checked;Z!==void 0&&(Z.visible=!e),Y!==void 0&&(Y.visible=!e),Q!==void 0&&(Q.visible=!e&&t),$!==void 0&&($.visible=!e&&t),e?zs&&(W.background=zs,W.backgroundBlurriness=.5,W.backgroundIntensity=.3):(W.background=new Be(2236962),W.backgroundBlurriness=0,W.backgroundIntensity=1),e&&q!==void 0&&(q.setScene(W,U),q.updateCamera())}),$o&&$o.addEventListener(`change`,e=>{let t=e.target.value;t===``?(U=G,Rs.enabled=!0):(U=ms[parseInt(t)],Rs.enabled=!1);let n=z.clientWidth,r=z.clientHeight;hs(U,n,r),Fs.camera=U,J&&J.checked&&q!==void 0&&(q.setScene(W,U),q.updateCamera())}),Rs.enableDamping=!0,Rs.dampingFactor=.1,Rs.maxDistance=2e3,W.environment=new be(K).fromScene(new re,.04).texture,W.environmentIntensity=.8;var Y=new h;W.add(Y);var X=new Re(16777215,1.5);X.castShadow=!0,X.shadow.mapSize.width=2048,X.shadow.mapSize.height=2048,X.shadow.bias=-5e-4,Y.add(X),Y.add(X.target);var Bs=new t(16777215,4473924,.6);Bs.position.set(0,20,0),Y.add(Bs);var Z=new v(new C(2e3,2e3),new n({color:2236962,roughness:.8,metalness:.1}));Z.rotation.x=-Math.PI/2,Z.receiveShadow=!0,W.add(Z);var Q=new i(2e3,100,5592405,3355443);Q.material.transparent=!0,Q.material.opacity=.5,W.add(Q);var $=new de(100);W.add($),Xo&&Xo.addEventListener(`change`,()=>{let e=Xo.checked;B&&B.traverse(t=>{t.isMesh&&t.material&&(Array.isArray(t.material)?t.material.forEach(t=>{t.wireframe=e}):t.material.wireframe=e)})}),Zo&&Lo&&(Zo.addEventListener(`click`,()=>{document.fullscreenElement?document.exitFullscreen():Lo.requestFullscreen().catch(e=>{console.warn(`Error attempting to enable fullscreen: ${e.message}`)})}),document.addEventListener(`fullscreenchange`,()=>{setTimeout(()=>window.dispatchEvent(new Event(`resize`)),100)})),Qo&&Qo.addEventListener(`click`,()=>{as=!0;let e=Qo.innerText;Qo.innerText=`✅`,setTimeout(()=>{Qo.innerText=e},1e3)});function Vs(){H&&(fs=!1,H.paused=!0,Go.innerText=`▶ Play`,Ss())}function Hs(e){if(!V||!ns[e])return;H&&H.stop();let t=ns[e];H=V.clipAction(t),H.play(),H.time=0,V.update(0),fs=!0,H.paused=!1,Go.innerText=`⏸ Pause`,qo.value=0,Ss()}Ko.addEventListener(`change`,e=>{Hs(parseInt(e.target.value))}),Go.addEventListener(`click`,()=>{H&&(fs=!fs,H.paused=!fs,Go.innerText=fs?`⏸ Pause`:`▶ Play`,Ss())}),qo.addEventListener(`mousedown`,()=>{ps=!0,Vs()}),qo.addEventListener(`mouseup`,()=>{ps=!1}),qo.addEventListener(`touchstart`,()=>{ps=!0,Vs()},{passive:!0}),qo.addEventListener(`touchend`,()=>{ps=!1},{passive:!0}),qo.addEventListener(`input`,e=>{if(Vs(),H){let t=H.getClip().duration;H.time=parseFloat(e.target.value)*t,V&&(V.update(0),J&&J.checked&&q!==void 0&&(Z!==void 0&&(Z.visible=!1),Y!==void 0&&(Y.visible=!1),Q!==void 0&&(Q.visible=!1),$!==void 0&&($.visible=!1),q.setScene(W,U),q.updateCamera()))}});function Us(){os&&ss&&ss.state!==`inactive`&&(ss.stop(),os=!1),B&&=(V&&=(V.stopAllAction(),V.uncacheRoot(V.getRoot()),null),H=null,W.remove(B),B.traverse(e=>{e.isMesh&&(e.geometry.dispose(),Array.isArray(e.material)?e.material.forEach(e=>e.dispose()):e.material&&e.material.dispose())}),null),Ss()}function Ws(e,t){if(!e)return new De;e.updateMatrixWorld(!0);let n=new De().setFromObject(e);if(!t||t.length===0)return n;let r=new Map;e.traverse(e=>{r.set(e,{position:e.position.clone(),quaternion:e.quaternion.clone(),scale:e.scale.clone()})});let i=new ce(e);for(let r of t){let t=i.clipAction(r);t.play();let a=new Set([0,r.duration]);if(r.tracks){for(let e of r.tracks)if(e.times)for(let t=0;t<e.times.length;t++)a.add(e.times[t])}if(r.duration>0)for(let e=0;e<=20;e++)a.add(e/20*r.duration);let o=Array.from(a).sort((e,t)=>e-t);if(o.length>60){let e=[],t=(o.length-1)/59;for(let n=0;n<60;n++)e.push(o[Math.round(n*t)]);o=e}for(let r of o)t.time=r,i.update(0),e.updateMatrixWorld(!0),n.expandByObject(e);t.stop()}return i.stopAllAction(),i.uncacheRoot(e),r.forEach((e,t)=>{t.position.copy(e.position),t.quaternion.copy(e.quaternion),t.scale.copy(e.scale)}),e.updateMatrixWorld(!0),n}function Gs(e){return new Promise((t,n)=>{let r=null;B&&(V&&H&&(H.time=0,V.update(0),J&&J.checked&&q!==void 0&&(Z!==void 0&&(Z.visible=!1),Y!==void 0&&(Y.visible=!1),Q!==void 0&&(Q.visible=!1),$!==void 0&&($.visible=!1),q.setScene(W,U),q.updateCamera())),r=Ws(B,ns)),Us();let i=e;e instanceof Uint8Array&&(i=e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)),new Te().parse(i,``,e=>{B=e.scene,ns=e.animations||[];let n=Xo?Xo.checked:!1;ms=[],B.traverse(e=>{e.isCamera&&ms.push(e)});let i=!1;$o?ms.length>0?($o.style.display=`inline-block`,$o.innerHTML=`<option value="">Free Camera</option>`,ms.forEach((e,t)=>{let n=document.createElement(`option`);n.value=t,n.innerText=e.name||`Camera ${t+1}`,$o.appendChild(n)}),$o.value=`0`,U=ms[0],Rs.enabled=!1,i=!0):($o.style.display=`none`,$o.value=``,U=G,Rs.enabled=!0):(U=G,Rs.enabled=!0);let a=z?z.clientWidth:window.innerWidth,o=z?z.clientHeight:window.innerHeight;hs(U,a,o),Fs.camera=U;let s=document.getElementById(`anim-slider-row`);ns.length?(V=new ce(B),i=!0,Go&&(Go.style.display=``),Ko&&(Ko.style.display=``),s&&(s.style.display=``),Ko.innerHTML=``,ns.forEach((e,t)=>{let n=document.createElement(`option`);n.value=t,n.innerText=e.name||`Animation ${t+1}`,Ko.appendChild(n)}),Hs(0)):(Go&&(Go.style.display=`none`),Ko&&(Ko.style.display=`none`),s&&(s.style.display=`none`),H=null,Ss()),Wo&&(Wo.style.display=i?`flex`:`none`),B.traverse(e=>{e.isMesh&&(e.castShadow=!0,e.receiveShadow=!0,e.frustumCulled=!1,e.material&&(Array.isArray(e.material)?e.material.forEach(e=>{e.wireframe=n}):e.material.wireframe=n))}),W.add(B);let c=!0;if(r&&!r.isEmpty()){let e=Ws(B,ns);if(!e.isEmpty()){let t=.001;r.min.distanceTo(e.min)<t&&r.max.distanceTo(e.max)<t&&(c=!1)}}if(c)typeof Ks==`function`&&Ks();else{let e=J&&J.checked,t=!Yo||Yo.checked;Z!==void 0&&(Z.visible=!e),Y!==void 0&&(Y.visible=!e),Q!==void 0&&(Q.visible=!e&&t),$!==void 0&&($.visible=!e&&t),q!==void 0&&(q.setScene(W,U),q.updateCamera())}t()},n)})}function Ks(){if(!B)return;let e=Ws(B,ns);if(e.isEmpty())return;let t=e.getCenter(new E),n=e.getSize(new E),r=Math.max(n.x,n.y,n.z)||10;Z.position.y=e.min.y-.01,Q.position.y=Z.position.y+.001,$.position.y=Z.position.y+.002;let i=G.fov*(Math.PI/180),o=r/(2*Math.tan(i/2));G.aspect<1&&(o/=G.aspect),o*=1.5,G.near=Math.max(.01,r*.01),G.far=Math.max(2e3,o*10),G.updateProjectionMatrix(),Rs.maxDistance=G.far,G.position.set(t.x+o*.8,t.y+o*.8,t.z-o*.8),G.lookAt(t),Rs.target.copy(t),Rs.update(),X.position.set(t.x+r,t.y+r*1.5,t.z-r),X.target.position.copy(t),X.target.updateMatrixWorld();let s=r*1.5;X.shadow.camera.left=-s,X.shadow.camera.right=s,X.shadow.camera.top=s,X.shadow.camera.bottom=-s,X.shadow.camera.near=.1,X.shadow.camera.far=r*5,X.shadow.camera.updateProjectionMatrix(),W.fog=new a(2236962,o*1.5,o*5);let c=J&&J.checked,l=!Yo||Yo.checked;Z!==void 0&&(Z.visible=!c),Y!==void 0&&(Y.visible=!c),Q!==void 0&&(Q.visible=!c&&l),$!==void 0&&($.visible=!c&&l),q!==void 0&&(q.setScene(W,U),q.updateCamera())}var qs=performance.now();function Js(){requestAnimationFrame(Js);let e=performance.now(),t=(e-qs)/1e3;if(qs=e,V&&(!(J&&J.checked)&&!os&&V.update(t),H&&fs&&!ps)){let e=H.getClip().duration;if(e>0){let t=H.time%e;t<0&&(t+=e),qo.value=t/e}}Rs.update();let n=J&&J.checked,r=!Yo||Yo.checked;n?(Z!==void 0&&(Z.visible=!1),Y!==void 0&&(Y.visible=!1),Q!==void 0&&(Q.visible=!1),$!==void 0&&($.visible=!1),q!==void 0&&!os&&(H&&fs&&q.updateCamera(),q.renderSample())):(Z!==void 0&&(Z.visible=!0),Y!==void 0&&(Y.visible=!0),Q!==void 0&&(Q.visible=r),$!==void 0&&($.visible=r),os||Ps.render()),as&&(as=!1,K.domElement.toBlob(e=>{e&&xs(e,`${_s()}.png`)},`image/png`))}Js(),window.addEventListener(`resize`,()=>{if(!z||os)return;let e=z.clientWidth,t=z.clientHeight;hs(U,e,t),K.setSize(e,t),Ps.setSize(e,t),q!==void 0&&q.updateCamera()}),setTimeout(()=>window.dispatchEvent(new Event(`resize`)),100);var Ys=[`localhost`,`127.0.0.1`,``].includes(window.location.hostname);R.placeholder=Ys?`Enter OpenSCAD code here...`:To,(async function(){if(Ys&&await Ns(Ro.value.trim(),!0),window.location.hash&&window.location.hash.length>1)try{let e=await Ts(window.location.hash.substring(1));e&&(R.value=e)}catch(e){console.error(`Failed to decode hash`,e)}Ms(),setTimeout(()=>ys(gs()),500)})();
4475
+ }`)r=n.origin+n.pathname+n.search;else try{n.hash=await se(e),r=n.toString()}catch{r=n.origin+n.pathname+n.search}window.history.replaceState(null,``,r);try{if(navigator.share){let e=Bo.value.trim(),t=e?e.replace(/[-_]/g,` `).replace(/\b\w/g,e=>e.toUpperCase()):`Scadify`;await navigator.share({title:t,url:r});let n=Ko.innerText;Ko.innerText=`✅ Shared!`,setTimeout(()=>Ko.innerText=n,2e3)}else{await navigator.clipboard.writeText(r);let e=Ko.innerText;Ko.innerText=`✅ Copied Link!`,setTimeout(()=>Ko.innerText=e,2e3)}}catch(e){if(e.name!==`AbortError`)try{await navigator.clipboard.writeText(r),Ko.innerText=`✅ Copied Link!`,setTimeout(()=>Ko.innerText=`🔗 Share`,2e3)}catch{alert(`Failed to share or copy link.`)}}};var Ls=document.getElementById(`drag-overlay`);window.addEventListener(`dragenter`,e=>{e.preventDefault(),Ls.classList.add(`active`)}),window.addEventListener(`dragover`,e=>{e.preventDefault(),Ls.classList.add(`active`)}),Ls.addEventListener(`dragleave`,e=>{e.preventDefault(),Ls.classList.remove(`active`)}),window.addEventListener(`drop`,async e=>{if(e.preventDefault(),Ls.classList.remove(`active`),e.dataTransfer.files&&e.dataTransfer.files.length>0){let n=e.dataTransfer.files[0],r=n.name.toLowerCase();if(r.endsWith(`.hdr`)){let e=URL.createObjectURL(n);new Po().load(e,e=>{e.mapping=303,K.environment=e,Zs=e,Y&&Y.checked?(K.background=e,K.backgroundBlurriness=.5,K.backgroundIntensity=.3):(K.background=new t(2236962),K.backgroundBlurriness=0,K.backgroundIntensity=1),J!==void 0&&(J.setScene(K,G),J.updateCamera())},void 0,e=>{console.warn(`Error loading HDR:`,e),alert(`Failed to load HDR file.`)})}else if(r.endsWith(`.scad`)||!n.type||n.type.includes(`text`))try{let e=await n.text();z.value=e,Us(),Ps(e)}catch(e){alert(`Failed to read file: `+e.message)}else alert(`Please drop a valid .scad or .hdr file.`)}});var Rs=null,zs=``;async function Bs(e){let t=await fetch(`${e}/api/scads`);if(!t.ok)throw Error(`Failed to fetch files`);return(await t.json()).files}function Vs(){$o.innerHTML=`<option value="">-- Create New Model --</option>`,Array.isArray(Rs)&&Rs.forEach((e,t)=>{let n=document.createElement(`option`);n.value=t,n.innerText=e,$o.appendChild(n)}),$o.value=ps,ts&&(ts.style.display=ps===``?`none`:`flex`)}function Hs(){let e=Bo.value.trim();return e&&=e.replace(/\.scad$/i,``).split(/[/\\]/).pop()+`.scad`,{filename:e}}function Us(){if(Rs===null)return;let e=$o.value===``,{filename:t}=Hs(),n=z.value,r=!1;e?(t&&t!==`.scad`||n.trim()!==``)&&(r=!0):n!==ws.content&&(r=!0),r?(es.style.display=`flex`,es.innerText=`Save`):es.style.display=`none`}async function Ws(e,t=!1){if(!e)return!1;try{return Zo.innerText=`Connecting...`,Rs=await Bs(e),zs=e,Cs=!0,Zo.innerText=`Connected`,Qo.classList.add(`active`),z.placeholder=`Enter OpenSCAD code here...`,ps=``,Vs(),ws={isNew:!0,content:``},Us(),!0}catch(e){return t?console.warn(`Auto-connect failed:`,e.message):alert(`Connection failed: `+e.message),Zo.innerText=`Connect`,Qo.classList.remove(`active`),Cs=!1,z.placeholder=Lo,!1}}Zo.onclick=async()=>{await Ws(Xo.value.trim(),!1)&&Ps(js())},$o.addEventListener(`change`,async()=>{if(es.style.display!==`none`&&!confirm(`You have unsaved changes. Are you sure you want to discard them and load another model?`)){$o.value=ps;return}let e=$o.value;if(ps=e,ts&&(ts.style.display=e===``?`none`:`flex`),e===``)Bo.value=``,z.value=``,Ps(js()),ws={isNew:!0,content:``},Us();else{let t=Rs[e];Bo.value=t.replace(/\.scad$/i,``);try{let e=await fetch(`${zs}/api/scads/${encodeURIComponent(t)}`);if(!e.ok){let t=await e.json();throw Error(t.error||`Failed to load model`)}let n=await e.json();z.value=n.content,Ps(js()),ws={isNew:!1,content:n.content},Us()}catch(e){alert(`Error loading model: `+e.message)}}}),es.onclick=async()=>{let{filename:e}=Hs();if(!e||e===`.scad`){let e=es.innerText;es.innerText=`⚠️ Name Required!`,es.style.borderColor=`#ff4444`,Bo.focus(),setTimeout(()=>{es.innerText=e,es.style.borderColor=``},2e3);return}if($o.value===``&&Rs&&Rs.includes(e)&&!confirm(`A model named "${e}" already exists. Are you sure you want to overwrite it?`))return;let t={filename:e,content:z.value};try{es.innerText=`Saving...`;let n=await fetch(`${zs}/api/scads`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(t)});if(!n.ok){let e=await n.json();throw Error(e.error||`Save failed`)}Rs=await Bs(zs);let r=Rs.findIndex(t=>t===e);ps=r>=0?r.toString():``,Vs(),ws={isNew:!1,content:z.value},Us()}catch(e){Us(),alert(`Error: `+e.message)}},ts.onclick=async()=>{let e=$o.value;if(e===``)return;let t=Rs[e];if(confirm(`Are you sure you want to delete "${t}"?`))try{ts.innerText=`Deleting...`;let e=await fetch(`${zs}/api/scads/${encodeURIComponent(t)}`,{method:`DELETE`});if(!e.ok){let t=`Delete failed`;try{let n=await e.json();n.error&&(t=n.error)}catch{}throw Error(t)}Rs=await Bs(zs),ps=``,Vs(),Bo.value=``,z.value=``,Ps(js()),ws={isNew:!0,content:``},Us(),ts.innerText=`Delete`}catch(e){alert(`Error: `+e.message),ts.innerText=`Delete`}};var K=new Ve;K.background=new t(2236962);var Gs=new y(60,B.clientWidth/B.clientHeight,.1,2e3);Gs.position.set(50,50,-50),G=Gs;var q=new Ke({antialias:!0,preserveDrawingBuffer:!0});q.setSize(B.clientWidth,B.clientHeight),q.setPixelRatio(Math.min(window.devicePixelRatio,2)),q.toneMapping=4,q.toneMappingExposure=1,q.shadowMap.enabled=!0,q.shadowMap.type=2,B.appendChild(q.domElement);var Ks=new Me(q),qs=new he(K,G);Ks.addPass(qs);var Js=new Io(new E(B.clientWidth,B.clientHeight),.1,.1,2);Js.enabled=!1,Ks.addPass(Js);var Ys=new fe;Ks.addPass(Ys);var J=new No(q);J.bounces=10,J.transmissiveBounces=10,J.multipleImportanceSampling=!0,J.filterGlossyFactor=.2;var Xs=new ae(Gs,q.domElement);Xs.addEventListener(`change`,()=>{J!==void 0&&J.updateCamera()});var Zs=null;new Po().load(`./aristea_wreck_puresky_2k.hdr`,e=>{e.mapping=303,K.environment=e,Zs=e;let n=Y&&Y.checked,r=!ss||ss.checked;Z!==void 0&&(Z.visible=!n),X!==void 0&&(X.visible=!n&&(!ks||V&&!V.checked)),Q!==void 0&&(Q.visible=!n&&r),$!==void 0&&($.visible=!n&&r),n?(K.background=e,K.backgroundBlurriness=.5,K.backgroundIntensity=.3):(K.background=new t(2236962),K.backgroundBlurriness=0,K.backgroundIntensity=1),J!==void 0&&(J.setScene(K,G),J.updateCamera())},void 0,e=>console.warn(`Error loading HDR:`,e));var Y=document.getElementById(`path-tracing-cb`);Y&&Y.addEventListener(`change`,()=>{let e=Y.checked,n=!ss||ss.checked;Z!==void 0&&(Z.visible=!e),X!==void 0&&(X.visible=!e&&(!ks||V&&!V.checked)),Q!==void 0&&(Q.visible=!e&&n),$!==void 0&&($.visible=!e&&n),e?Zs&&(K.background=Zs,K.backgroundBlurriness=.5,K.backgroundIntensity=.3):(K.background=new t(2236962),K.backgroundBlurriness=0,K.backgroundIntensity=1),e&&J!==void 0&&(J.setScene(K,G),J.updateCamera())}),fs&&fs.addEventListener(`change`,e=>{let t=e.target.value;t===``?(G=Gs,Xs.enabled=!0):(G=Ds[parseInt(t)],Xs.enabled=!1);let n=B.clientWidth,r=B.clientHeight;Be(G,n,r),qs.camera=G,Y&&Y.checked&&J!==void 0&&(J.setScene(K,G),J.updateCamera())}),Xs.enableDamping=!0,Xs.dampingFactor=.1,Xs.maxDistance=2e3,K.environment=new le(q).fromScene(new ye,.04).texture,K.environmentIntensity=.8;var X=new b;K.add(X);var Qs=new c(16777215,1.5);Qs.castShadow=!0,Qs.shadow.mapSize.width=2048,Qs.shadow.mapSize.height=2048,Qs.shadow.bias=-5e-4,X.add(Qs),X.add(Qs.target);var $s=new n(16777215,4473924,.6);$s.position.set(0,20,0),X.add($s);var Z=new f(new me(2e3,2e3),new re({color:2236962,roughness:.8,metalness:.1}));Z.rotation.x=-Math.PI/2,Z.receiveShadow=!0,K.add(Z);var Q=new m(2e3,100,5592405,3355443);Q.material.transparent=!0,Q.material.opacity=.5,K.add(Q);var $=new r(100);K.add($),cs&&cs.addEventListener(`change`,()=>{let e=cs.checked;Js.enabled=(ls?ls.checked:!1)&&!e,H&&Te(H,e)}),ls&&ls.addEventListener(`change`,()=>{let e=ls.checked,t=cs?cs.checked:!1;Js.enabled=e&&!t}),V&&V.addEventListener(`change`,()=>{let e=Y&&Y.checked,t=V.checked;Os.forEach(e=>e.visible=t),K.environmentIntensity=ks&&t?0:.8,q.toneMappingExposure=ks&&t?As:1,X!==void 0&&(X.visible=!e&&(!ks||!t)),e&&J!==void 0&&(J.setScene(K,G),J.updateCamera())}),us&&Yo&&(us.addEventListener(`click`,()=>{document.fullscreenElement?document.exitFullscreen():Yo.requestFullscreen().catch(e=>{console.warn(`Error attempting to enable fullscreen: ${e.message}`)})}),document.addEventListener(`fullscreenchange`,()=>{setTimeout(()=>window.dispatchEvent(new Event(`resize`)),100)})),ds&&ds.addEventListener(`click`,()=>{vs=!0;let e=ds.innerText;ds.innerText=`✅`,setTimeout(()=>{ds.innerText=e},1e3)});function ec(){W&&(Ts=!1,W.paused=!0,rs.innerText=`▶ Play`,Is())}function tc(e){if(!U||!hs[e])return;W&&W.stop();let t=hs[e];W=U.clipAction(t),W.play(),W.time=0,U.update(0),Ts=!0,W.paused=!1,rs.innerText=`⏸ Pause`,as.value=0,Is()}is.addEventListener(`change`,e=>{tc(parseInt(e.target.value))}),rs.addEventListener(`click`,()=>{W&&(Ts=!Ts,W.paused=!Ts,rs.innerText=Ts?`⏸ Pause`:`▶ Play`,Is())}),as.addEventListener(`mousedown`,()=>{Es=!0,ec()}),as.addEventListener(`mouseup`,()=>{Es=!1}),as.addEventListener(`touchstart`,()=>{Es=!0,ec()},{passive:!0}),as.addEventListener(`touchend`,()=>{Es=!1},{passive:!0}),as.addEventListener(`input`,e=>{if(ec(),W){let t=W.getClip().duration;W.time=parseFloat(e.target.value)*t,U&&(U.update(0),Y&&Y.checked&&J!==void 0&&(Z!==void 0&&(Z.visible=!1),X!==void 0&&(X.visible=!1),Q!==void 0&&(Q.visible=!1),$!==void 0&&($.visible=!1),J.setScene(K,G),J.updateCamera()))}});function nc(){ys&&bs&&bs.state!==`inactive`&&(bs.stop(),ys=!1),H&&=(U&&=(U.stopAllAction(),U.uncacheRoot(U.getRoot()),null),W=null,K.remove(H),H.traverse(e=>{e.isMesh&&(e.geometry.dispose(),Array.isArray(e.material)?e.material.forEach(e=>e.dispose()):e.material&&e.material.dispose())}),null),Is()}function rc(e){return new Promise((t,n)=>{let r=null;H&&(U&&W&&(W.time=0,U.update(0),Y&&Y.checked&&J!==void 0&&(Z!==void 0&&(Z.visible=!1),X!==void 0&&(X.visible=!1),Q!==void 0&&(Q.visible=!1),$!==void 0&&($.visible=!1),J.setScene(K,G),J.updateCamera())),r=ke(H,hs)),nc();let i=e;e instanceof Uint8Array&&(i=e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)),new _e().parse(i,``,e=>{H=e.scene,hs=e.animations||[];let n=cs?cs.checked:!1;Js.enabled=(ls?ls.checked:!1)&&!n;let i=Ae(H);Ds=i.gltfCameras,Os=i.gltfLights,ks=i.sceneHasLights,As=i.optimalExposure||1;let a=document.getElementById(`lights-cb-label`);a&&(a.style.display=ks?`flex`:`none`);let o=!V||V.checked;Os.forEach(e=>e.visible=o),K.environmentIntensity=ks&&o?0:.8,q.toneMappingExposure=ks&&o?As:1;let s=!1;fs?Ds.length>0?(fs.style.display=`inline-block`,fs.innerHTML=`<option value="">Free Camera</option>`,Ds.forEach((e,t)=>{let n=document.createElement(`option`);n.value=t,n.innerText=e.name||`Camera ${t+1}`,fs.appendChild(n)}),fs.value=`0`,G=Ds[0],Xs.enabled=!1,s=!0):(fs.style.display=`none`,fs.value=``,G=Gs,Xs.enabled=!0):(G=Gs,Xs.enabled=!0);let c=B?B.clientWidth:window.innerWidth,l=B?B.clientHeight:window.innerHeight;Be(G,c,l),qs.camera=G;let u=document.getElementById(`anim-slider-row`);hs.length?(U=new x(H),s=!0,rs&&(rs.style.display=``),is&&(is.style.display=``),u&&(u.style.display=``),is.innerHTML=``,hs.forEach((e,t)=>{let n=document.createElement(`option`);n.value=t,n.innerText=e.name||`Animation ${t+1}`,is.appendChild(n)}),tc(0)):(rs&&(rs.style.display=`none`),is&&(is.style.display=`none`),u&&(u.style.display=`none`),W=null,Is()),ns&&(ns.style.display=s?`flex`:`none`),Te(H,n),K.add(H);let d=!0,f=null;if(r&&!r.isEmpty()&&(f=ke(H,hs),!f.isEmpty())){let e=.001;r.min.distanceTo(f.min)<e&&r.max.distanceTo(f.max)<e&&(d=!1)}if(d)typeof ic==`function`&&ic();else{if(f){let e=f.getSize(new k),t=Math.max(e.x,e.y,e.z)||10;Re(H,t*1.5)}let e=Y&&Y.checked,t=!ss||ss.checked;Z!==void 0&&(Z.visible=!e),X!==void 0&&(X.visible=!e&&(!ks||V&&!V.checked)),Q!==void 0&&(Q.visible=!e&&t),$!==void 0&&($.visible=!e&&t),J!==void 0&&(J.setScene(K,G),J.updateCamera())}t()},n)})}function ic(){if(!H)return;let e=ke(H,hs);if(e.isEmpty())return;Z.position.y=e.min.y-.01,Q.position.y=Z.position.y+.001,$.position.y=Z.position.y+.002;let{maxDim:t}=ue(Gs,Xs,Qs,K,e);Re(H,t*1.5);let n=Y&&Y.checked,r=!ss||ss.checked;Z!==void 0&&(Z.visible=!n),X!==void 0&&(X.visible=!n&&(!ks||V&&!V.checked)),Q!==void 0&&(Q.visible=!n&&r),$!==void 0&&($.visible=!n&&r),J!==void 0&&(J.setScene(K,G),J.updateCamera())}var ac=performance.now();function oc(){requestAnimationFrame(oc);let e=performance.now(),t=(e-ac)/1e3;if(ac=e,U&&(!(Y&&Y.checked)&&!ys&&U.update(t),W&&Ts&&!Es)){let e=W.getClip().duration;if(e>0){let t=W.time%e;t<0&&(t+=e),as.value=t/e}}Xs.update();let n=Y&&Y.checked,r=!ss||ss.checked;n?(Z!==void 0&&(Z.visible=!1),X!==void 0&&(X.visible=!1),Q!==void 0&&(Q.visible=!1),$!==void 0&&($.visible=!1),J!==void 0&&!ys&&J.renderSample()):(Z!==void 0&&(Z.visible=!0),X!==void 0&&(X.visible=X.visible=!ks||V&&!V.checked),Q!==void 0&&(Q.visible=r),$!==void 0&&($.visible=r),ys||Ks.render()),vs&&(vs=!1,q.domElement.toBlob(e=>{e&&xe(e,`${Ms()}.png`)},`image/png`))}oc(),window.addEventListener(`resize`,()=>{if(!B||ys)return;let e=B.clientWidth,t=B.clientHeight;Be(G,e,t),q.setSize(e,t),Ks.setSize(e,t),J!==void 0&&J.updateCamera()}),setTimeout(()=>window.dispatchEvent(new Event(`resize`)),100);var sc=[`localhost`,`127.0.0.1`,``].includes(window.location.hostname);z.placeholder=sc?`Enter OpenSCAD code here...`:Lo,(async function(){if(sc&&await Ws(Xo.value.trim(),!0),window.location.hash&&window.location.hash.length>1)try{let e=window.location.hash.substring(1),t=await Pe(e);t&&(z.value=t)}catch(e){console.error(`Failed to decode hash`,e)}Us(),setTimeout(()=>Ps(js()),500)})();