3D engine that AI agents drive
Size the sun shadow bias in world units (part of #225)
Nguyen Xuan An325cc90af295
diff --git a/bench/reference/README.md b/bench/reference/README.mdindex d798d0fb..8ef725f3 100644--- a/bench/reference/README.md+++ b/bench/reference/README.md@@ -787,3 +787,22 @@ Measured with `npm run reference -- --skip-blender`, before and after on the sam| `outdoor-hdri` | 0.0462 | 0.0460 |`foliage` improves by 0.0034 but still trails EEVEE. The two brick wall scenes get worse (0.0074 and 0.0035), and this was isolated. The wedge of floor the wall shades beside it now has the shape Cycles draws; the loss comes from a faint stipple the shadow map puts on the wall itself. With the sun at (-3, 6, 4) the wall faces the light at N.L 0.51, the 9 tap filter spans about 3.5 texels (about 46 mm of depth) and the bias is about 39 mm. `brick-flat` against Cycles: shadow off 0.1277, old eye 0.1272, aligned eye 0.1346, aligned eye with a constant bias of 0.004 0.1260. So the old eye was hiding a depth bias that ignores slope and the map depth range (issue #225). A slope scaled bias (0.0025 x min(tan, 4)) fixed both brick scenes and improved long-street, fog and megacity-dusk, but moved `temple-hall` from 0.0646 to 0.0752 and `foliage` from 0.0989 to 0.1007, because the bias is in units of far minus near, so it was not kept. `fog` is 0.0016 worse, inside the limit.++## The sun shadow bias in world units (2026-11-03, issue #225)++`shadowVisibility` used `max(0.0006, 0.002 * (1 - N.L))` in the shadow map's 0 to 1 depth, so it ignored the slope against the sun and grew with `far - near`. It is now `shadowBiasPerSlope * clamp(tan, 0.25, 4)`, where tan is the angle between normal and sun and `shadowBiasPerSlope` (`src/shadow-light.js`, a uniform set from the light that drew the map) is `2 * extent / mapSize * 3.5 / (far - near)`: the world distance the depth changes across the 9 tap filter (taps 2.5 texels apart plus the bilinear blend, 3.5 texels) on a 45 degree slope. The same scene gets the same bias in metres for any depth range; a texel of the 2048 map is 7.8 mm in a 16 m wide map. The cascaded shadows already used a texel size over a depth range in metres, and the local shadow atlas pushes the receiver off the surface in angle units, so both stay. There is no CPU shadow bias.++Measured with `npm run reference -- --skip-blender`, same Cycles and EEVEE arrays, GPU against Cycles (EEVEE in brackets), against the state after the sun-aligned shadow. Scenes not listed move by less than 0.002.++| Scene | aligned shadow | world units bias |+| --- | --- | --- |+| `brick-flat` | 0.1346 (EEVEE 0.0581) | 0.1267 (0.0467) |+| `brick-graph` | 0.1637 (EEVEE 0.0910) | 0.1585 (0.0868) |+| `long-street` | 0.1267 | 0.1152 |+| `dark-shaft` | 0.0619 | 0.0559 |+| `fog` | 0.2256 (EEVEE 0.1016) | 0.2208 (0.1090) |+| `megacity-dusk` | 0.1627 | 0.1594 |+| `temple-hall` | 0.0659 | 0.0640 |+| `foliage` | 0.0989 (EEVEE 0.0811) | 0.0990 (0.0809) |++Both brick scenes are back under their scores before the aligned eye (0.1272 and 0.1602). The slope scaled bias in depth-range units that was tried first made `temple-hall` and `foliage` worse; in world units neither moves. `foliage` against EEVEE was 0.0888 with the old off-sun eye, 0.0811 after the aligned eye and is 0.0809 now, so the bias leaves the crown gap of issue #191 as it was. `fog` against EEVEE is 0.0074 worse while it is 0.0048 better against Cycles; the gate is Cycles.diff --git a/bench/reference/baseline.json b/bench/reference/baseline.jsonindex 5e1fd475..f213949b 100644--- a/bench/reference/baseline.json+++ b/bench/reference/baseline.json@@ -3,92 +3,96 @@"blenderVersion": "5.2.2 LTS","seconds": {"brick-flat": {- "cycles": 10.8,- "eevee": 1.6+ "cycles": 3.9,+ "eevee": 0.9},"brick-graph": {- "cycles": 8.9,- "eevee": 0.4+ "cycles": 4.3,+ "eevee": 0.3},"brick-room": {- "cycles": 19.6,- "eevee": 0.6+ "cycles": 8.6,+ "eevee": 0.3},"brushed-film": {- "cycles": 9.9,- "eevee": 0.7+ "cycles": 4.3,+ "eevee": 0.3},"car-paint": {- "cycles": 14.3,- "eevee": 0.5+ "cycles": 4.2,+ "eevee": 0.3},"character": {- "cycles": 7.7,- "eevee": 0.4+ "cycles": 4.1,+ "eevee": 0.3},"dark-shaft": {- "cycles": 129.3,- "eevee": 67.2- },- "fog": {- "cycles": 58.3,- "eevee": 1.4+ "cycles": 13.7,+ "eevee": 0.8},"fog-room": {- "cycles": 29.4,- "eevee": 2.7+ "cycles": 14.5,+ "eevee": 0.9},- "foliage": {- "cycles": 4.4,+ "fog-valley": {+ "cycles": 12.4,+ "eevee": 0.8+ },+ "fog": {+ "cycles": 14.5,"eevee": 0.9},+ "foliage": {+ "cycles": 5.0,+ "eevee": 0.4+ },"glass": {- "cycles": 18.9,- "eevee": 0.5+ "cycles": 4.9,+ "eevee": 0.3},"light-linking": {- "cycles": 6.9,- "eevee": 0.6+ "cycles": 2.9,+ "eevee": 0.3},"lit-interior": {- "cycles": 33.5,- "eevee": 0.6+ "cycles": 8.5,+ "eevee": 0.4},"long-street": {- "cycles": 10.1,- "eevee": 0.4+ "cycles": 3.7,+ "eevee": 0.3},"material-spheres": {- "cycles": 21.5,- "eevee": 0.4+ "cycles": 5.1,+ "eevee": 0.3},"megacity-dusk": {- "cycles": 22.1,- "eevee": 1.5- },- "metal": {- "cycles": 21,- "eevee": 1.8+ "cycles": 9.3,+ "eevee": 0.7},"metal-room": {- "cycles": 164.9,- "eevee": 41.9+ "cycles": 9.0,+ "eevee": 0.3+ },+ "metal": {+ "cycles": 4.4,+ "eevee": 0.3},"mountain-valley": {- "cycles": 13,- "eevee": 28.3+ "cycles": 15.5,+ "eevee": 11.3},"outdoor-hdri": {- "cycles": 12.3,- "eevee": 0.5+ "cycles": 3.7,+ "eevee": 0.3},"spot-area": {- "cycles": 8.7,- "eevee": 0.6+ "cycles": 3.4,+ "eevee": 0.3},"temple-hall": {- "cycles": 57.7,- "eevee": 1.1+ "cycles": 16.0,+ "eevee": 0.8}},"graphics": {@@ -97,20 +101,20 @@},"scores": {"brick-flat": {- "gpu-vs-cycles": 0.1346,- "gpu-vs-eevee": 0.0581,- "cpu-vs-cycles": 0.1344,- "cpu-vs-eevee": 0.0579,- "eevee-vs-cycles": 0.1098,- "gpu-vs-cpu": 0.0067+ "gpu-vs-cycles": 0.1267,+ "gpu-vs-eevee": 0.0467,+ "cpu-vs-cycles": 0.1264,+ "cpu-vs-eevee": 0.0463,+ "eevee-vs-cycles": 0.1099,+ "gpu-vs-cpu": 0.0066},"brick-graph": {- "gpu-vs-cycles": 0.1637,- "gpu-vs-eevee": 0.091,- "cpu-vs-cycles": 0.1637,- "cpu-vs-eevee": 0.0912,+ "gpu-vs-cycles": 0.1585,+ "gpu-vs-eevee": 0.0868,+ "cpu-vs-cycles": 0.1585,+ "cpu-vs-eevee": 0.0869,"eevee-vs-cycles": 0.1221,- "gpu-vs-cpu": 0.0079+ "gpu-vs-cpu": 0.0078},"brick-room": {"gpu-vs-cycles": 0.5102,@@ -124,80 +128,80 @@"gpu-vs-cycles": 0.1029,"gpu-vs-eevee": 0.0855,"cpu-vs-cycles": 0.1026,- "cpu-vs-eevee": 0.0854,- "eevee-vs-cycles": 0.0984,+ "cpu-vs-eevee": 0.0855,+ "eevee-vs-cycles": 0.0985,"gpu-vs-cpu": 0.0062},"car-paint": {- "gpu-vs-cycles": 0.1038,- "gpu-vs-eevee": 0.065,- "cpu-vs-cycles": 0.1035,- "cpu-vs-eevee": 0.065,+ "gpu-vs-cycles": 0.1037,+ "gpu-vs-eevee": 0.0649,+ "cpu-vs-cycles": 0.1034,+ "cpu-vs-eevee": 0.0649,"eevee-vs-cycles": 0.0844,- "gpu-vs-cpu": 0.0059+ "gpu-vs-cpu": 0.0058},"character": {"gpu-vs-cycles": 0.0717,- "gpu-vs-eevee": 0.0443,+ "gpu-vs-eevee": 0.0444,"cpu-vs-cycles": 0.0714,- "cpu-vs-eevee": 0.0442,+ "cpu-vs-eevee": 0.0443,"eevee-vs-cycles": 0.0622,"gpu-vs-cpu": 0.0065},"dark-shaft": {- "gpu-vs-cycles": 0.0619,- "gpu-vs-eevee": 0.2808,- "cpu-vs-cycles": 0.0618,- "cpu-vs-eevee": 0.2807,- "eevee-vs-cycles": 0.2653,- "gpu-vs-cpu": 0.0024,- "region-pixels": 0.099,- "region-gpu-vs-cycles": 0.1858,- "region-gpu-vs-eevee": 0.2997,- "region-eevee-vs-cycles": 0.2643+ "gpu-vs-cycles": 0.0559,+ "gpu-vs-eevee": 0.2639,+ "cpu-vs-cycles": 0.0559,+ "cpu-vs-eevee": 0.2637,+ "eevee-vs-cycles": 0.2654,+ "gpu-vs-cpu": 0.0031,+ "region-pixels": 0.0989,+ "region-gpu-vs-cycles": 0.1285,+ "region-gpu-vs-eevee": 0.2563,+ "region-eevee-vs-cycles": 0.2645},"fog": {- "gpu-vs-cycles": 0.2256,- "gpu-vs-eevee": 0.1016,- "cpu-vs-cycles": 0.2253,- "cpu-vs-eevee": 0.102,+ "gpu-vs-cycles": 0.2208,+ "gpu-vs-eevee": 0.109,+ "cpu-vs-cycles": 0.2205,+ "cpu-vs-eevee": 0.1095,"eevee-vs-cycles": 0.2662,- "gpu-vs-cpu": 0.0085+ "gpu-vs-cpu": 0.0087},"fog-room": {- "gpu-vs-cycles": 0.1049,- "gpu-vs-eevee": 0.3917,- "cpu-vs-cycles": 0.1047,- "cpu-vs-eevee": 0.3915,+ "gpu-vs-cycles": 0.1036,+ "gpu-vs-eevee": 0.3636,+ "cpu-vs-cycles": 0.1036,+ "cpu-vs-eevee": 0.3631,"eevee-vs-cycles": 0.373,- "gpu-vs-cpu": 0.0043,- "region-pixels": 0.243,- "region-gpu-vs-cycles": 0.2405,- "region-gpu-vs-eevee": 0.3662,- "region-eevee-vs-cycles": 0.3146+ "gpu-vs-cpu": 0.0066,+ "region-pixels": 0.2428,+ "region-gpu-vs-cycles": 0.1615,+ "region-gpu-vs-eevee": 0.3273,+ "region-eevee-vs-cycles": 0.3147},"fog-valley": {- "gpu-vs-cycles": 0.1855,+ "gpu-vs-cycles": 0.1857,"gpu-vs-eevee": 0.1443,- "cpu-vs-cycles": 0.1857,+ "cpu-vs-cycles": 0.1859,"cpu-vs-eevee": 0.1447,"eevee-vs-cycles": 0.1952,- "gpu-vs-cpu": 0.012+ "gpu-vs-cpu": 0.0119},"foliage": {- "gpu-vs-cycles": 0.0989,- "gpu-vs-eevee": 0.0811,+ "gpu-vs-cycles": 0.099,+ "gpu-vs-eevee": 0.0809,"cpu-vs-cycles": 0.0986,- "cpu-vs-eevee": 0.0808,+ "cpu-vs-eevee": 0.0806,"eevee-vs-cycles": 0.0888,- "gpu-vs-cpu": 0.0098+ "gpu-vs-cpu": 0.0097},"glass": {- "gpu-vs-cycles": 0.1034,- "gpu-vs-eevee": 0.0996,- "cpu-vs-cycles": 0.1034,- "cpu-vs-eevee": 0.1,- "eevee-vs-cycles": 0.1009,+ "gpu-vs-cycles": 0.1035,+ "gpu-vs-eevee": 0.0998,+ "cpu-vs-cycles": 0.1035,+ "cpu-vs-eevee": 0.1001,+ "eevee-vs-cycles": 0.1008,"gpu-vs-cpu": 0.0083},"light-linking": {@@ -217,34 +221,34 @@"gpu-vs-cpu": 0.0062},"long-street": {- "gpu-vs-cycles": 0.1267,- "gpu-vs-eevee": 0.1112,- "cpu-vs-cycles": 0.1267,- "cpu-vs-eevee": 0.1113,+ "gpu-vs-cycles": 0.1152,+ "gpu-vs-eevee": 0.0907,+ "cpu-vs-cycles": 0.1153,+ "cpu-vs-eevee": 0.091,"eevee-vs-cycles": 0.0976,- "gpu-vs-cpu": 0.0094+ "gpu-vs-cpu": 0.007},"material-spheres": {- "gpu-vs-cycles": 0.1293,- "gpu-vs-eevee": 0.0756,- "cpu-vs-cycles": 0.1291,- "cpu-vs-eevee": 0.0753,+ "gpu-vs-cycles": 0.1286,+ "gpu-vs-eevee": 0.0749,+ "cpu-vs-cycles": 0.1284,+ "cpu-vs-eevee": 0.0747,"eevee-vs-cycles": 0.1053,- "gpu-vs-cpu": 0.007+ "gpu-vs-cpu": 0.0069},"megacity-dusk": {- "gpu-vs-cycles": 0.1627,- "gpu-vs-eevee": 0.1022,- "cpu-vs-cycles": 0.162,- "cpu-vs-eevee": 0.1061,+ "gpu-vs-cycles": 0.1594,+ "gpu-vs-eevee": 0.1025,+ "cpu-vs-cycles": 0.1587,+ "cpu-vs-eevee": 0.1063,"eevee-vs-cycles": 0.1524,- "gpu-vs-cpu": 0.0281+ "gpu-vs-cpu": 0.0276},"metal": {- "gpu-vs-cycles": 0.1036,- "gpu-vs-eevee": 0.0782,+ "gpu-vs-cycles": 0.1037,+ "gpu-vs-eevee": 0.0781,"cpu-vs-cycles": 0.1033,- "cpu-vs-eevee": 0.0783,+ "cpu-vs-eevee": 0.0782,"eevee-vs-cycles": 0.0916,"gpu-vs-cpu": 0.0064},@@ -257,18 +261,18 @@"gpu-vs-cpu": 0.0062},"mountain-valley": {- "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-cycles": 0.1782,+ "gpu-vs-eevee": 0.2683,+ "cpu-vs-cycles": 0.1755,+ "cpu-vs-eevee": 0.2667,+ "eevee-vs-cycles": 0.1893,"gpu-vs-cpu": 0.0124},"outdoor-hdri": {- "gpu-vs-cycles": 0.046,- "gpu-vs-eevee": 0.0293,- "cpu-vs-cycles": 0.0454,- "cpu-vs-eevee": 0.029,+ "gpu-vs-cycles": 0.0451,+ "gpu-vs-eevee": 0.0281,+ "cpu-vs-cycles": 0.0445,+ "cpu-vs-eevee": 0.0279,"eevee-vs-cycles": 0.0403,"gpu-vs-cpu": 0.0065},@@ -281,16 +285,16 @@"gpu-vs-cpu": 0.0032},"temple-hall": {- "gpu-vs-cycles": 0.0659,- "gpu-vs-eevee": 0.2394,- "cpu-vs-cycles": 0.0659,- "cpu-vs-eevee": 0.2393,+ "gpu-vs-cycles": 0.064,+ "gpu-vs-eevee": 0.232,+ "cpu-vs-cycles": 0.064,+ "cpu-vs-eevee": 0.2318,"eevee-vs-cycles": 0.2341,- "gpu-vs-cpu": 0.0016,- "region-pixels": 0.0958,- "region-gpu-vs-cycles": 0.2805,- "region-gpu-vs-eevee": 0.3061,- "region-eevee-vs-cycles": 0.2455+ "gpu-vs-cpu": 0.0022,+ "region-pixels": 0.0961,+ "region-gpu-vs-cycles": 0.2362,+ "region-gpu-vs-eevee": 0.2844,+ "region-eevee-vs-cycles": 0.2453}}}diff --git a/bench/reference/images/brick-flat.jpg b/bench/reference/images/brick-flat.jpgindex aea57a90..3d0c75a2 100644Binary files a/bench/reference/images/brick-flat.jpg and b/bench/reference/images/brick-flat.jpg differdiff --git a/bench/reference/images/brick-graph.jpg b/bench/reference/images/brick-graph.jpgindex e747f98f..c76eebbf 100644Binary files a/bench/reference/images/brick-graph.jpg and b/bench/reference/images/brick-graph.jpg differdiff --git a/bench/reference/images/brick-room.jpg b/bench/reference/images/brick-room.jpgindex 46a0e848..b248c60b 100644Binary files a/bench/reference/images/brick-room.jpg and b/bench/reference/images/brick-room.jpg differdiff --git a/bench/reference/images/brushed-film.jpg b/bench/reference/images/brushed-film.jpgindex 3dbdab84..f9e71bb7 100644Binary files a/bench/reference/images/brushed-film.jpg and b/bench/reference/images/brushed-film.jpg differdiff --git a/bench/reference/images/car-paint.jpg b/bench/reference/images/car-paint.jpgindex 45f76e61..4634472c 100644Binary files a/bench/reference/images/car-paint.jpg and b/bench/reference/images/car-paint.jpg differdiff --git a/bench/reference/images/character.jpg b/bench/reference/images/character.jpgindex 83e37b0a..71459c86 100644Binary files a/bench/reference/images/character.jpg and b/bench/reference/images/character.jpg differdiff --git a/bench/reference/images/dark-shaft.jpg b/bench/reference/images/dark-shaft.jpgindex 50e597ff..0722147e 100644Binary files a/bench/reference/images/dark-shaft.jpg and b/bench/reference/images/dark-shaft.jpg differdiff --git a/bench/reference/images/fog-room.jpg b/bench/reference/images/fog-room.jpgindex aa00ff2b..32fe6af5 100644Binary files a/bench/reference/images/fog-room.jpg and b/bench/reference/images/fog-room.jpg differdiff --git a/bench/reference/images/fog-valley.jpg b/bench/reference/images/fog-valley.jpgindex 7bf7e5f1..78ed7f84 100644Binary files a/bench/reference/images/fog-valley.jpg and b/bench/reference/images/fog-valley.jpg differdiff --git a/bench/reference/images/fog.jpg b/bench/reference/images/fog.jpgindex 1e33a63a..b7debfb8 100644Binary files a/bench/reference/images/fog.jpg and b/bench/reference/images/fog.jpg differdiff --git a/bench/reference/images/foliage.jpg b/bench/reference/images/foliage.jpgindex c72599ed..237744b5 100644Binary files a/bench/reference/images/foliage.jpg and b/bench/reference/images/foliage.jpg differdiff --git a/bench/reference/images/glass.jpg b/bench/reference/images/glass.jpgindex 8b695191..e8b09263 100644Binary files a/bench/reference/images/glass.jpg and b/bench/reference/images/glass.jpg differdiff --git a/bench/reference/images/light-linking.jpg b/bench/reference/images/light-linking.jpgindex caa72283..8e6a7d86 100644Binary files a/bench/reference/images/light-linking.jpg and b/bench/reference/images/light-linking.jpg differdiff --git a/bench/reference/images/lit-interior.jpg b/bench/reference/images/lit-interior.jpgindex 8b82b680..7bba6028 100644Binary files a/bench/reference/images/lit-interior.jpg and b/bench/reference/images/lit-interior.jpg differdiff --git a/bench/reference/images/long-street.jpg b/bench/reference/images/long-street.jpgindex d8365d0f..4a397fe9 100644Binary files a/bench/reference/images/long-street.jpg and b/bench/reference/images/long-street.jpg differdiff --git a/bench/reference/images/material-spheres.jpg b/bench/reference/images/material-spheres.jpgindex efc2f507..d04ac5af 100644Binary files a/bench/reference/images/material-spheres.jpg and b/bench/reference/images/material-spheres.jpg differdiff --git a/bench/reference/images/megacity-dusk.jpg b/bench/reference/images/megacity-dusk.jpgindex 649ee1f8..32d7c336 100644Binary files a/bench/reference/images/megacity-dusk.jpg and b/bench/reference/images/megacity-dusk.jpg differdiff --git a/bench/reference/images/metal-room.jpg b/bench/reference/images/metal-room.jpgindex 7a659b32..8eed5114 100644Binary files a/bench/reference/images/metal-room.jpg and b/bench/reference/images/metal-room.jpg differdiff --git a/bench/reference/images/metal.jpg b/bench/reference/images/metal.jpgindex d75f90cf..17d74ec0 100644Binary files a/bench/reference/images/metal.jpg and b/bench/reference/images/metal.jpg differdiff --git a/bench/reference/images/mountain-valley.jpg b/bench/reference/images/mountain-valley.jpgindex ac3afed1..c699c48a 100644Binary files a/bench/reference/images/mountain-valley.jpg and b/bench/reference/images/mountain-valley.jpg differdiff --git a/bench/reference/images/outdoor-hdri.jpg b/bench/reference/images/outdoor-hdri.jpgindex 6fd64555..cddd474b 100644Binary files a/bench/reference/images/outdoor-hdri.jpg and b/bench/reference/images/outdoor-hdri.jpg differdiff --git a/bench/reference/images/spot-area.jpg b/bench/reference/images/spot-area.jpgindex 3e491803..b57442bd 100644Binary files a/bench/reference/images/spot-area.jpg and b/bench/reference/images/spot-area.jpg differdiff --git a/bench/reference/images/temple-hall.jpg b/bench/reference/images/temple-hall.jpgindex 7ddeee85..e949650c 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 2fa9e1f9..86fb0e17 100644--- a/bench/size/budgets.json+++ b/bench/size/budgets.json@@ -44,10 +44,10 @@"entry:material-layer-shaders": 20700,"entry:light-linking": 19000,"entry:cascaded-shadows": 27700,- "scene:lit-box": 62900,+ "scene:lit-box": 63000,"scene:gltf-box": 84200,"scene:gltf-box+lazy": 87600,- "scene:agent-scene": 104700,- "scene:agent-scene+lazy": 286600+ "scene:agent-scene": 104800,+ "scene:agent-scene+lazy": 286700}}diff --git a/changes/225.md b/changes/225.mdnew file mode 100644index 00000000..96dc01fb--- /dev/null+++ b/changes/225.md@@ -0,0 +1 @@+- The sun shadow's depth bias is now a world distance, slope times 3.5 shadow texels converted with the light's extent and far minus near, so the same scene gets the same bias in metres for any depth range; it removes the self-shadow stipple the sun-aligned map put on sloped walls (part of issue #225).diff --git a/src/renderer.js b/src/renderer.jsindex 0de903fe..3a1a07d2 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -14,7 +14,7 @@ import { validateSkinGeometry, validateSkinPose, skinTextureLayout, packSkinInflimport { validateSplatMaterial, splatBatchState, splatTextureBytes, resolveSplatSize, packSplatParameters, buildSplatPixels } from './splat.js';import { materialLayerState, materialLayerValue } from './material-layers.js';import { InstanceSelection, extractFrustumPlanes, geometryBoundingSphere, selectInstances } from './render-culling.js';-import { resolveShadowLight } from './shadow-light.js';+import { resolveShadowLight, shadowBiasPerSlope } from './shadow-light.js';import { validateMorphGeometry, validateMorphPose, validateMorphTransform, packMorphTargets } from './morph.js';const SHADOW_MAP_SIZE = 2048;@@ -537,6 +537,7 @@ export class Renderer67 {this.projection = mat4.create();this.viewProjection = mat4.create();this.lightProjection = mat4.create();+ this.shadowBiasPerSlope = 0;this.inverseViewProjection = mat4.create();this.shaderFeatures = shaderFeaturesOfParts(groups.flat());this.shaderFeatures.add(environment.map ? 'environmentMap' : 'studioLighting');@@ -611,7 +612,7 @@ export class Renderer67 {finishMainProgram(pendingProgram) {this.program = finishProgram(this.context, pendingProgram);- this.uniforms = Object.fromEntries(['materialDielectric', 'materialCoat', 'materialSheen', 'materialAnisotropy', 'materialIridescence', 'lightLinkMask', 'boxMapScale', 'boxMapAxis', 'lightMap', 'lightMapScale', 'hasLightMap', 'viewProjection', 'lightProjection', 'eye', 'armorColor', 'armorSurface', 'exposure', 'inspection', 'shadowMap', ...MAP_UNIFORMS, ...MAP_FLAGS, ...SKIN_UNIFORMS, ...MORPH_UNIFORMS, 'normalScale', 'occlusionStrength', 'materialMoss', ...SPLAT_UNIFORMS, 'hdrEnabled', 'time', 'wind', 'worldMode', 'daylight', 'fogColor', 'lightDirection', 'fogDensity', 'waterRipple', 'rippleTime', 'explosionProgress', 'explosionCenter', 'assemblyAmount', 'renderPassMode', 'renderPassDepthRange', 'renderPassObjectBase', ...ALPHA_UNIFORMS].map((name) => [name, this.context.getUniformLocation(this.program, name)]));+ this.uniforms = Object.fromEntries(['materialDielectric', 'materialCoat', 'materialSheen', 'materialAnisotropy', 'materialIridescence', 'lightLinkMask', 'boxMapScale', 'boxMapAxis', 'lightMap', 'lightMapScale', 'hasLightMap', 'viewProjection', 'lightProjection', 'eye', 'armorColor', 'armorSurface', 'exposure', 'inspection', 'shadowMap', 'shadowBiasPerSlope', ...MAP_UNIFORMS, ...MAP_FLAGS, ...SKIN_UNIFORMS, ...MORPH_UNIFORMS, 'normalScale', 'occlusionStrength', 'materialMoss', ...SPLAT_UNIFORMS, 'hdrEnabled', 'time', 'wind', 'worldMode', 'daylight', 'fogColor', 'lightDirection', 'fogDensity', 'waterRipple', 'rippleTime', 'explosionProgress', 'explosionCenter', 'assemblyAmount', 'renderPassMode', 'renderPassDepthRange', 'renderPassObjectBase', ...ALPHA_UNIFORMS].map((name) => [name, this.context.getUniformLocation(this.program, name)]));for (const name of LIGHT_UNIFORMS) {this.uniforms[name] = this.context.getUniformLocation(this.program, name);}@@ -1238,6 +1239,7 @@ export class Renderer67 {context.enable(context.DEPTH_TEST);context.enable(context.CULL_FACE);context.clear(context.DEPTH_BUFFER_BIT);+ this.shadowBiasPerSlope = shadowBiasPerSlope(light, SHADOW_MAP_SIZE);this.drawShadowCasters(this.lightProjection, SHADOW_MAP_SIZE);context.bindFramebuffer(context.FRAMEBUFFER, null);this.shadowsDirty = false;@@ -2035,6 +2037,7 @@ export class Renderer67 {context.useProgram(this.program);context.uniformMatrix4fv(this.uniforms.viewProjection, false, this.viewProjection);context.uniformMatrix4fv(this.uniforms.lightProjection, false, this.lightProjection);+ context.uniform1f(this.uniforms.shadowBiasPerSlope, this.shadowBiasPerSlope);context.uniform3fv(this.uniforms.eye, eye);this.shadowCascades?.bindUniforms(this);context.uniform3fv(this.uniforms.armorColor, armorColor);diff --git a/src/shaders.js b/src/shaders.jsindex e7f3a8d6..00ce777d 100644--- a/src/shaders.js+++ b/src/shaders.js@@ -410,6 +410,7 @@ uniform vec3 waterRipple;uniform float rippleTime;uniform float explosionProgress;uniform sampler2D shadowMap;+uniform float shadowBiasPerSlope;uniform sampler2D baseMap;uniform float hasBaseMap;uniform sampler2D normalMap;@@ -483,7 +484,8 @@ float filteredShadow(vec2 coordinates, float receiverDepth, ivec2 dimensions) {float shadowVisibility(vec3 normal) {vec3 projected = shadowPosition.xyz / shadowPosition.w * 0.5 + 0.5;if (projected.z > 1.0 || projected.z < 0.0 || any(lessThan(projected.xy, vec2(0.0))) || any(greaterThan(projected.xy, vec2(1.0)))) return 1.0;- float bias = max(0.0006, 0.002 * (1.0 - dot(normal, normalize(lightDirection))));+ float cosine = max(dot(normal, normalize(lightDirection)), 0.05);+ float bias = shadowBiasPerSlope * clamp(sqrt(1.0 - cosine * cosine) / cosine, 0.25, 4.0);float visibility = 0.0;ivec2 dimensions = textureSize(shadowMap, 0);vec2 texel = 1.0 / vec2(dimensions);diff --git a/src/shadow-light.js b/src/shadow-light.jsindex 8cc589cb..ab4bdab1 100644--- a/src/shadow-light.js+++ b/src/shadow-light.js@@ -17,3 +17,17 @@ export function resolveShadowLight(world, lightDirection, shadowBounds = {}) {const eye = target.map((value, axis) => value + (direction[axis] / length) * distance);return { ...view, eye, ...shadowBounds, target };}++// How far the 9 tap filter (taps 2.5 texels apart, plus the bilinear blend) reaches from its centre, in shadow texels.+const FILTER_REACH_TEXELS = 3.5;++/**+ * The depth bias the sun shadow needs per unit of slope (tan of the angle between the surface normal and the sun), in+ * the shadow map's 0 to 1 depth. It is the world distance the depth changes across the filter on a 45 degree slope,+ * `extent * 2 / mapSize * FILTER_REACH_TEXELS`, divided by `far - near`, so the same scene gives the same bias in metres+ * for any depth range. `light` is the result of `resolveShadowLight`.+ */+export function shadowBiasPerSlope(light, mapSize) {+ const texelWorldSize = (light.extent * 2) / mapSize;+ return (texelWorldSize * FILTER_REACH_TEXELS) / (light.far - light.near);+}diff --git a/tests/shadow-light.test.js b/tests/shadow-light.test.jsindex 6bf75093..bc3c47f0 100644--- a/tests/shadow-light.test.js+++ b/tests/shadow-light.test.js@@ -1,6 +1,6 @@import assert from 'node:assert/strict';import test from 'node:test';-import { resolveShadowLight } from '../src/shadow-light.js';+import { resolveShadowLight, shadowBiasPerSlope } from '../src/shadow-light.js';function directionBetween(light) {const offset = light.eye.map((value, axis) => value - light.target[axis]);@@ -29,3 +29,23 @@ test('a missing or zero sun falls back to the default direction', () => {assert.deepEqual(resolveShadowLight(true, undefined).eye, resolveShadowLight(true, [-3, 6, 4]).eye);assert.deepEqual(resolveShadowLight(true, [0, 0, 0]).eye, resolveShadowLight(true, [-3, 6, 4]).eye);});++test('the shadow bias is the same distance in metres for two depth ranges', () => {+ const mapSize = 2048;+ const studio = resolveShadowLight(false, [-3, 6, 4], { extent: 8, near: 0.1, far: 40 });+ const world = resolveShadowLight(false, [-3, 6, 4], { extent: 8, near: 1, far: 200 });+ const studioMetres = shadowBiasPerSlope(studio, mapSize) * (studio.far - studio.near);+ const worldMetres = shadowBiasPerSlope(world, mapSize) * (world.far - world.near);+ assert.ok(Math.abs(studioMetres - worldMetres) < 1e-12);+ // 3.5 texels of a 16 m wide map held in 2048 texels.+ assert.ok(Math.abs(studioMetres - (16 / 2048) * 3.5) < 1e-12);+ assert.ok(shadowBiasPerSlope(studio, mapSize) > shadowBiasPerSlope(world, mapSize));+});++test('the shadow bias in metres follows the texel size', () => {+ const mapSize = 2048;+ const narrow = resolveShadowLight(true, [-3, 6, 4], { extent: 4, near: 1, far: 100 });+ const wide = resolveShadowLight(true, [-3, 6, 4], { extent: 8, near: 1, far: 100 });+ const ratio = shadowBiasPerSlope(wide, mapSize) / shadowBiasPerSlope(narrow, mapSize);+ assert.ok(Math.abs(ratio - 2) < 1e-12);+});