3D engine that AI agents drive
Shade the faded skin of a lit local volume like its body and cap the key gain at a sunlit surface (#855)
Nguyen Xuan Anaabe574fcb91
diff --git a/changes/855-light-depth-skin.md b/changes/855-light-depth-skin.mdnew file mode 100644index 00000000..f38b6b52--- /dev/null+++ b/changes/855-light-depth-skin.md@@ -0,0 +1 @@+- A scattering local volume no longer glows along its faded skin or outshines a sunlit surface: the depth the sun and sky cross counts the faded outer share at up to three times its density, so a sun ray running along the top or side skin is shaded like the body, and the key gain drops from 8 to 4 so the smoke never returns more than a sunlit surface of its albedo, in the rasterizer and the CPU path tracer; `scatter: 0` is unchanged (#855)diff --git a/changes/855-light-depth-skin.size.md b/changes/855-light-depth-skin.size.mdnew file mode 100644index 00000000..dd04cebc--- /dev/null+++ b/changes/855-light-depth-skin.size.md@@ -0,0 +1,3 @@+- entry:fog-shader: one clamp and one constant in the light depth taps of the lit local volume step, a few dozen bytes for a smoke without a bright rim.+- scene:agent-scene+lazy: the same clamp in the CPU path tracer's light depth, counted in the lazy bundle.+- entry:path-tracer: the same clamp in the CPU path tracer's light depth, a few dozen bytes.diff --git a/docs/volumetric.md b/docs/volumetric.mdindex 94094c60..b90f059f 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.+- 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.### Mesh volumesdiff --git a/src/fog-shader.js b/src/fog-shader.jsindex 2ca37870..1867d55a 100644--- a/src/fog-shader.js+++ b/src/fog-shader.js@@ -1,5 +1,5 @@import { registerShaderFeatureCode } from './shader-registry.js';-import { resolveLocalVolumes, createLocalVolumeBuffers, packLocalVolumes, LOCAL_VOLUME_KEY_GAIN, LOCAL_VOLUME_SHADOW_STRENGTH, LOCAL_VOLUME_SKY_GAIN, LOCAL_VOLUME_SUN_FLATTEN } from './local-volumes.js';+import { resolveLocalVolumes, createLocalVolumeBuffers, packLocalVolumes, LOCAL_VOLUME_KEY_GAIN, LOCAL_VOLUME_LIGHT_DEPTH_SATURATION, LOCAL_VOLUME_SHADOW_STRENGTH, LOCAL_VOLUME_SKY_GAIN, LOCAL_VOLUME_SUN_FLATTEN } from './local-volumes.js';import { bindLocalVolumeParts } from './local-volume-parts.js';import { drawMeshVolumes } from './mesh-volume-pass.js';import { FogFieldTexture, createFogLightBuffers, packFogLights, volumeShadowAmbient, DEFAULT_FLOW_STRENGTH, DEFAULT_FLOW_PERIOD, resolveFogDetail, fogUsesDetail } from './fog-field.js';@@ -213,7 +213,7 @@ vec3 localVolumeDepth(int index, vec3 position, vec3 direction, int taps) {float profileSum = 0.0;for (int tap = 0; tap < 3; tap++) {if (tap >= taps) break;- profileSum += localVolumeProfile(index, position + direction * (exitDistance * (float(tap) + 0.5) / float(taps)), false);+ profileSum += min(localVolumeProfile(index, position + direction * (exitDistance * (float(tap) + 0.5) / float(taps)), false) * ${LOCAL_VOLUME_LIGHT_DEPTH_SATURATION.toFixed(3)}, 1.0);}return localVolumeAbsorption[index].rgb * (profileSum * exitDistance / float(taps));}diff --git a/src/local-volumes.js b/src/local-volumes.jsindex 681061c4..07263fee 100644--- a/src/local-volumes.js+++ b/src/local-volumes.js@@ -102,16 +102,24 @@ 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 = 8;+export const LOCAL_VOLUME_KEY_GAIN = 4;++/**+ * 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+ * `min(profile * saturation, 1)`. The many scatters of a real smoke spread light through its thin fringe as through its body, so a sun+ * ray running along the faded top or side skin is shaded by the whole volume, not by the skin's low density. With the plain profile that+ * skin is lit far above the core and reads as a bright rim around the form.+ */+export const LOCAL_VOLUME_LIGHT_DEPTH_SATURATION = 3;/*** How many times the volume's own depth toward the sun counts in the key light's attenuation. Counting it more than once keeps the shaded* side of the smoke clearly darker than the sunlit side, the way the many scatters of a real smoke take the light from its shade.*/-export const LOCAL_VOLUME_SHADOW_STRENGTH = 5;+export const LOCAL_VOLUME_SHADOW_STRENGTH = 3;/** The share of the mean sky radiance the sky term of a scattering volume keeps. The sky lights the whole volume evenly, so a high share flattens the form the sun gives it. */-export const LOCAL_VOLUME_SKY_GAIN = 0.2;+export const LOCAL_VOLUME_SKY_GAIN = 0.5;/*** How much of the sun's elevation the depth toward the sun keeps (1 is the true direction). A low sun lights a smoke box through its top face asdiff --git a/src/path-tracer-volume.js b/src/path-tracer-volume.jsindex 69633073..dc05c69c 100644--- a/src/path-tracer-volume.js+++ b/src/path-tracer-volume.js@@ -1,5 +1,5 @@import { resolveVolumeFog } from './volumetric.js';-import { localVolumeInterval, localVolumeProfile, LOCAL_VOLUME_KEY_GAIN, LOCAL_VOLUME_SHADOW_STRENGTH, LOCAL_VOLUME_SKY_GAIN, LOCAL_VOLUME_SUN_FLATTEN } from './local-volumes.js';+import { localVolumeInterval, localVolumeProfile, LOCAL_VOLUME_KEY_GAIN, LOCAL_VOLUME_LIGHT_DEPTH_SATURATION, LOCAL_VOLUME_SHADOW_STRENGTH, LOCAL_VOLUME_SKY_GAIN, LOCAL_VOLUME_SUN_FLATTEN } from './local-volumes.js';import { countOpenEdges } from './local-volume-parts.js';import { buildTriangleBvh } from './triangle-bvh.js';import { createTracer } from './ray-query.js';@@ -180,7 +180,7 @@ function writeOpticalDepth(volume, x, y, z, direction, taps, span, out) {let profileSum = 0;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, false);+ profileSum += Math.min(localVolumeProfile(volume, x + direction[0] * distance, y + direction[1] * distance, z + direction[2] * distance, false) * LOCAL_VOLUME_LIGHT_DEPTH_SATURATION, 1);}for (let channel = 0; channel < 3; channel++) out[channel] = volume.absorption[channel] * volume.density * profileSum * exitDistance / taps;}diff --git a/tests/local-volume-scatter.test.js b/tests/local-volume-scatter.test.jsindex 3e068efe..0f751ae1 100644--- a/tests/local-volume-scatter.test.js+++ b/tests/local-volume-scatter.test.js@@ -12,7 +12,7 @@ import { freePort } from './helpers/free-port.js';import { renderPathTracedImage } from '../src/path-tracer.js';import { applyViewTransform } from '../src/view-transform.js';import { encodePng } from '../src/export-png.js';-import { resolveLocalVolumes, createLocalVolumeBuffers, packLocalVolumes, localVolumeProfile, LOCAL_VOLUME_KEY_GAIN, LOCAL_VOLUME_SHADOW_STRENGTH, LOCAL_VOLUME_SKY_GAIN, LOCAL_VOLUME_SUN_FLATTEN } from '../src/local-volumes.js';+import { resolveLocalVolumes, createLocalVolumeBuffers, packLocalVolumes, localVolumeProfile, LOCAL_VOLUME_KEY_GAIN, LOCAL_VOLUME_LIGHT_DEPTH_SATURATION, LOCAL_VOLUME_SHADOW_STRENGTH, LOCAL_VOLUME_SKY_GAIN, LOCAL_VOLUME_SUN_FLATTEN } from '../src/local-volumes.js';import { createLocalVolumeCrossing, meanEnvironmentRadiance } from '../src/path-tracer-volume.js';const REGION_TOLERANCE = 0.01;@@ -142,6 +142,21 @@ test('the mean of an environment map weights each row by its solid angle', () =>assert.deepEqual(meanEnvironmentRadiance(null), [0, 0, 0]);});+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]);+ 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');+});++test('a sunlit volume never returns more light per unit it extinguishes than a surface facing the sun would at the same albedo', () => {+ const thin = downRay({ ...SMOKE_BOX, size: [0.05, 0.05, 0.05], falloff: 0 }, FULL_SUN, 0);+ const returned = thin.emitted[1] / (1 - thin.throughput[1]);+ const surfaceFacingSun = SMOKE_BOX.scatter * SUN_COLOR[1] / Math.PI;+ assert.ok(returned <= surfaceFacingSun, `smoke returns ${returned} per unit extinguished, a sunlit surface of the same albedo ${surfaceFacingSun}`);+});+async function startServer(port) {const server = spawn(process.execPath, ['node_modules/vite/bin/vite.js', '--host', '127.0.0.1', '--port', String(port), '--strictPort'], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });await new Promise((resolve, reject) => {