Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Fade a box local volume round at its corners and lower the key gain to 3 (#855)

Nguyen Xuan Andb1f7739c2ee
diff --git a/changes/855-round-fade.md b/changes/855-round-fade.md
new file mode 100644
index 00000000..12d4107c
--- /dev/null
+++ b/changes/855-round-fade.md
@@ -0,0 +1 @@
+- A box local volume with a `falloff` fades out round at its edges and corners (the distance past its core box) instead of along boxes with mitred creases, so a faded smoke has no visible box outline or corner crease; the fade along a face is unchanged, and the key gain is now 3 (#855)
diff --git a/changes/855-round-fade.size.md b/changes/855-round-fade.size.md
new file mode 100644
index 00000000..17133c7d
--- /dev/null
+++ b/changes/855-round-fade.size.md
@@ -0,0 +1,3 @@
+- entry:fog-shader: the round box fade in the volume profile, a few dozen bytes, so a faded smoke box shows no mitred creases.
+- scene:agent-scene+lazy: the same round fade in the CPU path tracer's volume profile, counted in the lazy bundle.
+- entry:path-tracer: the round fade in the CPU path tracer's profile, a few dozen bytes to match the rasterizer.
diff --git a/changes/855-sun-shadow-strength.md b/changes/855-sun-shadow-strength.md
index 33458a64..db748435 100644
--- a/changes/855-sun-shadow-strength.md
+++ b/changes/855-sun-shadow-strength.md
@@ -1 +1 @@
-- A scattering local volume lights its key term through the smooth depth toward the sun (counted four times, scaled by a key gain of 4) and its sky term through the smooth depth in five directions, so the sunward side is brighter than the shaded side and the core of a thick smoke is darker than its rim; the depth taps skip the noise, which cuts the cost of a lit volume by more than half; `scatter: 0` is unchanged (#855)
+- A scattering local volume lights its key term through the smooth depth toward the sun (counted four times, scaled by a key gain of 3) and its sky term through the smooth depth in five directions, so the sunward side is brighter than the shaded side and the core of a thick smoke is darker than its rim; the depth taps skip the noise, which cuts the cost of a lit volume by more than half; `scatter: 0` is unchanged (#855)
diff --git a/docs/volumetric.md b/docs/volumetric.md
index b90f059f..9742f3c4 100644
--- a/docs/volumetric.md
+++ b/docs/volumetric.md
@@ -139,7 +139,7 @@ A volume with `scatter` above `0` returns, in each step, `scatter * (1 - exp(-ex
- 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 of the smooth profile, so the middle of a thick smoke is darker than its rim instead of glowing as evenly as the edge. It is then scaled by `LOCAL_VOLUME_SKY_GAIN` (0.2): the sky lights the whole volume evenly, so a larger share veils the form the sun gives it and washes out what sits inside.
- 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.0001 of linear radiance and the whole frame matched to four places (0.3351 both), inside the 0.01 and 0.005 tolerances. The half toward the sun read 0.341 and the shaded half 0.329 in both renderers. On the 960 x 540 smoke still (sun from the right, a 3.2 x 2.4 x 3.2 box) the right third of the smoke is about 14 to 17 percent brighter than the left third in displayed luma; the sun sits in front of the box, so no setting of the two factors takes that far past 20 percent without clipping the sunlit side. The volume pass costs about 2 ms a frame at scatter 0.8 against 0.7 ms at scatter 0. Round 3 (#859): the depth the light crosses counts the faded skin at up to three times its density (`LOCAL_VOLUME_LIGHT_DEPTH_SATURATION`), because a sun ray running along the faded top skin met almost no density and lit it far above the core, which drew a bright rim; the key gain is 4 (a thin smoke returns at most what a sunlit surface of the same albedo does), the shadow strength 3 and the sky gain 0.5. A smoke with `falloff` 0.8 and `noiseAmount` 0.5 has no visible box outline.
+- 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.0001 of linear radiance and the whole frame matched to four places (0.3351 both), inside the 0.01 and 0.005 tolerances. The half toward the sun read 0.341 and the shaded half 0.329 in both renderers. On the 960 x 540 smoke still (sun from the right, a 3.2 x 2.4 x 3.2 box) the right third of the smoke is about 14 to 17 percent brighter than the left third in displayed luma; the sun sits in front of the box, so no setting of the two factors takes that far past 20 percent without clipping the sunlit side. The volume pass costs about 2 ms a frame at scatter 0.8 against 0.7 ms at scatter 0. Round 3 (#859): the depth the light crosses counts the faded skin at up to three times its density (`LOCAL_VOLUME_LIGHT_DEPTH_SATURATION`), because a sun ray running along the faded top skin met almost no density and lit it far above the core, which drew a bright rim; the key gain is 3 (a thin smoke returns at most what a sunlit surface of the same albedo does), the shadow strength 3 and the sky gain 0.5. The fade of a box is measured past its core box, so it is round at the corners; the largest-ratio fade drew mitred creases along the box diagonals. A smoke with `falloff` 0.7 and `noiseAmount` 0.5 has no visible box outline.
### Mesh volumes
diff --git a/src/fog-shader.js b/src/fog-shader.js
index 1867d55a..f4aed107 100644
--- a/src/fog-shader.js
+++ b/src/fog-shader.js
@@ -200,9 +200,18 @@ float localVolumeProfile(int index, vec3 position, bool withNoise) {
vec3 offset = position - localVolumeCenters[index].xyz;
if (localVolumeCenters[index].w < 0.5) offset = localVolumeLocal(index, offset);
vec3 halfSize = localVolumeExtents[index].xyz;
- float distanceRatio = localVolumeCenters[index].w > 0.5 ? length(offset) / halfSize.x : max(max(abs(offset.x) / halfSize.x, abs(offset.y) / halfSize.y), abs(offset.z) / halfSize.z);
float falloff = localVolumeExtents[index].w;
- float profile = falloff > 0.0 ? 1.0 - smoothstep(1.0 - falloff, 1.0, distanceRatio) : 1.0 - step(1.0, distanceRatio);
+ float profile;
+ if (localVolumeCenters[index].w > 0.5) {
+ float distanceRatio = length(offset) / halfSize.x;
+ profile = falloff > 0.0 ? 1.0 - smoothstep(1.0 - falloff, 1.0, distanceRatio) : 1.0 - step(1.0, distanceRatio);
+ } else {
+ vec3 ratios = abs(offset) / halfSize;
+ // The distance past the core box is round at the corners; the plain largest ratio fades along boxes with mitred creases (local-volumes.js repeats it).
+ float fadeDistance = length(max(ratios - (1.0 - falloff), 0.0));
+ profile = falloff > 0.0 ? 1.0 - smoothstep(0.0, 1.0, fadeDistance / falloff) : 1.0 - step(1.0, max(max(ratios.x, ratios.y), ratios.z));
+ if (max(max(ratios.x, ratios.y), ratios.z) >= 1.0) profile = 0.0;
+ }
float noiseAmount = localVolumeAbsorption[index].w;
if (withNoise && noiseAmount > 0.0) profile *= mix(1.0, localVolumeNoise(position * localVolumeEmission[index].w) * 2.0, noiseAmount);
return profile;
diff --git a/src/local-volumes.js b/src/local-volumes.js
index 07263fee..51d17c5d 100644
--- a/src/local-volumes.js
+++ b/src/local-volumes.js
@@ -102,7 +102,7 @@ export function packLocalVolumes(volumes, buffers) {
* and reads about as bright as a surface in the same sun, which one isotropic scatter (1 / 4 pi of the key colour) leaves far too dark; the gain keeps the
* sunlit side clearly above the shaded side. The rasterizer and the CPU path tracer both apply it.
*/
-export const LOCAL_VOLUME_KEY_GAIN = 4;
+export const LOCAL_VOLUME_KEY_GAIN = 3;
/**
* How much the faded outer share of a volume counts toward the depth the light crosses: the profile used for the sun and sky depth is
@@ -129,6 +129,11 @@ export const LOCAL_VOLUME_SKY_GAIN = 0.5;
*/
export const LOCAL_VOLUME_SUN_FLATTEN = 0.1;
+function fadeProfile(ratio) {
+ const clamped = Math.min(1, Math.max(0, ratio));
+ return 1 - clamped * clamped * (3 - 2 * clamped);
+}
+
/** The density factor of a volume at a point: 1 at the core, fading over the outer `falloff` share, times the noise factor (up to 2) when `noiseAmount` is above 0, as the post shader does. */
export function localVolumeProfile(volume, x, y, z, withNoise = true) {
if (volume.shape === 'mesh') return 1;
@@ -143,14 +148,22 @@ export function localVolumeProfile(volume, x, y, z, withNoise = true) {
offsetX = alongX;
offsetY = alongY;
}
- const distance = volume.shape === 'sphere'
- ? Math.hypot(offsetX, offsetY, offsetZ) / volume.radius
- : Math.max(Math.abs(offsetX) / (volume.size[0] / 2), Math.abs(offsetY) / (volume.size[1] / 2), Math.abs(offsetZ) / (volume.size[2] / 2));
- if (distance >= 1) return 0;
let profile = 1;
- if (volume.falloff > 0) {
- const ratio = Math.min(1, Math.max(0, (distance - (1 - volume.falloff)) / volume.falloff));
- profile = 1 - ratio * ratio * (3 - 2 * ratio);
+ if (volume.shape === 'sphere') {
+ const distance = Math.hypot(offsetX, offsetY, offsetZ) / volume.radius;
+ if (distance >= 1) return 0;
+ if (volume.falloff > 0) profile = fadeProfile((distance - (1 - volume.falloff)) / volume.falloff);
+ } else {
+ const ratioX = Math.abs(offsetX) / (volume.size[0] / 2);
+ const ratioY = Math.abs(offsetY) / (volume.size[1] / 2);
+ const ratioZ = Math.abs(offsetZ) / (volume.size[2] / 2);
+ if (Math.max(ratioX, ratioY, ratioZ) >= 1) return 0;
+ if (volume.falloff > 0) {
+ // The distance past the core box, which is round at the corners: the plain largest ratio fades along boxes with mitred creases.
+ const core = 1 - volume.falloff;
+ const fadeDistance = Math.hypot(Math.max(ratioX - core, 0), Math.max(ratioY - core, 0), Math.max(ratioZ - core, 0));
+ profile = fadeProfile(fadeDistance / volume.falloff);
+ }
}
if (withNoise && volume.noiseAmount > 0) profile *= localVolumeNoiseFactor(volume, x, y, z);
return profile;
diff --git a/tests/local-volume-scatter.test.js b/tests/local-volume-scatter.test.js
index 0f751ae1..1b16f4ce 100644
--- a/tests/local-volume-scatter.test.js
+++ b/tests/local-volume-scatter.test.js
@@ -144,7 +144,11 @@ test('the mean of an environment map weights each row by its solid angle', () =>
test('the light of a heavily faded box still falls off steadily from the sunward side: its skin is shaded like its body, not lit as a rim', () => {
const faded = { ...SMOKE_BOX, falloff: 0.9, size: [3, 3, 3], center: [0, 1.5, 0] };
- const columns = [-1.2, -0.6, 0, 0.6, 1.2].map(x => downRay(faded, FULL_SUN, x).emitted[1]);
+ // The light per unit the column extinguishes: a column near the round-faded side holds less smoke, which is not a dimmer light.
+ const columns = [-1.2, -0.6, 0, 0.6, 1.2].map(x => {
+ const column = downRay(faded, FULL_SUN, x);
+ return column.emitted[1] / (1 - column.throughput[1]);
+ });
for (let index = 1; index < columns.length; index++) assert.ok(columns[index] > columns[index - 1], `light at column ${index} (${columns[index]}) is not above column ${index - 1} (${columns[index - 1]})`);
const skinShare = LOCAL_VOLUME_LIGHT_DEPTH_SATURATION;
assert.ok(skinShare > 1, 'the faded skin counts at more than its own density toward the light depth');
@@ -368,3 +372,16 @@ test('the rasterizer and the CPU path tracer light a smoke box alike, and the si
server.kill();
}
});
+
+test('a box fades out round at its corners, so the faded skin shows no mitred crease along the box diagonals', () => {
+ const [box] = resolveLocalVolumes([{ shape: 'box', center: [0, 0, 0], size: [2, 2, 2], falloff: 0.5, noiseAmount: 0 }]);
+ const fadeRatio = 0.6;
+ const expectedOnAxis = 1 - fadeRatio * fadeRatio * (3 - 2 * fadeRatio);
+ assert.ok(Math.abs(localVolumeProfile(box, 0.8, 0, 0) - expectedOnAxis) < 1e-12, 'the fade along an axis is the plain smoothstep');
+ assert.equal(localVolumeProfile(box, 0.5, 0.5, 0.5), 1, 'the core box is full density');
+ const alongEdge = localVolumeProfile(box, 0.8, 0.8, 0);
+ assert.ok(alongEdge < expectedOnAxis, `the edge ${alongEdge} fades sooner than the face ${expectedOnAxis}`);
+ assert.equal(localVolumeProfile(box, 0.95, 0.95, 0.95), 0, 'the corner is clear before the box ends');
+ const diagonal = [0.5, 0.6, 0.7, 0.8].map(value => localVolumeProfile(box, value, value, 0));
+ for (let index = 1; index < diagonal.length; index++) assert.ok(diagonal[index] < diagonal[index - 1], 'the profile falls steadily along the diagonal');
+});