3D engine that AI agents drive
Give a lit local volume a gradient across its form instead of an edge glow (#855)
Nguyen Xuan Anc0707ea3ceb9
diff --git a/changes/855-sun-gradient.md b/changes/855-sun-gradient.mdnew file mode 100644index 00000000..22a9da1e--- /dev/null+++ b/changes/855-sun-gradient.md@@ -0,0 +1 @@+- The sun side of a scattering local volume is clearly brighter than its shade side without a glowing top or side rim: the depth toward the sun is measured along the sun's direction with its elevation cut to a tenth, the key gain rises to 8 and the shadow strength to 5, and the sky term keeps a fifth of its share, in the rasterizer and the CPU path tracer; `scatter: 0` is unchanged (#855)diff --git a/changes/855-sun-gradient.size.md b/changes/855-sun-gradient.size.mdnew file mode 100644index 00000000..db97166d--- /dev/null+++ b/changes/855-sun-gradient.size.md@@ -0,0 +1,3 @@+- entry:fog-shader: the flattened sun direction and the sky share in the lit local volume step, a few dozen bytes for a gradient across a smoke that reads as form instead of an edge glow.+- scene:agent-scene+lazy: the same flattened direction and sky share in the CPU path tracer's lit volume step, counted in the lazy bundle.+- entry:path-tracer: the flattened direction vector and the sky gain in the CPU path tracer's lit volume step, a few hundred bytes.diff --git a/docs/volumetric.md b/docs/volumetric.mdindex 90784b81..94094c60 100644--- a/docs/volumetric.md+++ b/docs/volumetric.md@@ -135,8 +135,8 @@ Each volume is marched on its own between its entry and its exit, cut at the firA 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 * 4)` 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 smooth profile, without the noise, so the noise does not speckle the shading). The factor 4 (`LOCAL_VOLUME_SHADOW_STRENGTH` in `src/local-volumes.js`) is there because a sun in front of the smoke reaches it through its near face, so the plain depth differs little across the frame; the key light is also scaled by `LOCAL_VOLUME_KEY_GAIN` (4), the many scatters of a thick smoke that one isotropic scatter leaves a dozen times dimmer than the sky term. Together they make the sunward side clearly 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. 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.+- The key light uses the same shadow map lookup (`keyVisibility`) and phase function (`henyeyGreenstein`, isotropic here) as `volumeFog`, times `exp(-depth * 5)` 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 smooth profile, without the noise, so the noise does not speckle the shading). The direction of those taps is the sun's with its elevation cut to a tenth (`LOCAL_VOLUME_SUN_FLATTEN`): a low sun reaches a smoke box through its top face as fast as through its side, which made the top edge glow as bright as the sunward edge and hid the gradient across the form, and the many scatters of a real smoke spread the light sideways anyway. The factor 5 (`LOCAL_VOLUME_SHADOW_STRENGTH` in `src/local-volumes.js`) counts the depth more than once so the shaded side stays dark; the key light is also scaled by `LOCAL_VOLUME_KEY_GAIN` (8), the many scatters of a thick smoke that one isotropic scatter leaves far dimmer. Together they make the sunward side clearly 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. 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.diff --git a/src/fog-shader.js b/src/fog-shader.jsindex 654632b9..2ca37870 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 } from './local-volumes.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 { 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';@@ -225,14 +225,18 @@ vec3 localVolumeSkyLight(int index, vec3 position) {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);+ return localSkyAmbient * (open * ${(0.2 * LOCAL_VOLUME_SKY_GAIN).toFixed(3)});}// 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 * ${LOCAL_VOLUME_KEY_GAIN.toFixed(1)}) * exp(-localVolumeDepth(index, position, towardSun, 3) * ${LOCAL_VOLUME_SHADOW_STRENGTH.toFixed(1)});+ if (visibility > 0.001) {+ vec3 lightDirection = normalize(vec3(towardSun.x, towardSun.y * ${LOCAL_VOLUME_SUN_FLATTEN.toFixed(3)}, towardSun.z));+ vec3 lightDepth = localVolumeDepth(index, position, lightDirection, 3) * ${LOCAL_VOLUME_SHADOW_STRENGTH.toFixed(3)};+ sunLight = volumeLightColor * (henyeyGreenstein(dot(direction, towardSun), 0.0) * visibility * ${LOCAL_VOLUME_KEY_GAIN.toFixed(3)}) * exp(-lightDepth);+ }return localVolumeSkyLight(index, position) + sunLight;}vec3 localVolumes(vec3 background) {diff --git a/src/local-volumes.js b/src/local-volumes.jsindex 8bcd723a..681061c4 100644--- a/src/local-volumes.js+++ b/src/local-volumes.js@@ -99,17 +99,27 @@ export function packLocalVolumes(volumes, buffers) {/*** How much the key light of a scattering volume is scaled over the single-scatter estimate. A smoke thick enough to show form scatters the sun many times- * and reads about as bright as a surface in the same sun, which one isotropic scatter (1 / 4 pi of the key colour) leaves about a dozen times too dark- * beside the sky term; the gain keeps the sunlit side clearly above the shaded side. The rasterizer and the CPU path tracer both apply it.+ * 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 = 8;/**- * How many times the volume's own depth toward the sun counts in the key light's attenuation. The sun of a scene seen from the front reaches a- * smoke through its near face, so the optical depth toward it differs little across the frame; counting the depth more than once keeps the shaded- * side of the smoke visibly darker than the sunlit side, the way the many scatters of a real smoke take the light from its shade.+ * 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 = 4;+export const LOCAL_VOLUME_SHADOW_STRENGTH = 5;++/** 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;++/**+ * 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 as+ * quickly as through its side, so with the true direction the top edge glows as bright as the sunward edge and the gradient across the form is lost;+ * flattening the direction measures the depth the light crosses sideways, as the many scatters of a real smoke spread it. Above 0 so a sun straight+ * overhead keeps a direction.+ */+export const LOCAL_VOLUME_SUN_FLATTEN = 0.1;/** 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) {diff --git a/src/path-tracer-volume.js b/src/path-tracer-volume.jsindex ca0d2cca..69633073 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 } from './local-volumes.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 { countOpenEdges } from './local-volume-parts.js';import { buildTriangleBvh } from './triangle-bvh.js';import { createTracer } from './ray-query.js';@@ -203,6 +203,7 @@ export function createLocalVolumeCrossing(volumes, lighting = null) {const sunDepth = new Float64Array(3);const sunLight = new Float64Array(3);const skyLight = new Float64Array(3);+ const flattenedDirection = 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) {@@ -211,7 +212,7 @@ export function createLocalVolumeCrossing(volumes, lighting = null) {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];+ for (let channel = 0; channel < 3; channel++) skyLight[channel] *= lighting.ambient[channel] * LOCAL_VOLUME_SKY_GAIN;}// Adds the light one step of a scattering volume sends back along the ray: scatter * (1 - exp(-extinction * width)) * (sky + sun).@@ -220,10 +221,16 @@ export function createLocalVolumeCrossing(volumes, lighting = null) {sunLight.fill(0);const sun = lighting.sun;if (sun && lighting.sunVisibility(x, y, z) > 0) {- writeOpticalDepth(volume, x, y, z, sun.direction, SUN_DEPTH_TAPS, span, sunDepth);+ const flattenedLength = Math.hypot(sun.direction[0], sun.direction[1] * LOCAL_VOLUME_SUN_FLATTEN, sun.direction[2]);+ flattenedDirection[0] = sun.direction[0] / flattenedLength;+ flattenedDirection[1] = sun.direction[1] * LOCAL_VOLUME_SUN_FLATTEN / flattenedLength;+ flattenedDirection[2] = sun.direction[2] / flattenedLength;+ writeOpticalDepth(volume, x, y, z, flattenedDirection, 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 * LOCAL_VOLUME_KEY_GAIN * Math.exp(-sunDepth[channel] * LOCAL_VOLUME_SHADOW_STRENGTH);+ for (let channel = 0; channel < 3; channel++) {+ sunLight[channel] = sun.color[channel] * phase * LOCAL_VOLUME_KEY_GAIN * Math.exp(-sunDepth[channel] * LOCAL_VOLUME_SHADOW_STRENGTH);+ }}for (let channel = 0; channel < 3; channel++) {const scattered = volume.scatter * (1 - Math.exp(-volume.absorption[channel] * volume.density * width));diff --git a/tests/local-volume-scatter.test.js b/tests/local-volume-scatter.test.jsindex 312ae636..3e068efe 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 } from '../src/local-volumes.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 { createLocalVolumeCrossing, meanEnvironmentRadiance } from '../src/path-tracer-volume.js';const REGION_TOLERANCE = 0.01;@@ -93,6 +93,19 @@ test('the key light of a thin volume is the phase function times the key gain, aassert.ok(lit > shaded * 2.5, `lit side ${lit} against shaded side ${shaded}`);});+test('the light falls off steadily across the box from the sunward side, with no bright rim at the middle or the shaded side', () => {+ const columns = [-1.2, -0.6, 0, 0.6, 1.2].map(x => downRay(SMOKE_BOX, 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]})`);+ assert.ok(columns[4] > columns[0] * 2.5, `sunward column ${columns[4]} against shaded column ${columns[0]}`);+});++test('a sun straight overhead still lights the volume: the flattened direction keeps a direction', () => {+ const overhead = { sun: { direction: [0, 1, 0], color: SUN_COLOR }, ambient: [0, 0, 0], sunVisibility: () => 1 };+ const emitted = downRay(SMOKE_BOX, overhead, 0).emitted;+ assert.ok(emitted.every(value => Number.isFinite(value) && value > 0), `overhead sun gave ${emitted}`);+ assert.ok(LOCAL_VOLUME_SUN_FLATTEN > 0 && LOCAL_VOLUME_SUN_FLATTEN < 1);+});+test('the light depth of a noisy volume is its smooth envelope, so the noise does not speckle the shading', () => {const [noisy] = resolveLocalVolumes([{ ...SMOKE_BOX, noiseAmount: 0.9, noiseScale: 2.2 }]);const [smooth] = resolveLocalVolumes([{ ...SMOKE_BOX }]);@@ -105,7 +118,7 @@ test('the light depth of a noisy volume is its smooth envelope, so the noise doefunction 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);+ return result.emitted[1] / ((1 - result.throughput[1]) * 0.8 * SKY_LEVEL * LOCAL_VOLUME_SKY_GAIN);}test('the sky lights a thin volume at full strength and a thick one less, through its own depth', () => {@@ -118,7 +131,7 @@ test('the sky lights a thin volume at full strength and a thick one less, througtest('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);- assert.ok(result.emitted[1] > 0 && result.emitted[1] < 0.8 * (1 - result.throughput[1]) * SKY_LEVEL);+ assert.ok(result.emitted[1] > 0 && result.emitted[1] < 0.8 * (1 - result.throughput[1]) * SKY_LEVEL * LOCAL_VOLUME_SKY_GAIN);const blocked = downRay(SMOKE_BOX, { ...FULL_SUN, sunVisibility: () => 0 }, 1.2);assert.deepEqual(blocked.emitted, [0, 0, 0]);});