@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.
Files changed (54) hide show
  1. package/README.md +12 -3
  2. package/dist/atlasPostprocess.d.ts +37 -0
  3. package/dist/atlasPostprocess.d.ts.map +1 -0
  4. package/dist/bakeGiBounceComputeWgsl.d.ts +7 -0
  5. package/dist/bakeGiBounceComputeWgsl.d.ts.map +1 -0
  6. package/dist/bakeLighting.d.ts +18 -0
  7. package/dist/bakeLighting.d.ts.map +1 -0
  8. package/dist/bakeLightmapComputeWgsl.d.ts +7 -0
  9. package/dist/bakeLightmapComputeWgsl.d.ts.map +1 -0
  10. package/dist/bakeSceneAccel.d.ts +143 -0
  11. package/dist/bakeSceneAccel.d.ts.map +1 -0
  12. package/dist/base64.d.ts +11 -0
  13. package/dist/base64.d.ts.map +1 -0
  14. package/dist/buildUvGBuffer.d.ts +25 -0
  15. package/dist/buildUvGBuffer.d.ts.map +1 -0
  16. package/dist/classifyStatic.d.ts +10 -0
  17. package/dist/classifyStatic.d.ts.map +1 -0
  18. package/dist/clearLightingBake.d.ts +22 -0
  19. package/dist/clearLightingBake.d.ts.map +1 -0
  20. package/dist/collectBakeLights.d.ts +73 -0
  21. package/dist/collectBakeLights.d.ts.map +1 -0
  22. package/dist/constants.d.ts +20 -0
  23. package/dist/constants.d.ts.map +1 -0
  24. package/dist/cpuIrradianceBake.d.ts +65 -0
  25. package/dist/cpuIrradianceBake.d.ts.map +1 -0
  26. package/dist/index.d.ts +44 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/lightmapperSceneExtension.d.ts +37 -0
  29. package/dist/lightmapperSceneExtension.d.ts.map +1 -0
  30. package/dist/packAtlasUv1.d.ts +36 -0
  31. package/dist/packAtlasUv1.d.ts.map +1 -0
  32. package/dist/packBakeLightsGpu.d.ts +5 -0
  33. package/dist/packBakeLightsGpu.d.ts.map +1 -0
  34. package/dist/planarUv1.d.ts +53 -0
  35. package/dist/planarUv1.d.ts.map +1 -0
  36. package/dist/prepareAtlas.d.ts +9 -0
  37. package/dist/prepareAtlas.d.ts.map +1 -0
  38. package/dist/probeVolume.d.ts +68 -0
  39. package/dist/probeVolume.d.ts.map +1 -0
  40. package/dist/restoreLightmapAtlas.d.ts +12 -0
  41. package/dist/restoreLightmapAtlas.d.ts.map +1 -0
  42. package/dist/runGpuBakeDirect.d.ts +28 -0
  43. package/dist/runGpuBakeDirect.d.ts.map +1 -0
  44. package/dist/runGpuBakeGiBounces.d.ts +17 -0
  45. package/dist/runGpuBakeGiBounces.d.ts.map +1 -0
  46. package/dist/softVisibility.d.ts +16 -0
  47. package/dist/softVisibility.d.ts.map +1 -0
  48. package/dist/types.d.ts +109 -0
  49. package/dist/types.d.ts.map +1 -0
  50. package/dist/w3dts-lightmapper.es.js +3465 -0
  51. package/dist/w3dts-lightmapper.umd.js +855 -0
  52. package/package.json +49 -3
  53. package/shaders/bake_gi_bounce.compute.wgsl +255 -0
  54. package/shaders/bake_lightmap.compute.wgsl +599 -0
@@ -0,0 +1,3465 @@
1
+ import { RenderableComponent as kt, EditorOnlyComponent as _e, StaticMergeChunkComponent as ae, SkinnedMeshComponent as bn, ComputeAnimationComponent as yn, WaveAnimationComponent as vn, GltfAnimationPlayerComponent as Mn, StaticComponent as ne, usesTransparentRenderQueue as pn, STANDARD_MESH_ATTR as tt, STANDARD_MESH_VERTEX_FLOATS as _t, LightmapInstanceComponent as Tt, invalidateStaticMergeRenderCaches as se, Mesh as _n, TransformComponent as St, DirectionalLightComponent as wn, isEntityEffectivelyVisible as Dt, PointLightComponent as An, SpotLightComponent as Sn, AreaLightComponent as Un, evalKhrPunctualAttenuation as De, evalKhrSpotAttenuation as Pn, ReflectionProbeComponent as In, encodeRgb9e5Buffer as Xe, setGameRenderGraphDiagnosticPreset as Tn } from "@naanouff/w3dts-core";
2
+ import { mat4 as ie, vec3 as ot, quat as kn, mat3 as Re } from "gl-matrix";
3
+ import { buildEmissiveCatalog as Bn, buildPathTracerBvhFromTriangles as Cn, materialUsesMeshMaps as On, PATH_TRACER_FLOATS_PER_TRIANGLE as Le, isPathTracerTriangleLeaf as Dn, sampleRectAreaLight as Rn } from "@naanouff/w3dts-path-tracer";
4
+ const Ln = "@naanouff/w3dts-lightmapper", $e = "1.0", we = 2048, je = 4, Nn = 32, Ke = 2, zn = 64;
5
+ function En(t, a) {
6
+ for (const e of t.getAllComponents(a)) {
7
+ const s = e.bodyType;
8
+ if (typeof s == "string" && s !== "fixed" && s !== "static")
9
+ return !0;
10
+ }
11
+ return !1;
12
+ }
13
+ function Gn(t) {
14
+ var e, n, s, i, r, l;
15
+ const a = [];
16
+ for (const [o, [h]] of t.query([kt]).iter()) {
17
+ if (!(h != null && h.visible) || t.hasComponent(o, _e) || t.hasComponent(o, ae) || t.hasComponent(o, bn) || t.hasComponent(o, yn) || t.hasComponent(o, vn) || t.hasComponent(o, Mn) || !t.hasComponent(o, ne) && En(t, o))
18
+ continue;
19
+ const m = h.mesh, f = h.material;
20
+ !m || typeof m == "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") || pn(f) || !((r = m.vertices) != null && r.length) || !((l = m.indices) != null && l.length) || a.push(o);
21
+ }
22
+ return a;
23
+ }
24
+ function Fn(t, a) {
25
+ for (const e of a)
26
+ t.hasComponent(e, ne) || t.addComponent(e, new ne());
27
+ }
28
+ const Nt = /* @__PURE__ */ new WeakMap();
29
+ function qn(t) {
30
+ const a = _t, e = Math.floor(t.length / a), n = new Float32Array(e * 2);
31
+ for (let s = 0; s < e; s++) {
32
+ const i = s * a + tt.uv1;
33
+ n[s * 2] = t[i] ?? 0, n[s * 2 + 1] = t[i + 1] ?? 0;
34
+ }
35
+ return n;
36
+ }
37
+ function Vn(t, a) {
38
+ const e = Nt.get(t);
39
+ e && Nt.set(a, {
40
+ vertices: new Float32Array(e.vertices),
41
+ indices: e.indices instanceof Uint16Array ? new Uint16Array(e.indices) : new Uint32Array(e.indices),
42
+ hasAuthoredUv1: e.hasAuthoredUv1,
43
+ morphPositionDeltaData: e.morphPositionDeltaData ? new Float32Array(e.morphPositionDeltaData) : void 0
44
+ });
45
+ }
46
+ function Hn(t) {
47
+ const a = Nt.get(t);
48
+ if (!a && t.lightmapOriginalUv1 == null) {
49
+ const l = new Float32Array(t.vertices), o = t.indices instanceof Uint16Array ? new Uint16Array(t.indices) : new Uint32Array(t.indices);
50
+ Nt.set(t, {
51
+ vertices: l,
52
+ indices: o,
53
+ hasAuthoredUv1: t.hasAuthoredUv1,
54
+ morphPositionDeltaData: t.morphPositionDeltaData ? new Float32Array(t.morphPositionDeltaData) : void 0
55
+ }), t.lightmapOriginalUv1 = qn(l), t.lightmapOriginalHasAuthoredUv1 = t.hasAuthoredUv1;
56
+ return;
57
+ }
58
+ if (a) {
59
+ const l = new Float32Array(a.vertices), o = a.indices instanceof Uint16Array ? new Uint16Array(a.indices) : new Uint32Array(a.indices);
60
+ t.updateData(l, o, t.name), a.morphPositionDeltaData && (t.morphPositionDeltaData = new Float32Array(a.morphPositionDeltaData)), t.hasAuthoredUv1 = a.hasAuthoredUv1;
61
+ return;
62
+ }
63
+ const e = _t, n = t.vertices, s = Math.floor(n.length / e), i = t.lightmapOriginalUv1, r = Math.min(s, Math.floor(i.length / 2));
64
+ for (let l = 0; l < r; l++) {
65
+ const o = l * e + tt.uv1;
66
+ n[o] = i[l * 2] ?? 0, n[o + 1] = i[l * 2 + 1] ?? 0;
67
+ }
68
+ t.hasAuthoredUv1 = t.lightmapOriginalHasAuthoredUv1 ?? !1;
69
+ }
70
+ function Yn(t) {
71
+ const a = Nt.get(t);
72
+ if (a) {
73
+ const l = new Float32Array(a.vertices), o = a.indices instanceof Uint16Array ? new Uint16Array(a.indices) : new Uint32Array(a.indices);
74
+ return t.updateData(l, o, t.name), a.morphPositionDeltaData && (t.morphPositionDeltaData = new Float32Array(a.morphPositionDeltaData)), t.hasAuthoredUv1 = a.hasAuthoredUv1, Nt.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;
75
+ }
76
+ if (t.lightmapOriginalUv1 == null) return !1;
77
+ const e = _t, n = t.vertices, s = Math.floor(n.length / e), i = t.lightmapOriginalUv1, r = Math.min(s, Math.floor(i.length / 2));
78
+ for (let l = 0; l < r; l++) {
79
+ const o = l * e + tt.uv1;
80
+ n[o] = i[l * 2] ?? 0, n[o + 1] = i[l * 2 + 1] ?? 0;
81
+ }
82
+ 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;
83
+ }
84
+ function Wn(t, a, e) {
85
+ const n = Math.abs(t), s = Math.abs(a), i = Math.abs(e);
86
+ return n >= s && n >= i ? t >= 0 ? 0 : 1 : s >= n && s >= i ? a >= 0 ? 2 : 3 : e >= 0 ? 4 : 5;
87
+ }
88
+ function Xn(t, a, e, n) {
89
+ switch (t) {
90
+ case 0:
91
+ return [-n, e];
92
+ case 1:
93
+ return [n, e];
94
+ case 2:
95
+ return [a, -n];
96
+ case 3:
97
+ return [a, n];
98
+ case 4:
99
+ return [a, e];
100
+ default:
101
+ return [-a, e];
102
+ }
103
+ }
104
+ function Ze(t, a = [1, 1, 1]) {
105
+ Hn(t);
106
+ const e = Math.max(1e-5, Math.abs(a[0] ?? 1)), n = Math.max(1e-5, Math.abs(a[1] ?? 1)), s = Math.max(1e-5, Math.abs(a[2] ?? 1)), i = _t, r = t.vertices, l = t.indices, o = Math.floor(r.length / i), h = Math.floor(l.length / 3);
107
+ if (o < 1 || h < 1)
108
+ return { generated: !1, islands: [] };
109
+ const m = new Float32Array(h);
110
+ let f = 0;
111
+ for (let _ = 0; _ < h; _++) {
112
+ const C = l[_ * 3] * i, S = l[_ * 3 + 1] * i, I = l[_ * 3 + 2] * i, T = r[C] * e, P = r[C + 1] * n, w = r[C + 2] * s, B = r[S] * e, N = r[S + 1] * n, E = r[S + 2] * s, Z = r[I] * e, J = r[I + 1] * n, et = r[I + 2] * s, rt = B - T, $ = N - P, Q = E - w, K = Z - T, nt = J - P, ct = et - w, it = $ * ct - Q * nt, st = Q * K - rt * ct, at = rt * nt - $ * K, ut = 0.5 * Math.hypot(it, st, at);
113
+ m[_] = ut, f += ut;
114
+ }
115
+ if (t.hasAuthoredUv1) {
116
+ const _ = new Uint32Array(o), C = new Float32Array(o * 2);
117
+ let S = 1 / 0, I = 1 / 0, T = -1 / 0, P = -1 / 0;
118
+ for (let w = 0; w < o; w++) {
119
+ _[w] = w;
120
+ const B = w * i + tt.uv1, N = r[B], E = r[B + 1];
121
+ C[w * 2] = N, C[w * 2 + 1] = E, N < S && (S = N), E < I && (I = E), N > T && (T = N), E > P && (P = E);
122
+ }
123
+ return Number.isFinite(S) || (S = 0, I = 0, T = 1, P = 1), T - S < 1e-6 && (T = S + 1e-6), P - I < 1e-6 && (P = I + 1e-6), {
124
+ generated: !1,
125
+ islands: [
126
+ {
127
+ vertexIndices: _,
128
+ metricUvs: C,
129
+ u0: S,
130
+ v0: I,
131
+ u1: T,
132
+ v1: P,
133
+ worldArea: Math.max(f, 1e-6)
134
+ }
135
+ ]
136
+ };
137
+ }
138
+ const u = new Int8Array(h);
139
+ let c = 1 / 0, d = 1 / 0, x = 1 / 0, b = -1 / 0, g = -1 / 0, v = -1 / 0;
140
+ for (let _ = 0; _ < o; _++) {
141
+ const C = _ * i, S = r[C] * e, I = r[C + 1] * n, T = r[C + 2] * s;
142
+ S < c && (c = S), I < d && (d = I), T < x && (x = T), S > b && (b = S), I > g && (g = I), T > v && (v = T);
143
+ }
144
+ const M = 1e5 / Math.max(1e-5, b - c, g - d, v - x);
145
+ for (let _ = 0; _ < h; _++) {
146
+ const C = l[_ * 3] * i, S = l[_ * 3 + 1] * i, I = l[_ * 3 + 2] * i, T = r[C] * e, P = r[C + 1] * n, w = r[C + 2] * s, B = r[S] * e, N = r[S + 1] * n, E = r[S + 2] * s, Z = r[I] * e, J = r[I + 1] * n, et = r[I + 2] * s;
147
+ let rt = (N - P) * (et - w) - (E - w) * (J - P), $ = (E - w) * (Z - T) - (B - T) * (et - w), Q = (B - T) * (J - P) - (N - P) * (Z - T);
148
+ Math.hypot(rt, $, Q) < 1e-11 && (rt = (r[C + tt.normal] ?? 0) / e, $ = (r[C + tt.normal + 1] ?? 1) / n, Q = (r[C + tt.normal + 2] ?? 0) / s), u[_] = Wn(rt, $, Q);
149
+ }
150
+ const p = new Int32Array(o), y = /* @__PURE__ */ new Map();
151
+ let O = 0;
152
+ for (let _ = 0; _ < o; _++) {
153
+ const C = _ * i, S = Math.round((r[C] * e - c) * M), I = Math.round((r[C + 1] * n - d) * M), T = Math.round((r[C + 2] * s - x) * M), P = `${S},${I},${T}`;
154
+ let w = y.get(P);
155
+ w === void 0 && (w = O++, y.set(P, w)), p[_] = w;
156
+ }
157
+ const k = /* @__PURE__ */ new Map();
158
+ for (let _ = 0; _ < h; _++) {
159
+ const C = u[_], S = p[l[_ * 3]], I = p[l[_ * 3 + 1]], T = p[l[_ * 3 + 2]], P = [
160
+ S < I ? [S, I] : [I, S],
161
+ I < T ? [I, T] : [T, I],
162
+ T < S ? [T, S] : [S, T]
163
+ ];
164
+ for (const [w, B] of P) {
165
+ if (w === B) continue;
166
+ const N = `${C}:${w}:${B}`;
167
+ let E = k.get(N);
168
+ E || (E = [], k.set(N, E)), E.push(_);
169
+ }
170
+ }
171
+ const U = new Int32Array(h).fill(-1), D = [], H = [], R = [];
172
+ for (let _ = 0; _ < h; _++) {
173
+ if (U[_] !== -1) continue;
174
+ const C = D.length, S = u[_];
175
+ D.push(S), U[_] = C, R.length = 0, R.push(_);
176
+ let I = 0, T = 0;
177
+ for (; T < R.length; ) {
178
+ const P = R[T++];
179
+ I += m[P];
180
+ const w = p[l[P * 3]], B = p[l[P * 3 + 1]], N = p[l[P * 3 + 2]], E = [
181
+ w < B ? [w, B] : [B, w],
182
+ B < N ? [B, N] : [N, B],
183
+ N < w ? [N, w] : [w, N]
184
+ ];
185
+ for (const [Z, J] of E) {
186
+ if (Z === J) continue;
187
+ const et = k.get(`${S}:${Z}:${J}`);
188
+ if (et)
189
+ for (const rt of et)
190
+ U[rt] === -1 && (U[rt] = C, R.push(rt));
191
+ }
192
+ }
193
+ H.push(Math.max(I, 1e-6));
194
+ }
195
+ const F = D.length, q = new Int32Array(o).fill(-1), W = /* @__PURE__ */ new Map(), z = new Uint32Array(h * 3), Y = [], G = new Array(o).fill(0);
196
+ for (let _ = 0; _ < h; _++) {
197
+ const C = U[_];
198
+ for (let S = 0; S < 3; S++) {
199
+ const I = l[_ * 3 + S], T = q[I];
200
+ if (T === -1)
201
+ q[I] = C, G[I] = C, z[_ * 3 + S] = I;
202
+ else if (T === C)
203
+ z[_ * 3 + S] = I;
204
+ else {
205
+ const P = `${I}:${C}`;
206
+ let w = W.get(P);
207
+ w === void 0 && (w = o + Y.length, Y.push(I), G.push(C), W.set(P, w)), z[_ * 3 + S] = w;
208
+ }
209
+ }
210
+ }
211
+ const L = o + Y.length;
212
+ if (Y.length > 0) {
213
+ const _ = new Float32Array(L * i);
214
+ _.set(r);
215
+ for (let S = 0; S < Y.length; S++) {
216
+ const I = Y[S] * i, T = (o + S) * i;
217
+ _.set(r.subarray(I, I + i), T);
218
+ }
219
+ const C = L <= 65535 ? new Uint16Array(z) : z;
220
+ if (t.updateData(_, C, t.name), t.morphPositionDeltaData && t.morphTargetCount > 0) {
221
+ const S = t.morphTargetCount * 4, I = new Float32Array(L * S);
222
+ I.set(t.morphPositionDeltaData);
223
+ for (let T = 0; T < Y.length; T++) {
224
+ const P = Y[T] * S, w = (o + T) * S;
225
+ I.set(t.morphPositionDeltaData.subarray(P, P + S), w);
226
+ }
227
+ t.morphPositionDeltaData = I;
228
+ }
229
+ }
230
+ const V = Array.from({ length: F }, () => []);
231
+ for (let _ = 0; _ < L; _++) {
232
+ const C = q[_] === -1 && _ < o ? 0 : G[_];
233
+ C >= 0 && C < F && V[C].push(_);
234
+ }
235
+ const j = t.vertices, X = [];
236
+ for (let _ = 0; _ < F; _++) {
237
+ const C = V[_];
238
+ if (C.length === 0) continue;
239
+ const S = D[_], I = new Uint32Array(C), T = new Float32Array(C.length * 2);
240
+ let P = 1 / 0, w = 1 / 0, B = -1 / 0, N = -1 / 0;
241
+ for (let E = 0; E < C.length; E++) {
242
+ const Z = C[E] * i, J = j[Z] * e, et = j[Z + 1] * n, rt = j[Z + 2] * s, [$, Q] = Xn(S, J, et, rt);
243
+ T[E * 2] = $, T[E * 2 + 1] = Q, $ < P && (P = $), Q < w && (w = Q), $ > B && (B = $), Q > N && (N = Q);
244
+ }
245
+ B - P < 1e-6 && (B = P + 1e-6), N - w < 1e-6 && (N = w + 1e-6), X.push({
246
+ vertexIndices: I,
247
+ metricUvs: T,
248
+ u0: P,
249
+ v0: w,
250
+ u1: B,
251
+ v1: N,
252
+ worldArea: H[_]
253
+ });
254
+ }
255
+ return t.hasAuthoredUv1 = !0, t.bakedGeometryDirty = !0, t.vertexBuffer = null, t.indexBuffer = null, { generated: !0, islands: X };
256
+ }
257
+ function Fr(t, a) {
258
+ const { generated: e, islands: n } = Ze(t, a == null ? void 0 : a.worldScale);
259
+ if (!e || n.length === 0) return !1;
260
+ const s = _t, i = t.vertices;
261
+ if (n.length === 1) {
262
+ const g = n[0], v = Math.max(1e-6, g.u1 - g.u0), A = Math.max(1e-6, g.v1 - g.v0);
263
+ for (let M = 0; M < g.vertexIndices.length; M++) {
264
+ const p = g.vertexIndices[M] * s + tt.uv1;
265
+ i[p] = (g.metricUvs[M * 2] - g.u0) / v, i[p + 1] = (g.metricUvs[M * 2 + 1] - g.v0) / A;
266
+ }
267
+ return !0;
268
+ }
269
+ const r = 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 = r.reduce((g, v) => g + v.w * v.h, 0), o = Math.max(
270
+ Math.sqrt(l * 1.35),
271
+ ...r.map((g) => Math.max(g.w, g.h) * 1.08),
272
+ 1e-5
273
+ ), h = o * 0.02;
274
+ let m = h, f = h, u = 0, c = h, d = h;
275
+ const x = [];
276
+ for (const g of r)
277
+ m + g.w + h > o && m > h && (m = h, f += u + h, u = 0), x.push({ x: m, y: f }), m += g.w + h, u = Math.max(u, g.h), c = Math.max(c, m), d = Math.max(d, f + u + h);
278
+ const b = Math.max(c, d, 1e-5);
279
+ for (let g = 0; g < r.length; g++) {
280
+ const { isl: v } = r[g], { x: A, y: M } = x[g];
281
+ for (let p = 0; p < v.vertexIndices.length; p++) {
282
+ const y = v.vertexIndices[p] * s + tt.uv1;
283
+ i[y] = (A + (v.metricUvs[p * 2] - v.u0)) / b, i[y + 1] = (M + (v.metricUvs[p * 2 + 1] - v.v0)) / b;
284
+ }
285
+ }
286
+ return !0;
287
+ }
288
+ function $n(t) {
289
+ const a = _t, e = t.vertices, n = t.indices;
290
+ let s = 0;
291
+ for (let i = 0; i + 2 < n.length; i += 3) {
292
+ const r = n[i] * a + tt.uv1, l = n[i + 1] * a + tt.uv1, o = n[i + 2] * a + tt.uv1, h = e[r], m = e[r + 1], f = e[l], u = e[l + 1], c = e[o], d = e[o + 1];
293
+ s += 0.5 * Math.abs((f - h) * (d - m) - (c - h) * (u - m));
294
+ }
295
+ return Math.max(s, 1e-6);
296
+ }
297
+ function jn(t, a) {
298
+ const e = (a == null ? void 0 : a.atlasSize) ?? we, n = Math.max(4, (a == null ? void 0 : a.padding) ?? je);
299
+ let s = (a == null ? void 0 : a.texelsPerUnit) ?? 40;
300
+ const i = [], r = t.map((u, c) => {
301
+ const d = u.worldScale ?? [1, 1, 1], { generated: x, islands: b } = Ze(u.mesh, d);
302
+ let g = 0;
303
+ for (let v = 0; v < b.length; v++) {
304
+ const A = b[v];
305
+ g += A.worldArea;
306
+ let M, p;
307
+ if (x)
308
+ M = Math.max(1e-3, A.u1 - A.u0), p = Math.max(1e-3, A.v1 - A.v0);
309
+ else {
310
+ const y = Math.max(1e-6, A.u1 - A.u0), O = Math.max(1e-6, A.v1 - A.v0), k = y * O, U = $n(u.mesh) / k, D = Math.min(1, Math.max(0.15, U)), H = A.worldArea / D, R = Math.min(20, Math.max(0.05, y / O));
311
+ M = Math.max(1e-3, Math.sqrt(H * R)), p = Math.max(1e-3, H / M);
312
+ }
313
+ i.push({
314
+ entityOrder: c,
315
+ islandIdx: v,
316
+ isl: A,
317
+ worldW: M,
318
+ worldH: p,
319
+ px: 0,
320
+ py: 0,
321
+ pw: 0,
322
+ ph: 0
323
+ });
324
+ }
325
+ return {
326
+ mesh: u.mesh,
327
+ entity: u.entity,
328
+ surfaceArea: Math.max(g, 1e-6),
329
+ u0: 0,
330
+ v0: 0,
331
+ u1: 1,
332
+ v1: 1,
333
+ px: 0,
334
+ py: 0,
335
+ pw: 0,
336
+ ph: 0,
337
+ uvScaleOffset: [1, 1, 0, 0]
338
+ };
339
+ });
340
+ if (i.length === 0)
341
+ return { charts: r, texelsPerUnit: s, atlasSize: e };
342
+ const l = () => {
343
+ for (const b of i)
344
+ if (b.pw = Math.max(3, Math.ceil(b.worldW * s)), b.ph = Math.max(3, Math.ceil(b.worldH * s)), b.pw > e - 2 * n || b.ph > e - 2 * n)
345
+ return !1;
346
+ const u = [...i].sort(
347
+ (b, g) => g.ph - b.ph || g.pw - b.pw || b.entityOrder - g.entityOrder || b.islandIdx - g.islandIdx
348
+ );
349
+ let c = n, d = n, x = 0;
350
+ for (const b of u) {
351
+ if (c + b.pw > e - n && (c = n, d += x + n, x = 0), d + b.ph > e - n)
352
+ return !1;
353
+ b.px = c, b.py = d, c += b.pw + n, x = Math.max(x, b.ph);
354
+ }
355
+ return !0;
356
+ };
357
+ for (; !l() && s > 2; )
358
+ s *= 0.85;
359
+ if (!l()) {
360
+ const u = Math.max(1, i.length), c = Math.ceil(Math.sqrt(u)), d = Math.max(3, Math.floor((e - n * (c + 1)) / c));
361
+ i.forEach((x, b) => {
362
+ const g = b % c, v = Math.floor(b / c);
363
+ x.px = n + g * (d + n), x.py = n + v * (d + n), x.pw = d, x.ph = d;
364
+ });
365
+ }
366
+ const o = _t, h = 1 / e;
367
+ for (const u of r)
368
+ u.px = e, u.py = e, u.pw = 0, u.ph = 0, u.u0 = 1, u.v0 = 1, u.u1 = 0, u.v1 = 0;
369
+ const m = new Int32Array(r.length), f = new Int32Array(r.length);
370
+ for (const u of i) {
371
+ const c = r[u.entityOrder], d = c.mesh.vertices, x = u.isl, b = Math.max(1e-6, x.u1 - x.u0), g = Math.max(1e-6, x.v1 - x.v0), v = Math.max(1, u.pw - 1), A = Math.max(1, u.ph - 1);
372
+ for (let M = 0; M < x.vertexIndices.length; M++) {
373
+ const y = x.vertexIndices[M] * o + tt.uv1, O = (x.metricUvs[M * 2] - x.u0) / b, k = (x.metricUvs[M * 2 + 1] - x.v0) / g, U = (u.px + 0.5 + O * v) * h, D = (u.py + 0.5 + k * A) * h;
374
+ d[y] = U, d[y + 1] = D, U < c.u0 && (c.u0 = U), D < c.v0 && (c.v0 = D), U > c.u1 && (c.u1 = U), D > c.v1 && (c.v1 = D);
375
+ }
376
+ u.px < c.px && (c.px = u.px), u.py < c.py && (c.py = u.py), u.px + u.pw > m[u.entityOrder] && (m[u.entityOrder] = u.px + u.pw), u.py + u.ph > f[u.entityOrder] && (f[u.entityOrder] = u.py + u.ph);
377
+ }
378
+ for (let u = 0; u < r.length; u++) {
379
+ const c = r[u];
380
+ m[u] > c.px ? (c.pw = m[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;
381
+ }
382
+ return { charts: r, texelsPerUnit: s, atlasSize: e };
383
+ }
384
+ function Kn(t, a) {
385
+ const e = t.indices instanceof Uint16Array ? new Uint16Array(t.indices) : new Uint32Array(t.indices), n = new _n(
386
+ new Float32Array(t.vertices),
387
+ e,
388
+ `${t.name}_lm_${a}`
389
+ );
390
+ return n.hasAuthoredUv1 = t.hasAuthoredUv1, n.lightmapOriginalUv1 = t.lightmapOriginalUv1 ? new Float32Array(t.lightmapOriginalUv1) : void 0, n.lightmapOriginalHasAuthoredUv1 = t.lightmapOriginalHasAuthoredUv1, Vn(t, n), n.primitiveSource = t.primitiveSource ? {
391
+ type: t.primitiveSource.type,
392
+ params: t.primitiveSource.params ? { ...t.primitiveSource.params } : void 0
393
+ } : 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;
394
+ }
395
+ function Zn(t, a) {
396
+ const e = t.getComponent(a, St);
397
+ if (!e) return [1, 1, 1];
398
+ 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), r = 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));
399
+ return Math.abs(s - 1) < 1e-5 && Math.abs(i - 1) < 1e-5 && Math.abs(r - 1) < 1e-5 && (Math.abs(l - 1) > 1e-4 || Math.abs(o - 1) > 1e-4 || Math.abs(h - 1) > 1e-4) ? [l, o, h] : [
400
+ Number.isFinite(s) && s > 1e-5 ? s : l,
401
+ Number.isFinite(i) && i > 1e-5 ? i : o,
402
+ Number.isFinite(r) && r > 1e-5 ? r : h
403
+ ];
404
+ }
405
+ function Qn(t, a, e) {
406
+ const n = Gn(t);
407
+ Fn(t, n);
408
+ const s = /* @__PURE__ */ new Map();
409
+ for (const [h, [m]] of t.query([kt]).iter()) {
410
+ if (t.hasComponent(h, ae)) continue;
411
+ const f = m == null ? void 0 : m.mesh;
412
+ !f || typeof f == "string" || s.set(f, (s.get(f) ?? 0) + 1);
413
+ }
414
+ const i = /* @__PURE__ */ new Set(), r = [];
415
+ for (const h of n) {
416
+ const m = t.getComponent(h, kt), f = m == null ? void 0 : m.mesh;
417
+ if (!m || !f || typeof f == "string") continue;
418
+ let u = f;
419
+ ((s.get(f) ?? 0) > 1 || i.has(f)) && (u = Kn(f, h), m.mesh = u), i.add(u), r.push({
420
+ entity: h,
421
+ mesh: u,
422
+ worldScale: Zn(t, h)
423
+ });
424
+ }
425
+ const l = jn(r, {
426
+ atlasSize: (e == null ? void 0 : e.atlasSize) ?? we,
427
+ padding: (e == null ? void 0 : e.padding) ?? je,
428
+ texelsPerUnit: e == null ? void 0 : e.texelsPerUnit
429
+ });
430
+ for (const h of l.charts) {
431
+ const m = t.getComponent(h.entity, Tt);
432
+ m ? (m.atlasIndex = 0, m.uvScaleOffset = [...h.uvScaleOffset]) : t.addComponent(
433
+ h.entity,
434
+ new Tt(0, [...h.uvScaleOffset])
435
+ ), a && a.uploadMesh(h.mesh);
436
+ }
437
+ se();
438
+ const o = l.charts.map((h) => ({
439
+ entity: h.entity,
440
+ atlasIndex: 0,
441
+ uvScaleOffset: h.uvScaleOffset,
442
+ surfaceArea: h.surfaceArea,
443
+ mesh: h.mesh,
444
+ px: h.px,
445
+ py: h.py,
446
+ pw: h.pw,
447
+ ph: h.ph
448
+ }));
449
+ return {
450
+ atlasSize: l.atlasSize,
451
+ texelsPerUnit: l.texelsPerUnit,
452
+ entities: o.map((h) => ({
453
+ entity: h.entity,
454
+ atlasIndex: h.atlasIndex,
455
+ uvScaleOffset: h.uvScaleOffset,
456
+ surfaceArea: h.surfaceArea
457
+ })),
458
+ charts: o
459
+ };
460
+ }
461
+ function Jn(t) {
462
+ let a = "";
463
+ for (let e = 0; e < t.length; e++)
464
+ a += String.fromCharCode(t[e]);
465
+ return btoa(a);
466
+ }
467
+ function tr(t) {
468
+ const a = atob(t), e = new Uint8Array(a.length);
469
+ for (let n = 0; n < a.length; n++)
470
+ e[n] = a.charCodeAt(n);
471
+ return e;
472
+ }
473
+ function Qe(t) {
474
+ return Jn(new Uint8Array(t.buffer, t.byteOffset, t.byteLength));
475
+ }
476
+ function er(t) {
477
+ const a = tr(t), e = new Uint8Array(a.byteLength);
478
+ return e.set(a), new Uint32Array(e.buffer);
479
+ }
480
+ function Ot(t) {
481
+ const a = Math.min(1, Math.max(0, t));
482
+ return a <= 0.04045 ? a / 12.92 : Math.pow((a + 0.055) / 1.055, 2.4);
483
+ }
484
+ const nr = 0.6180339887498949, Lt = 0.92;
485
+ function oe(t) {
486
+ let a = t >>> 0;
487
+ return a ^= a >>> 16, a = Math.imul(a, 2146121005) >>> 0, a ^= a >>> 15, a = Math.imul(a, 2221713035) >>> 0, a ^= a >>> 16, [(a & 65535) / 65536, (a >>> 16 & 65535) / 65536];
488
+ }
489
+ function Xt(t, a, e = 0) {
490
+ let n = (Math.imul(t | 0, 668265261) ^ Math.imul(a | 0, 374761393) ^ Math.imul(e | 0, 2654435769)) >>> 0;
491
+ return n ^= n >>> 15, n = Math.imul(n, 739982445) >>> 0, n ^= n >>> 12, n >>> 0;
492
+ }
493
+ function ee(t, a, e) {
494
+ if (e || a + 19 >= t.length)
495
+ return [1, 1, 1];
496
+ const n = t[a + tt.color] ?? 0, s = t[a + tt.color + 1] ?? 0, i = t[a + tt.color + 2] ?? 0, r = t[a + tt.color + 3] ?? 0;
497
+ return n === 0 && s === 0 && i === 0 && r === 0 ? [1, 1, 1] : [
498
+ Number.isFinite(n) ? Math.max(0, n) : 1,
499
+ Number.isFinite(s) ? Math.max(0, s) : 1,
500
+ Number.isFinite(i) ? Math.max(0, i) : 1
501
+ ];
502
+ }
503
+ function rr(t, a) {
504
+ var s, i, r, l, o;
505
+ if (!t || typeof t == "string") return null;
506
+ if (a) {
507
+ const h = a(t);
508
+ if (h && h.width > 0 && h.height > 0 && h.data.length >= 4)
509
+ return h;
510
+ }
511
+ const e = (i = (s = t.properties) == null ? void 0 : s.get) == null ? void 0 : i.call(s, "baseColorPixels");
512
+ if (e && e.width > 0 && e.height > 0 && ((r = e.data) == null ? void 0 : r.length) >= 4)
513
+ return e;
514
+ const n = ((o = (l = t.properties) == null ? void 0 : l.get) == null ? void 0 : o.call(l, "diffuseTexture")) ?? t.diffuseTexture;
515
+ if (n && typeof n == "object") {
516
+ if (typeof n.width == "number" && typeof n.height == "number" && n.data && n.data.length >= n.width * n.height * 4)
517
+ return n;
518
+ if (n.imageData && n.imageData.width > 0 && n.imageData.height > 0)
519
+ return n.imageData;
520
+ }
521
+ return null;
522
+ }
523
+ function Je(t, a, e) {
524
+ const n = Math.max(1, t.width | 0), s = Math.max(1, t.height | 0), i = (a % 1 + 1) % 1, r = (e % 1 + 1) % 1, l = i * n - 0.5, o = r * s - 0.5, h = (Math.floor(l) % n + n) % n, m = (Math.floor(o) % s + s) % s, f = (h + 1) % n, u = (m + 1) % s, c = l - Math.floor(l), d = o - Math.floor(o), x = (D, H) => {
525
+ const R = (H * n + D) * 4, F = (t.data[R + 3] ?? 255) > 1.0001 || (t.data[R] ?? 0) > 1.0001 ? 1 / 255 : 1, q = Math.min(1, Math.max(0, (t.data[R] ?? 255) * F)), W = Math.min(1, Math.max(0, (t.data[R + 1] ?? 255) * F)), z = Math.min(1, Math.max(0, (t.data[R + 2] ?? 255) * F));
526
+ return [Ot(q), Ot(W), Ot(z)];
527
+ }, b = t.data instanceof Uint8Array || t.data instanceof Uint8ClampedArray, g = (D, H) => {
528
+ if (!b) return x(D, H);
529
+ const R = (H * n + D) * 4;
530
+ return [
531
+ Ot((t.data[R] ?? 255) / 255),
532
+ Ot((t.data[R + 1] ?? 255) / 255),
533
+ Ot((t.data[R + 2] ?? 255) / 255)
534
+ ];
535
+ }, v = g(h, m), A = g(f, m), M = g(h, u), p = g(f, u), y = (1 - c) * (1 - d), O = c * (1 - d), k = (1 - c) * d, U = c * d;
536
+ return [
537
+ v[0] * y + A[0] * O + M[0] * k + p[0] * U,
538
+ v[1] * y + A[1] * O + M[1] * k + p[1] * U,
539
+ v[2] * y + A[2] * O + M[2] * k + p[2] * U
540
+ ];
541
+ }
542
+ function tn(t, a, e) {
543
+ var u, c, d, x, b, g;
544
+ const n = t.getComponent(a, kt), s = n == null ? void 0 : n.material;
545
+ if (!s || typeof s == "string")
546
+ return {
547
+ baseDiffuse: [0.65, 0.65, 0.65],
548
+ emissive: [0, 0, 0],
549
+ usesMeshMaps: !1,
550
+ srgbImg: null
551
+ };
552
+ const i = (c = (u = s.properties) == null ? void 0 : u.get) == null ? void 0 : c.call(u, "baseColor"), r = Array.isArray(i) || ArrayBuffer.isView(i) ? i : s.diffuseColor, l = (x = (d = s.properties) == null ? void 0 : d.get) == null ? void 0 : x.call(d, "metallic"), h = 1 - Math.min(
553
+ 1,
554
+ Math.max(0, typeof l == "number" ? l : Number(s.metallic ?? 0))
555
+ ), m = (g = (b = s.properties) == null ? void 0 : b.get) == null ? void 0 : g.call(b, "emissiveColor"), f = Array.isArray(m) || ArrayBuffer.isView(m) ? m : s.emissiveColor;
556
+ return {
557
+ baseDiffuse: [
558
+ Math.max(0, Number((r == null ? void 0 : r[0]) ?? 0.65)) * h,
559
+ Math.max(0, Number((r == null ? void 0 : r[1]) ?? 0.65)) * h,
560
+ Math.max(0, Number((r == null ? void 0 : r[2]) ?? 0.65)) * h
561
+ ],
562
+ emissive: [
563
+ Math.max(0, Number((f == null ? void 0 : f[0]) ?? 0)),
564
+ Math.max(0, Number((f == null ? void 0 : f[1]) ?? 0)),
565
+ Math.max(0, Number((f == null ? void 0 : f[2]) ?? 0))
566
+ ],
567
+ usesMeshMaps: On(s),
568
+ srgbImg: rr(s, e == null ? void 0 : e.resolveBaseColorPixels)
569
+ };
570
+ }
571
+ function ar(t, a, e, n) {
572
+ const s = _t, i = e.vertices, r = Math.floor(i.length / s), l = new Float32Array(r * 3), { baseDiffuse: o, usesMeshMaps: h, srgbImg: m } = tn(
573
+ t,
574
+ a,
575
+ n
576
+ );
577
+ for (let f = 0; f < r; f++) {
578
+ const u = f * s, c = ee(i, u, h);
579
+ let d = 1, x = 1, b = 1;
580
+ if (m) {
581
+ const g = i[u + tt.uv0] ?? 0, v = i[u + tt.uv0 + 1] ?? 0;
582
+ [d, x, b] = Je(m, g, v);
583
+ }
584
+ l[f * 3] = Math.min(Lt, o[0] * c[0] * d), l[f * 3 + 1] = Math.min(Lt, o[1] * c[1] * x), l[f * 3 + 2] = Math.min(Lt, o[2] * c[2] * b);
585
+ }
586
+ return l;
587
+ }
588
+ function sr(t) {
589
+ const a = new DataView(t.nodes), e = [];
590
+ function n(s) {
591
+ const i = s * 32, r = a.getFloat32(i, !0), l = a.getFloat32(i + 4, !0), o = a.getFloat32(i + 8, !0), h = a.getUint32(i + 12, !0), m = a.getFloat32(i + 16, !0), f = a.getFloat32(i + 20, !0), u = a.getFloat32(i + 24, !0), c = a.getUint32(i + 28, !0), d = e.length;
592
+ if (Dn(h)) {
593
+ const g = h & 2147483647;
594
+ return e.push({
595
+ min: [r, l, o],
596
+ max: [m, f, u],
597
+ left: -1,
598
+ right: -1,
599
+ triStart: c,
600
+ triCount: g
601
+ }), d;
602
+ }
603
+ e.push({
604
+ min: [r, l, o],
605
+ max: [m, f, u],
606
+ left: 0,
607
+ right: 0,
608
+ triStart: 0,
609
+ triCount: 0
610
+ });
611
+ const x = n(h), b = n(c);
612
+ return e[d].left = x, e[d].right = b, d;
613
+ }
614
+ return n(t.rootIndex), e;
615
+ }
616
+ function ir(t, a, e) {
617
+ const n = [], s = [], i = [], r = [];
618
+ for (const M of a) {
619
+ const p = t.getComponent(M.entity, St), y = (p == null ? void 0 : p.worldTransform) ?? ie.create(), O = tn(t, M.entity, e);
620
+ or(M.mesh, y, n, s, O, i, r);
621
+ }
622
+ const l = Math.floor(n.length / 9);
623
+ if (l < 1) return null;
624
+ const o = new Float32Array(n), h = new Float32Array(s), m = new Float32Array(i), f = new Float32Array(r), u = new Uint8Array(l), c = new Float32Array(l * Le);
625
+ for (let M = 0; M < l; M++) {
626
+ const p = M * Le, y = M * 9;
627
+ for (let H = 0; H < 9; H++)
628
+ c[p + H] = o[y + H];
629
+ const O = M * 3, k = f[O], U = f[O + 1], D = f[O + 2];
630
+ c[p + 53] = k, c[p + 54] = U, c[p + 55] = D, Math.max(k, U, D) > 1e-4 && (u[M] = 1);
631
+ }
632
+ const d = Bn(c, l, null), x = c.slice(), b = Cn(x, l);
633
+ if (!b) return null;
634
+ const g = b.permutation ?? new Uint32Array(l);
635
+ if (!b.permutation)
636
+ for (let M = 0; M < l; M++) g[M] = M;
637
+ const v = new Uint32Array(l);
638
+ for (let M = 0; M < l; M++)
639
+ v[g[M]] = M;
640
+ const A = sr(b);
641
+ return {
642
+ tris: o,
643
+ triUv1: h,
644
+ triOrder: g,
645
+ origToLeafOrder: v,
646
+ nodes: A,
647
+ root: 0,
648
+ albedo: m,
649
+ emissive: f,
650
+ isEmissiveTri: u,
651
+ emissiveCatalog: d,
652
+ bounceSrc: new Float32Array(l * 3)
653
+ };
654
+ }
655
+ function or(t, a, e, n, s, i, r) {
656
+ const l = _t, o = t.vertices, h = t.indices, m = ot.create(), f = ot.create(), u = ot.create(), c = 1 / 3;
657
+ for (let d = 0; d + 2 < h.length; d += 3) {
658
+ const x = h[d] * l, b = h[d + 1] * l, g = h[d + 2] * l;
659
+ ot.set(m, o[x], o[x + 1], o[x + 2]), ot.set(f, o[b], o[b + 1], o[b + 2]), ot.set(u, o[g], o[g + 1], o[g + 2]), ot.transformMat4(m, m, a), ot.transformMat4(f, f, a), ot.transformMat4(u, u, a), e.push(m[0], m[1], m[2], f[0], f[1], f[2], u[0], u[1], u[2]), n.push(
660
+ o[x + tt.uv1] ?? 0,
661
+ o[x + tt.uv1 + 1] ?? 0,
662
+ o[b + tt.uv1] ?? 0,
663
+ o[b + tt.uv1 + 1] ?? 0,
664
+ o[g + tt.uv1] ?? 0,
665
+ o[g + tt.uv1 + 1] ?? 0
666
+ );
667
+ const v = ee(o, x, s.usesMeshMaps), A = ee(o, b, s.usesMeshMaps), M = ee(o, g, s.usesMeshMaps), p = (v[0] + A[0] + M[0]) * c, y = (v[1] + A[1] + M[1]) * c, O = (v[2] + A[2] + M[2]) * c;
668
+ let k = 1, U = 1, D = 1;
669
+ if (s.srgbImg) {
670
+ const H = ((o[x + tt.uv0] ?? 0) + (o[b + tt.uv0] ?? 0) + (o[g + tt.uv0] ?? 0)) * c, R = ((o[x + tt.uv0 + 1] ?? 0) + (o[b + tt.uv0 + 1] ?? 0) + (o[g + tt.uv0 + 1] ?? 0)) * c;
671
+ [k, U, D] = Je(s.srgbImg, H, R);
672
+ }
673
+ i.push(
674
+ Math.min(Lt, s.baseDiffuse[0] * p * k),
675
+ Math.min(Lt, s.baseDiffuse[1] * y * U),
676
+ Math.min(Lt, s.baseDiffuse[2] * O * D)
677
+ ), r.push(s.emissive[0], s.emissive[1], s.emissive[2]);
678
+ }
679
+ }
680
+ const ht = new Int32Array(128), Rt = { t: 0, u: 0, v: 0 };
681
+ function Ae(t, a, e, n, s = -1) {
682
+ const i = a[0], r = a[1], l = a[2], o = e[0], h = e[1], m = e[2];
683
+ if (Bt(
684
+ i,
685
+ r,
686
+ l,
687
+ o,
688
+ h,
689
+ m,
690
+ t.nodes[t.root].min,
691
+ t.nodes[t.root].max,
692
+ n
693
+ ) < 0)
694
+ return !1;
695
+ let f = 0;
696
+ for (ht[f++] = t.root; f > 0; ) {
697
+ const u = ht[--f], c = t.nodes[u];
698
+ if (c.left < 0) {
699
+ for (let v = 0; v < c.triCount; v++) {
700
+ const A = t.triOrder[c.triStart + v];
701
+ if (A === s) continue;
702
+ const M = A * 9;
703
+ if (en(
704
+ i,
705
+ r,
706
+ l,
707
+ o,
708
+ h,
709
+ m,
710
+ t.tris[M],
711
+ t.tris[M + 1],
712
+ t.tris[M + 2],
713
+ t.tris[M + 3],
714
+ t.tris[M + 4],
715
+ t.tris[M + 5],
716
+ t.tris[M + 6],
717
+ t.tris[M + 7],
718
+ t.tris[M + 8],
719
+ Rt
720
+ ) && Rt.t < n)
721
+ return !0;
722
+ }
723
+ continue;
724
+ }
725
+ const d = t.nodes[c.left], x = t.nodes[c.right], b = Bt(i, r, l, o, h, m, d.min, d.max, n), g = Bt(i, r, l, o, h, m, x.min, x.max, n);
726
+ b >= 0 && g >= 0 ? b <= g ? (ht[f++] = c.right, ht[f++] = c.left) : (ht[f++] = c.left, ht[f++] = c.right) : b >= 0 ? ht[f++] = c.left : g >= 0 && (ht[f++] = c.right);
727
+ }
728
+ return !1;
729
+ }
730
+ function Se(t, a, e, n, s, i = !1) {
731
+ const r = a[0], l = a[1], o = a[2], h = e[0], m = e[1], f = e[2];
732
+ let u = n, c = -1, d = 0, x = 0, b = 0, g = 1, v = 0;
733
+ if (Bt(
734
+ r,
735
+ l,
736
+ o,
737
+ h,
738
+ m,
739
+ f,
740
+ t.nodes[t.root].min,
741
+ t.nodes[t.root].max,
742
+ u
743
+ ) < 0)
744
+ return null;
745
+ let A = 0;
746
+ for (ht[A++] = t.root; A > 0; ) {
747
+ const M = ht[--A], p = t.nodes[M];
748
+ if (Bt(r, l, o, h, m, f, p.min, p.max, u) < 0) continue;
749
+ if (p.left < 0) {
750
+ for (let D = 0; D < p.triCount; D++) {
751
+ const H = t.triOrder[p.triStart + D];
752
+ if (i && t.isEmissiveTri[H] !== 0) continue;
753
+ const R = H * 9, F = t.tris[R], q = t.tris[R + 1], W = t.tris[R + 2], z = t.tris[R + 3], Y = t.tris[R + 4], G = t.tris[R + 5], L = t.tris[R + 6], V = t.tris[R + 7], j = t.tris[R + 8];
754
+ if (en(r, l, o, h, m, f, F, q, W, z, Y, G, L, V, j, Rt)) {
755
+ const X = Rt.t;
756
+ if (X > 1e-4 && X < u) {
757
+ u = X, c = H, d = Rt.u, x = Rt.v;
758
+ const _ = z - F, C = Y - q, S = G - W, I = L - F, T = V - q, P = j - W;
759
+ let w = C * P - S * T, B = S * I - _ * P, N = _ * T - C * I;
760
+ const E = Math.hypot(w, B, N) || 1;
761
+ w /= E, B /= E, N /= E, w * h + B * m + N * f > 0 && (w = -w, B = -B, N = -N), b = w, g = B, v = N;
762
+ }
763
+ }
764
+ }
765
+ continue;
766
+ }
767
+ const y = t.nodes[p.left], O = t.nodes[p.right], k = Bt(r, l, o, h, m, f, y.min, y.max, u), U = Bt(r, l, o, h, m, f, O.min, O.max, u);
768
+ k >= 0 && U >= 0 ? k <= U ? (ht[A++] = p.right, ht[A++] = p.left) : (ht[A++] = p.left, ht[A++] = p.right) : k >= 0 ? ht[A++] = p.left : U >= 0 && (ht[A++] = p.right);
769
+ }
770
+ return c < 0 ? null : s ? (s.t = u, s.triIndex = c, s.baryU = d, s.baryV = x, s.normal[0] = b, s.normal[1] = g, s.normal[2] = v, s) : {
771
+ t: u,
772
+ triIndex: c,
773
+ baryU: d,
774
+ baryV: x,
775
+ normal: [b, g, v]
776
+ };
777
+ }
778
+ function cr(t, a, e, n, s = !0, i = 1) {
779
+ const r = t.emissiveCatalog.count, l = t.emissiveCatalog.totalPower;
780
+ if (r === 0 || l <= 0 || n < 1) return [0, 0, 0];
781
+ const o = new DataView(t.emissiveCatalog.bytes), h = 15e-4, m = [
782
+ a[0] + e[0] * h,
783
+ a[1] + e[1] * h,
784
+ a[2] + e[2] * h
785
+ ], [f, u] = oe(i ^ 1831565813);
786
+ let c = 0, d = 0, x = 0;
787
+ for (let g = 0; g < n; g++) {
788
+ const v = Math.min(0.999999, (g + f) / n);
789
+ let A = 0, M = r - 1;
790
+ for (; A < M; ) {
791
+ const ft = A + M >>> 1, Ut = o.getFloat32(16 + ft * 16 + 8, !0);
792
+ v <= Ut ? M = ft : A = ft + 1;
793
+ }
794
+ const p = 16 + A * 16, y = o.getUint32(p, !0);
795
+ if (o.getFloat32(p + 12, !0) <= 1e-10) continue;
796
+ const k = ve(u + g * 0.7548776662466927), U = ve(f * 0.713 + g * 0.5698402909980532), D = Math.sqrt(k), H = 1 - D, R = U * D, F = 1 - H - R, q = y * 9, W = t.tris[q], z = t.tris[q + 1], Y = t.tris[q + 2], G = t.tris[q + 3], L = t.tris[q + 4], V = t.tris[q + 5], j = t.tris[q + 6], X = t.tris[q + 7], _ = t.tris[q + 8], C = W * H + G * R + j * F, S = z * H + L * R + X * F, I = Y * H + V * R + _ * F, T = C - a[0], P = S - a[1], w = I - a[2], B = T * T + P * P + w * w;
797
+ if (B <= 1e-8) continue;
798
+ const E = 1 / Math.sqrt(B), Z = T * E, J = P * E, et = w * E, rt = e[0] * Z + e[1] * J + e[2] * et;
799
+ if (rt <= 15e-4) continue;
800
+ const $ = G - W, Q = L - z, K = V - Y, nt = j - W, ct = X - z, it = _ - Y;
801
+ let st = Q * it - K * ct, at = K * nt - $ * it, ut = $ * ct - Q * nt;
802
+ const lt = Math.hypot(st, at, ut);
803
+ if (lt <= 1e-10) continue;
804
+ st /= lt, at /= lt, ut /= lt;
805
+ const gt = -(st * Z + at * J + ut * et);
806
+ if (gt <= 1e-4) continue;
807
+ const bt = y * 3, vt = t.emissive[bt], Mt = t.emissive[bt + 1], dt = t.emissive[bt + 2], wt = 0.2126 * vt + 0.7152 * Mt + 0.0722 * dt;
808
+ if (wt <= 1e-7) continue;
809
+ if (s) {
810
+ const ft = C - m[0], Ut = S - m[1], Vt = I - m[2], It = Math.hypot(ft, Ut, Vt);
811
+ if (It > 2e-3 && Ae(
812
+ t,
813
+ m,
814
+ [ft / It, Ut / It, Vt / It],
815
+ It - 2e-3,
816
+ y
817
+ ))
818
+ continue;
819
+ }
820
+ const xt = rt * gt * l / (Math.PI * wt * Math.max(B, 25e-4));
821
+ c += vt * xt, d += Mt * xt, x += dt * xt;
822
+ }
823
+ const b = 1 / n;
824
+ return [c * b, d * b, x * b];
825
+ }
826
+ function he(t, a, e, n, s = 20, i) {
827
+ if (n < 1) return [0, 0, 0];
828
+ const r = 15e-4, l = [
829
+ a[0] + e[0] * r,
830
+ a[1] + e[1] * r,
831
+ a[2] + e[2] * r
832
+ ], [o, h] = (i == null ? void 0 : i.jitterSeed) !== void 0 ? oe(i.jitterSeed) : [0.5, 0], m = i == null ? void 0 : i.atlasRgba, f = (i == null ? void 0 : i.atlasSize) ?? 0, u = i == null ? void 0 : i.skyRadiance;
833
+ let c = 0, d = 0, x = 0;
834
+ const b = { t: 0, triIndex: -1, baryU: 0, baryV: 0, normal: [0, 1, 0] };
835
+ for (let v = 0; v < n; v++) {
836
+ const A = rn(e, v, n, o, h), M = Se(t, l, A, s, b);
837
+ if (!M) {
838
+ if (u) {
839
+ const z = typeof u == "function" ? u(A) : u;
840
+ c += z[0], d += z[1], x += z[2];
841
+ }
842
+ continue;
843
+ }
844
+ const p = M.triIndex * 3, y = t.albedo[p], O = t.albedo[p + 1], k = t.albedo[p + 2];
845
+ let U = t.bounceSrc[p], D = t.bounceSrc[p + 1], H = t.bounceSrc[p + 2];
846
+ if (m && f > 0) {
847
+ const z = M.triIndex * 6, Y = 1 - M.baryU - M.baryV, G = Y * t.triUv1[z] + M.baryU * t.triUv1[z + 2] + M.baryV * t.triUv1[z + 4], L = Y * t.triUv1[z + 1] + M.baryU * t.triUv1[z + 3] + M.baryV * t.triUv1[z + 5], V = Math.min(f - 1, Math.max(0, Math.floor(G * f))), X = (Math.min(f - 1, Math.max(0, Math.floor(L * f))) * f + V) * 4;
848
+ (m[X + 3] ?? 0) > 0 && (U = m[X], D = m[X + 1], H = m[X + 2]);
849
+ }
850
+ const R = y * U, F = O * D, q = k * H, W = Math.max(R, F, q);
851
+ if (W > 2.5) {
852
+ const Y = (2.5 + (W - 2.5) / (1 + (W - 2.5) * 0.5)) / W;
853
+ c += R * Y, d += F * Y, x += q * Y;
854
+ } else
855
+ c += R, d += F, x += q;
856
+ }
857
+ const g = 1 / n;
858
+ return [c * g, d * g, x * g];
859
+ }
860
+ function qr(t, a, e, n) {
861
+ const s = e.indices, i = Math.floor(s.length / 3);
862
+ for (let r = 0; r < i; r++) {
863
+ const l = s[r * 3], o = s[r * 3 + 1], h = s[r * 3 + 2], m = (a + r) * 3;
864
+ t.bounceSrc[m] = (n[l * 3] + n[o * 3] + n[h * 3]) / 3, t.bounceSrc[m + 1] = (n[l * 3 + 1] + n[o * 3 + 1] + n[h * 3 + 1]) / 3, t.bounceSrc[m + 2] = (n[l * 3 + 2] + n[o * 3 + 2] + n[h * 3 + 2]) / 3;
865
+ }
866
+ return i;
867
+ }
868
+ function re(t, a, e) {
869
+ const n = Math.floor(t.tris.length / 9);
870
+ for (let s = 0; s < n; s++) {
871
+ const i = s * 6, r = (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(r * e))), m = (Math.min(e - 1, Math.max(0, Math.floor(l * e))) * e + o) * 4;
872
+ if ((a[m + 3] ?? 0) > 0) {
873
+ const f = s * 3;
874
+ t.bounceSrc[f] = a[m], t.bounceSrc[f + 1] = a[m + 1], t.bounceSrc[f + 2] = a[m + 2];
875
+ }
876
+ }
877
+ }
878
+ function Bt(t, a, e, n, s, i, r, l, o) {
879
+ let h = 0, m = o;
880
+ if (Math.abs(n) < 1e-12) {
881
+ if (t < r[0] || t > l[0]) return -1;
882
+ } else {
883
+ const f = 1 / n;
884
+ let u = (r[0] - t) * f, c = (l[0] - t) * f;
885
+ if (u > c) {
886
+ const d = u;
887
+ u = c, c = d;
888
+ }
889
+ if (u > h && (h = u), c < m && (m = c), h > m) return -1;
890
+ }
891
+ if (Math.abs(s) < 1e-12) {
892
+ if (a < r[1] || a > l[1]) return -1;
893
+ } else {
894
+ const f = 1 / s;
895
+ let u = (r[1] - a) * f, c = (l[1] - a) * f;
896
+ if (u > c) {
897
+ const d = u;
898
+ u = c, c = d;
899
+ }
900
+ if (u > h && (h = u), c < m && (m = c), h > m) return -1;
901
+ }
902
+ if (Math.abs(i) < 1e-12) {
903
+ if (e < r[2] || e > l[2]) return -1;
904
+ } else {
905
+ const f = 1 / i;
906
+ let u = (r[2] - e) * f, c = (l[2] - e) * f;
907
+ if (u > c) {
908
+ const d = u;
909
+ u = c, c = d;
910
+ }
911
+ if (u > h && (h = u), c < m && (m = c), h > m) return -1;
912
+ }
913
+ return h;
914
+ }
915
+ function en(t, a, e, n, s, i, r, l, o, h, m, f, u, c, d, x) {
916
+ const b = h - r, g = m - l, v = f - o, A = u - r, M = c - l, p = d - o, y = s * p - i * M, O = i * A - n * p, k = n * M - s * A, U = b * y + g * O + v * k;
917
+ if (U > -1e-8 && U < 1e-8) return !1;
918
+ const D = 1 / U, H = t - r, R = a - l, F = e - o, q = D * (H * y + R * O + F * k);
919
+ if (q < 0 || q > 1) return !1;
920
+ const W = R * v - F * g, z = F * b - H * v, Y = H * g - R * b, G = D * (n * W + s * z + i * Y);
921
+ if (G < 0 || q + G > 1) return !1;
922
+ const L = D * (A * W + M * z + p * Y);
923
+ return L <= 1e-4 ? !1 : (x.t = L, x.u = q, x.v = G, !0);
924
+ }
925
+ const ur = {
926
+ t: 0,
927
+ triIndex: -1,
928
+ baryU: 0,
929
+ baryV: 0,
930
+ normal: [0, 1, 0]
931
+ };
932
+ function lr(t, a, e, n = 4, s = 0.4, i) {
933
+ if (n < 1 || s <= 1e-4) return 1;
934
+ const r = 15e-4, l = [
935
+ a[0] + e[0] * r,
936
+ a[1] + e[1] * r,
937
+ a[2] + e[2] * r
938
+ ], [o, h] = i !== void 0 ? oe(i) : [0.5, 0];
939
+ let m = 0;
940
+ for (let f = 0; f < n; f++) {
941
+ const u = rn(e, f, n, o, h), c = Se(t, l, u, s, ur, !0);
942
+ if (c && c.t < s) {
943
+ const d = 1 - c.t / s;
944
+ m += d * d;
945
+ }
946
+ }
947
+ return Math.max(0.35, 1 - 0.65 * (m / n));
948
+ }
949
+ function nn(t) {
950
+ const a = t.nodes.length, e = new ArrayBuffer(Math.max(1, a) * 32), n = new Float32Array(e), s = new Int32Array(e);
951
+ for (let c = 0; c < a; c++) {
952
+ const d = t.nodes[c], x = c * 8;
953
+ n[x] = d.min[0], n[x + 1] = d.min[1], n[x + 2] = d.min[2], n[x + 4] = d.max[0], n[x + 5] = d.max[1], n[x + 6] = d.max[2], d.left < 0 ? (s[x + 3] = -1 - d.triStart, s[x + 7] = d.triCount) : (s[x + 3] = d.left, s[x + 7] = d.right);
954
+ }
955
+ const i = t.triOrder.length, r = new Float32Array(Math.max(1, i) * 12), l = new Float32Array(Math.max(1, i) * 8), o = new Float32Array(Math.max(1, i) * 4);
956
+ for (let c = 0; c < i; c++) {
957
+ const d = t.triOrder[c], x = d * 9, b = c * 12, g = t.tris[x], v = t.tris[x + 1], A = t.tris[x + 2];
958
+ r[b] = g, r[b + 1] = v, r[b + 2] = A, r[b + 3] = t.isEmissiveTri[d] ? 1 : 0, r[b + 4] = t.tris[x + 3] - g, r[b + 5] = t.tris[x + 4] - v, r[b + 6] = t.tris[x + 5] - A, r[b + 7] = c, r[b + 8] = t.tris[x + 6] - g, r[b + 9] = t.tris[x + 7] - v, r[b + 10] = t.tris[x + 8] - A, r[b + 11] = 0;
959
+ const M = d * 6, p = c * 8;
960
+ l[p] = t.triUv1[M], l[p + 1] = t.triUv1[M + 1], l[p + 2] = t.triUv1[M + 2], l[p + 3] = t.triUv1[M + 3], l[p + 4] = t.triUv1[M + 4], l[p + 5] = t.triUv1[M + 5];
961
+ const y = d * 3, O = c * 4;
962
+ o[O] = t.albedo[y], o[O + 1] = t.albedo[y + 1], o[O + 2] = t.albedo[y + 2], o[O + 3] = 1;
963
+ }
964
+ const h = t.emissiveCatalog.count, m = t.emissiveCatalog.totalPower, f = new Float32Array(Math.max(1, h) * 16), u = new DataView(t.emissiveCatalog.bytes);
965
+ for (let c = 0; c < h; c++) {
966
+ const d = 16 + c * 16, x = u.getUint32(d, !0), b = u.getFloat32(d + 8, !0), g = u.getFloat32(d + 12, !0), v = t.origToLeafOrder[x] ?? 0, A = x * 9, M = x * 3, p = t.tris[A], y = t.tris[A + 1], O = t.tris[A + 2], k = c * 16;
967
+ f[k] = p, f[k + 1] = y, f[k + 2] = O, f[k + 3] = b, f[k + 4] = t.tris[A + 3] - p, f[k + 5] = t.tris[A + 4] - y, f[k + 6] = t.tris[A + 5] - O, f[k + 7] = g, f[k + 8] = t.tris[A + 6] - p, f[k + 9] = t.tris[A + 7] - y, f[k + 10] = t.tris[A + 8] - O, f[k + 11] = v, f[k + 12] = t.emissive[M], f[k + 13] = t.emissive[M + 1], f[k + 14] = t.emissive[M + 2], f[k + 15] = m;
968
+ }
969
+ return {
970
+ nodeBytes: e,
971
+ nodeCount: a,
972
+ rootIndex: Math.max(0, t.root),
973
+ triFloats: r,
974
+ triCount: i,
975
+ triUv1Floats: l,
976
+ triAlbedoFloats: o,
977
+ emissiveFloats: f,
978
+ emissiveCount: h
979
+ };
980
+ }
981
+ function rn(t, a, e, n, s) {
982
+ const i = Math.min(0.999999, (a + n) / e), r = ve(a * nr + s), l = Math.sqrt(i), o = 2 * Math.PI * r, h = l * Math.cos(o), m = l * Math.sin(o), f = Math.sqrt(Math.max(0, 1 - i)), u = Math.abs(t[1]) < 0.999 ? 0 : 1, c = Math.abs(t[1]) < 0.999 ? 1 : 0;
983
+ let d = c * t[2], x = -u * t[2], b = u * t[1] - c * t[0];
984
+ const g = Math.hypot(d, x, b) || 1;
985
+ d /= g, x /= g, b /= g;
986
+ const v = t[1] * b - t[2] * x, A = t[2] * d - t[0] * b, M = t[0] * x - t[1] * d, p = d * h + v * m + t[0] * f, y = x * h + A * m + t[1] * f, O = b * h + M * m + t[2] * f, k = Math.hypot(p, y, O) || 1;
987
+ return [p / k, y / k, O / k];
988
+ }
989
+ function ve(t) {
990
+ return t - Math.floor(t);
991
+ }
992
+ const an = 0.6180339887498949;
993
+ function fr(t) {
994
+ let a = t >>> 0;
995
+ a ^= a >>> 16, a = Math.imul(a, 2146121005) >>> 0, a ^= a >>> 15, a = Math.imul(a, 2221713035) >>> 0, a ^= a >>> 16;
996
+ const e = (a & 65535) / 65536, n = (a >>> 16 & 65535) / 65536;
997
+ return [e, n];
998
+ }
999
+ function de(t, a, e, n, s, i) {
1000
+ if (!s) return 1;
1001
+ const r = Math.max(1, (i == null ? void 0 : i.samples) ?? 6), [l, o] = (i == null ? void 0 : i.jitterSeed) !== void 0 ? fr(i.jitterSeed) : [0.5, 0];
1002
+ if (t.kind === "point" || t.kind === "spot") {
1003
+ const u = (i == null ? void 0 : i.pointLightRadius) ?? 0.16;
1004
+ if (r <= 1 || u <= 1e-5 || n <= 1e-4)
1005
+ return s(a, e, n) ? 0 : 1;
1006
+ const c = Math.min(u, n * 0.35), d = [
1007
+ a[0] + e[0] * n,
1008
+ a[1] + e[1] * n,
1009
+ a[2] + e[2] * n
1010
+ ], x = [
1011
+ -e[0],
1012
+ -e[1],
1013
+ -e[2]
1014
+ ];
1015
+ let b = 0;
1016
+ for (let g = 0; g < r; g++) {
1017
+ const v = Ne(
1018
+ d,
1019
+ x,
1020
+ c,
1021
+ g,
1022
+ r,
1023
+ l,
1024
+ o
1025
+ ), A = v[0] - a[0], M = v[1] - a[1], p = v[2] - a[2], y = Math.hypot(A, M, p) || 1e-6, O = [A / y, M / y, p / y];
1026
+ s(a, O, Math.max(0, y - 1e-3)) || b++;
1027
+ }
1028
+ return b / r;
1029
+ }
1030
+ if (t.kind === "directional") {
1031
+ const u = (i == null ? void 0 : i.sunAngularRadius) ?? 0.012;
1032
+ if (r <= 1 || u <= 0)
1033
+ return s(a, e, n) ? 0 : 1;
1034
+ let c = 0;
1035
+ for (let d = 0; d < r; d++) {
1036
+ const x = hr(e, u, d, r, l, o);
1037
+ s(a, x, n) || c++;
1038
+ }
1039
+ return c / r;
1040
+ }
1041
+ const h = Math.max(t.area, 1e-6), m = Math.sqrt(h / Math.PI);
1042
+ let f = 0;
1043
+ for (let u = 0; u < r; u++) {
1044
+ const c = Ne(
1045
+ t.position,
1046
+ t.normal,
1047
+ m,
1048
+ u,
1049
+ r,
1050
+ l,
1051
+ o
1052
+ ), d = c[0] - a[0], x = c[1] - a[1], b = c[2] - a[2], g = Math.hypot(d, x, b) || 1e-6, v = [d / g, x / g, b / g];
1053
+ s(a, v, Math.max(0, g - 1e-3)) || f++;
1054
+ }
1055
+ return f / r;
1056
+ }
1057
+ function sn(t) {
1058
+ const a = Math.abs(t[1]) < 0.999 ? 0 : 1, e = Math.abs(t[1]) < 0.999 ? 1 : 0;
1059
+ let n = e * t[2], s = -a * t[2], i = a * t[1] - e * t[0];
1060
+ const r = Math.hypot(n, s, i) || 1;
1061
+ n /= r, s /= r, i /= r;
1062
+ const l = t[1] * i - t[2] * s, o = t[2] * n - t[0] * i, h = t[0] * s - t[1] * n;
1063
+ return { t: [n, s, i], b: [l, o, h] };
1064
+ }
1065
+ function Ne(t, a, e, n, s, i, r) {
1066
+ const { t: l, b: o } = sn(a), h = Math.min(0.999999, (n + i) / s), m = on(n * an + r), f = Math.sqrt(h) * e, u = 2 * Math.PI * m, c = f * Math.cos(u), d = f * Math.sin(u);
1067
+ return [
1068
+ t[0] + l[0] * c + o[0] * d,
1069
+ t[1] + l[1] * c + o[1] * d,
1070
+ t[2] + l[2] * c + o[2] * d
1071
+ ];
1072
+ }
1073
+ function hr(t, a, e, n, s, i) {
1074
+ const { t: r, b: l } = sn(t), o = Math.min(0.999999, (e + s) / n), h = on(e * an + i + 0.1), m = Math.sqrt(o) * a, f = 2 * Math.PI * h, u = m * Math.cos(f), c = m * Math.sin(f), d = t[0] + r[0] * u + l[0] * c, x = t[1] + r[1] * u + l[1] * c, b = t[2] + r[2] * u + l[2] * c, g = Math.hypot(d, x, b) || 1;
1075
+ return [d / g, x / g, b / g];
1076
+ }
1077
+ function on(t) {
1078
+ return t - Math.floor(t);
1079
+ }
1080
+ function $t(t) {
1081
+ 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)];
1082
+ }
1083
+ const dr = ot.fromValues(0, -1, 0), ze = kn.create(), Et = ot.create(), Ee = 0.6180339887498949, jt = 0.05;
1084
+ function mr(t) {
1085
+ if (!t) return [0, 1, 0];
1086
+ ie.getRotation(ze, t), ot.transformQuat(Et, dr, ze);
1087
+ const a = ot.length(Et) || 1;
1088
+ return [-Et[0] / a, -Et[1] / a, -Et[2] / a];
1089
+ }
1090
+ function gr(t) {
1091
+ const a = (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(a, e, n) || 1;
1092
+ return [a / s, e / s, n / s];
1093
+ }
1094
+ function me(t) {
1095
+ const a = (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(a, e, n) || 1;
1096
+ return [a / s, e / s, n / s];
1097
+ }
1098
+ function xr(t) {
1099
+ if (!t) return [1, 1];
1100
+ const a = 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;
1101
+ return [a, e];
1102
+ }
1103
+ function ge(t) {
1104
+ return [t[12] ?? 0, t[13] ?? 0, t[14] ?? 0];
1105
+ }
1106
+ function cn(t) {
1107
+ const a = Math.abs(t[1]) < 0.99 ? 0 : 1, e = Math.abs(t[1]) < 0.99 ? 1 : 0, n = e * t[2], s = -a * t[2], i = a * t[1] - e * t[0], r = Math.hypot(n, s, i) || 1;
1108
+ return [n / r, s / r, i / r];
1109
+ }
1110
+ function br(t) {
1111
+ var e, n;
1112
+ const a = [];
1113
+ for (const [s, [i]] of t.query([wn]).iter()) {
1114
+ if (!Dt(t, s)) continue;
1115
+ const r = t.getComponent(s, St);
1116
+ a.push({
1117
+ kind: "directional",
1118
+ color: $t(i.color),
1119
+ intensity: i.intensity,
1120
+ toLight: mr(r == null ? void 0 : r.worldTransform)
1121
+ });
1122
+ }
1123
+ for (const [s, [i]] of t.query([An]).iter()) {
1124
+ if (!Dt(t, s)) continue;
1125
+ const r = t.getComponent(s, St);
1126
+ a.push({
1127
+ kind: "point",
1128
+ color: $t(i.color),
1129
+ intensity: i.intensity,
1130
+ position: r ? ge(r.worldTransform) : [0, 0, 0],
1131
+ range: i.range
1132
+ });
1133
+ }
1134
+ for (const [s, [i]] of t.query([Sn]).iter()) {
1135
+ if (!Dt(t, s)) continue;
1136
+ const r = t.getComponent(s, St), l = me(r == null ? void 0 : r.worldTransform);
1137
+ a.push({
1138
+ kind: "spot",
1139
+ color: $t(i.color),
1140
+ intensity: i.intensity,
1141
+ position: r ? ge(r.worldTransform) : [0, 0, 0],
1142
+ range: i.range,
1143
+ direction: [-l[0], -l[1], -l[2]],
1144
+ innerCos: Math.cos(i.innerAngle),
1145
+ outerCos: Math.cos(i.outerAngle)
1146
+ });
1147
+ }
1148
+ for (const [s, [i]] of t.query([Un]).iter()) {
1149
+ if (!Dt(t, s)) continue;
1150
+ const r = t.getComponent(s, St), l = r == null ? void 0 : r.worldTransform, o = me(l), h = gr(l), [m, f] = xr(l), u = Math.max(1e-3, i.width * m), c = Math.max(1e-3, i.height * f), d = i.shape === "disk", x = Math.max(1e-4, d ? Math.PI * (u * 0.5) * (c * 0.5) : u * c);
1151
+ a.push({
1152
+ kind: "area",
1153
+ color: $t(i.color),
1154
+ intensity: i.intensity,
1155
+ position: l ? ge(l) : [0, 0, 0],
1156
+ normal: [-o[0], -o[1], -o[2]],
1157
+ area: x,
1158
+ width: u,
1159
+ height: c,
1160
+ tangent: h,
1161
+ isDisk: d,
1162
+ range: i.range ?? 0
1163
+ });
1164
+ }
1165
+ for (const [s, [i]] of t.query([kt]).iter()) {
1166
+ if (i.visible === !1 || !Dt(t, s) || t.hasComponent(s, _e) || t.hasComponent(s, ae)) continue;
1167
+ const r = i.material;
1168
+ if (!r || typeof r == "string") continue;
1169
+ const l = (n = (e = r.properties) == null ? void 0 : e.get) == null ? void 0 : n.call(e, "emissiveColor"), o = Array.isArray(l) || ArrayBuffer.isView(l) ? l : r.emissiveColor, h = Number((o == null ? void 0 : o[0]) ?? 0), m = Number((o == null ? void 0 : o[1]) ?? 0), f = Number((o == null ? void 0 : o[2]) ?? 0), u = Math.max(h, m, f);
1170
+ if (u < 1e-4) continue;
1171
+ const c = i.mesh;
1172
+ if (!c || typeof c == "string") continue;
1173
+ const d = t.getComponent(s, St), x = ot.create();
1174
+ d ? ot.transformMat4(x, c.boundingSphere.center, d.worldTransform) : ot.copy(x, c.boundingSphere.center);
1175
+ const b = c.boundingSphere.radius, g = Math.max(1e-4, 4 * Math.PI * b * b * 0.25), v = me(d == null ? void 0 : d.worldTransform);
1176
+ a.push({
1177
+ kind: "emissive",
1178
+ color: [h / Math.max(u, 1e-6), m / Math.max(u, 1e-6), f / Math.max(u, 1e-6)],
1179
+ intensity: u,
1180
+ position: [x[0], x[1], x[2]],
1181
+ normal: [-v[0], -v[1], -v[2]],
1182
+ area: g
1183
+ });
1184
+ }
1185
+ return a;
1186
+ }
1187
+ function Kt(t) {
1188
+ return t - Math.floor(t);
1189
+ }
1190
+ function un(t, a, e, n, s) {
1191
+ let i = n[0], r = n[1], l = n[2];
1192
+ const o = e[0], h = e[1], m = e[2], f = (s == null ? void 0 : s.bias) ?? 15e-4, u = a[0] + o * f, c = a[1] + h * f, d = a[2] + m * f, x = [u, c, d], b = s == null ? void 0 : s.occluded, g = Math.max(1, (s == null ? void 0 : s.softShadowSamples) ?? 6), v = s == null ? void 0 : s.pointLightRadius, A = s == null ? void 0 : s.jitterSeed;
1193
+ for (let p = 0; p < t.length; p++) {
1194
+ const y = t[p], O = A !== void 0 ? (A ^ Math.imul(p + 1, 2654435769)) >>> 0 : void 0;
1195
+ if (y.kind === "directional") {
1196
+ const _ = o * y.toLight[0] + h * y.toLight[1] + m * y.toLight[2];
1197
+ if (_ <= 0) continue;
1198
+ const C = de(y, x, y.toLight, 1e4, b, {
1199
+ samples: g,
1200
+ jitterSeed: O
1201
+ });
1202
+ if (C <= 0) continue;
1203
+ const S = y.intensity * _ * C;
1204
+ i += y.color[0] * S, r += y.color[1] * S, l += y.color[2] * S;
1205
+ continue;
1206
+ }
1207
+ if (y.kind === "area") {
1208
+ const _ = Math.sqrt(Math.max(1e-6, y.area)), C = y.width ?? _, S = y.height ?? _, I = y.tangent ?? cn(y.normal), T = !!y.isDisk, P = y.range ?? 0, w = Math.max(4, g), B = Math.max(1, Math.ceil(Math.sqrt(w))), N = Math.max(1, Math.ceil(w / B)), [E, Z] = O !== void 0 ? oe(O) : [0.5, 0.5];
1209
+ let J = 0, et = 0, rt = 0, $ = 0;
1210
+ const Q = {
1211
+ position: y.position,
1212
+ direction: y.normal,
1213
+ tangent: I,
1214
+ width: C,
1215
+ height: S,
1216
+ color: y.color,
1217
+ intensity: y.intensity,
1218
+ range: P,
1219
+ isDisk: T
1220
+ };
1221
+ for (let K = 0; K < N && $ < w; K++)
1222
+ for (let nt = 0; nt < B && $ < w; nt++) {
1223
+ const ct = T ? Kt(($ + E) / w) : (nt + Kt(E + $ * Ee)) / B, it = T ? Kt(Z + $ * Ee) : (K + Kt(Z + $ * 0.7548776662)) / N;
1224
+ $++;
1225
+ const st = Rn(a, Q, ct, it);
1226
+ if (!st.valid || P > 0 && st.distance >= P) continue;
1227
+ const at = o * st.dir[0] + h * st.dir[1] + m * st.dir[2];
1228
+ if (!(at <= 15e-4)) {
1229
+ if (b) {
1230
+ const ut = a[0] + st.dir[0] * st.distance, lt = a[1] + st.dir[1] * st.distance, gt = a[2] + st.dir[2] * st.distance, bt = ut - u, vt = lt - c, Mt = gt - d, dt = Math.hypot(bt, vt, Mt);
1231
+ if (dt > f * 2 && b(x, [bt / dt, vt / dt, Mt / dt], dt - f * 2))
1232
+ continue;
1233
+ }
1234
+ J += st.radianceOverPdf[0] * at, et += st.radianceOverPdf[1] * at, rt += st.radianceOverPdf[2] * at;
1235
+ }
1236
+ }
1237
+ if ($ > 0) {
1238
+ const K = 1 / $;
1239
+ i += J * K, r += et * K, l += rt * K;
1240
+ }
1241
+ continue;
1242
+ }
1243
+ const k = y.position[0] - a[0], U = y.position[1] - a[1], D = y.position[2] - a[2], H = k * k + U * U + D * D, R = Math.sqrt(H) || 1e-6, F = 1 / R, q = k * F, W = U * F, z = D * F, Y = o * q + h * W + m * z;
1244
+ if (Y <= 0) continue;
1245
+ const G = [q, W, z], L = (y.kind === "point" || y.kind === "spot") && y.range > 0 ? Math.min(R, y.range) : R;
1246
+ if ((y.kind === "point" || y.kind === "spot") && y.range > 0 && R >= y.range)
1247
+ continue;
1248
+ let V = 0;
1249
+ if (y.kind === "point")
1250
+ V = De(R, y.range, jt);
1251
+ else if (y.kind === "spot") {
1252
+ const _ = Pn(
1253
+ G,
1254
+ y.direction,
1255
+ y.innerCos,
1256
+ y.outerCos
1257
+ );
1258
+ if (_ <= 0) continue;
1259
+ V = De(R, y.range, jt) * _;
1260
+ } else {
1261
+ const _ = y.normal[0] * -q + y.normal[1] * -W + y.normal[2] * -z;
1262
+ if (_ <= 0) continue;
1263
+ const C = de(
1264
+ y,
1265
+ x,
1266
+ G,
1267
+ Math.max(0, L - f * 2),
1268
+ b,
1269
+ { samples: g, jitterSeed: O }
1270
+ );
1271
+ if (C <= 0) continue;
1272
+ const S = y.intensity * (y.area * _ * Y / Math.max(H, jt * jt)) * C;
1273
+ i += y.color[0] * S, r += y.color[1] * S, l += y.color[2] * S;
1274
+ continue;
1275
+ }
1276
+ if (V <= 0) continue;
1277
+ const j = de(
1278
+ y,
1279
+ x,
1280
+ G,
1281
+ Math.max(0, L - f * 2),
1282
+ b,
1283
+ {
1284
+ samples: g,
1285
+ pointLightRadius: v,
1286
+ jitterSeed: O
1287
+ }
1288
+ );
1289
+ if (j <= 0) continue;
1290
+ const X = y.intensity * Y * V * j;
1291
+ i += y.color[0] * X, r += y.color[1] * X, l += y.color[2] * X;
1292
+ }
1293
+ const M = (s == null ? void 0 : s.ao) ?? 1;
1294
+ return [i * M, r * M, l * M];
1295
+ }
1296
+ const Ge = [1 / 16, 4 / 16, 6 / 16, 4 / 16, 1 / 16];
1297
+ function qt(t, a, e) {
1298
+ const n = Math.max(0, Math.floor(e));
1299
+ if (n < 1) return;
1300
+ const s = new Float32Array(t.length);
1301
+ for (let i = 0; i < n; i++) {
1302
+ s.set(t);
1303
+ for (let r = 0; r < a; r++)
1304
+ for (let l = 0; l < a; l++) {
1305
+ const o = (r * a + l) * 4;
1306
+ if ((s[o + 3] ?? 0) > 0) continue;
1307
+ let h = 0, m = 0, f = 0, u = 0;
1308
+ for (let c = -1; c <= 1; c++) {
1309
+ const d = r + c;
1310
+ if (!(d < 0 || d >= a))
1311
+ for (let x = -1; x <= 1; x++) {
1312
+ if (x === 0 && c === 0) continue;
1313
+ const b = l + x;
1314
+ if (b < 0 || b >= a) continue;
1315
+ const g = (d * a + b) * 4;
1316
+ if ((s[g + 3] ?? 0) <= 0) continue;
1317
+ const v = x === 0 || c === 0 ? 1 : 0.7071;
1318
+ h += s[g] * v, m += s[g + 1] * v, f += s[g + 2] * v, u += v;
1319
+ }
1320
+ }
1321
+ u > 0 && (t[o] = h / u, t[o + 1] = m / u, t[o + 2] = f / u, t[o + 3] = 1);
1322
+ }
1323
+ }
1324
+ }
1325
+ function Ft(t, a, e) {
1326
+ const n = e == null ? void 0 : e.gbuffer, s = (e == null ? void 0 : e.sigmaSpatial) ?? 1.15, i = (e == null ? void 0 : e.sigmaRange) ?? 0.85, r = Math.max(1e-4, (e == null ? void 0 : e.planeSigma) ?? 0.08), l = 1 / (2 * s * s), o = 1 / (2 * i * i), h = 1 / (2 * r * r), m = (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]);
1327
+ if (m.length === 0) return;
1328
+ const f = [];
1329
+ for (let d = 0; d < a; d++)
1330
+ for (let x = 0; x < a; x++)
1331
+ (t[(d * a + x) * 4 + 3] ?? 0) > 0 && f.push(x, d);
1332
+ if (f.length === 0) return;
1333
+ const u = new Float32Array(t.length), c = new Float32Array(8);
1334
+ for (let d = 0; d < m.length; d++) {
1335
+ const x = Math.max(1, m[d]), b = (e == null ? void 0 : e.wavelet5x5) ?? x > 1, g = b ? 2 : 1;
1336
+ u.set(t);
1337
+ for (let v = 0; v < f.length; v += 2) {
1338
+ const A = f[v], M = f[v + 1], p = (M * a + A) * 4;
1339
+ let y = u[p], O = u[p + 1], k = u[p + 2];
1340
+ const U = !!(n && (n.normal[p + 3] ?? 0) > 0), D = U ? n.position[p] : 0, H = U ? n.position[p + 1] : 0, R = U ? n.position[p + 2] : 0, F = U ? n.normal[p] : 0, q = U ? n.normal[p + 1] : 1, W = U ? n.normal[p + 2] : 0;
1341
+ if (d === 0) {
1342
+ let V = 0, j = 0, X = 0, _ = 0;
1343
+ for (let C = -1; C <= 1; C++) {
1344
+ const S = M + C;
1345
+ if (!(S < 0 || S >= a))
1346
+ for (let I = -1; I <= 1; I++) {
1347
+ if (I === 0 && C === 0) continue;
1348
+ const T = A + I;
1349
+ if (T < 0 || T >= a) continue;
1350
+ const P = (S * a + T) * 4;
1351
+ if ((u[P + 3] ?? 0) <= 0) continue;
1352
+ if (U && (n.normal[P + 3] ?? 0) > 0) {
1353
+ if (F * n.normal[P] + q * n.normal[P + 1] + W * n.normal[P + 2] < 0.8) continue;
1354
+ const Z = n.position[P] - D, J = n.position[P + 1] - H, et = n.position[P + 2] - R;
1355
+ if (Math.abs(F * Z + q * J + W * et) > r * 2) continue;
1356
+ }
1357
+ const w = u[P], B = u[P + 1], N = u[P + 2];
1358
+ c[V++] = (w + B + N) / 3, j += w, X += B, _ += N;
1359
+ }
1360
+ }
1361
+ if (V >= 3) {
1362
+ const C = c.subarray(0, V);
1363
+ C.sort();
1364
+ const S = C[V >> 1], I = (y + O + k) / 3;
1365
+ if (Math.abs(I - S) > 0.35)
1366
+ if (I > 1e-4) {
1367
+ const T = S / I;
1368
+ y *= T, O *= T, k *= T;
1369
+ } else {
1370
+ const T = Math.max((j + X + _) / (3 * V), 1e-4), P = S / T;
1371
+ y = j / V * P, O = X / V * P, k = _ / V * P;
1372
+ }
1373
+ }
1374
+ }
1375
+ let z = 0, Y = 0, G = 0, L = 0;
1376
+ for (let V = -g; V <= g; V++) {
1377
+ const j = M + V * x;
1378
+ if (j < 0 || j >= a) continue;
1379
+ const X = b ? Ge[V + 2] : 1;
1380
+ for (let _ = -g; _ <= g; _++) {
1381
+ const C = A + _ * x;
1382
+ if (C < 0 || C >= a) continue;
1383
+ const S = (j * a + C) * 4;
1384
+ if ((u[S + 3] ?? 0) <= 0) continue;
1385
+ let I = 1;
1386
+ if (U && (_ !== 0 || V !== 0)) {
1387
+ if ((n.normal[S + 3] ?? 0) <= 0) continue;
1388
+ const rt = F * n.normal[S] + q * n.normal[S + 1] + W * n.normal[S + 2];
1389
+ if (rt <= 0.72) continue;
1390
+ const $ = n.position[S] - D, Q = n.position[S + 1] - H, K = n.position[S + 2] - R, nt = F * $ + q * Q + W * K, ct = $ * $ + Q * Q + K * K, it = 0.55 * x * g;
1391
+ if (ct > it * it) continue;
1392
+ const st = Math.pow(rt, 16), at = Math.exp(-nt * nt * h);
1393
+ if (I = st * at, I < 1e-4) continue;
1394
+ }
1395
+ const T = _ === 0 && V === 0 ? y : u[S], P = _ === 0 && V === 0 ? O : u[S + 1], w = _ === 0 && V === 0 ? k : u[S + 2], B = T - y, N = P - O, E = w - k, Z = B * B + N * N + E * E, J = b ? Ge[_ + 2] * X : Math.exp(-(_ * _ + V * V) * l), et = I * J * Math.exp(-Z * o);
1396
+ z += T * et, Y += P * et, G += w * et, L += et;
1397
+ }
1398
+ }
1399
+ L > 1e-8 && (t[p] = z / L, t[p + 1] = Y / L, t[p + 2] = G / L);
1400
+ }
1401
+ }
1402
+ }
1403
+ function Fe(t, a, e) {
1404
+ let n = Math.max(0, t), s = Math.max(0, a), i = Math.max(0, e);
1405
+ const r = n + s + i;
1406
+ if (r <= 1e-12) return [1 / 3, 1 / 3, 1 / 3];
1407
+ const l = 1 / r;
1408
+ return n *= l, s *= l, i *= l, [n, s, i];
1409
+ }
1410
+ function yr(t, a) {
1411
+ const e = new Float32Array(a * a * 4), n = new Float32Array(a * a * 4), s = new Float32Array(a * a * 4);
1412
+ let i = 0;
1413
+ for (const r of t)
1414
+ i += qe(r, e, n, s, a, !1);
1415
+ for (const r of t)
1416
+ i += qe(r, e, n, s, a, !0);
1417
+ return { atlasSize: a, position: e, normal: n, albedo: s, coveredTexels: i };
1418
+ }
1419
+ function qe(t, a, e, n, s, i) {
1420
+ const r = t.mesh, l = _t, o = r.vertices, h = r.indices, m = 1 / s, f = 0.55 * m, { positions: u, normals: c, albedos: d } = t;
1421
+ let x = 0;
1422
+ for (let b = 0; b + 2 < h.length; b += 3) {
1423
+ const g = h[b], v = h[b + 1], A = h[b + 2], M = o[g * l + tt.uv1], p = o[g * l + tt.uv1 + 1], y = o[v * l + tt.uv1], O = o[v * l + tt.uv1 + 1], k = o[A * l + tt.uv1], U = o[A * l + tt.uv1 + 1], D = Math.min(M, y, k), H = Math.max(M, y, k), R = Math.min(p, O, U), F = Math.max(p, O, U);
1424
+ if (H - D < 1e-8 && F - R < 1e-8) continue;
1425
+ const q = Math.max(0, Math.floor(D * s)), W = Math.max(0, Math.floor(R * s)), z = Math.min(s - 1, Math.ceil(H * s)), Y = Math.min(s - 1, Math.ceil(F * s)), G = Zt(M, p, y, O, k, U);
1426
+ if (Math.abs(G) < 1e-12) continue;
1427
+ const L = 1 / G;
1428
+ for (let V = W; V <= Y; V++) {
1429
+ const j = (V + 0.5) * m;
1430
+ for (let X = q; X <= z; X++) {
1431
+ const _ = (V * s + X) * 4;
1432
+ if (i && (a[_ + 3] ?? 0) > 0) continue;
1433
+ const C = (X + 0.5) * m;
1434
+ let S = Zt(y, O, k, U, C, j) * L, I = Zt(k, U, M, p, C, j) * L, T = Zt(M, p, y, O, C, j) * L;
1435
+ if (i) {
1436
+ const [E, Z, J] = Fe(S, I, T), et = E * M + Z * y + J * k, rt = E * p + Z * O + J * U;
1437
+ if (Math.abs(et - C) > f || Math.abs(rt - j) > f)
1438
+ continue;
1439
+ S = E, I = Z, T = J;
1440
+ } else {
1441
+ if (S < -1e-4 || I < -1e-4 || T < -1e-4) continue;
1442
+ [S, I, T] = Fe(S, I, T);
1443
+ }
1444
+ (a[_ + 3] ?? 0) <= 0 && x++, a[_] = u[g * 3] * S + u[v * 3] * I + u[A * 3] * T, a[_ + 1] = u[g * 3 + 1] * S + u[v * 3 + 1] * I + u[A * 3 + 1] * T, a[_ + 2] = u[g * 3 + 2] * S + u[v * 3 + 2] * I + u[A * 3 + 2] * T, a[_ + 3] = 1;
1445
+ const P = c[g * 3] * S + c[v * 3] * I + c[A * 3] * T, w = c[g * 3 + 1] * S + c[v * 3 + 1] * I + c[A * 3 + 1] * T, B = c[g * 3 + 2] * S + c[v * 3 + 2] * I + c[A * 3 + 2] * T, N = Math.hypot(P, w, B) || 1;
1446
+ e[_] = P / N, e[_ + 1] = w / N, e[_ + 2] = B / N, e[_ + 3] = 1, d ? (n[_] = d[g * 3] * S + d[v * 3] * I + d[A * 3] * T, n[_ + 1] = d[g * 3 + 1] * S + d[v * 3 + 1] * I + d[A * 3 + 1] * T, n[_ + 2] = d[g * 3 + 2] * S + d[v * 3 + 2] * I + d[A * 3 + 2] * T, n[_ + 3] = 1) : (n[_] = 0.65, n[_ + 1] = 0.65, n[_ + 2] = 0.65, n[_ + 3] = 1);
1447
+ }
1448
+ }
1449
+ }
1450
+ return x;
1451
+ }
1452
+ function Zt(t, a, e, n, s, i) {
1453
+ return (s - t) * (n - a) - (i - a) * (e - t);
1454
+ }
1455
+ const Qt = 28;
1456
+ function Pt() {
1457
+ return typeof performance < "u" ? performance.now() : Date.now();
1458
+ }
1459
+ function Jt() {
1460
+ return new Promise((t) => {
1461
+ setTimeout(t, 0);
1462
+ });
1463
+ }
1464
+ function pt(t) {
1465
+ if (t != null && t.aborted)
1466
+ throw new DOMException("Bake aborted", "AbortError");
1467
+ }
1468
+ async function vr(t, a, e, n, s, i, r) {
1469
+ var V, j, X, _, C, S, I, T;
1470
+ const l = (r == null ? void 0 : r.aoSamples) ?? 6, o = (r == null ? void 0 : r.texelAoSamples) ?? (l > 0 ? Math.min(10, Math.max(4, Math.ceil(l))) : 0), h = (r == null ? void 0 : r.aoRadius) ?? 0.45, m = (r == null ? void 0 : r.shadows) !== !1, f = (r == null ? void 0 : r.softShadowSamples) ?? 8, u = ((r == null ? void 0 : r.yieldEveryCharts) ?? 2) > 0, c = Math.max(0, (r == null ? void 0 : r.bounces) ?? Ke), d = Math.max(4, (r == null ? void 0 : r.bounceSamples) ?? 16), x = (r == null ? void 0 : r.denoise) !== !1, b = (r == null ? void 0 : r.rasterMode) ?? "full", g = r == null ? void 0 : r.skyRadiance;
1471
+ pt(r == null ? void 0 : r.signal);
1472
+ const v = ir(t, a, r == null ? void 0 : r.accelOptions), A = !!(v && v.emissiveCatalog.count > 0), M = A ? s.filter((P) => P.kind !== "emissive") : s, p = m && v ? (P, w, B) => Ae(v, P, w, B) : void 0, y = [], O = ot.create(), k = ot.create();
1473
+ for (const P of a) {
1474
+ pt(r == null ? void 0 : r.signal);
1475
+ const w = t.getComponent(P.entity, St), B = (w == null ? void 0 : w.worldTransform) ?? ie.create(), N = Re.create();
1476
+ Re.normalFromMat4(N, B);
1477
+ const E = P.mesh, Z = _t, J = E.vertices, et = Math.floor(J.length / Z), rt = new Float32Array(et * 3), $ = new Float32Array(et * 3), Q = ar(t, P.entity, E, r == null ? void 0 : r.accelOptions);
1478
+ for (let K = 0; K < et; K++) {
1479
+ const nt = K * Z;
1480
+ ot.set(O, J[nt], J[nt + 1], J[nt + 2]), ot.transformMat4(O, O, B), rt[K * 3] = O[0], rt[K * 3 + 1] = O[1], rt[K * 3 + 2] = O[2], ot.set(
1481
+ k,
1482
+ J[nt + tt.normal],
1483
+ J[nt + tt.normal + 1],
1484
+ J[nt + tt.normal + 2]
1485
+ ), ot.transformMat3(k, k, N), ot.normalize(k, k), $[K * 3] = k[0], $[K * 3 + 1] = k[1], $[K * 3 + 2] = k[2];
1486
+ }
1487
+ y.push({ mesh: E, positions: rt, normals: $, albedos: Q });
1488
+ }
1489
+ const U = yr(y, n), D = n * n, H = [];
1490
+ for (let P = 0; P < D; P++)
1491
+ (U.position[P * 4 + 3] ?? 0) > 0 && H.push(P);
1492
+ const R = new Uint32Array(H), F = R.length, q = Math.max(1, 1 + c);
1493
+ if (F === 0)
1494
+ return (V = r == null ? void 0 : r.onProgress) == null || V.call(r, q, q), { gbufferCharts: y, gbuffer: U, accel: v };
1495
+ let W = Pt();
1496
+ const z = new Float32Array(D * 4), Y = new Float32Array(D * 4);
1497
+ let G = !1;
1498
+ if (r != null && r.gpuDirectRunner)
1499
+ try {
1500
+ const P = await r.gpuDirectRunner(U, m ? v : null);
1501
+ if (P.length === D * 4) {
1502
+ for (let w = 0; w < F; w++) {
1503
+ const B = R[w] * 4, N = o > 0 ? Math.min(1, Math.max(0.25, P[B + 3])) : 1;
1504
+ z[B] = P[B], z[B + 1] = P[B + 1], z[B + 2] = P[B + 2], z[B + 3] = 1, Y[B] = N, Y[B + 1] = N, Y[B + 2] = N, Y[B + 3] = 1;
1505
+ }
1506
+ G = !0;
1507
+ }
1508
+ } catch (P) {
1509
+ if (P instanceof DOMException && P.name === "AbortError") throw P;
1510
+ G = !1;
1511
+ }
1512
+ if (!G)
1513
+ for (let P = 0; P < F; P++) {
1514
+ !(P & 511) && u && Pt() - W >= Qt && (pt(r == null ? void 0 : r.signal), (j = r == null ? void 0 : r.onProgress) == null || j.call(r, P / F, q), await Jt(), W = Pt(), pt(r == null ? void 0 : r.signal));
1515
+ const w = R[P], B = w % n, N = w / n | 0, E = w * 4, Z = [
1516
+ U.position[E],
1517
+ U.position[E + 1],
1518
+ U.position[E + 2]
1519
+ ], J = [
1520
+ U.normal[E],
1521
+ U.normal[E + 1],
1522
+ U.normal[E + 2]
1523
+ ], et = Xt(B, N, 1), rt = v && o > 0 ? lr(v, Z, J, Math.max(6, o), h, et) : 1;
1524
+ Y[E] = rt, Y[E + 1] = rt, Y[E + 2] = rt, Y[E + 3] = 1;
1525
+ const $ = un(M, Z, J, i, {
1526
+ occluded: p,
1527
+ ao: 1,
1528
+ softShadowSamples: f,
1529
+ jitterSeed: et
1530
+ });
1531
+ let Q = $[0], K = $[1], nt = $[2];
1532
+ if (A && v) {
1533
+ const ct = cr(
1534
+ v,
1535
+ Z,
1536
+ J,
1537
+ Math.max(4, f),
1538
+ m,
1539
+ et
1540
+ );
1541
+ Q += ct[0], K += ct[1], nt += ct[2];
1542
+ }
1543
+ z[E] = Q, z[E + 1] = K, z[E + 2] = nt, z[E + 3] = 1;
1544
+ }
1545
+ x && (Ft(z, n, {
1546
+ gbuffer: U,
1547
+ stepSizes: [1],
1548
+ wavelet5x5: !0,
1549
+ sigmaRange: 1.25,
1550
+ planeSigma: 0.045
1551
+ }), Ft(Y, n, {
1552
+ gbuffer: U,
1553
+ stepSizes: [1, 2],
1554
+ wavelet5x5: !0,
1555
+ sigmaRange: 3,
1556
+ planeSigma: 0.05
1557
+ })), (X = r == null ? void 0 : r.onProgress) == null || X.call(r, 1, q);
1558
+ let L = new Float32Array(D * 4);
1559
+ if (c > 0 && v) {
1560
+ let P = !1;
1561
+ if (r != null && r.gpuBounceRunner)
1562
+ try {
1563
+ const w = await r.gpuBounceRunner(U, v, z, R);
1564
+ w.length === D * 4 && (L = w, P = !0);
1565
+ } catch (w) {
1566
+ if (w instanceof DOMException && w.name === "AbortError") throw w;
1567
+ P = !1;
1568
+ }
1569
+ if (!P) {
1570
+ const w = new Float32Array(D * 4);
1571
+ for (let $ = 0; $ < F; $++) {
1572
+ const Q = R[$] * 4;
1573
+ w[Q] = z[Q], w[Q + 1] = z[Q + 1], w[Q + 2] = z[Q + 2], w[Q + 3] = 1;
1574
+ }
1575
+ qt(w, n, 2), re(v, w, n);
1576
+ const B = new Float32Array(D * 4), N = Math.max(16, d), E = F > 75e4 ? 3 : F > 3e4 ? 2 : 1, Z = Math.ceil(n / E), J = E > 1 ? new Float32Array(Z * Z * 3) : null, et = E > 1 ? new Int32Array(Z * Z) : null, rt = [1, 2, 4, 8];
1577
+ for (let $ = 0; $ < c; $++) {
1578
+ if (pt(r == null ? void 0 : r.signal), B.fill(0), E === 1)
1579
+ for (let Q = 0; Q < F; Q++) {
1580
+ !(Q & 127) && u && Pt() - W >= Qt && (pt(r == null ? void 0 : r.signal), (_ = r == null ? void 0 : r.onProgress) == null || _.call(r, 1 + $ + Q / F, q), await Jt(), W = Pt(), pt(r == null ? void 0 : r.signal));
1581
+ const K = R[Q], nt = K % n, ct = K / n | 0, it = K * 4, st = [
1582
+ U.position[it],
1583
+ U.position[it + 1],
1584
+ U.position[it + 2]
1585
+ ], at = [
1586
+ U.normal[it],
1587
+ U.normal[it + 1],
1588
+ U.normal[it + 2]
1589
+ ], ut = Xt(nt, ct, 101 + $ * 37), lt = he(v, st, at, N, 25, {
1590
+ jitterSeed: ut,
1591
+ atlasRgba: w,
1592
+ atlasSize: n,
1593
+ skyRadiance: g
1594
+ });
1595
+ B[it] = lt[0], B[it + 1] = lt[1], B[it + 2] = lt[2], B[it + 3] = 1;
1596
+ }
1597
+ else {
1598
+ et.fill(-1);
1599
+ const Q = Z * Z;
1600
+ let K = 0;
1601
+ for (let nt = 0; nt < Z; nt++)
1602
+ for (let ct = 0; ct < Z; ct++) {
1603
+ K++, !(K & 127) && u && Pt() - W >= Qt && (pt(r == null ? void 0 : r.signal), (C = r == null ? void 0 : r.onProgress) == null || C.call(r, 1 + $ + K / Q * 0.85, q), await Jt(), W = Pt(), pt(r == null ? void 0 : r.signal));
1604
+ const it = ct * E, st = nt * E;
1605
+ let at = -1, ut = it, lt = st;
1606
+ t: for (let xt = 0; xt < E && st + xt < n; xt++)
1607
+ for (let ft = 0; ft < E && it + ft < n; ft++) {
1608
+ const Ut = (st + xt) * n + (it + ft);
1609
+ if ((U.position[Ut * 4 + 3] ?? 0) > 0) {
1610
+ at = Ut, ut = it + ft, lt = st + xt;
1611
+ break t;
1612
+ }
1613
+ }
1614
+ if (at < 0) continue;
1615
+ const gt = at * 4, bt = [
1616
+ U.position[gt],
1617
+ U.position[gt + 1],
1618
+ U.position[gt + 2]
1619
+ ], vt = [
1620
+ U.normal[gt],
1621
+ U.normal[gt + 1],
1622
+ U.normal[gt + 2]
1623
+ ], Mt = Xt(ut, lt, 101 + $ * 37), dt = he(v, bt, vt, N, 25, {
1624
+ jitterSeed: Mt,
1625
+ atlasRgba: w,
1626
+ atlasSize: n,
1627
+ skyRadiance: g
1628
+ }), wt = nt * Z + ct;
1629
+ et[wt] = at, J[wt * 3] = dt[0], J[wt * 3 + 1] = dt[1], J[wt * 3 + 2] = dt[2];
1630
+ }
1631
+ for (let nt = 0; nt < F; nt++) {
1632
+ !(nt & 511) && u && Pt() - W >= Qt && (pt(r == null ? void 0 : r.signal), (S = r == null ? void 0 : r.onProgress) == null || S.call(r, 1 + $ + 0.85 + nt / F * 0.15, q), await Jt(), W = Pt(), pt(r == null ? void 0 : r.signal));
1633
+ const ct = R[nt], it = ct % n, st = ct / n | 0, at = ct * 4, ut = U.position[at], lt = U.position[at + 1], gt = U.position[at + 2], bt = U.normal[at], vt = U.normal[at + 1], Mt = U.normal[at + 2], dt = (it + 0.5) / E - 0.5, wt = (st + 0.5) / E - 0.5, xt = Math.max(0, Math.min(Z - 1, Math.floor(dt))), ft = Math.max(0, Math.min(Z - 1, Math.floor(wt))), Ut = Math.min(Z - 1, xt + 1), Vt = Math.min(Z - 1, ft + 1), It = Math.max(0, Math.min(1, dt - xt)), Ue = Math.max(0, Math.min(1, wt - ft));
1634
+ let ce = 0, Pe = 0, Ie = 0, Te = 0;
1635
+ for (let At = 0; At < 2; At++) {
1636
+ const zt = At === 0 ? ft : Vt, gn = At === 0 ? 1 - Ue : Ue;
1637
+ for (let Ht = 0; Ht < 2; Ht++) {
1638
+ const xn = Ht === 0 ? xt : Ut, ke = (Ht === 0 ? 1 - It : It) * gn;
1639
+ if (ke <= 1e-5) continue;
1640
+ const Yt = zt * Z + xn, Be = et[Yt];
1641
+ if (Be < 0) continue;
1642
+ const Ct = Be * 4, Ce = bt * U.normal[Ct] + vt * U.normal[Ct + 1] + Mt * U.normal[Ct + 2];
1643
+ if (Ce < 0.78) continue;
1644
+ const ue = U.position[Ct] - ut, le = U.position[Ct + 1] - lt, fe = U.position[Ct + 2] - gt, Oe = Math.abs(bt * ue + vt * le + Mt * fe);
1645
+ if (Oe > 0.08 || ue * ue + le * le + fe * fe > 0.55) continue;
1646
+ const Wt = ke * Math.pow(Ce, 8) * Math.exp(-Oe / 0.05);
1647
+ ce += Wt, Pe += J[Yt * 3] * Wt, Ie += J[Yt * 3 + 1] * Wt, Te += J[Yt * 3 + 2] * Wt;
1648
+ }
1649
+ }
1650
+ if (ce > 1e-4) {
1651
+ const At = 1 / ce;
1652
+ B[at] = Pe * At, B[at + 1] = Ie * At, B[at + 2] = Te * At, B[at + 3] = 1;
1653
+ } else {
1654
+ const At = Xt(it, st, 101 + $ * 37), zt = he(
1655
+ v,
1656
+ [ut, lt, gt],
1657
+ [bt, vt, Mt],
1658
+ N,
1659
+ 25,
1660
+ {
1661
+ jitterSeed: At,
1662
+ atlasRgba: w,
1663
+ atlasSize: n,
1664
+ skyRadiance: g
1665
+ }
1666
+ );
1667
+ B[at] = zt[0], B[at + 1] = zt[1], B[at + 2] = zt[2], B[at + 3] = 1;
1668
+ }
1669
+ }
1670
+ }
1671
+ x && Ft(B, n, {
1672
+ gbuffer: U,
1673
+ stepSizes: rt,
1674
+ wavelet5x5: !0,
1675
+ sigmaRange: 14,
1676
+ planeSigma: 0.06
1677
+ });
1678
+ for (let Q = 0; Q < F; Q++) {
1679
+ const K = R[Q] * 4;
1680
+ L[K] = (L[K] ?? 0) + B[K], L[K + 1] = (L[K + 1] ?? 0) + B[K + 1], L[K + 2] = (L[K + 2] ?? 0) + B[K + 2], L[K + 3] = 1;
1681
+ }
1682
+ $ + 1 < c && (w.set(B), qt(w, n, 2), re(v, w, n)), (I = r == null ? void 0 : r.onProgress) == null || I.call(r, 2 + $, q);
1683
+ }
1684
+ }
1685
+ }
1686
+ for (let P = 0; P < F; P++) {
1687
+ const w = R[P] * 4, B = Y[w], N = 0.45 + 0.55 * B;
1688
+ b === "indirect-only" ? (e[w] = L[w] * N, e[w + 1] = L[w + 1] * N, e[w + 2] = L[w + 2] * N) : (e[w] = z[w] * B + L[w] * N, e[w + 1] = z[w + 1] * B + L[w + 1] * N, e[w + 2] = z[w + 2] * B + L[w + 2] * N), e[w + 3] = 1;
1689
+ }
1690
+ return (T = r == null ? void 0 : r.onProgress) == null || T.call(r, q, q), { gbufferCharts: y, gbuffer: U, accel: v };
1691
+ }
1692
+ const ln = 384, Mr = 12 + ln * 9 * 4, fn = 0.2820947918, xe = 0.4886025119, be = 1.0925484306, pr = 0.3153915653, _r = 0.5462742153, wr = Math.PI, ye = 2 * Math.PI / 3, Gt = Math.PI / 4, Ve = 64, Ar = Math.PI * (3 - Math.sqrt(5)), He = (() => {
1693
+ const t = [];
1694
+ for (let a = 0; a < Ve; a++) {
1695
+ const e = 1 - 2 * (a + 0.5) / Ve, n = Math.sqrt(Math.max(0, 1 - e * e)), s = a * Ar;
1696
+ t.push([Math.cos(s) * n, e, Math.sin(s) * n]);
1697
+ }
1698
+ return t;
1699
+ })();
1700
+ function Me(t) {
1701
+ const a = t[0], e = t[1], n = t[2];
1702
+ return [
1703
+ fn,
1704
+ -xe * e,
1705
+ xe * n,
1706
+ -xe * a,
1707
+ be * a * e,
1708
+ -be * e * n,
1709
+ pr * (3 * n * n - 1),
1710
+ -be * a * n,
1711
+ _r * (a * a - e * e)
1712
+ ];
1713
+ }
1714
+ function Sr(t, a, e) {
1715
+ const n = Me(e), s = [
1716
+ wr * n[0],
1717
+ ye * n[1],
1718
+ ye * n[2],
1719
+ ye * n[3],
1720
+ Gt * n[4],
1721
+ Gt * n[5],
1722
+ Gt * n[6],
1723
+ Gt * n[7],
1724
+ Gt * n[8]
1725
+ ];
1726
+ let i = 0, r = 0, l = 0;
1727
+ for (let o = 0; o < 9; o++) {
1728
+ const h = s[o], m = a + o * 3;
1729
+ i += (t[m] ?? 0) * h, r += (t[m + 1] ?? 0) * h, l += (t[m + 2] ?? 0) * h;
1730
+ }
1731
+ return [Math.max(0, i), Math.max(0, r), Math.max(0, l)];
1732
+ }
1733
+ function Ur(t) {
1734
+ const a = atob(t.coefficientsBase64), e = new Uint8Array(a.length);
1735
+ for (let n = 0; n < a.length; n++)
1736
+ e[n] = a.charCodeAt(n);
1737
+ return new Float32Array(e.buffer, e.byteOffset, Math.floor(e.byteLength / 4));
1738
+ }
1739
+ function Vr(t, a, e, n) {
1740
+ const [s, i, r] = t.resolution, [l, o, h] = t.boundsMin, [m, f, u] = t.boundsMax, c = s > 1 ? Math.max(0, Math.min(1, (e[0] - l) / Math.max(1e-5, m - l))) * (s - 1) : 0, d = i > 1 ? Math.max(0, Math.min(1, (e[1] - o) / Math.max(1e-5, f - o))) * (i - 1) : 0, x = r > 1 ? Math.max(0, Math.min(1, (e[2] - h) / Math.max(1e-5, u - h))) * (r - 1) : 0, b = Math.min(s - 1, Math.floor(c)), g = Math.min(i - 1, Math.floor(d)), v = Math.min(r - 1, Math.floor(x)), A = Math.min(s - 1, b + 1), M = Math.min(i - 1, g + 1), p = Math.min(r - 1, v + 1), y = c - b, O = d - g, k = x - v;
1741
+ let U = 0, D = 0, H = 0;
1742
+ for (let R = 0; R < 2; R++) {
1743
+ const F = R === 0 ? v : p, q = R === 0 ? 1 - k : k;
1744
+ for (let W = 0; W < 2; W++) {
1745
+ const z = W === 0 ? g : M, Y = (W === 0 ? 1 - O : O) * q;
1746
+ for (let G = 0; G < 2; G++) {
1747
+ const L = G === 0 ? b : A, V = (G === 0 ? 1 - y : y) * Y;
1748
+ if (V <= 1e-6) continue;
1749
+ const j = (F * i + z) * s + L, X = Sr(a, j * 27, n);
1750
+ U += X[0] * V, D += X[1] * V, H += X[2] * V;
1751
+ }
1752
+ }
1753
+ }
1754
+ return [U, D, H];
1755
+ }
1756
+ function Pr(t, a = 1) {
1757
+ const e = new ArrayBuffer(Mr * 4), n = new Float32Array(e), s = new Uint32Array(e);
1758
+ if (!t || a <= 0)
1759
+ return n;
1760
+ const [i, r, l] = t.resolution, o = Math.min(ln, i * r * l);
1761
+ if (o <= 0) return n;
1762
+ const h = Ur(t);
1763
+ 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] = a, s[8] = i, s[9] = r, s[10] = l, s[11] = o;
1764
+ for (let m = 0; m < o; m++) {
1765
+ const f = m * 27, u = 12 + m * 36;
1766
+ for (let c = 0; c < 9; c++)
1767
+ 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;
1768
+ }
1769
+ return n;
1770
+ }
1771
+ function Ir(t) {
1772
+ let a = 1 / 0, e = 1 / 0, n = 1 / 0, s = -1 / 0, i = -1 / 0, r = -1 / 0, l = !1;
1773
+ for (const [c, [d]] of t.query([kt]).iter()) {
1774
+ if (!t.hasComponent(c, ne) || t.hasComponent(c, _e) || t.hasComponent(c, ae)) continue;
1775
+ const x = d == null ? void 0 : d.mesh;
1776
+ if (!x || typeof x == "string") continue;
1777
+ const b = x.boundingSphere.center, g = t.getComponent(c, St);
1778
+ let v = b[0], A = b[1], M = b[2], p = x.boundingSphere.radius;
1779
+ if (g) {
1780
+ const y = g.worldTransform;
1781
+ v = (y[12] ?? 0) + (y[0] ?? 1) * b[0] + (y[4] ?? 0) * b[1] + (y[8] ?? 0) * b[2], A = (y[13] ?? 0) + (y[1] ?? 0) * b[0] + (y[5] ?? 1) * b[1] + (y[9] ?? 0) * b[2], M = (y[14] ?? 0) + (y[2] ?? 0) * b[0] + (y[6] ?? 0) * b[1] + (y[10] ?? 1) * b[2];
1782
+ const O = Math.hypot(y[0] ?? 1, y[1] ?? 0, y[2] ?? 0), k = Math.hypot(y[4] ?? 0, y[5] ?? 1, y[6] ?? 0), U = Math.hypot(y[8] ?? 0, y[9] ?? 0, y[10] ?? 1);
1783
+ p *= Math.max(O, k, U, 1e-4);
1784
+ }
1785
+ a = Math.min(a, v - p), e = Math.min(e, A - p), n = Math.min(n, M - p), s = Math.max(s, v + p), i = Math.max(i, A + p), r = Math.max(r, M + p), l = !0;
1786
+ }
1787
+ if (!l)
1788
+ return {
1789
+ boundsMin: [-1, 0, -1],
1790
+ boundsMax: [1, 2, 1],
1791
+ resolution: [3, 3, 3]
1792
+ };
1793
+ const o = 0.25;
1794
+ a += o, e += o, n += o, s -= o, i -= o, r -= o, s <= a && (s = a + 1), i <= e && (i = e + 1), r <= n && (r = n + 1);
1795
+ const h = 1, m = Math.max(2, Math.min(8, Math.ceil((s - a) / h) + 1)), f = Math.max(2, Math.min(6, Math.ceil((i - e) / h) + 1)), u = Math.max(2, Math.min(8, Math.ceil((r - n) / h) + 1));
1796
+ return {
1797
+ boundsMin: [a, e, n],
1798
+ boundsMax: [s, i, r],
1799
+ resolution: [m, f, u]
1800
+ };
1801
+ }
1802
+ function hn(t, a, e, n, s, i, r, l) {
1803
+ if (!n)
1804
+ return [
1805
+ r ? r[0] + e[0] : e[0],
1806
+ r ? r[1] + e[1] : e[1],
1807
+ r ? r[2] + e[2] : e[2]
1808
+ ];
1809
+ const o = Se(n, t, a, 40, l);
1810
+ if (!o)
1811
+ return [
1812
+ ((r == null ? void 0 : r[0]) ?? 0) + e[0],
1813
+ ((r == null ? void 0 : r[1]) ?? 0) + e[1],
1814
+ ((r == null ? void 0 : r[2]) ?? 0) + e[2]
1815
+ ];
1816
+ const h = o.triIndex * 3, m = n.albedo[h], f = n.albedo[h + 1], u = n.albedo[h + 2], c = n.emissive[h], d = n.emissive[h + 1], x = n.emissive[h + 2];
1817
+ let b = n.bounceSrc[h], g = n.bounceSrc[h + 1], v = n.bounceSrc[h + 2];
1818
+ if (s && i > 0) {
1819
+ const M = o.triIndex * 6, p = 1 - o.baryU - o.baryV, y = p * n.triUv1[M] + o.baryU * n.triUv1[M + 2] + o.baryV * n.triUv1[M + 4], O = p * n.triUv1[M + 1] + o.baryU * n.triUv1[M + 3] + o.baryV * n.triUv1[M + 5], k = Math.min(i - 1, Math.max(0, Math.floor(y * i))), D = (Math.min(i - 1, Math.max(0, Math.floor(O * i))) * i + k) * 4;
1820
+ (s[D + 3] ?? 0) > 0 && (b = s[D], g = s[D + 1], v = s[D + 2]);
1821
+ }
1822
+ const A = 1 / Math.PI;
1823
+ return [m * b * A + c, f * g * A + d, u * v * A + x];
1824
+ }
1825
+ function Tr(t, a, e, n) {
1826
+ const s = typeof n == "function" ? { occluded: n } : n ?? {}, i = s.accel ?? null, r = s.occluded ?? (i ? (b, g, v) => Ae(i, b, g, v) : void 0), [l, o, h] = t.resolution, m = l * o * h, f = new Float32Array(m * 9 * 3);
1827
+ if (!!!(e && e.length > 0) && !i && !s.skyRadiance) {
1828
+ const b = 4 * fn;
1829
+ for (let g = 0; g < m; g++) {
1830
+ const v = g * 27;
1831
+ f[v] = a[0] * b, f[v + 1] = a[1] * b, f[v + 2] = a[2] * b;
1832
+ }
1833
+ } else {
1834
+ const [b, g, v] = t.boundsMin, [A, M, p] = t.boundsMax, y = 4 * Math.PI / He.length, O = [
1835
+ a[0] / Math.PI,
1836
+ a[1] / Math.PI,
1837
+ a[2] / Math.PI
1838
+ ], k = { t: 0, triIndex: -1, baryU: 0, baryV: 0, normal: [0, 1, 0] };
1839
+ let U = 0;
1840
+ for (let D = 0; D < h; D++) {
1841
+ const H = h > 1 ? D / (h - 1) : 0.5, R = v + (p - v) * H;
1842
+ for (let F = 0; F < o; F++) {
1843
+ const q = o > 1 ? F / (o - 1) : 0.5, W = g + (M - g) * q;
1844
+ for (let z = 0; z < l; z++) {
1845
+ const Y = l > 1 ? z / (l - 1) : 0.5, G = b + (A - b) * Y, L = [G, W, R], V = U * 27;
1846
+ U++;
1847
+ for (const j of He) {
1848
+ const X = hn(
1849
+ L,
1850
+ j,
1851
+ O,
1852
+ i,
1853
+ s.atlasRgba,
1854
+ s.atlasSize ?? 0,
1855
+ s.skyRadiance,
1856
+ k
1857
+ ), _ = Me(j);
1858
+ for (let C = 0; C < 9; C++) {
1859
+ const S = _[C] * y, I = V + C * 3;
1860
+ f[I] = (f[I] ?? 0) + X[0] * S, f[I + 1] = (f[I + 1] ?? 0) + X[1] * S, f[I + 2] = (f[I + 2] ?? 0) + X[2] * S;
1861
+ }
1862
+ }
1863
+ if (e)
1864
+ for (const j of e) {
1865
+ if (i && i.emissiveCatalog.count > 0 && j.kind === "emissive")
1866
+ continue;
1867
+ let X;
1868
+ if (j.kind === "directional")
1869
+ X = j.toLight;
1870
+ else {
1871
+ const S = j.position[0] - G, I = j.position[1] - W, T = j.position[2] - R, P = Math.hypot(S, I, T);
1872
+ if (P <= 1e-4) continue;
1873
+ X = [S / P, I / P, T / P];
1874
+ }
1875
+ const _ = un([j], L, X, [0, 0, 0], {
1876
+ occluded: r,
1877
+ softShadowSamples: 2
1878
+ });
1879
+ if (_[0] + _[1] + _[2] <= 1e-6) continue;
1880
+ const C = Me(X);
1881
+ for (let S = 0; S < 9; S++) {
1882
+ const I = C[S], T = V + S * 3;
1883
+ f[T] = (f[T] ?? 0) + _[0] * I, f[T + 1] = (f[T + 1] ?? 0) + _[1] * I, f[T + 2] = (f[T + 2] ?? 0) + _[2] * I;
1884
+ }
1885
+ }
1886
+ }
1887
+ }
1888
+ }
1889
+ }
1890
+ const c = new Uint8Array(f.buffer, f.byteOffset, f.byteLength);
1891
+ let d = "";
1892
+ for (let b = 0; b < c.length; b++)
1893
+ d += String.fromCharCode(c[b]);
1894
+ const x = btoa(d);
1895
+ return {
1896
+ boundsMin: t.boundsMin,
1897
+ boundsMax: t.boundsMax,
1898
+ resolution: t.resolution,
1899
+ coefficientsBase64: x
1900
+ };
1901
+ }
1902
+ function kr(t, a, e) {
1903
+ let n = 0, s = 0, i = 0;
1904
+ switch (t) {
1905
+ case 0:
1906
+ n = 1, s = -e, i = -a;
1907
+ break;
1908
+ case 1:
1909
+ n = -1, s = -e, i = a;
1910
+ break;
1911
+ case 2:
1912
+ n = a, s = 1, i = e;
1913
+ break;
1914
+ case 3:
1915
+ n = a, s = -1, i = -e;
1916
+ break;
1917
+ case 4:
1918
+ n = a, s = -e, i = 1;
1919
+ break;
1920
+ default:
1921
+ n = -a, s = -e, i = -1;
1922
+ break;
1923
+ }
1924
+ const r = Math.hypot(n, s, i) || 1;
1925
+ return [n / r, s / r, i / r];
1926
+ }
1927
+ function Br(t, a, e) {
1928
+ const n = [], s = Math.max(8, Math.min(64, (e == null ? void 0 : e.faceSize) ?? 32)), i = (e == null ? void 0 : e.accel) ?? null, r = (e == null ? void 0 : e.ambient) ?? [0.05, 0.05, 0.05], l = [
1929
+ r[0] / Math.PI,
1930
+ r[1] / Math.PI,
1931
+ r[2] / Math.PI
1932
+ ], o = ot.create(), h = { t: 0, triIndex: -1, baryU: 0, baryV: 0, normal: [0, 1, 0] };
1933
+ for (const [m, [f]] of t.query([In]).iter()) {
1934
+ if (!Dt(t, m)) continue;
1935
+ const u = t.getComponent(m, St);
1936
+ u ? ie.getTranslation(o, u.worldTransform) : ot.set(o, 0, 0, 0);
1937
+ const c = [o[0], o[1], o[2]], d = e != null && e.skyRadiance ? [e.skyRadiance[0], e.skyRadiance[1], e.skyRadiance[2]] : [f.skyColor[0], f.skyColor[1], f.skyColor[2]], x = [
1938
+ `lightmap_refl_probe_${m}_px`,
1939
+ `lightmap_refl_probe_${m}_nx`,
1940
+ `lightmap_refl_probe_${m}_py`,
1941
+ `lightmap_refl_probe_${m}_ny`,
1942
+ `lightmap_refl_probe_${m}_pz`,
1943
+ `lightmap_refl_probe_${m}_nz`
1944
+ ], b = ["", "", "", "", "", ""];
1945
+ for (let g = 0; g < 6; g++) {
1946
+ const v = new Float32Array(s * s * 4);
1947
+ for (let M = 0; M < s; M++) {
1948
+ const p = (M + 0.5) / s * 2 - 1;
1949
+ for (let y = 0; y < s; y++) {
1950
+ const O = (y + 0.5) / s * 2 - 1, k = kr(g, O, p), U = i ? hn(
1951
+ c,
1952
+ k,
1953
+ l,
1954
+ i,
1955
+ e == null ? void 0 : e.atlasRgba,
1956
+ (e == null ? void 0 : e.atlasSize) ?? 0,
1957
+ d,
1958
+ h
1959
+ ) : [
1960
+ f.interiorColor[0] * r[0] + d[0] * 0.25,
1961
+ f.interiorColor[1] * r[1] + d[1] * 0.25,
1962
+ f.interiorColor[2] * r[2] + d[2] * 0.25
1963
+ ], D = (M * s + y) * 4;
1964
+ v[D] = U[0], v[D + 1] = U[1], v[D + 2] = U[2], v[D + 3] = 1;
1965
+ }
1966
+ }
1967
+ const A = Xe(v, s, s);
1968
+ if (b[g] = Qe(A), e != null && e.device && a && typeof a.registerTexture == "function") {
1969
+ const M = e.device.createTexture({
1970
+ label: x[g],
1971
+ size: { width: s, height: s },
1972
+ format: "rgb9e5ufloat",
1973
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
1974
+ }), p = Uint8Array.from(
1975
+ new Uint8Array(A.buffer, A.byteOffset, A.byteLength)
1976
+ );
1977
+ e.device.queue.writeTexture(
1978
+ { texture: M },
1979
+ p,
1980
+ { bytesPerRow: s * 4 },
1981
+ { width: s, height: s }
1982
+ ), a.registerTexture(x[g], M);
1983
+ }
1984
+ }
1985
+ n.push({
1986
+ entityId: `e_${m}`,
1987
+ faceTextureIds: x,
1988
+ faceSize: s,
1989
+ facesBase64: b
1990
+ });
1991
+ }
1992
+ return n;
1993
+ }
1994
+ const Cr = (
1995
+ /* wgsl */
1996
+ `/**
1997
+ * @file bake_lightmap.compute.wgsl
1998
+ * @description GPU lightmap irradiance kernel (BIGI-7 / KPTL-5) — ordered SAH BVH traversal
1999
+ * (without iteration cap), Khronos \`KHR_lights_punctual\` attenuation, stratified Rect/Disk Area
2000
+ * Light NEE, power-CDF emissive triangle NEE, and emissive-safe smooth contact AO.
2001
+ *
2002
+ * Keep byte-for-byte in sync with \`src/bakeLightmapComputeWgsl.ts\`.
2003
+ */
2004
+
2005
+ struct BakeParams {
2006
+ atlasSize: u32,
2007
+ tileOriginX: u32,
2008
+ tileOriginY: u32,
2009
+ lightCount: u32,
2010
+ ambient: vec4<f32>,
2011
+ bvhNodeCount: u32,
2012
+ bvhRoot: u32,
2013
+ softShadowSamples: u32,
2014
+ aoSamples: u32,
2015
+ emissiveCount: u32,
2016
+ _pad0: u32,
2017
+ _pad1: u32,
2018
+ _pad2: u32,
2019
+ }
2020
+
2021
+ @group(0) @binding(0) var<uniform> params: BakeParams;
2022
+ @group(0) @binding(1) var gbufferPos: texture_2d<f32>;
2023
+ @group(0) @binding(2) var gbufferNormal: texture_2d<f32>;
2024
+ @group(0) @binding(3) var outputIrradiance: texture_storage_2d<rgba16float, write>;
2025
+ @group(0) @binding(4) var<storage, read> lights: array<vec4<f32>>;
2026
+ @group(0) @binding(5) var<storage, read> bvhNodes: array<vec4<f32>>;
2027
+ @group(0) @binding(6) var<storage, read> bvhTris: array<vec4<f32>>;
2028
+ @group(0) @binding(7) var<storage, read> emissiveTris: array<vec4<f32>>;
2029
+
2030
+ const PI: f32 = 3.14159265;
2031
+ const GOLDEN_RATIO: f32 = 0.61803399;
2032
+ const GOLDEN_ANGLE: f32 = 2.39996323;
2033
+ const KHR_MIN_DIST_SQ: f32 = 0.0025;
2034
+ const POINT_DISK_RADIUS: f32 = 0.16;
2035
+ const SUN_TAN: f32 = 0.028;
2036
+ const IGNORE_TRI_NONE: u32 = 0xffffffffu;
2037
+
2038
+ fn hash_texel(x: u32, y: u32, salt: u32) -> u32 {
2039
+ var h = (x * 0x27d4eb2du) ^ (y * 0x165667b1u) ^ (salt * 0x9e3779b9u);
2040
+ h = h ^ (h >> 15u);
2041
+ h = h * 0x2c1b3c6du;
2042
+ h = h ^ (h >> 12u);
2043
+ return h;
2044
+ }
2045
+
2046
+ fn seed_to_pair(seed: u32) -> vec2<f32> {
2047
+ var h = seed ^ (seed >> 16u);
2048
+ h = h * 0x7feb352du;
2049
+ h = h ^ (h >> 15u);
2050
+ h = h * 0x846ca68bu;
2051
+ h = h ^ (h >> 16u);
2052
+ return vec2<f32>(f32(h & 0xffffu) / 65536.0, f32((h >> 16u) & 0xffffu) / 65536.0);
2053
+ }
2054
+
2055
+ fn make_tangent(axis: vec3<f32>) -> vec3<f32> {
2056
+ let up = select(vec3<f32>(1.0, 0.0, 0.0), vec3<f32>(0.0, 1.0, 0.0), abs(axis.y) < 0.99);
2057
+ return normalize(cross(up, axis));
2058
+ }
2059
+
2060
+ fn ray_aabb_t(o: vec3<f32>, d: vec3<f32>, bmin: vec3<f32>, bmax: vec3<f32>, maxDist: f32) -> f32 {
2061
+ var tMin = 0.0;
2062
+ var tMax = maxDist;
2063
+ if (abs(d.x) < 1e-8) {
2064
+ if (o.x < bmin.x || o.x > bmax.x) { return -1.0; }
2065
+ } else {
2066
+ let inv = 1.0 / d.x;
2067
+ let t0 = (bmin.x - o.x) * inv;
2068
+ let t1 = (bmax.x - o.x) * inv;
2069
+ tMin = max(tMin, min(t0, t1));
2070
+ tMax = min(tMax, max(t0, t1));
2071
+ if (tMin > tMax) { return -1.0; }
2072
+ }
2073
+ if (abs(d.y) < 1e-8) {
2074
+ if (o.y < bmin.y || o.y > bmax.y) { return -1.0; }
2075
+ } else {
2076
+ let inv = 1.0 / d.y;
2077
+ let t0 = (bmin.y - o.y) * inv;
2078
+ let t1 = (bmax.y - o.y) * inv;
2079
+ tMin = max(tMin, min(t0, t1));
2080
+ tMax = min(tMax, max(t0, t1));
2081
+ if (tMin > tMax) { return -1.0; }
2082
+ }
2083
+ if (abs(d.z) < 1e-8) {
2084
+ if (o.z < bmin.z || o.z > bmax.z) { return -1.0; }
2085
+ } else {
2086
+ let inv = 1.0 / d.z;
2087
+ let t0 = (bmin.z - o.z) * inv;
2088
+ let t1 = (bmax.z - o.z) * inv;
2089
+ tMin = max(tMin, min(t0, t1));
2090
+ tMax = min(tMax, max(t0, t1));
2091
+ if (tMin > tMax) { return -1.0; }
2092
+ }
2093
+ return tMin;
2094
+ }
2095
+
2096
+ fn ray_tri(o: vec3<f32>, d: vec3<f32>, triIdx: u32, maxDist: f32) -> f32 {
2097
+ let base = triIdx * 3u;
2098
+ let v0 = bvhTris[base].xyz;
2099
+ let e1 = bvhTris[base + 1u].xyz;
2100
+ let e2 = bvhTris[base + 2u].xyz;
2101
+ let h = cross(d, e2);
2102
+ let det = dot(e1, h);
2103
+ if (abs(det) < 1e-8) { return -1.0; }
2104
+ let f = 1.0 / det;
2105
+ let s = o - v0;
2106
+ let u = f * dot(s, h);
2107
+ if (u < 0.0 || u > 1.0) { return -1.0; }
2108
+ let q = cross(s, e1);
2109
+ let v = f * dot(d, q);
2110
+ if (v < 0.0 || u + v > 1.0) { return -1.0; }
2111
+ let t = f * dot(e2, q);
2112
+ if (t > 1e-4 && t < maxDist) { return t; }
2113
+ return -1.0;
2114
+ }
2115
+
2116
+ fn bvh_occluded(o: vec3<f32>, d: vec3<f32>, maxDist: f32, ignoreTri: u32) -> bool {
2117
+ if (params.bvhNodeCount == 0u) { return false; }
2118
+ let rootBase = params.bvhRoot * 2u;
2119
+ if (ray_aabb_t(o, d, bvhNodes[rootBase].xyz, bvhNodes[rootBase + 1u].xyz, maxDist) < 0.0) {
2120
+ return false;
2121
+ }
2122
+ var stack: array<u32, 64>;
2123
+ var sp = 1u;
2124
+ stack[0] = params.bvhRoot;
2125
+ while (sp > 0u) {
2126
+ sp -= 1u;
2127
+ let ni = stack[sp];
2128
+ let nb = ni * 2u;
2129
+ let n0 = bvhNodes[nb];
2130
+ let n1 = bvhNodes[nb + 1u];
2131
+ let meta0 = bitcast<i32>(n0.w);
2132
+ let meta1 = bitcast<i32>(n1.w);
2133
+ if (meta0 < 0) {
2134
+ let triStart = u32(-1 - meta0);
2135
+ let triCount = u32(max(meta1, 0));
2136
+ for (var k = 0u; k < triCount; k += 1u) {
2137
+ let tid = triStart + k;
2138
+ if (tid == ignoreTri) { continue; }
2139
+ if (ray_tri(o, d, tid, maxDist) > 0.0) {
2140
+ return true;
2141
+ }
2142
+ }
2143
+ } else {
2144
+ let leftIdx = u32(meta0);
2145
+ let rightIdx = u32(meta1);
2146
+ let lb = leftIdx * 2u;
2147
+ let rb = rightIdx * 2u;
2148
+ let tL = ray_aabb_t(o, d, bvhNodes[lb].xyz, bvhNodes[lb + 1u].xyz, maxDist);
2149
+ let tR = ray_aabb_t(o, d, bvhNodes[rb].xyz, bvhNodes[rb + 1u].xyz, maxDist);
2150
+ if (tL >= 0.0 && tR >= 0.0) {
2151
+ if (sp < 62u) {
2152
+ if (tL <= tR) {
2153
+ stack[sp] = rightIdx;
2154
+ stack[sp + 1u] = leftIdx;
2155
+ } else {
2156
+ stack[sp] = leftIdx;
2157
+ stack[sp + 1u] = rightIdx;
2158
+ }
2159
+ sp += 2u;
2160
+ }
2161
+ } else if (tL >= 0.0 && sp < 63u) {
2162
+ stack[sp] = leftIdx;
2163
+ sp += 1u;
2164
+ } else if (tR >= 0.0 && sp < 63u) {
2165
+ stack[sp] = rightIdx;
2166
+ sp += 1u;
2167
+ }
2168
+ }
2169
+ }
2170
+ return false;
2171
+ }
2172
+
2173
+ fn bvh_closest_t_ao(o: vec3<f32>, d: vec3<f32>, maxDist: f32) -> f32 {
2174
+ if (params.bvhNodeCount == 0u) { return maxDist; }
2175
+ let rootBase = params.bvhRoot * 2u;
2176
+ if (ray_aabb_t(o, d, bvhNodes[rootBase].xyz, bvhNodes[rootBase + 1u].xyz, maxDist) < 0.0) {
2177
+ return maxDist;
2178
+ }
2179
+ var bestT = maxDist;
2180
+ var stack: array<u32, 64>;
2181
+ var sp = 1u;
2182
+ stack[0] = params.bvhRoot;
2183
+ while (sp > 0u) {
2184
+ sp -= 1u;
2185
+ let ni = stack[sp];
2186
+ let nb = ni * 2u;
2187
+ let n0 = bvhNodes[nb];
2188
+ let n1 = bvhNodes[nb + 1u];
2189
+ if (ray_aabb_t(o, d, n0.xyz, n1.xyz, bestT) < 0.0) {
2190
+ continue;
2191
+ }
2192
+ let meta0 = bitcast<i32>(n0.w);
2193
+ let meta1 = bitcast<i32>(n1.w);
2194
+ if (meta0 < 0) {
2195
+ let triStart = u32(-1 - meta0);
2196
+ let triCount = u32(max(meta1, 0));
2197
+ for (var k = 0u; k < triCount; k += 1u) {
2198
+ let tid = triStart + k;
2199
+ // Skip emissive surfaces so ceiling lamps never cast dark contact AO halos.
2200
+ if (bvhTris[tid * 3u].w > 0.5) { continue; }
2201
+ let t = ray_tri(o, d, tid, bestT);
2202
+ if (t > 0.0 && t < bestT) {
2203
+ bestT = t;
2204
+ }
2205
+ }
2206
+ } else {
2207
+ let leftIdx = u32(meta0);
2208
+ let rightIdx = u32(meta1);
2209
+ let lb = leftIdx * 2u;
2210
+ let rb = rightIdx * 2u;
2211
+ let tL = ray_aabb_t(o, d, bvhNodes[lb].xyz, bvhNodes[lb + 1u].xyz, bestT);
2212
+ let tR = ray_aabb_t(o, d, bvhNodes[rb].xyz, bvhNodes[rb + 1u].xyz, bestT);
2213
+ if (tL >= 0.0 && tR >= 0.0) {
2214
+ if (sp < 62u) {
2215
+ if (tL <= tR) {
2216
+ stack[sp] = rightIdx;
2217
+ stack[sp + 1u] = leftIdx;
2218
+ } else {
2219
+ stack[sp] = leftIdx;
2220
+ stack[sp + 1u] = rightIdx;
2221
+ }
2222
+ sp += 2u;
2223
+ }
2224
+ } else if (tL >= 0.0 && sp < 63u) {
2225
+ stack[sp] = leftIdx;
2226
+ sp += 1u;
2227
+ } else if (tR >= 0.0 && sp < 63u) {
2228
+ stack[sp] = rightIdx;
2229
+ sp += 1u;
2230
+ }
2231
+ }
2232
+ }
2233
+ return bestT;
2234
+ }
2235
+
2236
+ fn khr_punctual_attenuation(dist: f32, range: f32) -> f32 {
2237
+ let d2 = max(dist * dist, KHR_MIN_DIST_SQ);
2238
+ if (range <= 0.0) {
2239
+ return 1.0 / d2;
2240
+ }
2241
+ if (dist >= range) {
2242
+ return 0.0;
2243
+ }
2244
+ let r = dist / range;
2245
+ let r2 = r * r;
2246
+ let win = clamp(1.0 - r2 * r2, 0.0, 1.0);
2247
+ return (win * win) / d2;
2248
+ }
2249
+
2250
+ fn khr_spot_attenuation(cd: f32, innerCos: f32, outerCos: f32) -> f32 {
2251
+ if (cd <= outerCos) { return 0.0; }
2252
+ if (cd >= innerCos) { return 1.0; }
2253
+ let t = clamp((cd - outerCos) / max(innerCos - outerCos, 1e-5), 0.0, 1.0);
2254
+ return t * t;
2255
+ }
2256
+
2257
+ fn soft_dir_vis(origin: vec3<f32>, toL: vec3<f32>, seed: u32) -> f32 {
2258
+ if (params.bvhNodeCount == 0u) { return 1.0; }
2259
+ let nSmp = max(1u, params.softShadowSamples);
2260
+ if (nSmp == 1u) {
2261
+ return select(1.0, 0.0, bvh_occluded(origin, toL, 1e4, IGNORE_TRI_NONE));
2262
+ }
2263
+ let cp = seed_to_pair(seed);
2264
+ let t1 = make_tangent(toL);
2265
+ let t2 = cross(toL, t1);
2266
+ let rotAngle = cp.y * 2.0 * PI;
2267
+ var vis = 0.0;
2268
+ for (var i = 0u; i < nSmp; i += 1u) {
2269
+ let rad = sqrt(min(0.9999, (f32(i) + cp.x) / f32(nSmp))) * SUN_TAN;
2270
+ let ang = f32(i) * GOLDEN_ANGLE + rotAngle;
2271
+ let dir = normalize(toL + t1 * (cos(ang) * rad) + t2 * (sin(ang) * rad));
2272
+ if (!bvh_occluded(origin, dir, 1e4, IGNORE_TRI_NONE)) {
2273
+ vis += 1.0;
2274
+ }
2275
+ }
2276
+ return vis / f32(nSmp);
2277
+ }
2278
+
2279
+ fn soft_point_vis(origin: vec3<f32>, surfN: vec3<f32>, lightPos: vec3<f32>, radius: f32, seed: u32) -> f32 {
2280
+ if (params.bvhNodeCount == 0u) { return 1.0; }
2281
+ let lvec = lightPos - origin;
2282
+ let dist = length(lvec);
2283
+ if (dist < 1e-4) { return 1.0; }
2284
+ let nSmp = max(1u, params.softShadowSamples);
2285
+ if (nSmp == 1u || radius <= 1e-4) {
2286
+ return select(1.0, 0.0, bvh_occluded(origin, lvec / dist, max(0.0, dist - 2e-3), IGNORE_TRI_NONE));
2287
+ }
2288
+ let cp = seed_to_pair(seed);
2289
+ let axis = lvec / dist;
2290
+ let t1 = make_tangent(axis);
2291
+ let t2 = cross(axis, t1);
2292
+ let effR = min(radius, dist * 0.45);
2293
+ let rotAngle = cp.y * 2.0 * PI;
2294
+ var vis = 0.0;
2295
+ var validSmp = 0.0;
2296
+ for (var i = 0u; i < nSmp; i += 1u) {
2297
+ let rad = sqrt(min(0.9999, (f32(i) + cp.x) / f32(nSmp))) * effR;
2298
+ let ang = f32(i) * GOLDEN_ANGLE + rotAngle;
2299
+ let samplePos = lightPos + t1 * (cos(ang) * rad) + t2 * (sin(ang) * rad);
2300
+ let sv = samplePos - origin;
2301
+ let sd = length(sv);
2302
+ if (sd < 1e-4) {
2303
+ vis += 1.0;
2304
+ validSmp += 1.0;
2305
+ continue;
2306
+ }
2307
+ let sdir = sv / sd;
2308
+ if (dot(surfN, sdir) <= 1.5e-3) {
2309
+ continue;
2310
+ }
2311
+ validSmp += 1.0;
2312
+ if (!bvh_occluded(origin, sdir, max(0.0, sd - 2e-3), IGNORE_TRI_NONE)) {
2313
+ vis += 1.0;
2314
+ }
2315
+ }
2316
+ if (validSmp < 0.5) { return 0.0; }
2317
+ return vis / validSmp;
2318
+ }
2319
+
2320
+ fn eval_area_light_nee(
2321
+ p: vec3<f32>,
2322
+ shadowOrigin: vec3<f32>,
2323
+ n: vec3<f32>,
2324
+ pos: vec3<f32>,
2325
+ ln: vec3<f32>,
2326
+ tangentAngle: f32,
2327
+ isDisk: bool,
2328
+ halfW: f32,
2329
+ halfH: f32,
2330
+ radiance: vec3<f32>,
2331
+ seed: u32
2332
+ ) -> vec3<f32> {
2333
+ let t0 = make_tangent(ln);
2334
+ let b0 = cross(ln, t0);
2335
+ let cA = cos(tangentAngle);
2336
+ let sA = sin(tangentAngle);
2337
+ let tAxis = t0 * cA + b0 * sA;
2338
+ let bAxis = cross(ln, tAxis);
2339
+
2340
+ let emitterArea = select(4.0 * halfW * halfH, PI * halfW * halfH, isDisk);
2341
+ if (emitterArea <= 1e-7) { return vec3<f32>(0.0); }
2342
+
2343
+ let nSmp = max(4u, params.softShadowSamples);
2344
+ let cols = max(1u, u32(ceil(sqrt(f32(nSmp)))));
2345
+ let rows = max(1u, u32(ceil(f32(nSmp) / f32(cols))));
2346
+ let cp = seed_to_pair(seed);
2347
+
2348
+ var acc = vec3<f32>(0.0);
2349
+ var k = 0u;
2350
+ for (var rr = 0u; rr < rows; rr += 1u) {
2351
+ for (var cc = 0u; cc < cols; cc += 1u) {
2352
+ if (k >= nSmp) { break; }
2353
+ var ox = 0.0;
2354
+ var oy = 0.0;
2355
+ if (isDisk) {
2356
+ let u1 = fract((f32(k) + cp.x) / f32(nSmp));
2357
+ let u2 = fract(cp.y + f32(k) * GOLDEN_RATIO);
2358
+ let rad = sqrt(max(u1, 0.0));
2359
+ let phi = 2.0 * PI * u2;
2360
+ ox = rad * cos(phi) * halfW;
2361
+ oy = rad * sin(phi) * halfH;
2362
+ } else {
2363
+ let u = (f32(cc) + fract(cp.x + f32(k) * GOLDEN_RATIO)) / f32(cols) * 2.0 - 1.0;
2364
+ let v = (f32(rr) + fract(cp.y + f32(k) * 0.75487766)) / f32(rows) * 2.0 - 1.0;
2365
+ ox = u * halfW;
2366
+ oy = v * halfH;
2367
+ }
2368
+ k += 1u;
2369
+
2370
+ let samplePos = pos + tAxis * ox + bAxis * oy;
2371
+ let toL = samplePos - p;
2372
+ let distSq = dot(toL, toL);
2373
+ if (distSq <= 1e-8) { continue; }
2374
+ let dist = sqrt(distSq);
2375
+ let lDir = toL / dist;
2376
+ let ndl = dot(n, lDir);
2377
+ if (ndl <= 1.5e-3) { continue; }
2378
+ let cosLight = dot(ln, -lDir);
2379
+ if (cosLight <= 1e-4) { continue; }
2380
+
2381
+ if (params.bvhNodeCount > 0u) {
2382
+ let sv = samplePos - shadowOrigin;
2383
+ let sd = length(sv);
2384
+ if (sd > 2e-3 && bvh_occluded(shadowOrigin, sv / sd, sd - 2e-3, IGNORE_TRI_NONE)) {
2385
+ continue;
2386
+ }
2387
+ }
2388
+
2389
+ let w = (ndl * cosLight * emitterArea) / max(distSq, 1e-6);
2390
+ acc += radiance * w;
2391
+ }
2392
+ }
2393
+ if (k == 0u) { return vec3<f32>(0.0); }
2394
+ return acc / f32(k);
2395
+ }
2396
+
2397
+ fn eval_emissive_catalog(p: vec3<f32>, shadowOrigin: vec3<f32>, n: vec3<f32>, seed: u32) -> vec3<f32> {
2398
+ let count = params.emissiveCount;
2399
+ if (count == 0u) { return vec3<f32>(0.0); }
2400
+ let totalPower = emissiveTris[3].w;
2401
+ if (totalPower <= 0.0) { return vec3<f32>(0.0); }
2402
+
2403
+ let nSmp = max(4u, params.softShadowSamples);
2404
+ let cp = seed_to_pair(seed ^ 0x6d2b79f5u);
2405
+ var acc = vec3<f32>(0.0);
2406
+
2407
+ for (var s = 0u; s < nSmp; s += 1u) {
2408
+ let uPick = min(0.999999, (f32(s) + cp.x) / f32(nSmp));
2409
+ var lo = 0u;
2410
+ var hi = count - 1u;
2411
+ while (lo < hi) {
2412
+ let mid = (lo + hi) >> 1u;
2413
+ let cdfMid = emissiveTris[mid * 4u].w;
2414
+ if (uPick <= cdfMid) {
2415
+ hi = mid;
2416
+ } else {
2417
+ lo = mid + 1u;
2418
+ }
2419
+ }
2420
+
2421
+ let eb = lo * 4u;
2422
+ let v0Cdf = emissiveTris[eb];
2423
+ let e1Area = emissiveTris[eb + 1u];
2424
+ let e2Idx = emissiveTris[eb + 2u];
2425
+ let emPow = emissiveTris[eb + 3u];
2426
+ if (e1Area.w <= 1e-10) { continue; }
2427
+
2428
+ let u1 = fract(cp.y + f32(s) * GOLDEN_RATIO);
2429
+ let u2 = fract(cp.x * 0.713 + f32(s) * 0.75487766);
2430
+ let su = sqrt(u1);
2431
+ let b1 = u2 * su;
2432
+ let b2 = su - b1;
2433
+ let samplePos = v0Cdf.xyz + e1Area.xyz * b1 + e2Idx.xyz * b2;
2434
+
2435
+ let toL = samplePos - p;
2436
+ let distSq = dot(toL, toL);
2437
+ if (distSq <= 1e-8) { continue; }
2438
+ let dist = sqrt(distSq);
2439
+ let lDir = toL / dist;
2440
+ let cosSurf = dot(n, lDir);
2441
+ if (cosSurf <= 1.5e-3) { continue; }
2442
+
2443
+ let rawN = cross(e1Area.xyz, e2Idx.xyz);
2444
+ let rawLen = length(rawN);
2445
+ if (rawLen <= 1e-10) { continue; }
2446
+ let triN = rawN / rawLen;
2447
+ let cosLight = dot(triN, -lDir);
2448
+ if (cosLight <= 1e-4) { continue; }
2449
+
2450
+ let emRgb = emPow.xyz;
2451
+ let lum = 0.2126 * emRgb.x + 0.7152 * emRgb.y + 0.0722 * emRgb.z;
2452
+ if (lum <= 1e-7) { continue; }
2453
+
2454
+ let leafTriIdx = u32(e2Idx.w + 0.5);
2455
+ if (params.bvhNodeCount > 0u) {
2456
+ let sv = samplePos - shadowOrigin;
2457
+ let sd = length(sv);
2458
+ if (sd > 2e-3 && bvh_occluded(shadowOrigin, sv / sd, sd - 2e-3, leafTriIdx)) {
2459
+ continue;
2460
+ }
2461
+ }
2462
+
2463
+ let w = (cosSurf * cosLight * totalPower) / (PI * lum * max(distSq, KHR_MIN_DIST_SQ));
2464
+ acc += emRgb * w;
2465
+ }
2466
+
2467
+ return acc / f32(nSmp);
2468
+ }
2469
+
2470
+ fn eval_ao(p: vec3<f32>, n: vec3<f32>, seed: u32) -> f32 {
2471
+ let radius = params.ambient.w;
2472
+ if (params.bvhNodeCount == 0u || params.aoSamples == 0u || radius <= 1e-4) {
2473
+ return 1.0;
2474
+ }
2475
+ let origin = p + n * 1.5e-3;
2476
+ let cp = seed_to_pair(seed ^ 0x5bd1e995u);
2477
+ let t1 = make_tangent(n);
2478
+ let t2 = cross(n, t1);
2479
+ var occ = 0.0;
2480
+ let nSmp = params.aoSamples;
2481
+ for (var i = 0u; i < nSmp; i += 1u) {
2482
+ let u1 = min(0.999999, (f32(i) + cp.x) / f32(nSmp));
2483
+ let u2 = fract(f32(i) * GOLDEN_RATIO + cp.y);
2484
+ let r = sqrt(u1);
2485
+ let phi = 2.0 * PI * u2;
2486
+ let z = sqrt(max(0.0, 1.0 - u1));
2487
+ let dir = normalize(t1 * (r * cos(phi)) + t2 * (r * sin(phi)) + n * z);
2488
+ let hitT = bvh_closest_t_ao(origin, dir, radius);
2489
+ if (hitT < radius) {
2490
+ let f = 1.0 - hitT / radius;
2491
+ occ += f * f;
2492
+ }
2493
+ }
2494
+ return max(0.35, 1.0 - 0.65 * (occ / f32(nSmp)));
2495
+ }
2496
+
2497
+ fn light_base(i: u32) -> u32 {
2498
+ return i * 4u;
2499
+ }
2500
+
2501
+ fn eval_direct(p: vec3<f32>, n: vec3<f32>, seed: u32) -> vec3<f32> {
2502
+ var irr = params.ambient.xyz;
2503
+ let shadowOrigin = p + n * 1.5e-3;
2504
+ let count = params.lightCount;
2505
+ for (var i = 0u; i < count; i += 1u) {
2506
+ let b = light_base(i);
2507
+ let a = lights[b];
2508
+ let b1 = lights[b + 1u];
2509
+ let b2 = lights[b + 2u];
2510
+ let b3 = lights[b + 3u];
2511
+ let kind = u32(a.x);
2512
+ let color = a.yzw;
2513
+ let intensity = b1.x;
2514
+ let lSeed = seed ^ ((i + 1u) * 0x9e3779b9u);
2515
+
2516
+ if (kind == 0u) {
2517
+ let toL = normalize(b1.yzw);
2518
+ let ndl = max(dot(n, toL), 0.0);
2519
+ if (ndl <= 0.0) { continue; }
2520
+ let vis = soft_dir_vis(shadowOrigin, toL, lSeed);
2521
+ if (vis <= 0.0) { continue; }
2522
+ irr += color * (intensity * ndl * vis);
2523
+ } else if (kind == 1u || kind == 2u) {
2524
+ let pos = b1.yzw;
2525
+ let lvec = pos - p;
2526
+ let dist = max(length(lvec), 1e-4);
2527
+ let toL = lvec / dist;
2528
+ let ndl = max(dot(n, toL), 0.0);
2529
+ if (ndl <= 0.0) { continue; }
2530
+ let range = b2.w;
2531
+ var atten = khr_punctual_attenuation(dist, range);
2532
+ if (atten <= 0.0) { continue; }
2533
+ if (kind == 2u) {
2534
+ let dir = normalize(b2.xyz);
2535
+ let spotAtten = khr_spot_attenuation(-dot(toL, dir), b3.x, b3.y);
2536
+ if (spotAtten <= 0.0) { continue; }
2537
+ atten *= spotAtten;
2538
+ }
2539
+ let vis = soft_point_vis(shadowOrigin, n, pos, POINT_DISK_RADIUS, lSeed);
2540
+ if (vis <= 0.0) { continue; }
2541
+ irr += color * (intensity * ndl * atten * vis);
2542
+ } else if (kind == 3u) {
2543
+ irr += eval_area_light_nee(
2544
+ p,
2545
+ shadowOrigin,
2546
+ n,
2547
+ b1.yzw,
2548
+ normalize(b2.xyz),
2549
+ b3.x,
2550
+ b3.y > 0.5,
2551
+ max(b3.z, 1e-3),
2552
+ max(b3.w, 1e-3),
2553
+ color * intensity,
2554
+ lSeed
2555
+ );
2556
+ } else if (params.emissiveCount == 0u) {
2557
+ irr += eval_area_light_nee(
2558
+ p,
2559
+ shadowOrigin,
2560
+ n,
2561
+ b1.yzw,
2562
+ normalize(b2.xyz),
2563
+ 0.0,
2564
+ false,
2565
+ max(b3.z, 1e-3),
2566
+ max(b3.w, 1e-3),
2567
+ color * intensity,
2568
+ lSeed
2569
+ );
2570
+ }
2571
+ }
2572
+ irr += eval_emissive_catalog(p, shadowOrigin, n, seed);
2573
+ return irr;
2574
+ }
2575
+
2576
+ @compute @workgroup_size(8, 8, 1)
2577
+ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
2578
+ let x = params.tileOriginX + gid.x;
2579
+ let y = params.tileOriginY + gid.y;
2580
+ if (x >= params.atlasSize || y >= params.atlasSize) {
2581
+ return;
2582
+ }
2583
+ let uv = vec2<i32>(i32(x), i32(y));
2584
+ let posS = textureLoad(gbufferPos, uv, 0);
2585
+ if (posS.a < 0.5) {
2586
+ textureStore(outputIrradiance, uv, vec4<f32>(0.0));
2587
+ return;
2588
+ }
2589
+ let n = normalize(textureLoad(gbufferNormal, uv, 0).xyz);
2590
+ let seed = hash_texel(x, y, 1u);
2591
+ let irr = eval_direct(posS.xyz, n, seed);
2592
+ let ao = eval_ao(posS.xyz, n, seed);
2593
+ textureStore(outputIrradiance, uv, vec4<f32>(irr, clamp(ao, 0.05, 1.0)));
2594
+ }
2595
+ `
2596
+ ), pe = 16;
2597
+ function Or(t) {
2598
+ const a = new Float32Array(Math.max(1, t.length) * pe);
2599
+ for (let e = 0; e < t.length; e++) {
2600
+ const n = t[e], s = e * pe, i = n.kind === "directional" ? 0 : n.kind === "point" ? 1 : n.kind === "spot" ? 2 : n.kind === "area" ? 3 : 4;
2601
+ if (a[s] = i, a[s + 1] = n.color[0], a[s + 2] = n.color[1], a[s + 3] = n.color[2], a[s + 4] = n.intensity, n.kind === "directional" ? (a[s + 5] = n.toLight[0], a[s + 6] = n.toLight[1], a[s + 7] = n.toLight[2]) : (a[s + 5] = n.position[0], a[s + 6] = n.position[1], a[s + 7] = n.position[2]), n.kind === "spot")
2602
+ a[s + 8] = n.direction[0], a[s + 9] = n.direction[1], a[s + 10] = n.direction[2], a[s + 11] = n.range, a[s + 12] = n.innerCos, a[s + 13] = n.outerCos;
2603
+ else if (n.kind === "point")
2604
+ a[s + 11] = n.range;
2605
+ else if (n.kind === "area") {
2606
+ a[s + 8] = n.normal[0], a[s + 9] = n.normal[1], a[s + 10] = n.normal[2], a[s + 11] = n.area;
2607
+ const r = Math.sqrt(Math.max(1e-6, n.area)), l = (n.width ?? r) * 0.5, o = (n.height ?? r) * 0.5;
2608
+ let h = 0;
2609
+ if (n.tangent) {
2610
+ const m = cn(n.normal), f = n.normal[1] * m[2] - n.normal[2] * m[1], u = n.normal[2] * m[0] - n.normal[0] * m[2], c = n.normal[0] * m[1] - n.normal[1] * m[0], d = n.tangent[0] * m[0] + n.tangent[1] * m[1] + n.tangent[2] * m[2], x = n.tangent[0] * f + n.tangent[1] * u + n.tangent[2] * c;
2611
+ h = Math.atan2(x, d);
2612
+ }
2613
+ a[s + 12] = h, a[s + 13] = n.isDisk ? 1 : 0, a[s + 14] = l, a[s + 15] = o;
2614
+ } else if (n.kind === "emissive") {
2615
+ a[s + 8] = n.normal[0], a[s + 9] = n.normal[1], a[s + 10] = n.normal[2], a[s + 11] = n.area;
2616
+ const r = Math.sqrt(Math.max(1e-6, n.area)) * 0.5;
2617
+ a[s + 14] = r, a[s + 15] = r;
2618
+ }
2619
+ }
2620
+ return a;
2621
+ }
2622
+ const Hr = pe, Ye = 256;
2623
+ async function Dr(t, a, e, n) {
2624
+ var j, X;
2625
+ if ((j = n == null ? void 0 : n.signal) != null && j.aborted)
2626
+ throw new DOMException("Bake aborted", "AbortError");
2627
+ const s = a.atlasSize, i = (n == null ? void 0 : n.ambient) ?? [0.08, 0.08, 0.1];
2628
+ n == null || n.tileSize;
2629
+ const r = (n == null ? void 0 : n.accel) ?? null, l = r ? Math.max(1, (n == null ? void 0 : n.softShadowSamples) ?? 8) : 0, o = r ? Math.max(0, (n == null ? void 0 : n.aoSamples) ?? 8) : 0, h = Math.max(0, (n == null ? void 0 : n.aoRadius) ?? 0.45), m = t.createTexture({
2630
+ label: "lightmap_gbuffer_pos",
2631
+ size: { width: s, height: s },
2632
+ format: "rgba32float",
2633
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
2634
+ }), f = t.createTexture({
2635
+ label: "lightmap_gbuffer_nrm",
2636
+ size: { width: s, height: s },
2637
+ format: "rgba32float",
2638
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
2639
+ }), u = t.createTexture({
2640
+ label: "lightmap_gpu_direct",
2641
+ size: { width: s, height: s },
2642
+ format: "rgba16float",
2643
+ usage: GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_SRC
2644
+ }), c = yt(a.position), d = yt(a.normal);
2645
+ t.queue.writeTexture(
2646
+ { texture: m },
2647
+ c,
2648
+ { bytesPerRow: s * 16 },
2649
+ { width: s, height: s }
2650
+ ), t.queue.writeTexture(
2651
+ { texture: f },
2652
+ d,
2653
+ { bytesPerRow: s * 16 },
2654
+ { width: s, height: s }
2655
+ );
2656
+ const x = Or(e), b = t.createBuffer({
2657
+ label: "lightmap_bake_lights",
2658
+ size: Math.max(16, x.byteLength),
2659
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
2660
+ });
2661
+ t.queue.writeBuffer(b, 0, yt(x));
2662
+ const g = r ? nn(r) : null, v = g ? new Uint8Array(g.nodeBytes) : new Uint8Array(32), A = g ? yt(g.triFloats) : new Uint8Array(48), M = g ? yt(g.emissiveFloats) : new Uint8Array(64), p = t.createBuffer({
2663
+ label: "lightmap_bvh_nodes",
2664
+ size: Math.max(32, v.byteLength),
2665
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
2666
+ });
2667
+ t.queue.writeBuffer(p, 0, v);
2668
+ const y = t.createBuffer({
2669
+ label: "lightmap_bvh_tris",
2670
+ size: Math.max(48, A.byteLength),
2671
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
2672
+ });
2673
+ t.queue.writeBuffer(y, 0, A);
2674
+ const O = t.createBuffer({
2675
+ label: "lightmap_emissive_tris",
2676
+ size: Math.max(64, M.byteLength),
2677
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
2678
+ });
2679
+ t.queue.writeBuffer(O, 0, M);
2680
+ const k = new ArrayBuffer(64), U = new Uint32Array(k, 0, 4), D = new Float32Array(k, 16, 4), H = new Uint32Array(k, 32, 4), R = new Uint32Array(k, 48, 4);
2681
+ U[0] = s, U[1] = 0, U[2] = 0, U[3] = e.length, D[0] = i[0], D[1] = i[1], D[2] = i[2], D[3] = h, H[0] = g ? g.nodeCount : 0, H[1] = g ? g.rootIndex : 0, H[2] = l, H[3] = o, R[0] = g ? g.emissiveCount : 0;
2682
+ const F = t.createBuffer({
2683
+ label: "lightmap_bake_params",
2684
+ size: 64,
2685
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
2686
+ });
2687
+ t.queue.writeBuffer(F, 0, k);
2688
+ const q = t.createShaderModule({
2689
+ label: "bake_lightmap.compute",
2690
+ code: Cr
2691
+ }), W = t.createComputePipeline({
2692
+ label: "lightmap_bake_pipeline",
2693
+ layout: "auto",
2694
+ compute: { module: q, entryPoint: "main" }
2695
+ }), z = t.createBindGroup({
2696
+ layout: W.getBindGroupLayout(0),
2697
+ entries: [
2698
+ { binding: 0, resource: { buffer: F } },
2699
+ { binding: 1, resource: m.createView() },
2700
+ { binding: 2, resource: f.createView() },
2701
+ { binding: 3, resource: u.createView() },
2702
+ { binding: 4, resource: { buffer: b } },
2703
+ { binding: 5, resource: { buffer: p } },
2704
+ { binding: 6, resource: { buffer: y } },
2705
+ { binding: 7, resource: { buffer: O } }
2706
+ ]
2707
+ });
2708
+ if ((X = n == null ? void 0 : n.signal) != null && X.aborted)
2709
+ throw new DOMException("Bake aborted", "AbortError");
2710
+ const Y = t.createCommandEncoder({ label: "lightmap_gpu_bake" }), G = Y.beginComputePass({ label: "lightmap_gpu_direct" });
2711
+ G.setPipeline(W), G.setBindGroup(0, z);
2712
+ const L = Math.ceil(s / 8);
2713
+ G.dispatchWorkgroups(L, L, 1), G.end();
2714
+ const V = await dn(t, Y, u, s);
2715
+ return m.destroy(), f.destroy(), u.destroy(), b.destroy(), p.destroy(), y.destroy(), O.destroy(), F.destroy(), V;
2716
+ }
2717
+ function yt(t) {
2718
+ const a = new Uint8Array(t.buffer, t.byteOffset, t.byteLength), e = new Uint8Array(a.byteLength);
2719
+ return e.set(a), e;
2720
+ }
2721
+ function te(t) {
2722
+ const a = t >> 15 & 1, e = t >> 10 & 31, n = t & 1023;
2723
+ return e === 0 ? (a ? -1 : 1) * Math.pow(2, -14) * (n / 1024) : e === 31 ? n !== 0 ? NaN : a ? -1 / 0 : 1 / 0 : (a ? -1 : 1) * Math.pow(2, e - 15) * (1 + n / 1024);
2724
+ }
2725
+ async function dn(t, a, e, n) {
2726
+ const i = Math.ceil(n * 8 / Ye) * Ye, r = t.createBuffer({
2727
+ label: "lightmap_gpu_readback",
2728
+ size: i * n,
2729
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
2730
+ });
2731
+ a.copyTextureToBuffer(
2732
+ { texture: e },
2733
+ { buffer: r, bytesPerRow: i, rowsPerImage: n },
2734
+ { width: n, height: n }
2735
+ ), t.queue.submit([a.finish()]), await t.queue.onSubmittedWorkDone(), await r.mapAsync(GPUMapMode.READ);
2736
+ const l = new Uint8Array(r.getMappedRange()), o = new Uint8Array(l.length);
2737
+ o.set(l), r.unmap(), r.destroy();
2738
+ const h = new Float32Array(n * n * 4), m = new DataView(o.buffer, o.byteOffset, o.byteLength);
2739
+ let f = 0;
2740
+ for (let u = 0; u < n; u++) {
2741
+ const c = u * i;
2742
+ for (let d = 0; d < n; d++) {
2743
+ const x = c + d * 8;
2744
+ h[f++] = te(m.getUint16(x, !0)), h[f++] = te(m.getUint16(x + 2, !0)), h[f++] = te(m.getUint16(x + 4, !0)), h[f++] = te(m.getUint16(x + 6, !0));
2745
+ }
2746
+ }
2747
+ return h;
2748
+ }
2749
+ const Rr = (
2750
+ /* wgsl */
2751
+ `/**
2752
+ * @file bake_gi_bounce.compute.wgsl
2753
+ * @description GPU multi-bounce diffuse GI radiosity kernel (KPTL-6) — ordered SAH BVH traversal
2754
+ * (without iteration cap), barycentric \`UV1\` texel-space lookup into \`prevBounceTex\`, sRGB-decoded
2755
+ * \`(1 - metallic)\` diffuse reflectance, and sky/HDRI environment fallback on escaped rays.
2756
+ *
2757
+ * Keep byte-for-byte in sync with \`src/bakeGiBounceComputeWgsl.ts\`.
2758
+ */
2759
+
2760
+ struct GiBounceParams {
2761
+ atlasSize: u32,
2762
+ bounceIndex: u32,
2763
+ bounceSamples: u32,
2764
+ bvhNodeCount: u32,
2765
+ bvhRoot: u32,
2766
+ _pad0: u32,
2767
+ _pad1: u32,
2768
+ _pad2: u32,
2769
+ skyAndMaxDist: vec4<f32>,
2770
+ }
2771
+
2772
+ @group(0) @binding(0) var<uniform> params: GiBounceParams;
2773
+ @group(0) @binding(1) var gbufferPos: texture_2d<f32>;
2774
+ @group(0) @binding(2) var gbufferNormal: texture_2d<f32>;
2775
+ @group(0) @binding(3) var prevBounceTex: texture_2d<f32>;
2776
+ @group(0) @binding(4) var outputBounce: texture_storage_2d<rgba16float, write>;
2777
+ @group(0) @binding(5) var<storage, read> bvhNodes: array<vec4<f32>>;
2778
+ @group(0) @binding(6) var<storage, read> bvhTris: array<vec4<f32>>;
2779
+ @group(0) @binding(7) var<storage, read> triUv1: array<vec4<f32>>;
2780
+ @group(0) @binding(8) var<storage, read> triAlbedo: array<vec4<f32>>;
2781
+ @group(0) @binding(9) var<storage, read> triFallbackBounce: array<vec4<f32>>;
2782
+
2783
+ const PI: f32 = 3.14159265;
2784
+ const GOLDEN_RATIO: f32 = 0.61803399;
2785
+
2786
+ struct BounceHit {
2787
+ hit: bool,
2788
+ t: f32,
2789
+ triIdx: u32,
2790
+ u: f32,
2791
+ v: f32,
2792
+ }
2793
+
2794
+ fn hash_texel(x: u32, y: u32, salt: u32) -> u32 {
2795
+ var h = (x * 0x27d4eb2du) ^ (y * 0x165667b1u) ^ (salt * 0x9e3779b9u);
2796
+ h = h ^ (h >> 15u);
2797
+ h = h * 0x2c1b3c6du;
2798
+ h = h ^ (h >> 12u);
2799
+ return h;
2800
+ }
2801
+
2802
+ fn seed_to_pair(seed: u32) -> vec2<f32> {
2803
+ var h = seed ^ (seed >> 16u);
2804
+ h = h * 0x7feb352du;
2805
+ h = h ^ (h >> 15u);
2806
+ h = h * 0x846ca68bu;
2807
+ h = h ^ (h >> 16u);
2808
+ return vec2<f32>(f32(h & 0xffffu) / 65536.0, f32((h >> 16u) & 0xffffu) / 65536.0);
2809
+ }
2810
+
2811
+ fn make_tangent(axis: vec3<f32>) -> vec3<f32> {
2812
+ let up = select(vec3<f32>(1.0, 0.0, 0.0), vec3<f32>(0.0, 1.0, 0.0), abs(axis.y) < 0.999);
2813
+ return normalize(cross(up, axis));
2814
+ }
2815
+
2816
+ fn ray_aabb_t(o: vec3<f32>, d: vec3<f32>, bmin: vec3<f32>, bmax: vec3<f32>, maxDist: f32) -> f32 {
2817
+ var tMin = 0.0;
2818
+ var tMax = maxDist;
2819
+ if (abs(d.x) < 1e-8) {
2820
+ if (o.x < bmin.x || o.x > bmax.x) { return -1.0; }
2821
+ } else {
2822
+ let inv = 1.0 / d.x;
2823
+ let t0 = (bmin.x - o.x) * inv;
2824
+ let t1 = (bmax.x - o.x) * inv;
2825
+ tMin = max(tMin, min(t0, t1));
2826
+ tMax = min(tMax, max(t0, t1));
2827
+ if (tMin > tMax) { return -1.0; }
2828
+ }
2829
+ if (abs(d.y) < 1e-8) {
2830
+ if (o.y < bmin.y || o.y > bmax.y) { return -1.0; }
2831
+ } else {
2832
+ let inv = 1.0 / d.y;
2833
+ let t0 = (bmin.y - o.y) * inv;
2834
+ let t1 = (bmax.y - o.y) * inv;
2835
+ tMin = max(tMin, min(t0, t1));
2836
+ tMax = min(tMax, max(t0, t1));
2837
+ if (tMin > tMax) { return -1.0; }
2838
+ }
2839
+ if (abs(d.z) < 1e-8) {
2840
+ if (o.z < bmin.z || o.z > bmax.z) { return -1.0; }
2841
+ } else {
2842
+ let inv = 1.0 / d.z;
2843
+ let t0 = (bmin.z - o.z) * inv;
2844
+ let t1 = (bmax.z - o.z) * inv;
2845
+ tMin = max(tMin, min(t0, t1));
2846
+ tMax = min(tMax, max(t0, t1));
2847
+ if (tMin > tMax) { return -1.0; }
2848
+ }
2849
+ return tMin;
2850
+ }
2851
+
2852
+ fn bvh_closest_hit(o: vec3<f32>, d: vec3<f32>, maxDist: f32) -> BounceHit {
2853
+ var res: BounceHit;
2854
+ res.hit = false;
2855
+ res.t = maxDist;
2856
+ res.triIdx = 0u;
2857
+ res.u = 0.0;
2858
+ res.v = 0.0;
2859
+
2860
+ if (params.bvhNodeCount == 0u) { return res; }
2861
+ let rootBase = params.bvhRoot * 2u;
2862
+ if (ray_aabb_t(o, d, bvhNodes[rootBase].xyz, bvhNodes[rootBase + 1u].xyz, maxDist) < 0.0) {
2863
+ return res;
2864
+ }
2865
+
2866
+ var stack: array<u32, 64>;
2867
+ var sp = 1u;
2868
+ stack[0] = params.bvhRoot;
2869
+ while (sp > 0u) {
2870
+ sp -= 1u;
2871
+ let ni = stack[sp];
2872
+ let nb = ni * 2u;
2873
+ let n0 = bvhNodes[nb];
2874
+ let n1 = bvhNodes[nb + 1u];
2875
+ if (ray_aabb_t(o, d, n0.xyz, n1.xyz, res.t) < 0.0) {
2876
+ continue;
2877
+ }
2878
+ let meta0 = bitcast<i32>(n0.w);
2879
+ let meta1 = bitcast<i32>(n1.w);
2880
+ if (meta0 < 0) {
2881
+ let triStart = u32(-1 - meta0);
2882
+ let triCount = u32(max(meta1, 0));
2883
+ for (var k = 0u; k < triCount; k += 1u) {
2884
+ let tid = triStart + k;
2885
+ let base = tid * 3u;
2886
+ let v0 = bvhTris[base].xyz;
2887
+ let e1 = bvhTris[base + 1u].xyz;
2888
+ let e2 = bvhTris[base + 2u].xyz;
2889
+ let h = cross(d, e2);
2890
+ let det = dot(e1, h);
2891
+ if (abs(det) < 1e-8) { continue; }
2892
+ let f = 1.0 / det;
2893
+ let s = o - v0;
2894
+ let u = f * dot(s, h);
2895
+ if (u < 0.0 || u > 1.0) { continue; }
2896
+ let q = cross(s, e1);
2897
+ let v = f * dot(d, q);
2898
+ if (v < 0.0 || u + v > 1.0) { continue; }
2899
+ let t = f * dot(e2, q);
2900
+ if (t > 1e-4 && t < res.t) {
2901
+ res.hit = true;
2902
+ res.t = t;
2903
+ res.triIdx = tid;
2904
+ res.u = u;
2905
+ res.v = v;
2906
+ }
2907
+ }
2908
+ } else {
2909
+ let leftIdx = u32(meta0);
2910
+ let rightIdx = u32(meta1);
2911
+ let lb = leftIdx * 2u;
2912
+ let rb = rightIdx * 2u;
2913
+ let tL = ray_aabb_t(o, d, bvhNodes[lb].xyz, bvhNodes[lb + 1u].xyz, res.t);
2914
+ let tR = ray_aabb_t(o, d, bvhNodes[rb].xyz, bvhNodes[rb + 1u].xyz, res.t);
2915
+ if (tL >= 0.0 && tR >= 0.0) {
2916
+ if (sp < 62u) {
2917
+ if (tL <= tR) {
2918
+ stack[sp] = rightIdx;
2919
+ stack[sp + 1u] = leftIdx;
2920
+ } else {
2921
+ stack[sp] = leftIdx;
2922
+ stack[sp + 1u] = rightIdx;
2923
+ }
2924
+ sp += 2u;
2925
+ }
2926
+ } else if (tL >= 0.0 && sp < 63u) {
2927
+ stack[sp] = leftIdx;
2928
+ sp += 1u;
2929
+ } else if (tR >= 0.0 && sp < 63u) {
2930
+ stack[sp] = rightIdx;
2931
+ sp += 1u;
2932
+ }
2933
+ }
2934
+ }
2935
+ return res;
2936
+ }
2937
+
2938
+ @compute @workgroup_size(8, 8, 1)
2939
+ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
2940
+ let x = gid.x;
2941
+ let y = gid.y;
2942
+ if (x >= params.atlasSize || y >= params.atlasSize) {
2943
+ return;
2944
+ }
2945
+ let uv = vec2<i32>(i32(x), i32(y));
2946
+ let posS = textureLoad(gbufferPos, uv, 0);
2947
+ if (posS.a < 0.5) {
2948
+ textureStore(outputBounce, uv, vec4<f32>(0.0));
2949
+ return;
2950
+ }
2951
+
2952
+ let n = normalize(textureLoad(gbufferNormal, uv, 0).xyz);
2953
+ let origin = posS.xyz + n * 1.5e-3;
2954
+ let nSmp = max(1u, params.bounceSamples);
2955
+ let seed = hash_texel(x, y, 101u + params.bounceIndex * 37u);
2956
+ let cp = seed_to_pair(seed);
2957
+ let t1 = make_tangent(n);
2958
+ let t2 = cross(n, t1);
2959
+ let maxDist = max(0.5, params.skyAndMaxDist.w);
2960
+ let skyRad = params.skyAndMaxDist.xyz;
2961
+ let atlasMax = i32(params.atlasSize) - 1;
2962
+ let atlasF = f32(params.atlasSize);
2963
+
2964
+ var acc = vec3<f32>(0.0);
2965
+ for (var i = 0u; i < nSmp; i += 1u) {
2966
+ let u1 = min(0.999999, (f32(i) + cp.x) / f32(nSmp));
2967
+ let u2 = fract(f32(i) * GOLDEN_RATIO + cp.y);
2968
+ let r = sqrt(u1);
2969
+ let phi = 2.0 * PI * u2;
2970
+ let z = sqrt(max(0.0, 1.0 - u1));
2971
+ let dir = normalize(t1 * (r * cos(phi)) + t2 * (r * sin(phi)) + n * z);
2972
+
2973
+ let hit = bvh_closest_hit(origin, dir, maxDist);
2974
+ if (!hit.hit) {
2975
+ acc += skyRad;
2976
+ continue;
2977
+ }
2978
+
2979
+ let alb = triAlbedo[hit.triIdx].xyz;
2980
+ var hitIrr = triFallbackBounce[hit.triIdx].xyz;
2981
+
2982
+ let uBase = hit.triIdx * 2u;
2983
+ let uvAB = triUv1[uBase];
2984
+ let uvC = triUv1[uBase + 1u].xy;
2985
+ let w0 = 1.0 - hit.u - hit.v;
2986
+ let hitUv = uvAB.xy * w0 + uvAB.zw * hit.u + uvC * hit.v;
2987
+ let tx = clamp(i32(floor(hitUv.x * atlasF)), 0, atlasMax);
2988
+ let ty = clamp(i32(floor(hitUv.y * atlasF)), 0, atlasMax);
2989
+ let texSmp = textureLoad(prevBounceTex, vec2<i32>(tx, ty), 0);
2990
+ if (texSmp.a > 0.0) {
2991
+ hitIrr = texSmp.xyz;
2992
+ }
2993
+
2994
+ let bounced = alb * hitIrr;
2995
+ let maxCh = max(bounced.x, max(bounced.y, bounced.z));
2996
+ if (maxCh > 2.5) {
2997
+ let clamped = 2.5 + (maxCh - 2.5) / (1.0 + (maxCh - 2.5) * 0.5);
2998
+ acc += bounced * (clamped / maxCh);
2999
+ } else {
3000
+ acc += bounced;
3001
+ }
3002
+ }
3003
+
3004
+ textureStore(outputBounce, uv, vec4<f32>(acc / f32(nSmp), 1.0));
3005
+ }
3006
+ `
3007
+ );
3008
+ function We(t) {
3009
+ const a = t.triOrder.length, e = new Float32Array(Math.max(1, a) * 4);
3010
+ for (let n = 0; n < a; n++) {
3011
+ const i = t.triOrder[n] * 3, r = n * 4;
3012
+ e[r] = t.bounceSrc[i], e[r + 1] = t.bounceSrc[i + 1], e[r + 2] = t.bounceSrc[i + 2], e[r + 3] = 1;
3013
+ }
3014
+ return e;
3015
+ }
3016
+ async function Lr(t, a, e, n, s, i) {
3017
+ var _, C, S;
3018
+ if ((_ = i.signal) != null && _.aborted)
3019
+ throw new DOMException("Bake aborted", "AbortError");
3020
+ const r = a.atlasSize, l = r * r, o = Math.max(0, i.bounces | 0), h = new Float32Array(l * 4);
3021
+ if (o === 0 || s.length === 0)
3022
+ return h;
3023
+ const m = Math.max(8, i.bounceSamples ?? 24), f = Math.max(1, i.maxDist ?? 25), u = i.skyRadiance ?? [0, 0, 0], c = i.denoise !== !1, d = [1, 2, 4, 8], x = new Float32Array(l * 4);
3024
+ for (let I = 0; I < s.length; I++) {
3025
+ const T = s[I] * 4;
3026
+ x[T] = n[T], x[T + 1] = n[T + 1], x[T + 2] = n[T + 2], x[T + 3] = 1;
3027
+ }
3028
+ qt(x, r, 2), re(e, x, r);
3029
+ const b = nn(e), g = t.createTexture({
3030
+ label: "lightmap_gi_pos",
3031
+ size: { width: r, height: r },
3032
+ format: "rgba32float",
3033
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
3034
+ }), v = t.createTexture({
3035
+ label: "lightmap_gi_nrm",
3036
+ size: { width: r, height: r },
3037
+ format: "rgba32float",
3038
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
3039
+ }), A = t.createTexture({
3040
+ label: "lightmap_gi_prev",
3041
+ size: { width: r, height: r },
3042
+ format: "rgba32float",
3043
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
3044
+ }), M = t.createTexture({
3045
+ label: "lightmap_gi_out",
3046
+ size: { width: r, height: r },
3047
+ format: "rgba16float",
3048
+ usage: GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_SRC
3049
+ });
3050
+ t.queue.writeTexture(
3051
+ { texture: g },
3052
+ yt(a.position),
3053
+ { bytesPerRow: r * 16 },
3054
+ { width: r, height: r }
3055
+ ), t.queue.writeTexture(
3056
+ { texture: v },
3057
+ yt(a.normal),
3058
+ { bytesPerRow: r * 16 },
3059
+ { width: r, height: r }
3060
+ );
3061
+ const p = new Uint8Array(b.nodeBytes), y = t.createBuffer({
3062
+ label: "lightmap_gi_bvh_nodes",
3063
+ size: Math.max(32, p.byteLength),
3064
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
3065
+ });
3066
+ t.queue.writeBuffer(y, 0, p);
3067
+ const O = yt(b.triFloats), k = t.createBuffer({
3068
+ label: "lightmap_gi_bvh_tris",
3069
+ size: Math.max(48, O.byteLength),
3070
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
3071
+ });
3072
+ t.queue.writeBuffer(k, 0, O);
3073
+ const U = yt(b.triUv1Floats), D = t.createBuffer({
3074
+ label: "lightmap_gi_tri_uv1",
3075
+ size: Math.max(32, U.byteLength),
3076
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
3077
+ });
3078
+ t.queue.writeBuffer(D, 0, U);
3079
+ const H = yt(b.triAlbedoFloats), R = t.createBuffer({
3080
+ label: "lightmap_gi_tri_albedo",
3081
+ size: Math.max(16, H.byteLength),
3082
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
3083
+ });
3084
+ t.queue.writeBuffer(R, 0, H);
3085
+ const F = We(e), q = t.createBuffer({
3086
+ label: "lightmap_gi_tri_fallback",
3087
+ size: Math.max(16, F.byteLength),
3088
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
3089
+ }), W = new ArrayBuffer(48), z = new Uint32Array(W, 0, 8), Y = new Float32Array(W, 32, 4);
3090
+ z[0] = r, z[2] = m, z[3] = b.nodeCount, z[4] = b.rootIndex, Y[0] = u[0], Y[1] = u[1], Y[2] = u[2], Y[3] = f;
3091
+ const G = t.createBuffer({
3092
+ label: "lightmap_gi_params",
3093
+ size: 48,
3094
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
3095
+ }), L = t.createShaderModule({
3096
+ label: "bake_gi_bounce.compute",
3097
+ code: Rr
3098
+ }), V = t.createComputePipeline({
3099
+ label: "lightmap_gi_bounce_pipeline",
3100
+ layout: "auto",
3101
+ compute: { module: L, entryPoint: "main" }
3102
+ }), j = t.createBindGroup({
3103
+ layout: V.getBindGroupLayout(0),
3104
+ entries: [
3105
+ { binding: 0, resource: { buffer: G } },
3106
+ { binding: 1, resource: g.createView() },
3107
+ { binding: 2, resource: v.createView() },
3108
+ { binding: 3, resource: A.createView() },
3109
+ { binding: 4, resource: M.createView() },
3110
+ { binding: 5, resource: { buffer: y } },
3111
+ { binding: 6, resource: { buffer: k } },
3112
+ { binding: 7, resource: { buffer: D } },
3113
+ { binding: 8, resource: { buffer: R } },
3114
+ { binding: 9, resource: { buffer: q } }
3115
+ ]
3116
+ }), X = Math.ceil(r / 8);
3117
+ try {
3118
+ for (let I = 0; I < o; I++) {
3119
+ if ((C = i.signal) != null && C.aborted)
3120
+ throw new DOMException("Bake aborted", "AbortError");
3121
+ z[1] = I, t.queue.writeBuffer(G, 0, W), t.queue.writeTexture(
3122
+ { texture: A },
3123
+ yt(x),
3124
+ { bytesPerRow: r * 16 },
3125
+ { width: r, height: r }
3126
+ ), t.queue.writeBuffer(
3127
+ q,
3128
+ 0,
3129
+ yt(We(e))
3130
+ );
3131
+ const T = t.createCommandEncoder({ label: `lightmap_gi_bounce_${I}` }), P = T.beginComputePass({ label: `lightmap_gi_bounce_pass_${I}` });
3132
+ P.setPipeline(V), P.setBindGroup(0, j), P.dispatchWorkgroups(X, X, 1), P.end();
3133
+ const w = await dn(t, T, M, r);
3134
+ c && Ft(w, r, {
3135
+ gbuffer: a,
3136
+ stepSizes: d,
3137
+ wavelet5x5: !0,
3138
+ sigmaRange: 14,
3139
+ planeSigma: 0.06
3140
+ });
3141
+ for (let B = 0; B < s.length; B++) {
3142
+ const N = s[B] * 4;
3143
+ h[N] = (h[N] ?? 0) + w[N], h[N + 1] = (h[N + 1] ?? 0) + w[N + 1], h[N + 2] = (h[N + 2] ?? 0) + w[N + 2], h[N + 3] = 1;
3144
+ }
3145
+ I + 1 < o && (x.set(w), qt(x, r, 2), re(e, x, r)), (S = i.onBounceComplete) == null || S.call(i, I);
3146
+ }
3147
+ } finally {
3148
+ g.destroy(), v.destroy(), A.destroy(), M.destroy(), y.destroy(), k.destroy(), D.destroy(), R.destroy(), q.destroy(), G.destroy();
3149
+ }
3150
+ return h;
3151
+ }
3152
+ function mt(t, a, e, n) {
3153
+ t == null || t({ phase: a, completed: e, total: n });
3154
+ }
3155
+ async function Yr(t, a, e) {
3156
+ var R, F, q, W, z, Y;
3157
+ const n = {
3158
+ texelsPerUnit: ((R = e == null ? void 0 : e.settings) == null ? void 0 : R.texelsPerUnit) ?? 40,
3159
+ bounces: ((F = e == null ? void 0 : e.settings) == null ? void 0 : F.bounces) ?? Ke,
3160
+ samplesPerTexel: ((q = e == null ? void 0 : e.settings) == null ? void 0 : q.samplesPerTexel) ?? Nn,
3161
+ atlasSize: ((W = e == null ? void 0 : e.settings) == null ? void 0 : W.atlasSize) ?? we
3162
+ };
3163
+ if (mt(e == null ? void 0 : e.onProgress, "prepare", 0, 1), (z = e == null ? void 0 : e.signal) != null && z.aborted)
3164
+ throw new DOMException("Bake aborted", "AbortError");
3165
+ const s = Qn(t, a, {
3166
+ atlasSize: n.atlasSize,
3167
+ texelsPerUnit: n.texelsPerUnit
3168
+ });
3169
+ n.texelsPerUnit = s.texelsPerUnit, mt(e == null ? void 0 : e.onProgress, "prepare", 1, 1);
3170
+ const i = s.atlasSize, r = new Float32Array(i * i * 4), l = (e == null ? void 0 : e.ambientIrradiance) ?? [0.08, 0.08, 0.1], o = br(t), h = (e == null ? void 0 : e.aoSamples) ?? 6, m = (e == null ? void 0 : e.aoRadius) ?? 0.45;
3171
+ mt(e == null ? void 0 : e.onProgress, "irradiance", 0, s.charts.length || 1);
3172
+ 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, d = !!(((e == null ? void 0 : e.gpuDirect) ?? !0) && c), x = !!(((e == null ? void 0 : e.gpuBounces) ?? !0) && c), b = Math.max(1, 1 + n.bounces), g = await vr(
3173
+ t,
3174
+ s.charts,
3175
+ r,
3176
+ i,
3177
+ o,
3178
+ l,
3179
+ {
3180
+ signal: e == null ? void 0 : e.signal,
3181
+ bounces: n.bounces,
3182
+ aoSamples: h,
3183
+ aoRadius: m,
3184
+ softShadowSamples: f,
3185
+ bounceSamples: u,
3186
+ skyRadiance: e == null ? void 0 : e.skyRadiance,
3187
+ denoise: (e == null ? void 0 : e.denoise) !== !1,
3188
+ gpuDirectRunner: d && c ? async (G, L) => {
3189
+ const V = await Dr(c, G, o, {
3190
+ signal: e == null ? void 0 : e.signal,
3191
+ ambient: l,
3192
+ accel: L,
3193
+ softShadowSamples: f,
3194
+ aoSamples: h,
3195
+ aoRadius: m
3196
+ });
3197
+ return mt(e == null ? void 0 : e.onProgress, "irradiance", 1, b), V;
3198
+ } : void 0,
3199
+ gpuBounceRunner: x && c ? async (G, L, V, j) => Lr(c, G, L, V, j, {
3200
+ signal: e == null ? void 0 : e.signal,
3201
+ bounces: n.bounces,
3202
+ bounceSamples: u,
3203
+ skyRadiance: e == null ? void 0 : e.skyRadiance,
3204
+ denoise: (e == null ? void 0 : e.denoise) !== !1,
3205
+ onBounceComplete: (X) => mt(e == null ? void 0 : e.onProgress, "irradiance", 2 + X, b)
3206
+ }) : void 0,
3207
+ onProgress: (G, L) => mt(e == null ? void 0 : e.onProgress, "irradiance", G, L)
3208
+ }
3209
+ ), v = g.gbuffer;
3210
+ if (mt(e == null ? void 0 : e.onProgress, "denoise", 0, 1), (e == null ? void 0 : e.denoise) !== !1 && Ft(r, i, {
3211
+ gbuffer: v,
3212
+ stepSizes: [1],
3213
+ sigmaSpatial: 0.9,
3214
+ sigmaRange: 0.7,
3215
+ planeSigma: 0.06
3216
+ }), e != null && e.denoiseHdr) {
3217
+ if ((Y = e.signal) != null && Y.aborted)
3218
+ throw new DOMException("Bake aborted", "AbortError");
3219
+ try {
3220
+ const G = await e.denoiseHdr(r, i, e.signal, {
3221
+ albedo: v.albedo,
3222
+ normal: v.normal
3223
+ });
3224
+ if (G && G.length === r.length)
3225
+ for (let L = 0; L < r.length; L += 4)
3226
+ (r[L + 3] ?? 0) > 0 && (r[L] = Math.max(0, G[L] ?? 0), r[L + 1] = Math.max(0, G[L + 1] ?? 0), r[L + 2] = Math.max(0, G[L + 2] ?? 0));
3227
+ } catch {
3228
+ }
3229
+ }
3230
+ mt(e == null ? void 0 : e.onProgress, "denoise", 1, 1), mt(e == null ? void 0 : e.onProgress, "dilate", 0, 1), qt(r, i, 3), mt(e == null ? void 0 : e.onProgress, "dilate", 1, 1), mt(e == null ? void 0 : e.onProgress, "encode", 0, 1);
3231
+ const A = Xe(r, i, i), M = [A], p = [], y = [];
3232
+ if (e != null && e.device) {
3233
+ const G = e.device.createTexture({
3234
+ label: "lightmap_atlas_0",
3235
+ size: { width: i, height: i },
3236
+ format: "rgb9e5ufloat",
3237
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
3238
+ }), L = Uint8Array.from(
3239
+ new Uint8Array(A.buffer, A.byteOffset, A.byteLength)
3240
+ );
3241
+ e.device.queue.writeTexture(
3242
+ { texture: G },
3243
+ L,
3244
+ { bytesPerRow: i * 4 },
3245
+ { width: i, height: i }
3246
+ ), p.push(G);
3247
+ const V = "lightmap_atlas_0";
3248
+ y.push(V), a.registerTexture(V, G);
3249
+ } else
3250
+ y.push("lightmap_atlas_0");
3251
+ mt(e == null ? void 0 : e.onProgress, "encode", 1, 1), mt(e == null ? void 0 : e.onProgress, "probes", 0, 1);
3252
+ const O = Ir(t), k = Tr(O, l, o, {
3253
+ accel: g.accel,
3254
+ atlasRgba: r,
3255
+ atlasSize: i,
3256
+ skyRadiance: e == null ? void 0 : e.skyRadiance
3257
+ }), U = Br(t, a, {
3258
+ accel: g.accel,
3259
+ atlasRgba: r,
3260
+ atlasSize: i,
3261
+ ambient: l,
3262
+ skyRadiance: e == null ? void 0 : e.skyRadiance,
3263
+ device: c
3264
+ });
3265
+ mt(e == null ? void 0 : e.onProgress, "probes", 1, 1);
3266
+ const D = {};
3267
+ for (const G of s.entities) {
3268
+ const L = t.getComponent(G.entity, Tt);
3269
+ D[`e_${G.entity}`] = {
3270
+ atlasIndex: (L == null ? void 0 : L.atlasIndex) ?? 0,
3271
+ uvScaleOffset: (L == null ? void 0 : L.uvScaleOffset) ?? [1, 1, 0, 0]
3272
+ };
3273
+ }
3274
+ const H = {
3275
+ extVersion: $e,
3276
+ settings: n,
3277
+ atlasTextureIds: y,
3278
+ atlasPagesBase64: M.map((G) => Qe(G)),
3279
+ bakedEntities: D,
3280
+ shVolumes: [k],
3281
+ bakedReflectionProbes: U
3282
+ };
3283
+ return se(), mt(e == null ? void 0 : e.onProgress, "done", 1, 1), { payload: H, atlasPages: M, atlasSize: i, atlasTextures: p };
3284
+ }
3285
+ function Wr(t, a, e) {
3286
+ var l, o, h, m, f, u, c;
3287
+ (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);
3288
+ const n = /* @__PURE__ */ new Set(["lightmap_atlas_0"]);
3289
+ for (const d of ((f = (m = e == null ? void 0 : e.state) == null ? void 0 : m.lastPayload) == null ? void 0 : f.atlasTextureIds) ?? [])
3290
+ d && n.add(d);
3291
+ for (const d of ((c = (u = e == null ? void 0 : e.state) == null ? void 0 : u.lastPayload) == null ? void 0 : c.bakedReflectionProbes) ?? [])
3292
+ for (const x of d.faceTextureIds)
3293
+ x && n.add(x);
3294
+ if (a && typeof a.unregisterTexture == "function")
3295
+ for (const d of n)
3296
+ a.unregisterTexture(d, { destroy: !0 });
3297
+ e != null && e.state && (e.state.lastPayload = null);
3298
+ const s = [];
3299
+ for (const [d] of t.query([Tt]).iter())
3300
+ s.push(d);
3301
+ for (const d of s)
3302
+ t.removeComponent(d, Tt);
3303
+ const i = /* @__PURE__ */ new Set();
3304
+ let r = 0;
3305
+ for (const [, [d]] of t.query([kt]).iter()) {
3306
+ const x = d == null ? void 0 : d.mesh;
3307
+ !x || typeof x == "string" || i.has(x) || (i.add(x), Yn(x) && (r++, a == null || a.uploadMesh(x)));
3308
+ }
3309
+ return se(), {
3310
+ clearedEntities: s.length,
3311
+ restoredMeshes: r
3312
+ };
3313
+ }
3314
+ function Nr(t, a, e) {
3315
+ var r;
3316
+ const n = ((r = e.settings) == null ? void 0 : r.atlasSize) ?? 0, s = e.atlasPagesBase64 ?? [], i = [];
3317
+ if (!n || s.length === 0)
3318
+ return { textures: i, atlasSize: n };
3319
+ for (let l = 0; l < s.length; l++) {
3320
+ const o = er(s[l]), h = n * n;
3321
+ if (o.length !== h)
3322
+ throw new Error(`lightmap atlas page ${l}: expected ${h} u32, got ${o.length}`);
3323
+ const m = e.atlasTextureIds[l] ?? `lightmap_atlas_${l}`;
3324
+ typeof a.unregisterTexture == "function" && a.unregisterTexture(m, { destroy: !0 });
3325
+ const f = t.createTexture({
3326
+ label: m,
3327
+ size: { width: n, height: n },
3328
+ format: "rgb9e5ufloat",
3329
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
3330
+ }), u = Uint8Array.from(
3331
+ new Uint8Array(o.buffer, o.byteOffset, o.byteLength)
3332
+ );
3333
+ t.queue.writeTexture(
3334
+ { texture: f },
3335
+ u,
3336
+ { bytesPerRow: n * 4 },
3337
+ { width: n, height: n }
3338
+ ), a.registerTexture(m, f), i.push(f);
3339
+ }
3340
+ return { textures: i, atlasSize: n };
3341
+ }
3342
+ function Xr() {
3343
+ return { lastPayload: null };
3344
+ }
3345
+ function mn(t) {
3346
+ if (!t || typeof t != "object") return !1;
3347
+ const a = t;
3348
+ return a.extVersion === $e && typeof a.settings == "object";
3349
+ }
3350
+ function $r(t) {
3351
+ return {
3352
+ id: Ln,
3353
+ serialize: (a) => {
3354
+ if (t.lastPayload) {
3355
+ if (a.entityToId && t.lastPayload.bakedEntities) {
3356
+ const e = {};
3357
+ for (const [n, s] of a.entityToId) {
3358
+ const i = a.world.getComponent(n, Tt);
3359
+ i && (e[s] = {
3360
+ atlasIndex: i.atlasIndex,
3361
+ uvScaleOffset: [...i.uvScaleOffset]
3362
+ });
3363
+ }
3364
+ return {
3365
+ ...t.lastPayload,
3366
+ bakedEntities: Object.keys(e).length > 0 ? e : t.lastPayload.bakedEntities
3367
+ };
3368
+ }
3369
+ return t.lastPayload;
3370
+ }
3371
+ },
3372
+ deserialize: async (a, e) => {
3373
+ var l, o, h, m, f, u, c;
3374
+ if (!mn(a)) return;
3375
+ t.lastPayload = a;
3376
+ const n = e.params.world;
3377
+ for (const [d, x] of Object.entries(a.bakedEntities)) {
3378
+ let b = (l = e.entityMap) == null ? void 0 : l.get(d);
3379
+ if (b == null && d.startsWith("e_") && (b = Number(d.slice(2))), b == null || !Number.isFinite(b)) continue;
3380
+ const g = n.getComponent(b, Tt);
3381
+ g ? (g.atlasIndex = x.atlasIndex, g.uvScaleOffset = [...x.uvScaleOffset]) : n.addComponent(
3382
+ b,
3383
+ new Tt(x.atlasIndex, [...x.uvScaleOffset])
3384
+ );
3385
+ const v = n.getComponent(b, kt);
3386
+ v != null && v.mesh && typeof v.mesh != "string" && (v.mesh.bakedGeometryDirty = !0, v.mesh.lightmapBakeRevision = Math.max(
3387
+ 1,
3388
+ v.mesh.lightmapBakeRevision ?? 0
3389
+ ));
3390
+ }
3391
+ const s = (h = (o = e.params.engine) == null ? void 0 : o.webGPUContext) == null ? void 0 : h.device, i = e.params.resourceManager, r = (m = e.params.engine) == null ? void 0 : m.renderer;
3392
+ if (s && i && r && ((f = a.atlasPagesBase64) != null && f.length)) {
3393
+ const { textures: d } = Nr(s, i, a);
3394
+ d[0] && r.setLightmapAtlas(d[0], 1);
3395
+ }
3396
+ if (r && typeof r.setLightmapShVolume == "function") {
3397
+ const d = ((u = a.shVolumes) == null ? void 0 : u[0]) ?? null;
3398
+ r.setLightmapShVolume(d ? Pr(d, 1) : null);
3399
+ }
3400
+ se(), Tn("bakedInterior"), typeof ((c = e.params.engine) == null ? void 0 : c.reloadGameRenderGraph) == "function" && await e.params.engine.reloadGameRenderGraph();
3401
+ }
3402
+ };
3403
+ }
3404
+ function jr(t) {
3405
+ return mn(t) ? t : void 0;
3406
+ }
3407
+ export {
3408
+ Rr as BAKE_GI_BOUNCE_COMPUTE_WGSL,
3409
+ Cr as BAKE_LIGHTMAP_COMPUTE_WGSL,
3410
+ Hr as BAKE_LIGHT_FLOATS,
3411
+ Ln as LIGHTMAPPER_EXTENSION_ID,
3412
+ $e as LIGHTMAPPER_EXT_VERSION,
3413
+ je as LIGHTMAP_ATLAS_PADDING,
3414
+ we as LIGHTMAP_ATLAS_SIZE,
3415
+ Ke as LIGHTMAP_DEFAULT_BOUNCES,
3416
+ Nn as LIGHTMAP_DEFAULT_SAMPLES,
3417
+ zn as LIGHTMAP_TILE_SIZE,
3418
+ ln as SH_PROBE_VOLUME_MAX_PROBES,
3419
+ Mr as SH_PROBE_VOLUME_UNIFORM_FLOATS,
3420
+ vr as bakeCpuIrradianceAtlas,
3421
+ Yr as bakeLighting,
3422
+ Se as bakeRayClosestHit,
3423
+ Ae as bakeRayOccluded,
3424
+ Br as bakeReflectionProbesPt,
3425
+ Tr as bakeShVolumeCpu,
3426
+ er as base64ToUint32,
3427
+ ir as buildBakeSceneAccel,
3428
+ yr as buildUvGBuffer,
3429
+ Wr as clearLightingBake,
3430
+ Gn as collectBakeCandidateEntities,
3431
+ br as collectBakeLights,
3432
+ ar as computeVertexDiffuseAlbedos,
3433
+ Xr as createLightmapperRuntimeState,
3434
+ $r as createLightmapperSceneExtensionHandler,
3435
+ Ur as decodeShVolumeCoefficients,
3436
+ Ft as denoiseAtlas,
3437
+ qt as dilateAtlas,
3438
+ Fr as ensurePlanarUv1,
3439
+ Fn as ensureStaticTags,
3440
+ un as evalBakeIrradiance,
3441
+ Me as evalShL2Basis,
3442
+ Sr as evalShL2ProbeIrradiance,
3443
+ Vr as evalShVolumeIrradiance,
3444
+ oe as hashSeedToUnitPair,
3445
+ Xt as hashTexel2d,
3446
+ cn as makeCanonicalTangent,
3447
+ Or as packBakeLightsGpu,
3448
+ nn as packBakeSceneAccelGpu,
3449
+ jn as packMeshesIntoAtlas,
3450
+ Pr as packShVolumeUniformF32,
3451
+ jr as parseLightmapperScenePayload,
3452
+ Ir as placeAutoProbeVolume,
3453
+ Qn as prepareAtlas,
3454
+ Nr as restoreLightmapAtlasFromPayload,
3455
+ Dr as runGpuBakeDirect,
3456
+ Lr as runGpuBakeGiBounces,
3457
+ lr as sampleContactAo,
3458
+ cr as sampleEmissiveCatalogDirect,
3459
+ he as sampleIndirectBounce,
3460
+ Je as sampleSrgbImageLinear,
3461
+ de as softLightVisibility,
3462
+ Qe as uint32ToBase64,
3463
+ re as updateBounceSrcFromAtlas,
3464
+ qr as writeChartBounceSrc
3465
+ };