Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Light the sky term of a scattering local volume through its own depth (#855)

Nguyen Xuan An873c44320a6c
diff --git a/changes/855-self-shadow.md b/changes/855-self-shadow.md
new file mode 100644
index 00000000..9398cd35
--- /dev/null
+++ b/changes/855-self-shadow.md
@@ -0,0 +1 @@
+- The sky light of a scattering local volume passes through the volume's own depth (up and four side directions, one tap each, in the rasterizer and the CPU path tracer), so the core of a thick smoke is darker than its rim; `scatter: 0` is unchanged (#855)
diff --git a/changes/855-self-shadow.size.md b/changes/855-self-shadow.size.md
new file mode 100644
index 00000000..b108386f
--- /dev/null
+++ b/changes/855-self-shadow.size.md
@@ -0,0 +1,3 @@
+- entry:fog-shader: the sky term of a lit local volume gets its own depth through five short tap sets in the fog addon, so a thick smoke has a darker core instead of a flat glow.
+- scene:agent-scene+lazy: the same sky depth in the CPU path tracer's lit volume step, counted in the lazy bundle.
+- entry:path-tracer: the sky depth loop and its direction list in the CPU path tracer's lit volume step, a few hundred bytes for a darker core in thick smoke.
diff --git a/docs/volumetric.md b/docs/volumetric.md
index 77c16dd4..5cd39a94 100644
--- a/docs/volumetric.md
+++ b/docs/volumetric.md
@@ -136,7 +136,7 @@ Each volume is marched on its own between its entry and its exit, cut at the fir
A volume with `scatter` above `0` returns, in each step, `scatter * (1 - exp(-extinction * step))` of the light it dims, per colour channel, times the light that arrives at that point: the sky term plus the key light. Smoke or haze in sun then reads bright on the side toward the sun and grey in shade, where a `scatter: 0` volume draws as a dark stain.
- The key light uses the same shadow map lookup (`keyVisibility`) and phase function (`henyeyGreenstein`, isotropic here) as `volumeFog`, times `exp(-depth)` where `depth` is the volume's own optical depth between the point and the volume's far side toward the sun (three midpoint taps of its profile, noise included). That depth is what makes the sunward side brighter than the shaded side. Only the key light and the sky light a volume: point, spot and area lights do not.
-- The sky term is the mean of `skyTop` and `skyBottom` times `ambientIntensity` in the rasterizer, and the mean radiance of `environment.map` in the CPU path tracer. It is not occluded. With an environment map the rasterizer still reads `skyTop` and `skyBottom`.
+- The sky term is the mean of `skyTop` and `skyBottom` times `ambientIntensity` in the rasterizer, and the mean radiance of `environment.map` in the CPU path tracer. With an environment map the rasterizer still reads `skyTop` and `skyBottom`. It passes through the volume's own depth too: the mean of `exp(-depth)` over the up direction and the four horizontal directions, one midpoint tap each, so the middle of a thick smoke is darker than its rim instead of glowing as evenly as the edge.
- The CPU path tracer uses the same model on `backend: "cpu"`: the sky term, a shadow ray toward the sun, and the same three taps. The sun's colour is `keyColor` there.
- A scene with a lit volume draws the key light's shadow map for the post pass, as a fog does. A volume with `scatter: 0` pays nothing extra per step.
- Measured (`tests/local-volume-scatter.test.js`, a density 1.2 smoke box under a side sun over an even grey sky, seen from above; 64 px rasterizer on SwiftShader against the 32 px, 16 sample CPU tracer): the worst of 64 regions differed by 0.0003 of linear radiance and the whole frame matched to four places (0.4141 both), inside the 0.05 and 0.02 tolerances. The half toward the sun read 0.424 and the shaded half 0.404 in both renderers.
diff --git a/src/fog-shader.js b/src/fog-shader.js
index 7ed0db82..3daf0681 100644
--- a/src/fog-shader.js
+++ b/src/fog-shader.js
@@ -207,20 +207,33 @@ float localVolumeProfile(int index, vec3 position) {
if (noiseAmount > 0.0) profile *= mix(1.0, localVolumeNoise(position * localVolumeEmission[index].w) * 2.0, noiseAmount);
return profile;
}
-// The optical depth of a volume between a point inside it and its far side toward the sun, from three midpoint taps of its profile.
-vec3 localVolumeSunDepth(int index, vec3 position, vec3 towardSun) {
- float exitDistance = max(localVolumeInterval(index, position, towardSun).y, 0.0);
+// The optical depth of a volume between a point inside it and its far side along a direction, from midpoint taps of its profile.
+vec3 localVolumeDepth(int index, vec3 position, vec3 direction, int taps) {
+ float exitDistance = max(localVolumeInterval(index, position, direction).y, 0.0);
float profileSum = 0.0;
- for (int tap = 0; tap < 3; tap++) profileSum += localVolumeProfile(index, position + towardSun * (exitDistance * (float(tap) + 0.5) / 3.0));
- return localVolumeAbsorption[index].rgb * (profileSum * exitDistance / 3.0);
+ for (int tap = 0; tap < 3; tap++) {
+ if (tap >= taps) break;
+ profileSum += localVolumeProfile(index, position + direction * (exitDistance * (float(tap) + 0.5) / float(taps)));
+ }
+ return localVolumeAbsorption[index].rgb * (profileSum * exitDistance / float(taps));
+}
+// The sky light a point receives through the volume's own depth: the mean over the up and four side directions of what one tap leaves.
+// Without it the sky term is flat, so a thick smoke glows as evenly deep inside as at its edge and hides the key light's side.
+vec3 localVolumeSkyLight(int index, vec3 position) {
+ vec3 open = exp(-localVolumeDepth(index, position, vec3(0.0, 1.0, 0.0), 1));
+ open += exp(-localVolumeDepth(index, position, vec3(1.0, 0.0, 0.0), 1));
+ open += exp(-localVolumeDepth(index, position, vec3(-1.0, 0.0, 0.0), 1));
+ open += exp(-localVolumeDepth(index, position, vec3(0.0, 0.0, 1.0), 1));
+ open += exp(-localVolumeDepth(index, position, vec3(0.0, 0.0, -1.0), 1));
+ return localSkyAmbient * (open * 0.2);
}
-// The light a scattering volume receives at a point: the sky term plus the key light through the shadow map and the volume's own depth.
+// The light a scattering volume receives at a point: the sky term and the key light through the shadow map, each through the volume's own depth.
vec3 localVolumeInScatter(int index, vec3 position, vec3 direction) {
vec3 towardSun = normalize(volumeLightDirection);
float visibility = keyVisibility(position);
vec3 sunLight = vec3(0.0);
- if (visibility > 0.001) sunLight = volumeLightColor * (henyeyGreenstein(dot(direction, towardSun), 0.0) * visibility) * exp(-localVolumeSunDepth(index, position, towardSun));
- return localSkyAmbient + sunLight;
+ if (visibility > 0.001) sunLight = volumeLightColor * (henyeyGreenstein(dot(direction, towardSun), 0.0) * visibility) * exp(-localVolumeDepth(index, position, towardSun, 3));
+ return localVolumeSkyLight(index, position) + sunLight;
}
vec3 localVolumes(vec3 background) {
vec2 clipXY = textureCoordinate * 2.0 - 1.0;
diff --git a/src/path-tracer-volume.js b/src/path-tracer-volume.js
index ee95dab6..1face12d 100644
--- a/src/path-tracer-volume.js
+++ b/src/path-tracer-volume.js
@@ -167,19 +167,22 @@ export function meanEnvironmentRadiance(environment) {
}
const SUN_DEPTH_TAPS = 3;
+const SKY_DEPTH_TAPS = 1;
+// The up and four side directions the sky light is gathered from (the rasterizer's localVolumeSkyLight holds the same list).
+const SKY_DIRECTIONS = [[0, 1, 0], [1, 0, 0], [-1, 0, 0], [0, 0, 1], [0, 0, -1]];
/**
- * The optical depth per colour channel between a point inside a volume and the volume's far side toward the sun, from three
- * midpoint taps of its profile: the rasterizer's `localVolumeSunDepth`, so the lit side of a volume is brighter than the shaded side.
+ * The optical depth per colour channel between a point inside a volume and the volume's far side along a direction, from midpoint
+ * taps of its profile: the rasterizer's `localVolumeDepth`, so the lit side of a volume is brighter than the shaded side.
*/
-function writeSunOpticalDepth(volume, x, y, z, toward, span, out) {
- const exitDistance = localVolumeInterval(volume, x, y, z, toward[0], toward[1], toward[2], Infinity, span) ? Math.max(span[1], 0) : 0;
+function writeOpticalDepth(volume, x, y, z, direction, taps, span, out) {
+ const exitDistance = localVolumeInterval(volume, x, y, z, direction[0], direction[1], direction[2], Infinity, span) ? Math.max(span[1], 0) : 0;
let profileSum = 0;
- for (let tap = 0; tap < SUN_DEPTH_TAPS; tap++) {
- const distance = exitDistance * (tap + 0.5) / SUN_DEPTH_TAPS;
- profileSum += localVolumeProfile(volume, x + toward[0] * distance, y + toward[1] * distance, z + toward[2] * distance);
+ for (let tap = 0; tap < taps; tap++) {
+ const distance = exitDistance * (tap + 0.5) / taps;
+ profileSum += localVolumeProfile(volume, x + direction[0] * distance, y + direction[1] * distance, z + direction[2] * distance);
}
- for (let channel = 0; channel < 3; channel++) out[channel] = volume.absorption[channel] * volume.density * profileSum * exitDistance / SUN_DEPTH_TAPS;
+ for (let channel = 0; channel < 3; channel++) out[channel] = volume.absorption[channel] * volume.density * profileSum * exitDistance / taps;
}
/**
@@ -199,20 +202,32 @@ export function createLocalVolumeCrossing(volumes, lighting = null) {
const span = new Float64Array(2);
const sunDepth = new Float64Array(3);
const sunLight = new Float64Array(3);
+ const skyLight = new Float64Array(3);
+
+ // Writes the sky term of a point inside a scattering volume: the mean sky radiance times what the volume's own depth leaves open in five directions.
+ function writeSkyLight(volume, x, y, z) {
+ skyLight.fill(0);
+ for (const direction of SKY_DIRECTIONS) {
+ writeOpticalDepth(volume, x, y, z, direction, SKY_DEPTH_TAPS, span, sunDepth);
+ for (let channel = 0; channel < 3; channel++) skyLight[channel] += Math.exp(-sunDepth[channel]) / SKY_DIRECTIONS.length;
+ }
+ for (let channel = 0; channel < 3; channel++) skyLight[channel] *= lighting.ambient[channel];
+ }
// Adds the light one step of a scattering volume sends back along the ray: scatter * (1 - exp(-extinction * width)) * (sky + sun).
function addScatteredLight(volume, x, y, z, directionX, directionY, directionZ, width, throughput, emitted) {
+ writeSkyLight(volume, x, y, z);
sunLight.fill(0);
const sun = lighting.sun;
if (sun && lighting.sunVisibility(x, y, z) > 0) {
- writeSunOpticalDepth(volume, x, y, z, sun.direction, span, sunDepth);
+ writeOpticalDepth(volume, x, y, z, sun.direction, SUN_DEPTH_TAPS, span, sunDepth);
const cosine = sun.direction[0] * directionX + sun.direction[1] * directionY + sun.direction[2] * directionZ;
const phase = henyeyGreensteinPhase(0, cosine);
for (let channel = 0; channel < 3; channel++) sunLight[channel] = sun.color[channel] * phase * Math.exp(-sunDepth[channel]);
}
for (let channel = 0; channel < 3; channel++) {
const scattered = volume.scatter * (1 - Math.exp(-volume.absorption[channel] * volume.density * width));
- emitted[channel] += throughput[channel] * scattered * (lighting.ambient[channel] + sunLight[channel]);
+ emitted[channel] += throughput[channel] * scattered * (skyLight[channel] + sunLight[channel]);
}
}
diff --git a/tests/local-volume-scatter.test.js b/tests/local-volume-scatter.test.js
index 01a94aa9..6479b160 100644
--- a/tests/local-volume-scatter.test.js
+++ b/tests/local-volume-scatter.test.js
@@ -80,11 +80,24 @@ test('a volume with scatter 0 draws exactly as it did: no light is added and the
assert.ok(withLighting.throughput[0] < 1);
});
-test('a shaded volume with only the sky around it returns the sky colour scaled by what it extinguishes', () => {
+// The sky light per unit of what a ray extinguishes, as a share of the open-sky value (scatter times the sky level).
+function skyShare(volume) {
+ const sky = { sun: null, ambient: [SKY_LEVEL, SKY_LEVEL, SKY_LEVEL], sunVisibility: () => 1 };
+ const result = downRay({ ...volume, falloff: 0 }, sky, 0);
+ return result.emitted[1] / ((1 - result.throughput[1]) * 0.8 * SKY_LEVEL);
+}
+
+test('the sky lights a thin volume at full strength and a thick one less, through its own depth', () => {
+ const thin = skyShare({ ...SMOKE_BOX, center: [0, 1.5, 0], size: [0.1, 0.1, 0.1] });
+ const thick = skyShare(SMOKE_BOX);
+ assert.ok(thin > 0.9 && thin <= 1, `thin volume sky share ${thin}`);
+ assert.ok(thick > 0.3 && thick < 0.85 * thin, `thick volume sky share ${thick} against thin ${thin}`);
+});
+
+test('a shaded volume with only the sky around it returns less than the open sky and no sun', () => {
const sky = { sun: null, ambient: [SKY_LEVEL, SKY_LEVEL, SKY_LEVEL], sunVisibility: () => 1 };
const result = downRay({ ...SMOKE_BOX, falloff: 0 }, sky, 0);
- const extinguished = 1 - result.throughput[1];
- assert.ok(Math.abs(result.emitted[1] - 0.8 * extinguished * SKY_LEVEL) < 1e-9, `${result.emitted[1]} against ${0.8 * extinguished * SKY_LEVEL}`);
+ assert.ok(result.emitted[1] > 0 && result.emitted[1] < 0.8 * (1 - result.throughput[1]) * SKY_LEVEL);
const blocked = downRay(SMOKE_BOX, { ...FULL_SUN, sunVisibility: () => 0 }, 1.2);
assert.deepEqual(blocked.emitted, [0, 0, 0]);
});