Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Clip the ambient term of a mapped normal to the surface plane (part of #50)

Nguyen Xuan An47f199a9e554
diff --git a/bench/reference/README.md b/bench/reference/README.md
index 2ec02cce..6a187a73 100644
--- a/bench/reference/README.md
+++ b/bench/reference/README.md
@@ -1037,3 +1037,22 @@ Oracle ceilings (Cycles pixels pasted into one region of the 67js frame, whole f
- Bake: `--bake metal --bake-preset standard` scores 0.1526, worse.
Where EEVEE beats 67js on the floor is the mid band (46% of the gap) and the open and far floor (30%), where the means are within 0.002 to 0.007 and the difference is spread thin (FLIP is also reading Cycles' own noise there); the one concentrated piece, the halo at the chrome sphere, is bounced light. No engine file, scene, baseline row or budget changed, and the first box of #167 stays open: `metal` 0.0957 against EEVEE 0.0916. The floor has no single cause left that a measured change reaches; what is left sits in the parts (the chrome sphere's lower half and the steel cylinder) and in bounce light.
+
+
+## Bump edges and the sky light below the surface (2026-11-03, part of issue #50)
+
+Main reproduced `baseline.json` first on the stored arrays (`brick-flat` 0.1264, `brick-graph` 0.1584, EEVEE against Cycles 0.1099 and 0.1221, GPU). The Cycles arrays came from a worktree whose flat wall matched to 4 digits; one other worktree's arrays gave 0.1301 for the flat wall's EEVEE gap and were not used. Regions come from projecting the wall back to UV (mortar `body` under 0.05, edge 0.05 to 0.95, brick over 0.95), FLIP per pixel against Cycles.
+
+| `brick-graph`, GPU | all | mortar | edge | brick | edge luminance (Cycles 0.444, EEVEE 0.494) |
+| --- | --- | --- | --- | --- | --- |
+| Bump 0 (no normal map) | 0.1467 | 0.2855 | 0.4293 | 0.2854 | 0.593 |
+| Bump 0.5 | 0.1536 | 0.3177 | 0.4029 | 0.3116 | 0.513 |
+| Bump 1 (baseline) | 0.1584 | 0.3342 | 0.4286 | 0.3204 | 0.521 |
+| Bump 2 | 0.1622 | 0.3471 | 0.4507 | 0.3267 | 0.533 |
+| Bump 1, ambient clipped to the surface plane | 0.1518 | 0.3238 | 0.3565 | 0.3150 | 0.465 |
+
+- The bump hurts against Cycles at every strength, and stronger is worse, so the scale (#219) and the footprint are not the lead. The 512 texel normal map puts the joint edges where Cycles puts them (the albedo transition of the bump-free render sits on the same pixels, 423 to 430 on row 168), and the signs match.
+- Across a joint's right edge Cycles stays dark over the whole 6 pixel transition (0.25 to 0.36) and 67js is dark over 2 or 3 pixels (0.33 to 0.47). Cycles discards light that arrives from below the geometric surface, and a bumped normal tilted 60 to 70 degrees has a third of its lobe there. 67js lit the full lobe.
+- Change: for a mapped normal the ambient and reflected sky term is scaled by `0.5 + 0.5 * dot(mapped, geometric)`, the share of an even sky lobe that stays above the surface. `brick-graph` 0.1584 to 0.1518 (EEVEE 0.0875 to 0.0844, CPU the same); all 22 other scenes reproduce their rows to 4 digits (none uses a normal map). Excess over its flat wall: 67js 0.0320 to 0.0254, EEVEE 0.0122.
+- Still open: the sun is not clipped against the surface plane (the wall faces the sun, so it changes nothing here), the colour gap on the brick faces (Cycles 0.524, EEVEE 0.476, 67js 0.466 in luminance), and a bump scale per world unit instead of per UV unit (Blender's Distance 0.15 is 0.025 per UV unit across and 0.0375 up on this 6 by 4 wall, the bake gives 0.031 both ways). Whether the condition of #50 can be met is still An's decision.
+Bundle size: the two shader lines add 9 to 18 bytes gzip to eight bundles that carry the main fragment shader (ntry:core, ntry:agent-scene, ntry:agent-asset, ntry:cascaded-shadows, scene:lit-box, scene:gltf-box, scene:gltf-box+lazy, scene:agent-scene); their budgets rose by 20 to 30 bytes.
\ No newline at end of file
diff --git a/bench/reference/baseline.json b/bench/reference/baseline.json
index f9edcf2a..b0d0d868 100644
--- a/bench/reference/baseline.json
+++ b/bench/reference/baseline.json
@@ -109,10 +109,10 @@
"gpu-vs-cpu": 0.0065
},
"brick-graph": {
- "gpu-vs-cycles": 0.1584,
- "gpu-vs-eevee": 0.0875,
- "cpu-vs-cycles": 0.1584,
- "cpu-vs-eevee": 0.0876,
+ "gpu-vs-cycles": 0.1518,
+ "gpu-vs-eevee": 0.0844,
+ "cpu-vs-cycles": 0.1518,
+ "cpu-vs-eevee": 0.0844,
"eevee-vs-cycles": 0.1221,
"gpu-vs-cpu": 0.0077
},
diff --git a/bench/reference/images/brick-graph.jpg b/bench/reference/images/brick-graph.jpg
index a598c330..29b30b11 100644
Binary files a/bench/reference/images/brick-graph.jpg and b/bench/reference/images/brick-graph.jpg differ
diff --git a/bench/size/budgets.json b/bench/size/budgets.json
index 839c22b8..77366f2f 100644
--- a/bench/size/budgets.json
+++ b/bench/size/budgets.json
@@ -6,7 +6,7 @@
"agent-scene": 100000
},
"budgets": {
- "entry:core": 85920,
+ "entry:core": 85950,
"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": 77780,
+ "entry:agent-scene": 77810,
"entry:agent-scene+lazy": 259980,
- "entry:agent-asset": 109130,
+ "entry:agent-asset": 109160,
"entry:agent-asset+lazy": 271380,
"entry:hdri": 3300,
"entry:camera-effects": 8600,
@@ -43,11 +43,11 @@
"entry:render-passes": 1700,
"entry:material-layer-shaders": 20700,
"entry:light-linking": 19000,
- "entry:cascaded-shadows": 27700,
- "scene:lit-box": 63310,
- "scene:gltf-box": 84330,
- "scene:gltf-box+lazy": 87650,
- "scene:agent-scene": 105110,
+ "entry:cascaded-shadows": 27730,
+ "scene:lit-box": 63330,
+ "scene:gltf-box": 84350,
+ "scene:gltf-box+lazy": 87680,
+ "scene:agent-scene": 105130,
"scene:agent-scene+lazy": 287300
}
}
diff --git a/changes/50-bump.md b/changes/50-bump.md
new file mode 100644
index 00000000..7072d444
--- /dev/null
+++ b/changes/50-bump.md
@@ -0,0 +1 @@
+- Take the sky light below the surface plane out of the ambient term for a mapped normal, as Cycles does, so the edges of a bump read as dark as Cycles draws them (part of issue #50). `brick-graph` goes from 0.1584 to 0.1518 against Cycles (EEVEE 0.0875 to 0.0844) and no other scene changes.
diff --git a/src/shaders.js b/src/shaders.js
index 6b75f4f4..26ff0759 100644
--- a/src/shaders.js
+++ b/src/shaders.js
@@ -715,6 +715,7 @@ void main() {
// canopy, so the back face keeps it instead of flipping to point into the crown.
bool canopyShaded = CANOPY_TAG;
if (doubleSided > 0.5 && !gl_FrontFacing && !canopyShaded) normal = -normal;
+ vec3 geometricNormal = normal;
if (USES_NORMAL_MAP) normal = boxMapScale > 0.0 ? boxMappedNormal(normal) : mappedNormal(normal);
vec3 viewDirection = normalize(eye - worldPosition);
vec3 color = mix(baseColor, armorColor, step(0.5, surface.w) * (1.0 - step(1.5, surface.w)));
@@ -871,6 +872,7 @@ ${featureCode("linkArea")} vec3 lightNormal = areaLightNormals[index].xyz;
if (USES_OCCLUSION_MAP) occlusion = mix(1.0, texture(occlusionMap, mapUV(hasOcclusionMap)).r, occlusionStrength);
// Trees and terrain bake how much sky each vertex sees; everything else carries 1.
occlusion *= splatCavity * vertexSkyVisibility;
+ if (USES_NORMAL_MAP) occlusion *= 0.5 + 0.5 * dot(normal, geometricNormal);
if (ENVIRONMENT_ENABLED) {
float room = 1.0 - smoothstep(0.01, 0.06, dot(environmentDiffuse(vec3(0, 1, 0)), vec3(1)) / max(dot(keyColor, vec3(1)), 0.003));
occlusion *= mix(1.0, mix(0.25, 1.0, visibility), room * (1.0 - metallic));
diff --git a/tests/shader-features.test.js b/tests/shader-features.test.js
index e7c7d052..26e588c1 100644
--- a/tests/shader-features.test.js
+++ b/tests/shader-features.test.js
@@ -9,6 +9,11 @@ test('the default fragment shader keeps every feature', () => {
assert.match(fragmentShader, /#define USES_NORMAL_MAP \(hasNormalMap > 0\.5\)/);
});
+test('a mapped normal takes the sky light below the surface plane out of the ambient term', () => {
+ assert.match(fragmentShader, /vec3 geometricNormal = normal;\s+if \(USES_NORMAL_MAP\) normal = /);
+ assert.match(fragmentShader, /if \(USES_NORMAL_MAP\) occlusion \*= 0\.5 \+ 0\.5 \* dot\(normal, geometricNormal\);/);
+});
+
test('a feature that a scene does not use becomes a constant false', () => {
const plain = createFragmentShader([]);
for (const macro of ['USES_NORMAL_MAP', 'USES_SPLAT', 'USES_MOSS', 'USES_ALPHA_MODE', 'TAG_WATER', 'TAG_FIRE']) {