3D engine that AI agents drive
#506: automatic grid density fits the bake time limit, fog in a grid scene reads the grid and counts the sky only in its ambient
Nguyen Xuan An020fb22697d9
diff --git a/changes/506-diffuse.md b/changes/506-diffuse.mdnew file mode 100644index 00000000..df4cd305--- /dev/null+++ b/changes/506-diffuse.md@@ -0,0 +1 @@+- Irradiance grid (issue #506): `irradianceGrid: true` now picks up to 48 nodes along the longest side within 8,192 nodes, instead of 24, so a long hall gets a cell about half the size. A first try with a 32,768 node budget made a closed 68 m hall take 40 s, past the 20 s bake limit, and the scene was drawn with no grid at all; the budget keeps every reference hall inside the limit. In a grid scene the fog's bounce is now the grid's mean light and the fog's ambient counts the sky only. FLIP against Cycles: `temple-hall-a` 0.1454 to 0.1323, `temple-hall-b` 0.1211 to 0.1085, `temple-hall-c` 0.1078 to 0.0874, `temple-hall` 0.0576 to 0.0513, `dark-shaft` 0.0569 to 0.0732.diff --git a/docs/light-bake.md b/docs/light-bake.mdindex a5999ce8..b3b36c1d 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` 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.+`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 8,192 nodes, so the trace fits the bake's time limit) 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/docs/volumetric.md b/docs/volumetric.mdindex f797c043..e03f139b 100644--- a/docs/volumetric.md+++ b/docs/volumetric.md@@ -64,3 +64,5 @@ Resolved depth adds four estimated bytes per rendered pixel, or 2,073,600 bytesThis implements one bounded medium and one directional scattering source. Point-light scattering, multiple scattering, attenuation toward the source, transparent depth, volume grids, particles, temporal accumulation and half-resolution rendering remain unfinished. The legacy film explosion is separate and does not use this depth-clipped medium. Fog runs after bloom extraction, so scattering itself does not feed bloom.Run `node scripts/smoke-volumetric.mjs` with the development server running for native GPU extinction, foreground clipping, directional shadow, long-distance ray, resource reuse and disposal checks. Run `node scripts/benchmark-volumetric.mjs` for a fixed laboratory comparison and deterministic noise seeking. These fixtures establish behavior, not cinematic trailer quality.++In a scene with an irradiance grid (`bake.irradianceGrid`) the bounce term is the grid's mean light at the cell instead of the fog's own 64 rays: the grid already holds the sun's bounce, the lights and the emission over several bounces, so the sun is not left out there. The fog's ambient then stands for the sky only (it follows the cell's sky visibility, with no floor for the sun's shadow), because the grid replaces what the ambient stood in for. Measured on the reference halls (FLIP against Cycles, lower is better): `temple-hall-a` 0.1454 to 0.1323, `temple-hall-b` 0.1211 to 0.1085, `temple-hall-c` 0.1078 to 0.0874, `temple-hall` 0.0576 to 0.0513. `dark-shaft`, a small room with dense fog, goes from 0.0569 to 0.0732: the grid's light in its shade is about three times the fog's old stand-in, and Cycles draws that shade darker still.diff --git a/src/irradiance-grid.js b/src/irradiance-grid.jsindex 1da58028..9c5d44dc 100644--- a/src/irradiance-grid.js+++ b/src/irradiance-grid.js@@ -15,11 +15,13 @@ 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 });-// `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).+// `true` picks the cell size itself: at most this many nodes along the longest side and at most AUTO_NODE_BUDGET in all. The+// budget keeps the trace inside the bake's time limit (20 s on the page's thread): a closed 68 m hall with 31,768 nodes took+// 40 s, the bake was refused and the scene was drawn with no grid at all (temple-hall-b, FLIP 0.1535 against 0.1211 with a grid).+// A long hall still gets about twice the resolution of the old 24 nodes, and a coarse 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 AUTO_NODE_BUDGET = 8192;const DEFAULT_GRID_SAMPLES = 32;const VALIDITY_DIRECTIONS = buildValidityDirections();const BACKFACE_INVALID_SHARE = 0.5;diff --git a/src/post.js b/src/post.jsindex 78d5df9c..75b0c4d8 100644--- a/src/post.js+++ b/src/post.js@@ -38,6 +38,7 @@ uniform vec3 volumeDrift;uniform int volumeSteps;uniform float volumeAnisotropy;uniform float volumeShadowAmbient;+uniform float volumeGridFog; // 1 when an irradiance grid lights the fog: the ambient then stands for the sky onlyuniform float volumeNoiseAmount;uniform float volumeNoiseScale;uniform float volumeMaxDistance;@@ -105,7 +106,9 @@ vec3 volumeFog(vec3 background) {}vec4 skyCell = POST_SKY ? texture(fogSkyVisibility, (position - volumeMinimum) / (volumeMaximum - volumeMinimum)) : vec4(0);float shadowShare = POST_SKY ? 0.1 + 0.9 * min(1.0, 30.0 * skyCell.r) : volumeShadowAmbient;- vec3 scattering = volumeColor * (volumeAmbient * mix(shadowShare, 1.0, visibility) + volumeLightColor * phase * visibility + 0.6 * skyCell.gba);+ float ambientShare = mix(shadowShare, 1.0, visibility);+ if (volumeGridFog > 0.5) ambientShare = min(1.0, 30.0 * skyCell.r);+ vec3 scattering = volumeColor * (volumeAmbient * ambientShare + volumeLightColor * phase * visibility + 0.6 * skyCell.gba);accumulated += transmittance * opacity * scattering;transmittance *= 1.0 - opacity;if (transmittance < 0.001) break;diff --git a/src/renderer.js b/src/renderer.jsindex bd318956..3458a744 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -46,7 +46,7 @@ const DEFORMING_TAGS = new Set(['foliage', 'grass', 'water', 'fire', 'smoke']);// strength, so they can be culled and given levels like rigid parts.const SWAY_REACH_PER_STRENGTH = 0.56;-const POST_UNIFORMS = ['transparent', 'frameTexture', 'bloomTexture', 'bloomStrength', 'hdrEnabled', 'grade', 'vignette', 'grainAmount', 'resolution', 'monochrome', 'fade', 'cinematic', 'time', 'volumeExplosion', 'explosionProgress', 'inverseViewProjection', 'eye', 'sceneDepth', 'volumeShadowMap', 'volumeLightProjection', 'volumeLightDirection', 'volumeLightColor', 'volumeDensity', 'volumeMinimum', 'volumeMaximum', 'volumeColor', 'volumeAmbient', 'volumeDrift', 'volumeSteps', 'volumeAnisotropy', 'volumeShadowAmbient', 'volumeNoiseAmount', 'volumeNoiseScale', 'volumeHeightFalloff', 'volumeMaxDistance', 'fogSkyVisibility', 'orthographic', 'occlusionTexture', 'occlusionStrength', 'occlusionTexel'];+const POST_UNIFORMS = ['transparent', 'frameTexture', 'bloomTexture', 'bloomStrength', 'hdrEnabled', 'grade', 'vignette', 'grainAmount', 'resolution', 'monochrome', 'fade', 'cinematic', 'time', 'volumeExplosion', 'explosionProgress', 'inverseViewProjection', 'eye', 'sceneDepth', 'volumeShadowMap', 'volumeLightProjection', 'volumeLightDirection', 'volumeLightColor', 'volumeDensity', 'volumeMinimum', 'volumeMaximum', 'volumeColor', 'volumeAmbient', 'volumeDrift', 'volumeSteps', 'volumeAnisotropy', 'volumeShadowAmbient', 'volumeGridFog', 'volumeNoiseAmount', 'volumeNoiseScale', 'volumeHeightFalloff', 'volumeMaxDistance', 'fogSkyVisibility', 'orthographic', 'occlusionTexture', 'occlusionStrength', 'occlusionTexel'];function postUniformLocations(context, program) {return Object.fromEntries(POST_UNIFORMS.map(name => [name, context.getUniformLocation(program, name)]));@@ -1813,6 +1813,7 @@ export class Renderer67 {context.uniform1f(post.uniforms.volumeHeightFalloff, volume.heightFalloff);context.uniform1f(post.uniforms.orthographic, frame.projection === 'orthographic' ? 1 : 0);context.uniform1i(post.uniforms.fogSkyVisibility, 5);+ context.uniform1f(post.uniforms.volumeGridFog, this.sceneParts.some(part => part.material?.irradianceGrid) ? 1 : 0);context.activeTexture(context.TEXTURE2);context.bindTexture(context.TEXTURE_2D, effects.bloom.strength > 0 ? this.bloomTargets[1].texture : this.whiteTexture);context.uniform1i(post.uniforms.bloomTexture, 2);