@naanouff/w3dts-lightmapper 0.0.0-stage → 0.0.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.
- package/README.md +12 -3
- package/dist/atlasPostprocess.d.ts +37 -0
- package/dist/atlasPostprocess.d.ts.map +1 -0
- package/dist/bakeGiBounceComputeWgsl.d.ts +7 -0
- package/dist/bakeGiBounceComputeWgsl.d.ts.map +1 -0
- package/dist/bakeLighting.d.ts +18 -0
- package/dist/bakeLighting.d.ts.map +1 -0
- package/dist/bakeLightmapComputeWgsl.d.ts +7 -0
- package/dist/bakeLightmapComputeWgsl.d.ts.map +1 -0
- package/dist/bakeSceneAccel.d.ts +143 -0
- package/dist/bakeSceneAccel.d.ts.map +1 -0
- package/dist/base64.d.ts +11 -0
- package/dist/base64.d.ts.map +1 -0
- package/dist/buildUvGBuffer.d.ts +25 -0
- package/dist/buildUvGBuffer.d.ts.map +1 -0
- package/dist/classifyStatic.d.ts +10 -0
- package/dist/classifyStatic.d.ts.map +1 -0
- package/dist/clearLightingBake.d.ts +22 -0
- package/dist/clearLightingBake.d.ts.map +1 -0
- package/dist/collectBakeLights.d.ts +73 -0
- package/dist/collectBakeLights.d.ts.map +1 -0
- package/dist/constants.d.ts +20 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/cpuIrradianceBake.d.ts +65 -0
- package/dist/cpuIrradianceBake.d.ts.map +1 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/lightmapperSceneExtension.d.ts +37 -0
- package/dist/lightmapperSceneExtension.d.ts.map +1 -0
- package/dist/packAtlasUv1.d.ts +36 -0
- package/dist/packAtlasUv1.d.ts.map +1 -0
- package/dist/packBakeLightsGpu.d.ts +5 -0
- package/dist/packBakeLightsGpu.d.ts.map +1 -0
- package/dist/planarUv1.d.ts +53 -0
- package/dist/planarUv1.d.ts.map +1 -0
- package/dist/prepareAtlas.d.ts +9 -0
- package/dist/prepareAtlas.d.ts.map +1 -0
- package/dist/probeVolume.d.ts +68 -0
- package/dist/probeVolume.d.ts.map +1 -0
- package/dist/restoreLightmapAtlas.d.ts +12 -0
- package/dist/restoreLightmapAtlas.d.ts.map +1 -0
- package/dist/runGpuBakeDirect.d.ts +28 -0
- package/dist/runGpuBakeDirect.d.ts.map +1 -0
- package/dist/runGpuBakeGiBounces.d.ts +17 -0
- package/dist/runGpuBakeGiBounces.d.ts.map +1 -0
- package/dist/softVisibility.d.ts +16 -0
- package/dist/softVisibility.d.ts.map +1 -0
- package/dist/types.d.ts +109 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/w3dts-lightmapper.es.js +3465 -0
- package/dist/w3dts-lightmapper.umd.js +855 -0
- package/package.json +49 -3
- package/shaders/bake_gi_bounce.compute.wgsl +255 -0
- package/shaders/bake_lightmap.compute.wgsl +599 -0
|
@@ -0,0 +1,855 @@
|
|
|
1
|
+
(function(H,I){typeof exports=="object"&&typeof module<"u"?I(exports,require("@naanouff/w3dts-core"),require("gl-matrix"),require("@naanouff/w3dts-path-tracer")):typeof define=="function"&&define.amd?define(["exports","@naanouff/w3dts-core","gl-matrix","@naanouff/w3dts-path-tracer"],I):(H=typeof globalThis<"u"?globalThis:H||self,I(H.W3dtsLightmapper={},H.W3dtsCore,H.glMatrix,H.W3dtsPathTracer))})(this,function(H,I,it,Ut){"use strict";const Te="@naanouff/w3dts-lightmapper",ie="1.0";function In(t,r){for(const e of t.getAllComponents(r)){const s=e.bodyType;if(typeof s=="string"&&s!=="fixed"&&s!=="static")return!0}return!1}function pe(t){var e,n,s,i,a,l;const r=[];for(const[o,[h]]of t.query([I.RenderableComponent]).iter()){if(!(h!=null&&h.visible)||t.hasComponent(o,I.EditorOnlyComponent)||t.hasComponent(o,I.StaticMergeChunkComponent)||t.hasComponent(o,I.SkinnedMeshComponent)||t.hasComponent(o,I.ComputeAnimationComponent)||t.hasComponent(o,I.WaveAnimationComponent)||t.hasComponent(o,I.GltfAnimationPlayerComponent)||!t.hasComponent(o,I.StaticComponent)&&In(t,o))continue;const d=h.mesh,f=h.material;!d||typeof d=="string"||!f||typeof f=="string"||f.shadingModel==="unlit"||((n=(e=f.properties)==null?void 0:e.get)==null?void 0:n.call(e,"bsdfKind"))===3||(i=(s=f.shaderGraph)==null?void 0:s.nodes)!=null&&i.some(u=>u.type==="UnlitMasterNode")||I.usesTransparentRenderQueue(f)||!((a=d.vertices)!=null&&a.length)||!((l=d.indices)!=null&&l.length)||r.push(o)}return r}function Ie(t,r){for(const e of r)t.hasComponent(e,I.StaticComponent)||t.addComponent(e,new I.StaticComponent)}const Rt=new WeakMap;function Pn(t){const r=I.STANDARD_MESH_VERTEX_FLOATS,e=Math.floor(t.length/r),n=new Float32Array(e*2);for(let s=0;s<e;s++){const i=s*r+I.STANDARD_MESH_ATTR.uv1;n[s*2]=t[i]??0,n[s*2+1]=t[i+1]??0}return n}function Un(t,r){const e=Rt.get(t);e&&Rt.set(r,{vertices:new Float32Array(e.vertices),indices:e.indices instanceof Uint16Array?new Uint16Array(e.indices):new Uint32Array(e.indices),hasAuthoredUv1:e.hasAuthoredUv1,morphPositionDeltaData:e.morphPositionDeltaData?new Float32Array(e.morphPositionDeltaData):void 0})}function Dn(t){const r=Rt.get(t);if(!r&&t.lightmapOriginalUv1==null){const l=new Float32Array(t.vertices),o=t.indices instanceof Uint16Array?new Uint16Array(t.indices):new Uint32Array(t.indices);Rt.set(t,{vertices:l,indices:o,hasAuthoredUv1:t.hasAuthoredUv1,morphPositionDeltaData:t.morphPositionDeltaData?new Float32Array(t.morphPositionDeltaData):void 0}),t.lightmapOriginalUv1=Pn(l),t.lightmapOriginalHasAuthoredUv1=t.hasAuthoredUv1;return}if(r){const l=new Float32Array(r.vertices),o=r.indices instanceof Uint16Array?new Uint16Array(r.indices):new Uint32Array(r.indices);t.updateData(l,o,t.name),r.morphPositionDeltaData&&(t.morphPositionDeltaData=new Float32Array(r.morphPositionDeltaData)),t.hasAuthoredUv1=r.hasAuthoredUv1;return}const e=I.STANDARD_MESH_VERTEX_FLOATS,n=t.vertices,s=Math.floor(n.length/e),i=t.lightmapOriginalUv1,a=Math.min(s,Math.floor(i.length/2));for(let l=0;l<a;l++){const o=l*e+I.STANDARD_MESH_ATTR.uv1;n[o]=i[l*2]??0,n[o+1]=i[l*2+1]??0}t.hasAuthoredUv1=t.lightmapOriginalHasAuthoredUv1??!1}function Rn(t){const r=Rt.get(t);if(r){const l=new Float32Array(r.vertices),o=r.indices instanceof Uint16Array?new Uint16Array(r.indices):new Uint32Array(r.indices);return t.updateData(l,o,t.name),r.morphPositionDeltaData&&(t.morphPositionDeltaData=new Float32Array(r.morphPositionDeltaData)),t.hasAuthoredUv1=r.hasAuthoredUv1,Rt.delete(t),t.lightmapOriginalUv1=null,t.lightmapOriginalHasAuthoredUv1=void 0,t.bakedGeometryDirty=!1,t.lightmapBakeRevision=(t.lightmapBakeRevision??0)+1,t.vertexBuffer=null,t.indexBuffer=null,!0}if(t.lightmapOriginalUv1==null)return!1;const e=I.STANDARD_MESH_VERTEX_FLOATS,n=t.vertices,s=Math.floor(n.length/e),i=t.lightmapOriginalUv1,a=Math.min(s,Math.floor(i.length/2));for(let l=0;l<a;l++){const o=l*e+I.STANDARD_MESH_ATTR.uv1;n[o]=i[l*2]??0,n[o+1]=i[l*2+1]??0}return t.hasAuthoredUv1=t.lightmapOriginalHasAuthoredUv1??!1,t.lightmapOriginalUv1=null,t.lightmapOriginalHasAuthoredUv1=void 0,t.bakedGeometryDirty=!1,t.lightmapBakeRevision=(t.lightmapBakeRevision??0)+1,t.vertexBuffer=null,t.indexBuffer=null,!0}function En(t,r,e){const n=Math.abs(t),s=Math.abs(r),i=Math.abs(e);return n>=s&&n>=i?t>=0?0:1:s>=n&&s>=i?r>=0?2:3:e>=0?4:5}function kn(t,r,e,n){switch(t){case 0:return[-n,e];case 1:return[n,e];case 2:return[r,-n];case 3:return[r,n];case 4:return[r,e];default:return[-r,e]}}function Pe(t,r=[1,1,1]){Dn(t);const e=Math.max(1e-5,Math.abs(r[0]??1)),n=Math.max(1e-5,Math.abs(r[1]??1)),s=Math.max(1e-5,Math.abs(r[2]??1)),i=I.STANDARD_MESH_VERTEX_FLOATS,a=t.vertices,l=t.indices,o=Math.floor(a.length/i),h=Math.floor(l.length/3);if(o<1||h<1)return{generated:!1,islands:[]};const d=new Float32Array(h);let f=0;for(let _=0;_<h;_++){const B=l[_*3]*i,p=l[_*3+1]*i,D=l[_*3+2]*i,R=a[B]*e,U=a[B+1]*n,S=a[B+2]*s,k=a[p]*e,G=a[p+1]*n,C=a[p+2]*s,J=a[D]*e,et=a[D+1]*n,nt=a[D+2]*s,rt=k-R,K=G-U,tt=C-S,Q=J-R,at=et-U,ut=nt-S,ct=K*ut-tt*at,ot=tt*Q-rt*ut,st=rt*at-K*Q,ft=.5*Math.hypot(ct,ot,st);d[_]=ft,f+=ft}if(t.hasAuthoredUv1){const _=new Uint32Array(o),B=new Float32Array(o*2);let p=1/0,D=1/0,R=-1/0,U=-1/0;for(let S=0;S<o;S++){_[S]=S;const k=S*i+I.STANDARD_MESH_ATTR.uv1,G=a[k],C=a[k+1];B[S*2]=G,B[S*2+1]=C,G<p&&(p=G),C<D&&(D=C),G>R&&(R=G),C>U&&(U=C)}return Number.isFinite(p)||(p=0,D=0,R=1,U=1),R-p<1e-6&&(R=p+1e-6),U-D<1e-6&&(U=D+1e-6),{generated:!1,islands:[{vertexIndices:_,metricUvs:B,u0:p,v0:D,u1:R,v1:U,worldArea:Math.max(f,1e-6)}]}}const u=new Int8Array(h);let c=1/0,m=1/0,b=1/0,y=-1/0,g=-1/0,v=-1/0;for(let _=0;_<o;_++){const B=_*i,p=a[B]*e,D=a[B+1]*n,R=a[B+2]*s;p<c&&(c=p),D<m&&(m=D),R<b&&(b=R),p>y&&(y=p),D>g&&(g=D),R>v&&(v=R)}const M=1e5/Math.max(1e-5,y-c,g-m,v-b);for(let _=0;_<h;_++){const B=l[_*3]*i,p=l[_*3+1]*i,D=l[_*3+2]*i,R=a[B]*e,U=a[B+1]*n,S=a[B+2]*s,k=a[p]*e,G=a[p+1]*n,C=a[p+2]*s,J=a[D]*e,et=a[D+1]*n,nt=a[D+2]*s;let rt=(G-U)*(nt-S)-(C-S)*(et-U),K=(C-S)*(J-R)-(k-R)*(nt-S),tt=(k-R)*(et-U)-(G-U)*(J-R);Math.hypot(rt,K,tt)<1e-11&&(rt=(a[B+I.STANDARD_MESH_ATTR.normal]??0)/e,K=(a[B+I.STANDARD_MESH_ATTR.normal+1]??1)/n,tt=(a[B+I.STANDARD_MESH_ATTR.normal+2]??0)/s),u[_]=En(rt,K,tt)}const A=new Int32Array(o),x=new Map;let L=0;for(let _=0;_<o;_++){const B=_*i,p=Math.round((a[B]*e-c)*M),D=Math.round((a[B+1]*n-m)*M),R=Math.round((a[B+2]*s-b)*M),U=`${p},${D},${R}`;let S=x.get(U);S===void 0&&(S=L++,x.set(U,S)),A[_]=S}const E=new Map;for(let _=0;_<h;_++){const B=u[_],p=A[l[_*3]],D=A[l[_*3+1]],R=A[l[_*3+2]],U=[p<D?[p,D]:[D,p],D<R?[D,R]:[R,D],R<p?[R,p]:[p,R]];for(const[S,k]of U){if(S===k)continue;const G=`${B}:${S}:${k}`;let C=E.get(G);C||(C=[],E.set(G,C)),C.push(_)}}const P=new Int32Array(h).fill(-1),O=[],X=[],w=[];for(let _=0;_<h;_++){if(P[_]!==-1)continue;const B=O.length,p=u[_];O.push(p),P[_]=B,w.length=0,w.push(_);let D=0,R=0;for(;R<w.length;){const U=w[R++];D+=d[U];const S=A[l[U*3]],k=A[l[U*3+1]],G=A[l[U*3+2]],C=[S<k?[S,k]:[k,S],k<G?[k,G]:[G,k],G<S?[G,S]:[S,G]];for(const[J,et]of C){if(J===et)continue;const nt=E.get(`${p}:${J}:${et}`);if(nt)for(const rt of nt)P[rt]===-1&&(P[rt]=B,w.push(rt))}}X.push(Math.max(D,1e-6))}const V=O.length,q=new Int32Array(o).fill(-1),$=new Map,z=new Uint32Array(h*3),W=[],F=new Array(o).fill(0);for(let _=0;_<h;_++){const B=P[_];for(let p=0;p<3;p++){const D=l[_*3+p],R=q[D];if(R===-1)q[D]=B,F[D]=B,z[_*3+p]=D;else if(R===B)z[_*3+p]=D;else{const U=`${D}:${B}`;let S=$.get(U);S===void 0&&(S=o+W.length,W.push(D),F.push(B),$.set(U,S)),z[_*3+p]=S}}}const N=o+W.length;if(W.length>0){const _=new Float32Array(N*i);_.set(a);for(let p=0;p<W.length;p++){const D=W[p]*i,R=(o+p)*i;_.set(a.subarray(D,D+i),R)}const B=N<=65535?new Uint16Array(z):z;if(t.updateData(_,B,t.name),t.morphPositionDeltaData&&t.morphTargetCount>0){const p=t.morphTargetCount*4,D=new Float32Array(N*p);D.set(t.morphPositionDeltaData);for(let R=0;R<W.length;R++){const U=W[R]*p,S=(o+R)*p;D.set(t.morphPositionDeltaData.subarray(U,U+p),S)}t.morphPositionDeltaData=D}}const Y=Array.from({length:V},()=>[]);for(let _=0;_<N;_++){const B=q[_]===-1&&_<o?0:F[_];B>=0&&B<V&&Y[B].push(_)}const Z=t.vertices,j=[];for(let _=0;_<V;_++){const B=Y[_];if(B.length===0)continue;const p=O[_],D=new Uint32Array(B),R=new Float32Array(B.length*2);let U=1/0,S=1/0,k=-1/0,G=-1/0;for(let C=0;C<B.length;C++){const J=B[C]*i,et=Z[J]*e,nt=Z[J+1]*n,rt=Z[J+2]*s,[K,tt]=kn(p,et,nt,rt);R[C*2]=K,R[C*2+1]=tt,K<U&&(U=K),tt<S&&(S=tt),K>k&&(k=K),tt>G&&(G=tt)}k-U<1e-6&&(k=U+1e-6),G-S<1e-6&&(G=S+1e-6),j.push({vertexIndices:D,metricUvs:R,u0:U,v0:S,u1:k,v1:G,worldArea:X[_]})}return t.hasAuthoredUv1=!0,t.bakedGeometryDirty=!0,t.vertexBuffer=null,t.indexBuffer=null,{generated:!0,islands:j}}function Bn(t,r){const{generated:e,islands:n}=Pe(t,r==null?void 0:r.worldScale);if(!e||n.length===0)return!1;const s=I.STANDARD_MESH_VERTEX_FLOATS,i=t.vertices;if(n.length===1){const g=n[0],v=Math.max(1e-6,g.u1-g.u0),T=Math.max(1e-6,g.v1-g.v0);for(let M=0;M<g.vertexIndices.length;M++){const A=g.vertexIndices[M]*s+I.STANDARD_MESH_ATTR.uv1;i[A]=(g.metricUvs[M*2]-g.u0)/v,i[A+1]=(g.metricUvs[M*2+1]-g.v0)/T}return!0}const a=n.map((g,v)=>({isl:g,idx:v,w:g.u1-g.u0,h:g.v1-g.v0})).sort((g,v)=>v.h-g.h||v.w-g.w||g.idx-v.idx),l=a.reduce((g,v)=>g+v.w*v.h,0),o=Math.max(Math.sqrt(l*1.35),...a.map(g=>Math.max(g.w,g.h)*1.08),1e-5),h=o*.02;let d=h,f=h,u=0,c=h,m=h;const b=[];for(const g of a)d+g.w+h>o&&d>h&&(d=h,f+=u+h,u=0),b.push({x:d,y:f}),d+=g.w+h,u=Math.max(u,g.h),c=Math.max(c,d),m=Math.max(m,f+u+h);const y=Math.max(c,m,1e-5);for(let g=0;g<a.length;g++){const{isl:v}=a[g],{x:T,y:M}=b[g];for(let A=0;A<v.vertexIndices.length;A++){const x=v.vertexIndices[A]*s+I.STANDARD_MESH_ATTR.uv1;i[x]=(T+(v.metricUvs[A*2]-v.u0))/y,i[x+1]=(M+(v.metricUvs[A*2+1]-v.v0))/y}}return!0}function Ln(t){const r=I.STANDARD_MESH_VERTEX_FLOATS,e=t.vertices,n=t.indices;let s=0;for(let i=0;i+2<n.length;i+=3){const a=n[i]*r+I.STANDARD_MESH_ATTR.uv1,l=n[i+1]*r+I.STANDARD_MESH_ATTR.uv1,o=n[i+2]*r+I.STANDARD_MESH_ATTR.uv1,h=e[a],d=e[a+1],f=e[l],u=e[l+1],c=e[o],m=e[o+1];s+=.5*Math.abs((f-h)*(m-d)-(c-h)*(u-d))}return Math.max(s,1e-6)}function Ue(t,r){const e=(r==null?void 0:r.atlasSize)??2048,n=Math.max(4,(r==null?void 0:r.padding)??4);let s=(r==null?void 0:r.texelsPerUnit)??40;const i=[],a=t.map((u,c)=>{const m=u.worldScale??[1,1,1],{generated:b,islands:y}=Pe(u.mesh,m);let g=0;for(let v=0;v<y.length;v++){const T=y[v];g+=T.worldArea;let M,A;if(b)M=Math.max(.001,T.u1-T.u0),A=Math.max(.001,T.v1-T.v0);else{const x=Math.max(1e-6,T.u1-T.u0),L=Math.max(1e-6,T.v1-T.v0),E=x*L,P=Ln(u.mesh)/E,O=Math.min(1,Math.max(.15,P)),X=T.worldArea/O,w=Math.min(20,Math.max(.05,x/L));M=Math.max(.001,Math.sqrt(X*w)),A=Math.max(.001,X/M)}i.push({entityOrder:c,islandIdx:v,isl:T,worldW:M,worldH:A,px:0,py:0,pw:0,ph:0})}return{mesh:u.mesh,entity:u.entity,surfaceArea:Math.max(g,1e-6),u0:0,v0:0,u1:1,v1:1,px:0,py:0,pw:0,ph:0,uvScaleOffset:[1,1,0,0]}});if(i.length===0)return{charts:a,texelsPerUnit:s,atlasSize:e};const l=()=>{for(const y of i)if(y.pw=Math.max(3,Math.ceil(y.worldW*s)),y.ph=Math.max(3,Math.ceil(y.worldH*s)),y.pw>e-2*n||y.ph>e-2*n)return!1;const u=[...i].sort((y,g)=>g.ph-y.ph||g.pw-y.pw||y.entityOrder-g.entityOrder||y.islandIdx-g.islandIdx);let c=n,m=n,b=0;for(const y of u){if(c+y.pw>e-n&&(c=n,m+=b+n,b=0),m+y.ph>e-n)return!1;y.px=c,y.py=m,c+=y.pw+n,b=Math.max(b,y.ph)}return!0};for(;!l()&&s>2;)s*=.85;if(!l()){const u=Math.max(1,i.length),c=Math.ceil(Math.sqrt(u)),m=Math.max(3,Math.floor((e-n*(c+1))/c));i.forEach((b,y)=>{const g=y%c,v=Math.floor(y/c);b.px=n+g*(m+n),b.py=n+v*(m+n),b.pw=m,b.ph=m})}const o=I.STANDARD_MESH_VERTEX_FLOATS,h=1/e;for(const u of a)u.px=e,u.py=e,u.pw=0,u.ph=0,u.u0=1,u.v0=1,u.u1=0,u.v1=0;const d=new Int32Array(a.length),f=new Int32Array(a.length);for(const u of i){const c=a[u.entityOrder],m=c.mesh.vertices,b=u.isl,y=Math.max(1e-6,b.u1-b.u0),g=Math.max(1e-6,b.v1-b.v0),v=Math.max(1,u.pw-1),T=Math.max(1,u.ph-1);for(let M=0;M<b.vertexIndices.length;M++){const x=b.vertexIndices[M]*o+I.STANDARD_MESH_ATTR.uv1,L=(b.metricUvs[M*2]-b.u0)/y,E=(b.metricUvs[M*2+1]-b.v0)/g,P=(u.px+.5+L*v)*h,O=(u.py+.5+E*T)*h;m[x]=P,m[x+1]=O,P<c.u0&&(c.u0=P),O<c.v0&&(c.v0=O),P>c.u1&&(c.u1=P),O>c.v1&&(c.v1=O)}u.px<c.px&&(c.px=u.px),u.py<c.py&&(c.py=u.py),u.px+u.pw>d[u.entityOrder]&&(d[u.entityOrder]=u.px+u.pw),u.py+u.ph>f[u.entityOrder]&&(f[u.entityOrder]=u.py+u.ph)}for(let u=0;u<a.length;u++){const c=a[u];d[u]>c.px?(c.pw=d[u]-c.px,c.ph=f[u]-c.py):(c.px=0,c.py=0),c.uvScaleOffset=[1,1,0,0],c.mesh.bakedGeometryDirty=!0,c.mesh.lightmapBakeRevision=(c.mesh.lightmapBakeRevision??0)+1,c.mesh.vertexBuffer=null,c.mesh.indexBuffer=null}return{charts:a,texelsPerUnit:s,atlasSize:e}}function On(t,r){const e=t.indices instanceof Uint16Array?new Uint16Array(t.indices):new Uint32Array(t.indices),n=new I.Mesh(new Float32Array(t.vertices),e,`${t.name}_lm_${r}`);return n.hasAuthoredUv1=t.hasAuthoredUv1,n.lightmapOriginalUv1=t.lightmapOriginalUv1?new Float32Array(t.lightmapOriginalUv1):void 0,n.lightmapOriginalHasAuthoredUv1=t.lightmapOriginalHasAuthoredUv1,Un(t,n),n.primitiveSource=t.primitiveSource?{type:t.primitiveSource.type,params:t.primitiveSource.params?{...t.primitiveSource.params}:void 0}:void 0,n.brepFaceStepIdPerTriangle=t.brepFaceStepIdPerTriangle?new Uint32Array(t.brepFaceStepIdPerTriangle):void 0,n.morphPositionDeltaData=t.morphPositionDeltaData?new Float32Array(t.morphPositionDeltaData):void 0,n.morphTargetCount=t.morphTargetCount,n}function wn(t,r){const e=t.getComponent(r,I.TransformComponent);if(!e)return[1,1,1];const n=e.worldTransform,s=Math.hypot(n[0]??1,n[1]??0,n[2]??0),i=Math.hypot(n[4]??0,n[5]??1,n[6]??0),a=Math.hypot(n[8]??0,n[9]??0,n[10]??1),l=Math.max(1e-5,Math.abs(e.scale[0]??1)),o=Math.max(1e-5,Math.abs(e.scale[1]??1)),h=Math.max(1e-5,Math.abs(e.scale[2]??1));return Math.abs(s-1)<1e-5&&Math.abs(i-1)<1e-5&&Math.abs(a-1)<1e-5&&(Math.abs(l-1)>1e-4||Math.abs(o-1)>1e-4||Math.abs(h-1)>1e-4)?[l,o,h]:[Number.isFinite(s)&&s>1e-5?s:l,Number.isFinite(i)&&i>1e-5?i:o,Number.isFinite(a)&&a>1e-5?a:h]}function De(t,r,e){const n=pe(t);Ie(t,n);const s=new Map;for(const[h,[d]]of t.query([I.RenderableComponent]).iter()){if(t.hasComponent(h,I.StaticMergeChunkComponent))continue;const f=d==null?void 0:d.mesh;!f||typeof f=="string"||s.set(f,(s.get(f)??0)+1)}const i=new Set,a=[];for(const h of n){const d=t.getComponent(h,I.RenderableComponent),f=d==null?void 0:d.mesh;if(!d||!f||typeof f=="string")continue;let u=f;((s.get(f)??0)>1||i.has(f))&&(u=On(f,h),d.mesh=u),i.add(u),a.push({entity:h,mesh:u,worldScale:wn(t,h)})}const l=Ue(a,{atlasSize:(e==null?void 0:e.atlasSize)??2048,padding:(e==null?void 0:e.padding)??4,texelsPerUnit:e==null?void 0:e.texelsPerUnit});for(const h of l.charts){const d=t.getComponent(h.entity,I.LightmapInstanceComponent);d?(d.atlasIndex=0,d.uvScaleOffset=[...h.uvScaleOffset]):t.addComponent(h.entity,new I.LightmapInstanceComponent(0,[...h.uvScaleOffset])),r&&r.uploadMesh(h.mesh)}I.invalidateStaticMergeRenderCaches();const o=l.charts.map(h=>({entity:h.entity,atlasIndex:0,uvScaleOffset:h.uvScaleOffset,surfaceArea:h.surfaceArea,mesh:h.mesh,px:h.px,py:h.py,pw:h.pw,ph:h.ph}));return{atlasSize:l.atlasSize,texelsPerUnit:l.texelsPerUnit,entities:o.map(h=>({entity:h.entity,atlasIndex:h.atlasIndex,uvScaleOffset:h.uvScaleOffset,surfaceArea:h.surfaceArea})),charts:o}}function Nn(t){let r="";for(let e=0;e<t.length;e++)r+=String.fromCharCode(t[e]);return btoa(r)}function Gn(t){const r=atob(t),e=new Uint8Array(r.length);for(let n=0;n<r.length;n++)e[n]=r.charCodeAt(n);return e}function oe(t){return Nn(new Uint8Array(t.buffer,t.byteOffset,t.byteLength))}function Re(t){const r=Gn(t),e=new Uint8Array(r.byteLength);return e.set(r),new Uint32Array(e.buffer)}function Et(t){const r=Math.min(1,Math.max(0,t));return r<=.04045?r/12.92:Math.pow((r+.055)/1.055,2.4)}const zn=.6180339887498949,kt=.92;function Nt(t){let r=t>>>0;return r^=r>>>16,r=Math.imul(r,2146121005)>>>0,r^=r>>>15,r=Math.imul(r,2221713035)>>>0,r^=r>>>16,[(r&65535)/65536,(r>>>16&65535)/65536]}function Gt(t,r,e=0){let n=(Math.imul(t|0,668265261)^Math.imul(r|0,374761393)^Math.imul(e|0,2654435769))>>>0;return n^=n>>>15,n=Math.imul(n,739982445)>>>0,n^=n>>>12,n>>>0}function Vt(t,r,e){if(e||r+19>=t.length)return[1,1,1];const n=t[r+I.STANDARD_MESH_ATTR.color]??0,s=t[r+I.STANDARD_MESH_ATTR.color+1]??0,i=t[r+I.STANDARD_MESH_ATTR.color+2]??0,a=t[r+I.STANDARD_MESH_ATTR.color+3]??0;return n===0&&s===0&&i===0&&a===0?[1,1,1]:[Number.isFinite(n)?Math.max(0,n):1,Number.isFinite(s)?Math.max(0,s):1,Number.isFinite(i)?Math.max(0,i):1]}function Cn(t,r){var s,i,a,l,o;if(!t||typeof t=="string")return null;if(r){const h=r(t);if(h&&h.width>0&&h.height>0&&h.data.length>=4)return h}const e=(i=(s=t.properties)==null?void 0:s.get)==null?void 0:i.call(s,"baseColorPixels");if(e&&e.width>0&&e.height>0&&((a=e.data)==null?void 0:a.length)>=4)return e;const n=((o=(l=t.properties)==null?void 0:l.get)==null?void 0:o.call(l,"diffuseTexture"))??t.diffuseTexture;if(n&&typeof n=="object"){if(typeof n.width=="number"&&typeof n.height=="number"&&n.data&&n.data.length>=n.width*n.height*4)return n;if(n.imageData&&n.imageData.width>0&&n.imageData.height>0)return n.imageData}return null}function ce(t,r,e){const n=Math.max(1,t.width|0),s=Math.max(1,t.height|0),i=(r%1+1)%1,a=(e%1+1)%1,l=i*n-.5,o=a*s-.5,h=(Math.floor(l)%n+n)%n,d=(Math.floor(o)%s+s)%s,f=(h+1)%n,u=(d+1)%s,c=l-Math.floor(l),m=o-Math.floor(o),b=(O,X)=>{const w=(X*n+O)*4,V=(t.data[w+3]??255)>1.0001||(t.data[w]??0)>1.0001?1/255:1,q=Math.min(1,Math.max(0,(t.data[w]??255)*V)),$=Math.min(1,Math.max(0,(t.data[w+1]??255)*V)),z=Math.min(1,Math.max(0,(t.data[w+2]??255)*V));return[Et(q),Et($),Et(z)]},y=t.data instanceof Uint8Array||t.data instanceof Uint8ClampedArray,g=(O,X)=>{if(!y)return b(O,X);const w=(X*n+O)*4;return[Et((t.data[w]??255)/255),Et((t.data[w+1]??255)/255),Et((t.data[w+2]??255)/255)]},v=g(h,d),T=g(f,d),M=g(h,u),A=g(f,u),x=(1-c)*(1-m),L=c*(1-m),E=(1-c)*m,P=c*m;return[v[0]*x+T[0]*L+M[0]*E+A[0]*P,v[1]*x+T[1]*L+M[1]*E+A[1]*P,v[2]*x+T[2]*L+M[2]*E+A[2]*P]}function Ee(t,r,e){var u,c,m,b,y,g;const n=t.getComponent(r,I.RenderableComponent),s=n==null?void 0:n.material;if(!s||typeof s=="string")return{baseDiffuse:[.65,.65,.65],emissive:[0,0,0],usesMeshMaps:!1,srgbImg:null};const i=(c=(u=s.properties)==null?void 0:u.get)==null?void 0:c.call(u,"baseColor"),a=Array.isArray(i)||ArrayBuffer.isView(i)?i:s.diffuseColor,l=(b=(m=s.properties)==null?void 0:m.get)==null?void 0:b.call(m,"metallic"),h=1-Math.min(1,Math.max(0,typeof l=="number"?l:Number(s.metallic??0))),d=(g=(y=s.properties)==null?void 0:y.get)==null?void 0:g.call(y,"emissiveColor"),f=Array.isArray(d)||ArrayBuffer.isView(d)?d:s.emissiveColor;return{baseDiffuse:[Math.max(0,Number((a==null?void 0:a[0])??.65))*h,Math.max(0,Number((a==null?void 0:a[1])??.65))*h,Math.max(0,Number((a==null?void 0:a[2])??.65))*h],emissive:[Math.max(0,Number((f==null?void 0:f[0])??0)),Math.max(0,Number((f==null?void 0:f[1])??0)),Math.max(0,Number((f==null?void 0:f[2])??0))],usesMeshMaps:Ut.materialUsesMeshMaps(s),srgbImg:Cn(s,e==null?void 0:e.resolveBaseColorPixels)}}function ke(t,r,e,n){const s=I.STANDARD_MESH_VERTEX_FLOATS,i=e.vertices,a=Math.floor(i.length/s),l=new Float32Array(a*3),{baseDiffuse:o,usesMeshMaps:h,srgbImg:d}=Ee(t,r,n);for(let f=0;f<a;f++){const u=f*s,c=Vt(i,u,h);let m=1,b=1,y=1;if(d){const g=i[u+I.STANDARD_MESH_ATTR.uv0]??0,v=i[u+I.STANDARD_MESH_ATTR.uv0+1]??0;[m,b,y]=ce(d,g,v)}l[f*3]=Math.min(kt,o[0]*c[0]*m),l[f*3+1]=Math.min(kt,o[1]*c[1]*b),l[f*3+2]=Math.min(kt,o[2]*c[2]*y)}return l}function Fn(t){const r=new DataView(t.nodes),e=[];function n(s){const i=s*32,a=r.getFloat32(i,!0),l=r.getFloat32(i+4,!0),o=r.getFloat32(i+8,!0),h=r.getUint32(i+12,!0),d=r.getFloat32(i+16,!0),f=r.getFloat32(i+20,!0),u=r.getFloat32(i+24,!0),c=r.getUint32(i+28,!0),m=e.length;if(Ut.isPathTracerTriangleLeaf(h)){const g=h&2147483647;return e.push({min:[a,l,o],max:[d,f,u],left:-1,right:-1,triStart:c,triCount:g}),m}e.push({min:[a,l,o],max:[d,f,u],left:0,right:0,triStart:0,triCount:0});const b=n(h),y=n(c);return e[m].left=b,e[m].right=y,m}return n(t.rootIndex),e}function Be(t,r,e){const n=[],s=[],i=[],a=[];for(const M of r){const A=t.getComponent(M.entity,I.TransformComponent),x=(A==null?void 0:A.worldTransform)??it.mat4.create(),L=Ee(t,M.entity,e);Hn(M.mesh,x,n,s,L,i,a)}const l=Math.floor(n.length/9);if(l<1)return null;const o=new Float32Array(n),h=new Float32Array(s),d=new Float32Array(i),f=new Float32Array(a),u=new Uint8Array(l),c=new Float32Array(l*Ut.PATH_TRACER_FLOATS_PER_TRIANGLE);for(let M=0;M<l;M++){const A=M*Ut.PATH_TRACER_FLOATS_PER_TRIANGLE,x=M*9;for(let X=0;X<9;X++)c[A+X]=o[x+X];const L=M*3,E=f[L],P=f[L+1],O=f[L+2];c[A+53]=E,c[A+54]=P,c[A+55]=O,Math.max(E,P,O)>1e-4&&(u[M]=1)}const m=Ut.buildEmissiveCatalog(c,l,null),b=c.slice(),y=Ut.buildPathTracerBvhFromTriangles(b,l);if(!y)return null;const g=y.permutation??new Uint32Array(l);if(!y.permutation)for(let M=0;M<l;M++)g[M]=M;const v=new Uint32Array(l);for(let M=0;M<l;M++)v[g[M]]=M;const T=Fn(y);return{tris:o,triUv1:h,triOrder:g,origToLeafOrder:v,nodes:T,root:0,albedo:d,emissive:f,isEmissiveTri:u,emissiveCatalog:m,bounceSrc:new Float32Array(l*3)}}function Hn(t,r,e,n,s,i,a){const l=I.STANDARD_MESH_VERTEX_FLOATS,o=t.vertices,h=t.indices,d=it.vec3.create(),f=it.vec3.create(),u=it.vec3.create(),c=1/3;for(let m=0;m+2<h.length;m+=3){const b=h[m]*l,y=h[m+1]*l,g=h[m+2]*l;it.vec3.set(d,o[b],o[b+1],o[b+2]),it.vec3.set(f,o[y],o[y+1],o[y+2]),it.vec3.set(u,o[g],o[g+1],o[g+2]),it.vec3.transformMat4(d,d,r),it.vec3.transformMat4(f,f,r),it.vec3.transformMat4(u,u,r),e.push(d[0],d[1],d[2],f[0],f[1],f[2],u[0],u[1],u[2]),n.push(o[b+I.STANDARD_MESH_ATTR.uv1]??0,o[b+I.STANDARD_MESH_ATTR.uv1+1]??0,o[y+I.STANDARD_MESH_ATTR.uv1]??0,o[y+I.STANDARD_MESH_ATTR.uv1+1]??0,o[g+I.STANDARD_MESH_ATTR.uv1]??0,o[g+I.STANDARD_MESH_ATTR.uv1+1]??0);const v=Vt(o,b,s.usesMeshMaps),T=Vt(o,y,s.usesMeshMaps),M=Vt(o,g,s.usesMeshMaps),A=(v[0]+T[0]+M[0])*c,x=(v[1]+T[1]+M[1])*c,L=(v[2]+T[2]+M[2])*c;let E=1,P=1,O=1;if(s.srgbImg){const X=((o[b+I.STANDARD_MESH_ATTR.uv0]??0)+(o[y+I.STANDARD_MESH_ATTR.uv0]??0)+(o[g+I.STANDARD_MESH_ATTR.uv0]??0))*c,w=((o[b+I.STANDARD_MESH_ATTR.uv0+1]??0)+(o[y+I.STANDARD_MESH_ATTR.uv0+1]??0)+(o[g+I.STANDARD_MESH_ATTR.uv0+1]??0))*c;[E,P,O]=ce(s.srgbImg,X,w)}i.push(Math.min(kt,s.baseDiffuse[0]*A*E),Math.min(kt,s.baseDiffuse[1]*x*P),Math.min(kt,s.baseDiffuse[2]*L*O)),a.push(s.emissive[0],s.emissive[1],s.emissive[2])}}const lt=new Int32Array(128),Bt={t:0,u:0,v:0};function qt(t,r,e,n,s=-1){const i=r[0],a=r[1],l=r[2],o=e[0],h=e[1],d=e[2];if(Dt(i,a,l,o,h,d,t.nodes[t.root].min,t.nodes[t.root].max,n)<0)return!1;let f=0;for(lt[f++]=t.root;f>0;){const u=lt[--f],c=t.nodes[u];if(c.left<0){for(let v=0;v<c.triCount;v++){const T=t.triOrder[c.triStart+v];if(T===s)continue;const M=T*9;if(Oe(i,a,l,o,h,d,t.tris[M],t.tris[M+1],t.tris[M+2],t.tris[M+3],t.tris[M+4],t.tris[M+5],t.tris[M+6],t.tris[M+7],t.tris[M+8],Bt)&&Bt.t<n)return!0}continue}const m=t.nodes[c.left],b=t.nodes[c.right],y=Dt(i,a,l,o,h,d,m.min,m.max,n),g=Dt(i,a,l,o,h,d,b.min,b.max,n);y>=0&&g>=0?y<=g?(lt[f++]=c.right,lt[f++]=c.left):(lt[f++]=c.left,lt[f++]=c.right):y>=0?lt[f++]=c.left:g>=0&&(lt[f++]=c.right)}return!1}function Yt(t,r,e,n,s,i=!1){const a=r[0],l=r[1],o=r[2],h=e[0],d=e[1],f=e[2];let u=n,c=-1,m=0,b=0,y=0,g=1,v=0;if(Dt(a,l,o,h,d,f,t.nodes[t.root].min,t.nodes[t.root].max,u)<0)return null;let T=0;for(lt[T++]=t.root;T>0;){const M=lt[--T],A=t.nodes[M];if(Dt(a,l,o,h,d,f,A.min,A.max,u)<0)continue;if(A.left<0){for(let O=0;O<A.triCount;O++){const X=t.triOrder[A.triStart+O];if(i&&t.isEmissiveTri[X]!==0)continue;const w=X*9,V=t.tris[w],q=t.tris[w+1],$=t.tris[w+2],z=t.tris[w+3],W=t.tris[w+4],F=t.tris[w+5],N=t.tris[w+6],Y=t.tris[w+7],Z=t.tris[w+8];if(Oe(a,l,o,h,d,f,V,q,$,z,W,F,N,Y,Z,Bt)){const j=Bt.t;if(j>1e-4&&j<u){u=j,c=X,m=Bt.u,b=Bt.v;const _=z-V,B=W-q,p=F-$,D=N-V,R=Y-q,U=Z-$;let S=B*U-p*R,k=p*D-_*U,G=_*R-B*D;const C=Math.hypot(S,k,G)||1;S/=C,k/=C,G/=C,S*h+k*d+G*f>0&&(S=-S,k=-k,G=-G),y=S,g=k,v=G}}}continue}const x=t.nodes[A.left],L=t.nodes[A.right],E=Dt(a,l,o,h,d,f,x.min,x.max,u),P=Dt(a,l,o,h,d,f,L.min,L.max,u);E>=0&&P>=0?E<=P?(lt[T++]=A.right,lt[T++]=A.left):(lt[T++]=A.left,lt[T++]=A.right):E>=0?lt[T++]=A.left:P>=0&&(lt[T++]=A.right)}return c<0?null:s?(s.t=u,s.triIndex=c,s.baryU=m,s.baryV=b,s.normal[0]=y,s.normal[1]=g,s.normal[2]=v,s):{t:u,triIndex:c,baryU:m,baryV:b,normal:[y,g,v]}}function Le(t,r,e,n,s=!0,i=1){const a=t.emissiveCatalog.count,l=t.emissiveCatalog.totalPower;if(a===0||l<=0||n<1)return[0,0,0];const o=new DataView(t.emissiveCatalog.bytes),h=.0015,d=[r[0]+e[0]*h,r[1]+e[1]*h,r[2]+e[2]*h],[f,u]=Nt(i^1831565813);let c=0,m=0,b=0;for(let g=0;g<n;g++){const v=Math.min(.999999,(g+f)/n);let T=0,M=a-1;for(;T<M;){const mt=T+M>>>1,It=o.getFloat32(16+mt*16+8,!0);v<=It?M=mt:T=mt+1}const A=16+T*16,x=o.getUint32(A,!0);if(o.getFloat32(A+12,!0)<=1e-10)continue;const E=le(u+g*.7548776662466927),P=le(f*.713+g*.5698402909980532),O=Math.sqrt(E),X=1-O,w=P*O,V=1-X-w,q=x*9,$=t.tris[q],z=t.tris[q+1],W=t.tris[q+2],F=t.tris[q+3],N=t.tris[q+4],Y=t.tris[q+5],Z=t.tris[q+6],j=t.tris[q+7],_=t.tris[q+8],B=$*X+F*w+Z*V,p=z*X+N*w+j*V,D=W*X+Y*w+_*V,R=B-r[0],U=p-r[1],S=D-r[2],k=R*R+U*U+S*S;if(k<=1e-8)continue;const C=1/Math.sqrt(k),J=R*C,et=U*C,nt=S*C,rt=e[0]*J+e[1]*et+e[2]*nt;if(rt<=.0015)continue;const K=F-$,tt=N-z,Q=Y-W,at=Z-$,ut=j-z,ct=_-W;let ot=tt*ct-Q*ut,st=Q*at-K*ct,ft=K*ut-tt*at;const ht=Math.hypot(ot,st,ft);if(ht<=1e-10)continue;ot/=ht,st/=ht,ft/=ht;const bt=-(ot*J+st*et+ft*nt);if(bt<=1e-4)continue;const vt=x*3,At=t.emissive[vt],_t=t.emissive[vt+1],gt=t.emissive[vt+2],St=.2126*At+.7152*_t+.0722*gt;if(St<=1e-7)continue;if(s){const mt=B-d[0],It=p-d[1],ne=D-d[2],Pt=Math.hypot(mt,It,ne);if(Pt>.002&&qt(t,d,[mt/Pt,It/Pt,ne/Pt],Pt-.002,x))continue}const yt=rt*bt*l/(Math.PI*St*Math.max(k,.0025));c+=At*yt,m+=_t*yt,b+=gt*yt}const y=1/n;return[c*y,m*y,b*y]}function Xt(t,r,e,n,s=20,i){if(n<1)return[0,0,0];const a=.0015,l=[r[0]+e[0]*a,r[1]+e[1]*a,r[2]+e[2]*a],[o,h]=(i==null?void 0:i.jitterSeed)!==void 0?Nt(i.jitterSeed):[.5,0],d=i==null?void 0:i.atlasRgba,f=(i==null?void 0:i.atlasSize)??0,u=i==null?void 0:i.skyRadiance;let c=0,m=0,b=0;const y={t:0,triIndex:-1,baryU:0,baryV:0,normal:[0,1,0]};for(let v=0;v<n;v++){const T=Ne(e,v,n,o,h),M=Yt(t,l,T,s,y);if(!M){if(u){const z=typeof u=="function"?u(T):u;c+=z[0],m+=z[1],b+=z[2]}continue}const A=M.triIndex*3,x=t.albedo[A],L=t.albedo[A+1],E=t.albedo[A+2];let P=t.bounceSrc[A],O=t.bounceSrc[A+1],X=t.bounceSrc[A+2];if(d&&f>0){const z=M.triIndex*6,W=1-M.baryU-M.baryV,F=W*t.triUv1[z]+M.baryU*t.triUv1[z+2]+M.baryV*t.triUv1[z+4],N=W*t.triUv1[z+1]+M.baryU*t.triUv1[z+3]+M.baryV*t.triUv1[z+5],Y=Math.min(f-1,Math.max(0,Math.floor(F*f))),j=(Math.min(f-1,Math.max(0,Math.floor(N*f)))*f+Y)*4;(d[j+3]??0)>0&&(P=d[j],O=d[j+1],X=d[j+2])}const w=x*P,V=L*O,q=E*X,$=Math.max(w,V,q);if($>2.5){const W=(2.5+($-2.5)/(1+($-2.5)*.5))/$;c+=w*W,m+=V*W,b+=q*W}else c+=w,m+=V,b+=q}const g=1/n;return[c*g,m*g,b*g]}function Vn(t,r,e,n){const s=e.indices,i=Math.floor(s.length/3);for(let a=0;a<i;a++){const l=s[a*3],o=s[a*3+1],h=s[a*3+2],d=(r+a)*3;t.bounceSrc[d]=(n[l*3]+n[o*3]+n[h*3])/3,t.bounceSrc[d+1]=(n[l*3+1]+n[o*3+1]+n[h*3+1])/3,t.bounceSrc[d+2]=(n[l*3+2]+n[o*3+2]+n[h*3+2])/3}return i}function zt(t,r,e){const n=Math.floor(t.tris.length/9);for(let s=0;s<n;s++){const i=s*6,a=(t.triUv1[i]+t.triUv1[i+2]+t.triUv1[i+4])/3,l=(t.triUv1[i+1]+t.triUv1[i+3]+t.triUv1[i+5])/3,o=Math.min(e-1,Math.max(0,Math.floor(a*e))),d=(Math.min(e-1,Math.max(0,Math.floor(l*e)))*e+o)*4;if((r[d+3]??0)>0){const f=s*3;t.bounceSrc[f]=r[d],t.bounceSrc[f+1]=r[d+1],t.bounceSrc[f+2]=r[d+2]}}}function Dt(t,r,e,n,s,i,a,l,o){let h=0,d=o;if(Math.abs(n)<1e-12){if(t<a[0]||t>l[0])return-1}else{const f=1/n;let u=(a[0]-t)*f,c=(l[0]-t)*f;if(u>c){const m=u;u=c,c=m}if(u>h&&(h=u),c<d&&(d=c),h>d)return-1}if(Math.abs(s)<1e-12){if(r<a[1]||r>l[1])return-1}else{const f=1/s;let u=(a[1]-r)*f,c=(l[1]-r)*f;if(u>c){const m=u;u=c,c=m}if(u>h&&(h=u),c<d&&(d=c),h>d)return-1}if(Math.abs(i)<1e-12){if(e<a[2]||e>l[2])return-1}else{const f=1/i;let u=(a[2]-e)*f,c=(l[2]-e)*f;if(u>c){const m=u;u=c,c=m}if(u>h&&(h=u),c<d&&(d=c),h>d)return-1}return h}function Oe(t,r,e,n,s,i,a,l,o,h,d,f,u,c,m,b){const y=h-a,g=d-l,v=f-o,T=u-a,M=c-l,A=m-o,x=s*A-i*M,L=i*T-n*A,E=n*M-s*T,P=y*x+g*L+v*E;if(P>-1e-8&&P<1e-8)return!1;const O=1/P,X=t-a,w=r-l,V=e-o,q=O*(X*x+w*L+V*E);if(q<0||q>1)return!1;const $=w*v-V*g,z=V*y-X*v,W=X*g-w*y,F=O*(n*$+s*z+i*W);if(F<0||q+F>1)return!1;const N=O*(T*$+M*z+A*W);return N<=1e-4?!1:(b.t=N,b.u=q,b.v=F,!0)}const qn={t:0,triIndex:-1,baryU:0,baryV:0,normal:[0,1,0]};function we(t,r,e,n=4,s=.4,i){if(n<1||s<=1e-4)return 1;const a=.0015,l=[r[0]+e[0]*a,r[1]+e[1]*a,r[2]+e[2]*a],[o,h]=i!==void 0?Nt(i):[.5,0];let d=0;for(let f=0;f<n;f++){const u=Ne(e,f,n,o,h),c=Yt(t,l,u,s,qn,!0);if(c&&c.t<s){const m=1-c.t/s;d+=m*m}}return Math.max(.35,1-.65*(d/n))}function ue(t){const r=t.nodes.length,e=new ArrayBuffer(Math.max(1,r)*32),n=new Float32Array(e),s=new Int32Array(e);for(let c=0;c<r;c++){const m=t.nodes[c],b=c*8;n[b]=m.min[0],n[b+1]=m.min[1],n[b+2]=m.min[2],n[b+4]=m.max[0],n[b+5]=m.max[1],n[b+6]=m.max[2],m.left<0?(s[b+3]=-1-m.triStart,s[b+7]=m.triCount):(s[b+3]=m.left,s[b+7]=m.right)}const i=t.triOrder.length,a=new Float32Array(Math.max(1,i)*12),l=new Float32Array(Math.max(1,i)*8),o=new Float32Array(Math.max(1,i)*4);for(let c=0;c<i;c++){const m=t.triOrder[c],b=m*9,y=c*12,g=t.tris[b],v=t.tris[b+1],T=t.tris[b+2];a[y]=g,a[y+1]=v,a[y+2]=T,a[y+3]=t.isEmissiveTri[m]?1:0,a[y+4]=t.tris[b+3]-g,a[y+5]=t.tris[b+4]-v,a[y+6]=t.tris[b+5]-T,a[y+7]=c,a[y+8]=t.tris[b+6]-g,a[y+9]=t.tris[b+7]-v,a[y+10]=t.tris[b+8]-T,a[y+11]=0;const M=m*6,A=c*8;l[A]=t.triUv1[M],l[A+1]=t.triUv1[M+1],l[A+2]=t.triUv1[M+2],l[A+3]=t.triUv1[M+3],l[A+4]=t.triUv1[M+4],l[A+5]=t.triUv1[M+5];const x=m*3,L=c*4;o[L]=t.albedo[x],o[L+1]=t.albedo[x+1],o[L+2]=t.albedo[x+2],o[L+3]=1}const h=t.emissiveCatalog.count,d=t.emissiveCatalog.totalPower,f=new Float32Array(Math.max(1,h)*16),u=new DataView(t.emissiveCatalog.bytes);for(let c=0;c<h;c++){const m=16+c*16,b=u.getUint32(m,!0),y=u.getFloat32(m+8,!0),g=u.getFloat32(m+12,!0),v=t.origToLeafOrder[b]??0,T=b*9,M=b*3,A=t.tris[T],x=t.tris[T+1],L=t.tris[T+2],E=c*16;f[E]=A,f[E+1]=x,f[E+2]=L,f[E+3]=y,f[E+4]=t.tris[T+3]-A,f[E+5]=t.tris[T+4]-x,f[E+6]=t.tris[T+5]-L,f[E+7]=g,f[E+8]=t.tris[T+6]-A,f[E+9]=t.tris[T+7]-x,f[E+10]=t.tris[T+8]-L,f[E+11]=v,f[E+12]=t.emissive[M],f[E+13]=t.emissive[M+1],f[E+14]=t.emissive[M+2],f[E+15]=d}return{nodeBytes:e,nodeCount:r,rootIndex:Math.max(0,t.root),triFloats:a,triCount:i,triUv1Floats:l,triAlbedoFloats:o,emissiveFloats:f,emissiveCount:h}}function Ne(t,r,e,n,s){const i=Math.min(.999999,(r+n)/e),a=le(r*zn+s),l=Math.sqrt(i),o=2*Math.PI*a,h=l*Math.cos(o),d=l*Math.sin(o),f=Math.sqrt(Math.max(0,1-i)),u=Math.abs(t[1])<.999?0:1,c=Math.abs(t[1])<.999?1:0;let m=c*t[2],b=-u*t[2],y=u*t[1]-c*t[0];const g=Math.hypot(m,b,y)||1;m/=g,b/=g,y/=g;const v=t[1]*y-t[2]*b,T=t[2]*m-t[0]*y,M=t[0]*b-t[1]*m,A=m*h+v*d+t[0]*f,x=b*h+T*d+t[1]*f,L=y*h+M*d+t[2]*f,E=Math.hypot(A,x,L)||1;return[A/E,x/E,L/E]}function le(t){return t-Math.floor(t)}const Ge=.6180339887498949;function Yn(t){let r=t>>>0;r^=r>>>16,r=Math.imul(r,2146121005)>>>0,r^=r>>>15,r=Math.imul(r,2221713035)>>>0,r^=r>>>16;const e=(r&65535)/65536,n=(r>>>16&65535)/65536;return[e,n]}function Wt(t,r,e,n,s,i){if(!s)return 1;const a=Math.max(1,(i==null?void 0:i.samples)??6),[l,o]=(i==null?void 0:i.jitterSeed)!==void 0?Yn(i.jitterSeed):[.5,0];if(t.kind==="point"||t.kind==="spot"){const u=(i==null?void 0:i.pointLightRadius)??.16;if(a<=1||u<=1e-5||n<=1e-4)return s(r,e,n)?0:1;const c=Math.min(u,n*.35),m=[r[0]+e[0]*n,r[1]+e[1]*n,r[2]+e[2]*n],b=[-e[0],-e[1],-e[2]];let y=0;for(let g=0;g<a;g++){const v=Ce(m,b,c,g,a,l,o),T=v[0]-r[0],M=v[1]-r[1],A=v[2]-r[2],x=Math.hypot(T,M,A)||1e-6,L=[T/x,M/x,A/x];s(r,L,Math.max(0,x-.001))||y++}return y/a}if(t.kind==="directional"){const u=(i==null?void 0:i.sunAngularRadius)??.012;if(a<=1||u<=0)return s(r,e,n)?0:1;let c=0;for(let m=0;m<a;m++){const b=Xn(e,u,m,a,l,o);s(r,b,n)||c++}return c/a}const h=Math.max(t.area,1e-6),d=Math.sqrt(h/Math.PI);let f=0;for(let u=0;u<a;u++){const c=Ce(t.position,t.normal,d,u,a,l,o),m=c[0]-r[0],b=c[1]-r[1],y=c[2]-r[2],g=Math.hypot(m,b,y)||1e-6,v=[m/g,b/g,y/g];s(r,v,Math.max(0,g-.001))||f++}return f/a}function ze(t){const r=Math.abs(t[1])<.999?0:1,e=Math.abs(t[1])<.999?1:0;let n=e*t[2],s=-r*t[2],i=r*t[1]-e*t[0];const a=Math.hypot(n,s,i)||1;n/=a,s/=a,i/=a;const l=t[1]*i-t[2]*s,o=t[2]*n-t[0]*i,h=t[0]*s-t[1]*n;return{t:[n,s,i],b:[l,o,h]}}function Ce(t,r,e,n,s,i,a){const{t:l,b:o}=ze(r),h=Math.min(.999999,(n+i)/s),d=Fe(n*Ge+a),f=Math.sqrt(h)*e,u=2*Math.PI*d,c=f*Math.cos(u),m=f*Math.sin(u);return[t[0]+l[0]*c+o[0]*m,t[1]+l[1]*c+o[1]*m,t[2]+l[2]*c+o[2]*m]}function Xn(t,r,e,n,s,i){const{t:a,b:l}=ze(t),o=Math.min(.999999,(e+s)/n),h=Fe(e*Ge+i+.1),d=Math.sqrt(o)*r,f=2*Math.PI*h,u=d*Math.cos(f),c=d*Math.sin(f),m=t[0]+a[0]*u+l[0]*c,b=t[1]+a[1]*u+l[1]*c,y=t[2]+a[2]*u+l[2]*c,g=Math.hypot(m,b,y)||1;return[m/g,b/g,y/g]}function Fe(t){return t-Math.floor(t)}function $t(t){return[Number((t==null?void 0:t[0])??1),Number((t==null?void 0:t[1])??1),Number((t==null?void 0:t[2])??1)]}const Wn=it.vec3.fromValues(0,-1,0),He=it.quat.create(),Ct=it.vec3.create(),Ve=.6180339887498949,jt=.05;function $n(t){if(!t)return[0,1,0];it.mat4.getRotation(He,t),it.vec3.transformQuat(Ct,Wn,He);const r=it.vec3.length(Ct)||1;return[-Ct[0]/r,-Ct[1]/r,-Ct[2]/r]}function jn(t){const r=(t==null?void 0:t[0])??1,e=(t==null?void 0:t[1])??0,n=(t==null?void 0:t[2])??0,s=Math.hypot(r,e,n)||1;return[r/s,e/s,n/s]}function fe(t){const r=(t==null?void 0:t[8])??0,e=(t==null?void 0:t[9])??0,n=(t==null?void 0:t[10])??1,s=Math.hypot(r,e,n)||1;return[r/s,e/s,n/s]}function Kn(t){if(!t)return[1,1];const r=Math.hypot(t[0]??1,t[1]??0,t[2]??0)||1,e=Math.hypot(t[4]??0,t[5]??1,t[6]??0)||1;return[r,e]}function he(t){return[t[12]??0,t[13]??0,t[14]??0]}function me(t){const r=Math.abs(t[1])<.99?0:1,e=Math.abs(t[1])<.99?1:0,n=e*t[2],s=-r*t[2],i=r*t[1]-e*t[0],a=Math.hypot(n,s,i)||1;return[n/a,s/a,i/a]}function qe(t){var e,n;const r=[];for(const[s,[i]]of t.query([I.DirectionalLightComponent]).iter()){if(!I.isEntityEffectivelyVisible(t,s))continue;const a=t.getComponent(s,I.TransformComponent);r.push({kind:"directional",color:$t(i.color),intensity:i.intensity,toLight:$n(a==null?void 0:a.worldTransform)})}for(const[s,[i]]of t.query([I.PointLightComponent]).iter()){if(!I.isEntityEffectivelyVisible(t,s))continue;const a=t.getComponent(s,I.TransformComponent);r.push({kind:"point",color:$t(i.color),intensity:i.intensity,position:a?he(a.worldTransform):[0,0,0],range:i.range})}for(const[s,[i]]of t.query([I.SpotLightComponent]).iter()){if(!I.isEntityEffectivelyVisible(t,s))continue;const a=t.getComponent(s,I.TransformComponent),l=fe(a==null?void 0:a.worldTransform);r.push({kind:"spot",color:$t(i.color),intensity:i.intensity,position:a?he(a.worldTransform):[0,0,0],range:i.range,direction:[-l[0],-l[1],-l[2]],innerCos:Math.cos(i.innerAngle),outerCos:Math.cos(i.outerAngle)})}for(const[s,[i]]of t.query([I.AreaLightComponent]).iter()){if(!I.isEntityEffectivelyVisible(t,s))continue;const a=t.getComponent(s,I.TransformComponent),l=a==null?void 0:a.worldTransform,o=fe(l),h=jn(l),[d,f]=Kn(l),u=Math.max(.001,i.width*d),c=Math.max(.001,i.height*f),m=i.shape==="disk",b=Math.max(1e-4,m?Math.PI*(u*.5)*(c*.5):u*c);r.push({kind:"area",color:$t(i.color),intensity:i.intensity,position:l?he(l):[0,0,0],normal:[-o[0],-o[1],-o[2]],area:b,width:u,height:c,tangent:h,isDisk:m,range:i.range??0})}for(const[s,[i]]of t.query([I.RenderableComponent]).iter()){if(i.visible===!1||!I.isEntityEffectivelyVisible(t,s)||t.hasComponent(s,I.EditorOnlyComponent)||t.hasComponent(s,I.StaticMergeChunkComponent))continue;const a=i.material;if(!a||typeof a=="string")continue;const l=(n=(e=a.properties)==null?void 0:e.get)==null?void 0:n.call(e,"emissiveColor"),o=Array.isArray(l)||ArrayBuffer.isView(l)?l:a.emissiveColor,h=Number((o==null?void 0:o[0])??0),d=Number((o==null?void 0:o[1])??0),f=Number((o==null?void 0:o[2])??0),u=Math.max(h,d,f);if(u<1e-4)continue;const c=i.mesh;if(!c||typeof c=="string")continue;const m=t.getComponent(s,I.TransformComponent),b=it.vec3.create();m?it.vec3.transformMat4(b,c.boundingSphere.center,m.worldTransform):it.vec3.copy(b,c.boundingSphere.center);const y=c.boundingSphere.radius,g=Math.max(1e-4,4*Math.PI*y*y*.25),v=fe(m==null?void 0:m.worldTransform);r.push({kind:"emissive",color:[h/Math.max(u,1e-6),d/Math.max(u,1e-6),f/Math.max(u,1e-6)],intensity:u,position:[b[0],b[1],b[2]],normal:[-v[0],-v[1],-v[2]],area:g})}return r}function Kt(t){return t-Math.floor(t)}function de(t,r,e,n,s){let i=n[0],a=n[1],l=n[2];const o=e[0],h=e[1],d=e[2],f=(s==null?void 0:s.bias)??.0015,u=r[0]+o*f,c=r[1]+h*f,m=r[2]+d*f,b=[u,c,m],y=s==null?void 0:s.occluded,g=Math.max(1,(s==null?void 0:s.softShadowSamples)??6),v=s==null?void 0:s.pointLightRadius,T=s==null?void 0:s.jitterSeed;for(let A=0;A<t.length;A++){const x=t[A],L=T!==void 0?(T^Math.imul(A+1,2654435769))>>>0:void 0;if(x.kind==="directional"){const _=o*x.toLight[0]+h*x.toLight[1]+d*x.toLight[2];if(_<=0)continue;const B=Wt(x,b,x.toLight,1e4,y,{samples:g,jitterSeed:L});if(B<=0)continue;const p=x.intensity*_*B;i+=x.color[0]*p,a+=x.color[1]*p,l+=x.color[2]*p;continue}if(x.kind==="area"){const _=Math.sqrt(Math.max(1e-6,x.area)),B=x.width??_,p=x.height??_,D=x.tangent??me(x.normal),R=!!x.isDisk,U=x.range??0,S=Math.max(4,g),k=Math.max(1,Math.ceil(Math.sqrt(S))),G=Math.max(1,Math.ceil(S/k)),[C,J]=L!==void 0?Nt(L):[.5,.5];let et=0,nt=0,rt=0,K=0;const tt={position:x.position,direction:x.normal,tangent:D,width:B,height:p,color:x.color,intensity:x.intensity,range:U,isDisk:R};for(let Q=0;Q<G&&K<S;Q++)for(let at=0;at<k&&K<S;at++){const ut=R?Kt((K+C)/S):(at+Kt(C+K*Ve))/k,ct=R?Kt(J+K*Ve):(Q+Kt(J+K*.7548776662))/G;K++;const ot=Ut.sampleRectAreaLight(r,tt,ut,ct);if(!ot.valid||U>0&&ot.distance>=U)continue;const st=o*ot.dir[0]+h*ot.dir[1]+d*ot.dir[2];if(!(st<=.0015)){if(y){const ft=r[0]+ot.dir[0]*ot.distance,ht=r[1]+ot.dir[1]*ot.distance,bt=r[2]+ot.dir[2]*ot.distance,vt=ft-u,At=ht-c,_t=bt-m,gt=Math.hypot(vt,At,_t);if(gt>f*2&&y(b,[vt/gt,At/gt,_t/gt],gt-f*2))continue}et+=ot.radianceOverPdf[0]*st,nt+=ot.radianceOverPdf[1]*st,rt+=ot.radianceOverPdf[2]*st}}if(K>0){const Q=1/K;i+=et*Q,a+=nt*Q,l+=rt*Q}continue}const E=x.position[0]-r[0],P=x.position[1]-r[1],O=x.position[2]-r[2],X=E*E+P*P+O*O,w=Math.sqrt(X)||1e-6,V=1/w,q=E*V,$=P*V,z=O*V,W=o*q+h*$+d*z;if(W<=0)continue;const F=[q,$,z],N=(x.kind==="point"||x.kind==="spot")&&x.range>0?Math.min(w,x.range):w;if((x.kind==="point"||x.kind==="spot")&&x.range>0&&w>=x.range)continue;let Y=0;if(x.kind==="point")Y=I.evalKhrPunctualAttenuation(w,x.range,jt);else if(x.kind==="spot"){const _=I.evalKhrSpotAttenuation(F,x.direction,x.innerCos,x.outerCos);if(_<=0)continue;Y=I.evalKhrPunctualAttenuation(w,x.range,jt)*_}else{const _=x.normal[0]*-q+x.normal[1]*-$+x.normal[2]*-z;if(_<=0)continue;const B=Wt(x,b,F,Math.max(0,N-f*2),y,{samples:g,jitterSeed:L});if(B<=0)continue;const p=x.intensity*(x.area*_*W/Math.max(X,jt*jt))*B;i+=x.color[0]*p,a+=x.color[1]*p,l+=x.color[2]*p;continue}if(Y<=0)continue;const Z=Wt(x,b,F,Math.max(0,N-f*2),y,{samples:g,pointLightRadius:v,jitterSeed:L});if(Z<=0)continue;const j=x.intensity*W*Y*Z;i+=x.color[0]*j,a+=x.color[1]*j,l+=x.color[2]*j}const M=(s==null?void 0:s.ao)??1;return[i*M,a*M,l*M]}const Ye=[1/16,4/16,6/16,4/16,1/16];function Lt(t,r,e){const n=Math.max(0,Math.floor(e));if(n<1)return;const s=new Float32Array(t.length);for(let i=0;i<n;i++){s.set(t);for(let a=0;a<r;a++)for(let l=0;l<r;l++){const o=(a*r+l)*4;if((s[o+3]??0)>0)continue;let h=0,d=0,f=0,u=0;for(let c=-1;c<=1;c++){const m=a+c;if(!(m<0||m>=r))for(let b=-1;b<=1;b++){if(b===0&&c===0)continue;const y=l+b;if(y<0||y>=r)continue;const g=(m*r+y)*4;if((s[g+3]??0)<=0)continue;const v=b===0||c===0?1:.7071;h+=s[g]*v,d+=s[g+1]*v,f+=s[g+2]*v,u+=v}}u>0&&(t[o]=h/u,t[o+1]=d/u,t[o+2]=f/u,t[o+3]=1)}}}function Ot(t,r,e){const n=e==null?void 0:e.gbuffer,s=(e==null?void 0:e.sigmaSpatial)??1.15,i=(e==null?void 0:e.sigmaRange)??.85,a=Math.max(1e-4,(e==null?void 0:e.planeSigma)??.08),l=1/(2*s*s),o=1/(2*i*i),h=1/(2*a*a),d=(e==null?void 0:e.stepSizes)??((e==null?void 0:e.iterations)!==void 0?Array.from({length:Math.max(0,e.iterations)},()=>1):n?[1,2,4]:[1,1]);if(d.length===0)return;const f=[];for(let m=0;m<r;m++)for(let b=0;b<r;b++)(t[(m*r+b)*4+3]??0)>0&&f.push(b,m);if(f.length===0)return;const u=new Float32Array(t.length),c=new Float32Array(8);for(let m=0;m<d.length;m++){const b=Math.max(1,d[m]),y=(e==null?void 0:e.wavelet5x5)??b>1,g=y?2:1;u.set(t);for(let v=0;v<f.length;v+=2){const T=f[v],M=f[v+1],A=(M*r+T)*4;let x=u[A],L=u[A+1],E=u[A+2];const P=!!(n&&(n.normal[A+3]??0)>0),O=P?n.position[A]:0,X=P?n.position[A+1]:0,w=P?n.position[A+2]:0,V=P?n.normal[A]:0,q=P?n.normal[A+1]:1,$=P?n.normal[A+2]:0;if(m===0){let Y=0,Z=0,j=0,_=0;for(let B=-1;B<=1;B++){const p=M+B;if(!(p<0||p>=r))for(let D=-1;D<=1;D++){if(D===0&&B===0)continue;const R=T+D;if(R<0||R>=r)continue;const U=(p*r+R)*4;if((u[U+3]??0)<=0)continue;if(P&&(n.normal[U+3]??0)>0){if(V*n.normal[U]+q*n.normal[U+1]+$*n.normal[U+2]<.8)continue;const J=n.position[U]-O,et=n.position[U+1]-X,nt=n.position[U+2]-w;if(Math.abs(V*J+q*et+$*nt)>a*2)continue}const S=u[U],k=u[U+1],G=u[U+2];c[Y++]=(S+k+G)/3,Z+=S,j+=k,_+=G}}if(Y>=3){const B=c.subarray(0,Y);B.sort();const p=B[Y>>1],D=(x+L+E)/3;if(Math.abs(D-p)>.35)if(D>1e-4){const R=p/D;x*=R,L*=R,E*=R}else{const R=Math.max((Z+j+_)/(3*Y),1e-4),U=p/R;x=Z/Y*U,L=j/Y*U,E=_/Y*U}}}let z=0,W=0,F=0,N=0;for(let Y=-g;Y<=g;Y++){const Z=M+Y*b;if(Z<0||Z>=r)continue;const j=y?Ye[Y+2]:1;for(let _=-g;_<=g;_++){const B=T+_*b;if(B<0||B>=r)continue;const p=(Z*r+B)*4;if((u[p+3]??0)<=0)continue;let D=1;if(P&&(_!==0||Y!==0)){if((n.normal[p+3]??0)<=0)continue;const rt=V*n.normal[p]+q*n.normal[p+1]+$*n.normal[p+2];if(rt<=.72)continue;const K=n.position[p]-O,tt=n.position[p+1]-X,Q=n.position[p+2]-w,at=V*K+q*tt+$*Q,ut=K*K+tt*tt+Q*Q,ct=.55*b*g;if(ut>ct*ct)continue;const ot=Math.pow(rt,16),st=Math.exp(-at*at*h);if(D=ot*st,D<1e-4)continue}const R=_===0&&Y===0?x:u[p],U=_===0&&Y===0?L:u[p+1],S=_===0&&Y===0?E:u[p+2],k=R-x,G=U-L,C=S-E,J=k*k+G*G+C*C,et=y?Ye[_+2]*j:Math.exp(-(_*_+Y*Y)*l),nt=D*et*Math.exp(-J*o);z+=R*nt,W+=U*nt,F+=S*nt,N+=nt}}N>1e-8&&(t[A]=z/N,t[A+1]=W/N,t[A+2]=F/N)}}}function Xe(t,r,e){let n=Math.max(0,t),s=Math.max(0,r),i=Math.max(0,e);const a=n+s+i;if(a<=1e-12)return[1/3,1/3,1/3];const l=1/a;return n*=l,s*=l,i*=l,[n,s,i]}function We(t,r){const e=new Float32Array(r*r*4),n=new Float32Array(r*r*4),s=new Float32Array(r*r*4);let i=0;for(const a of t)i+=$e(a,e,n,s,r,!1);for(const a of t)i+=$e(a,e,n,s,r,!0);return{atlasSize:r,position:e,normal:n,albedo:s,coveredTexels:i}}function $e(t,r,e,n,s,i){const a=t.mesh,l=I.STANDARD_MESH_VERTEX_FLOATS,o=a.vertices,h=a.indices,d=1/s,f=.55*d,{positions:u,normals:c,albedos:m}=t;let b=0;for(let y=0;y+2<h.length;y+=3){const g=h[y],v=h[y+1],T=h[y+2],M=o[g*l+I.STANDARD_MESH_ATTR.uv1],A=o[g*l+I.STANDARD_MESH_ATTR.uv1+1],x=o[v*l+I.STANDARD_MESH_ATTR.uv1],L=o[v*l+I.STANDARD_MESH_ATTR.uv1+1],E=o[T*l+I.STANDARD_MESH_ATTR.uv1],P=o[T*l+I.STANDARD_MESH_ATTR.uv1+1],O=Math.min(M,x,E),X=Math.max(M,x,E),w=Math.min(A,L,P),V=Math.max(A,L,P);if(X-O<1e-8&&V-w<1e-8)continue;const q=Math.max(0,Math.floor(O*s)),$=Math.max(0,Math.floor(w*s)),z=Math.min(s-1,Math.ceil(X*s)),W=Math.min(s-1,Math.ceil(V*s)),F=Zt(M,A,x,L,E,P);if(Math.abs(F)<1e-12)continue;const N=1/F;for(let Y=$;Y<=W;Y++){const Z=(Y+.5)*d;for(let j=q;j<=z;j++){const _=(Y*s+j)*4;if(i&&(r[_+3]??0)>0)continue;const B=(j+.5)*d;let p=Zt(x,L,E,P,B,Z)*N,D=Zt(E,P,M,A,B,Z)*N,R=Zt(M,A,x,L,B,Z)*N;if(i){const[C,J,et]=Xe(p,D,R),nt=C*M+J*x+et*E,rt=C*A+J*L+et*P;if(Math.abs(nt-B)>f||Math.abs(rt-Z)>f)continue;p=C,D=J,R=et}else{if(p<-1e-4||D<-1e-4||R<-1e-4)continue;[p,D,R]=Xe(p,D,R)}(r[_+3]??0)<=0&&b++,r[_]=u[g*3]*p+u[v*3]*D+u[T*3]*R,r[_+1]=u[g*3+1]*p+u[v*3+1]*D+u[T*3+1]*R,r[_+2]=u[g*3+2]*p+u[v*3+2]*D+u[T*3+2]*R,r[_+3]=1;const U=c[g*3]*p+c[v*3]*D+c[T*3]*R,S=c[g*3+1]*p+c[v*3+1]*D+c[T*3+1]*R,k=c[g*3+2]*p+c[v*3+2]*D+c[T*3+2]*R,G=Math.hypot(U,S,k)||1;e[_]=U/G,e[_+1]=S/G,e[_+2]=k/G,e[_+3]=1,m?(n[_]=m[g*3]*p+m[v*3]*D+m[T*3]*R,n[_+1]=m[g*3+1]*p+m[v*3+1]*D+m[T*3+1]*R,n[_+2]=m[g*3+2]*p+m[v*3+2]*D+m[T*3+2]*R,n[_+3]=1):(n[_]=.65,n[_+1]=.65,n[_+2]=.65,n[_+3]=1)}}}return b}function Zt(t,r,e,n,s,i){return(s-t)*(n-r)-(i-r)*(e-t)}const Qt=28;function pt(){return typeof performance<"u"?performance.now():Date.now()}function Jt(){return new Promise(t=>{setTimeout(t,0)})}function Mt(t){if(t!=null&&t.aborted)throw new DOMException("Bake aborted","AbortError")}async function je(t,r,e,n,s,i,a){var Y,Z,j,_,B,p,D,R;const l=(a==null?void 0:a.aoSamples)??6,o=(a==null?void 0:a.texelAoSamples)??(l>0?Math.min(10,Math.max(4,Math.ceil(l))):0),h=(a==null?void 0:a.aoRadius)??.45,d=(a==null?void 0:a.shadows)!==!1,f=(a==null?void 0:a.softShadowSamples)??8,u=((a==null?void 0:a.yieldEveryCharts)??2)>0,c=Math.max(0,(a==null?void 0:a.bounces)??2),m=Math.max(4,(a==null?void 0:a.bounceSamples)??16),b=(a==null?void 0:a.denoise)!==!1,y=(a==null?void 0:a.rasterMode)??"full",g=a==null?void 0:a.skyRadiance;Mt(a==null?void 0:a.signal);const v=Be(t,r,a==null?void 0:a.accelOptions),T=!!(v&&v.emissiveCatalog.count>0),M=T?s.filter(U=>U.kind!=="emissive"):s,A=d&&v?(U,S,k)=>qt(v,U,S,k):void 0,x=[],L=it.vec3.create(),E=it.vec3.create();for(const U of r){Mt(a==null?void 0:a.signal);const S=t.getComponent(U.entity,I.TransformComponent),k=(S==null?void 0:S.worldTransform)??it.mat4.create(),G=it.mat3.create();it.mat3.normalFromMat4(G,k);const C=U.mesh,J=I.STANDARD_MESH_VERTEX_FLOATS,et=C.vertices,nt=Math.floor(et.length/J),rt=new Float32Array(nt*3),K=new Float32Array(nt*3),tt=ke(t,U.entity,C,a==null?void 0:a.accelOptions);for(let Q=0;Q<nt;Q++){const at=Q*J;it.vec3.set(L,et[at],et[at+1],et[at+2]),it.vec3.transformMat4(L,L,k),rt[Q*3]=L[0],rt[Q*3+1]=L[1],rt[Q*3+2]=L[2],it.vec3.set(E,et[at+I.STANDARD_MESH_ATTR.normal],et[at+I.STANDARD_MESH_ATTR.normal+1],et[at+I.STANDARD_MESH_ATTR.normal+2]),it.vec3.transformMat3(E,E,G),it.vec3.normalize(E,E),K[Q*3]=E[0],K[Q*3+1]=E[1],K[Q*3+2]=E[2]}x.push({mesh:C,positions:rt,normals:K,albedos:tt})}const P=We(x,n),O=n*n,X=[];for(let U=0;U<O;U++)(P.position[U*4+3]??0)>0&&X.push(U);const w=new Uint32Array(X),V=w.length,q=Math.max(1,1+c);if(V===0)return(Y=a==null?void 0:a.onProgress)==null||Y.call(a,q,q),{gbufferCharts:x,gbuffer:P,accel:v};let $=pt();const z=new Float32Array(O*4),W=new Float32Array(O*4);let F=!1;if(a!=null&&a.gpuDirectRunner)try{const U=await a.gpuDirectRunner(P,d?v:null);if(U.length===O*4){for(let S=0;S<V;S++){const k=w[S]*4,G=o>0?Math.min(1,Math.max(.25,U[k+3])):1;z[k]=U[k],z[k+1]=U[k+1],z[k+2]=U[k+2],z[k+3]=1,W[k]=G,W[k+1]=G,W[k+2]=G,W[k+3]=1}F=!0}}catch(U){if(U instanceof DOMException&&U.name==="AbortError")throw U;F=!1}if(!F)for(let U=0;U<V;U++){!(U&511)&&u&&pt()-$>=Qt&&(Mt(a==null?void 0:a.signal),(Z=a==null?void 0:a.onProgress)==null||Z.call(a,U/V,q),await Jt(),$=pt(),Mt(a==null?void 0:a.signal));const S=w[U],k=S%n,G=S/n|0,C=S*4,J=[P.position[C],P.position[C+1],P.position[C+2]],et=[P.normal[C],P.normal[C+1],P.normal[C+2]],nt=Gt(k,G,1),rt=v&&o>0?we(v,J,et,Math.max(6,o),h,nt):1;W[C]=rt,W[C+1]=rt,W[C+2]=rt,W[C+3]=1;const K=de(M,J,et,i,{occluded:A,ao:1,softShadowSamples:f,jitterSeed:nt});let tt=K[0],Q=K[1],at=K[2];if(T&&v){const ut=Le(v,J,et,Math.max(4,f),d,nt);tt+=ut[0],Q+=ut[1],at+=ut[2]}z[C]=tt,z[C+1]=Q,z[C+2]=at,z[C+3]=1}b&&(Ot(z,n,{gbuffer:P,stepSizes:[1],wavelet5x5:!0,sigmaRange:1.25,planeSigma:.045}),Ot(W,n,{gbuffer:P,stepSizes:[1,2],wavelet5x5:!0,sigmaRange:3,planeSigma:.05})),(j=a==null?void 0:a.onProgress)==null||j.call(a,1,q);let N=new Float32Array(O*4);if(c>0&&v){let U=!1;if(a!=null&&a.gpuBounceRunner)try{const S=await a.gpuBounceRunner(P,v,z,w);S.length===O*4&&(N=S,U=!0)}catch(S){if(S instanceof DOMException&&S.name==="AbortError")throw S;U=!1}if(!U){const S=new Float32Array(O*4);for(let K=0;K<V;K++){const tt=w[K]*4;S[tt]=z[tt],S[tt+1]=z[tt+1],S[tt+2]=z[tt+2],S[tt+3]=1}Lt(S,n,2),zt(v,S,n);const k=new Float32Array(O*4),G=Math.max(16,m),C=V>75e4?3:V>3e4?2:1,J=Math.ceil(n/C),et=C>1?new Float32Array(J*J*3):null,nt=C>1?new Int32Array(J*J):null,rt=[1,2,4,8];for(let K=0;K<c;K++){if(Mt(a==null?void 0:a.signal),k.fill(0),C===1)for(let tt=0;tt<V;tt++){!(tt&127)&&u&&pt()-$>=Qt&&(Mt(a==null?void 0:a.signal),(_=a==null?void 0:a.onProgress)==null||_.call(a,1+K+tt/V,q),await Jt(),$=pt(),Mt(a==null?void 0:a.signal));const Q=w[tt],at=Q%n,ut=Q/n|0,ct=Q*4,ot=[P.position[ct],P.position[ct+1],P.position[ct+2]],st=[P.normal[ct],P.normal[ct+1],P.normal[ct+2]],ft=Gt(at,ut,101+K*37),ht=Xt(v,ot,st,G,25,{jitterSeed:ft,atlasRgba:S,atlasSize:n,skyRadiance:g});k[ct]=ht[0],k[ct+1]=ht[1],k[ct+2]=ht[2],k[ct+3]=1}else{nt.fill(-1);const tt=J*J;let Q=0;for(let at=0;at<J;at++)for(let ut=0;ut<J;ut++){Q++,!(Q&127)&&u&&pt()-$>=Qt&&(Mt(a==null?void 0:a.signal),(B=a==null?void 0:a.onProgress)==null||B.call(a,1+K+Q/tt*.85,q),await Jt(),$=pt(),Mt(a==null?void 0:a.signal));const ct=ut*C,ot=at*C;let st=-1,ft=ct,ht=ot;t:for(let yt=0;yt<C&&ot+yt<n;yt++)for(let mt=0;mt<C&&ct+mt<n;mt++){const It=(ot+yt)*n+(ct+mt);if((P.position[It*4+3]??0)>0){st=It,ft=ct+mt,ht=ot+yt;break t}}if(st<0)continue;const bt=st*4,vt=[P.position[bt],P.position[bt+1],P.position[bt+2]],At=[P.normal[bt],P.normal[bt+1],P.normal[bt+2]],_t=Gt(ft,ht,101+K*37),gt=Xt(v,vt,At,G,25,{jitterSeed:_t,atlasRgba:S,atlasSize:n,skyRadiance:g}),St=at*J+ut;nt[St]=st,et[St*3]=gt[0],et[St*3+1]=gt[1],et[St*3+2]=gt[2]}for(let at=0;at<V;at++){!(at&511)&&u&&pt()-$>=Qt&&(Mt(a==null?void 0:a.signal),(p=a==null?void 0:a.onProgress)==null||p.call(a,1+K+.85+at/V*.15,q),await Jt(),$=pt(),Mt(a==null?void 0:a.signal));const ut=w[at],ct=ut%n,ot=ut/n|0,st=ut*4,ft=P.position[st],ht=P.position[st+1],bt=P.position[st+2],vt=P.normal[st],At=P.normal[st+1],_t=P.normal[st+2],gt=(ct+.5)/C-.5,St=(ot+.5)/C-.5,yt=Math.max(0,Math.min(J-1,Math.floor(gt))),mt=Math.max(0,Math.min(J-1,Math.floor(St))),It=Math.min(J-1,yt+1),ne=Math.min(J-1,mt+1),Pt=Math.max(0,Math.min(1,gt-yt)),xn=Math.max(0,Math.min(1,St-mt));let Me=0,vn=0,Mn=0,An=0;for(let Tt=0;Tt<2;Tt++){const Ht=Tt===0?mt:ne,ua=Tt===0?1-xn:xn;for(let ae=0;ae<2;ae++){const la=ae===0?yt:It,_n=(ae===0?1-Pt:Pt)*ua;if(_n<=1e-5)continue;const re=Ht*J+la,Sn=nt[re];if(Sn<0)continue;const wt=Sn*4,Tn=vt*P.normal[wt]+At*P.normal[wt+1]+_t*P.normal[wt+2];if(Tn<.78)continue;const Ae=P.position[wt]-ft,_e=P.position[wt+1]-ht,Se=P.position[wt+2]-bt,pn=Math.abs(vt*Ae+At*_e+_t*Se);if(pn>.08||Ae*Ae+_e*_e+Se*Se>.55)continue;const se=_n*Math.pow(Tn,8)*Math.exp(-pn/.05);Me+=se,vn+=et[re*3]*se,Mn+=et[re*3+1]*se,An+=et[re*3+2]*se}}if(Me>1e-4){const Tt=1/Me;k[st]=vn*Tt,k[st+1]=Mn*Tt,k[st+2]=An*Tt,k[st+3]=1}else{const Tt=Gt(ct,ot,101+K*37),Ht=Xt(v,[ft,ht,bt],[vt,At,_t],G,25,{jitterSeed:Tt,atlasRgba:S,atlasSize:n,skyRadiance:g});k[st]=Ht[0],k[st+1]=Ht[1],k[st+2]=Ht[2],k[st+3]=1}}}b&&Ot(k,n,{gbuffer:P,stepSizes:rt,wavelet5x5:!0,sigmaRange:14,planeSigma:.06});for(let tt=0;tt<V;tt++){const Q=w[tt]*4;N[Q]=(N[Q]??0)+k[Q],N[Q+1]=(N[Q+1]??0)+k[Q+1],N[Q+2]=(N[Q+2]??0)+k[Q+2],N[Q+3]=1}K+1<c&&(S.set(k),Lt(S,n,2),zt(v,S,n)),(D=a==null?void 0:a.onProgress)==null||D.call(a,2+K,q)}}}for(let U=0;U<V;U++){const S=w[U]*4,k=W[S],G=.45+.55*k;y==="indirect-only"?(e[S]=N[S]*G,e[S+1]=N[S+1]*G,e[S+2]=N[S+2]*G):(e[S]=z[S]*k+N[S]*G,e[S+1]=z[S+1]*k+N[S+1]*G,e[S+2]=z[S+2]*k+N[S+2]*G),e[S+3]=1}return(R=a==null?void 0:a.onProgress)==null||R.call(a,q,q),{gbufferCharts:x,gbuffer:P,accel:v}}const ge=384,Ke=12+ge*9*4,Ze=.2820947918,be=.4886025119,ye=1.0925484306,Zn=.3153915653,Qn=.5462742153,Jn=Math.PI,xe=2*Math.PI/3,Ft=Math.PI/4,Qe=64,ta=Math.PI*(3-Math.sqrt(5)),Je=(()=>{const t=[];for(let r=0;r<Qe;r++){const e=1-2*(r+.5)/Qe,n=Math.sqrt(Math.max(0,1-e*e)),s=r*ta;t.push([Math.cos(s)*n,e,Math.sin(s)*n])}return t})();function te(t){const r=t[0],e=t[1],n=t[2];return[Ze,-be*e,be*n,-be*r,ye*r*e,-ye*e*n,Zn*(3*n*n-1),-ye*r*n,Qn*(r*r-e*e)]}function tn(t,r,e){const n=te(e),s=[Jn*n[0],xe*n[1],xe*n[2],xe*n[3],Ft*n[4],Ft*n[5],Ft*n[6],Ft*n[7],Ft*n[8]];let i=0,a=0,l=0;for(let o=0;o<9;o++){const h=s[o],d=r+o*3;i+=(t[d]??0)*h,a+=(t[d+1]??0)*h,l+=(t[d+2]??0)*h}return[Math.max(0,i),Math.max(0,a),Math.max(0,l)]}function en(t){const r=atob(t.coefficientsBase64),e=new Uint8Array(r.length);for(let n=0;n<r.length;n++)e[n]=r.charCodeAt(n);return new Float32Array(e.buffer,e.byteOffset,Math.floor(e.byteLength/4))}function ea(t,r,e,n){const[s,i,a]=t.resolution,[l,o,h]=t.boundsMin,[d,f,u]=t.boundsMax,c=s>1?Math.max(0,Math.min(1,(e[0]-l)/Math.max(1e-5,d-l)))*(s-1):0,m=i>1?Math.max(0,Math.min(1,(e[1]-o)/Math.max(1e-5,f-o)))*(i-1):0,b=a>1?Math.max(0,Math.min(1,(e[2]-h)/Math.max(1e-5,u-h)))*(a-1):0,y=Math.min(s-1,Math.floor(c)),g=Math.min(i-1,Math.floor(m)),v=Math.min(a-1,Math.floor(b)),T=Math.min(s-1,y+1),M=Math.min(i-1,g+1),A=Math.min(a-1,v+1),x=c-y,L=m-g,E=b-v;let P=0,O=0,X=0;for(let w=0;w<2;w++){const V=w===0?v:A,q=w===0?1-E:E;for(let $=0;$<2;$++){const z=$===0?g:M,W=($===0?1-L:L)*q;for(let F=0;F<2;F++){const N=F===0?y:T,Y=(F===0?1-x:x)*W;if(Y<=1e-6)continue;const Z=(V*i+z)*s+N,j=tn(r,Z*27,n);P+=j[0]*Y,O+=j[1]*Y,X+=j[2]*Y}}}return[P,O,X]}function nn(t,r=1){const e=new ArrayBuffer(Ke*4),n=new Float32Array(e),s=new Uint32Array(e);if(!t||r<=0)return n;const[i,a,l]=t.resolution,o=Math.min(ge,i*a*l);if(o<=0)return n;const h=en(t);n[0]=t.boundsMin[0],n[1]=t.boundsMin[1],n[2]=t.boundsMin[2],n[3]=1,n[4]=t.boundsMax[0],n[5]=t.boundsMax[1],n[6]=t.boundsMax[2],n[7]=r,s[8]=i,s[9]=a,s[10]=l,s[11]=o;for(let d=0;d<o;d++){const f=d*27,u=12+d*36;for(let c=0;c<9;c++)n[u+c*4]=h[f+c*3]??0,n[u+c*4+1]=h[f+c*3+1]??0,n[u+c*4+2]=h[f+c*3+2]??0,n[u+c*4+3]=0}return n}function an(t){let r=1/0,e=1/0,n=1/0,s=-1/0,i=-1/0,a=-1/0,l=!1;for(const[c,[m]]of t.query([I.RenderableComponent]).iter()){if(!t.hasComponent(c,I.StaticComponent)||t.hasComponent(c,I.EditorOnlyComponent)||t.hasComponent(c,I.StaticMergeChunkComponent))continue;const b=m==null?void 0:m.mesh;if(!b||typeof b=="string")continue;const y=b.boundingSphere.center,g=t.getComponent(c,I.TransformComponent);let v=y[0],T=y[1],M=y[2],A=b.boundingSphere.radius;if(g){const x=g.worldTransform;v=(x[12]??0)+(x[0]??1)*y[0]+(x[4]??0)*y[1]+(x[8]??0)*y[2],T=(x[13]??0)+(x[1]??0)*y[0]+(x[5]??1)*y[1]+(x[9]??0)*y[2],M=(x[14]??0)+(x[2]??0)*y[0]+(x[6]??0)*y[1]+(x[10]??1)*y[2];const L=Math.hypot(x[0]??1,x[1]??0,x[2]??0),E=Math.hypot(x[4]??0,x[5]??1,x[6]??0),P=Math.hypot(x[8]??0,x[9]??0,x[10]??1);A*=Math.max(L,E,P,1e-4)}r=Math.min(r,v-A),e=Math.min(e,T-A),n=Math.min(n,M-A),s=Math.max(s,v+A),i=Math.max(i,T+A),a=Math.max(a,M+A),l=!0}if(!l)return{boundsMin:[-1,0,-1],boundsMax:[1,2,1],resolution:[3,3,3]};const o=.25;r+=o,e+=o,n+=o,s-=o,i-=o,a-=o,s<=r&&(s=r+1),i<=e&&(i=e+1),a<=n&&(a=n+1);const h=1,d=Math.max(2,Math.min(8,Math.ceil((s-r)/h)+1)),f=Math.max(2,Math.min(6,Math.ceil((i-e)/h)+1)),u=Math.max(2,Math.min(8,Math.ceil((a-n)/h)+1));return{boundsMin:[r,e,n],boundsMax:[s,i,a],resolution:[d,f,u]}}function rn(t,r,e,n,s,i,a,l){if(!n)return[a?a[0]+e[0]:e[0],a?a[1]+e[1]:e[1],a?a[2]+e[2]:e[2]];const o=Yt(n,t,r,40,l);if(!o)return[((a==null?void 0:a[0])??0)+e[0],((a==null?void 0:a[1])??0)+e[1],((a==null?void 0:a[2])??0)+e[2]];const h=o.triIndex*3,d=n.albedo[h],f=n.albedo[h+1],u=n.albedo[h+2],c=n.emissive[h],m=n.emissive[h+1],b=n.emissive[h+2];let y=n.bounceSrc[h],g=n.bounceSrc[h+1],v=n.bounceSrc[h+2];if(s&&i>0){const M=o.triIndex*6,A=1-o.baryU-o.baryV,x=A*n.triUv1[M]+o.baryU*n.triUv1[M+2]+o.baryV*n.triUv1[M+4],L=A*n.triUv1[M+1]+o.baryU*n.triUv1[M+3]+o.baryV*n.triUv1[M+5],E=Math.min(i-1,Math.max(0,Math.floor(x*i))),O=(Math.min(i-1,Math.max(0,Math.floor(L*i)))*i+E)*4;(s[O+3]??0)>0&&(y=s[O],g=s[O+1],v=s[O+2])}const T=1/Math.PI;return[d*y*T+c,f*g*T+m,u*v*T+b]}function sn(t,r,e,n){const s=typeof n=="function"?{occluded:n}:n??{},i=s.accel??null,a=s.occluded??(i?(y,g,v)=>qt(i,y,g,v):void 0),[l,o,h]=t.resolution,d=l*o*h,f=new Float32Array(d*9*3);if(!!!(e&&e.length>0)&&!i&&!s.skyRadiance){const y=4*Ze;for(let g=0;g<d;g++){const v=g*27;f[v]=r[0]*y,f[v+1]=r[1]*y,f[v+2]=r[2]*y}}else{const[y,g,v]=t.boundsMin,[T,M,A]=t.boundsMax,x=4*Math.PI/Je.length,L=[r[0]/Math.PI,r[1]/Math.PI,r[2]/Math.PI],E={t:0,triIndex:-1,baryU:0,baryV:0,normal:[0,1,0]};let P=0;for(let O=0;O<h;O++){const X=h>1?O/(h-1):.5,w=v+(A-v)*X;for(let V=0;V<o;V++){const q=o>1?V/(o-1):.5,$=g+(M-g)*q;for(let z=0;z<l;z++){const W=l>1?z/(l-1):.5,F=y+(T-y)*W,N=[F,$,w],Y=P*27;P++;for(const Z of Je){const j=rn(N,Z,L,i,s.atlasRgba,s.atlasSize??0,s.skyRadiance,E),_=te(Z);for(let B=0;B<9;B++){const p=_[B]*x,D=Y+B*3;f[D]=(f[D]??0)+j[0]*p,f[D+1]=(f[D+1]??0)+j[1]*p,f[D+2]=(f[D+2]??0)+j[2]*p}}if(e)for(const Z of e){if(i&&i.emissiveCatalog.count>0&&Z.kind==="emissive")continue;let j;if(Z.kind==="directional")j=Z.toLight;else{const p=Z.position[0]-F,D=Z.position[1]-$,R=Z.position[2]-w,U=Math.hypot(p,D,R);if(U<=1e-4)continue;j=[p/U,D/U,R/U]}const _=de([Z],N,j,[0,0,0],{occluded:a,softShadowSamples:2});if(_[0]+_[1]+_[2]<=1e-6)continue;const B=te(j);for(let p=0;p<9;p++){const D=B[p],R=Y+p*3;f[R]=(f[R]??0)+_[0]*D,f[R+1]=(f[R+1]??0)+_[1]*D,f[R+2]=(f[R+2]??0)+_[2]*D}}}}}}const c=new Uint8Array(f.buffer,f.byteOffset,f.byteLength);let m="";for(let y=0;y<c.length;y++)m+=String.fromCharCode(c[y]);const b=btoa(m);return{boundsMin:t.boundsMin,boundsMax:t.boundsMax,resolution:t.resolution,coefficientsBase64:b}}function na(t,r,e){let n=0,s=0,i=0;switch(t){case 0:n=1,s=-e,i=-r;break;case 1:n=-1,s=-e,i=r;break;case 2:n=r,s=1,i=e;break;case 3:n=r,s=-1,i=-e;break;case 4:n=r,s=-e,i=1;break;default:n=-r,s=-e,i=-1;break}const a=Math.hypot(n,s,i)||1;return[n/a,s/a,i/a]}function on(t,r,e){const n=[],s=Math.max(8,Math.min(64,(e==null?void 0:e.faceSize)??32)),i=(e==null?void 0:e.accel)??null,a=(e==null?void 0:e.ambient)??[.05,.05,.05],l=[a[0]/Math.PI,a[1]/Math.PI,a[2]/Math.PI],o=it.vec3.create(),h={t:0,triIndex:-1,baryU:0,baryV:0,normal:[0,1,0]};for(const[d,[f]]of t.query([I.ReflectionProbeComponent]).iter()){if(!I.isEntityEffectivelyVisible(t,d))continue;const u=t.getComponent(d,I.TransformComponent);u?it.mat4.getTranslation(o,u.worldTransform):it.vec3.set(o,0,0,0);const c=[o[0],o[1],o[2]],m=e!=null&&e.skyRadiance?[e.skyRadiance[0],e.skyRadiance[1],e.skyRadiance[2]]:[f.skyColor[0],f.skyColor[1],f.skyColor[2]],b=[`lightmap_refl_probe_${d}_px`,`lightmap_refl_probe_${d}_nx`,`lightmap_refl_probe_${d}_py`,`lightmap_refl_probe_${d}_ny`,`lightmap_refl_probe_${d}_pz`,`lightmap_refl_probe_${d}_nz`],y=["","","","","",""];for(let g=0;g<6;g++){const v=new Float32Array(s*s*4);for(let M=0;M<s;M++){const A=(M+.5)/s*2-1;for(let x=0;x<s;x++){const L=(x+.5)/s*2-1,E=na(g,L,A),P=i?rn(c,E,l,i,e==null?void 0:e.atlasRgba,(e==null?void 0:e.atlasSize)??0,m,h):[f.interiorColor[0]*a[0]+m[0]*.25,f.interiorColor[1]*a[1]+m[1]*.25,f.interiorColor[2]*a[2]+m[2]*.25],O=(M*s+x)*4;v[O]=P[0],v[O+1]=P[1],v[O+2]=P[2],v[O+3]=1}}const T=I.encodeRgb9e5Buffer(v,s,s);if(y[g]=oe(T),e!=null&&e.device&&r&&typeof r.registerTexture=="function"){const M=e.device.createTexture({label:b[g],size:{width:s,height:s},format:"rgb9e5ufloat",usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST}),A=Uint8Array.from(new Uint8Array(T.buffer,T.byteOffset,T.byteLength));e.device.queue.writeTexture({texture:M},A,{bytesPerRow:s*4},{width:s,height:s}),r.registerTexture(b[g],M)}}n.push({entityId:`e_${d}`,faceTextureIds:b,faceSize:s,facesBase64:y})}return n}const cn=`/**
|
|
2
|
+
* @file bake_lightmap.compute.wgsl
|
|
3
|
+
* @description GPU lightmap irradiance kernel (BIGI-7 / KPTL-5) — ordered SAH BVH traversal
|
|
4
|
+
* (without iteration cap), Khronos \`KHR_lights_punctual\` attenuation, stratified Rect/Disk Area
|
|
5
|
+
* Light NEE, power-CDF emissive triangle NEE, and emissive-safe smooth contact AO.
|
|
6
|
+
*
|
|
7
|
+
* Keep byte-for-byte in sync with \`src/bakeLightmapComputeWgsl.ts\`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
struct BakeParams {
|
|
11
|
+
atlasSize: u32,
|
|
12
|
+
tileOriginX: u32,
|
|
13
|
+
tileOriginY: u32,
|
|
14
|
+
lightCount: u32,
|
|
15
|
+
ambient: vec4<f32>,
|
|
16
|
+
bvhNodeCount: u32,
|
|
17
|
+
bvhRoot: u32,
|
|
18
|
+
softShadowSamples: u32,
|
|
19
|
+
aoSamples: u32,
|
|
20
|
+
emissiveCount: u32,
|
|
21
|
+
_pad0: u32,
|
|
22
|
+
_pad1: u32,
|
|
23
|
+
_pad2: u32,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@group(0) @binding(0) var<uniform> params: BakeParams;
|
|
27
|
+
@group(0) @binding(1) var gbufferPos: texture_2d<f32>;
|
|
28
|
+
@group(0) @binding(2) var gbufferNormal: texture_2d<f32>;
|
|
29
|
+
@group(0) @binding(3) var outputIrradiance: texture_storage_2d<rgba16float, write>;
|
|
30
|
+
@group(0) @binding(4) var<storage, read> lights: array<vec4<f32>>;
|
|
31
|
+
@group(0) @binding(5) var<storage, read> bvhNodes: array<vec4<f32>>;
|
|
32
|
+
@group(0) @binding(6) var<storage, read> bvhTris: array<vec4<f32>>;
|
|
33
|
+
@group(0) @binding(7) var<storage, read> emissiveTris: array<vec4<f32>>;
|
|
34
|
+
|
|
35
|
+
const PI: f32 = 3.14159265;
|
|
36
|
+
const GOLDEN_RATIO: f32 = 0.61803399;
|
|
37
|
+
const GOLDEN_ANGLE: f32 = 2.39996323;
|
|
38
|
+
const KHR_MIN_DIST_SQ: f32 = 0.0025;
|
|
39
|
+
const POINT_DISK_RADIUS: f32 = 0.16;
|
|
40
|
+
const SUN_TAN: f32 = 0.028;
|
|
41
|
+
const IGNORE_TRI_NONE: u32 = 0xffffffffu;
|
|
42
|
+
|
|
43
|
+
fn hash_texel(x: u32, y: u32, salt: u32) -> u32 {
|
|
44
|
+
var h = (x * 0x27d4eb2du) ^ (y * 0x165667b1u) ^ (salt * 0x9e3779b9u);
|
|
45
|
+
h = h ^ (h >> 15u);
|
|
46
|
+
h = h * 0x2c1b3c6du;
|
|
47
|
+
h = h ^ (h >> 12u);
|
|
48
|
+
return h;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
fn seed_to_pair(seed: u32) -> vec2<f32> {
|
|
52
|
+
var h = seed ^ (seed >> 16u);
|
|
53
|
+
h = h * 0x7feb352du;
|
|
54
|
+
h = h ^ (h >> 15u);
|
|
55
|
+
h = h * 0x846ca68bu;
|
|
56
|
+
h = h ^ (h >> 16u);
|
|
57
|
+
return vec2<f32>(f32(h & 0xffffu) / 65536.0, f32((h >> 16u) & 0xffffu) / 65536.0);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fn make_tangent(axis: vec3<f32>) -> vec3<f32> {
|
|
61
|
+
let up = select(vec3<f32>(1.0, 0.0, 0.0), vec3<f32>(0.0, 1.0, 0.0), abs(axis.y) < 0.99);
|
|
62
|
+
return normalize(cross(up, axis));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fn ray_aabb_t(o: vec3<f32>, d: vec3<f32>, bmin: vec3<f32>, bmax: vec3<f32>, maxDist: f32) -> f32 {
|
|
66
|
+
var tMin = 0.0;
|
|
67
|
+
var tMax = maxDist;
|
|
68
|
+
if (abs(d.x) < 1e-8) {
|
|
69
|
+
if (o.x < bmin.x || o.x > bmax.x) { return -1.0; }
|
|
70
|
+
} else {
|
|
71
|
+
let inv = 1.0 / d.x;
|
|
72
|
+
let t0 = (bmin.x - o.x) * inv;
|
|
73
|
+
let t1 = (bmax.x - o.x) * inv;
|
|
74
|
+
tMin = max(tMin, min(t0, t1));
|
|
75
|
+
tMax = min(tMax, max(t0, t1));
|
|
76
|
+
if (tMin > tMax) { return -1.0; }
|
|
77
|
+
}
|
|
78
|
+
if (abs(d.y) < 1e-8) {
|
|
79
|
+
if (o.y < bmin.y || o.y > bmax.y) { return -1.0; }
|
|
80
|
+
} else {
|
|
81
|
+
let inv = 1.0 / d.y;
|
|
82
|
+
let t0 = (bmin.y - o.y) * inv;
|
|
83
|
+
let t1 = (bmax.y - o.y) * inv;
|
|
84
|
+
tMin = max(tMin, min(t0, t1));
|
|
85
|
+
tMax = min(tMax, max(t0, t1));
|
|
86
|
+
if (tMin > tMax) { return -1.0; }
|
|
87
|
+
}
|
|
88
|
+
if (abs(d.z) < 1e-8) {
|
|
89
|
+
if (o.z < bmin.z || o.z > bmax.z) { return -1.0; }
|
|
90
|
+
} else {
|
|
91
|
+
let inv = 1.0 / d.z;
|
|
92
|
+
let t0 = (bmin.z - o.z) * inv;
|
|
93
|
+
let t1 = (bmax.z - o.z) * inv;
|
|
94
|
+
tMin = max(tMin, min(t0, t1));
|
|
95
|
+
tMax = min(tMax, max(t0, t1));
|
|
96
|
+
if (tMin > tMax) { return -1.0; }
|
|
97
|
+
}
|
|
98
|
+
return tMin;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
fn ray_tri(o: vec3<f32>, d: vec3<f32>, triIdx: u32, maxDist: f32) -> f32 {
|
|
102
|
+
let base = triIdx * 3u;
|
|
103
|
+
let v0 = bvhTris[base].xyz;
|
|
104
|
+
let e1 = bvhTris[base + 1u].xyz;
|
|
105
|
+
let e2 = bvhTris[base + 2u].xyz;
|
|
106
|
+
let h = cross(d, e2);
|
|
107
|
+
let det = dot(e1, h);
|
|
108
|
+
if (abs(det) < 1e-8) { return -1.0; }
|
|
109
|
+
let f = 1.0 / det;
|
|
110
|
+
let s = o - v0;
|
|
111
|
+
let u = f * dot(s, h);
|
|
112
|
+
if (u < 0.0 || u > 1.0) { return -1.0; }
|
|
113
|
+
let q = cross(s, e1);
|
|
114
|
+
let v = f * dot(d, q);
|
|
115
|
+
if (v < 0.0 || u + v > 1.0) { return -1.0; }
|
|
116
|
+
let t = f * dot(e2, q);
|
|
117
|
+
if (t > 1e-4 && t < maxDist) { return t; }
|
|
118
|
+
return -1.0;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
fn bvh_occluded(o: vec3<f32>, d: vec3<f32>, maxDist: f32, ignoreTri: u32) -> bool {
|
|
122
|
+
if (params.bvhNodeCount == 0u) { return false; }
|
|
123
|
+
let rootBase = params.bvhRoot * 2u;
|
|
124
|
+
if (ray_aabb_t(o, d, bvhNodes[rootBase].xyz, bvhNodes[rootBase + 1u].xyz, maxDist) < 0.0) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
var stack: array<u32, 64>;
|
|
128
|
+
var sp = 1u;
|
|
129
|
+
stack[0] = params.bvhRoot;
|
|
130
|
+
while (sp > 0u) {
|
|
131
|
+
sp -= 1u;
|
|
132
|
+
let ni = stack[sp];
|
|
133
|
+
let nb = ni * 2u;
|
|
134
|
+
let n0 = bvhNodes[nb];
|
|
135
|
+
let n1 = bvhNodes[nb + 1u];
|
|
136
|
+
let meta0 = bitcast<i32>(n0.w);
|
|
137
|
+
let meta1 = bitcast<i32>(n1.w);
|
|
138
|
+
if (meta0 < 0) {
|
|
139
|
+
let triStart = u32(-1 - meta0);
|
|
140
|
+
let triCount = u32(max(meta1, 0));
|
|
141
|
+
for (var k = 0u; k < triCount; k += 1u) {
|
|
142
|
+
let tid = triStart + k;
|
|
143
|
+
if (tid == ignoreTri) { continue; }
|
|
144
|
+
if (ray_tri(o, d, tid, maxDist) > 0.0) {
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
let leftIdx = u32(meta0);
|
|
150
|
+
let rightIdx = u32(meta1);
|
|
151
|
+
let lb = leftIdx * 2u;
|
|
152
|
+
let rb = rightIdx * 2u;
|
|
153
|
+
let tL = ray_aabb_t(o, d, bvhNodes[lb].xyz, bvhNodes[lb + 1u].xyz, maxDist);
|
|
154
|
+
let tR = ray_aabb_t(o, d, bvhNodes[rb].xyz, bvhNodes[rb + 1u].xyz, maxDist);
|
|
155
|
+
if (tL >= 0.0 && tR >= 0.0) {
|
|
156
|
+
if (sp < 62u) {
|
|
157
|
+
if (tL <= tR) {
|
|
158
|
+
stack[sp] = rightIdx;
|
|
159
|
+
stack[sp + 1u] = leftIdx;
|
|
160
|
+
} else {
|
|
161
|
+
stack[sp] = leftIdx;
|
|
162
|
+
stack[sp + 1u] = rightIdx;
|
|
163
|
+
}
|
|
164
|
+
sp += 2u;
|
|
165
|
+
}
|
|
166
|
+
} else if (tL >= 0.0 && sp < 63u) {
|
|
167
|
+
stack[sp] = leftIdx;
|
|
168
|
+
sp += 1u;
|
|
169
|
+
} else if (tR >= 0.0 && sp < 63u) {
|
|
170
|
+
stack[sp] = rightIdx;
|
|
171
|
+
sp += 1u;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
fn bvh_closest_t_ao(o: vec3<f32>, d: vec3<f32>, maxDist: f32) -> f32 {
|
|
179
|
+
if (params.bvhNodeCount == 0u) { return maxDist; }
|
|
180
|
+
let rootBase = params.bvhRoot * 2u;
|
|
181
|
+
if (ray_aabb_t(o, d, bvhNodes[rootBase].xyz, bvhNodes[rootBase + 1u].xyz, maxDist) < 0.0) {
|
|
182
|
+
return maxDist;
|
|
183
|
+
}
|
|
184
|
+
var bestT = maxDist;
|
|
185
|
+
var stack: array<u32, 64>;
|
|
186
|
+
var sp = 1u;
|
|
187
|
+
stack[0] = params.bvhRoot;
|
|
188
|
+
while (sp > 0u) {
|
|
189
|
+
sp -= 1u;
|
|
190
|
+
let ni = stack[sp];
|
|
191
|
+
let nb = ni * 2u;
|
|
192
|
+
let n0 = bvhNodes[nb];
|
|
193
|
+
let n1 = bvhNodes[nb + 1u];
|
|
194
|
+
if (ray_aabb_t(o, d, n0.xyz, n1.xyz, bestT) < 0.0) {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
let meta0 = bitcast<i32>(n0.w);
|
|
198
|
+
let meta1 = bitcast<i32>(n1.w);
|
|
199
|
+
if (meta0 < 0) {
|
|
200
|
+
let triStart = u32(-1 - meta0);
|
|
201
|
+
let triCount = u32(max(meta1, 0));
|
|
202
|
+
for (var k = 0u; k < triCount; k += 1u) {
|
|
203
|
+
let tid = triStart + k;
|
|
204
|
+
// Skip emissive surfaces so ceiling lamps never cast dark contact AO halos.
|
|
205
|
+
if (bvhTris[tid * 3u].w > 0.5) { continue; }
|
|
206
|
+
let t = ray_tri(o, d, tid, bestT);
|
|
207
|
+
if (t > 0.0 && t < bestT) {
|
|
208
|
+
bestT = t;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
let leftIdx = u32(meta0);
|
|
213
|
+
let rightIdx = u32(meta1);
|
|
214
|
+
let lb = leftIdx * 2u;
|
|
215
|
+
let rb = rightIdx * 2u;
|
|
216
|
+
let tL = ray_aabb_t(o, d, bvhNodes[lb].xyz, bvhNodes[lb + 1u].xyz, bestT);
|
|
217
|
+
let tR = ray_aabb_t(o, d, bvhNodes[rb].xyz, bvhNodes[rb + 1u].xyz, bestT);
|
|
218
|
+
if (tL >= 0.0 && tR >= 0.0) {
|
|
219
|
+
if (sp < 62u) {
|
|
220
|
+
if (tL <= tR) {
|
|
221
|
+
stack[sp] = rightIdx;
|
|
222
|
+
stack[sp + 1u] = leftIdx;
|
|
223
|
+
} else {
|
|
224
|
+
stack[sp] = leftIdx;
|
|
225
|
+
stack[sp + 1u] = rightIdx;
|
|
226
|
+
}
|
|
227
|
+
sp += 2u;
|
|
228
|
+
}
|
|
229
|
+
} else if (tL >= 0.0 && sp < 63u) {
|
|
230
|
+
stack[sp] = leftIdx;
|
|
231
|
+
sp += 1u;
|
|
232
|
+
} else if (tR >= 0.0 && sp < 63u) {
|
|
233
|
+
stack[sp] = rightIdx;
|
|
234
|
+
sp += 1u;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return bestT;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
fn khr_punctual_attenuation(dist: f32, range: f32) -> f32 {
|
|
242
|
+
let d2 = max(dist * dist, KHR_MIN_DIST_SQ);
|
|
243
|
+
if (range <= 0.0) {
|
|
244
|
+
return 1.0 / d2;
|
|
245
|
+
}
|
|
246
|
+
if (dist >= range) {
|
|
247
|
+
return 0.0;
|
|
248
|
+
}
|
|
249
|
+
let r = dist / range;
|
|
250
|
+
let r2 = r * r;
|
|
251
|
+
let win = clamp(1.0 - r2 * r2, 0.0, 1.0);
|
|
252
|
+
return (win * win) / d2;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
fn khr_spot_attenuation(cd: f32, innerCos: f32, outerCos: f32) -> f32 {
|
|
256
|
+
if (cd <= outerCos) { return 0.0; }
|
|
257
|
+
if (cd >= innerCos) { return 1.0; }
|
|
258
|
+
let t = clamp((cd - outerCos) / max(innerCos - outerCos, 1e-5), 0.0, 1.0);
|
|
259
|
+
return t * t;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
fn soft_dir_vis(origin: vec3<f32>, toL: vec3<f32>, seed: u32) -> f32 {
|
|
263
|
+
if (params.bvhNodeCount == 0u) { return 1.0; }
|
|
264
|
+
let nSmp = max(1u, params.softShadowSamples);
|
|
265
|
+
if (nSmp == 1u) {
|
|
266
|
+
return select(1.0, 0.0, bvh_occluded(origin, toL, 1e4, IGNORE_TRI_NONE));
|
|
267
|
+
}
|
|
268
|
+
let cp = seed_to_pair(seed);
|
|
269
|
+
let t1 = make_tangent(toL);
|
|
270
|
+
let t2 = cross(toL, t1);
|
|
271
|
+
let rotAngle = cp.y * 2.0 * PI;
|
|
272
|
+
var vis = 0.0;
|
|
273
|
+
for (var i = 0u; i < nSmp; i += 1u) {
|
|
274
|
+
let rad = sqrt(min(0.9999, (f32(i) + cp.x) / f32(nSmp))) * SUN_TAN;
|
|
275
|
+
let ang = f32(i) * GOLDEN_ANGLE + rotAngle;
|
|
276
|
+
let dir = normalize(toL + t1 * (cos(ang) * rad) + t2 * (sin(ang) * rad));
|
|
277
|
+
if (!bvh_occluded(origin, dir, 1e4, IGNORE_TRI_NONE)) {
|
|
278
|
+
vis += 1.0;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return vis / f32(nSmp);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
fn soft_point_vis(origin: vec3<f32>, surfN: vec3<f32>, lightPos: vec3<f32>, radius: f32, seed: u32) -> f32 {
|
|
285
|
+
if (params.bvhNodeCount == 0u) { return 1.0; }
|
|
286
|
+
let lvec = lightPos - origin;
|
|
287
|
+
let dist = length(lvec);
|
|
288
|
+
if (dist < 1e-4) { return 1.0; }
|
|
289
|
+
let nSmp = max(1u, params.softShadowSamples);
|
|
290
|
+
if (nSmp == 1u || radius <= 1e-4) {
|
|
291
|
+
return select(1.0, 0.0, bvh_occluded(origin, lvec / dist, max(0.0, dist - 2e-3), IGNORE_TRI_NONE));
|
|
292
|
+
}
|
|
293
|
+
let cp = seed_to_pair(seed);
|
|
294
|
+
let axis = lvec / dist;
|
|
295
|
+
let t1 = make_tangent(axis);
|
|
296
|
+
let t2 = cross(axis, t1);
|
|
297
|
+
let effR = min(radius, dist * 0.45);
|
|
298
|
+
let rotAngle = cp.y * 2.0 * PI;
|
|
299
|
+
var vis = 0.0;
|
|
300
|
+
var validSmp = 0.0;
|
|
301
|
+
for (var i = 0u; i < nSmp; i += 1u) {
|
|
302
|
+
let rad = sqrt(min(0.9999, (f32(i) + cp.x) / f32(nSmp))) * effR;
|
|
303
|
+
let ang = f32(i) * GOLDEN_ANGLE + rotAngle;
|
|
304
|
+
let samplePos = lightPos + t1 * (cos(ang) * rad) + t2 * (sin(ang) * rad);
|
|
305
|
+
let sv = samplePos - origin;
|
|
306
|
+
let sd = length(sv);
|
|
307
|
+
if (sd < 1e-4) {
|
|
308
|
+
vis += 1.0;
|
|
309
|
+
validSmp += 1.0;
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
let sdir = sv / sd;
|
|
313
|
+
if (dot(surfN, sdir) <= 1.5e-3) {
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
validSmp += 1.0;
|
|
317
|
+
if (!bvh_occluded(origin, sdir, max(0.0, sd - 2e-3), IGNORE_TRI_NONE)) {
|
|
318
|
+
vis += 1.0;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (validSmp < 0.5) { return 0.0; }
|
|
322
|
+
return vis / validSmp;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
fn eval_area_light_nee(
|
|
326
|
+
p: vec3<f32>,
|
|
327
|
+
shadowOrigin: vec3<f32>,
|
|
328
|
+
n: vec3<f32>,
|
|
329
|
+
pos: vec3<f32>,
|
|
330
|
+
ln: vec3<f32>,
|
|
331
|
+
tangentAngle: f32,
|
|
332
|
+
isDisk: bool,
|
|
333
|
+
halfW: f32,
|
|
334
|
+
halfH: f32,
|
|
335
|
+
radiance: vec3<f32>,
|
|
336
|
+
seed: u32
|
|
337
|
+
) -> vec3<f32> {
|
|
338
|
+
let t0 = make_tangent(ln);
|
|
339
|
+
let b0 = cross(ln, t0);
|
|
340
|
+
let cA = cos(tangentAngle);
|
|
341
|
+
let sA = sin(tangentAngle);
|
|
342
|
+
let tAxis = t0 * cA + b0 * sA;
|
|
343
|
+
let bAxis = cross(ln, tAxis);
|
|
344
|
+
|
|
345
|
+
let emitterArea = select(4.0 * halfW * halfH, PI * halfW * halfH, isDisk);
|
|
346
|
+
if (emitterArea <= 1e-7) { return vec3<f32>(0.0); }
|
|
347
|
+
|
|
348
|
+
let nSmp = max(4u, params.softShadowSamples);
|
|
349
|
+
let cols = max(1u, u32(ceil(sqrt(f32(nSmp)))));
|
|
350
|
+
let rows = max(1u, u32(ceil(f32(nSmp) / f32(cols))));
|
|
351
|
+
let cp = seed_to_pair(seed);
|
|
352
|
+
|
|
353
|
+
var acc = vec3<f32>(0.0);
|
|
354
|
+
var k = 0u;
|
|
355
|
+
for (var rr = 0u; rr < rows; rr += 1u) {
|
|
356
|
+
for (var cc = 0u; cc < cols; cc += 1u) {
|
|
357
|
+
if (k >= nSmp) { break; }
|
|
358
|
+
var ox = 0.0;
|
|
359
|
+
var oy = 0.0;
|
|
360
|
+
if (isDisk) {
|
|
361
|
+
let u1 = fract((f32(k) + cp.x) / f32(nSmp));
|
|
362
|
+
let u2 = fract(cp.y + f32(k) * GOLDEN_RATIO);
|
|
363
|
+
let rad = sqrt(max(u1, 0.0));
|
|
364
|
+
let phi = 2.0 * PI * u2;
|
|
365
|
+
ox = rad * cos(phi) * halfW;
|
|
366
|
+
oy = rad * sin(phi) * halfH;
|
|
367
|
+
} else {
|
|
368
|
+
let u = (f32(cc) + fract(cp.x + f32(k) * GOLDEN_RATIO)) / f32(cols) * 2.0 - 1.0;
|
|
369
|
+
let v = (f32(rr) + fract(cp.y + f32(k) * 0.75487766)) / f32(rows) * 2.0 - 1.0;
|
|
370
|
+
ox = u * halfW;
|
|
371
|
+
oy = v * halfH;
|
|
372
|
+
}
|
|
373
|
+
k += 1u;
|
|
374
|
+
|
|
375
|
+
let samplePos = pos + tAxis * ox + bAxis * oy;
|
|
376
|
+
let toL = samplePos - p;
|
|
377
|
+
let distSq = dot(toL, toL);
|
|
378
|
+
if (distSq <= 1e-8) { continue; }
|
|
379
|
+
let dist = sqrt(distSq);
|
|
380
|
+
let lDir = toL / dist;
|
|
381
|
+
let ndl = dot(n, lDir);
|
|
382
|
+
if (ndl <= 1.5e-3) { continue; }
|
|
383
|
+
let cosLight = dot(ln, -lDir);
|
|
384
|
+
if (cosLight <= 1e-4) { continue; }
|
|
385
|
+
|
|
386
|
+
if (params.bvhNodeCount > 0u) {
|
|
387
|
+
let sv = samplePos - shadowOrigin;
|
|
388
|
+
let sd = length(sv);
|
|
389
|
+
if (sd > 2e-3 && bvh_occluded(shadowOrigin, sv / sd, sd - 2e-3, IGNORE_TRI_NONE)) {
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
let w = (ndl * cosLight * emitterArea) / max(distSq, 1e-6);
|
|
395
|
+
acc += radiance * w;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (k == 0u) { return vec3<f32>(0.0); }
|
|
399
|
+
return acc / f32(k);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
fn eval_emissive_catalog(p: vec3<f32>, shadowOrigin: vec3<f32>, n: vec3<f32>, seed: u32) -> vec3<f32> {
|
|
403
|
+
let count = params.emissiveCount;
|
|
404
|
+
if (count == 0u) { return vec3<f32>(0.0); }
|
|
405
|
+
let totalPower = emissiveTris[3].w;
|
|
406
|
+
if (totalPower <= 0.0) { return vec3<f32>(0.0); }
|
|
407
|
+
|
|
408
|
+
let nSmp = max(4u, params.softShadowSamples);
|
|
409
|
+
let cp = seed_to_pair(seed ^ 0x6d2b79f5u);
|
|
410
|
+
var acc = vec3<f32>(0.0);
|
|
411
|
+
|
|
412
|
+
for (var s = 0u; s < nSmp; s += 1u) {
|
|
413
|
+
let uPick = min(0.999999, (f32(s) + cp.x) / f32(nSmp));
|
|
414
|
+
var lo = 0u;
|
|
415
|
+
var hi = count - 1u;
|
|
416
|
+
while (lo < hi) {
|
|
417
|
+
let mid = (lo + hi) >> 1u;
|
|
418
|
+
let cdfMid = emissiveTris[mid * 4u].w;
|
|
419
|
+
if (uPick <= cdfMid) {
|
|
420
|
+
hi = mid;
|
|
421
|
+
} else {
|
|
422
|
+
lo = mid + 1u;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
let eb = lo * 4u;
|
|
427
|
+
let v0Cdf = emissiveTris[eb];
|
|
428
|
+
let e1Area = emissiveTris[eb + 1u];
|
|
429
|
+
let e2Idx = emissiveTris[eb + 2u];
|
|
430
|
+
let emPow = emissiveTris[eb + 3u];
|
|
431
|
+
if (e1Area.w <= 1e-10) { continue; }
|
|
432
|
+
|
|
433
|
+
let u1 = fract(cp.y + f32(s) * GOLDEN_RATIO);
|
|
434
|
+
let u2 = fract(cp.x * 0.713 + f32(s) * 0.75487766);
|
|
435
|
+
let su = sqrt(u1);
|
|
436
|
+
let b1 = u2 * su;
|
|
437
|
+
let b2 = su - b1;
|
|
438
|
+
let samplePos = v0Cdf.xyz + e1Area.xyz * b1 + e2Idx.xyz * b2;
|
|
439
|
+
|
|
440
|
+
let toL = samplePos - p;
|
|
441
|
+
let distSq = dot(toL, toL);
|
|
442
|
+
if (distSq <= 1e-8) { continue; }
|
|
443
|
+
let dist = sqrt(distSq);
|
|
444
|
+
let lDir = toL / dist;
|
|
445
|
+
let cosSurf = dot(n, lDir);
|
|
446
|
+
if (cosSurf <= 1.5e-3) { continue; }
|
|
447
|
+
|
|
448
|
+
let rawN = cross(e1Area.xyz, e2Idx.xyz);
|
|
449
|
+
let rawLen = length(rawN);
|
|
450
|
+
if (rawLen <= 1e-10) { continue; }
|
|
451
|
+
let triN = rawN / rawLen;
|
|
452
|
+
let cosLight = dot(triN, -lDir);
|
|
453
|
+
if (cosLight <= 1e-4) { continue; }
|
|
454
|
+
|
|
455
|
+
let emRgb = emPow.xyz;
|
|
456
|
+
let lum = 0.2126 * emRgb.x + 0.7152 * emRgb.y + 0.0722 * emRgb.z;
|
|
457
|
+
if (lum <= 1e-7) { continue; }
|
|
458
|
+
|
|
459
|
+
let leafTriIdx = u32(e2Idx.w + 0.5);
|
|
460
|
+
if (params.bvhNodeCount > 0u) {
|
|
461
|
+
let sv = samplePos - shadowOrigin;
|
|
462
|
+
let sd = length(sv);
|
|
463
|
+
if (sd > 2e-3 && bvh_occluded(shadowOrigin, sv / sd, sd - 2e-3, leafTriIdx)) {
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
let w = (cosSurf * cosLight * totalPower) / (PI * lum * max(distSq, KHR_MIN_DIST_SQ));
|
|
469
|
+
acc += emRgb * w;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
return acc / f32(nSmp);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
fn eval_ao(p: vec3<f32>, n: vec3<f32>, seed: u32) -> f32 {
|
|
476
|
+
let radius = params.ambient.w;
|
|
477
|
+
if (params.bvhNodeCount == 0u || params.aoSamples == 0u || radius <= 1e-4) {
|
|
478
|
+
return 1.0;
|
|
479
|
+
}
|
|
480
|
+
let origin = p + n * 1.5e-3;
|
|
481
|
+
let cp = seed_to_pair(seed ^ 0x5bd1e995u);
|
|
482
|
+
let t1 = make_tangent(n);
|
|
483
|
+
let t2 = cross(n, t1);
|
|
484
|
+
var occ = 0.0;
|
|
485
|
+
let nSmp = params.aoSamples;
|
|
486
|
+
for (var i = 0u; i < nSmp; i += 1u) {
|
|
487
|
+
let u1 = min(0.999999, (f32(i) + cp.x) / f32(nSmp));
|
|
488
|
+
let u2 = fract(f32(i) * GOLDEN_RATIO + cp.y);
|
|
489
|
+
let r = sqrt(u1);
|
|
490
|
+
let phi = 2.0 * PI * u2;
|
|
491
|
+
let z = sqrt(max(0.0, 1.0 - u1));
|
|
492
|
+
let dir = normalize(t1 * (r * cos(phi)) + t2 * (r * sin(phi)) + n * z);
|
|
493
|
+
let hitT = bvh_closest_t_ao(origin, dir, radius);
|
|
494
|
+
if (hitT < radius) {
|
|
495
|
+
let f = 1.0 - hitT / radius;
|
|
496
|
+
occ += f * f;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return max(0.35, 1.0 - 0.65 * (occ / f32(nSmp)));
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
fn light_base(i: u32) -> u32 {
|
|
503
|
+
return i * 4u;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
fn eval_direct(p: vec3<f32>, n: vec3<f32>, seed: u32) -> vec3<f32> {
|
|
507
|
+
var irr = params.ambient.xyz;
|
|
508
|
+
let shadowOrigin = p + n * 1.5e-3;
|
|
509
|
+
let count = params.lightCount;
|
|
510
|
+
for (var i = 0u; i < count; i += 1u) {
|
|
511
|
+
let b = light_base(i);
|
|
512
|
+
let a = lights[b];
|
|
513
|
+
let b1 = lights[b + 1u];
|
|
514
|
+
let b2 = lights[b + 2u];
|
|
515
|
+
let b3 = lights[b + 3u];
|
|
516
|
+
let kind = u32(a.x);
|
|
517
|
+
let color = a.yzw;
|
|
518
|
+
let intensity = b1.x;
|
|
519
|
+
let lSeed = seed ^ ((i + 1u) * 0x9e3779b9u);
|
|
520
|
+
|
|
521
|
+
if (kind == 0u) {
|
|
522
|
+
let toL = normalize(b1.yzw);
|
|
523
|
+
let ndl = max(dot(n, toL), 0.0);
|
|
524
|
+
if (ndl <= 0.0) { continue; }
|
|
525
|
+
let vis = soft_dir_vis(shadowOrigin, toL, lSeed);
|
|
526
|
+
if (vis <= 0.0) { continue; }
|
|
527
|
+
irr += color * (intensity * ndl * vis);
|
|
528
|
+
} else if (kind == 1u || kind == 2u) {
|
|
529
|
+
let pos = b1.yzw;
|
|
530
|
+
let lvec = pos - p;
|
|
531
|
+
let dist = max(length(lvec), 1e-4);
|
|
532
|
+
let toL = lvec / dist;
|
|
533
|
+
let ndl = max(dot(n, toL), 0.0);
|
|
534
|
+
if (ndl <= 0.0) { continue; }
|
|
535
|
+
let range = b2.w;
|
|
536
|
+
var atten = khr_punctual_attenuation(dist, range);
|
|
537
|
+
if (atten <= 0.0) { continue; }
|
|
538
|
+
if (kind == 2u) {
|
|
539
|
+
let dir = normalize(b2.xyz);
|
|
540
|
+
let spotAtten = khr_spot_attenuation(-dot(toL, dir), b3.x, b3.y);
|
|
541
|
+
if (spotAtten <= 0.0) { continue; }
|
|
542
|
+
atten *= spotAtten;
|
|
543
|
+
}
|
|
544
|
+
let vis = soft_point_vis(shadowOrigin, n, pos, POINT_DISK_RADIUS, lSeed);
|
|
545
|
+
if (vis <= 0.0) { continue; }
|
|
546
|
+
irr += color * (intensity * ndl * atten * vis);
|
|
547
|
+
} else if (kind == 3u) {
|
|
548
|
+
irr += eval_area_light_nee(
|
|
549
|
+
p,
|
|
550
|
+
shadowOrigin,
|
|
551
|
+
n,
|
|
552
|
+
b1.yzw,
|
|
553
|
+
normalize(b2.xyz),
|
|
554
|
+
b3.x,
|
|
555
|
+
b3.y > 0.5,
|
|
556
|
+
max(b3.z, 1e-3),
|
|
557
|
+
max(b3.w, 1e-3),
|
|
558
|
+
color * intensity,
|
|
559
|
+
lSeed
|
|
560
|
+
);
|
|
561
|
+
} else if (params.emissiveCount == 0u) {
|
|
562
|
+
irr += eval_area_light_nee(
|
|
563
|
+
p,
|
|
564
|
+
shadowOrigin,
|
|
565
|
+
n,
|
|
566
|
+
b1.yzw,
|
|
567
|
+
normalize(b2.xyz),
|
|
568
|
+
0.0,
|
|
569
|
+
false,
|
|
570
|
+
max(b3.z, 1e-3),
|
|
571
|
+
max(b3.w, 1e-3),
|
|
572
|
+
color * intensity,
|
|
573
|
+
lSeed
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
irr += eval_emissive_catalog(p, shadowOrigin, n, seed);
|
|
578
|
+
return irr;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
@compute @workgroup_size(8, 8, 1)
|
|
582
|
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
583
|
+
let x = params.tileOriginX + gid.x;
|
|
584
|
+
let y = params.tileOriginY + gid.y;
|
|
585
|
+
if (x >= params.atlasSize || y >= params.atlasSize) {
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
let uv = vec2<i32>(i32(x), i32(y));
|
|
589
|
+
let posS = textureLoad(gbufferPos, uv, 0);
|
|
590
|
+
if (posS.a < 0.5) {
|
|
591
|
+
textureStore(outputIrradiance, uv, vec4<f32>(0.0));
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
let n = normalize(textureLoad(gbufferNormal, uv, 0).xyz);
|
|
595
|
+
let seed = hash_texel(x, y, 1u);
|
|
596
|
+
let irr = eval_direct(posS.xyz, n, seed);
|
|
597
|
+
let ao = eval_ao(posS.xyz, n, seed);
|
|
598
|
+
textureStore(outputIrradiance, uv, vec4<f32>(irr, clamp(ao, 0.05, 1.0)));
|
|
599
|
+
}
|
|
600
|
+
`,ve=16;function un(t){const r=new Float32Array(Math.max(1,t.length)*ve);for(let e=0;e<t.length;e++){const n=t[e],s=e*ve,i=n.kind==="directional"?0:n.kind==="point"?1:n.kind==="spot"?2:n.kind==="area"?3:4;if(r[s]=i,r[s+1]=n.color[0],r[s+2]=n.color[1],r[s+3]=n.color[2],r[s+4]=n.intensity,n.kind==="directional"?(r[s+5]=n.toLight[0],r[s+6]=n.toLight[1],r[s+7]=n.toLight[2]):(r[s+5]=n.position[0],r[s+6]=n.position[1],r[s+7]=n.position[2]),n.kind==="spot")r[s+8]=n.direction[0],r[s+9]=n.direction[1],r[s+10]=n.direction[2],r[s+11]=n.range,r[s+12]=n.innerCos,r[s+13]=n.outerCos;else if(n.kind==="point")r[s+11]=n.range;else if(n.kind==="area"){r[s+8]=n.normal[0],r[s+9]=n.normal[1],r[s+10]=n.normal[2],r[s+11]=n.area;const a=Math.sqrt(Math.max(1e-6,n.area)),l=(n.width??a)*.5,o=(n.height??a)*.5;let h=0;if(n.tangent){const d=me(n.normal),f=n.normal[1]*d[2]-n.normal[2]*d[1],u=n.normal[2]*d[0]-n.normal[0]*d[2],c=n.normal[0]*d[1]-n.normal[1]*d[0],m=n.tangent[0]*d[0]+n.tangent[1]*d[1]+n.tangent[2]*d[2],b=n.tangent[0]*f+n.tangent[1]*u+n.tangent[2]*c;h=Math.atan2(b,m)}r[s+12]=h,r[s+13]=n.isDisk?1:0,r[s+14]=l,r[s+15]=o}else if(n.kind==="emissive"){r[s+8]=n.normal[0],r[s+9]=n.normal[1],r[s+10]=n.normal[2],r[s+11]=n.area;const a=Math.sqrt(Math.max(1e-6,n.area))*.5;r[s+14]=a,r[s+15]=a}}return r}const aa=ve,ln=256;async function fn(t,r,e,n){var Z,j;if((Z=n==null?void 0:n.signal)!=null&&Z.aborted)throw new DOMException("Bake aborted","AbortError");const s=r.atlasSize,i=(n==null?void 0:n.ambient)??[.08,.08,.1];n==null||n.tileSize;const a=(n==null?void 0:n.accel)??null,l=a?Math.max(1,(n==null?void 0:n.softShadowSamples)??8):0,o=a?Math.max(0,(n==null?void 0:n.aoSamples)??8):0,h=Math.max(0,(n==null?void 0:n.aoRadius)??.45),d=t.createTexture({label:"lightmap_gbuffer_pos",size:{width:s,height:s},format:"rgba32float",usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST}),f=t.createTexture({label:"lightmap_gbuffer_nrm",size:{width:s,height:s},format:"rgba32float",usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST}),u=t.createTexture({label:"lightmap_gpu_direct",size:{width:s,height:s},format:"rgba16float",usage:GPUTextureUsage.STORAGE_BINDING|GPUTextureUsage.COPY_SRC}),c=xt(r.position),m=xt(r.normal);t.queue.writeTexture({texture:d},c,{bytesPerRow:s*16},{width:s,height:s}),t.queue.writeTexture({texture:f},m,{bytesPerRow:s*16},{width:s,height:s});const b=un(e),y=t.createBuffer({label:"lightmap_bake_lights",size:Math.max(16,b.byteLength),usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST});t.queue.writeBuffer(y,0,xt(b));const g=a?ue(a):null,v=g?new Uint8Array(g.nodeBytes):new Uint8Array(32),T=g?xt(g.triFloats):new Uint8Array(48),M=g?xt(g.emissiveFloats):new Uint8Array(64),A=t.createBuffer({label:"lightmap_bvh_nodes",size:Math.max(32,v.byteLength),usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST});t.queue.writeBuffer(A,0,v);const x=t.createBuffer({label:"lightmap_bvh_tris",size:Math.max(48,T.byteLength),usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST});t.queue.writeBuffer(x,0,T);const L=t.createBuffer({label:"lightmap_emissive_tris",size:Math.max(64,M.byteLength),usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST});t.queue.writeBuffer(L,0,M);const E=new ArrayBuffer(64),P=new Uint32Array(E,0,4),O=new Float32Array(E,16,4),X=new Uint32Array(E,32,4),w=new Uint32Array(E,48,4);P[0]=s,P[1]=0,P[2]=0,P[3]=e.length,O[0]=i[0],O[1]=i[1],O[2]=i[2],O[3]=h,X[0]=g?g.nodeCount:0,X[1]=g?g.rootIndex:0,X[2]=l,X[3]=o,w[0]=g?g.emissiveCount:0;const V=t.createBuffer({label:"lightmap_bake_params",size:64,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST});t.queue.writeBuffer(V,0,E);const q=t.createShaderModule({label:"bake_lightmap.compute",code:cn}),$=t.createComputePipeline({label:"lightmap_bake_pipeline",layout:"auto",compute:{module:q,entryPoint:"main"}}),z=t.createBindGroup({layout:$.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:V}},{binding:1,resource:d.createView()},{binding:2,resource:f.createView()},{binding:3,resource:u.createView()},{binding:4,resource:{buffer:y}},{binding:5,resource:{buffer:A}},{binding:6,resource:{buffer:x}},{binding:7,resource:{buffer:L}}]});if((j=n==null?void 0:n.signal)!=null&&j.aborted)throw new DOMException("Bake aborted","AbortError");const W=t.createCommandEncoder({label:"lightmap_gpu_bake"}),F=W.beginComputePass({label:"lightmap_gpu_direct"});F.setPipeline($),F.setBindGroup(0,z);const N=Math.ceil(s/8);F.dispatchWorkgroups(N,N,1),F.end();const Y=await hn(t,W,u,s);return d.destroy(),f.destroy(),u.destroy(),y.destroy(),A.destroy(),x.destroy(),L.destroy(),V.destroy(),Y}function xt(t){const r=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),e=new Uint8Array(r.byteLength);return e.set(r),e}function ee(t){const r=t>>15&1,e=t>>10&31,n=t&1023;return e===0?(r?-1:1)*Math.pow(2,-14)*(n/1024):e===31?n!==0?NaN:r?-1/0:1/0:(r?-1:1)*Math.pow(2,e-15)*(1+n/1024)}async function hn(t,r,e,n){const i=Math.ceil(n*8/ln)*ln,a=t.createBuffer({label:"lightmap_gpu_readback",size:i*n,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});r.copyTextureToBuffer({texture:e},{buffer:a,bytesPerRow:i,rowsPerImage:n},{width:n,height:n}),t.queue.submit([r.finish()]),await t.queue.onSubmittedWorkDone(),await a.mapAsync(GPUMapMode.READ);const l=new Uint8Array(a.getMappedRange()),o=new Uint8Array(l.length);o.set(l),a.unmap(),a.destroy();const h=new Float32Array(n*n*4),d=new DataView(o.buffer,o.byteOffset,o.byteLength);let f=0;for(let u=0;u<n;u++){const c=u*i;for(let m=0;m<n;m++){const b=c+m*8;h[f++]=ee(d.getUint16(b,!0)),h[f++]=ee(d.getUint16(b+2,!0)),h[f++]=ee(d.getUint16(b+4,!0)),h[f++]=ee(d.getUint16(b+6,!0))}}return h}const mn=`/**
|
|
601
|
+
* @file bake_gi_bounce.compute.wgsl
|
|
602
|
+
* @description GPU multi-bounce diffuse GI radiosity kernel (KPTL-6) — ordered SAH BVH traversal
|
|
603
|
+
* (without iteration cap), barycentric \`UV1\` texel-space lookup into \`prevBounceTex\`, sRGB-decoded
|
|
604
|
+
* \`(1 - metallic)\` diffuse reflectance, and sky/HDRI environment fallback on escaped rays.
|
|
605
|
+
*
|
|
606
|
+
* Keep byte-for-byte in sync with \`src/bakeGiBounceComputeWgsl.ts\`.
|
|
607
|
+
*/
|
|
608
|
+
|
|
609
|
+
struct GiBounceParams {
|
|
610
|
+
atlasSize: u32,
|
|
611
|
+
bounceIndex: u32,
|
|
612
|
+
bounceSamples: u32,
|
|
613
|
+
bvhNodeCount: u32,
|
|
614
|
+
bvhRoot: u32,
|
|
615
|
+
_pad0: u32,
|
|
616
|
+
_pad1: u32,
|
|
617
|
+
_pad2: u32,
|
|
618
|
+
skyAndMaxDist: vec4<f32>,
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
@group(0) @binding(0) var<uniform> params: GiBounceParams;
|
|
622
|
+
@group(0) @binding(1) var gbufferPos: texture_2d<f32>;
|
|
623
|
+
@group(0) @binding(2) var gbufferNormal: texture_2d<f32>;
|
|
624
|
+
@group(0) @binding(3) var prevBounceTex: texture_2d<f32>;
|
|
625
|
+
@group(0) @binding(4) var outputBounce: texture_storage_2d<rgba16float, write>;
|
|
626
|
+
@group(0) @binding(5) var<storage, read> bvhNodes: array<vec4<f32>>;
|
|
627
|
+
@group(0) @binding(6) var<storage, read> bvhTris: array<vec4<f32>>;
|
|
628
|
+
@group(0) @binding(7) var<storage, read> triUv1: array<vec4<f32>>;
|
|
629
|
+
@group(0) @binding(8) var<storage, read> triAlbedo: array<vec4<f32>>;
|
|
630
|
+
@group(0) @binding(9) var<storage, read> triFallbackBounce: array<vec4<f32>>;
|
|
631
|
+
|
|
632
|
+
const PI: f32 = 3.14159265;
|
|
633
|
+
const GOLDEN_RATIO: f32 = 0.61803399;
|
|
634
|
+
|
|
635
|
+
struct BounceHit {
|
|
636
|
+
hit: bool,
|
|
637
|
+
t: f32,
|
|
638
|
+
triIdx: u32,
|
|
639
|
+
u: f32,
|
|
640
|
+
v: f32,
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
fn hash_texel(x: u32, y: u32, salt: u32) -> u32 {
|
|
644
|
+
var h = (x * 0x27d4eb2du) ^ (y * 0x165667b1u) ^ (salt * 0x9e3779b9u);
|
|
645
|
+
h = h ^ (h >> 15u);
|
|
646
|
+
h = h * 0x2c1b3c6du;
|
|
647
|
+
h = h ^ (h >> 12u);
|
|
648
|
+
return h;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
fn seed_to_pair(seed: u32) -> vec2<f32> {
|
|
652
|
+
var h = seed ^ (seed >> 16u);
|
|
653
|
+
h = h * 0x7feb352du;
|
|
654
|
+
h = h ^ (h >> 15u);
|
|
655
|
+
h = h * 0x846ca68bu;
|
|
656
|
+
h = h ^ (h >> 16u);
|
|
657
|
+
return vec2<f32>(f32(h & 0xffffu) / 65536.0, f32((h >> 16u) & 0xffffu) / 65536.0);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
fn make_tangent(axis: vec3<f32>) -> vec3<f32> {
|
|
661
|
+
let up = select(vec3<f32>(1.0, 0.0, 0.0), vec3<f32>(0.0, 1.0, 0.0), abs(axis.y) < 0.999);
|
|
662
|
+
return normalize(cross(up, axis));
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
fn ray_aabb_t(o: vec3<f32>, d: vec3<f32>, bmin: vec3<f32>, bmax: vec3<f32>, maxDist: f32) -> f32 {
|
|
666
|
+
var tMin = 0.0;
|
|
667
|
+
var tMax = maxDist;
|
|
668
|
+
if (abs(d.x) < 1e-8) {
|
|
669
|
+
if (o.x < bmin.x || o.x > bmax.x) { return -1.0; }
|
|
670
|
+
} else {
|
|
671
|
+
let inv = 1.0 / d.x;
|
|
672
|
+
let t0 = (bmin.x - o.x) * inv;
|
|
673
|
+
let t1 = (bmax.x - o.x) * inv;
|
|
674
|
+
tMin = max(tMin, min(t0, t1));
|
|
675
|
+
tMax = min(tMax, max(t0, t1));
|
|
676
|
+
if (tMin > tMax) { return -1.0; }
|
|
677
|
+
}
|
|
678
|
+
if (abs(d.y) < 1e-8) {
|
|
679
|
+
if (o.y < bmin.y || o.y > bmax.y) { return -1.0; }
|
|
680
|
+
} else {
|
|
681
|
+
let inv = 1.0 / d.y;
|
|
682
|
+
let t0 = (bmin.y - o.y) * inv;
|
|
683
|
+
let t1 = (bmax.y - o.y) * inv;
|
|
684
|
+
tMin = max(tMin, min(t0, t1));
|
|
685
|
+
tMax = min(tMax, max(t0, t1));
|
|
686
|
+
if (tMin > tMax) { return -1.0; }
|
|
687
|
+
}
|
|
688
|
+
if (abs(d.z) < 1e-8) {
|
|
689
|
+
if (o.z < bmin.z || o.z > bmax.z) { return -1.0; }
|
|
690
|
+
} else {
|
|
691
|
+
let inv = 1.0 / d.z;
|
|
692
|
+
let t0 = (bmin.z - o.z) * inv;
|
|
693
|
+
let t1 = (bmax.z - o.z) * inv;
|
|
694
|
+
tMin = max(tMin, min(t0, t1));
|
|
695
|
+
tMax = min(tMax, max(t0, t1));
|
|
696
|
+
if (tMin > tMax) { return -1.0; }
|
|
697
|
+
}
|
|
698
|
+
return tMin;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
fn bvh_closest_hit(o: vec3<f32>, d: vec3<f32>, maxDist: f32) -> BounceHit {
|
|
702
|
+
var res: BounceHit;
|
|
703
|
+
res.hit = false;
|
|
704
|
+
res.t = maxDist;
|
|
705
|
+
res.triIdx = 0u;
|
|
706
|
+
res.u = 0.0;
|
|
707
|
+
res.v = 0.0;
|
|
708
|
+
|
|
709
|
+
if (params.bvhNodeCount == 0u) { return res; }
|
|
710
|
+
let rootBase = params.bvhRoot * 2u;
|
|
711
|
+
if (ray_aabb_t(o, d, bvhNodes[rootBase].xyz, bvhNodes[rootBase + 1u].xyz, maxDist) < 0.0) {
|
|
712
|
+
return res;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
var stack: array<u32, 64>;
|
|
716
|
+
var sp = 1u;
|
|
717
|
+
stack[0] = params.bvhRoot;
|
|
718
|
+
while (sp > 0u) {
|
|
719
|
+
sp -= 1u;
|
|
720
|
+
let ni = stack[sp];
|
|
721
|
+
let nb = ni * 2u;
|
|
722
|
+
let n0 = bvhNodes[nb];
|
|
723
|
+
let n1 = bvhNodes[nb + 1u];
|
|
724
|
+
if (ray_aabb_t(o, d, n0.xyz, n1.xyz, res.t) < 0.0) {
|
|
725
|
+
continue;
|
|
726
|
+
}
|
|
727
|
+
let meta0 = bitcast<i32>(n0.w);
|
|
728
|
+
let meta1 = bitcast<i32>(n1.w);
|
|
729
|
+
if (meta0 < 0) {
|
|
730
|
+
let triStart = u32(-1 - meta0);
|
|
731
|
+
let triCount = u32(max(meta1, 0));
|
|
732
|
+
for (var k = 0u; k < triCount; k += 1u) {
|
|
733
|
+
let tid = triStart + k;
|
|
734
|
+
let base = tid * 3u;
|
|
735
|
+
let v0 = bvhTris[base].xyz;
|
|
736
|
+
let e1 = bvhTris[base + 1u].xyz;
|
|
737
|
+
let e2 = bvhTris[base + 2u].xyz;
|
|
738
|
+
let h = cross(d, e2);
|
|
739
|
+
let det = dot(e1, h);
|
|
740
|
+
if (abs(det) < 1e-8) { continue; }
|
|
741
|
+
let f = 1.0 / det;
|
|
742
|
+
let s = o - v0;
|
|
743
|
+
let u = f * dot(s, h);
|
|
744
|
+
if (u < 0.0 || u > 1.0) { continue; }
|
|
745
|
+
let q = cross(s, e1);
|
|
746
|
+
let v = f * dot(d, q);
|
|
747
|
+
if (v < 0.0 || u + v > 1.0) { continue; }
|
|
748
|
+
let t = f * dot(e2, q);
|
|
749
|
+
if (t > 1e-4 && t < res.t) {
|
|
750
|
+
res.hit = true;
|
|
751
|
+
res.t = t;
|
|
752
|
+
res.triIdx = tid;
|
|
753
|
+
res.u = u;
|
|
754
|
+
res.v = v;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
} else {
|
|
758
|
+
let leftIdx = u32(meta0);
|
|
759
|
+
let rightIdx = u32(meta1);
|
|
760
|
+
let lb = leftIdx * 2u;
|
|
761
|
+
let rb = rightIdx * 2u;
|
|
762
|
+
let tL = ray_aabb_t(o, d, bvhNodes[lb].xyz, bvhNodes[lb + 1u].xyz, res.t);
|
|
763
|
+
let tR = ray_aabb_t(o, d, bvhNodes[rb].xyz, bvhNodes[rb + 1u].xyz, res.t);
|
|
764
|
+
if (tL >= 0.0 && tR >= 0.0) {
|
|
765
|
+
if (sp < 62u) {
|
|
766
|
+
if (tL <= tR) {
|
|
767
|
+
stack[sp] = rightIdx;
|
|
768
|
+
stack[sp + 1u] = leftIdx;
|
|
769
|
+
} else {
|
|
770
|
+
stack[sp] = leftIdx;
|
|
771
|
+
stack[sp + 1u] = rightIdx;
|
|
772
|
+
}
|
|
773
|
+
sp += 2u;
|
|
774
|
+
}
|
|
775
|
+
} else if (tL >= 0.0 && sp < 63u) {
|
|
776
|
+
stack[sp] = leftIdx;
|
|
777
|
+
sp += 1u;
|
|
778
|
+
} else if (tR >= 0.0 && sp < 63u) {
|
|
779
|
+
stack[sp] = rightIdx;
|
|
780
|
+
sp += 1u;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
return res;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
@compute @workgroup_size(8, 8, 1)
|
|
788
|
+
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
789
|
+
let x = gid.x;
|
|
790
|
+
let y = gid.y;
|
|
791
|
+
if (x >= params.atlasSize || y >= params.atlasSize) {
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
let uv = vec2<i32>(i32(x), i32(y));
|
|
795
|
+
let posS = textureLoad(gbufferPos, uv, 0);
|
|
796
|
+
if (posS.a < 0.5) {
|
|
797
|
+
textureStore(outputBounce, uv, vec4<f32>(0.0));
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
let n = normalize(textureLoad(gbufferNormal, uv, 0).xyz);
|
|
802
|
+
let origin = posS.xyz + n * 1.5e-3;
|
|
803
|
+
let nSmp = max(1u, params.bounceSamples);
|
|
804
|
+
let seed = hash_texel(x, y, 101u + params.bounceIndex * 37u);
|
|
805
|
+
let cp = seed_to_pair(seed);
|
|
806
|
+
let t1 = make_tangent(n);
|
|
807
|
+
let t2 = cross(n, t1);
|
|
808
|
+
let maxDist = max(0.5, params.skyAndMaxDist.w);
|
|
809
|
+
let skyRad = params.skyAndMaxDist.xyz;
|
|
810
|
+
let atlasMax = i32(params.atlasSize) - 1;
|
|
811
|
+
let atlasF = f32(params.atlasSize);
|
|
812
|
+
|
|
813
|
+
var acc = vec3<f32>(0.0);
|
|
814
|
+
for (var i = 0u; i < nSmp; i += 1u) {
|
|
815
|
+
let u1 = min(0.999999, (f32(i) + cp.x) / f32(nSmp));
|
|
816
|
+
let u2 = fract(f32(i) * GOLDEN_RATIO + cp.y);
|
|
817
|
+
let r = sqrt(u1);
|
|
818
|
+
let phi = 2.0 * PI * u2;
|
|
819
|
+
let z = sqrt(max(0.0, 1.0 - u1));
|
|
820
|
+
let dir = normalize(t1 * (r * cos(phi)) + t2 * (r * sin(phi)) + n * z);
|
|
821
|
+
|
|
822
|
+
let hit = bvh_closest_hit(origin, dir, maxDist);
|
|
823
|
+
if (!hit.hit) {
|
|
824
|
+
acc += skyRad;
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
let alb = triAlbedo[hit.triIdx].xyz;
|
|
829
|
+
var hitIrr = triFallbackBounce[hit.triIdx].xyz;
|
|
830
|
+
|
|
831
|
+
let uBase = hit.triIdx * 2u;
|
|
832
|
+
let uvAB = triUv1[uBase];
|
|
833
|
+
let uvC = triUv1[uBase + 1u].xy;
|
|
834
|
+
let w0 = 1.0 - hit.u - hit.v;
|
|
835
|
+
let hitUv = uvAB.xy * w0 + uvAB.zw * hit.u + uvC * hit.v;
|
|
836
|
+
let tx = clamp(i32(floor(hitUv.x * atlasF)), 0, atlasMax);
|
|
837
|
+
let ty = clamp(i32(floor(hitUv.y * atlasF)), 0, atlasMax);
|
|
838
|
+
let texSmp = textureLoad(prevBounceTex, vec2<i32>(tx, ty), 0);
|
|
839
|
+
if (texSmp.a > 0.0) {
|
|
840
|
+
hitIrr = texSmp.xyz;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
let bounced = alb * hitIrr;
|
|
844
|
+
let maxCh = max(bounced.x, max(bounced.y, bounced.z));
|
|
845
|
+
if (maxCh > 2.5) {
|
|
846
|
+
let clamped = 2.5 + (maxCh - 2.5) / (1.0 + (maxCh - 2.5) * 0.5);
|
|
847
|
+
acc += bounced * (clamped / maxCh);
|
|
848
|
+
} else {
|
|
849
|
+
acc += bounced;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
textureStore(outputBounce, uv, vec4<f32>(acc / f32(nSmp), 1.0));
|
|
854
|
+
}
|
|
855
|
+
`;function dn(t){const r=t.triOrder.length,e=new Float32Array(Math.max(1,r)*4);for(let n=0;n<r;n++){const i=t.triOrder[n]*3,a=n*4;e[a]=t.bounceSrc[i],e[a+1]=t.bounceSrc[i+1],e[a+2]=t.bounceSrc[i+2],e[a+3]=1}return e}async function gn(t,r,e,n,s,i){var _,B,p;if((_=i.signal)!=null&&_.aborted)throw new DOMException("Bake aborted","AbortError");const a=r.atlasSize,l=a*a,o=Math.max(0,i.bounces|0),h=new Float32Array(l*4);if(o===0||s.length===0)return h;const d=Math.max(8,i.bounceSamples??24),f=Math.max(1,i.maxDist??25),u=i.skyRadiance??[0,0,0],c=i.denoise!==!1,m=[1,2,4,8],b=new Float32Array(l*4);for(let D=0;D<s.length;D++){const R=s[D]*4;b[R]=n[R],b[R+1]=n[R+1],b[R+2]=n[R+2],b[R+3]=1}Lt(b,a,2),zt(e,b,a);const y=ue(e),g=t.createTexture({label:"lightmap_gi_pos",size:{width:a,height:a},format:"rgba32float",usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST}),v=t.createTexture({label:"lightmap_gi_nrm",size:{width:a,height:a},format:"rgba32float",usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST}),T=t.createTexture({label:"lightmap_gi_prev",size:{width:a,height:a},format:"rgba32float",usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST}),M=t.createTexture({label:"lightmap_gi_out",size:{width:a,height:a},format:"rgba16float",usage:GPUTextureUsage.STORAGE_BINDING|GPUTextureUsage.COPY_SRC});t.queue.writeTexture({texture:g},xt(r.position),{bytesPerRow:a*16},{width:a,height:a}),t.queue.writeTexture({texture:v},xt(r.normal),{bytesPerRow:a*16},{width:a,height:a});const A=new Uint8Array(y.nodeBytes),x=t.createBuffer({label:"lightmap_gi_bvh_nodes",size:Math.max(32,A.byteLength),usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST});t.queue.writeBuffer(x,0,A);const L=xt(y.triFloats),E=t.createBuffer({label:"lightmap_gi_bvh_tris",size:Math.max(48,L.byteLength),usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST});t.queue.writeBuffer(E,0,L);const P=xt(y.triUv1Floats),O=t.createBuffer({label:"lightmap_gi_tri_uv1",size:Math.max(32,P.byteLength),usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST});t.queue.writeBuffer(O,0,P);const X=xt(y.triAlbedoFloats),w=t.createBuffer({label:"lightmap_gi_tri_albedo",size:Math.max(16,X.byteLength),usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST});t.queue.writeBuffer(w,0,X);const V=dn(e),q=t.createBuffer({label:"lightmap_gi_tri_fallback",size:Math.max(16,V.byteLength),usage:GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_DST}),$=new ArrayBuffer(48),z=new Uint32Array($,0,8),W=new Float32Array($,32,4);z[0]=a,z[2]=d,z[3]=y.nodeCount,z[4]=y.rootIndex,W[0]=u[0],W[1]=u[1],W[2]=u[2],W[3]=f;const F=t.createBuffer({label:"lightmap_gi_params",size:48,usage:GPUBufferUsage.UNIFORM|GPUBufferUsage.COPY_DST}),N=t.createShaderModule({label:"bake_gi_bounce.compute",code:mn}),Y=t.createComputePipeline({label:"lightmap_gi_bounce_pipeline",layout:"auto",compute:{module:N,entryPoint:"main"}}),Z=t.createBindGroup({layout:Y.getBindGroupLayout(0),entries:[{binding:0,resource:{buffer:F}},{binding:1,resource:g.createView()},{binding:2,resource:v.createView()},{binding:3,resource:T.createView()},{binding:4,resource:M.createView()},{binding:5,resource:{buffer:x}},{binding:6,resource:{buffer:E}},{binding:7,resource:{buffer:O}},{binding:8,resource:{buffer:w}},{binding:9,resource:{buffer:q}}]}),j=Math.ceil(a/8);try{for(let D=0;D<o;D++){if((B=i.signal)!=null&&B.aborted)throw new DOMException("Bake aborted","AbortError");z[1]=D,t.queue.writeBuffer(F,0,$),t.queue.writeTexture({texture:T},xt(b),{bytesPerRow:a*16},{width:a,height:a}),t.queue.writeBuffer(q,0,xt(dn(e)));const R=t.createCommandEncoder({label:`lightmap_gi_bounce_${D}`}),U=R.beginComputePass({label:`lightmap_gi_bounce_pass_${D}`});U.setPipeline(Y),U.setBindGroup(0,Z),U.dispatchWorkgroups(j,j,1),U.end();const S=await hn(t,R,M,a);c&&Ot(S,a,{gbuffer:r,stepSizes:m,wavelet5x5:!0,sigmaRange:14,planeSigma:.06});for(let k=0;k<s.length;k++){const G=s[k]*4;h[G]=(h[G]??0)+S[G],h[G+1]=(h[G+1]??0)+S[G+1],h[G+2]=(h[G+2]??0)+S[G+2],h[G+3]=1}D+1<o&&(b.set(S),Lt(b,a,2),zt(e,b,a)),(p=i.onBounceComplete)==null||p.call(i,D)}}finally{g.destroy(),v.destroy(),T.destroy(),M.destroy(),x.destroy(),E.destroy(),O.destroy(),w.destroy(),q.destroy(),F.destroy()}return h}function dt(t,r,e,n){t==null||t({phase:r,completed:e,total:n})}async function ra(t,r,e){var w,V,q,$,z,W;const n={texelsPerUnit:((w=e==null?void 0:e.settings)==null?void 0:w.texelsPerUnit)??40,bounces:((V=e==null?void 0:e.settings)==null?void 0:V.bounces)??2,samplesPerTexel:((q=e==null?void 0:e.settings)==null?void 0:q.samplesPerTexel)??32,atlasSize:(($=e==null?void 0:e.settings)==null?void 0:$.atlasSize)??2048};if(dt(e==null?void 0:e.onProgress,"prepare",0,1),(z=e==null?void 0:e.signal)!=null&&z.aborted)throw new DOMException("Bake aborted","AbortError");const s=De(t,r,{atlasSize:n.atlasSize,texelsPerUnit:n.texelsPerUnit});n.texelsPerUnit=s.texelsPerUnit,dt(e==null?void 0:e.onProgress,"prepare",1,1);const i=s.atlasSize,a=new Float32Array(i*i*4),l=(e==null?void 0:e.ambientIrradiance)??[.08,.08,.1],o=qe(t),h=(e==null?void 0:e.aoSamples)??6,d=(e==null?void 0:e.aoRadius)??.45;dt(e==null?void 0:e.onProgress,"irradiance",0,s.charts.length||1);const f=Math.max(6,Math.min(16,Math.ceil(n.samplesPerTexel/4))),u=Math.max(12,Math.min(36,Math.ceil(n.samplesPerTexel/2.5))),c=e==null?void 0:e.device,m=!!(((e==null?void 0:e.gpuDirect)??!0)&&c),b=!!(((e==null?void 0:e.gpuBounces)??!0)&&c),y=Math.max(1,1+n.bounces),g=await je(t,s.charts,a,i,o,l,{signal:e==null?void 0:e.signal,bounces:n.bounces,aoSamples:h,aoRadius:d,softShadowSamples:f,bounceSamples:u,skyRadiance:e==null?void 0:e.skyRadiance,denoise:(e==null?void 0:e.denoise)!==!1,gpuDirectRunner:m&&c?async(F,N)=>{const Y=await fn(c,F,o,{signal:e==null?void 0:e.signal,ambient:l,accel:N,softShadowSamples:f,aoSamples:h,aoRadius:d});return dt(e==null?void 0:e.onProgress,"irradiance",1,y),Y}:void 0,gpuBounceRunner:b&&c?async(F,N,Y,Z)=>gn(c,F,N,Y,Z,{signal:e==null?void 0:e.signal,bounces:n.bounces,bounceSamples:u,skyRadiance:e==null?void 0:e.skyRadiance,denoise:(e==null?void 0:e.denoise)!==!1,onBounceComplete:j=>dt(e==null?void 0:e.onProgress,"irradiance",2+j,y)}):void 0,onProgress:(F,N)=>dt(e==null?void 0:e.onProgress,"irradiance",F,N)}),v=g.gbuffer;if(dt(e==null?void 0:e.onProgress,"denoise",0,1),(e==null?void 0:e.denoise)!==!1&&Ot(a,i,{gbuffer:v,stepSizes:[1],sigmaSpatial:.9,sigmaRange:.7,planeSigma:.06}),e!=null&&e.denoiseHdr){if((W=e.signal)!=null&&W.aborted)throw new DOMException("Bake aborted","AbortError");try{const F=await e.denoiseHdr(a,i,e.signal,{albedo:v.albedo,normal:v.normal});if(F&&F.length===a.length)for(let N=0;N<a.length;N+=4)(a[N+3]??0)>0&&(a[N]=Math.max(0,F[N]??0),a[N+1]=Math.max(0,F[N+1]??0),a[N+2]=Math.max(0,F[N+2]??0))}catch{}}dt(e==null?void 0:e.onProgress,"denoise",1,1),dt(e==null?void 0:e.onProgress,"dilate",0,1),Lt(a,i,3),dt(e==null?void 0:e.onProgress,"dilate",1,1),dt(e==null?void 0:e.onProgress,"encode",0,1);const T=I.encodeRgb9e5Buffer(a,i,i),M=[T],A=[],x=[];if(e!=null&&e.device){const F=e.device.createTexture({label:"lightmap_atlas_0",size:{width:i,height:i},format:"rgb9e5ufloat",usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST}),N=Uint8Array.from(new Uint8Array(T.buffer,T.byteOffset,T.byteLength));e.device.queue.writeTexture({texture:F},N,{bytesPerRow:i*4},{width:i,height:i}),A.push(F);const Y="lightmap_atlas_0";x.push(Y),r.registerTexture(Y,F)}else x.push("lightmap_atlas_0");dt(e==null?void 0:e.onProgress,"encode",1,1),dt(e==null?void 0:e.onProgress,"probes",0,1);const L=an(t),E=sn(L,l,o,{accel:g.accel,atlasRgba:a,atlasSize:i,skyRadiance:e==null?void 0:e.skyRadiance}),P=on(t,r,{accel:g.accel,atlasRgba:a,atlasSize:i,ambient:l,skyRadiance:e==null?void 0:e.skyRadiance,device:c});dt(e==null?void 0:e.onProgress,"probes",1,1);const O={};for(const F of s.entities){const N=t.getComponent(F.entity,I.LightmapInstanceComponent);O[`e_${F.entity}`]={atlasIndex:(N==null?void 0:N.atlasIndex)??0,uvScaleOffset:(N==null?void 0:N.uvScaleOffset)??[1,1,0,0]}}const X={extVersion:ie,settings:n,atlasTextureIds:x,atlasPagesBase64:M.map(F=>oe(F)),bakedEntities:O,shVolumes:[E],bakedReflectionProbes:P};return I.invalidateStaticMergeRenderCaches(),dt(e==null?void 0:e.onProgress,"done",1,1),{payload:X,atlasPages:M,atlasSize:i,atlasTextures:A}}function sa(t,r,e){var l,o,h,d,f,u,c;(l=e==null?void 0:e.renderer)==null||l.setLightmapAtlas(null,0),(h=(o=e==null?void 0:e.renderer)==null?void 0:o.setLightmapShVolume)==null||h.call(o,null);const n=new Set(["lightmap_atlas_0"]);for(const m of((f=(d=e==null?void 0:e.state)==null?void 0:d.lastPayload)==null?void 0:f.atlasTextureIds)??[])m&&n.add(m);for(const m of((c=(u=e==null?void 0:e.state)==null?void 0:u.lastPayload)==null?void 0:c.bakedReflectionProbes)??[])for(const b of m.faceTextureIds)b&&n.add(b);if(r&&typeof r.unregisterTexture=="function")for(const m of n)r.unregisterTexture(m,{destroy:!0});e!=null&&e.state&&(e.state.lastPayload=null);const s=[];for(const[m]of t.query([I.LightmapInstanceComponent]).iter())s.push(m);for(const m of s)t.removeComponent(m,I.LightmapInstanceComponent);const i=new Set;let a=0;for(const[,[m]]of t.query([I.RenderableComponent]).iter()){const b=m==null?void 0:m.mesh;!b||typeof b=="string"||i.has(b)||(i.add(b),Rn(b)&&(a++,r==null||r.uploadMesh(b)))}return I.invalidateStaticMergeRenderCaches(),{clearedEntities:s.length,restoredMeshes:a}}function bn(t,r,e){var a;const n=((a=e.settings)==null?void 0:a.atlasSize)??0,s=e.atlasPagesBase64??[],i=[];if(!n||s.length===0)return{textures:i,atlasSize:n};for(let l=0;l<s.length;l++){const o=Re(s[l]),h=n*n;if(o.length!==h)throw new Error(`lightmap atlas page ${l}: expected ${h} u32, got ${o.length}`);const d=e.atlasTextureIds[l]??`lightmap_atlas_${l}`;typeof r.unregisterTexture=="function"&&r.unregisterTexture(d,{destroy:!0});const f=t.createTexture({label:d,size:{width:n,height:n},format:"rgb9e5ufloat",usage:GPUTextureUsage.TEXTURE_BINDING|GPUTextureUsage.COPY_DST}),u=Uint8Array.from(new Uint8Array(o.buffer,o.byteOffset,o.byteLength));t.queue.writeTexture({texture:f},u,{bytesPerRow:n*4},{width:n,height:n}),r.registerTexture(d,f),i.push(f)}return{textures:i,atlasSize:n}}function ia(){return{lastPayload:null}}function yn(t){if(!t||typeof t!="object")return!1;const r=t;return r.extVersion===ie&&typeof r.settings=="object"}function oa(t){return{id:Te,serialize:r=>{if(t.lastPayload){if(r.entityToId&&t.lastPayload.bakedEntities){const e={};for(const[n,s]of r.entityToId){const i=r.world.getComponent(n,I.LightmapInstanceComponent);i&&(e[s]={atlasIndex:i.atlasIndex,uvScaleOffset:[...i.uvScaleOffset]})}return{...t.lastPayload,bakedEntities:Object.keys(e).length>0?e:t.lastPayload.bakedEntities}}return t.lastPayload}},deserialize:async(r,e)=>{var l,o,h,d,f,u,c;if(!yn(r))return;t.lastPayload=r;const n=e.params.world;for(const[m,b]of Object.entries(r.bakedEntities)){let y=(l=e.entityMap)==null?void 0:l.get(m);if(y==null&&m.startsWith("e_")&&(y=Number(m.slice(2))),y==null||!Number.isFinite(y))continue;const g=n.getComponent(y,I.LightmapInstanceComponent);g?(g.atlasIndex=b.atlasIndex,g.uvScaleOffset=[...b.uvScaleOffset]):n.addComponent(y,new I.LightmapInstanceComponent(b.atlasIndex,[...b.uvScaleOffset]));const v=n.getComponent(y,I.RenderableComponent);v!=null&&v.mesh&&typeof v.mesh!="string"&&(v.mesh.bakedGeometryDirty=!0,v.mesh.lightmapBakeRevision=Math.max(1,v.mesh.lightmapBakeRevision??0))}const s=(h=(o=e.params.engine)==null?void 0:o.webGPUContext)==null?void 0:h.device,i=e.params.resourceManager,a=(d=e.params.engine)==null?void 0:d.renderer;if(s&&i&&a&&((f=r.atlasPagesBase64)!=null&&f.length)){const{textures:m}=bn(s,i,r);m[0]&&a.setLightmapAtlas(m[0],1)}if(a&&typeof a.setLightmapShVolume=="function"){const m=((u=r.shVolumes)==null?void 0:u[0])??null;a.setLightmapShVolume(m?nn(m,1):null)}I.invalidateStaticMergeRenderCaches(),I.setGameRenderGraphDiagnosticPreset("bakedInterior"),typeof((c=e.params.engine)==null?void 0:c.reloadGameRenderGraph)=="function"&&await e.params.engine.reloadGameRenderGraph()}}}function ca(t){return yn(t)?t:void 0}H.BAKE_GI_BOUNCE_COMPUTE_WGSL=mn,H.BAKE_LIGHTMAP_COMPUTE_WGSL=cn,H.BAKE_LIGHT_FLOATS=aa,H.LIGHTMAPPER_EXTENSION_ID=Te,H.LIGHTMAPPER_EXT_VERSION=ie,H.LIGHTMAP_ATLAS_PADDING=4,H.LIGHTMAP_ATLAS_SIZE=2048,H.LIGHTMAP_DEFAULT_BOUNCES=2,H.LIGHTMAP_DEFAULT_SAMPLES=32,H.LIGHTMAP_TILE_SIZE=64,H.SH_PROBE_VOLUME_MAX_PROBES=ge,H.SH_PROBE_VOLUME_UNIFORM_FLOATS=Ke,H.bakeCpuIrradianceAtlas=je,H.bakeLighting=ra,H.bakeRayClosestHit=Yt,H.bakeRayOccluded=qt,H.bakeReflectionProbesPt=on,H.bakeShVolumeCpu=sn,H.base64ToUint32=Re,H.buildBakeSceneAccel=Be,H.buildUvGBuffer=We,H.clearLightingBake=sa,H.collectBakeCandidateEntities=pe,H.collectBakeLights=qe,H.computeVertexDiffuseAlbedos=ke,H.createLightmapperRuntimeState=ia,H.createLightmapperSceneExtensionHandler=oa,H.decodeShVolumeCoefficients=en,H.denoiseAtlas=Ot,H.dilateAtlas=Lt,H.ensurePlanarUv1=Bn,H.ensureStaticTags=Ie,H.evalBakeIrradiance=de,H.evalShL2Basis=te,H.evalShL2ProbeIrradiance=tn,H.evalShVolumeIrradiance=ea,H.hashSeedToUnitPair=Nt,H.hashTexel2d=Gt,H.makeCanonicalTangent=me,H.packBakeLightsGpu=un,H.packBakeSceneAccelGpu=ue,H.packMeshesIntoAtlas=Ue,H.packShVolumeUniformF32=nn,H.parseLightmapperScenePayload=ca,H.placeAutoProbeVolume=an,H.prepareAtlas=De,H.restoreLightmapAtlasFromPayload=bn,H.runGpuBakeDirect=fn,H.runGpuBakeGiBounces=gn,H.sampleContactAo=we,H.sampleEmissiveCatalogDirect=Le,H.sampleIndirectBounce=Xt,H.sampleSrgbImageLinear=ce,H.softLightVisibility=Wt,H.uint32ToBase64=oe,H.updateBounceSrcFromAtlas=zt,H.writeChartBounceSrc=Vn,Object.defineProperty(H,Symbol.toStringTag,{value:"Module"})});
|