3D engine that AI agents drive
Add multiple scattering to the direct light specular (part of #167)
Nguyen Xuan An01d17106057c
diff --git a/bench/reference/README.md b/bench/reference/README.mdindex cd3de95a..d9598371 100644--- a/bench/reference/README.md+++ b/bench/reference/README.md@@ -952,3 +952,21 @@ Both water boxes now stand 1 cm above the ground. Cycles is re-rendered for `meg| `megacity-dusk-b` GPU / CPU | 0.1242 / 0.1258 | 0.1108 / 0.1120 |Mean linear luminance of 67js over Cycles after the change, `megacity-dusk-a`: towers 0.92, street 0.90, river middle 0.82, river sides 0.82. The river is now slightly dark in 67js instead of 2.4 times bright. The scores of `mountain-valley-a` and the default suite are not touched: no engine file changed, and neither scene uses these boxes (inferred; not re-run). The measuring switch `render-gpu.mjs --water off|ssr` is new. Done-when 1 of #232 holds (the cause is measured); the sky visibility grid of its scope is not needed, and the issue's second item is moot.++## Multiple scattering on direct light (2026-11-03, issue #167)++`environmentLighting` added back the energy a rough microfacet loses on its first bounce (PR #236), but a sun or lamp's specular did not. `illuminate` now scales each light's specular by the same gain, `1 / (1 - (1 - scale - offset) * averageFresnel)`, taken from the BRDF table once per fragment (a global `directSpecularGain` set after the normal is final), so a fragment with five lights still samples the table twice (the environment term and the gain), not once per light. It applies only where an environment map is bound, since the table exists only then. A studio or outdoor-sky scene without a map is unchanged.++Scores against Cycles, GPU, `--skip-blender` on the stored Cycles and EEVEE arrays of the #236 worker (main reproduced `baseline.json` to four digits on them first, `mountain-valley` included):++| Scene | Before | After | EEVEE |+| --- | --- | --- | --- |+| `metal` | 0.0974 | 0.0962 | 0.0916 |+| `material-spheres` | 0.1234 | 0.1185 | 0.1053 |+| `character` | 0.0715 | 0.0712 | 0.0622 |+| `brick-flat` | 0.1267 | 0.1264 | 0.1099 |+| `spot-area` | 0.1242 | 0.1244 | 0.1935 |++No scene got worse by more than 0.0002 (`spot-area`, `fog-valley` 0.1855 to 0.1859). Frame cost on the RTX 3060 (`frame-time.mjs`, median of three runs): `brick-flat` 0.375 to 0.39 ms before and 0.375 to 0.38 after, `metal` 0.44 to 0.45 before and 0.44 after, so the extra lookup is below the timer's resolution. SwiftShader `transparency` in `bench/runtime`: 345.9 ms before, 347.5 after (15 frames; that scene has no environment map, so the shader path is the same).++`metal` is still 0.0046 above EEVEE, so the first box of #167 stays open. Where the rest is, by 6 by 8 block of the FLIP map (measured): the largest error is the lower half of the chrome sphere, where 67js is 0.062 brighter in display luminance than Cycles (EEVEE 0.022). Cycles shows the floor there darkened by the sphere's own contact shadow, and the sphere's probe traces the scene without the sphere, so the floor it shows is lit all the way to its base (inferred; the "seeing through the part itself" try above, 0.1117, was the attempt at that and was not kept). The rest of the frame is 67js 0.01 to 0.03 darker than Cycles, and EEVEE is darker still, so there is little left to gain from the direct specular. The floor holds over half of the error (0.0639 of 0.1147 in the table above) and is not a reflector.diff --git a/bench/reference/baseline.json b/bench/reference/baseline.jsonindex b93e49f0..a58bf0ad 100644--- a/bench/reference/baseline.json+++ b/bench/reference/baseline.json@@ -101,52 +101,52 @@},"scores": {"brick-flat": {- "gpu-vs-cycles": 0.1267,- "gpu-vs-eevee": 0.047,- "cpu-vs-cycles": 0.1264,- "cpu-vs-eevee": 0.0466,+ "gpu-vs-cycles": 0.1264,+ "gpu-vs-eevee": 0.0465,+ "cpu-vs-cycles": 0.1261,+ "cpu-vs-eevee": 0.0462,"eevee-vs-cycles": 0.1099,- "gpu-vs-cpu": 0.0066+ "gpu-vs-cpu": 0.0065},"brick-graph": {- "gpu-vs-cycles": 0.1587,- "gpu-vs-eevee": 0.0874,- "cpu-vs-cycles": 0.1586,+ "gpu-vs-cycles": 0.1584,+ "gpu-vs-eevee": 0.0875,+ "cpu-vs-cycles": 0.1584,"cpu-vs-eevee": 0.0876,"eevee-vs-cycles": 0.1221,- "gpu-vs-cpu": 0.0078+ "gpu-vs-cpu": 0.0077},"brick-room": {- "gpu-vs-cycles": 0.5102,+ "gpu-vs-cycles": 0.5096,"gpu-vs-eevee": 0.0711,- "cpu-vs-cycles": 0.5095,+ "cpu-vs-cycles": 0.509,"cpu-vs-eevee": 0.0714,"eevee-vs-cycles": 0.502,"gpu-vs-cpu": 0.0071},"brushed-film": {- "gpu-vs-cycles": 0.1027,- "gpu-vs-eevee": 0.0844,- "cpu-vs-cycles": 0.1026,- "cpu-vs-eevee": 0.0845,+ "gpu-vs-cycles": 0.1026,+ "gpu-vs-eevee": 0.0841,+ "cpu-vs-cycles": 0.1024,+ "cpu-vs-eevee": 0.0842,"eevee-vs-cycles": 0.0985,"gpu-vs-cpu": 0.0061},"car-paint": {- "gpu-vs-cycles": 0.103,- "gpu-vs-eevee": 0.0645,- "cpu-vs-cycles": 0.1028,- "cpu-vs-eevee": 0.0646,+ "gpu-vs-cycles": 0.1028,+ "gpu-vs-eevee": 0.0641,+ "cpu-vs-cycles": 0.1025,+ "cpu-vs-eevee": 0.0642,"eevee-vs-cycles": 0.0844,"gpu-vs-cpu": 0.0058},"character": {- "gpu-vs-cycles": 0.0715,- "gpu-vs-eevee": 0.0445,- "cpu-vs-cycles": 0.0712,- "cpu-vs-eevee": 0.0445,+ "gpu-vs-cycles": 0.0712,+ "gpu-vs-eevee": 0.0438,+ "cpu-vs-cycles": 0.071,+ "cpu-vs-eevee": 0.0437,"eevee-vs-cycles": 0.0622,- "gpu-vs-cpu": 0.0065+ "gpu-vs-cpu": 0.0066},"dark-shaft": {"gpu-vs-cycles": 0.0559,@@ -161,10 +161,10 @@"region-eevee-vs-cycles": 0.2645},"fog": {- "gpu-vs-cycles": 0.2208,- "gpu-vs-eevee": 0.109,- "cpu-vs-cycles": 0.2205,- "cpu-vs-eevee": 0.1095,+ "gpu-vs-cycles": 0.2207,+ "gpu-vs-eevee": 0.1092,+ "cpu-vs-cycles": 0.2204,+ "cpu-vs-eevee": 0.1097,"eevee-vs-cycles": 0.2662,"gpu-vs-cpu": 0.0086},@@ -172,91 +172,91 @@"gpu-vs-cycles": 0.1036,"gpu-vs-eevee": 0.3635,"cpu-vs-cycles": 0.1036,- "cpu-vs-eevee": 0.3631,+ "cpu-vs-eevee": 0.363,"eevee-vs-cycles": 0.373,"gpu-vs-cpu": 0.0065,"region-pixels": 0.2428,"region-gpu-vs-cycles": 0.1614,- "region-gpu-vs-eevee": 0.3273,+ "region-gpu-vs-eevee": 0.3271,"region-eevee-vs-cycles": 0.3147},"fog-valley": {- "gpu-vs-cycles": 0.1855,- "gpu-vs-eevee": 0.1456,- "cpu-vs-cycles": 0.1857,- "cpu-vs-eevee": 0.146,+ "gpu-vs-cycles": 0.1859,+ "gpu-vs-eevee": 0.1459,+ "cpu-vs-cycles": 0.1861,+ "cpu-vs-eevee": 0.1463,"eevee-vs-cycles": 0.1952,"gpu-vs-cpu": 0.0116},"foliage": {- "gpu-vs-cycles": 0.0981,- "gpu-vs-eevee": 0.0806,- "cpu-vs-cycles": 0.0978,- "cpu-vs-eevee": 0.0803,+ "gpu-vs-cycles": 0.0979,+ "gpu-vs-eevee": 0.0804,+ "cpu-vs-cycles": 0.0976,+ "cpu-vs-eevee": 0.0802,"eevee-vs-cycles": 0.0888,"gpu-vs-cpu": 0.0095},"glass": {"gpu-vs-cycles": 0.1033,- "gpu-vs-eevee": 0.1,+ "gpu-vs-eevee": 0.1001,"cpu-vs-cycles": 0.1033,- "cpu-vs-eevee": 0.1002,+ "cpu-vs-eevee": 0.1003,"eevee-vs-cycles": 0.1008,- "gpu-vs-cpu": 0.0081+ "gpu-vs-cpu": 0.0078},"light-linking": {- "gpu-vs-cycles": 0.0998,- "gpu-vs-eevee": 0.1122,- "cpu-vs-cycles": 0.0993,- "cpu-vs-eevee": 0.1128,+ "gpu-vs-cycles": 0.0993,+ "gpu-vs-eevee": 0.1127,+ "cpu-vs-cycles": 0.0988,+ "cpu-vs-eevee": 0.1132,"eevee-vs-cycles": 0.1558,"gpu-vs-cpu": 0.0027},"lit-interior": {- "gpu-vs-cycles": 0.5054,- "gpu-vs-eevee": 0.0923,- "cpu-vs-cycles": 0.505,- "cpu-vs-eevee": 0.0929,+ "gpu-vs-cycles": 0.505,+ "gpu-vs-eevee": 0.0926,+ "cpu-vs-cycles": 0.5046,+ "cpu-vs-eevee": 0.0932,"eevee-vs-cycles": 0.5159,"gpu-vs-cpu": 0.0062},"long-street": {- "gpu-vs-cycles": 0.1153,- "gpu-vs-eevee": 0.0909,- "cpu-vs-cycles": 0.1155,- "cpu-vs-eevee": 0.0914,+ "gpu-vs-cycles": 0.1149,+ "gpu-vs-eevee": 0.0911,+ "cpu-vs-cycles": 0.115,+ "cpu-vs-eevee": 0.0919,"eevee-vs-cycles": 0.0976,- "gpu-vs-cpu": 0.0062+ "gpu-vs-cpu": 0.0066},"material-spheres": {- "gpu-vs-cycles": 0.1234,- "gpu-vs-eevee": 0.0723,- "cpu-vs-cycles": 0.1233,- "cpu-vs-eevee": 0.0722,+ "gpu-vs-cycles": 0.1185,+ "gpu-vs-eevee": 0.0686,+ "cpu-vs-cycles": 0.1184,+ "cpu-vs-eevee": 0.0686,"eevee-vs-cycles": 0.1053,- "gpu-vs-cpu": 0.0073+ "gpu-vs-cpu": 0.0082},"megacity-dusk": {- "gpu-vs-cycles": 0.1589,- "gpu-vs-eevee": 0.1034,- "cpu-vs-cycles": 0.1582,- "cpu-vs-eevee": 0.1072,+ "gpu-vs-cycles": 0.1584,+ "gpu-vs-eevee": 0.1038,+ "cpu-vs-cycles": 0.1577,+ "cpu-vs-eevee": 0.1076,"eevee-vs-cycles": 0.1524,"gpu-vs-cpu": 0.0276},"metal": {- "gpu-vs-cycles": 0.0974,- "gpu-vs-eevee": 0.0813,- "cpu-vs-cycles": 0.0972,- "cpu-vs-eevee": 0.0815,+ "gpu-vs-cycles": 0.0962,+ "gpu-vs-eevee": 0.0821,+ "cpu-vs-cycles": 0.096,+ "cpu-vs-eevee": 0.0823,"eevee-vs-cycles": 0.0916,- "gpu-vs-cpu": 0.0064+ "gpu-vs-cpu": 0.0063},"metal-room": {- "gpu-vs-cycles": 0.5074,- "gpu-vs-eevee": 0.1085,- "cpu-vs-cycles": 0.507,- "cpu-vs-eevee": 0.1091,+ "gpu-vs-cycles": 0.5071,+ "gpu-vs-eevee": 0.1089,+ "cpu-vs-cycles": 0.5067,+ "cpu-vs-eevee": 0.1095,"eevee-vs-cycles": 0.5262,"gpu-vs-cpu": 0.0062},@@ -269,31 +269,31 @@"gpu-vs-cpu": 0.0124},"outdoor-hdri": {- "gpu-vs-cycles": 0.0445,- "gpu-vs-eevee": 0.0283,- "cpu-vs-cycles": 0.0439,+ "gpu-vs-cycles": 0.0443,+ "gpu-vs-eevee": 0.0282,+ "cpu-vs-cycles": 0.0437,"cpu-vs-eevee": 0.0281,"eevee-vs-cycles": 0.0403,- "gpu-vs-cpu": 0.0062+ "gpu-vs-cpu": 0.0061},"spot-area": {- "gpu-vs-cycles": 0.1242,- "gpu-vs-eevee": 0.1707,- "cpu-vs-cycles": 0.1242,- "cpu-vs-eevee": 0.171,+ "gpu-vs-cycles": 0.1244,+ "gpu-vs-eevee": 0.1709,+ "cpu-vs-cycles": 0.1245,+ "cpu-vs-eevee": 0.1712,"eevee-vs-cycles": 0.1935,- "gpu-vs-cpu": 0.0032+ "gpu-vs-cpu": 0.0033},"temple-hall": {"gpu-vs-cycles": 0.064,"gpu-vs-eevee": 0.2319,"cpu-vs-cycles": 0.064,- "cpu-vs-eevee": 0.2318,+ "cpu-vs-eevee": 0.2317,"eevee-vs-cycles": 0.2341,"gpu-vs-cpu": 0.0021,"region-pixels": 0.0961,- "region-gpu-vs-cycles": 0.2362,- "region-gpu-vs-eevee": 0.2844,+ "region-gpu-vs-cycles": 0.236,+ "region-gpu-vs-eevee": 0.2841,"region-eevee-vs-cycles": 0.2453}}diff --git a/bench/reference/images/brick-flat.jpg b/bench/reference/images/brick-flat.jpgindex e20a1856..fd45db53 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 037f8b2a..a598c330 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 84cd28b2..b1744988 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 0e7d71f1..ef9453bf 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 2260a6cc..33c9d628 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 7b1f085c..f0a2215d 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 6e0350cb..9a557c9b 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 51cb92c2..af2dcc26 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 ba71797a..4ee2b6b1 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 36e441b6..9331357f 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 f8f42709..5c179eef 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 e0eca2ce..1576a939 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 3d81593f..a9e70f6b 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 441c670c..289c53cf 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 1413a380..b6a4934e 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 7797f15c..6b177877 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 636a8309..b04e71b2 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 fc6dd5aa..7d0efd3f 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 077c58b9..dccd696f 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 204e0541..2bb7d67b 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 3279cf94..a52c8dd8 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 6d796583..e3a3ea4c 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 45e56fe8..b20fbaf7 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 13a95e5c..8ed69987 100644--- a/bench/size/budgets.json+++ b/bench/size/budgets.json@@ -6,7 +6,7 @@"agent-scene": 100000},"budgets": {- "entry:core": 85800,+ "entry:core": 85920,"entry:model": 14400,"entry:nature": 13200,"entry:motion": 13600,@@ -33,9 +33,9 @@"entry:light-bake": 28500,"entry:path-tracer": 14600,"entry:texture-set": 900,- "entry:agent-scene": 77600,- "entry:agent-scene+lazy": 259500,- "entry:agent-asset": 108900,+ "entry:agent-scene": 77780,+ "entry:agent-scene+lazy": 259700,+ "entry:agent-asset": 109130,"entry:agent-asset+lazy": 271100,"entry:hdri": 3300,"entry:camera-effects": 8600,@@ -44,10 +44,10 @@"entry:material-layer-shaders": 20700,"entry:light-linking": 19000,"entry:cascaded-shadows": 27700,- "scene:lit-box": 63150,- "scene:gltf-box": 84200,- "scene:gltf-box+lazy": 87600,- "scene:agent-scene": 104940,- "scene:agent-scene+lazy": 286860+ "scene:lit-box": 63310,+ "scene:gltf-box": 84330,+ "scene:gltf-box+lazy": 87650,+ "scene:agent-scene": 105110,+ "scene:agent-scene+lazy": 287020}}diff --git a/changes/167-direct.md b/changes/167-direct.mdnew file mode 100644index 00000000..ec4d8adf--- /dev/null+++ b/changes/167-direct.md@@ -0,0 +1 @@+- Add multiple scattering to the direct light specular, as the environment specular has it (part of #167): each light's specular is scaled by the gain from the BRDF table, sampled once per fragment, wherever an environment map is bound. `metal` goes from 0.0974 to 0.0962 against Cycles (EEVEE 0.0916) and `material-spheres` from 0.1234 to 0.1185; no scene is worse by more than 0.0002, and the frame cost of `brick-flat` and `metal` is unchanged within timer noise.diff --git a/src/environment.js b/src/environment.jsindex 111af7ed..aa7bfdba 100644--- a/src/environment.js+++ b/src/environment.js@@ -287,14 +287,18 @@ vec3 environmentDiffuse(vec3 normal) {+ environmentIrradiance[8] * (mapped.x * mapped.x - mapped.y * mapped.y);return max(result, vec3(0.0)) * environmentIntensity;}+// Light a rough microfacet loses on its first bounce and gets back on a second one, as a factor on single-scatter specular.+vec3 multipleScatterGain(vec2 lookup, vec3 baseReflectance) {+ float missingEnergy = 1.0 - (lookup.x + lookup.y);+ vec3 averageFresnel = baseReflectance + (1.0 - baseReflectance) / 21.0;+ return 1.0 / (1.0 - missingEnergy * averageFresnel);+}// specularWeight scales only the reflected sky; leaf cards use less, since their bent normals face the view at grazing angles.vec3 environmentLighting(vec3 normal, vec3 specularNormal, vec3 baseReflectance, vec3 viewDirection, vec3 color, float metallic, float roughness, float occlusion, float ambient, float specularWeight) {float normalView = clamp(dot(normal, viewDirection), 0.001, 1.0);vec2 lookup = texture(environmentBrdf, vec2(normalView, roughness)).rg;vec3 singleScatter = baseReflectance * lookup.x + lookup.y;- float missingEnergy = 1.0 - (lookup.x + lookup.y);- vec3 averageFresnel = baseReflectance + (1.0 - baseReflectance) / 21.0;- vec3 specularColor = singleScatter / (1.0 - missingEnergy * averageFresnel) * specularWeight;+ vec3 specularColor = singleScatter * multipleScatterGain(lookup, baseReflectance) * specularWeight;vec3 specular = environmentRadiance(reflect(-viewDirection, specularNormal), roughness * environmentLevelMax) * specularColor;vec3 diffuse = environmentDiffuse(normal) * color * (1.0 - metallic) * (1.0 - specularColor);return (specular + diffuse) * occlusion * ambient;diff --git a/src/shaders.js b/src/shaders.jsindex d353d915..1423c841 100644--- a/src/shaders.js+++ b/src/shaders.js@@ -536,6 +536,9 @@ vec3 studio(vec3 direction, float roughness) {${environmentLookupGlsl}${environmentShadingGlsl}+// Set once per fragment from the environment's BRDF table; every light's specular scales by it.+vec3 directSpecularGain = vec3(1.0);+${featureCode("declarations")}vec3 illuminate(vec3 normal, vec3 viewDirection, vec3 lightDirection, vec3 radiance, vec3 color, float metallic, float roughness) {float normalLight = max(dot(normal, lightDirection), 0.0);if (normalLight == 0.0) return vec3(0);@@ -551,6 +554,7 @@ ${featureCode("declarations")}vec3 illuminate(vec3 normal, vec3 viewDirection, vvec3 baseReflectance = mix(materialDielectric, color, metallic);${featureCode("illuminateReflectance")} vec3 reflectance = fresnel(max(dot(halfway, viewDirection), 0.0), baseReflectance);vec3 specular = distribution * geometry * reflectance / max(4.0 * normalView * normalLight, 0.001);+ specular *= directSpecularGain;${featureCode("illuminateSpecular")} vec3 diffuse = (1.0 - reflectance) * (1.0 - metallic) * color / PI;return (diffuse + specular) * radiance * normalLight;}@@ -796,7 +800,11 @@ void main() {}vec3 specularNormal = normal;vec3 baseReflectance = mix(materialDielectric, color, metallic);-${featureCode("mainSetup")} float visibility = shadowVisibility(normal);+${featureCode("mainSetup")} if (ENVIRONMENT_ENABLED) {+ vec2 brdfLookup = texture(environmentBrdf, vec2(clamp(dot(normal, viewDirection), 0.001, 1.0), roughness)).rg;+ directSpecularGain = multipleScatterGain(brdfLookup, baseReflectance);+ }+ float visibility = shadowVisibility(normal);vec3 radiance = illuminate(normal, viewDirection, normalize(lightDirection), keyColor * visibility${featureCode("linkSun")}, color, metallic, roughness);if (FILL_ON) {radiance += illuminate(normal, viewDirection, normalize(vec3(4.0, 2.0, -3.0)), mix(vec3(0.8, 2.0, 1.8), vec3(0.12, 0.17, 0.20) * daylight, worldMode) * fillIntensity * mix(1.0, vertexSkyVisibility, worldMode), color, metallic, roughness);diff --git a/tests/environment.test.js b/tests/environment.test.jsindex a58fb191..b051f463 100644--- a/tests/environment.test.js+++ b/tests/environment.test.js@@ -1,6 +1,7 @@import test from 'node:test';import assert from 'node:assert/strict';import { createEnvironmentMap, computeIrradianceCoefficients, evaluateIrradiance, estimateEnvironmentBytes, environmentSpecularLayout, ENVIRONMENT_BRDF_SIZE, environmentShadingGlsl } from '../src/environment.js';+import { createFragmentShader } from '../src/shaders.js';import { parseRadianceHdr, loadHdri } from '../src/hdri.js';function filledMap(width, height, fillPixel, extra = {}) {@@ -123,5 +124,13 @@ test('environment memory accounting counts the half-float chain and the BRDF lootest('environment specular adds the energy a rough surface scatters a second time', () => {assert.match(environmentShadingGlsl, /missingEnergy/);- assert.match(environmentShadingGlsl, /singleScatter \/ \(1\.0 - missingEnergy \* averageFresnel\)/);+ assert.match(environmentShadingGlsl, /return 1\.0 \/ \(1\.0 - missingEnergy \* averageFresnel\)/);+ assert.match(environmentShadingGlsl, /singleScatter \* multipleScatterGain\(lookup, baseReflectance\)/);+});++test('direct light specular takes the same multiple scatter gain once per fragment', () => {+ const source = createFragmentShader(['environmentMap']);+ assert.match(source, /specular \*= directSpecularGain;/);+ assert.equal(source.match(/directSpecularGain = multipleScatterGain/g).length, 1);+ assert.equal(source.match(/texture\(environmentBrdf/g).length, 2);});