3D engine that AI agents drive
Light fog shafts: keep closed rooms dark and add a glow around bright sources
Nguyen Xuan And0ad63472bd3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b631243e..e7791205 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -2,6 +2,7 @@## Unreleased+- Light the fog around bright sources and keep it dark in closed rooms (part of issue #159, which stays open). `volumeFog` added a constant ambient everywhere, so the air of a closed room glowed; it now keeps 15 percent of it where the sun's shadow map says the fog is in shadow. It also scatters the light of bright pixels sideways: 24 jittered taps of the frame, weighted by the fog opacity. Against Cycles, `dark-shaft` goes from 0.2321 to 0.1425 (shaft region 0.2157 to 0.1219), `temple-hall` from 0.2126 to 0.1438 (region 0.2512 to 0.2341), and the older `fog` from 0.2643 to 0.2238; all other scenes are unchanged to four digits. Core grows 115 bytes gzip and a frame with fog costs about 0.1 to 0.3 ms more. **Fog scenes get brighter around bright sources and darker in shadow.**- Stop world light from reaching closed spaces in a baked scene, and score bright regions on their own (part of issue #159, which stays open). A bake now records how much sky each texel sees from the rays it already shoots, and a part that sees under 98 percent of it gets that as an `occlusionMap` read through the lightmap UVs, so the shader's world diffuse light and reflections shrink inside a closed room. A part that sees the sky gets no map, a scene without a bake runs the same shaders, and there is no new recipe field. Against Cycles, `dark-shaft` (new: a closed room, one roof opening, a sun shaft in fog) goes from 0.2612 with the old bake to 0.1301 and `temple-hall` from 0.2239 to 0.1129 (EEVEE 0.2653 and 0.2341), and a `brightRegion` that `score.py` picks from the Cycles image scores the shaft pixels on their own. The no-bake scores of `outdoor-hdri`, `character`, `metal`, `car-paint` and `foliage` are unchanged to four digits. **A baked part that sees little sky now draws darker.**- Let a key shadow block the sun fully. It kept 18 percent of the sun, so a sealed hall was evenly lit and every shadow was too light. **This darkens shadows in existing scenes that have no fill light.** Against Cycles, `temple-hall` goes from 0.5841 to 0.2126 (EEVEE 0.2341), `mountain-valley` from 0.3658 to 0.3473 and `long-street` from 0.1555 to 0.1407; `fog` (+0.0031) and `megacity-dusk` (+0.0044) get worse.- Compare reference scenes under one sun, and add `mountain-valley`, `temple-hall` and `megacity-dusk`. The HDRI holds its own sun (6 pixels with 48.1 percent of its light), so Cycles and EEVEE drew two shadows of each object where 67js drew one. Both sides now cap the sky at radiance 16, and the mean of the 16 older scenes against Cycles goes from 0.2001 to 0.1751, then to 0.1732 with the shadow change. `khronos.json` and `path-tracer.json` still use the sky with two suns.diff --git a/bench/reference/README.md b/bench/reference/README.mdindex 7e274d5f..490f7a8d 100644--- a/bench/reference/README.md+++ b/bench/reference/README.md@@ -79,6 +79,27 @@ The no-bake scores of the five outdoor scenes are the same to four digits afterFrame cost of a baked room (`node bench/reference/frame-time.mjs dark-shaft gpu`, 960 x 540, RTX 3060 shared with other jobs, so noisy): plain 2.34 and 2.47 ms in two runs, baked with sky visibility 2.98 and 3.86 ms, and the same bake without it 2.04 ms against 2.04 ms plain. So the occlusion map costs roughly 0.6 to 1.4 ms in a closed room on this load; treat it as a measured range, not a figure. Only a baked part that sees under 98 percent of the sky pays it.+## Fog shafts (2026-11-02, part of issue #159)++Gap 3 of the issue, measured on the 67js renders against the Cycles arrays (`volumeFog` in `src/post.js`). The phase function and the units were checked first and are correct: `volumeLightColor` is the sun irradiance, the Henyey-Greenstein term has its 1/4 pi, and the sign of `anisotropy` matches Blender's Volume Scatter, so no tuning constant sits on a bug. Step placement was tried and does not matter: spacing the steps by distance squared moved `dark-shaft` from 0.1292 to 0.1295 and `temple-hall` from 0.2381 to 0.2378, so that change was dropped.++Two errors carry the score:++- **A constant ambient term in a closed room.** The fog adds `ambient` (0.02) everywhere, as if the world lit it. In `dark-shaft` the 67js frame is 0.0125 in linear luminance away from the shaft, where Cycles has 0.002 to 0.006, because a closed room does not see the world. With the fog ambient set to zero the whole frame goes from 0.2321 to 0.1144 and the region from 0.2157 to 0.1728. The ambient now keeps 15 percent of its value where the sun's shadow map says the fog is in shadow, and all of it where the sun reaches the fog.+- **No glow around a bright source.** Cycles scatters the light of bright pixels sideways. Around the lit door of `temple-hall`, the mean linear luminance in a ring 30 to 45 pixels from the door is 0.1055 in Cycles and 0.0373 in 67js (EEVEE 0.0526). The fog now adds a blur of the frame: 24 taps over about 14 percent of the frame height, weighted by the fog's opacity along the ray (`1 - transmittance`) and tinted by the fog colour. The taps are rotated by the same per-pixel jitter as the steps. The same ring now reads 0.0974, and every ring out to 300 pixels is within 0.008 of Cycles. The blur radius and the weight (0.009) are fitted to that profile and to the two region scores; they are not a recipe field.++Scores against Cycles on 2026-11-02, whole frame (shaft region), RTX 3060, 67js side only:++| Scene | No bake before | No bake now | Baked before | Baked now | EEVEE |+| --- | --- | --- | --- | --- | --- |+| `dark-shaft` | 0.2321 (0.2157) | 0.1425 (0.1219) | 0.1301 (0.1710) | 0.0669 (0.1814) | 0.2653 (0.2643) |+| `temple-hall` | 0.2126 (0.2512) | 0.1438 (0.2341) | 0.1129 (0.2528) | 0.0716 (0.2575) | 0.2341 (0.2455) |+| `fog` | 0.2643 | 0.2238 | | | 0.2662 |++The region of `dark-shaft` improved clearly without a bake (0.2157 to 0.1219). The region of `temple-hall` moved less (0.2512 to 0.2341). What is left there is haze outside the shafts, where 67js is 0.0095 and Cycles 0.0033. A higher ambient floor lowers the region score (0.2100 at a floor of 0.4) but raises the region of `dark-shaft` (0.1676) and both whole-frame scores, so one constant cannot serve both scenes. The baked rows improved as a whole frame and got slightly worse in the region (0.1710 to 0.1814, 0.2528 to 0.2575): the bake already darkens the walls, and the fog change takes light from the same pixels. `fog` improved by 0.0405, well inside the limit. Every other scene has the same scores to four digits, and a scene without `volumeFog` runs the same post shader as before.++Cost: `frame-time.mjs dark-shaft gpu`, plain frame, two runs each on a loaded machine: 1.9 and 2.1 ms before, 2.1 and 2.3 ms after, so about 0.1 to 0.3 ms for the 24 extra taps. The core entry grows by 115 bytes gzip (83,853 to 83,968), and six size budgets rose by 50 to 100 bytes because their bundles contain the post shader. `images/dark-shaft-baked.jpg` and `images/temple-hall-baked.jpg` come from `npm run reference -- --bake dark-shaft,temple-hall`.+## BaselineMeasured on 2026-10-30 with an RTX 3060, SwiftShader and Blender 5.2.2. The metric is [FLIP](https://github.com/NVlabs/flip) for LDR images: 0 means identical, and a higher score means a larger visible difference.diff --git a/bench/reference/baseline.json b/bench/reference/baseline.jsonindex ec6c9017..058440b8 100644--- a/bench/reference/baseline.json+++ b/bench/reference/baseline.json@@ -141,24 +141,24 @@"gpu-vs-cpu": 0.0066},"dark-shaft": {- "gpu-vs-cycles": 0.2321,- "gpu-vs-eevee": 0.0811,- "cpu-vs-cycles": 0.2323,- "cpu-vs-eevee": 0.0807,+ "gpu-vs-cycles": 0.1425,+ "gpu-vs-eevee": 0.1826,+ "cpu-vs-cycles": 0.1431,+ "cpu-vs-eevee": 0.182,"eevee-vs-cycles": 0.2653,- "gpu-vs-cpu": 0.0029,+ "gpu-vs-cpu": 0.0057,"region-pixels": 0.099,- "region-gpu-vs-cycles": 0.2157,- "region-gpu-vs-eevee": 0.1444,+ "region-gpu-vs-cycles": 0.1219,+ "region-gpu-vs-eevee": 0.2248,"region-eevee-vs-cycles": 0.2643},"fog": {- "gpu-vs-cycles": 0.2643,- "gpu-vs-eevee": 0.053,- "cpu-vs-cycles": 0.2639,- "cpu-vs-eevee": 0.053,+ "gpu-vs-cycles": 0.2238,+ "gpu-vs-eevee": 0.1077,+ "cpu-vs-cycles": 0.2235,+ "cpu-vs-eevee": 0.1082,"eevee-vs-cycles": 0.2662,- "gpu-vs-cpu": 0.0085+ "gpu-vs-cpu": 0.0111},"foliage": {"gpu-vs-cycles": 0.129,@@ -257,15 +257,15 @@"gpu-vs-cpu": 0.0032},"temple-hall": {- "gpu-vs-cycles": 0.2126,- "gpu-vs-eevee": 0.0803,- "cpu-vs-cycles": 0.2128,- "cpu-vs-eevee": 0.0804,+ "gpu-vs-cycles": 0.1438,+ "gpu-vs-eevee": 0.1553,+ "cpu-vs-cycles": 0.1443,+ "cpu-vs-eevee": 0.1548,"eevee-vs-cycles": 0.2341,- "gpu-vs-cpu": 0.0025,+ "gpu-vs-cpu": 0.007,"region-pixels": 0.0958,- "region-gpu-vs-cycles": 0.2512,- "region-gpu-vs-eevee": 0.1931,+ "region-gpu-vs-cycles": 0.2341,+ "region-gpu-vs-eevee": 0.2759,"region-eevee-vs-cycles": 0.2455}}diff --git a/bench/reference/images/dark-shaft-baked.jpg b/bench/reference/images/dark-shaft-baked.jpgindex 847caaae..c65a20a0 100644Binary files a/bench/reference/images/dark-shaft-baked.jpg and b/bench/reference/images/dark-shaft-baked.jpg differdiff --git a/bench/reference/images/dark-shaft.jpg b/bench/reference/images/dark-shaft.jpgindex 802a7872..5ec8379d 100644Binary files a/bench/reference/images/dark-shaft.jpg and b/bench/reference/images/dark-shaft.jpg differdiff --git a/bench/reference/images/fog.jpg b/bench/reference/images/fog.jpgindex ec6f6d3d..ed4709a5 100644Binary files a/bench/reference/images/fog.jpg and b/bench/reference/images/fog.jpg differdiff --git a/bench/reference/images/temple-hall-baked.jpg b/bench/reference/images/temple-hall-baked.jpgindex 6ab312c5..10156610 100644Binary files a/bench/reference/images/temple-hall-baked.jpg and b/bench/reference/images/temple-hall-baked.jpg differdiff --git a/bench/reference/images/temple-hall.jpg b/bench/reference/images/temple-hall.jpgindex 462e7f74..a211300d 100644Binary files a/bench/reference/images/temple-hall.jpg and b/bench/reference/images/temple-hall.jpg differdiff --git a/bench/size/budgets.json b/bench/size/budgets.jsonindex d806d302..d9f2012f 100644--- a/bench/size/budgets.json+++ b/bench/size/budgets.json@@ -11,8 +11,8 @@"entry:nature": 13200,"entry:motion": 13600,"entry:canvas2d": 9400,- "entry:gltf": 52800,- "entry:gltf+lazy": 56150,+ "entry:gltf": 52850,+ "entry:gltf+lazy": 56200,"entry:modeling": 8700,"entry:sculpt": 2500,"entry:surface": 10100,@@ -34,7 +34,7 @@"entry:path-tracer": 14400,"entry:texture-set": 900,"entry:agent-scene": 74900,- "entry:agent-scene+lazy": 253400,+ "entry:agent-scene+lazy": 253450,"entry:agent-asset": 106300,"entry:agent-asset+lazy": 264850,"entry:hdri": 3300,@@ -43,10 +43,10 @@"entry:material-layer-shaders": 20300,"entry:light-linking": 18650,"entry:cascaded-shadows": 27300,- "scene:lit-box": 61400,+ "scene:lit-box": 61450,"scene:gltf-box": 82500,- "scene:gltf-box+lazy": 85700,- "scene:agent-scene": 101150,+ "scene:gltf-box+lazy": 85800,+ "scene:agent-scene": 101200,"scene:agent-scene+lazy": 279700}}diff --git a/src/post.js b/src/post.jsindex dfb994dd..9ef02846 100644--- a/src/post.js+++ b/src/post.js@@ -136,11 +136,16 @@ vec3 volumeFog(vec3 background) {if (all(greaterThanEqual(shadow, vec3(0))) && all(lessThanEqual(shadow, vec3(1)))) {visibility = step(shadow.z - 0.0008, texture(volumeShadowMap, shadow.xy).r);}- vec3 scattering = volumeColor * (volumeAmbient + volumeLightColor * phase * visibility);+ vec3 scattering = volumeColor * (volumeAmbient * mix(0.15, 1.0, visibility) + volumeLightColor * phase * visibility);accumulated += transmittance * opacity * scattering;transmittance *= 1.0 - opacity;if (transmittance < 0.001) break;}+ for (int tap = 0; tap < 24; tap++) {+ float index = float(tap) + jitter;+ vec2 offset = vec2(cos(index * 2.4) * resolution.y / resolution.x, sin(index * 2.4)) * (0.03 + 0.0002 * index * index);+ accumulated += texture(frameTexture, textureCoordinate + offset).rgb * volumeColor * (0.009 * (1.0 - transmittance) / (1.0 + 0.04 * index));+ }return accumulated + background * transmittance;}void main() {