A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(lightmap): a scene with nothing in it no longer walks one node forever
esengine735df199cd7e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 85d3c53fd..ac3b2f76d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -121,7 +121,12 @@ published separately; it ships inside the editor.stood. Every bake in the corpus carried it: shadows that leaked, bounces thatnever arrived, probes gathering light through walls. The tree now stores theleft child and reads the right beside it, and agrees with brute force on every- ray; the shipped bakes are rebaked.+ ray; the shipped bakes are rebaked. Two more ways the same walk went wrong are+ closed with it: a scene with no surfaces at all (probes only) is one empty leaf,+ which the walk read as an inner node and circled forever, and a tree deeper than+ the walk's fixed 64-entry stack silently dropped its deepest nodes. Inner nodes+ are now marked apart from leaves, the stack is sized to the tree, and a walk that+ revisits a node throws instead of hanging.- **A material-shaded surface shaded itself at z = 0.** The Model shader passed`vec3(v_worldPos, 0)` as the place it stands — which a directional light cannotdiff --git a/sdk/src/lightmap/bvh.ts b/sdk/src/lightmap/bvh.tsindex 4889d3a3b..9e9a238ec 100644--- a/sdk/src/lightmap/bvh.ts+++ b/sdk/src/lightmap/bvh.ts@@ -17,19 +17,24 @@ export interface TriangleSoup {}const LEAF_TRIS = 4;-const STACK = 64;+/** The count an inner node carries: a leaf's is never negative, an empty one included. */+const INNER = -1;export class Bvh {/** Six floats per node: min xyz, max xyz. */private readonly bounds: Float32Array;/** Two ints per node: a leaf's first triangle or an inner node's LEFT child- * (the right is the one after it), then the triangle count — zero for an- * inner node. */+ * (the right is the one after it), then the triangle count — {@link INNER}+ * for an inner node. */private readonly node: Int32Array;/** Triangle indices, reordered so a leaf's are contiguous. */private readonly order: Int32Array;private readonly tris: TriangleSoup;private used = 0;+ private depth = 0;+ /** Sized to the tree: a walk leaves one waiting sibling per level, and a+ * fixed stack drops the deepest ones without a word. */+ private readonly stack: Int32Array;constructor(tris: TriangleSoup) {this.tris = tris;@@ -39,7 +44,8 @@ export class Bvh {this.bounds = new Float32Array(maxNodes * 6);this.node = new Int32Array(maxNodes * 2);this.used = 1;- this.build(0, 0, tris.count);+ this.build(0, 0, tris.count, 0);+ this.stack = new Int32Array(this.depth + 2);}private centroid(tri: number, axis: number): number {@@ -65,7 +71,8 @@ export class Bvh {/** Split on the widest axis at the centroid midpoint — cheap, and a bake* builds this once per scene while it traverses it for hours. */- private build(node: number, from: number, to: number): void {+ private build(node: number, from: number, to: number, depth: number): void {+ if (depth > this.depth) this.depth = depth;this.bound(node, from, to);const n = to - from;if (n <= LEAF_TRIS) {@@ -97,9 +104,9 @@ export class Bvh {const left = this.used++;const right = this.used++;this.node[node * 2] = left;- this.node[node * 2 + 1] = 0;- this.build(left, from, split);- this.build(right, split, to);+ this.node[node * 2 + 1] = INNER;+ this.build(left, from, split, depth + 1);+ this.build(right, split, to, depth + 1);}/**@@ -126,8 +133,6 @@ export class Bvh {hitU = 0;hitV = 0;- private readonly stack = new Int32Array(STACK);-private trace(ox: number, oy: number, oz: number, dx: number, dy: number, dz: number,far: number, epsilon: number, anyHit: boolean): number {const invX = 1 / dx, invY = 1 / dy, invZ = 1 / dz;@@ -135,7 +140,11 @@ export class Bvh {let bestT = far;let sp = 0;this.stack[sp++] = 0;+ // A tree is walked once per node at most; past that the walk is going in+ // circles, and a bake that hangs says less than one that throws.+ let visits = 0;while (sp > 0) {+ if (++visits > this.used) throw new Error('Bvh: the walk revisited a node — the tree is malformed');const node = this.stack[--sp];const b = node * 6;const tx1 = (this.bounds[b] - ox) * invX, tx2 = (this.bounds[b + 3] - ox) * invX;@@ -147,7 +156,7 @@ export class Bvh {if (tmax < Math.max(tmin, epsilon) || tmin > bestT) continue;const count = this.node[node * 2 + 1];- if (count === 0) {+ if (count === INNER) {const left = this.node[node * 2];this.stack[sp++] = left;this.stack[sp++] = left + 1;diff --git a/sdk/tests/lightmap-bvh.test.ts b/sdk/tests/lightmap-bvh.test.tsindex 7232996e3..272a17d94 100644--- a/sdk/tests/lightmap-bvh.test.ts+++ b/sdk/tests/lightmap-bvh.test.ts@@ -92,6 +92,38 @@ describe('the bake\'s ray tree', () => {.toBe(`${asked}/${asked} agree`);});+ it('answers nothing for a scene with nothing in it', () => {+ // The empty scene is one leaf with no triangles, and a walk that reads a+ // count of zero as "inner node" goes round that one node forever.+ const bvh = new Bvh({ positions: new Float32Array(0), count: 0 });+ expect(bvh.hit(0, 0, 0, 0, 1, 0, 1e7, 0)).toBe(-1);+ expect(bvh.occluded(0, 0, 0, 0, 1, 0, 1e7, 0)).toBe(false);+ });++ it('finds what testing every triangle finds in a tree deeper than any fixed stack', () => {+ // Triangles at -2^i along X: the midpoint split peels one off per level,+ // and the rest — the side a walk pops first — goes one level deeper, so the+ // stack holds a waiting sibling per level for all ninety of them.+ const count = 90;+ const positions = new Float32Array(count * 9);+ for (let i = 0; i < count; i++) {+ const s = 2 ** i;+ positions.set([-s, 0, -s / 4, -1.25 * s, 0, -s / 4, -s, 0, s / 4], i * 9);+ }+ const tris: TriangleSoup = { positions, count };+ const bvh = new Bvh(tris);+ const missed: number[] = [];+ for (let i = 0; i < count; i++) {+ const s = 2 ** i;+ const o = [-1.05 * s, s, 0];+ const d = [0, -1, 0];+ const want = brute(tris, o, d, 3e38);+ if (want !== i) throw new Error(`the ray at triangle ${i} does not hit it alone (brute force: ${want})`);+ if (bvh.hit(o[0]!, o[1]!, o[2]!, d[0]!, d[1]!, d[2]!, 3e38, 0) !== want) missed.push(i);+ }+ expect(missed).toEqual([]);+ });+it('blocks a segment exactly where a triangle is in it', () => {const tris = soup(200, 3);const bvh = new Bvh(tris);