3D engine that AI agents drive
Dim the sun that the grid fog scatters by the fog on its way (issue #543)
Nguyen Xuan Ana6b1b50e4852
diff --git a/changes/543-fog-sun.md b/changes/543-fog-sun.mdnew file mode 100644index 00000000..0fce794a--- /dev/null+++ b/changes/543-fog-sun.md@@ -0,0 +1 @@+- bake.gridFog: the sun the fog scatters is dimmed by the fog between it and the sun (#543)diff --git a/src/grid-medium.js b/src/grid-medium.jsindex 2710c4a0..66ee3da9 100644--- a/src/grid-medium.js+++ b/src/grid-medium.js@@ -20,7 +20,7 @@ export function createGridMedium({ lighting, sun, tracer, scale }) {if (!volume) return null;const interval = [0, 0];const shadowReach = scale * 1e6;- return function medium(origin, direction, distance, throughput, sum, texelIndex, sample, leg) {+ 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;@@ -31,13 +31,21 @@ export function createGridMedium({ lighting, sun, tracer, scale }) {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++) sum[channel] += throughput[channel] * volume.albedo[channel] * sun.color[channel] * phase;+ 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++) 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;- };+ }+ return medium;+}++/** 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;+ return Math.exp(-volume.density * (interval[1] - interval[0]));}/** Turns `direction` (a unit vector) in place by the polar angle whose cosine is given, around it by `azimuth`. */diff --git a/tests/grid-medium.test.js b/tests/grid-medium.test.jsindex 8f1e261e..41751b36 100644--- a/tests/grid-medium.test.js+++ b/tests/grid-medium.test.js@@ -37,8 +37,10 @@ test('a leg scatters with the probability a photon would and the path goes on frassert.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));+ // Isotropic phase is 1 / (4 pi), so the sun adds throughput times albedo times its colour over 4 pi,+ // dimmed by the fog between the point and the sun (the sun is straight up, the box ends at y = 10).+ const sunReaching = Math.exp(-FOG_BOX.density * (10 - leg.origin[1]));+ FOG_BOX.color.forEach((albedo, channel) => assert.ok(Math.abs(leg.sum[channel] - albedo * 2 * sunReaching / (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}`);