Skip to content

3D engine that AI agents drive

Light the buildings a hand-placed reflection probe sees with the environment map (#1385)

Nguyen Xuan An6560a68d24a6
diff --git a/changes/1385.md b/changes/1385.md
new file mode 100644
index 00000000..6eb6a714
--- /dev/null
+++ b/changes/1385.md
@@ -0,0 +1 @@
+- A reflection probe placed by hand now sees the buildings around it lit by the environment map, so glass in front of a building reflects the building and not a black silhouette (Part of #1385).
diff --git a/docs/light-bake.md b/docs/light-bake.md
index 209c6c5e..18e57ba0 100644
--- a/docs/light-bake.md
+++ b/docs/light-bake.md
@@ -90,7 +90,7 @@ Nodes inside a solid (most rays hit the back of a face) are filled from their ne
## Reflection probes
-A probe is the room around a point, traced once and prefiltered like an environment map (`createEnvironmentResources`), so a glossy part reflects the room and not only the sky. `crucible/addons/reflection-probes` holds placement and the renderer link; the probe tracer traces on the CPU with the bake's ray tracer (direct light, bounced light, emission; a ray that leaves the scene reads the environment map or the sky colours), 64 by 32 texels, 32 samples and 2 bounces, deterministic, about 0.2 to 0.3 s per probe. A part takes the nearest probe whose `radius` reaches the middle of its bounds, else the environment. A probe lights the specular term only: diffuse stays with the environment and the bake. No parallax correction (not built: a probe at the centre of a room is wrong near the walls).
+A probe is the room around a point, traced once and prefiltered like an environment map (`createEnvironmentResources`), so a glossy part reflects the room and not only the sky. `crucible/addons/reflection-probes` holds placement and the renderer link; the probe tracer traces on the CPU with the bake's ray tracer (direct light, bounced light, emission; a ray that leaves the scene reads the environment map or the sky colours), 64 by 32 texels, 32 samples and 2 bounces, deterministic, about 0.2 to 0.3 s per probe. A probe placed by hand (not the automatic `room` probe, whose walls the sky does not reach) also lights the surfaces it sees with the diffuse light of the environment map, so a building seen from an open scene shows its colour and not black. A part takes the nearest probe whose `radius` reaches the middle of its bounds, else the environment. A probe lights the specular term only: diffuse stays with the environment and the bake. No parallax correction (not built: a probe at the centre of a room is wrong near the walls).
```js
import { applyReflectionProbes } from 'crucible/addons/reflection-probes';
diff --git a/src/probe-trace.js b/src/probe-trace.js
index d81bbbca..e2f93a82 100644
--- a/src/probe-trace.js
+++ b/src/probe-trace.js
@@ -322,7 +322,10 @@ export function traceProbes(parts, lighting, specs, options = {}) {
const sceneRadius = roomCentreProbe(parts).radius * 2;
const started = performance.now();
const positions = requested.map(probe => probe.position);
- const maps = requested.length === 0 ? [] : traceProbeMaps(parts, lighting, positions, { width: PROBE_MAP_WIDTH, samples, bounces, rotation, missRadiance: environmentMap ? environmentMissRadiance(environmentMap) : skyMissRadiance(environment), maxSeconds });
+ // A probe placed by hand sits in an open scene as often as in a room, so the buildings it sees take the light of the environment map
+ // too; the automatic room probe is for enclosed rooms, where the sky does not reach the walls.
+ const skyIrradiance = environmentMap && !automatic ? environmentSkyIrradiance(environmentMap) : null;
+ const maps = requested.length === 0 ? [] : traceProbeMaps(parts, lighting, positions, { width: PROBE_MAP_WIDTH, samples, bounces, rotation, missRadiance: environmentMap ? environmentMissRadiance(environmentMap) : skyMissRadiance(environment), skyIrradiance, maxSeconds });
const traced = [];
requested.forEach((spec, index) => {
if (automatic && maps[index].hitFraction < ENCLOSED_HIT_FRACTION) {
diff --git a/tests/glass-reflections.test.js b/tests/glass-reflections.test.js
index 0a8acc6b..aa2060fb 100644
--- a/tests/glass-reflections.test.js
+++ b/tests/glass-reflections.test.js
@@ -6,6 +6,9 @@ import { liquidProbeSupport } from '../src/shader-registry.js';
import { createFragmentShader, liquidFragmentSource } from '../src/shaders.js';
import '../src/liquid-shader.js';
import { probeResourcesOf } from '../src/reflection-probes.js';
+import { box } from '../src/geometry.js';
+import { createEnvironmentMap } from '../src/environment.js';
+import { traceProbes } from '../src/probe-trace.js';
// Issue #1385: glass reflects the room a probe was traced in, and a blended dielectric keeps its specular at full strength.
@@ -132,3 +135,36 @@ test('the pass leaves the global environment bound when its last batch had a pro
liquidProbeSupport.finish(renderer, uniforms);
assert.deepEqual(renderer.bound, ['probe-specular', 'white']);
});
+
+function wallPlacement(translation, scale, name, color) {
+ const matrix = new Float32Array([scale[0], 0, 0, 0, 0, scale[1], 0, 0, 0, 0, scale[2], 0, translation[0], translation[1], translation[2], 1]);
+ return { name, geometry: box(0, 1), matrix, material: { tag: 'surface', color, metallic: 0, roughness: 0.8, emission: 0 } };
+}
+
+const greySky = () => createEnvironmentMap({ width: 8, height: 4, data: new Float32Array(8 * 4 * 3).fill(1) });
+
+// The map is equirectangular around -z: a ray along -x sits a quarter of the way across, on the equator.
+function radianceAlongMinusX(probe) {
+ const offset = (Math.floor(probe.height / 2) * probe.width + Math.floor(probe.width * 0.25)) * 3;
+ return [probe.data[offset], probe.data[offset + 1], probe.data[offset + 2]];
+}
+
+test('a probe placed by hand sees a wall lit by the environment map, not a black one (#1385)', () => {
+ const wall = wallPlacement([-4, 1.5, 0], [0.2, 6, 6], 'red wall', [0.8, 0.1, 0.1]);
+ const trace = traceProbes([wall], { keyColor: [0, 0, 0] }, [{ position: [0, 1.5, 0], radius: 10 }], { environmentMap: greySky() });
+ const radiance = radianceAlongMinusX(trace.traced[0]);
+ assert.ok(radiance[0] > 0.3, `the red channel of the wall is ${radiance[0]}`);
+ assert.ok(radiance[0] > radiance[1] * 4, 'the wall keeps its red colour');
+});
+
+test('the automatic room probe leaves the walls of an enclosed room unlit by the sky', () => {
+ const grey = [0.7, 0.7, 0.7];
+ const room = [
+ wallPlacement([0, -0.05, 0], [6, 0.1, 6], 'floor', grey), wallPlacement([0, 3.05, 0], [6, 0.1, 6], 'ceiling', grey),
+ wallPlacement([0, 1.5, -3.05], [6, 3.2, 0.1], 'back', grey), wallPlacement([0, 1.5, 3.05], [6, 3.2, 0.1], 'front', grey),
+ wallPlacement([-3.05, 1.5, 0], [0.1, 3.2, 6], 'left', grey), wallPlacement([3.05, 1.5, 0], [0.1, 3.2, 6], 'right', grey),
+ ];
+ const [probe] = traceProbes(room, { keyColor: [0, 0, 0] }, 'room', { environmentMap: greySky() }).traced;
+ assert.ok(probe, 'the closed room gets its probe');
+ assert.ok(Math.max(...probe.data) < 1e-6, 'nothing in the closed room is lit');
+});