Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Add bake.gridFog: the irradiance grid scatters in the volume fog, opt in (issue #543)

Nguyen Xuan Anf0c2c94170c4
diff --git a/bench/reference/harness.js b/bench/reference/harness.js
index 3445d8ef..0f644720 100644
--- a/bench/reference/harness.js
+++ b/bench/reference/harness.js
@@ -177,7 +177,7 @@ function describeScene(id, scene) {
// Blender always gets the original parts: the bake is 67js's approximation of what Cycles computes.
async function bakeScene(scene, bakeOptions) {
if (bakeOptions.preset) {
- return bakeScenePreset(scene, bakeOptions.preset, bakeOptions.irradianceGrid, { bounces: bakeOptions.bounces });
+ return bakeScenePreset(scene, bakeOptions.preset, bakeOptions.irradianceGrid, { bounces: bakeOptions.bounces, gridFog: bakeOptions.gridFog });
}
const { bakeLightmapsAsync } = await import('/src/light-bake-gpu.js');
const { pointLights, keyColor, lightDirection } = scene.environment;
@@ -191,7 +191,7 @@ async function bakeScene(scene, bakeOptions) {
// from --probes, as in the other rows.
async function bakeScenePreset(scene, preset, irradianceGrid = false, overrides = {}) {
const { normalizeAgentBake, bakeAgentParts } = await import('/src/agent-bake.js');
- const settings = normalizeAgentBake({ preset, probes: false, ...(overrides.bounces ? { bounces: overrides.bounces } : {}), ...(irradianceGrid ? { irradianceGrid } : {}) });
+ const settings = normalizeAgentBake({ preset, probes: false, ...(overrides.bounces ? { bounces: overrides.bounces } : {}), ...(irradianceGrid ? { irradianceGrid } : {}), ...(overrides.gridFog ? { gridFog: true } : {}) });
const started = performance.now();
let baked;
try {
diff --git a/bench/reference/run.mjs b/bench/reference/run.mjs
index 7df60937..4e76eb42 100644
--- a/bench/reference/run.mjs
+++ b/bench/reference/run.mjs
@@ -118,7 +118,7 @@ const sceneIdsGiven = lookIds ?? (sceneFlag >= 0 ? argumentsGiven[sceneFlag + 1]
// The fresh bake.json lands in the work directory and is checked against the committed one (compare-scores.mjs); --update-baseline replaces
// the committed bake.json and images/<id>-baked.jpg with it.
const bakeScoreRun = bakedSceneIds !== null && !sceneSet && !pathTracerFlag;
-const bakeVariantFlags = ['--no-glossy', '--sky-only', '--bake-preset', '--bake-grid', '--no-bake-worker', '--bake-bounces', '--bake-backend', '--bake-samples', '--bake-density', '--probes', '--ambient', '--ambient-occlusion', '--cascades', '--no-object-probes', '--object-probes', '--no-water', '--ssr-water', '--no-ground-bounce', '--floor-sky', '--floor-sky-rooms', '--no-floor-sky'];
+const bakeVariantFlags = ['--no-glossy', '--sky-only', '--bake-preset', '--bake-grid', '--bake-grid-fog', '--no-bake-worker', '--bake-bounces', '--bake-backend', '--bake-samples', '--bake-density', '--probes', '--ambient', '--ambient-occlusion', '--cascades', '--no-object-probes', '--object-probes', '--no-water', '--ssr-water', '--no-ground-bounce', '--floor-sky', '--floor-sky-rooms', '--no-floor-sky'];
const bakeScoreIsBaseline = bakeScoreRun && argumentsGiven.includes('--update-baseline') && !bakeVariantFlags.some(flag => argumentsGiven.includes(flag));
const onlyScenes = sceneIdsGiven ?? (bakeScoreRun ? bakedSceneIds : null);
const skipCpu = skipCpuFlag || bakeScoreRun || Boolean(viewTransform);
@@ -148,7 +148,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: Number(argumentsGiven[argumentsGiven.indexOf('--bake-grid') + 1]) || true } : {}), ...(argumentsGiven.includes('--bake-bounces') ? { bounces: Number(argumentsGiven[argumentsGiven.indexOf('--bake-bounces') + 1]) } : {}), ...(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 } : {}), ...(argumentsGiven.includes('--bake-grid-fog') ? { gridFog: true } : {}), ...(argumentsGiven.includes('--bake-bounces') ? { bounces: Number(argumentsGiven[argumentsGiven.indexOf('--bake-bounces') + 1]) } : {}), ...(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/543-grid-fog.md b/changes/543-grid-fog.md
new file mode 100644
index 00000000..3296186a
--- /dev/null
+++ b/changes/543-grid-fog.md
@@ -0,0 +1 @@
+- bake.gridFog: true (with irradianceGrid) lets the irradiance grid scatter in the scene's volume fog, off by default, for sunlit closed rooms with fog (#543)
diff --git a/changes/543-grid-fog.size.md b/changes/543-grid-fog.size.md
new file mode 100644
index 00000000..ec4166a6
--- /dev/null
+++ b/changes/543-grid-fog.size.md
@@ -0,0 +1,2 @@
+- scene:agent-scene+lazy: the opt-in grid fog (a volumetric path in the fog, gridFog) brings temple-hall-a and -b closer to Cycles; the code loads only with a bake.
+- goal:agent-scene: gridFog adds a validated bake field (36 bytes gzip); the fog trace itself is in the lazy bake module.
diff --git a/docs/light-bake.md b/docs/light-bake.md
index 11c99250..0313b1c6 100644
--- a/docs/light-bake.md
+++ b/docs/light-bake.md
@@ -78,6 +78,8 @@ An open scene with a floor gets the light the floor bounces without a bake and w
`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.
+`bake: { irradianceGrid: true, gridFog: true }` lets the grid's paths scatter inside the scene's `volumeFog` (opt in, off by default, so a bake without it keeps its bytes). A path scatters in the fog with the probability a photon would, adds the sun at each scattering, and goes on from the point (up to 8 scatterings), so the fog's bounce light reaches the walls. Use it for a sunlit closed room with fog. Measured against Cycles, it brings the sunlit halls closer and makes a dark shaft too bright, so leave it off there. It needs `irradianceGrid` and a scene with fog.
+
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.
## Reflection probes
diff --git a/src/agent-bake.js b/src/agent-bake.js
index 1fd8a598..67e09132 100644
--- a/src/agent-bake.js
+++ b/src/agent-bake.js
@@ -11,7 +11,7 @@ const PRESET_SAMPLES_FLOOR = 16;
// Recipe `bake`: bounced light written into lightmaps when the scene builds. This module loads on demand, so scenes
// without a bake never download the ray tracer.
-const BAKE_FIELDS = ['preset', 'samples', 'bounces', 'texelsPerUnit', 'probes', 'irradianceGrid'];
+const BAKE_FIELDS = ['preset', 'samples', 'bounces', 'texelsPerUnit', 'probes', 'irradianceGrid', 'gridFog'];
const BAKED_MATERIAL_FIELDS = ['emission', 'emissiveColor', 'emissiveMap', 'lightMap', 'lightMapScale', 'occlusionMap', 'occlusionStrength'];
/** Validates a recipe bake section and returns the plain, resolved form stored in the recipe (null clears it). */
@@ -42,6 +42,16 @@ export function normalizeAgentBake(input) {
if (irradianceGrid) {
resolved.irradianceGrid = irradianceGrid;
}
+ // Opt in on top of the grid: the grid's paths scatter in the scene's volume fog. Not stored when off, so the default bake keeps its bytes.
+ if (input.gridFog !== undefined && typeof input.gridFog !== 'boolean') {
+ throw new RangeError('bake.gridFog must be a boolean.');
+ }
+ if (input.gridFog === true) {
+ if (!irradianceGrid) {
+ throw new RangeError('bake.gridFog needs irradianceGrid.');
+ }
+ resolved.gridFog = true;
+ }
for (const field of ['samples', 'bounces']) {
if (!Number.isInteger(resolved[field])) {
throw new RangeError(`Bake ${field} must be an integer.`);
@@ -209,8 +219,10 @@ export function bakeAgentParts(parts, worldMatrices, environment, settings, gene
skyTop: environment.skyTop,
skyBottom: environment.skyBottom,
lightDirection: environment.lightDirection,
- volumeFog: environment.volumeFog,
};
+ if (settings.gridFog) {
+ lighting.volumeFog = environment.volumeFog;
+ }
// A hair, particle or cloth node is drawn from geometry built every frame, which has no lightmap UVs.
const snapshots = parts.map(part => ({
...part,
diff --git a/src/agent-scene-capabilities.js b/src/agent-scene-capabilities.js
index 6cbdcefc..982a9f3d 100644
--- a/src/agent-scene-capabilities.js
+++ b/src/agent-scene-capabilities.js
@@ -273,7 +273,7 @@ export const AGENT_SCENE_CAPABILITIES = Object.freeze({
}),
// Recipe bake: bounced light written into lightmaps at build time (src/light-bake.js, loaded on demand).
bake: Object.freeze({
- fields: 'preset, or samples (1-256), bounces (1-8), texelsPerUnit (1-12) over it; irradianceGrid true or 8-48 nodes: a baked grid of bounced diffuse light in place of lightmaps (opt in, static); probes room by default, false for none, or up to 4 {position, radius?}. Same recipe, same bytes.',
+ fields: 'preset, or samples (1-256), bounces (1-8), texelsPerUnit (1-12) over it; irradianceGrid true or 8-48 nodes: a baked grid of bounced diffuse light in place of lightmaps (opt in, static); gridFog true adds the fog (sunlit closed rooms; dark shafts too bright); probes room by default, false for none, or up to 4 {position, radius?}. Same recipe, same bytes.',
presets: BAKE_PRESETS,
notes: 'Bounced light from point, spot, area and key lights. Reruns when parts, lights, materials or bake change. Skips alpha, skin, morph, hair, cloth (bake.skipped). A preset shrinks to fit, never refuses (bake.used, bake.reduction); explicit settings fail over maxPathWork or past maxSeconds. No environment map or specular. Probes: specular only, none in glTF.',
limits: Object.freeze({ maxSeconds: BAKE_MAXIMUM_SECONDS, workerMaxSeconds: BAKE_WORKER_MAXIMUM_SECONDS, maxPathWork: BAKE_EXPLICIT_PATH_WORK_LIMIT, maxPointLights: MAX_POINT_LIGHTS }),
diff --git a/src/light-bake.js b/src/light-bake.js
index d26dd5d9..2a94f08c 100644
--- a/src/light-bake.js
+++ b/src/light-bake.js
@@ -499,10 +499,8 @@ export function createGatherer({ world, tracer, albedoOfPart, emissionOfPart, di
const albedo = albedoOfPart[owner];
const emission = emissionOfPart[owner];
const irradiance = directIrradiance(hitPoint, normal, owner);
- const lightTarget = fogScatters > 0 ? scatterSum : sum;
for (let channel = 0; channel < 3; channel++) {
- // Light that reaches the point through a fog scattering counts as the fog's, which the fog on screen scatters again itself.
- lightTarget[channel] += throughput[channel] * (emission[channel] + albedo[channel] * irradiance[channel] / Math.PI);
+ sum[channel] += throughput[channel] * (emission[channel] + albedo[channel] * irradiance[channel] / Math.PI);
throughput[channel] *= albedo[channel];
}
if (throughput[0] + throughput[1] + throughput[2] < 1e-4) break;
diff --git a/tests/irradiance-grid.test.js b/tests/irradiance-grid.test.js
index 0359062b..d09021b8 100644
--- a/tests/irradiance-grid.test.js
+++ b/tests/irradiance-grid.test.js
@@ -127,3 +127,22 @@ test('a scene with splat layers and the grid keeps the two on different texture
assert.match(source, /irradianceGridSample/);
assert.match(source, /splatColor/);
});
+
+test('bake.gridFog is a boolean that needs the grid, and is not stored when off', () => {
+ assert.equal(normalizeAgentBake({ irradianceGrid: true, gridFog: true }).gridFog, true);
+ assert.equal(Object.hasOwn(normalizeAgentBake({ irradianceGrid: true, gridFog: false }), 'gridFog'), false);
+ assert.equal(Object.hasOwn(normalizeAgentBake({ irradianceGrid: true }), 'gridFog'), false);
+ assert.throws(() => normalizeAgentBake({ gridFog: true }), /needs bake\.irradianceGrid/);
+ assert.throws(() => normalizeAgentBake({ irradianceGrid: true, gridFog: 'yes' }), /boolean/);
+});
+
+test('the grid scatters in the fog only when the lighting carries the fog', () => {
+ const fog = { minimum: [-4, -1, -4], maximum: [4, 4, 4], density: 0.4, color: [1, 1, 1] };
+ const options = { resolution: 10, samples: 16, bounces: 2 };
+ const without = traceIrradianceGrid(sunlitRoom(), sun, options);
+ const withFog = traceIrradianceGrid(sunlitRoom(), { ...sun, volumeFog: fog }, options);
+ assert.equal(Object.hasOwn(without, 'fogScattered'), false);
+ assert.ok(withFog.fogScattered.some(value => value > 0), 'the sun the fog scattered is recorded');
+ assert.notDeepEqual(Array.from(withFog.data), Array.from(without.data));
+ assert.deepEqual(Array.from(traceIrradianceGrid(sunlitRoom(), sun, options).data), Array.from(without.data));
+});