3D engine that AI agents drive
Irradiance grid: sky visibility per node, wall-aware fill and smoothing, no sun bounce on a grid scene (issue #506)
Nguyen Xuan An054e2b5e9880
diff --git a/bench/reference/run.mjs b/bench/reference/run.mjsindex ea445798..f9537511 100644--- a/bench/reference/run.mjs+++ b/bench/reference/run.mjs@@ -147,7 +147,7 @@ async function renderBackend(backend, serverUrl, graphics) {// Object probes are on by default, as in a recipe, so the baseline scores what a user gets.const objectProbesThisScene = !argumentsGiven.includes('--no-object-probes') && (objectProbeSceneIds === null || objectProbeSceneIds.includes(id));const probes = probesThisScene || (objectProbesThisScene ? 'objects' : false);- const bake = bakesThisScene ? { ...skyOnlyOptions, ...(bakePreset ? { preset: bakePreset } : {}), ...(argumentsGiven.includes('--bake-grid') ? { irradianceGrid: true } : {}), ...(bakeBackend ? { backend: bakeBackend } : {}), ...(bakeSamples ? { samples: bakeSamples } : {}), ...(bakeDensity ? { texelsPerUnit: bakeDensity } : {}) } : null;+ const bake = bakesThisScene ? { ...skyOnlyOptions, ...(bakePreset ? { preset: bakePreset } : {}), ...(argumentsGiven.includes('--bake-grid') ? { irradianceGrid: Number(argumentsGiven[argumentsGiven.indexOf('--bake-grid') + 1]) || true } : {}), ...(bakeBackend ? { backend: bakeBackend } : {}), ...(bakeSamples ? { samples: bakeSamples } : {}), ...(bakeDensity ? { texelsPerUnit: bakeDensity } : {}) } : null;const result = await page.evaluate(([sceneId, shouldDescribe, ambientOcclusion, ambientOcclusionRadius, bakeOptions, cascadeCount, withProbes, ambientScale, selectedViewTransform]) => window.renderReferenceScene(sceneId, { describe: shouldDescribe, ambientOcclusion, ambientOcclusionRadius, bake: bakeOptions, cascades: cascadeCount, probes: withProbes, ambient: ambientScale, viewTransform: selectedViewTransform }), [id, describe, ambientOcclusion, ambientOcclusionRadius, bake, cascades, probes, ambient, viewTransform]);if (result.glError !== 0) throw new Error(`${id} on ${backend} gave WebGL error ${result.glError}.`);graphics[backend] = result.graphics;diff --git a/changes/506-grid-reference.md b/changes/506-grid-reference.mdnew file mode 100644index 00000000..40431c60--- /dev/null+++ b/changes/506-grid-reference.md@@ -0,0 +1 @@+- Fix the opt-in `bake.irradianceGrid` lifting closed rooms (issue #506). Against Cycles on the five reference halls the grid scored 0.15 to 0.19 FLIP and was 1.6 to 1.8 times too bright; it now scores 0.0576, 0.0569, 0.1455, 0.1211 and 0.1077 (temple-hall, dark-shaft, temple-hall-a, -b, -c), better than the lightmap bake on temple-hall-a and -b and within 0.003 on temple-hall and dark-shaft, in about a second where the bake takes 14 to 57. Three causes: the world's light reached the grid's surfaces unoccluded (each node now stores the share of sky it sees and the shader scales the environment light by it, as the lightmap path does with its sky visibility map); nodes inside a wall were filled with the mean of both sides, which carried the sunlit outside into the room (they now copy the darkest neighbour); and the smoothing blurred across walls (a neighbour counts only when a ray between the two nodes is free). A grid scene also no longer gets the sun-bounce point lights or the ground bounce, which doubled the light the grid already holds. `bake.irradianceGrid` stays opt-in. The reference harness takes `--bake-grid [nodes]`.diff --git a/changes/506-grid-reference.size.md b/changes/506-grid-reference.size.mdnew file mode 100644index 00000000..1d5e8882--- /dev/null+++ b/changes/506-grid-reference.size.md@@ -0,0 +1 @@+- scene:agent-scene+lazy: +268 bytes gzip (375814 to 376082) in the lazy irradiance grid module: the sky texel per node, the darkest-neighbour fill, the ray-checked smoothing and one more shader slot. Accepted for #506: it takes the opt-in grid from 1.6 to 1.8 times too bright to within 0.003 FLIP of the lightmap bake on two halls and ahead of it on two; the core entries do not grow.diff --git a/src/ground-bounce.js b/src/ground-bounce.jsindex cf3fb47e..572e667f 100644--- a/src/ground-bounce.js+++ b/src/ground-bounce.js@@ -118,7 +118,7 @@ export function withGroundBounce(map, { albedo, keyColor = [0, 0, 0], lightDirecconst cache = new WeakMap()function isDrawnWithBakedLight(parts) {- return parts.some(part => part.material?.lightMap)+ return parts.some(part => part.material?.lightMap || part.material?.irradianceGrid)}function isClosedRoom(parts) {diff --git a/src/irradiance-grid.js b/src/irradiance-grid.jsindex 995ee7c1..2daa68be 100644--- a/src/irradiance-grid.js+++ b/src/irradiance-grid.js@@ -20,7 +20,10 @@ const DEFAULT_GRID_SAMPLES = 32;const VALIDITY_DIRECTIONS = buildValidityDirections();const BACKFACE_INVALID_SHARE = 0.5;const FILL_PASSES = 12;-const COEFFICIENTS_PER_NODE = 12;+const COEFFICIENTS_PER_NODE = 16;+// The fourth texel of a node holds the share of sky the node sees (rays that leave the scene on their first leg), in the same+// constant plus slope form, so the shader can darken the world's light where the sky cannot reach.+const SKY_SLOT = 12;// The grid is one 3D texture: node (x, y, z) takes three texels along x, one per colour channel, each holding the channel's// constant term and its x, y and z terms.// Unit 16, past the 16 the renderer's other samplers use (splat layers take 11 to 14, local shadows 15). A unit index is limited by@@ -61,6 +64,13 @@ export function normalizeIrradianceGrid(input) {return resolution;}+function nodePoint(layout, node, point) {+ const [width, height] = layout.size;+ point[0] = layout.origin[0] + (node % width) * layout.cellSize;+ point[1] = layout.origin[1] + (Math.floor(node / width) % height) * layout.cellSize;+ point[2] = layout.origin[2] + Math.floor(node / (width * height)) * layout.cellSize;+}+function gridLayout(parts, resolution) {const minimum = [Infinity, Infinity, Infinity];const maximum = [-Infinity, -Infinity, -Infinity];@@ -110,8 +120,10 @@ function fillInvalidNodes(data, valid, size) {continue;}const coordinates = [node % width, Math.floor(node / width) % height, Math.floor(node / (width * height))];- const sum = new Float32Array(COEFFICIENTS_PER_NODE);- let count = 0;+ // A node inside a wall copies its darkest valid neighbour. A mean would carry the sunlit outside of a thin wall into the+ // room, because a surface on the wall reads this node through the trilinear lookup.+ let darkest = -1;+ let darkestLight = Infinity;for (let axis = 0; axis < 3; axis++) {for (const step of [-1, 1]) {const moved = coordinates[axis] + step;@@ -122,15 +134,17 @@ function fillInvalidNodes(data, valid, size) {if (!valid[neighbour]) {continue;}- for (let slot = 0; slot < COEFFICIENTS_PER_NODE; slot++) {- sum[slot] += data[neighbour * COEFFICIENTS_PER_NODE + slot];+ const base = neighbour * COEFFICIENTS_PER_NODE;+ const light = data[base] + data[base + 4] + data[base + 8];+ if (light < darkestLight) {+ darkestLight = light;+ darkest = neighbour;}- count++;}}- if (count > 0) {+ if (darkest >= 0) {for (let slot = 0; slot < COEFFICIENTS_PER_NODE; slot++) {- data[node * COEFFICIENTS_PER_NODE + slot] = sum[slot] / count;+ data[node * COEFFICIENTS_PER_NODE + slot] = data[darkest * COEFFICIENTS_PER_NODE + slot];}newlyValid.push(node);}@@ -146,7 +160,7 @@ function fillInvalidNodes(data, valid, size) {// A [1 2 1] x [1 2 1] x [1 2 1] filter over the coefficients of valid neighbours removes the low-frequency noise of the few// samples each node gets, at the cost of a little blur across a wall. Nodes inside solids keep their filled values.-function smoothNodes(data, valid, size) {+function smoothNodes(data, valid, size, isConnected) {const [width, height, depth] = size;const source = Float32Array.from(data);const weights = [1, 2, 1];@@ -169,7 +183,8 @@ function smoothNodes(data, valid, size) {continue;}const neighbour = nx + width * (ny + height * nz);- if (!valid[neighbour]) {+ // A neighbour across a wall is another room's light: it only counts when a ray between the two nodes is free.+ if (!valid[neighbour] || (neighbour !== node && !isConnected(node, neighbour))) {continue;}const weight = weights[dx + 1] * weights[dy + 1] * weights[dz + 1];@@ -216,6 +231,7 @@ export function traceIrradianceGrid(parts, lighting, { resolution = DEFAULT_GRIDconst valid = new Uint8Array(nodeCount);const point = [0, 0, 0];const axisLight = [new Float64Array(3), new Float64Array(3)];+ const axisSky = [0, 0];const axisNormal = [0, 0, 0];const [width, height] = layout.size;let solidNodes = 0;@@ -235,8 +251,11 @@ export function traceIrradianceGrid(parts, lighting, { resolution = DEFAULT_GRIDfor (let side = 0; side < 2; side++) {axisNormal.fill(0);axisNormal[axis] = side === 0 ? 1 : -1;- gather(point, axisNormal, node * 6 + axis * 2 + side, axisLight[side]);+ axisSky[side] = gather(point, axisNormal, node * 6 + axis * 2 + side, axisLight[side]);}+ const skyBase = node * COEFFICIENTS_PER_NODE + SKY_SLOT;+ data[skyBase] += (axisSky[0] + axisSky[1]) / 6;+ data[skyBase + 1 + axis] = (axisSky[0] - axisSky[1]) / 2;for (let channel = 0; channel < 3; channel++) {const base = node * COEFFICIENTS_PER_NODE + channel * 4;data[base] += (axisLight[0][channel] + axisLight[1][channel]) / 6;@@ -244,8 +263,22 @@ export function traceIrradianceGrid(parts, lighting, { resolution = DEFAULT_GRID}}}+ const traced = Uint8Array.from(valid);+ const target = [0, 0, 0];+ const isConnected = (from, to) => {+ if (!traced[from] || !traced[to]) {+ return false;+ }+ nodePoint(layout, from, point);+ nodePoint(layout, to, target);+ const dx = target[0] - point[0];+ const dy = target[1] - point[1];+ const dz = target[2] - point[2];+ const length = Math.hypot(dx, dy, dz);+ return tracer.trace(point[0], point[1], point[2], dx / length, dy / length, dz / length, length, true).nearestTriangle < 0;+ };fillInvalidNodes(data, valid, layout.size);- smoothNodes(data, valid, layout.size);+ smoothNodes(data, valid, layout.size, isConnected);return { ...layout, data, nodes: nodeCount, solidNodes, seconds: (performance.now() - started) / 1000 };}@@ -293,26 +326,27 @@ registerShaderFeatureCode('irradianceGrid', Object.freeze({'uniform vec3 irradianceGridSize;',// The grid stores light for the cosine lobe already, so this is the same unit as environmentDiffuse. The x axis is// lerped by hand because three nodes share a row of texels (one per colour channel); y and z use the hardware filter.- 'vec3 irradianceGridDiffuse(vec3 position, vec3 normal) {',+ 'vec4 irradianceGridSample(vec3 position, vec3 normal) {',' vec3 grid = clamp((position + normal * irradianceGridOrigin.w * 0.5 - irradianceGridOrigin.xyz) / irradianceGridOrigin.w, vec3(0.0), irradianceGridSize - 1.0);',' float left = floor(grid.x);',' float right = min(left + 1.0, irradianceGridSize.x - 1.0);',' float blend = grid.x - left;',' vec2 across = (grid.yz + 0.5) / irradianceGridSize.yz;',- ' float rowWidth = 3.0 * irradianceGridSize.x;',+ ' float rowWidth = 4.0 * irradianceGridSize.x;',' vec4 direction = vec4(1.0, normal);',- ' vec3 result = vec3(0.0);',- ' for (int channel = 0; channel < 3; channel++) {',+ ' vec4 result = vec4(0.0);',+ ' for (int channel = 0; channel < 4; channel++) {',' float slot = float(channel) + 0.5;',- ' vec4 first = texture(irradianceGridTexture, vec3((3.0 * left + slot) / rowWidth, across));',- ' vec4 second = texture(irradianceGridTexture, vec3((3.0 * right + slot) / rowWidth, across));',+ ' vec4 first = texture(irradianceGridTexture, vec3((4.0 * left + slot) / rowWidth, across));',+ ' vec4 second = texture(irradianceGridTexture, vec3((4.0 * right + slot) / rowWidth, across));',' result[channel] = dot(mix(first, second, blend), direction);',' }',- ' return max(result, vec3(0.0));',+ ' return max(result, vec4(0.0));','}','',].join('\n'),- irradianceGrid: ' if (irradianceGridEnabled > 0.5) radiance += color * (1.0 - metallic) * irradianceGridDiffuse(worldPosition, normal);\n',+ irradianceGridOcclusion: ' vec4 irradianceGridValue = irradianceGridEnabled > 0.5 ? irradianceGridSample(worldPosition, normal) : vec4(0.0, 0.0, 0.0, 1.0);\n occlusion *= min(irradianceGridValue.a, 1.0);\n',+ irradianceGrid: ' if (irradianceGridEnabled > 0.5) radiance += color * (1.0 - metallic) * irradianceGridValue.rgb;\n',}));const rendererStates = new WeakMap();@@ -340,7 +374,7 @@ function uploadGrid(renderer, grid) {const texture = context.createTexture();const previous = context.getParameter(context.TEXTURE_BINDING_3D);context.bindTexture(context.TEXTURE_3D, texture);- context.texImage3D(context.TEXTURE_3D, 0, context.RGBA16F, nodesX * 3, nodesY, nodesZ, 0, context.RGBA, context.FLOAT, grid.data);+ context.texImage3D(context.TEXTURE_3D, 0, context.RGBA16F, nodesX * 4, nodesY, nodesZ, 0, context.RGBA, context.FLOAT, grid.data);context.texParameteri(context.TEXTURE_3D, context.TEXTURE_MIN_FILTER, context.LINEAR);context.texParameteri(context.TEXTURE_3D, context.TEXTURE_MAG_FILTER, context.LINEAR);for (const wrap of [context.TEXTURE_WRAP_S, context.TEXTURE_WRAP_T, context.TEXTURE_WRAP_R]) {diff --git a/src/shaders.js b/src/shaders.jsindex c96c75be..a0effe95 100644--- a/src/shaders.js+++ b/src/shaders.js@@ -675,7 +675,7 @@ ${featureCode("linkArea")} vec3 lightNormal = areaLightNormals[index].xyz;// Leaf and grass cards keep a faint sheen; full sky reflection washed grazing cards grey-blue.float skyReflection = canopyShaded ? 0.3 : 1.0;vec3 reflected = reflect(-viewDirection, specularNormal);-${featureCode("probeReflection")} if (ENVIRONMENT_ENABLED) {+${featureCode("probeReflection")}${featureCode("irradianceGridOcclusion")} if (ENVIRONMENT_ENABLED) {radiance += environmentLighting(normal, specularNormal, baseReflectance, viewDirection, reflected, color, metallic, roughness, occlusion, ambientIntensity, skyReflection);} else {// Outdoors the surroundings are sky and haze; reflecting the studio softboxes there lit grazing leaves white and cyan.diff --git a/src/sun-bounce.js b/src/sun-bounce.jsindex 053338b5..6e528ec4 100644--- a/src/sun-bounce.js+++ b/src/sun-bounce.js@@ -192,7 +192,7 @@ function lightOfPatch(group, strength, tracer, probeOffset, sceneSize) {* `maximumLights` caps how many patches there are.*/export function findSunBounceLights(parts, environment, { strength = 1, maximumLights = SUN_BOUNCE_PATCH_LIGHTS } = {}) {- if (strength <= 0 || maximumLights <= 0 || !hasSun(environment) || parts.some(part => part.material?.lightMap)) return [];+ if (strength <= 0 || maximumLights <= 0 || !hasSun(environment) || parts.some(part => part.material?.lightMap || part.material?.irradianceGrid)) return [];const share = fittedClosedRoomShare(parts, new Map(parts.map(part => [part.name, part.matrix])));if (share === null || share < CLOSED_ROOM_SHARE) return [];const surfaces = parts.filter(isBounceSurface);diff --git a/tests/irradiance-grid.test.js b/tests/irradiance-grid.test.jsindex 7ac372a9..ee64f782 100644--- a/tests/irradiance-grid.test.js+++ b/tests/irradiance-grid.test.js@@ -53,6 +53,19 @@ test('a warm floor sends warm light to a wall the sun never reaches, and the samassert.ok(lookingAtFloor[0] > lookingAtCeiling[0], 'the lit floor is seen from above, so the downward lobe is brighter');});+test('the grid stores the sky a node sees: little in a closed room, a lot under open sky', () => {+ const skyAtRoomCentre = grid => {+ const [x, y, z] = [0, 1.5, 0].map((value, axis) => Math.round((value - grid.origin[axis]) / grid.cellSize));+ return grid.data[(x + grid.size[0] * (y + grid.size[1] * z)) * 16 + 12];+ };+ const withoutCeiling = sunlitRoom().filter(part => part.name !== 'ceiling-left' && part.name !== 'ceiling-right');+ const closed = traceIrradianceGrid(withoutCeiling.concat([placed('roof', [0, 3.05, 0], [6.2, 0.1, 6.2], [0.8, 0.8, 0.8])]), sun, { resolution: 10, samples: 16, bounces: 1 });+ const open = traceIrradianceGrid(withoutCeiling, sun, { resolution: 10, samples: 16, bounces: 1 });+ const closedSky = skyAtRoomCentre(closed);+ const openSky = skyAtRoomCentre(open);+ assert.ok(closedSky < 0.05 && openSky > 0.2, `closed ${closedSky}, open ${openSky}`);+});+test('nodes inside a solid are found and filled from their neighbours', () => {const solid = traceIrradianceGrid([placed('block', [0, 0, 0], [4, 4, 4], [0.5, 0.5, 0.5])], sun, { resolution: 9, samples: 4, bounces: 1 });assert.ok(solid.solidNodes > 0 && solid.solidNodes < solid.nodes, `${solid.solidNodes} of ${solid.nodes}`);@@ -61,7 +74,7 @@ test('nodes inside a solid are found and filled from their neighbours', () => {test('nodes inside walls are filled and every coefficient is finite', () => {const grid = traceIrradianceGrid(sunlitRoom(), sun, { resolution: 10, samples: 8, bounces: 1 });assert.ok(grid.data.every(Number.isFinite));- assert.equal(grid.data.length, grid.nodes * 12);+ assert.equal(grid.data.length, grid.nodes * 16);});test('placing the grid wraps each material once and keeps reading the live material', () => {@@ -74,7 +87,8 @@ test('placing the grid wraps each material once and keeps reading the live mater});test('the shader carries the grid code only for a renderer that asks for the feature', () => {- assert.match(createFragmentShader(['irradianceGrid']), /irradianceGridDiffuse\(worldPosition, normal\)/);+ assert.match(createFragmentShader(['irradianceGrid']), /irradianceGridSample\(worldPosition, normal\)/);+ assert.match(createFragmentShader(['irradianceGrid']), /occlusion \*= min\(irradianceGridValue\.a, 1\.0\)/);assert.doesNotMatch(createFragmentShader([]), /irradianceGrid/);});@@ -110,6 +124,6 @@ test('a scene with splat layers and the grid keeps the two on different textureconst taken = [...Object.values(SPLAT_UNITS), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15];assert.ok(!taken.includes(IRRADIANCE_GRID_TEXTURE_UNIT), 'the grid sampler has its own unit');const source = createFragmentShader(['splat', 'irradianceGrid']);- assert.match(source, /irradianceGridDiffuse/);+ assert.match(source, /irradianceGridSample/);assert.match(source, /splatColor/);});