three-realtime-rt 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,424 +1,447 @@
1
- import * as THREE from "three";
2
- import { mergeGeometries } from "three/addons/utils/BufferGeometryUtils.js";
3
- import {
4
- MeshBVH,
5
- MeshBVHUniformStruct,
6
- FloatVertexAttributeTexture,
7
- SAH,
8
- CENTER,
9
- } from "three-mesh-bvh";
10
- import { decodeBlueNoise, BLUE_NOISE_SIZE } from "./blueNoise.js";
11
-
12
- const MAX_LIGHTS = 16; // stage-1 cap; a data-texture light list is future work
13
-
14
- // Emissive-mesh triangles sampled by next-event estimation. Beyond the cap the
15
- // largest-area triangles win (they carry the most light); the rest are dropped
16
- // from the light list with a warning.
17
- const MAX_EMISSIVE_TRIS = 256;
18
-
19
- /**
20
- * A two-level BVH scene. Static geometry lives in one BVH uploaded to the GPU
21
- * ONCE; dynamic (moving) meshes live in a second, small BVH that is re-baked and
22
- * re-uploaded every frame. The lighting shader traces both and takes the nearest
23
- * hit, so the per-frame cost scales with the *moving* triangle count only — not
24
- * the size of the static world. (This is the TLAS/BLAS idea, minus per-instance
25
- * transforms: one merged dynamic mesh is plenty for typical rigid-body scenes.)
26
- */
27
- export class CompiledScene {
28
- constructor() {
29
- // Static level (built once). Per level, one packed RGBA attribute texture
30
- // (normal.xyz + materialIndex in .w): two BVH structs already cost 8 of the
31
- // 16 guaranteed fragment samplers, so attributes must stay lean.
32
- this.staticBvh = null;
33
- this.staticBvhUniform = new MeshBVHUniformStruct();
34
- this.staticAttrTex = new FloatVertexAttributeTexture();
35
-
36
- // Dynamic level (re-baked/refit each frame).
37
- this.dynamicBvh = null;
38
- this.dynamicBvhUniform = new MeshBVHUniformStruct();
39
- this.dynamicAttrTex = new FloatVertexAttributeTexture();
40
- this.dynamicMerged = null;
41
- this.dynamicPacked = null; // Float32Array + BufferAttribute for re-baking
42
- this.dynamicPackedAttr = null;
43
- this.dynamic = []; // [{ mesh, start, count, localPos, localNorm }]
44
- this.hasDynamic = false;
45
-
46
- this.materialsTex = null;
47
- this.materials = [];
48
- this.lightPosType = [];
49
- this.lightColorRadius = [];
50
- this.lightCount = 0;
51
- this.emissiveTriCount = 0;
52
- this.triangleCount = 0;
53
-
54
- this._m3 = new THREE.Matrix3();
55
- this._normalFrame = 0;
56
- this._dynBuildVolume = null; // world-volume of the dynamic set at build time
57
- }
58
-
59
- /**
60
- * Re-bake the dynamic meshes' current world transforms into the dynamic
61
- * geometry, refit the dynamic BVH, and re-upload ONLY the (small) dynamic
62
- * textures. The static BVH is never touched. Call once per frame after moving
63
- * the meshes.
64
- */
65
- updateDynamic() {
66
- if (!this.hasDynamic || this.dynamic.length === 0) return;
67
- const posAttr = this.dynamicMerged.getAttribute("position");
68
- const pos = posAttr.array;
69
- const packed = this.dynamicPacked; // normal.xyz + matIndex.w per vertex
70
- let minX = Infinity, minY = Infinity, minZ = Infinity;
71
- let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
72
-
73
- for (const seg of this.dynamic) {
74
- seg.mesh.updateWorldMatrix(true, false);
75
- const m = seg.mesh.matrixWorld.elements;
76
- const nm = this._m3.getNormalMatrix(seg.mesh.matrixWorld).elements;
77
- const lp = seg.localPos;
78
- const ln = seg.localNorm;
79
- let o = seg.start * 3;
80
- let p = seg.start * 4;
81
- for (let i = 0; i < seg.count; i++) {
82
- const x = lp[i * 3], y = lp[i * 3 + 1], z = lp[i * 3 + 2];
83
- const wx = m[0] * x + m[4] * y + m[8] * z + m[12];
84
- const wy = m[1] * x + m[5] * y + m[9] * z + m[13];
85
- const wz = m[2] * x + m[6] * y + m[10] * z + m[14];
86
- pos[o] = wx;
87
- pos[o + 1] = wy;
88
- pos[o + 2] = wz;
89
- if (wx < minX) minX = wx; if (wx > maxX) maxX = wx;
90
- if (wy < minY) minY = wy; if (wy > maxY) maxY = wy;
91
- if (wz < minZ) minZ = wz; if (wz > maxZ) maxZ = wz;
92
- const nx = ln[i * 3], ny = ln[i * 3 + 1], nz = ln[i * 3 + 2];
93
- const tx = nm[0] * nx + nm[3] * ny + nm[6] * nz;
94
- const ty = nm[1] * nx + nm[4] * ny + nm[7] * nz;
95
- const tz = nm[2] * nx + nm[5] * ny + nm[8] * nz;
96
- const il = 1.0 / (Math.hypot(tx, ty, tz) || 1);
97
- packed[p] = tx * il;
98
- packed[p + 1] = ty * il;
99
- packed[p + 2] = tz * il;
100
- // packed[p + 3] (matIndex) never changes
101
- o += 3;
102
- p += 4;
103
- }
104
- }
105
- posAttr.needsUpdate = true;
106
-
107
- // refit() keeps the tree TOPOLOGY from build time. While the props sit in
108
- // a pile that's fine but once they scatter (an explosion), triangles
109
- // that are tree-neighbors end up across the room, refitted nodes balloon
110
- // into huge overlapping boxes, and every ray wades through them (observed:
111
- // 30 fps 10 on mobile). When the set has spread well past its
112
- // build-time volume, rebuild the tree from scratch instead a few ms,
113
- // paid only on large redistributions.
114
- const vol =
115
- Math.max(maxX - minX, 1e-6) *
116
- Math.max(maxY - minY, 1e-6) *
117
- Math.max(maxZ - minZ, 1e-6);
118
- if (this._dynBuildVolume == null) this._dynBuildVolume = vol;
119
- if (vol > this._dynBuildVolume * 3 || vol < this._dynBuildVolume / 3) {
120
- this.dynamicBvh = new MeshBVH(this.dynamicMerged, { strategy: CENTER });
121
- this._dynBuildVolume = vol;
122
- } else {
123
- this.dynamicBvh.refit();
124
- }
125
- this.dynamicBvhUniform.updateFrom(this.dynamicBvh);
126
- // Normals only feed GI-bounce shading off movers — amortize their upload.
127
- if (this._normalFrame++ % 8 === 0) {
128
- this.dynamicAttrTex.updateFrom(this.dynamicPackedAttr);
129
- }
130
- }
131
-
132
- dispose() {
133
- this.staticBvhUniform.dispose();
134
- this.staticAttrTex.dispose();
135
- this.dynamicBvhUniform.dispose();
136
- this.dynamicAttrTex.dispose();
137
- if (this.materialsTex) this.materialsTex.dispose();
138
- if (this.staticBvh) this.staticBvh.geometry.dispose();
139
- if (this.dynamicMerged) this.dynamicMerged.dispose();
140
- }
141
- }
142
-
143
- function extractMeshGeometry(mesh, materialIndex) {
144
- const src = mesh.geometry.index
145
- ? mesh.geometry.toNonIndexed()
146
- : mesh.geometry.clone();
147
-
148
- if (!src.getAttribute("normal")) src.computeVertexNormals();
149
- const localPos = src.getAttribute("position").array.slice();
150
- const localNorm = src.getAttribute("normal").array.slice();
151
-
152
- const geo = new THREE.BufferGeometry();
153
- geo.setAttribute("position", src.getAttribute("position").clone());
154
- geo.setAttribute("normal", src.getAttribute("normal").clone());
155
- geo.applyMatrix4(mesh.matrixWorld); // bake world transform
156
-
157
- const count = geo.getAttribute("position").count;
158
- const mi = new Float32Array(count).fill(materialIndex);
159
- geo.setAttribute("materialIndex", new THREE.BufferAttribute(mi, 1));
160
- return { geo, localPos, localNorm, count };
161
- }
162
-
163
- // Effective emissive colour (already scaled by intensity), or null if the
164
- // material doesn't emit. Matches the shading table's emissiveMap exclusion.
165
- function emissiveColor(mat) {
166
- if (mat.emissiveMap != null || !mat.emissive) return null;
167
- const i = mat.emissiveIntensity ?? 1;
168
- if (i <= 0 || mat.emissive.r + mat.emissive.g + mat.emissive.b <= 0) return null;
169
- return [mat.emissive.r * i, mat.emissive.g * i, mat.emissive.b * i];
170
- }
171
-
172
- // Row 0: materials, 2 texels each (albedo+rough, emissive+metal).
173
- // Row 1: emissive triangles for NEE, 4 texels each:
174
- // [v0.xyz | area] [e1.xyz | emit.r] [e2.xyz | emit.g] [n.xyz | emit.b]
175
- // Rows 2..65: a 64x64 RGBA blue-noise tile for low-discrepancy sampling.
176
- // All packed into ONE texture because the lighting pass already sits at the
177
- // WebGL2-guaranteed 16-sampler limit extra samplers are not available.
178
- function buildSceneDataTexture(materials, emissiveTris) {
179
- const bn = decodeBlueNoise();
180
- const width = Math.max(materials.length * 2, emissiveTris.length * 4, BLUE_NOISE_SIZE);
181
- const height = 2 + BLUE_NOISE_SIZE;
182
- const data = new Float32Array(width * height * 4);
183
- materials.forEach((mat, i) => {
184
- const o = i * 8;
185
- const color = mat.color ?? new THREE.Color(1, 1, 1);
186
- const emissive = emissiveColor(mat) ?? [0, 0, 0];
187
- data[o + 0] = color.r;
188
- data[o + 1] = color.g;
189
- data[o + 2] = color.b;
190
- data[o + 3] = mat.roughness ?? 1;
191
- data[o + 4] = emissive[0];
192
- data[o + 5] = emissive[1];
193
- data[o + 6] = emissive[2];
194
- data[o + 7] = mat.metalness ?? 0;
195
- });
196
- const row = width * 4;
197
- emissiveTris.forEach((t, i) => {
198
- const o = row + i * 16;
199
- data[o + 0] = t.v0[0]; data[o + 1] = t.v0[1]; data[o + 2] = t.v0[2]; data[o + 3] = t.area;
200
- data[o + 4] = t.e1[0]; data[o + 5] = t.e1[1]; data[o + 6] = t.e1[2]; data[o + 7] = t.emit[0];
201
- data[o + 8] = t.e2[0]; data[o + 9] = t.e2[1]; data[o + 10] = t.e2[2]; data[o + 11] = t.emit[1];
202
- data[o + 12] = t.n[0]; data[o + 13] = t.n[1]; data[o + 14] = t.n[2]; data[o + 15] = t.emit[2];
203
- });
204
- for (let y = 0; y < BLUE_NOISE_SIZE; y++) {
205
- const o = (2 + y) * row;
206
- const src = y * BLUE_NOISE_SIZE * 4;
207
- for (let i = 0; i < BLUE_NOISE_SIZE * 4; i++) {
208
- data[o + i] = (bn[src + i] + 0.5) / 256.0;
209
- }
210
- }
211
- const tex = new THREE.DataTexture(data, width, height, THREE.RGBAFormat, THREE.FloatType);
212
- tex.minFilter = THREE.NearestFilter;
213
- tex.magFilter = THREE.NearestFilter;
214
- tex.needsUpdate = true;
215
- return tex;
216
- }
217
-
218
- // Collect world-space triangles of an emissive mesh for the NEE light list.
219
- // `geo` is already non-indexed and world-baked by extractMeshGeometry.
220
- function collectEmissiveTriangles(geo, emit, out) {
221
- const pos = geo.getAttribute("position").array;
222
- for (let i = 0; i + 8 < pos.length; i += 9) {
223
- const e1 = [pos[i + 3] - pos[i], pos[i + 4] - pos[i + 1], pos[i + 5] - pos[i + 2]];
224
- const e2 = [pos[i + 6] - pos[i], pos[i + 7] - pos[i + 1], pos[i + 8] - pos[i + 2]];
225
- const cx = e1[1] * e2[2] - e1[2] * e2[1];
226
- const cy = e1[2] * e2[0] - e1[0] * e2[2];
227
- const cz = e1[0] * e2[1] - e1[1] * e2[0];
228
- const len = Math.hypot(cx, cy, cz);
229
- if (len < 1e-10) continue; // degenerate
230
- out.push({
231
- v0: [pos[i], pos[i + 1], pos[i + 2]],
232
- e1,
233
- e2,
234
- n: [cx / len, cy / len, cz / len],
235
- area: len * 0.5,
236
- emit,
237
- });
238
- }
239
- }
240
-
241
- // A single degenerate triangle so the dynamic BVH textures are always valid even
242
- // when there are no dynamic meshes (tracing is gated by a uHasDynamic flag).
243
- function degenerateGeometry() {
244
- const geo = new THREE.BufferGeometry();
245
- geo.setAttribute("position", new THREE.BufferAttribute(new Float32Array(9), 3));
246
- geo.setAttribute("normal", new THREE.BufferAttribute(new Float32Array([0, 1, 0, 0, 1, 0, 0, 1, 0]), 3));
247
- geo.setAttribute("materialIndex", new THREE.BufferAttribute(new Float32Array(3), 1));
248
- return geo;
249
- }
250
-
251
- // Pack normal.xyz + materialIndex.w into one itemSize-4 attribute so each BVH
252
- // level costs a single sampler for its per-vertex data.
253
- function packAttributes(merged) {
254
- const norm = merged.getAttribute("normal");
255
- const matIdx = merged.getAttribute("materialIndex");
256
- const count = norm.count;
257
- const packed = new Float32Array(count * 4);
258
- for (let i = 0; i < count; i++) {
259
- packed[i * 4] = norm.getX(i);
260
- packed[i * 4 + 1] = norm.getY(i);
261
- packed[i * 4 + 2] = norm.getZ(i);
262
- packed[i * 4 + 3] = matIdx.getX(i);
263
- }
264
- return { packed, attr: new THREE.BufferAttribute(packed, 4) };
265
- }
266
-
267
- // Build one BVH level (merge geometries, upload BVH + attribute textures).
268
- function buildLevel(geometries, { dynamic }) {
269
- const merged =
270
- geometries.length > 0 ? mergeGeometries(geometries, false) : degenerateGeometry();
271
- const bvh = new MeshBVH(merged, { strategy: dynamic ? CENTER : SAH });
272
- return { merged, bvh, ...packAttributes(merged) };
273
- }
274
-
275
- export function compileScene(scene, options = {}) {
276
- scene.updateMatrixWorld(true);
277
- const dynamicSet = options.dynamicMeshes ? new Set(options.dynamicMeshes) : null;
278
-
279
- const compiled = new CompiledScene();
280
- const materials = compiled.materials;
281
- const staticGeoms = [];
282
- const dynamicGeoms = [];
283
- const emissiveTris = [];
284
- let dynVertexOffset = 0;
285
- const tmpGeoms = []; // to dispose after merge
286
-
287
- scene.traverse((obj) => {
288
- if (!obj.isMesh || !obj.geometry || !obj.visible || obj.userData.rtExclude) return;
289
- const mat = Array.isArray(obj.material) ? obj.material[0] : obj.material;
290
- // Transparent surfaces must not act as opaque occluders — e.g.
291
- // LittlestTokyo's glass display case (texture-alpha, opacity 1) would put
292
- // the whole model in shadow. Alpha-textured glass can't be cheaply tested,
293
- // so ANY transparent material is skipped like rtExclude (still
294
- // rasterized). alphaTest cut-outs (transparent: false) stay occluders.
295
- if (mat.transparent) return;
296
- let mi = materials.indexOf(mat);
297
- if (mi < 0) { mi = materials.length; materials.push(mat); }
298
-
299
- const extracted = extractMeshGeometry(obj, mi);
300
- tmpGeoms.push(extracted.geo);
301
- // Static emissive meshes become NEE area lights (sampled directly with
302
- // shadow rays instead of waiting for a GI ray to stumble into them).
303
- // Dynamic emitters are left out their world-space triangles would go
304
- // stale so they keep lighting the old way, via GI-ray hits.
305
- const isDynamic = dynamicSet && dynamicSet.has(obj);
306
- if (!isDynamic) {
307
- const emit = emissiveColor(mat);
308
- if (emit) collectEmissiveTriangles(extracted.geo, emit, emissiveTris);
309
- }
310
- if (isDynamic) {
311
- dynamicGeoms.push(extracted.geo);
312
- compiled.dynamic.push({
313
- mesh: obj,
314
- start: dynVertexOffset,
315
- count: extracted.count,
316
- localPos: extracted.localPos,
317
- localNorm: extracted.localNorm,
318
- });
319
- dynVertexOffset += extracted.count;
320
- } else {
321
- staticGeoms.push(extracted.geo);
322
- }
323
- });
324
-
325
- if (staticGeoms.length === 0 && dynamicGeoms.length === 0) {
326
- throw new Error("three-realtime-rt: no meshes found in scene");
327
- }
328
-
329
- // Static level.
330
- const s = buildLevel(staticGeoms, { dynamic: false });
331
- compiled.staticBvh = s.bvh;
332
- compiled.staticBvhUniform.updateFrom(s.bvh);
333
- compiled.staticAttrTex.updateFrom(s.attr);
334
-
335
- // Dynamic level.
336
- compiled.hasDynamic = dynamicGeoms.length > 0;
337
- const d = buildLevel(dynamicGeoms, { dynamic: true });
338
- compiled.dynamicMerged = d.merged;
339
- compiled.dynamicBvh = d.bvh;
340
- compiled.dynamicBvhUniform.updateFrom(d.bvh);
341
- compiled.dynamicPacked = d.packed;
342
- compiled.dynamicPackedAttr = d.attr;
343
- compiled.dynamicAttrTex.updateFrom(d.attr);
344
-
345
- compiled.triangleCount =
346
- (s.merged.getAttribute("position").count +
347
- (compiled.hasDynamic ? d.merged.getAttribute("position").count : 0)) / 3;
348
-
349
- // World-space extent of the static level. Used to auto-scale the ray offset
350
- // epsilon: dense/large scenes (e.g. a detailed diorama normalized to a few
351
- // units) need a bigger offset or every shadow ray self-intersects nearby
352
- // micro-geometry and the scene renders black.
353
- s.merged.computeBoundingBox();
354
- const bb = s.merged.boundingBox;
355
- compiled.sceneDiagonal = bb.isEmpty() ? 1 : bb.min.distanceTo(bb.max);
356
-
357
- if (emissiveTris.length > MAX_EMISSIVE_TRIS) {
358
- console.warn(
359
- `three-realtime-rt: ${emissiveTris.length} emissive triangles exceed the ` +
360
- `NEE cap of ${MAX_EMISSIVE_TRIS}; keeping the largest by area. Dropped ` +
361
- `triangles no longer act as lights prefer low-poly emitter meshes.`
362
- );
363
- emissiveTris.sort((a, b) => b.area - a.area);
364
- emissiveTris.length = MAX_EMISSIVE_TRIS;
365
- }
366
- compiled.emissiveTriCount = emissiveTris.length;
367
- compiled.materialsTex = buildSceneDataTexture(materials, emissiveTris);
368
- syncLights(scene, compiled);
369
-
370
- // Static merged geometry is owned by its BVH (disposed with it); dynamic
371
- // merged geometry is kept live for re-baking. Dispose the per-mesh temporaries
372
- // that aren't the merged buffers.
373
- for (const g of tmpGeoms) {
374
- if (g !== s.merged && g !== d.merged) g.dispose();
375
- }
376
- return compiled;
377
- }
378
-
379
- /** (Re)scan the scene's lights into the compiled light tables. Cheap; call anytime. */
380
- export function syncLights(scene, compiled) {
381
- const posType = compiled.lightPosType;
382
- const colorRadius = compiled.lightColorRadius;
383
- posType.length = 0;
384
- colorRadius.length = 0;
385
- let count = 0;
386
- const tmpP = new THREE.Vector3();
387
- const tmpT = new THREE.Vector3();
388
-
389
- scene.traverse((obj) => {
390
- if (!obj.isLight || !obj.visible || obj.intensity <= 0) return;
391
- if (count >= MAX_LIGHTS) return;
392
- if (obj.isPointLight) {
393
- obj.getWorldPosition(tmpP);
394
- posType.push(tmpP.x, tmpP.y, tmpP.z, 0);
395
- colorRadius.push(
396
- obj.color.r * obj.intensity,
397
- obj.color.g * obj.intensity,
398
- obj.color.b * obj.intensity,
399
- obj.userData.rtRadius ?? 0.15
400
- );
401
- count++;
402
- } else if (obj.isDirectionalLight) {
403
- obj.getWorldPosition(tmpP);
404
- obj.target.getWorldPosition(tmpT);
405
- const dir = tmpT.sub(tmpP).normalize();
406
- posType.push(dir.x, dir.y, dir.z, 1);
407
- colorRadius.push(
408
- obj.color.r * obj.intensity,
409
- obj.color.g * obj.intensity,
410
- obj.color.b * obj.intensity,
411
- obj.userData.rtRadius ?? 0.02
412
- );
413
- count++;
414
- }
415
- });
416
-
417
- compiled.lightCount = count;
418
- while (posType.length < MAX_LIGHTS * 4) {
419
- posType.push(0, 0, 0, 0);
420
- colorRadius.push(0, 0, 0, 0);
421
- }
422
- }
423
-
424
- export { MAX_LIGHTS };
1
+ import * as THREE from "three";
2
+ import { mergeGeometries } from "three/addons/utils/BufferGeometryUtils.js";
3
+ import {
4
+ MeshBVH,
5
+ MeshBVHUniformStruct,
6
+ FloatVertexAttributeTexture,
7
+ SAH,
8
+ CENTER,
9
+ } from "three-mesh-bvh";
10
+ import { decodeBlueNoise, BLUE_NOISE_SIZE } from "./blueNoise.js";
11
+
12
+ const MAX_LIGHTS = 32; // stage-1 cap; a data-texture light list is future work
13
+
14
+ // Emissive-mesh triangles sampled by next-event estimation. Beyond the cap the
15
+ // largest-area triangles win (they carry the most light); the rest are dropped
16
+ // from the light list with a warning.
17
+ const MAX_EMISSIVE_TRIS = 256;
18
+
19
+ /**
20
+ * A two-level BVH scene. Static geometry lives in one BVH uploaded to the GPU
21
+ * ONCE; dynamic (moving) meshes live in a second, small BVH that is re-baked and
22
+ * re-uploaded every frame. The lighting shader traces both and takes the nearest
23
+ * hit, so the per-frame cost scales with the *moving* triangle count only — not
24
+ * the size of the static world. (This is the TLAS/BLAS idea, minus per-instance
25
+ * transforms: one merged dynamic mesh is plenty for typical rigid-body scenes.)
26
+ */
27
+ export class CompiledScene {
28
+ constructor() {
29
+ // Static level (built once). Per level, one packed RGBA attribute texture
30
+ // (normal.xyz + materialIndex in .w): two BVH structs already cost 8 of the
31
+ // 16 guaranteed fragment samplers, so attributes must stay lean.
32
+ this.staticBvh = null;
33
+ this.staticBvhUniform = new MeshBVHUniformStruct();
34
+ this.staticAttrTex = new FloatVertexAttributeTexture();
35
+
36
+ // Dynamic level (re-baked/refit each frame).
37
+ this.dynamicBvh = null;
38
+ this.dynamicBvhUniform = new MeshBVHUniformStruct();
39
+ this.dynamicAttrTex = new FloatVertexAttributeTexture();
40
+ this.dynamicMerged = null;
41
+ this.dynamicPacked = null; // Float32Array + BufferAttribute for re-baking
42
+ this.dynamicPackedAttr = null;
43
+ this.dynamic = []; // [{ mesh, start, count, localPos, localNorm }]
44
+ this.hasDynamic = false;
45
+
46
+ this.materialsTex = null;
47
+ this.materials = [];
48
+ this.lightPosType = [];
49
+ this.lightColorRadius = [];
50
+ this.lightDirCone = []; // spot direction.xyz + cos(outer angle)
51
+ this.lightCount = 0;
52
+ this.emissiveTriCount = 0;
53
+ this.triangleCount = 0;
54
+
55
+ this._m3 = new THREE.Matrix3();
56
+ this._normalFrame = 0;
57
+ this._dynBuildVolume = null; // world-volume of the dynamic set at build time
58
+ }
59
+
60
+ /**
61
+ * Re-bake the dynamic meshes' current world transforms into the dynamic
62
+ * geometry, refit the dynamic BVH, and re-upload ONLY the (small) dynamic
63
+ * textures. The static BVH is never touched. Call once per frame after moving
64
+ * the meshes.
65
+ */
66
+ updateDynamic() {
67
+ if (!this.hasDynamic || this.dynamic.length === 0) return;
68
+ const posAttr = this.dynamicMerged.getAttribute("position");
69
+ const pos = posAttr.array;
70
+ const packed = this.dynamicPacked; // normal.xyz + matIndex.w per vertex
71
+ let minX = Infinity, minY = Infinity, minZ = Infinity;
72
+ let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
73
+
74
+ for (const seg of this.dynamic) {
75
+ seg.mesh.updateWorldMatrix(true, false);
76
+ const m = seg.mesh.matrixWorld.elements;
77
+ const nm = this._m3.getNormalMatrix(seg.mesh.matrixWorld).elements;
78
+ const lp = seg.localPos;
79
+ const ln = seg.localNorm;
80
+ let o = seg.start * 3;
81
+ let p = seg.start * 4;
82
+ for (let i = 0; i < seg.count; i++) {
83
+ const x = lp[i * 3], y = lp[i * 3 + 1], z = lp[i * 3 + 2];
84
+ const wx = m[0] * x + m[4] * y + m[8] * z + m[12];
85
+ const wy = m[1] * x + m[5] * y + m[9] * z + m[13];
86
+ const wz = m[2] * x + m[6] * y + m[10] * z + m[14];
87
+ pos[o] = wx;
88
+ pos[o + 1] = wy;
89
+ pos[o + 2] = wz;
90
+ if (wx < minX) minX = wx; if (wx > maxX) maxX = wx;
91
+ if (wy < minY) minY = wy; if (wy > maxY) maxY = wy;
92
+ if (wz < minZ) minZ = wz; if (wz > maxZ) maxZ = wz;
93
+ const nx = ln[i * 3], ny = ln[i * 3 + 1], nz = ln[i * 3 + 2];
94
+ const tx = nm[0] * nx + nm[3] * ny + nm[6] * nz;
95
+ const ty = nm[1] * nx + nm[4] * ny + nm[7] * nz;
96
+ const tz = nm[2] * nx + nm[5] * ny + nm[8] * nz;
97
+ const il = 1.0 / (Math.hypot(tx, ty, tz) || 1);
98
+ packed[p] = tx * il;
99
+ packed[p + 1] = ty * il;
100
+ packed[p + 2] = tz * il;
101
+ // packed[p + 3] (matIndex) never changes
102
+ o += 3;
103
+ p += 4;
104
+ }
105
+ }
106
+ posAttr.needsUpdate = true;
107
+
108
+ // refit() keeps the tree TOPOLOGY from build time. While the props sit in
109
+ // a pile that's fine but once they scatter (an explosion), triangles
110
+ // that are tree-neighbors end up across the room, refitted nodes balloon
111
+ // into huge overlapping boxes, and every ray wades through them (observed:
112
+ // 30 fps 10 on mobile). When the set has spread well past its
113
+ // build-time volume, rebuild the tree from scratch instead — a few ms,
114
+ // paid only on large redistributions.
115
+ const vol =
116
+ Math.max(maxX - minX, 1e-6) *
117
+ Math.max(maxY - minY, 1e-6) *
118
+ Math.max(maxZ - minZ, 1e-6);
119
+ if (this._dynBuildVolume == null) this._dynBuildVolume = vol;
120
+ if (vol > this._dynBuildVolume * 3 || vol < this._dynBuildVolume / 3) {
121
+ this.dynamicBvh = new MeshBVH(this.dynamicMerged, { strategy: CENTER });
122
+ this._dynBuildVolume = vol;
123
+ } else {
124
+ this.dynamicBvh.refit();
125
+ }
126
+ this.dynamicBvhUniform.updateFrom(this.dynamicBvh);
127
+ // Normals only feed GI-bounce shading off movers — amortize their upload.
128
+ if (this._normalFrame++ % 8 === 0) {
129
+ this.dynamicAttrTex.updateFrom(this.dynamicPackedAttr);
130
+ }
131
+ }
132
+
133
+ dispose() {
134
+ this.staticBvhUniform.dispose();
135
+ this.staticAttrTex.dispose();
136
+ this.dynamicBvhUniform.dispose();
137
+ this.dynamicAttrTex.dispose();
138
+ if (this.materialsTex) this.materialsTex.dispose();
139
+ if (this.staticBvh) this.staticBvh.geometry.dispose();
140
+ if (this.dynamicMerged) this.dynamicMerged.dispose();
141
+ }
142
+ }
143
+
144
+ function extractMeshGeometry(mesh, materialIndex) {
145
+ const src = mesh.geometry.index
146
+ ? mesh.geometry.toNonIndexed()
147
+ : mesh.geometry.clone();
148
+
149
+ if (!src.getAttribute("normal")) src.computeVertexNormals();
150
+ const localPos = src.getAttribute("position").array.slice();
151
+ const localNorm = src.getAttribute("normal").array.slice();
152
+
153
+ const geo = new THREE.BufferGeometry();
154
+ geo.setAttribute("position", src.getAttribute("position").clone());
155
+ geo.setAttribute("normal", src.getAttribute("normal").clone());
156
+ geo.applyMatrix4(mesh.matrixWorld); // bake world transform
157
+
158
+ const count = geo.getAttribute("position").count;
159
+ const mi = new Float32Array(count).fill(materialIndex);
160
+ geo.setAttribute("materialIndex", new THREE.BufferAttribute(mi, 1));
161
+ return { geo, localPos, localNorm, count };
162
+ }
163
+
164
+ // Effective emissive colour (already scaled by intensity), or null if the
165
+ // material doesn't emit. Matches the shading table's emissiveMap exclusion.
166
+ function emissiveColor(mat) {
167
+ if (mat.emissiveMap != null || !mat.emissive) return null;
168
+ const i = mat.emissiveIntensity ?? 1;
169
+ if (i <= 0 || mat.emissive.r + mat.emissive.g + mat.emissive.b <= 0) return null;
170
+ return [mat.emissive.r * i, mat.emissive.g * i, mat.emissive.b * i];
171
+ }
172
+
173
+ // Row 0: materials, 2 texels each (albedo+rough, emissive+metal).
174
+ // Row 1: emissive triangles for NEE, 4 texels each:
175
+ // [v0.xyz | area] [e1.xyz | emit.r] [e2.xyz | emit.g] [n.xyz | emit.b]
176
+ // Rows 2..65: a 64x64 RGBA blue-noise tile for low-discrepancy sampling.
177
+ // All packed into ONE texture because the lighting pass already sits at the
178
+ // WebGL2-guaranteed 16-sampler limit — extra samplers are not available.
179
+ function buildSceneDataTexture(materials, emissiveTris) {
180
+ const bn = decodeBlueNoise();
181
+ const width = Math.max(materials.length * 2, emissiveTris.length * 4, BLUE_NOISE_SIZE);
182
+ const height = 2 + BLUE_NOISE_SIZE;
183
+ const data = new Float32Array(width * height * 4);
184
+ materials.forEach((mat, i) => {
185
+ const o = i * 8;
186
+ const color = mat.color ?? new THREE.Color(1, 1, 1);
187
+ const emissive = emissiveColor(mat) ?? [0, 0, 0];
188
+ data[o + 0] = color.r;
189
+ data[o + 1] = color.g;
190
+ data[o + 2] = color.b;
191
+ data[o + 3] = mat.roughness ?? 1;
192
+ data[o + 4] = emissive[0];
193
+ data[o + 5] = emissive[1];
194
+ data[o + 6] = emissive[2];
195
+ data[o + 7] = mat.metalness ?? 0;
196
+ });
197
+ const row = width * 4;
198
+ emissiveTris.forEach((t, i) => {
199
+ const o = row + i * 16;
200
+ data[o + 0] = t.v0[0]; data[o + 1] = t.v0[1]; data[o + 2] = t.v0[2]; data[o + 3] = t.area;
201
+ data[o + 4] = t.e1[0]; data[o + 5] = t.e1[1]; data[o + 6] = t.e1[2]; data[o + 7] = t.emit[0];
202
+ data[o + 8] = t.e2[0]; data[o + 9] = t.e2[1]; data[o + 10] = t.e2[2]; data[o + 11] = t.emit[1];
203
+ data[o + 12] = t.n[0]; data[o + 13] = t.n[1]; data[o + 14] = t.n[2]; data[o + 15] = t.emit[2];
204
+ });
205
+ for (let y = 0; y < BLUE_NOISE_SIZE; y++) {
206
+ const o = (2 + y) * row;
207
+ const src = y * BLUE_NOISE_SIZE * 4;
208
+ for (let i = 0; i < BLUE_NOISE_SIZE * 4; i++) {
209
+ data[o + i] = (bn[src + i] + 0.5) / 256.0;
210
+ }
211
+ }
212
+ const tex = new THREE.DataTexture(data, width, height, THREE.RGBAFormat, THREE.FloatType);
213
+ tex.minFilter = THREE.NearestFilter;
214
+ tex.magFilter = THREE.NearestFilter;
215
+ tex.needsUpdate = true;
216
+ return tex;
217
+ }
218
+
219
+ // Collect world-space triangles of an emissive mesh for the NEE light list.
220
+ // `geo` is already non-indexed and world-baked by extractMeshGeometry.
221
+ function collectEmissiveTriangles(geo, emit, out) {
222
+ const pos = geo.getAttribute("position").array;
223
+ for (let i = 0; i + 8 < pos.length; i += 9) {
224
+ const e1 = [pos[i + 3] - pos[i], pos[i + 4] - pos[i + 1], pos[i + 5] - pos[i + 2]];
225
+ const e2 = [pos[i + 6] - pos[i], pos[i + 7] - pos[i + 1], pos[i + 8] - pos[i + 2]];
226
+ const cx = e1[1] * e2[2] - e1[2] * e2[1];
227
+ const cy = e1[2] * e2[0] - e1[0] * e2[2];
228
+ const cz = e1[0] * e2[1] - e1[1] * e2[0];
229
+ const len = Math.hypot(cx, cy, cz);
230
+ if (len < 1e-10) continue; // degenerate
231
+ out.push({
232
+ v0: [pos[i], pos[i + 1], pos[i + 2]],
233
+ e1,
234
+ e2,
235
+ n: [cx / len, cy / len, cz / len],
236
+ area: len * 0.5,
237
+ emit,
238
+ });
239
+ }
240
+ }
241
+
242
+ // A single degenerate triangle so the dynamic BVH textures are always valid even
243
+ // when there are no dynamic meshes (tracing is gated by a uHasDynamic flag).
244
+ function degenerateGeometry() {
245
+ const geo = new THREE.BufferGeometry();
246
+ geo.setAttribute("position", new THREE.BufferAttribute(new Float32Array(9), 3));
247
+ geo.setAttribute("normal", new THREE.BufferAttribute(new Float32Array([0, 1, 0, 0, 1, 0, 0, 1, 0]), 3));
248
+ geo.setAttribute("materialIndex", new THREE.BufferAttribute(new Float32Array(3), 1));
249
+ return geo;
250
+ }
251
+
252
+ // Pack normal.xyz + materialIndex.w into one itemSize-4 attribute so each BVH
253
+ // level costs a single sampler for its per-vertex data.
254
+ function packAttributes(merged) {
255
+ const norm = merged.getAttribute("normal");
256
+ const matIdx = merged.getAttribute("materialIndex");
257
+ const count = norm.count;
258
+ const packed = new Float32Array(count * 4);
259
+ for (let i = 0; i < count; i++) {
260
+ packed[i * 4] = norm.getX(i);
261
+ packed[i * 4 + 1] = norm.getY(i);
262
+ packed[i * 4 + 2] = norm.getZ(i);
263
+ packed[i * 4 + 3] = matIdx.getX(i);
264
+ }
265
+ return { packed, attr: new THREE.BufferAttribute(packed, 4) };
266
+ }
267
+
268
+ // Build one BVH level (merge geometries, upload BVH + attribute textures).
269
+ function buildLevel(geometries, { dynamic }) {
270
+ const merged =
271
+ geometries.length > 0 ? mergeGeometries(geometries, false) : degenerateGeometry();
272
+ const bvh = new MeshBVH(merged, { strategy: dynamic ? CENTER : SAH });
273
+ return { merged, bvh, ...packAttributes(merged) };
274
+ }
275
+
276
+ export function compileScene(scene, options = {}) {
277
+ scene.updateMatrixWorld(true);
278
+ const dynamicSet = options.dynamicMeshes ? new Set(options.dynamicMeshes) : null;
279
+
280
+ const compiled = new CompiledScene();
281
+ const materials = compiled.materials;
282
+ const staticGeoms = [];
283
+ const dynamicGeoms = [];
284
+ const emissiveTris = [];
285
+ let dynVertexOffset = 0;
286
+ const tmpGeoms = []; // to dispose after merge
287
+
288
+ scene.traverse((obj) => {
289
+ if (!obj.isMesh || !obj.geometry || !obj.visible || obj.userData.rtExclude) return;
290
+ const mat = Array.isArray(obj.material) ? obj.material[0] : obj.material;
291
+ // Transparent surfaces must not act as opaque occluders — e.g.
292
+ // LittlestTokyo's glass display case (texture-alpha, opacity 1) would put
293
+ // the whole model in shadow. Alpha-textured glass can't be cheaply tested,
294
+ // so ANY transparent material is skipped like rtExclude (still
295
+ // rasterized). alphaTest cut-outs (transparent: false) stay occluders.
296
+ if (mat.transparent) return;
297
+ let mi = materials.indexOf(mat);
298
+ if (mi < 0) { mi = materials.length; materials.push(mat); }
299
+
300
+ const extracted = extractMeshGeometry(obj, mi);
301
+ tmpGeoms.push(extracted.geo);
302
+ // Static emissive meshes become NEE area lights (sampled directly with
303
+ // shadow rays instead of waiting for a GI ray to stumble into them).
304
+ // Dynamic emitters are left out their world-space triangles would go
305
+ // stale so they keep lighting the old way, via GI-ray hits.
306
+ const isDynamic = dynamicSet && dynamicSet.has(obj);
307
+ if (!isDynamic) {
308
+ const emit = emissiveColor(mat);
309
+ if (emit) collectEmissiveTriangles(extracted.geo, emit, emissiveTris);
310
+ }
311
+ if (isDynamic) {
312
+ dynamicGeoms.push(extracted.geo);
313
+ compiled.dynamic.push({
314
+ mesh: obj,
315
+ start: dynVertexOffset,
316
+ count: extracted.count,
317
+ localPos: extracted.localPos,
318
+ localNorm: extracted.localNorm,
319
+ });
320
+ dynVertexOffset += extracted.count;
321
+ } else {
322
+ staticGeoms.push(extracted.geo);
323
+ }
324
+ });
325
+
326
+ if (staticGeoms.length === 0 && dynamicGeoms.length === 0) {
327
+ throw new Error("three-realtime-rt: no meshes found in scene");
328
+ }
329
+
330
+ // Static level.
331
+ const s = buildLevel(staticGeoms, { dynamic: false });
332
+ compiled.staticBvh = s.bvh;
333
+ compiled.staticBvhUniform.updateFrom(s.bvh);
334
+ compiled.staticAttrTex.updateFrom(s.attr);
335
+
336
+ // Dynamic level.
337
+ compiled.hasDynamic = dynamicGeoms.length > 0;
338
+ const d = buildLevel(dynamicGeoms, { dynamic: true });
339
+ compiled.dynamicMerged = d.merged;
340
+ compiled.dynamicBvh = d.bvh;
341
+ compiled.dynamicBvhUniform.updateFrom(d.bvh);
342
+ compiled.dynamicPacked = d.packed;
343
+ compiled.dynamicPackedAttr = d.attr;
344
+ compiled.dynamicAttrTex.updateFrom(d.attr);
345
+
346
+ compiled.triangleCount =
347
+ (s.merged.getAttribute("position").count +
348
+ (compiled.hasDynamic ? d.merged.getAttribute("position").count : 0)) / 3;
349
+
350
+ // World-space extent of the static level. Used to auto-scale the ray offset
351
+ // epsilon: dense/large scenes (e.g. a detailed diorama normalized to a few
352
+ // units) need a bigger offset or every shadow ray self-intersects nearby
353
+ // micro-geometry and the scene renders black.
354
+ s.merged.computeBoundingBox();
355
+ const bb = s.merged.boundingBox;
356
+ compiled.sceneDiagonal = bb.isEmpty() ? 1 : bb.min.distanceTo(bb.max);
357
+
358
+ if (emissiveTris.length > MAX_EMISSIVE_TRIS) {
359
+ console.warn(
360
+ `three-realtime-rt: ${emissiveTris.length} emissive triangles exceed the ` +
361
+ `NEE cap of ${MAX_EMISSIVE_TRIS}; keeping the largest by area. Dropped ` +
362
+ `triangles no longer act as lights — prefer low-poly emitter meshes.`
363
+ );
364
+ emissiveTris.sort((a, b) => b.area - a.area);
365
+ emissiveTris.length = MAX_EMISSIVE_TRIS;
366
+ }
367
+ compiled.emissiveTriCount = emissiveTris.length;
368
+ compiled.materialsTex = buildSceneDataTexture(materials, emissiveTris);
369
+ syncLights(scene, compiled);
370
+
371
+ // Static merged geometry is owned by its BVH (disposed with it); dynamic
372
+ // merged geometry is kept live for re-baking. Dispose the per-mesh temporaries
373
+ // that aren't the merged buffers.
374
+ for (const g of tmpGeoms) {
375
+ if (g !== s.merged && g !== d.merged) g.dispose();
376
+ }
377
+ return compiled;
378
+ }
379
+
380
+ /** (Re)scan the scene's lights into the compiled light tables. Cheap; call anytime. */
381
+ export function syncLights(scene, compiled) {
382
+ const posType = compiled.lightPosType;
383
+ const colorRadius = compiled.lightColorRadius;
384
+ const dirCone = compiled.lightDirCone;
385
+ posType.length = 0;
386
+ colorRadius.length = 0;
387
+ dirCone.length = 0;
388
+ let count = 0;
389
+ const tmpP = new THREE.Vector3();
390
+ const tmpT = new THREE.Vector3();
391
+
392
+ scene.traverse((obj) => {
393
+ if (!obj.isLight || !obj.visible || obj.intensity <= 0) return;
394
+ if (count >= MAX_LIGHTS) return;
395
+ if (obj.isSpotLight) {
396
+ // posType.w encodes type AND the inner-cone cosine: w = 2 + cosInner
397
+ // (any w >= 1.5 is a spot). Direction + outer cosine live in dirCone.
398
+ obj.getWorldPosition(tmpP);
399
+ obj.target.getWorldPosition(tmpT);
400
+ const dir = tmpT.sub(tmpP).normalize();
401
+ const cosOuter = Math.cos(obj.angle);
402
+ const cosInner = Math.cos(obj.angle * (1 - (obj.penumbra ?? 0)));
403
+ posType.push(tmpP.x, tmpP.y, tmpP.z, 2 + cosInner);
404
+ colorRadius.push(
405
+ obj.color.r * obj.intensity,
406
+ obj.color.g * obj.intensity,
407
+ obj.color.b * obj.intensity,
408
+ obj.userData.rtRadius ?? 0.1
409
+ );
410
+ dirCone.push(dir.x, dir.y, dir.z, cosOuter);
411
+ count++;
412
+ } else if (obj.isPointLight) {
413
+ obj.getWorldPosition(tmpP);
414
+ posType.push(tmpP.x, tmpP.y, tmpP.z, 0);
415
+ colorRadius.push(
416
+ obj.color.r * obj.intensity,
417
+ obj.color.g * obj.intensity,
418
+ obj.color.b * obj.intensity,
419
+ obj.userData.rtRadius ?? 0.15
420
+ );
421
+ dirCone.push(0, 0, 0, 0);
422
+ count++;
423
+ } else if (obj.isDirectionalLight) {
424
+ obj.getWorldPosition(tmpP);
425
+ obj.target.getWorldPosition(tmpT);
426
+ const dir = tmpT.sub(tmpP).normalize();
427
+ posType.push(dir.x, dir.y, dir.z, 1);
428
+ colorRadius.push(
429
+ obj.color.r * obj.intensity,
430
+ obj.color.g * obj.intensity,
431
+ obj.color.b * obj.intensity,
432
+ obj.userData.rtRadius ?? 0.02
433
+ );
434
+ dirCone.push(0, 0, 0, 0);
435
+ count++;
436
+ }
437
+ });
438
+
439
+ compiled.lightCount = count;
440
+ while (posType.length < MAX_LIGHTS * 4) {
441
+ posType.push(0, 0, 0, 0);
442
+ colorRadius.push(0, 0, 0, 0);
443
+ dirCone.push(0, 0, 0, 0);
444
+ }
445
+ }
446
+
447
+ export { MAX_LIGHTS };