Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Trace one bounce in object probes and mirror the sky on non-metal hits (part of #167)

Nguyen Xuan Anc8f3238e40aa
diff --git a/bench/reference/README.md b/bench/reference/README.md
index 7bfa8abd..fa044c2b 100644
--- a/bench/reference/README.md
+++ b/bench/reference/README.md
@@ -1144,3 +1144,31 @@ Which scenes the bounce skips (a temporary log of the reason in the harness, not
Nothing moved, so `baseline.json` is unchanged. `foliage` is still 0.0979 against EEVEE 0.0888, and the issue stays open.
Bundle size: `entry:ground-bounce` is 9451 bytes gzip (9318 before the texture mean, which adds 133 bytes), so its budget rose from 9330 to 9460.
+
+## One bounce and the sky in object probes (2026-11-03, part of issue #167)
+
+`OBJECT_PROBE_BOUNCES` went from 0 to 1, and a probe hit on a non-metal surface now adds the sky that surface mirrors toward the probe. Measured on SwiftShader first, then the GPU (FLIP against Cycles, `baseline.json` rows of `metal`, `metal-room`, `brushed-film`, `car-paint` and `material-spheres` updated; mountain-valley moved 0.1769 to 0.1820 on this machine's run with no probe in the scene, so its baseline row and the other 17 rows were kept as they were).
+
+The earlier bounce try (#209, 0.1246 at 64 samples) was measuring a bug: `directIrradiance` returns one shared array, the gatherer calls it again for every neighbour it hits, and `traceProbeMaps` read `direct` after that call, so each probe pixel got the direct light of the last neighbour the gatherer visited (usually in shadow). The floor's mean in the probe fell from 0.25 to 0.15 with one bounce. With a copy of the direct light first, one bounce adds a little light and no longer darkens.
+
+| SwiftShader, `metal` | FLIP | probe trace |
+| --- | --- | --- |
+| Main | 0.0962 | 0.12 s |
+| One bounce, bug as it was | 0.1098 | 0.24 s |
+| One bounce, direct light copied | 0.0955 | 0.25 s |
+| Plus dielectric sky reflection (Fresnel, F0 0.04, capped by gloss, sky blurred to a cosine lobe, one shadow ray) | 0.0942 | 0.25 s |
+| Same, Fresnel not capped by gloss | 0.0938 (`brushed-film` 0.1019) | |
+| Same, plus metal parts mirroring the sky too | 0.0939 | |
+| Two bounces | 0.0941 | 0.28 s |
+
+Kept: one bounce, the copy, the capped Fresnel sky reflection. The uncapped Fresnel, the metal reflection and the second bounce are worth 0.0003 or less and cost code or time. GPU result against Cycles:
+
+| Scene | Main | Now | EEVEE |
+| --- | --- | --- | --- |
+| `metal` | 0.0964 | 0.0944 | 0.0916 |
+| `metal-room` | 0.5071 | 0.5045 | 0.5262 |
+| `material-spheres` | 0.0956 | 0.0954 | 0.1053 |
+| `car-paint` | 0.0979 | 0.0978 | 0.0844 |
+| `brushed-film` | 0.1017 | 0.1019 | 0.0984 |
+
+`brushed-film` is 0.0002 worse (the sky reflection; it was 0.1015 with the bounce alone on SwiftShader). Probe trace time on this machine (SwiftShader run, JS, so the same on the GPU run): `metal` 0.12 to 0.25 s, `brushed-film` 0.09 to 0.20 s, `car-paint` 0.06 to 0.10 s, `material-spheres` 0.19 to 0.37 s, `metal-room` 0.06 to 0.41 s; the trace runs when parts, lights or materials change, and a scene without a reflective part never loads it. `metal` is still 0.0028 above EEVEE: the cylinder's floor reflection is the closest gap left (probe 0.567 against Cycles 0.578 in the region, mean display luminance), the rest is light between metal parts that a diffuse bounce does not carry. The room probe trace (`PROBE_BOUNCES` 2) had the same overwritten `direct` and now reads the right light; no baseline scene uses it.
diff --git a/bench/reference/baseline.json b/bench/reference/baseline.json
index 66effb0c..37b0408e 100644
--- a/bench/reference/baseline.json
+++ b/bench/reference/baseline.json
@@ -125,18 +125,18 @@
"gpu-vs-cpu": 0.0071
},
"brushed-film": {
- "gpu-vs-cycles": 0.1017,
- "gpu-vs-eevee": 0.0836,
- "cpu-vs-cycles": 0.1014,
- "cpu-vs-eevee": 0.0836,
- "eevee-vs-cycles": 0.0985,
+ "gpu-vs-cycles": 0.1019,
+ "gpu-vs-eevee": 0.0835,
+ "cpu-vs-cycles": 0.1017,
+ "cpu-vs-eevee": 0.0835,
+ "eevee-vs-cycles": 0.0984,
"gpu-vs-cpu": 0.0064
},
"car-paint": {
- "gpu-vs-cycles": 0.0979,
- "gpu-vs-eevee": 0.0662,
- "cpu-vs-cycles": 0.0977,
- "cpu-vs-eevee": 0.0663,
+ "gpu-vs-cycles": 0.0978,
+ "gpu-vs-eevee": 0.066,
+ "cpu-vs-cycles": 0.0975,
+ "cpu-vs-eevee": 0.0661,
"eevee-vs-cycles": 0.0844,
"gpu-vs-cpu": 0.0061
},
@@ -229,10 +229,10 @@
"gpu-vs-cpu": 0.0066
},
"material-spheres": {
- "gpu-vs-cycles": 0.0956,
- "gpu-vs-eevee": 0.0741,
- "cpu-vs-cycles": 0.0951,
- "cpu-vs-eevee": 0.074,
+ "gpu-vs-cycles": 0.0954,
+ "gpu-vs-eevee": 0.0744,
+ "cpu-vs-cycles": 0.0949,
+ "cpu-vs-eevee": 0.0743,
"eevee-vs-cycles": 0.1053,
"gpu-vs-cpu": 0.0067
},
@@ -245,18 +245,18 @@
"gpu-vs-cpu": 0.0276
},
"metal": {
- "gpu-vs-cycles": 0.0964,
- "gpu-vs-eevee": 0.0806,
- "cpu-vs-cycles": 0.0962,
- "cpu-vs-eevee": 0.0808,
+ "gpu-vs-cycles": 0.0944,
+ "gpu-vs-eevee": 0.0826,
+ "cpu-vs-cycles": 0.0942,
+ "cpu-vs-eevee": 0.0828,
"eevee-vs-cycles": 0.0916,
"gpu-vs-cpu": 0.0067
},
"metal-room": {
- "gpu-vs-cycles": 0.5071,
- "gpu-vs-eevee": 0.109,
- "cpu-vs-cycles": 0.5067,
- "cpu-vs-eevee": 0.1097,
+ "gpu-vs-cycles": 0.5045,
+ "gpu-vs-eevee": 0.1122,
+ "cpu-vs-cycles": 0.5041,
+ "cpu-vs-eevee": 0.1129,
"eevee-vs-cycles": 0.5262,
"gpu-vs-cpu": 0.0062
},
diff --git a/bench/reference/images/brushed-film.jpg b/bench/reference/images/brushed-film.jpg
index ee78a795..988bdd97 100644
Binary files a/bench/reference/images/brushed-film.jpg and b/bench/reference/images/brushed-film.jpg differ
diff --git a/bench/reference/images/car-paint.jpg b/bench/reference/images/car-paint.jpg
index 8fc3f65d..bae65644 100644
Binary files a/bench/reference/images/car-paint.jpg and b/bench/reference/images/car-paint.jpg differ
diff --git a/bench/reference/images/material-spheres.jpg b/bench/reference/images/material-spheres.jpg
index 8b02dc20..d2b46382 100644
Binary files a/bench/reference/images/material-spheres.jpg and b/bench/reference/images/material-spheres.jpg differ
diff --git a/bench/reference/images/metal-room.jpg b/bench/reference/images/metal-room.jpg
index 6779c043..5a136d36 100644
Binary files a/bench/reference/images/metal-room.jpg and b/bench/reference/images/metal-room.jpg differ
diff --git a/bench/reference/images/metal.jpg b/bench/reference/images/metal.jpg
index c3526134..29b19a16 100644
Binary files a/bench/reference/images/metal.jpg and b/bench/reference/images/metal.jpg differ
diff --git a/bench/size/budgets.json b/bench/size/budgets.json
index 1d51c311..03e5b24e 100644
--- a/bench/size/budgets.json
+++ b/bench/size/budgets.json
@@ -35,9 +35,9 @@
"entry:path-tracer": 14600,
"entry:texture-set": 900,
"entry:agent-scene": 77810,
- "entry:agent-scene+lazy": 259980,
+ "entry:agent-scene+lazy": 260280,
"entry:agent-asset": 109170,
- "entry:agent-asset+lazy": 271380,
+ "entry:agent-asset+lazy": 271680,
"entry:hdri": 3300,
"entry:camera-effects": 8600,
"entry:water-reflection": 26000,
@@ -49,6 +49,6 @@
"scene:gltf-box": 84380,
"scene:gltf-box+lazy": 87710,
"scene:agent-scene": 105160,
- "scene:agent-scene+lazy": 287330
+ "scene:agent-scene+lazy": 287630
}
}
diff --git a/changes/issue-167-bounce.md b/changes/issue-167-bounce.md
new file mode 100644
index 00000000..7b86f50a
--- /dev/null
+++ b/changes/issue-167-bounce.md
@@ -0,0 +1 @@
+- Trace one bounce in object probes and let the probe's surfaces mirror the sky (part of issue #167). `traceProbeMaps` also stops overwriting a hit's direct light with the light of the last neighbour the gatherer visited, which is why #209's bounce made the probe floor darker. Against Cycles on the GPU `metal` goes from 0.0964 to 0.0944 (EEVEE 0.0916), `metal-room` 0.5071 to 0.5045, `material-spheres` 0.0956 to 0.0954, `car-paint` 0.0979 to 0.0978, and `brushed-film` 0.1017 to 0.1019. The probe trace takes about twice as long (`metal` 0.12 s to 0.25 s); a scene with no reflective part never runs it.
diff --git a/src/probe-trace.js b/src/probe-trace.js
index 652e34c7..e8e8697a 100644
--- a/src/probe-trace.js
+++ b/src/probe-trace.js
@@ -22,7 +22,7 @@ const LEVEL_FLOOR_NORMAL = 0.99;
export const MAXIMUM_OBJECT_PROBES = 8;
const OBJECT_PROBE_WIDTH = 128;
const OBJECT_PROBE_SAMPLES = 16;
-const OBJECT_PROBE_BOUNCES = 0;
+const OBJECT_PROBE_BOUNCES = 1;
const OBJECT_PROBE_MINIMUM_METALLIC = 0.5;
const OBJECT_PROBE_MAXIMUM_ROUGHNESS = 0.6;
@@ -56,12 +56,14 @@ export function traceProbeMaps(parts, lighting, positions, { width = 64, samples
const albedoOfPart = parts.map(part => probeAlbedo(part.material));
const emissionOfPart = parts.map(part => emittedRadiance(part.material));
const gather = createGatherer({ world, tracer, albedoOfPart, emissionOfPart, directIrradiance, offset, samples, bounces });
+ const skyReflection = skyIrradiance ? createSkyReflection({ tracer, offset, skyIrradiance, parts }) : null;
const started = performance.now();
const rotationCosine = Math.cos(rotation);
const rotationSine = Math.sin(rotation);
const direction = [0, 0, 0];
const point = [0, 0, 0];
const normal = [0, 0, 0];
+ const direct = [0, 0, 0];
const bounced = [0, 0, 0];
const missed = [0, 0, 0];
return positions.map((position, probeIndex) => {
@@ -90,13 +92,19 @@ export function traceProbeMaps(parts, lighting, positions, { width = 64, samples
for (let axis = 0; axis < 3; axis++) point[axis] = position[axis] + direction[axis] * hit.nearestDistance;
hitNormal(world, hit.nearestTriangle, direction, normal);
const owner = world.triangleOwner[hit.nearestTriangle];
- const direct = directIrradiance(point, normal, owner);
+ // The direct light function reuses one result array, and the gatherer calls it again for every neighbour it hits.
+ const shared = directIrradiance(point, normal, owner);
+ direct[0] = shared[0];
+ direct[1] = shared[1];
+ direct[2] = shared[2];
gather(point, normal, (probeIndex + 7777) * 1_000_003 + row * width + column, bounced);
// An open scene's surfaces are lit by the sky too, and the room trace leaves that to the environment map.
const sky = skyIrradiance ? skyIrradiance(normal) : null;
+ const reflected = skyReflection ? skyReflection(point, normal, direction, owner) : null;
for (let channel = 0; channel < 3; channel++) {
const skyLight = sky ? sky[channel] : 0;
- data[target + channel] = emissionOfPart[owner][channel] + albedoOfPart[owner][channel] * (direct[channel] / Math.PI + bounced[channel] + skyLight);
+ const reflectedLight = reflected ? reflected[channel] : 0;
+ data[target + channel] = emissionOfPart[owner][channel] + albedoOfPart[owner][channel] * (direct[channel] / Math.PI + bounced[channel] + skyLight) + reflectedLight;
}
}
}
@@ -104,6 +112,30 @@ export function traceProbeMaps(parts, lighting, positions, { width = 64, samples
});
}
+// The sky a dielectric surface mirrors toward the probe: Fresnel (Schlick, F0 0.04, capped by gloss) times the sky light of
+// the mirror direction, blurred to a cosine lobe, and only where that direction leaves the scene.
+function createSkyReflection({ tracer, offset, skyIrradiance, parts }) {
+ const result = [0, 0, 0];
+ const mirror = [0, 0, 0];
+ return function skyReflection(point, normal, incoming, owner) {
+ const material = parts[owner].material;
+ const dielectric = 1 - (material.metallic ?? 0);
+ const cosine = -(incoming[0] * normal[0] + incoming[1] * normal[1] + incoming[2] * normal[2]);
+ result[0] = 0;
+ result[1] = 0;
+ result[2] = 0;
+ if (dielectric <= 0 || cosine <= 0) return result;
+ for (let axis = 0; axis < 3; axis++) mirror[axis] = incoming[axis] + 2 * cosine * normal[axis];
+ const escapes = tracer.trace(point[0] + normal[0] * offset, point[1] + normal[1] * offset, point[2] + normal[2] * offset, mirror[0], mirror[1], mirror[2], Infinity, true).nearestTriangle < 0;
+ if (!escapes) return result;
+ const gloss = 1 - (material.roughness ?? 0.5);
+ const fresnel = Math.min(0.04 + 0.96 * (1 - cosine) ** 5, Math.max(gloss, 0.04)) * dielectric;
+ const light = skyIrradiance(mirror);
+ for (let channel = 0; channel < 3; channel++) result[channel] = fresnel * light[channel] / Math.PI;
+ return result;
+ };
+}
+
// The height of the horizontal surface straight under a probe, or null when the ground there is not level. A reflected ray
// that heads down meets this plane, which is how the lookup is corrected for the probe sitting away from the shaded point.
function floorBelow(world, tracer, position) {
diff --git a/tests/reflection-probes.test.js b/tests/reflection-probes.test.js
index 23a41d6c..80188074 100644
--- a/tests/reflection-probes.test.js
+++ b/tests/reflection-probes.test.js
@@ -168,3 +168,13 @@ test('rough metal and plain surfaces are not reflective', () => {
assert.equal(isReflectivePart({ metallic: 0.2, roughness: 0.1 }), false);
assert.equal(isReflectivePart({}), false);
});
+
+test('a bounce in the probe trace adds light to a lit surface and never takes direct light away', () => {
+ const total = options => {
+ const [probe] = traceObjectProbes(objectScene(), lamp, { width: 32, samples: 8, ...options }).traced;
+ return probe.data.reduce((sum, value) => sum + value, 0);
+ };
+ const withoutBounce = total({ bounces: 0 });
+ assert.ok(withoutBounce > 0);
+ assert.ok(total({ bounces: 1 }) >= withoutBounce, 'the gatherer reuses the direct light buffer, which must not leak into the probe pixel');
+});