3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Add opt-in beamRadius so fog octaves stay near the sun beam (part of #643)
Nguyen Xuan An935fdce67466
diff --git a/changes/643.md b/changes/643.mdnew file mode 100644index 00000000..34f73a9f--- /dev/null+++ b/changes/643.md@@ -0,0 +1 @@+- Volume fog takes `beamRadius` (metres, off by default): with `scatterOctaves` above 1 the extra octaves count only as far as the sun reaches around each point, so a narrow shaft keeps a narrow glow; in the dark-shaft reference the octaves cost 0.01 FLIP and with it on that cost is gone (#643)diff --git a/changes/643.size.md b/changes/643.size.mdnew file mode 100644index 00000000..785f57f9--- /dev/null+++ b/changes/643.size.md@@ -0,0 +1,2 @@+- scene:agent-scene+lazy: 178 bytes gzip for the beam-local octave weight (four shadow-map taps and one uniform) that removes the 0.01 FLIP the fog octaves cost in the dark-shaft reference.+- goal:agent-scene: 29 bytes gzip for the same beam-local octave weight, which is the whole fog shader's growth in the core scene.diff --git a/docs/agent-look.md b/docs/agent-look.mdindex 0f961d04..7cb8a2d1 100644--- a/docs/agent-look.md+++ b/docs/agent-look.md@@ -228,7 +228,7 @@ These controls reuse `resolveVolumeFog` and its existing domain rules.| `noiseScale` | `0.3` | `0.001` to `100` || `maxDistance` | `100` | `0.01` to `10000` || `shadowAmbient` | derived from the scene: 0.15 in a closed room, 1 under open sky | `0` to `1` |-| `scatterOctaves`, `multiScatter`, `multiExtinction`, `multiAnisotropy`, `keyTransmittance`, `surfaceTransmittance` | `1`, `0.5`, `0.5`, `0.5`, `0`, `0` | Integer `1` to `4`, then `0` to `1` each; off by default, see [volume fog](https://crucible.codepawl.com/volumetric.md) |+| `scatterOctaves`, `multiScatter`, `multiExtinction`, `multiAnisotropy`, `keyTransmittance`, `surfaceTransmittance`, `beamRadius` | `1`, `0.5`, `0.5`, `0.5`, `0`, `0`, `0` | Integer `1` to `4`, then `0` to `1` each, `beamRadius` `0` to `20` metres; off by default, see [volume fog](https://crucible.codepawl.com/volumetric.md) || `detailOctaves`, `detailAmount`, `warp`, `contrast`, `profileSoftness`, `keyScattering` | `1`, `0.5`, `0`, `1`, `0.5`, `1` | Integer `1` to `3`, `0` to `1`, `0` to `4`, `0` to `4`, `0` to `0.95`, `0` to `10`; see [volume fog](https://crucible.codepawl.com/volumetric.md) |Fog bounds reject dimensions that collapse after Float32 rounding.diff --git a/docs/volumetric.md b/docs/volumetric.mdindex bb8d5522..08ecd3bc 100644--- a/docs/volumetric.md+++ b/docs/volumetric.md@@ -58,6 +58,7 @@ renderer.render({ eye: [0, 2, 5], target: [0, 1, 0], time: 0 });| `multiScatter`, `multiExtinction`, `multiAnisotropy` | `0.5`, `0.5`, `0.5` | `0…1` each; the octave factors above. They act only with `scatterOctaves` above 1 || `keyTransmittance` | `0` | `0…1`; how much of the fog between a point and the sun dims the key light scattered there (three taps along the light, to the box exit). 0 keeps today's look || `surfaceTransmittance` | `0` | `0…1`; the same dimming for the key light on a lit surface inside the box, so a dense haze dims a sunlit floor. 0 keeps today's look |+| `beamRadius` | `0` | `0…20` metres; above 0 the octaves beyond the first count only as far as the sun reaches around the point: four shadow-map taps this far off, across the sun's direction, give the lit share that scales them. A narrow shaft keeps a narrow glow and a wide beam keeps all of it. 0 keeps every octave as is. Acts only with `scatterOctaves` above 1 |Values must be finite numbers, and the detail fields are checked with the rest when the scene or a `set-environment` edit is built. Active volume fog accepts finite frame time within ±1,000,000 seconds and a nonzero light direction with components within ±1,000,000. Noise samples absolute time, so backward seeks reproduce earlier density.diff --git a/src/agent-look.js b/src/agent-look.jsindex 72008cdf..7f272b80 100644--- a/src/agent-look.js+++ b/src/agent-look.js@@ -38,7 +38,7 @@ const POINT_LIGHT_FIELDS = ['position', 'color', 'intensity', 'radius', 'range',const SPOT_LIGHT_FIELDS = ['position', 'direction', 'angle', 'penumbra', 'color', 'intensity', 'radius', 'range', 'castShadow', 'softFalloff', 'ies', 'affects'];const AREA_LIGHT_FIELDS = ['position', 'direction', 'up', 'width', 'height', 'color', 'intensity', 'castShadow', 'ies', 'portal', 'affects'];const VOLUME_VECTOR_FIELDS = ['minimum', 'maximum', 'color', 'ambient', 'drift'];-const VOLUME_NUMBER_FIELDS = ['density', 'steps', 'anisotropy', 'noiseAmount', 'noiseScale', 'maxDistance', 'shadowAmbient', 'heightFalloff', 'detailOctaves', 'detailAmount', 'warp', 'contrast', 'profileSoftness', 'keyScattering', 'scatterOctaves', 'multiScatter', 'multiExtinction', 'multiAnisotropy', 'keyTransmittance', 'surfaceTransmittance'];+const VOLUME_NUMBER_FIELDS = ['density', 'steps', 'anisotropy', 'noiseAmount', 'noiseScale', 'maxDistance', 'shadowAmbient', 'heightFalloff', 'detailOctaves', 'detailAmount', 'warp', 'contrast', 'profileSoftness', 'keyScattering', 'scatterOctaves', 'multiScatter', 'multiExtinction', 'multiAnisotropy', 'keyTransmittance', 'surfaceTransmittance', 'beamRadius'];export const VOLUME_FOG_FIELDS = Object.freeze([...VOLUME_VECTOR_FIELDS, ...VOLUME_NUMBER_FIELDS]);const BLOOM_FIELDS = ['strength', 'threshold', 'radius'];const DEPTH_OF_FIELD_FIELDS = ['blur', 'focusDistance'];diff --git a/src/fog-field.js b/src/fog-field.jsindex 170dd214..396750c9 100644--- a/src/fog-field.js+++ b/src/fog-field.js@@ -6,7 +6,7 @@ export const FOG_LIGHT_LIMIT = 8;export const DEFAULT_FLOW_STRENGTH = 1;export const DEFAULT_FLOW_PERIOD = 2;// What a fog without detail controls uses: one octave, no warp, the noise as is, the layer top fading from half its height.-const DEFAULT_DETAIL = { detailOctaves: 1, detailAmount: 0.5, warp: 0, contrast: 1, profileSoftness: 0.5, keyScattering: 1, scatterOctaves: 1, multiScatter: 0.5, multiExtinction: 0.5, multiAnisotropy: 0.5, keyTransmittance: 0, surfaceTransmittance: 0 };+const DEFAULT_DETAIL = { detailOctaves: 1, detailAmount: 0.5, warp: 0, contrast: 1, profileSoftness: 0.5, keyScattering: 1, scatterOctaves: 1, multiScatter: 0.5, multiExtinction: 0.5, multiAnisotropy: 0.5, keyTransmittance: 0, surfaceTransmittance: 0, beamRadius: 0 };const DETAIL_LIMITS = {detailOctaves: [1, 3, 'Volume detail octaves'],detailAmount: [0, 1, 'Volume detail amount'],@@ -20,6 +20,7 @@ const DETAIL_LIMITS = {multiAnisotropy: [0, 1, 'Volume multi anisotropy'],keyTransmittance: [0, 1, 'Volume key transmittance'],surfaceTransmittance: [0, 1, 'Volume surface transmittance'],+ beamRadius: [0, 20, 'Volume beam radius'],};/** The six detail fields of a resolved volume fog with defaults filled in. Anything unset or out of range throws like the core fields. */diff --git a/src/fog-shader.js b/src/fog-shader.jsindex 4f5a4d4a..f400926b 100644--- a/src/fog-shader.js+++ b/src/fog-shader.js@@ -60,6 +60,7 @@ function bindDetail(context, post, volume) {context.uniform1f(locate('volumeMultiAnisotropy'), detail.multiAnisotropy);context.uniform1f(locate('volumeKeyTransmittance'), detail.keyTransmittance);context.uniform1f(locate('volumeSurfaceTransmittance'), detail.surfaceTransmittance);+ context.uniform1f(locate('volumeBeamRadius'), detail.beamRadius);}// The fog's field texture and light buffers; a program without those steps has no such uniforms, and a null location is ignored.@@ -842,6 +843,7 @@ uniform float volumeMultiExtinction;uniform float volumeMultiAnisotropy;uniform float volumeKeyTransmittance;uniform float volumeSurfaceTransmittance;+uniform float volumeBeamRadius;uniform int volumeLightCount;uniform vec4 volumeLightPositions[8];uniform vec4 volumeLightColors[8];@@ -946,7 +948,15 @@ float sunOpticalDepth(vec3 position, vec3 towardSun) {}// The sun's in-scatter at a point. The octave sum approximates multiple scattering (Wrenninge et al. 2015, "Oz: the great and// volumetric"): octave n scatters multiScatter^n as much, sees half^n of the extinction and has half^n of the anisotropy.-float keyInScatter(float cosine, float opticalDepth) {+// With beamRadius above 0 the octaves beyond the first count only as far as the sun reaches around the point: multiple scattering+// of a narrow shaft leaves it sideways, so its glow stays narrow, and a wide beam keeps all of it. beamShare is the lit share of four taps that far+// off the point across the sun's direction.+float beamShare(vec3 position, vec3 towardSun) {+ vec3 across = normalize(cross(towardSun, abs(towardSun.y) < 0.9 ? vec3(0, 1, 0) : vec3(1, 0, 0)));+ vec3 upward = cross(towardSun, across);+ return 0.25 * (keyVisibility(position + across * volumeBeamRadius) + keyVisibility(position - across * volumeBeamRadius) + keyVisibility(position + upward * volumeBeamRadius) + keyVisibility(position - upward * volumeBeamRadius));+}+float keyInScatter(float cosine, float opticalDepth, float octaveShare) {float total = 0.0;float weight = 1.0;float extinction = 1.0;@@ -954,7 +964,7 @@ float keyInScatter(float cosine, float opticalDepth) {for (int octave = 0; octave < 4; octave++) {if (octave >= volumeScatterOctaves) break;total += weight * henyeyGreenstein(cosine, anisotropy) * exp(-opticalDepth * volumeKeyTransmittance * extinction);- weight *= volumeMultiScatter;+ weight *= volumeMultiScatter * octaveShare;extinction *= volumeMultiExtinction;anisotropy *= volumeMultiAnisotropy;}@@ -1006,7 +1016,9 @@ vec3 volumeFog(vec3 background) {if (volumeGridFog > 0.5) ambientShare = min(1.0, 30.0 * skyCell.r);float opticalDepth = 0.0;if (sunTransmittance && visibility > 0.001) opticalDepth = sunOpticalDepth(position, towardSun);- float phase = keyInScatter(cosine, opticalDepth);+ float octaveShare = 1.0;+ if (volumeBeamRadius > 0.0 && volumeScatterOctaves > 1 && visibility > 0.001) octaveShare = beamShare(position, towardSun);+ float phase = keyInScatter(cosine, opticalDepth, octaveShare);vec3 scattering = volumeColor * (volumeAmbient * ambientShare + 0.6 * skyCell.gba + volumeLightColor * (phase * visibility * volumeKeyScattering));if (POST_LIGHTS) scattering += volumeColor * fogLightScattering(position, direction);accumulated += transmittance * opacity * scattering;diff --git a/tests/fog-field.test.js b/tests/fog-field.test.jsindex c6a5147c..8be70339 100644--- a/tests/fog-field.test.js+++ b/tests/fog-field.test.js@@ -84,7 +84,7 @@ const DETAIL_FIELDS = ['detailOctaves', 'detailAmount', 'warp', 'contrast', 'protest('a fog without detail controls resolves to the default detail and needs no detail step', () => {const resolved = resolveVolumeFog({ density: 0.1 });for (const key of DETAIL_FIELDS) assert.equal(Object.hasOwn(resolved, key), false, key);- assert.deepEqual(resolveFogDetail(resolved), { detailOctaves: 1, detailAmount: 0.5, warp: 0, contrast: 1, profileSoftness: 0.5, keyScattering: 1, scatterOctaves: 1, multiScatter: 0.5, multiExtinction: 0.5, multiAnisotropy: 0.5, keyTransmittance: 0, surfaceTransmittance: 0 });+ assert.deepEqual(resolveFogDetail(resolved), { detailOctaves: 1, detailAmount: 0.5, warp: 0, contrast: 1, profileSoftness: 0.5, keyScattering: 1, scatterOctaves: 1, multiScatter: 0.5, multiExtinction: 0.5, multiAnisotropy: 0.5, keyTransmittance: 0, surfaceTransmittance: 0, beamRadius: 0 });assert.equal(fogUsesDetail(resolved), false);assert.equal(fogUsesDetail(resolveVolumeFog({ density: 0.1, detailOctaves: 1, warp: 0, contrast: 1, profileSoftness: 0.2, keyScattering: 3 })), false, 'profile and key scattering are plain uniforms');});@@ -114,3 +114,10 @@ test('the detail step is compiled in only on request', () => {assert.match(createPostFragment(), /#define POST_DETAIL false/);assert.match(createPostFragment([...POST_STEPS, 'detail']), /#define POST_DETAIL \(true\)/);});++test('beamRadius keeps the beam-local octave radius, defaults to off and stays within 0 to 20 metres', () => {+ assert.equal(resolveFogDetail(resolveVolumeFog({ density: 0.1 })).beamRadius, 0);+ assert.equal(resolveFogDetail(resolveVolumeFog({ density: 0.1, beamRadius: 1.5 })).beamRadius, 1.5);+ assert.equal(normalizeAgentLook({ environment: { volumeFog: { density: 0.5, beamRadius: 2 } } }).environment.volumeFog.beamRadius, 2);+ for (const bad of [-1, 21, '2', NaN]) assert.throws(() => resolveFogDetail(resolveVolumeFog({ beamRadius: bad })), /Volume beam radius must be between 0 and 20/, String(bad));+});diff --git a/tests/volume-build-validation.test.js b/tests/volume-build-validation.test.jsindex b2647caf..4dee507c 100644--- a/tests/volume-build-validation.test.js+++ b/tests/volume-build-validation.test.js@@ -31,6 +31,7 @@ const BAD_DETAIL_FIELDS = [['multiScatter', 2, /Volume multi scatter must be between 0 and 1/],['detailAmount', 2, /Volume detail amount must be between 0 and 1/],['surfaceTransmittance', -0.5, /Volume surface transmittance must be between 0 and 1/],+ ['beamRadius', 21, /Volume beam radius must be between 0 and 20/],['multiExtinction', 2, /Volume multi extinction must be between 0 and 1/],['multiAnisotropy', -1, /Volume multi anisotropy must be between 0 and 1/],['keyTransmittance', 2, /Volume key transmittance must be between 0 and 1/],