Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

WIP #506: fog reads the irradiance grid, automatic finer grid for true

Nguyen Xuan An78d22b59743e
diff --git a/docs/light-bake.md b/docs/light-bake.md
index b2b669c9..a5999ce8 100644
--- a/docs/light-bake.md
+++ b/docs/light-bake.md
@@ -76,7 +76,7 @@ An open scene with a floor gets the light the floor bounces without a bake and w
## Irradiance grid
-`bake: { preset: 'standard', irradianceGrid: true }` (or a node count from 8 to 48 along the longest side; `true` is 24) bakes a grid of diffuse light instead of lightmaps. `src/irradiance-grid.js` (a lazy module that loads with the bake) places a node every cell of the parts' bounds, and for each node traces the light that surfaces send toward it along the six axis directions with the bake's gatherer (emission and direct light at every hit, `bounces` reflections, about a quarter of the preset's samples). The six values become first order spherical harmonics per colour channel, stored in one RGBA16F 3D texture (three texels per node). A lit surface reads it with one trilinear lookup and its normal, so the warm light a sunlit floor sends up to the walls and the ceiling costs no ray per frame. A ray that leaves the scene adds nothing, so the sky stays with the environment map, and the grid replaces lightmaps for the scene. Probes still run.
+`bake: { preset: 'standard', irradianceGrid: true }` (or a node count from 8 to 48 along the longest side; `true` picks up to 48 along the longest side within 32,768 nodes) bakes a grid of diffuse light instead of lightmaps. `src/irradiance-grid.js` (a lazy module that loads with the bake) places a node every cell of the parts' bounds, and for each node traces the light that surfaces send toward it along the six axis directions with the bake's gatherer (emission and direct light at every hit, `bounces` reflections, about a quarter of the preset's samples). The six values become first order spherical harmonics per colour channel, stored in one RGBA16F 3D texture (three texels per node). A lit surface reads it with one trilinear lookup and its normal, so the warm light a sunlit floor sends up to the walls and the ceiling costs no ray per frame. A ray that leaves the scene adds nothing, so the sky stays with the environment map, and the grid replaces lightmaps for the scene. Probes still run.
Nodes inside a solid (most rays hit the back of a face) are filled from their neighbours, and the lookup moves half a cell along the normal. A wall thinner than a cell can still leak light, and the grid is static: it is traced again when the parts, the lights or the bake change, and a part that moves does not move its bounce. The report is `bake.irradianceGrid` (`size`, `cellSize`, `nodes`, `solidNodes`, `seconds`). The sampler has its own texture unit, 16, so it works beside splat layers. A 3x3x3 [1 2 1] filter over the valid nodes' coefficients removes the low-frequency noise of the few samples each node gets.
diff --git a/src/fog-sky-visibility.js b/src/fog-sky-visibility.js
index 64087455..243a9d95 100644
--- a/src/fog-sky-visibility.js
+++ b/src/fog-sky-visibility.js
@@ -19,6 +19,25 @@ function packCells(grid, bounce) {
return cells;
}
+// The irradiance grid's mean light at the centre of each cell, in the layout fogBounceGrid returns.
+function gridBounce(irradianceGrid, size, minimum, maximum) {
+ const data = new Float32Array(size[0] * size[1] * size[2] * 3);
+ const point = [0, 0, 0];
+ let cellIndex = 0;
+ for (let z = 0; z < size[2]; z++) {
+ for (let y = 0; y < size[1]; y++) {
+ for (let x = 0; x < size[0]; x++) {
+ [x, y, z].forEach((index, axis) => {
+ point[axis] = minimum[axis] + (index + 0.5) / size[axis] * (maximum[axis] - minimum[axis]);
+ });
+ data.set(irradianceGrid.meanRadiance(point), cellIndex * 3);
+ cellIndex++;
+ }
+ }
+ }
+ return data;
+}
+
function uploadGrid(context, grid, bounce) {
const texture = context.createTexture();
context.bindTexture(context.TEXTURE_3D, texture);
@@ -47,8 +66,12 @@ export function fogSkyVisibility(parts, volume, context, environment = {}) {
let entry = uploaded.get(parts);
if (entry?.context !== context || entry.key !== key) {
const grid = skyVisibilityGrid(parts, volume.minimum, volume.maximum);
- // The sun is left out of the bounce: see docs/volumetric.md.
- const bounce = grid && fogBounceGrid(parts, grid.size, volume.minimum, volume.maximum, { pointLights: environment.pointLights });
+ // The sun is left out of the bounce: see docs/volumetric.md. A scene with an irradiance grid already holds the sun's
+ // bounce, the lights and the emission over several bounces, so the fog reads the grid instead of tracing its own.
+ const irradianceGrid = parts.find(part => part.material?.irradianceGrid)?.material.irradianceGrid;
+ const bounce = !grid ? null
+ : irradianceGrid ? gridBounce(irradianceGrid, grid.size, volume.minimum, volume.maximum)
+ : fogBounceGrid(parts, grid.size, volume.minimum, volume.maximum, { pointLights: environment.pointLights });
entry = { context, key, texture: grid && uploadGrid(context, grid, bounce) };
uploaded.set(parts, entry);
}
diff --git a/src/irradiance-grid.js b/src/irradiance-grid.js
index 2daa68be..1da58028 100644
--- a/src/irradiance-grid.js
+++ b/src/irradiance-grid.js
@@ -15,7 +15,11 @@ import { worldBounds } from './probe-trace.js';
// material field `irradianceGrid`, which batches parts by grid.
export const IRRADIANCE_GRID_LIMITS = Object.freeze({ resolution: [8, 48], samples: [4, 128], bounces: [1, 3], maxNodes: 65536 });
-export const DEFAULT_GRID_RESOLUTION = 24;
+// `true` picks the cell size itself: at most this many nodes along the longest side and at most AUTO_NODE_BUDGET in all, so a
+// cube-shaped scene stays under the hard node limit and the trace stays within a few seconds. A coarser grid smears the sun's
+// patches into the shade (measured on the grand halls: a 9.7 m cell read 1.6 times the light of a brute-force gather).
+export const AUTO_GRID_RESOLUTION = 48;
+const AUTO_NODE_BUDGET = 32768;
const DEFAULT_GRID_SAMPLES = 32;
const VALIDITY_DIRECTIONS = buildValidityDirections();
const BACKFACE_INVALID_SHARE = 0.5;
@@ -56,7 +60,10 @@ export function normalizeIrradianceGrid(input) {
if (input === undefined || input === null || input === false) {
return null;
}
- const resolution = input === true ? DEFAULT_GRID_RESOLUTION : input;
+ if (input === true) {
+ return true;
+ }
+ const resolution = input;
const [minimum, maximum] = IRRADIANCE_GRID_LIMITS.resolution;
if (!Number.isInteger(resolution) || resolution < minimum || resolution > maximum) {
throw new RangeError(`bake.irradianceGrid must be true or an integer node count from ${minimum} to ${maximum}.`);
@@ -82,9 +89,14 @@ function gridLayout(parts, resolution) {
}
}
const extent = maximum.map((value, axis) => Math.max(value - minimum[axis], 1e-3));
- const cellSize = Math.max(...extent) / (resolution - 1);
- const size = extent.map(length => Math.max(2, Math.round(length / cellSize) + 1));
- return { origin: minimum, cellSize, size };
+ const sizeFor = cell => extent.map(length => Math.max(2, Math.round(length / cell) + 1));
+ let cellSize = Math.max(...extent) / ((resolution === true ? AUTO_GRID_RESOLUTION : resolution) - 1);
+ if (resolution === true) {
+ while (sizeFor(cellSize).reduce((product, count) => product * count, 1) > AUTO_NODE_BUDGET) {
+ cellSize *= 1.05;
+ }
+ }
+ return { origin: minimum, cellSize, size: sizeFor(cellSize) };
}
// A node inside a solid sees the back of the surfaces around it; its light would be the inside of a wall, not the room's.
@@ -207,7 +219,7 @@ function smoothNodes(data, valid, size, isConnected) {
* the mean of the opposite pair is the constant term and half their difference the slope. Nodes inside solids are filled from
* their neighbours. Returns `{ origin, cellSize, size, data, seconds, nodes, solidNodes }`; the same input gives the same bytes.
*/
-export function traceIrradianceGrid(parts, lighting, { resolution = DEFAULT_GRID_RESOLUTION, samples = DEFAULT_GRID_SAMPLES, bounces = 3, maxSeconds = 60 } = {}) {
+export function traceIrradianceGrid(parts, lighting, { resolution = true, samples = DEFAULT_GRID_SAMPLES, bounces = 3, maxSeconds = 60 } = {}) {
if (!Array.isArray(parts) || parts.length === 0 || parts.length > MAX_BAKE_PARTS) {
throw new RangeError(`The irradiance grid takes from 1 to ${MAX_BAKE_PARTS} parts.`);
}
@@ -279,9 +291,15 @@ export function traceIrradianceGrid(parts, lighting, { resolution = DEFAULT_GRID
};
fillInvalidNodes(data, valid, layout.size);
smoothNodes(data, valid, layout.size, isConnected);
- return { ...layout, data, nodes: nodeCount, solidNodes, seconds: (performance.now() - started) / 1000 };
+ const grid = { ...layout, data, nodes: nodeCount, solidNodes, seconds: (performance.now() - started) / 1000 };
+ // The fog's scattering reads the grid without importing this lazy module: the mean light arriving at a point from every
+ // direction is the constant term of the fit. Not enumerable, so the report that copies the grid's fields stays plain data.
+ Object.defineProperty(grid, 'meanRadiance', { value: position => evaluateIrradianceGrid(grid, position, NO_DIRECTION) });
+ return grid;
}
+const NO_DIRECTION = Object.freeze([0, 0, 0]);
+
/** The light a surface with this normal receives from the grid at a point (trilinear, as the shader reads it): what the shader multiplies by the albedo. */
export function evaluateIrradianceGrid(grid, position, normal) {
const coordinates = [0, 1, 2].map(axis => Math.min(Math.max((position[axis] + normal[axis] * grid.cellSize * 0.5 - grid.origin[axis]) / grid.cellSize, 0), grid.size[axis] - 1));
diff --git a/tests/irradiance-grid.test.js b/tests/irradiance-grid.test.js
index ee64f782..0359062b 100644
--- a/tests/irradiance-grid.test.js
+++ b/tests/irradiance-grid.test.js
@@ -33,13 +33,13 @@ const sun = { keyColor: [3, 3, 3], lightDirection: [0.2, 1, 0.1] };
test('the recipe field is true, an integer node count or off, and anything else is refused', () => {
assert.equal(normalizeIrradianceGrid(undefined), null);
assert.equal(normalizeIrradianceGrid(false), null);
- assert.equal(normalizeIrradianceGrid(true), 24);
+ assert.equal(normalizeIrradianceGrid(true), true);
assert.equal(normalizeIrradianceGrid(16), 16);
for (const bad of [4, 49, 1.5, 'big', {}]) {
assert.throws(() => normalizeIrradianceGrid(bad), /bake\.irradianceGrid/);
}
assert.equal(normalizeAgentBake({ preset: 'draft' }).irradianceGrid, undefined);
- assert.equal(normalizeAgentBake({ preset: 'draft', irradianceGrid: true }).irradianceGrid, 24);
+ assert.equal(normalizeAgentBake({ preset: 'draft', irradianceGrid: true }).irradianceGrid, true);
});
test('a warm floor sends warm light to a wall the sun never reaches, and the same input gives the same bytes', () => {