Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

WIP: fog dims the sun on surfaces, sky mean in fog paths, fog reads multi-scatter (issue #543, not within the rule)

Nguyen Xuan An5dababcbe108
diff --git a/src/agent-bake.js b/src/agent-bake.js
index 1df31bb1..55c64344 100644
--- a/src/agent-bake.js
+++ b/src/agent-bake.js
@@ -3,6 +3,7 @@ import { runKernel } from './kernel-offload.js';
import { normalizeProbeList, traceProbes } from './probe-trace.js';
import { placeProbes } from './reflection-probes.js';
import { normalizeIrradianceGrid, placeIrradianceGrid, traceIrradianceGrid, withMeanRadiance } from './irradiance-grid.js';
+import { meanSkyRadiance } from './grid-medium.js';
import { knownKeys, finiteNumber } from './agent-recipe-checks.js';
import { BAKE_PRESETS, BAKE_RANGES, BAKE_MAXIMUM_SECONDS, BAKE_WORKER_MAXIMUM_SECONDS, BAKE_PRESET_PATH_WORK, BAKE_EXPLICIT_PATH_WORK_LIMIT } from './agent-scene-capabilities.js';
@@ -222,6 +223,7 @@ export function bakeAgentParts(parts, worldMatrices, environment, settings, gene
};
if (settings.gridFog) {
lighting.volumeFog = environment.volumeFog;
+ lighting.skyMean = meanSkyRadiance(environment.map);
}
// A hair, particle or cloth node is drawn from geometry built every frame, which has no lightmap UVs.
const snapshots = parts.map(part => ({
diff --git a/src/grid-medium.js b/src/grid-medium.js
index 66ee3da9..fa9d3dd7 100644
--- a/src/grid-medium.js
+++ b/src/grid-medium.js
@@ -19,8 +19,10 @@ export function createGridMedium({ lighting, sun, tracer, scale }) {
const volume = resolveTraceVolume(lighting.volumeFog);
if (!volume) return null;
const interval = [0, 0];
+ const reachInterval = [0, 0];
+ const skyMean = lighting.skyMean ?? null;
const shadowReach = scale * 1e6;
- function medium(origin, direction, distance, throughput, sum, texelIndex, sample, leg) {
+ function medium(origin, direction, distance, throughput, sum, texelIndex, sample, leg, singleSum = null) {
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;
@@ -32,16 +34,45 @@ export function createGridMedium({ lighting, sun, tracer, scale }) {
if (!sunBlocked) {
const phase = henyeyGreensteinPhase(volume.anisotropy, direction[0] * sun.direction[0] + direction[1] * sun.direction[1] + direction[2] * sun.direction[2]);
const sunLight = sunTransmittance(volume, origin, sun.direction, interval);
- for (let channel = 0; channel < 3; channel++) sum[channel] += throughput[channel] * volume.albedo[channel] * sun.color[channel] * phase * sunLight;
+ for (let channel = 0; channel < 3; channel++) {
+ const scattered = throughput[channel] * volume.albedo[channel] * sun.color[channel] * phase * sunLight;
+ sum[channel] += scattered;
+ // The sun scattered once on the first leg is what the fog on screen adds along the view ray.
+ if (singleSum && leg === 0) singleSum[channel] += scattered;
+ }
}
}
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;
}
+ // How much of the sun the fog lets through to a point on a surface.
+ medium.sunReaching = point => (sun ? sunTransmittance(volume, point, sun.direction, reachInterval) : 1);
+ // The sky the path sees after it left the fog, as the mean radiance of the sky (an even sky, not one per direction).
+ medium.addSky = (throughput, sum) => {
+ if (!skyMean) return;
+ for (let channel = 0; channel < 3; channel++) sum[channel] += throughput[channel] * skyMean[channel];
+ };
return medium;
}
+/** The mean radiance of an environment map over the sphere, or null when there is no map. */
+export function meanSkyRadiance(map) {
+ if (!map?.data) return null;
+ const { width, height, channels, data, intensity } = map;
+ const total = [0, 0, 0];
+ let weightSum = 0;
+ for (let row = 0; row < height; row++) {
+ const weight = Math.sin(((row + 0.5) / height) * Math.PI);
+ for (let column = 0; column < width; column++) {
+ const base = (row * width + column) * channels;
+ for (let channel = 0; channel < 3; channel++) total[channel] += data[base + channel] * weight;
+ weightSum += weight;
+ }
+ }
+ return total.map(value => (value * intensity) / weightSum);
+}
+
/** The share of the sun that reaches a point inside the fog: the fog's extinction along the way to the sun. */
function sunTransmittance(volume, origin, sunDirection, interval) {
if (!volumeInterval(volume, origin[0], origin[1], origin[2], sunDirection[0], sunDirection[1], sunDirection[2], Infinity, interval)) return 1;
diff --git a/src/irradiance-grid.js b/src/irradiance-grid.js
index d91fbc52..be46d85e 100644
--- a/src/irradiance-grid.js
+++ b/src/irradiance-grid.js
@@ -237,11 +237,11 @@ export function traceIrradianceGrid(parts, lighting, { resolution = true, sample
const scale = sceneScale(world.positions);
const offset = scale * 1e-4;
const tracer = createTracer(world);
- const directIrradiance = createDirectIrradiance(tracer, withReach(lights, parts), offset, scale);
- const albedoOfPart = parts.map(part => diffuseAlbedo(part.material));
- const emissionOfPart = parts.map(part => emittedRadiance(part.material));
const sun = lights.sun ?? null;
const medium = createGridMedium({ lighting, sun, tracer, scale });
+ const directIrradiance = createDirectIrradiance(tracer, withReach(lights, parts), offset, scale, medium?.sunReaching);
+ const albedoOfPart = parts.map(part => diffuseAlbedo(part.material));
+ const emissionOfPart = parts.map(part => emittedRadiance(part.material));
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);
diff --git a/src/light-bake.js b/src/light-bake.js
index 2a94f08c..e0e2422c 100644
--- a/src/light-bake.js
+++ b/src/light-bake.js
@@ -335,7 +335,7 @@ export function cosineDirection(normal, firstUnit, secondUnit, out) {
* Builds the function that returns the irradiance (light power per area, before the albedo and 1/pi of a diffuse
* surface) of the direct lights at a point, with shadow rays against the world.
*/
-export function createDirectIrradiance(tracer, lights, offset, scale) {
+export function createDirectIrradiance(tracer, lights, offset, scale, sunReaching = null) {
const result = [0, 0, 0];
const shadowReach = scale * 1e6;
function addLight(point, normal, toLight, distance, power) {
@@ -350,6 +350,7 @@ export function createDirectIrradiance(tracer, lights, offset, scale) {
}
const toLight = [0, 0, 0];
const power = [0, 0, 0];
+ const sunPower = [0, 0, 0];
// Aims `toLight` at a light's position and returns the distance, or 0 when the point sits on the light.
function aimAtPosition(point, lightX, lightY, lightZ) {
const deltaX = lightX - point[0];
@@ -420,7 +421,16 @@ export function createDirectIrradiance(tracer, lights, offset, scale) {
for (let index = 0; index < lights.areaLights.length; index++) {
if (lightReaches(lights.areaLights[index], owner)) addAreaLight(point, normal, lights.areaLights[index], index);
}
- if (lights.sun && lightReaches(lights.sun, owner)) addLight(point, normal, lights.sun.direction, shadowReach, lights.sun.color);
+ if (lights.sun && lightReaches(lights.sun, owner)) {
+ if (!sunReaching) {
+ addLight(point, normal, lights.sun.direction, shadowReach, lights.sun.color);
+ } else {
+ // The fog dims the sun on its way to the surface.
+ const reaching = sunReaching(point);
+ for (let channel = 0; channel < 3; channel++) sunPower[channel] = lights.sun.color[channel] * reaching;
+ addLight(point, normal, lights.sun.direction, shadowReach, sunPower);
+ }
+ }
return result;
};
}
@@ -453,6 +463,7 @@ const SCRATCH_TRIANGLE = new Float64Array(9);
*/
export function createGatherer({ world, tracer, albedoOfPart, emissionOfPart, directIrradiance, offset, samples, bounces, medium = null, fogScatterLimit = 8 }) {
const scatterSum = [0, 0, 0];
+ const singleScatterSum = [0, 0, 0];
const direction = [0, 0, 0];
const origin = [0, 0, 0];
const normal = [0, 0, 0];
@@ -463,6 +474,7 @@ export function createGatherer({ world, tracer, albedoOfPart, emissionOfPart, di
sum[1] = 0;
sum[2] = 0;
scatterSum.fill(0);
+ singleScatterSum.fill(0);
const rotation = hashUnit(texelIndex, 1, 7);
// Rays that leave the scene on their first leg see the sky, so the share of them is how much sky this point sees.
let escaped = 0;
@@ -485,12 +497,14 @@ export function createGatherer({ world, tracer, albedoOfPart, emissionOfPart, di
const hitTriangle = hit.nearestTriangle;
const hitDistance = hit.nearestDistance;
// 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 (medium && medium(origin, direction, hitTriangle < 0 ? Infinity : hitDistance, throughput, scatterSum, texelIndex, sample, leg++, singleScatterSum)) {
if (++fogScatters >= fogScatterLimit) break;
continue;
}
if (hitTriangle < 0) {
if (bounce === 0) escaped++;
+ // A path the fog scattered brings the sky's light with it: the sky lights the fog through the openings.
+ if (fogScatters > 0) medium.addSky(throughput, scatterSum);
break;
}
for (let axis = 0; axis < 3; axis++) hitPoint[axis] = origin[axis] + direction[axis] * hitDistance;
@@ -511,11 +525,11 @@ export function createGatherer({ world, tracer, albedoOfPart, emissionOfPart, di
bounce++;
}
}
- // `sum` holds the fog's light too; `fogLight`, when given, gets that part alone.
+ // `sum` holds the fog's light too; `fogLight`, when given, gets the single scattered sun alone, the part the fog on screen adds itself.
for (let channel = 0; channel < 3; channel++) {
scatterSum[channel] /= samples;
sum[channel] = sum[channel] / samples + scatterSum[channel];
- if (fogLight) fogLight[channel] = scatterSum[channel];
+ if (fogLight) fogLight[channel] = singleScatterSum[channel] / samples;
}
return escaped / samples;
};