3D engine that AI agents drive
Draw both faces in the sun shadow pass so open surfaces stop leaking light (part of #206)
Nguyen Xuan An005c1aaff5cf
diff --git a/bench/reference/README.md b/bench/reference/README.mdindex 1ca326ef..f555a32b 100644--- a/bench/reference/README.md+++ b/bench/reference/README.md@@ -39,7 +39,7 @@ All of them are against the Blender 5.2.2 renders taken on 2026-11-03 with the s| `megacity-dusk` | 0.1627 | 0.1524 | 0.1022 | | || `metal` | 0.1036 | 0.0916 | 0.0782 | | 0.1492 || `metal-room` | 0.5076 | 0.5262 | 0.1082 | 0.1716 | |-| `mountain-valley` | 0.2629 | 0.1879 | 0.2981 | | |+| `mountain-valley` | 0.1792 | 0.1879 | 0.2692 | | || `outdoor-hdri` | 0.0462 | 0.0403 | 0.0296 | | 0.1242 || `spot-area` | 0.1242 | 0.1935 | 0.1707 | | 0.0658 || `temple-hall` | 0.0659 | 0.2341 | 0.2394 | 0.0554 | |@@ -93,7 +93,7 @@ Three scenes test what people want to make, not one feature. Each one is built w| Scene | Parts | Triangles drawn | 67js against Cycles | EEVEE against Cycles | Largest visible gap || --- | --- | --- | --- | --- | --- || `megacity-dusk` | 4,300 | 51.6 thousand | 0.1627 | 0.1524 | Window bands are sharper, and facades get no light from the windows |-| `mountain-valley` | 7,698 | 9.72 million | 0.2623 | 0.1879 | The valley floor and the trees are 1.3 to 1.7 times as bright as in Cycles, and shadowed rock is brighter (see "Sky visibility in crowns and valleys") |+| `mountain-valley` | 7,698 | 9.72 million | 0.1792 | 0.1879 | The valley floor and the trees are 1.3 to 1.7 times as bright as in Cycles, and shadowed rock is brighter (see "Sky visibility in crowns and valleys") || `temple-hall` | 155 | 11 thousand | 0.2126 | 0.2341 | The fog shafts are stronger in 67js, and the floor gets no bounce light |With the shadow floor `temple-hall` scored 0.5841, `mountain-valley` 0.3658 and `megacity-dusk` 0.1583.@@ -635,6 +635,21 @@ Method: Cycles was rendered a second time with the world light at 0 (sun only) f- Terrain sky visibility did read a little high. A brute-force march (48 azimuths, 8 m first step, 8 percent growth) gives 0.68 to 0.69 on the floor and `terrain-sky.js` gave 0.73, because the 1.3 step growth skips narrow peaks. A step growth of 1.1 gives 0.694 at about 0.03 s more build time. It moved the floor from 0.1117 to 0.1073 (nearer Cycles), but the slopes from 0.0258 to 0.0232 (further from Cycles 0.0394), and `mountain-valley` went from 0.2623 to 0.2743 against Cycles. Reverted. The slopes are already darker than Cycles, which has bounced light off the lit walls.- The sky radiance by elevation of the capped HDRI is nearly flat (the share of irradiance above a 40 degree horizon is 0.70 against 0.59 for a uniform sky), so a sky that is brighter near the horizon does not explain the floor.+## The valley floor's sun: back faces in the shadow pass (2026-11-03, part of issue #206)++Probe: the sun shadow depth (2048 x 2048, 24 bit) was copied to a float target and read back, and 259 floor points were projected into it. A march over the terrain's own mesh blocks the sun at all 259; the map shadows 123 of them. For the others the texel holds the floor's own depth where the march's first hit, 1,000 to 1,500 m toward the sun and 300 m up, has a depth of 0.36 against the floor's 0.58. Nothing is drawn there. The cause is the cull state of the shadow pass: it culled back faces. A ray that enters the terrain sideways (the mesh has no wall on its outer edge, and a slope that climbs toward the sun faster than the sun's 13 degrees faces away from the light) meets only back faces, writes no depth, and the sun passes through the rock. In the readback the floor shows as an unoccluded patch under the rim. The ten single changes of #221 could not close it because each left the culling alone; a finest-level terrain (`lodDistance` large) also left the floor at 0.1727.++The shadow depth pass now draws both faces (`applyAlphaState` in `src/renderer.js`, `textured` false). A closed mesh gives the same map, because its front face is the nearest; only an open surface changes. Measured, GPU, 960 x 540:++| | before | after | Cycles |+| --- | --- | --- | --- |+| `mountain-valley` floor, world light 0, display, rows 400 to 540 | 0.1727 | 0.0000 | 0.0003 (linear) |+| `mountain-valley` against Cycles, FLIP | 0.2612 | 0.1792 | |+| every other scene, FLIP against Cycles and EEVEE | | unchanged to four digits | |+| shadow map redraw, median of 3 runs of 40 (RTX 3060) | `mountain-valley` 1.75 to 1.81 ms, `foliage` 0.25 to 0.27, `long-street` 0.15 to 0.18 | 1.81 to 1.88, 0.28 to 0.31, 0.19 | |++The map is redrawn only when a caster, the wind or the assembly changes, so a still scene pays nothing per frame. The floor is now lit by the sky alone, so a tree shadow cannot fall on it: the sun does not reach it, as in Cycles. The `foliage` crown leak is not this cause: its FLIP and picture did not move. The crown's sun-shadow rows (9,252 pixels sun-lit in 67js and shadowed in Cycles) stay open under #206; the depth bias is the remaining lead (a tenth of it removed 2,941 pixels).+## Fog valley against EEVEE (2026-11-03, part of issue #165)`fog-valley` scored 0.1967 against EEVEE's 0.1952. Measured by band of rows on the GPU render (FLIP against Cycles): 67js is better than EEVEE in the sky (0.1311 against 0.1608, and 0.0512 against 0.0815 within 40 pixels of the sun, so the glow is not the problem), and worse below the horizon (0.2437 against 0.2317 at rows 230 to 300, 0.2664 against 0.2576 at rows 300 to 400) and in the left third of the frame (0.2866 against 0.2556), where the shadowed face of the near tower and the ground shadow are darker than in Cycles. The fog's in-scatter inside the sun's shadow is the term that lifts them, and it keeps 15 percent of the ambient there, a constant fitted to closed rooms, where the air in shadow sees walls and not sky.diff --git a/bench/reference/baseline.json b/bench/reference/baseline.jsonindex 5e9cb9eb..19d87182 100644--- a/bench/reference/baseline.json+++ b/bench/reference/baseline.json@@ -257,12 +257,12 @@"gpu-vs-cpu": 0.0062},"mountain-valley": {- "gpu-vs-cycles": 0.2629,- "gpu-vs-eevee": 0.2981,- "cpu-vs-cycles": 0.2607,- "cpu-vs-eevee": 0.2959,+ "gpu-vs-cycles": 0.1792,+ "gpu-vs-eevee": 0.2692,+ "cpu-vs-cycles": 0.1765,+ "cpu-vs-eevee": 0.2676,"eevee-vs-cycles": 0.1879,- "gpu-vs-cpu": 0.0188+ "gpu-vs-cpu": 0.0124},"outdoor-hdri": {"gpu-vs-cycles": 0.0462,diff --git a/bench/reference/images/mountain-valley.jpg b/bench/reference/images/mountain-valley.jpgindex 8acedfd9..ac3afed1 100644Binary files a/bench/reference/images/mountain-valley.jpg and b/bench/reference/images/mountain-valley.jpg differdiff --git a/changes/206.md b/changes/206.mdnew file mode 100644index 00000000..5ed43088--- /dev/null+++ b/changes/206.md@@ -0,0 +1 @@+- Fix the sun reaching surfaces that sit behind an open single-sided surface (part of issue #206). The sun shadow pass culled back faces, so a ray that entered a single-sided mesh from behind, such as the open edge of the terrain, wrote no depth and the sun passed through the rock. The shadow depth pass now draws both faces (`applyAlphaState` in `src/renderer.js`). `mountain-valley` with the world light at 0 reads 0.0000 on the floor (0.1727 before, Cycles 0.0003) and scores 0.1792 against Cycles instead of 0.2612; every other reference scene is unchanged to four digits and a shadow map redraw costs 0.05 to 0.1 ms more. The `foliage` crown leak is not this cause and stays open. See `bench/reference/README.md`.diff --git a/src/renderer.js b/src/renderer.jsindex 2fa498cd..49d68b12 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -1810,7 +1810,9 @@ export class Renderer67 {/** Culling, cutout and opacity state for one batch, in the color pass (textured) or the shadow depth pass. */applyAlphaState(batch, textured) {const context = this.context;- if (batch.doubleSided) {+ // The shadow depth pass (not textured) draws both faces. A single-sided terrain edge, leaf card or open shell seen from+ // behind would otherwise write no depth, and the sun would pass through it.+ if (batch.doubleSided || !textured) {context.disable(context.CULL_FACE);} else {context.enable(context.CULL_FACE);diff --git a/tests/renderer.test.js b/tests/renderer.test.jsindex f6c944c5..5b490a52 100644--- a/tests/renderer.test.js+++ b/tests/renderer.test.js@@ -503,3 +503,22 @@ test('color, benchmark and shadow passes bind the same skin textures and disableassert.ok(uniforms.some(([name, value]) => name === 'shadow enabled' && value === 1));assert.deepEqual(uniforms.at(-1), ['shadow enabled', 0]);});++test('the shadow depth pass draws both faces of a single-sided batch, so the sun cannot pass through an open surface seen from behind', () => {+ const renderer = Object.create(Renderer67.prototype);+ const culling = [];+ renderer.depthAlphaUniforms = {};+ renderer.uniforms = {};+ renderer.context = {+ CULL_FACE: 2884,+ enable(capability) { culling.push(['enable', capability]); },+ disable(capability) { culling.push(['disable', capability]); },+ uniform1f() {}, activeTexture() {}, bindTexture() {},+ };+ const singleSided = { doubleSided: false, alphaMode: 'opaque', alphaCutoff: 0, opacity: 1, mapState: [null], textures: [] };+ renderer.applyAlphaState(singleSided, false);+ assert.deepEqual(culling, [['disable', 2884]]);+ culling.length = 0;+ renderer.applyAlphaState(singleSided, true);+ assert.equal(culling[0][0], 'enable', 'the colour pass still culls back faces of a single-sided batch');+});