3D engine that AI agents drive
Trace the grid fog as a volumetric path (issue #543, WIP, not within the rule)
Nguyen Xuan An8730ccdf872e
diff --git a/src/grid-medium.js b/src/grid-medium.jsindex 50c34a8c..2710c4a0 100644--- a/src/grid-medium.js+++ b/src/grid-medium.js@@ -1,61 +1,64 @@import { hashUnit } from './light-bake.js';-import { henyeyGreensteinPhase, resolveTraceVolume, volumeInterval } from './path-tracer-volume.js';+import { henyeyGreensteinPhase, resolveTraceVolume, sampleHenyeyGreensteinCosine, volumeInterval } from './path-tracer-volume.js';// The fog a path of the irradiance grid crosses. It is optional and lives here, so a trace of a scene without fog runs the plain gatherer.-const FOUR_PI = 4 * Math.PI;+/** Scattering events one path may have, as many orders as Cycles keeps for the fog of the reference scenes. */+export const MAXIMUM_FOG_SCATTERS = 8;/*** The fog along one leg of a grid path, from the same box, density, colour and Henyey-Greenstein anisotropy the renderer's* `environment.volumeFog` uses. It returns null when the lighting has no fog. Otherwise it returns- * `medium(origin, direction, distance, throughput, sum, texelIndex, sample, bounce)`: one scattering point drawn from the leg's- * free-flight distribution, the light it scatters toward the path added to `sum`, and `throughput` dimmed by the leg's- * transmittance, which the surface beyond the leg inherits. The scattered light is the sun (one shadow ray) and, when- * `surfaceRadiance(point, direction, out)` is given, the light the nearest surface in one random direction sends back- * (the fog's second scattering, which a bounce off the lit walls feeds). Neither is dimmed by the fog on its way.+ * `medium(origin, direction, distance, throughput, sum, texelIndex, sample, leg)`, which treats the leg as a volumetric path:+ * the path scatters inside the fog with the probability that a photon would, and otherwise reaches the surface beyond with+ * its throughput intact. A scattering adds the sun the point sees (one shadow ray, phase weighted) to `sum`, multiplies+ * `throughput` by the fog colour, moves `origin` to the point, turns `direction` by a Henyey-Greenstein draw, and returns+ * true: the path goes on from there, so light that the fog scatters again and again stays in the fog instead of being lost.*/-export function createGridMedium({ lighting, sun, tracer, scale, surfaceRadiance = null }) {+export function createGridMedium({ lighting, sun, tracer, scale }) {const volume = resolveTraceVolume(lighting.volumeFog);if (!volume) return null;const interval = [0, 0];- const point = [0, 0, 0];- const sampleDirection = [0, 0, 0];- const surfaceColor = [0, 0, 0];- const scattered = [0, 0, 0];const shadowReach = scale * 1e6;- return function medium(origin, direction, distance, throughput, sum, texelIndex, sample, bounce) {- if (!volumeInterval(volume, origin[0], origin[1], origin[2], direction[0], direction[1], direction[2], distance, interval)) return;- const transmittance = Math.exp(-volume.density * (interval[1] - interval[0]));- const scatteredShare = 1 - transmittance;- const seed = bounce * 4;+ return function medium(origin, direction, distance, throughput, sum, texelIndex, sample, leg) {+ if (!volumeInterval(volume, origin[0], origin[1], origin[2], direction[0], direction[1], direction[2], distance, interval)) return false;+ const scatteredShare = 1 - Math.exp(-volume.density * (interval[1] - interval[0]));+ const seed = leg * 4;+ if (hashUnit(texelIndex, sample, seed + 200) >= scatteredShare) return false;const along = interval[0] - Math.log(1 - hashUnit(texelIndex, sample, seed + 201) * scatteredShare) / volume.density;- for (let axis = 0; axis < 3; axis++) point[axis] = origin[axis] + direction[axis] * along;- scattered[0] = 0;- scattered[1] = 0;- scattered[2] = 0;+ for (let axis = 0; axis < 3; axis++) origin[axis] += direction[axis] * along;if (sun) {- const sunBlocked = tracer.trace(point[0], point[1], point[2], sun.direction[0], sun.direction[1], sun.direction[2], shadowReach, true).nearestTriangle >= 0;+ const sunBlocked = tracer.trace(origin[0], origin[1], origin[2], sun.direction[0], sun.direction[1], sun.direction[2], shadowReach, true).nearestTriangle >= 0;if (!sunBlocked) {const phase = henyeyGreensteinPhase(volume.anisotropy, direction[0] * sun.direction[0] + direction[1] * sun.direction[1] + direction[2] * sun.direction[2]);- for (let channel = 0; channel < 3; channel++) scattered[channel] += sun.color[channel] * phase;+ for (let channel = 0; channel < 3; channel++) sum[channel] += throughput[channel] * volume.albedo[channel] * sun.color[channel] * phase;}}- if (surfaceRadiance) {- const height = 1 - 2 * hashUnit(texelIndex, sample, seed + 202);- const ring = Math.sqrt(Math.max(0, 1 - height * height));- const angle = 2 * Math.PI * hashUnit(texelIndex, sample, seed + 203);- sampleDirection[0] = ring * Math.cos(angle);- sampleDirection[1] = height;- sampleDirection[2] = ring * Math.sin(angle);- surfaceRadiance(point, sampleDirection, surfaceColor);- const cosine = direction[0] * sampleDirection[0] + direction[1] * sampleDirection[1] + direction[2] * sampleDirection[2];- // Uniform directions have the density 1 / (4 pi), so the estimate is the radiance times the phase times 4 pi.- const weight = henyeyGreensteinPhase(volume.anisotropy, cosine) * FOUR_PI;- for (let channel = 0; channel < 3; channel++) scattered[channel] += surfaceColor[channel] * weight;- }- for (let channel = 0; channel < 3; channel++) {- sum[channel] += throughput[channel] * scatteredShare * volume.albedo[channel] * scattered[channel];- throughput[channel] *= transmittance;- }+ for (let channel = 0; channel < 3; channel++) throughput[channel] *= volume.albedo[channel];+ turnDirection(direction, sampleHenyeyGreensteinCosine(volume.anisotropy, hashUnit(texelIndex, sample, seed + 202)), 2 * Math.PI * hashUnit(texelIndex, sample, seed + 203));+ return true;};}++/** Turns `direction` (a unit vector) in place by the polar angle whose cosine is given, around it by `azimuth`. */+function turnDirection(direction, cosine, azimuth) {+ const [x, y, z] = direction;+ const sine = Math.sqrt(Math.max(0, 1 - cosine * cosine));+ const helperIsX = Math.abs(x) < 0.9;+ // tangent = normalize(helper x direction), bitangent = direction x tangent+ const tangentX = helperIsX ? 0 : -z;+ const tangentY = helperIsX ? z : 0;+ const tangentZ = helperIsX ? -y : x;+ const tangentLength = Math.hypot(tangentX, tangentY, tangentZ);+ const firstX = tangentX / tangentLength;+ const firstY = tangentY / tangentLength;+ const firstZ = tangentZ / tangentLength;+ const secondX = y * firstZ - z * firstY;+ const secondY = z * firstX - x * firstZ;+ const secondZ = x * firstY - y * firstX;+ const cosineAzimuth = Math.cos(azimuth) * sine;+ const sineAzimuth = Math.sin(azimuth) * sine;+ direction[0] = x * cosine + firstX * cosineAzimuth + secondX * sineAzimuth;+ direction[1] = y * cosine + firstY * cosineAzimuth + secondY * sineAzimuth;+ direction[2] = z * cosine + firstZ * cosineAzimuth + secondZ * sineAzimuth;+}diff --git a/src/irradiance-grid.js b/src/irradiance-grid.jsindex 80ba6e2f..d91fbc52 100644--- a/src/irradiance-grid.js+++ b/src/irradiance-grid.js@@ -2,10 +2,10 @@ import { createTracer } from './ray-query.js';import { readTriangle } from './triangle-math.js';import { registerShaderFeatureCode, layerTextureSupport } from './shader-registry.js';import {- MAX_BAKE_PARTS, buildWorld, createDirectIrradiance, createGatherer, diffuseAlbedo, emittedRadiance, hitNormal, normalizeLights, sceneScale, validatePart, withReach,+ MAX_BAKE_PARTS, buildWorld, createDirectIrradiance, createGatherer, diffuseAlbedo, emittedRadiance, normalizeLights, sceneScale, validatePart, withReach,} from './light-bake.js';import { worldBounds } from './probe-trace.js';-import { createGridMedium } from './grid-medium.js';+import { createGridMedium, MAXIMUM_FOG_SCATTERS } from './grid-medium.js';// A baked grid of diffuse light: at each node of a 3D grid the light that other surfaces bounce toward it (the sun's lit// patches included, over several bounces), stored as first order spherical harmonics per colour channel. A lit surface reads@@ -216,30 +216,6 @@ function smoothNodes(data, valid, size, isConnected) {}}-const SURFACE_SCATTER = true;--// The radiance the nearest surface along a ray sends back: its emission and its direct light (what one bounce of the gatherer adds).-function createSurfaceRadiance({ world, tracer, albedoOfPart, emissionOfPart, directIrradiance }) {- const hitPoint = [0, 0, 0];- const normal = [0, 0, 0];- return function surfaceRadiance(point, direction, out) {- out[0] = 0;- out[1] = 0;- out[2] = 0;- const hit = tracer.trace(point[0], point[1], point[2], direction[0], direction[1], direction[2], Infinity, false);- const triangle = hit.nearestTriangle;- if (triangle < 0) return;- const distance = hit.nearestDistance;- for (let axis = 0; axis < 3; axis++) hitPoint[axis] = point[axis] + direction[axis] * distance;- hitNormal(world, triangle, direction, normal);- const owner = world.triangleOwner[triangle];- const irradiance = directIrradiance(hitPoint, normal, owner);- for (let channel = 0; channel < 3; channel++) {- out[channel] = emissionOfPart[owner][channel] + albedoOfPart[owner][channel] * irradiance[channel] / Math.PI;- }- };-}-/*** Traces the grid. For each node, the cosine weighted light arriving along the six axis directions (the bake's gatherer:* emission and direct light at every hit, bounced over `bounces` reflections) becomes a first order fit per colour channel:@@ -265,9 +241,8 @@ export function traceIrradianceGrid(parts, lighting, { resolution = true, sampleconst albedoOfPart = parts.map(part => diffuseAlbedo(part.material));const emissionOfPart = parts.map(part => emittedRadiance(part.material));const sun = lights.sun ?? null;- const surfaceRadiance = createSurfaceRadiance({ world, tracer, albedoOfPart, emissionOfPart, directIrradiance });- const medium = createGridMedium({ lighting, sun, tracer, scale, surfaceRadiance: SURFACE_SCATTER ? surfaceRadiance : null });- const gather = createGatherer({ world, tracer, albedoOfPart, emissionOfPart, directIrradiance, offset, samples, bounces, medium });+ const medium = createGridMedium({ lighting, sun, tracer, scale });+ const gather = createGatherer({ world, tracer, albedoOfPart, emissionOfPart, directIrradiance, offset, samples, bounces, medium, fogScatterLimit: MAXIMUM_FOG_SCATTERS });const started = performance.now();const data = new Float32Array(nodeCount * COEFFICIENTS_PER_NODE);// The part of `data` that the fog scattered into the paths. The fog drawn on screen already scatters the sun itself, so it reads the grid without this part.diff --git a/src/light-bake.js b/src/light-bake.jsindex 43a6ceee..d26dd5d9 100644--- a/src/light-bake.js+++ b/src/light-bake.js@@ -451,7 +451,7 @@ const SCRATCH_TRIANGLE = new Float64Array(9);* The mean radiance arriving at one texel from other surfaces: cosine-weighted paths of up to `bounces` reflections,* each hit adding its own emission and its direct light. The caller multiplies by the texel's albedo.*/-export function createGatherer({ world, tracer, albedoOfPart, emissionOfPart, directIrradiance, offset, samples, bounces, medium = null }) {+export function createGatherer({ world, tracer, albedoOfPart, emissionOfPart, directIrradiance, offset, samples, bounces, medium = null, fogScatterLimit = 8 }) {const scatterSum = [0, 0, 0];const direction = [0, 0, 0];const origin = [0, 0, 0];@@ -476,13 +476,19 @@ export function createGatherer({ world, tracer, albedoOfPart, emissionOfPart, dithroughput[0] = 1;throughput[1] = 1;throughput[2] = 1;- for (let bounce = 0; bounce < bounces; bounce++) {+ let bounce = 0;+ let leg = 0;+ let fogScatters = 0;+ while (bounce < bounces) {const hit = tracer.trace(origin[0], origin[1], origin[2], direction[0], direction[1], direction[2], Infinity, false);// The tracer reuses its hit record, and the fog traces rays of its own, so the hit is read first.const hitTriangle = hit.nearestTriangle;const hitDistance = hit.nearestDistance;- // The fog along this leg scatters light into the path and dims what lies beyond it.- if (medium) medium(origin, direction, hitTriangle < 0 ? Infinity : hitDistance, throughput, scatterSum, texelIndex, sample, bounce);+ // The fog may scatter the path inside this leg: the path then goes on from the scattering point.+ if (medium && medium(origin, direction, hitTriangle < 0 ? Infinity : hitDistance, throughput, scatterSum, texelIndex, sample, leg++)) {+ if (++fogScatters >= fogScatterLimit) break;+ continue;+ }if (hitTriangle < 0) {if (bounce === 0) escaped++;break;@@ -493,8 +499,10 @@ export function createGatherer({ world, tracer, albedoOfPart, emissionOfPart, diconst 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++) {- sum[channel] += throughput[channel] * (emission[channel] + albedo[channel] * irradiance[channel] / Math.PI);+ // 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);throughput[channel] *= albedo[channel];}if (throughput[0] + throughput[1] + throughput[2] < 1e-4) break;@@ -502,6 +510,7 @@ export function createGatherer({ world, tracer, albedoOfPart, emissionOfPart, diorigin[1] = hitPoint[1] + normal[1] * offset;origin[2] = hitPoint[2] + normal[2] * offset;cosineDirection(normal, hashUnit(texelIndex, sample, bounce * 2 + 11), hashUnit(texelIndex, sample, bounce * 2 + 12), direction);+ bounce++;}}// `sum` holds the fog's light too; `fogLight`, when given, gets that part alone.diff --git a/tests/grid-medium.test.js b/tests/grid-medium.test.jsindex 8448d146..8f1e261e 100644--- a/tests/grid-medium.test.js+++ b/tests/grid-medium.test.js@@ -7,44 +7,69 @@ const WALLED_IN = { trace: () => ({ nearestTriangle: 0, nearestDistance: 0.5 })const FOG_BOX = { density: 0.5, minimum: [-10, -10, -10], maximum: [10, 10, 10], color: [1, 0.5, 0.25], anisotropy: 0, maxDistance: 100 };const SUN = { color: [2, 2, 2], direction: [0, 1, 0] };-function runLeg(medium, distance) {+function runLeg(medium, distance, sample = 5) {+ const origin = [0, 0, 0];+ const direction = [1, 0, 0];const throughput = [1, 1, 1];const sum = [0, 0, 0];- medium([0, 0, 0], [1, 0, 0], distance, throughput, sum, 3, 5, 0);- return { throughput, sum };+ const scattered = medium(origin, direction, distance, throughput, sum, 3, sample, 0);+ return { scattered, origin, direction, throughput, sum };}test('lighting without fog gives no medium', () => {- assert.equal(createGridMedium({ lighting: {}, sun: SUN, tracer: OPEN_SPACE, scale: 1, surfaceRadiance: null }), null);- assert.equal(createGridMedium({ lighting: { volumeFog: { density: 0 } }, sun: SUN, tracer: OPEN_SPACE, scale: 1, surfaceRadiance: null }), null);+ assert.equal(createGridMedium({ lighting: {}, sun: SUN, tracer: OPEN_SPACE, scale: 1 }), null);+ assert.equal(createGridMedium({ lighting: { volumeFog: { density: 0 } }, sun: SUN, tracer: OPEN_SPACE, scale: 1 }), null);});-test('a leg through fog is dimmed by its transmittance and gains the single scattered sun', () => {- const medium = createGridMedium({ lighting: { volumeFog: FOG_BOX }, sun: SUN, tracer: OPEN_SPACE, scale: 1, surfaceRadiance: null });- const { throughput, sum } = runLeg(medium, 2);- const transmittance = Math.exp(-0.5 * 2);- for (const value of throughput) assert.ok(Math.abs(value - transmittance) < 1e-12);- // Isotropic phase is 1 / (4 pi), whatever point of the leg scatters.- FOG_BOX.color.forEach((albedo, channel) => {- const expected = (1 - transmittance) * albedo * 2 / (4 * Math.PI);- assert.ok(Math.abs(sum[channel] - expected) < 1e-12, `channel ${channel}: ${sum[channel]} against ${expected}`);- });+test('a leg scatters with the probability a photon would and the path goes on from the point', () => {+ const medium = createGridMedium({ lighting: { volumeFog: FOG_BOX }, sun: SUN, tracer: OPEN_SPACE, scale: 1 });+ const distance = 2;+ const trials = 4000;+ let scatterCount = 0;+ for (let sample = 0; sample < trials; sample++) {+ const leg = runLeg(medium, distance, sample);+ if (!leg.scattered) {+ assert.deepEqual(leg.throughput, [1, 1, 1]);+ assert.deepEqual(leg.origin, [0, 0, 0]);+ continue;+ }+ scatterCount++;+ assert.ok(leg.origin[0] > 0 && leg.origin[0] < distance, 'the point lies inside the leg');+ assert.ok(Math.abs(Math.hypot(...leg.direction) - 1) < 1e-9, 'the new direction is a unit vector');+ FOG_BOX.color.forEach((albedo, channel) => assert.ok(Math.abs(leg.throughput[channel] - albedo) < 1e-12));+ // Isotropic phase is 1 / (4 pi), so the sun adds throughput times albedo times its colour over 4 pi.+ FOG_BOX.color.forEach((albedo, channel) => assert.ok(Math.abs(leg.sum[channel] - albedo * 2 / (4 * Math.PI)) < 1e-12));+ }+ const expectedShare = 1 - Math.exp(-0.5 * distance);+ assert.ok(Math.abs(scatterCount / trials - expectedShare) < 0.03, `${scatterCount / trials} against ${expectedShare}`);});-test('a shadowed sun scatters nothing, and a leg outside the box is untouched', () => {- const shadowed = createGridMedium({ lighting: { volumeFog: FOG_BOX }, sun: SUN, tracer: WALLED_IN, scale: 1, surfaceRadiance: null });- assert.deepEqual(runLeg(shadowed, 2).sum, [0, 0, 0]);- const outside = createGridMedium({ lighting: { volumeFog: { ...FOG_BOX, minimum: [20, 20, 20], maximum: [30, 30, 30] } }, sun: SUN, tracer: OPEN_SPACE, scale: 1, surfaceRadiance: null });- const { throughput, sum } = runLeg(outside, 2);- assert.deepEqual(throughput, [1, 1, 1]);- assert.deepEqual(sum, [0, 0, 0]);+test('a shadowed sun adds nothing, and a leg outside the box is untouched', () => {+ const shadowed = createGridMedium({ lighting: { volumeFog: FOG_BOX }, sun: SUN, tracer: WALLED_IN, scale: 1 });+ for (let sample = 0; sample < 50; sample++) {+ assert.deepEqual(runLeg(shadowed, 2, sample).sum, [0, 0, 0]);+ }+ const outside = createGridMedium({ lighting: { volumeFog: { ...FOG_BOX, minimum: [20, 20, 20], maximum: [30, 30, 30] } }, sun: SUN, tracer: OPEN_SPACE, scale: 1 });+ const leg = runLeg(outside, 2);+ assert.equal(leg.scattered, false);+ assert.deepEqual(leg.throughput, [1, 1, 1]);+ assert.deepEqual(leg.sum, [0, 0, 0]);});-test('fog also scatters the light of the surface it sees, in proportion to it', () => {- const dim = createGridMedium({ lighting: { volumeFog: FOG_BOX }, sun: null, tracer: OPEN_SPACE, scale: 1, surfaceRadiance: (point, direction, out) => out.fill(0.5) });- const bright = createGridMedium({ lighting: { volumeFog: FOG_BOX }, sun: null, tracer: OPEN_SPACE, scale: 1, surfaceRadiance: (point, direction, out) => out.fill(1) });- const dimSum = runLeg(dim, 2).sum;- const brightSum = runLeg(bright, 2).sum;- assert.ok(dimSum[0] > 0);- assert.ok(Math.abs(brightSum[0] / dimSum[0] - 2) < 1e-9);+test('a forward scattering fog keeps the path closer to its direction than a backward one', () => {+ const meanCosine = anisotropy => {+ const medium = createGridMedium({ lighting: { volumeFog: { ...FOG_BOX, anisotropy } }, sun: null, tracer: OPEN_SPACE, scale: 1 });+ let total = 0;+ let count = 0;+ for (let sample = 0; sample < 2000; sample++) {+ const leg = runLeg(medium, 20, sample);+ if (leg.scattered) {+ total += leg.direction[0];+ count++;+ }+ }+ return total / count;+ };+ assert.ok(Math.abs(meanCosine(0.6) - 0.6) < 0.05);+ assert.ok(Math.abs(meanCosine(-0.6) + 0.6) < 0.05);});