Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Compare trees like with like in the reference suite (issue #166)

Nguyen Xuan An835a68f41d2b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 321cc82c..67d9146f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
## Unreleased
+- Compare trees like with like (issue #166, part of it). The Blender side of `bench/reference` now rebuilds the 67js leaf model for `foliage` materials (back-face normal, colour and normal variation, sky reflection 0.3, and light from behind as a Translucent BSDF with weight `0.3 * pi / (3.7, 3.4, 2.9)`), and the 67js back light is now shadowed by the sun shadow. `foliage` goes from 0.1290 to 0.1173 against Cycles (EEVEE 0.0864 to 0.0888) and `mountain-valley` from 0.3473 to 0.3483 (EEVEE 0.1828 to 0.1879); no other scene moved. The remaining gap is canopy and valley sky occlusion, recorded in `bench/reference/README.md`.
- Fix links in the npm package that pointed at docs it does not ship (issue #178). `docs/guide.md`, `docs/material-maps.md`, `README.md` and `ROADMAP.md` now link the 20 unshipped docs, `CONTRIBUTING.md`, the texture credits and the changelog by absolute GitHub URL; shipping those docs would add 64 KB gzip. `npm run check:links:package` packs the archive, unpacks it and checks every link inside (26 files, 0 broken), and the `Test` workflow runs it.
- Take 34 KB of headroom under the npm archive budget without raising it (issue #177). `CHANGELOG.md` no longer ships in the package, because GitHub serves it; the archive went from 740,972 to 705,961 bytes against the 740,000 budget. `bench/size/README.md` now says how the budget is set and what it protects.
- Add a test that fails when the MCP tool count in `docs/guide.md`, `docs/mcp.md`, `index.html` or `scripts/smoke-mcp.mjs` differs from the `tools/list` result (issue #180). The server registers 22 tools; the 23rd `register(` in the file is the function definition, so the documented count was right.
diff --git a/bench/reference/README.md b/bench/reference/README.md
index a8324ecc..d539703e 100644
--- a/bench/reference/README.md
+++ b/bench/reference/README.md
@@ -31,7 +31,7 @@ Scores against Cycles, with each change in turn:
| `brushed-film` | 0.1613 | 0.1172 | 0.1149 | 0.0984 |
| `metal` | 0.1868 | 0.1309 | 0.1294 | 0.0916 |
| `material-spheres` | 0.1598 | 0.1337 | 0.1312 | 0.1053 |
-| `foliage` | 0.1594 | 0.1346 | 0.1290 | 0.0864 |
+| `foliage` | 0.1594 | 0.1346 | 0.1173 | 0.0888 |
| `brick-flat` | 0.1999 | 0.1273 | 0.1272 | 0.1098 |
| `brick-graph` | 0.2269 | 0.1604 | 0.1602 | 0.1221 |
| `long-street` | 0.1555 | 0.1555 | 0.1407 | 0.0976 |
@@ -45,7 +45,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.3473 | 0.1828 | Trees are lighter from leaf translucency, and shadowed rock is brighter |
+| `mountain-valley` | 7,698 | 9.72 million | 0.3483 | 0.1879 | The valley floor and the trees are 2 to 3 times as bright as in Cycles, and shadowed rock is brighter (see "Leaf model and tree regions") |
| `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.
@@ -267,11 +267,53 @@ Noise contributes nothing (0.2269 against 0.2270 without it), so the different n
The two solvers differ in kind, so this mostly scores how alike the motion is. The banner sits at the EEVEE yardstick, which is a weak test: a flat sheet leaning in the wind has few pixels to get wrong. By eye the banner differs in shape: Blender's sheet wrinkles and sags along the bottom edge, 67js stays smooth and flat. The drape is about twice the yardstick: Blender's sheet has landed on the ball as a bell with wrinkles and its corners still up, while 67js has pulled its corners down and hangs flat panels on both sides of the ball. Wind is matched by the steady swing of the banner (Blender field strength 2 is 1.5 m/s here), because Blender's wind is a linear drag and this solver's is quadratic. The drape numbers also moved when the drag was matched: see "Recipe cloth" in `docs/physics.md`.
+## Leaf model and tree regions (issue #166)
+
+Before this change the Blender leaf was a plain Principled BSDF, while the 67js foliage shader (`CANOPY_TAG` in `src/shaders.js`) is not. `render-blender.py` now rebuilds each term of that shader on the Blender side, with the shader's own constants, for every material tagged `foliage`:
+
+| 67js foliage term | Blender node setup |
+| --- | --- |
+| Back faces keep the stored normal | Geometry normal times `1 - 2 * Backfacing` undoes Blender's flip |
+| Colour times `0.78 + 0.16 * sin(13 x + sin(9 y)) * sin(11 z - 5 y)` of the world position | The same sines on Geometry position, multiplied into the base colour |
+| Normal moved by `0.06 * (sin(12 z), sin(9 x), cos(11 y))`, then normalised | The same vector in Blender axes, on the Principled and Translucent normals |
+| Sky reflection 0.3 of a dielectric's | Specular IOR Level 0.5 * 0.3 |
+| Light from behind: `colour * max(dot(-N, L), 0) * 0.3 * keyColor / (3.7, 3.4, 2.9)` | Translucent BSDF with colour weight `0.3 * pi / (3.7, 3.4, 2.9)`, which returns the same radiance for a sun of irradiance `keyColor`, added to the Principled BSDF |
+| Alpha mask at 0.5 | Mix Shader with a Transparent BSDF, so the Translucent part is cut out too |
+
+The weights are derived from the shader, not tuned. One difference stays: the 67js term was not shadowed, and the Blender Translucent BSDF is. The 67js term now multiplies by the sun shadow `visibility` as well (a leaf can only pass sun that reaches it), which is the one engine change of this issue. It moved `foliage` from 0.1181 to 0.1173; on its own the shader change is small.
+
+Scores before and after (FLIP against Cycles, RTX 3060, 2026-11-02), with EEVEE against Cycles beside them:
+
+| Scene | Before | Fair leaf, old shader | After | EEVEE |
+| --- | --- | --- | --- | --- |
+| `foliage` | 0.1290 | 0.1181 | 0.1173 | 0.0864 before, 0.0888 after |
+| `mountain-valley` | 0.3473 | 0.3485 | 0.3483 | 0.1828 before, 0.1879 after |
+
+The fair leaf alone moved `foliage` by 0.0109 (the Cycles crown got brighter, from 0.135 to 0.176 mean display luminance) and `mountain-valley` by 0.001: the leaf model was never the main gap there. No other scene changed to four digits in the full rescore.
+
+Regions, measured on the Cycles image (display luminance, mean of the region; FLIP is the mean error of 67js against Cycles):
+
+| Scene and region | Cycles | EEVEE | 67js | FLIP 67js | Share of the error |
+| --- | --- | --- | --- | --- | --- |
+| `foliage` crown (green, upper rows) | 0.176 | 0.229 | 0.300 | 0.472 | 0.37 |
+| `foliage` trunk and ground under the tree | 0.139 | 0.191 | 0.208 | 0.313 | 0.02 |
+| `foliage` open ground and sky | 0.689 to 0.793 | the same | the same to 0.01 | 0.03 to 0.14 | 0.55 |
+| `mountain-valley` trees (green) | 0.186 | 0.225 | 0.335 | 0.481 | 0.53 |
+| `mountain-valley` valley floor, rows 440 to 530 | 0.272 | 0.306 | 0.416 | about 0.5 | |
+
+What the experiments show, each with the flags of this runner:
+
+- `foliage` crown with `--ambient 0` (sun and shading only): 67js 0.176, the same as the whole Cycles crown. So the sun term alone is already as bright as Cycles, and the sky term adds 0.12 on top. Cycles loses most of the sky inside a dense crown to the leaves around each card; 67js has no occlusion in the ambient term, only vertex colours that darken from the crown centre. Screen space occlusion (`--ambient-occlusion 1 --ambient-occlusion-radius 1`) takes `foliage` from 0.1181 to 0.1133 and the crown to 0.283, which is not enough to close the gap. The remaining gap is recorded, not fixed: it needs occlusion between cards, which is the light bake's job (`addons/light-bake`) and is not part of a shader.
+- `mountain-valley` floor with `--ambient 0`: 67js 0.180, against 0.272 for the full Cycles frame. The valley floor sits inside a ring of 1,100 m peaks that hide much of the sky in Cycles, and 67js lights the floor with the whole sky. This is inferred from the ring geometry and from the floor matching on the open `foliage` scene (0.698 against 0.689); it was not measured by removing the mountains. It is the main reason the trees there are lighter than in Cycles too. Fixing it needs sky visibility for terrain, which no shader has.
+- Ground under the `foliage` tree: the floor is within 0.01 luminance of Cycles, and FLIP is worst in a band of 0.2 to 0.5 at the canopy rows, not at the trunk base. The trunk and its contact shadow are 2% of the error.
+
+Blender render time of the two rerun scenes, now in `baseline.json`: `foliage` 26.4 s in Cycles and 13.5 s in EEVEE, `mountain-valley` 35.9 s and 39.2 s (other jobs loaded the machine).
+
## Limits
- Scores come from one camera for each scene and one machine. They do not measure motion or speed.
- This suite sets an HDRI from code and leaves occlusion off; `--ambient-occlusion <strength> [--ambient-occlusion-radius <units>]` turns it on. A recipe reaches only the named procedural setups, so these scores still describe a better environment than an agent gets (issue #53).
-- The Blender leaf material has no translucency, and the 67js foliage shader adds it by a trick. `foliage` is therefore partly a difference in materials.
+- Both sides use the same leaf model since issue #166 (see "Leaf model and tree regions"). Grass has its own extra term in the 67js shader (`TAG_GRASS`), and Blender does not get it; no reference scene draws grass cards.
- `render-blender.py` does not read normal maps. No scene uses one yet, so a new scene with a normal map must add that first.
- `glass` and `car-paint` use the real 67js `transmission` and `clearcoat` layers, and the Blender side sets the same values on the Principled BSDF. 67js refracts through the front surface only, so the sphere does not show the inverted sky that Cycles traces through the back face.
- `car-paint` scored 0.1521 against Cycles and `glass` 0.1326 (it was 0.1450 with the `liquid` material). The other scenes scored the same as in the table above, to four digits. The table above is the 2026-10-30 baseline and has not been rewritten.
diff --git a/bench/reference/baseline.json b/bench/reference/baseline.json
index 8b3ee0e5..e65c9311 100644
--- a/bench/reference/baseline.json
+++ b/bench/reference/baseline.json
@@ -26,13 +26,17 @@
"cycles": 7.7,
"eevee": 0.4
},
+ "dark-shaft": {
+ "cycles": 129.3,
+ "eevee": 67.2
+ },
"fog": {
"cycles": 58.3,
"eevee": 1.4
},
"foliage": {
- "cycles": 16.9,
- "eevee": 0.6
+ "cycles": 26.4,
+ "eevee": 13.5
},
"glass": {
"cycles": 18.9,
@@ -67,8 +71,8 @@
"eevee": 41.9
},
"mountain-valley": {
- "cycles": 30.5,
- "eevee": 11.8
+ "cycles": 35.9,
+ "eevee": 39.2
},
"outdoor-hdri": {
"cycles": 12.3,
@@ -81,10 +85,6 @@
"temple-hall": {
"cycles": 57.7,
"eevee": 1.1
- },
- "dark-shaft": {
- "cycles": 129.3,
- "eevee": 67.2
}
},
"graphics": {
@@ -161,11 +161,11 @@
"gpu-vs-cpu": 0.0089
},
"foliage": {
- "gpu-vs-cycles": 0.129,
- "gpu-vs-eevee": 0.091,
- "cpu-vs-cycles": 0.1287,
- "cpu-vs-eevee": 0.0906,
- "eevee-vs-cycles": 0.0864,
+ "gpu-vs-cycles": 0.1173,
+ "gpu-vs-eevee": 0.0836,
+ "cpu-vs-cycles": 0.117,
+ "cpu-vs-eevee": 0.0833,
+ "eevee-vs-cycles": 0.0888,
"gpu-vs-cpu": 0.01
},
"glass": {
@@ -233,11 +233,11 @@
"gpu-vs-cpu": 0.0061
},
"mountain-valley": {
- "gpu-vs-cycles": 0.3473,
- "gpu-vs-eevee": 0.242,
- "cpu-vs-cycles": 0.346,
- "cpu-vs-eevee": 0.2406,
- "eevee-vs-cycles": 0.1828,
+ "gpu-vs-cycles": 0.3483,
+ "gpu-vs-eevee": 0.2382,
+ "cpu-vs-cycles": 0.3469,
+ "cpu-vs-eevee": 0.2368,
+ "eevee-vs-cycles": 0.1879,
"gpu-vs-cpu": 0.02
},
"outdoor-hdri": {
diff --git a/bench/reference/images/foliage.jpg b/bench/reference/images/foliage.jpg
index cc324d18..7aa38e8c 100644
Binary files a/bench/reference/images/foliage.jpg and b/bench/reference/images/foliage.jpg differ
diff --git a/bench/reference/images/mountain-valley.jpg b/bench/reference/images/mountain-valley.jpg
index 741b0a78..9d8d41c5 100644
Binary files a/bench/reference/images/mountain-valley.jpg and b/bench/reference/images/mountain-valley.jpg differ
diff --git a/bench/reference/render-blender.py b/bench/reference/render-blender.py
index d84df33c..1e1f2ed7 100644
--- a/bench/reference/render-blender.py
+++ b/bench/reference/render-blender.py
@@ -363,6 +363,122 @@ def add_graph(material, principled, description):
principled.inputs["Emission Strength"].default_value = description.get("emission", 1)
+# The 67js foliage shader (CANOPY_TAG in src/shaders.js) is not a plain Principled leaf. Each term is rebuilt below with the
+# same constants, so both sides evaluate the same material:
+# - the shading normal of a double-sided card keeps its direction on the back face (Blender flips it);
+# - the colour is scaled by 0.78 + 0.16 * sin(13 x + sin(9 y)) * sin(11 z - 5 y) of the world position;
+# - the normal is moved by 0.06 * (sin(12 z), sin(9 x), cos(11 y)) of the world position, then normalised;
+# - environment reflection is 0.3 of a dielectric's, which halves-and-more the Principled specular level (0.5 is F0 0.04);
+# - light from behind adds colour * max(dot(-N, L), 0) * 0.3 * keyColor / resolve, with no shadow test.
+# resolve is the daylight divisor of the shader in world mode. A Translucent BSDF returns albedo * E / pi * cos, so the weight
+# that gives the same radiance is 0.3 * pi / resolve for each channel.
+CANOPY_NORMAL_JITTER = 0.06
+CANOPY_COLOR_BASE = 0.78
+CANOPY_COLOR_SWING = 0.16
+CANOPY_SKY_REFLECTION = 0.3
+CANOPY_BACK_LIGHT = 0.3
+WORLD_KEY_RESOLVE = (3.7, 3.4, 2.9)
+PRINCIPLED_SPECULAR_LEVEL_AT_DIELECTRIC = 0.5
+
+
+def add_canopy_leaf_model(material, principled):
+ nodes = material.node_tree.nodes
+ links = material.node_tree.links
+
+ def vector_math(operation, first, second=None, scale=None):
+ node = nodes.new("ShaderNodeVectorMath")
+ node.operation = operation
+ links.new(first, node.inputs[0])
+ if second is not None:
+ links.new(second, node.inputs[1])
+ if isinstance(scale, (int, float)):
+ node.inputs[3].default_value = scale
+ elif scale is not None:
+ links.new(scale, node.inputs[3])
+ return node.outputs[0]
+
+ def scalar_math(operation, first, second=None):
+ node = nodes.new("ShaderNodeMath")
+ node.operation = operation
+ for index, value in enumerate((first, second)):
+ if value is None:
+ continue
+ if isinstance(value, (int, float)):
+ node.inputs[index].default_value = value
+ else:
+ links.new(value, node.inputs[index])
+ return node.outputs[0]
+
+ geometry = nodes.new("ShaderNodeNewGeometry")
+ position = nodes.new("ShaderNodeSeparateXYZ")
+ links.new(geometry.outputs["Position"], position.inputs[0])
+ # 67js (x, y, z) is Blender (X, Z, -Y).
+ world_x = position.outputs["X"]
+ world_y = position.outputs["Z"]
+ world_z = scalar_math("MULTIPLY", position.outputs["Y"], -1.0)
+
+ inner = scalar_math("SINE", scalar_math("MULTIPLY", world_y, 9.0))
+ first_wave = scalar_math("SINE", scalar_math("ADD", scalar_math("MULTIPLY", world_x, 13.0), inner))
+ second_wave = scalar_math("SINE", scalar_math("SUBTRACT", scalar_math("MULTIPLY", world_z, 11.0), scalar_math("MULTIPLY", world_y, 5.0)))
+ swing = scalar_math("MULTIPLY", scalar_math("MULTIPLY", first_wave, second_wave), CANOPY_COLOR_SWING)
+ modulation = scalar_math("ADD", swing, CANOPY_COLOR_BASE)
+
+ base_input = principled.inputs["Base Color"]
+ if base_input.is_linked:
+ base_color = base_input.links[0].from_socket
+ else:
+ constant = nodes.new("ShaderNodeRGB")
+ constant.outputs[0].default_value = base_input.default_value
+ base_color = constant.outputs[0]
+ modulated = nodes.new("ShaderNodeMixRGB")
+ modulated.blend_type = "MULTIPLY"
+ modulated.inputs[0].default_value = 1
+ links.new(base_color, modulated.inputs[1])
+ links.new(modulation, modulated.inputs[2])
+ links.new(modulated.outputs["Color"], base_input)
+
+ # Back face keeps the stored normal: undo Blender's flip, then add the jitter.
+ unflip = scalar_math("SUBTRACT", 1.0, scalar_math("MULTIPLY", geometry.outputs["Backfacing"], 2.0))
+ stored_normal = vector_math("SCALE", geometry.outputs["Normal"], scale=unflip)
+ jitter = nodes.new("ShaderNodeCombineXYZ")
+ # 67js (sin(12 z), sin(9 x), cos(11 y)) is Blender (X, Y, Z) = (sin(12 z), -cos(11 y), sin(9 x)).
+ links.new(scalar_math("SINE", scalar_math("MULTIPLY", world_z, 12.0)), jitter.inputs["X"])
+ links.new(scalar_math("MULTIPLY", scalar_math("COSINE", scalar_math("MULTIPLY", world_y, 11.0)), -1.0), jitter.inputs["Y"])
+ links.new(scalar_math("SINE", scalar_math("MULTIPLY", world_x, 9.0)), jitter.inputs["Z"])
+ jitter_scaled = vector_math("SCALE", jitter.outputs[0], scale=CANOPY_NORMAL_JITTER)
+ shading_normal = vector_math("NORMALIZE", vector_math("ADD", stored_normal, jitter_scaled))
+ links.new(shading_normal, principled.inputs["Normal"])
+
+ principled.inputs["Specular IOR Level"].default_value = PRINCIPLED_SPECULAR_LEVEL_AT_DIELECTRIC * CANOPY_SKY_REFLECTION
+
+ translucent = nodes.new("ShaderNodeBsdfTranslucent")
+ weight = nodes.new("ShaderNodeRGB")
+ weight.outputs[0].default_value = tuple(CANOPY_BACK_LIGHT * math.pi / resolve for resolve in WORLD_KEY_RESOLVE) + (1,)
+ weighted = nodes.new("ShaderNodeMixRGB")
+ weighted.blend_type = "MULTIPLY"
+ weighted.inputs[0].default_value = 1
+ links.new(modulated.outputs["Color"], weighted.inputs[1])
+ links.new(weight.outputs[0], weighted.inputs[2])
+ links.new(weighted.outputs["Color"], translucent.inputs["Color"])
+ links.new(shading_normal, translucent.inputs["Normal"])
+
+ total = nodes.new("ShaderNodeAddShader")
+ links.new(principled.outputs["BSDF"], total.inputs[0])
+ links.new(translucent.outputs["BSDF"], total.inputs[1])
+ final = total.outputs[0]
+ alpha_input = principled.inputs["Alpha"]
+ if alpha_input.is_linked:
+ mask = alpha_input.links[0].from_socket
+ links.remove(alpha_input.links[0])
+ cutout = nodes.new("ShaderNodeMixShader")
+ links.new(mask, cutout.inputs[0])
+ links.new(nodes.new("ShaderNodeBsdfTransparent").outputs[0], cutout.inputs[1])
+ links.new(total.outputs[0], cutout.inputs[2])
+ final = cutout.outputs[0]
+ output = next(node for node in nodes if node.type == "OUTPUT_MATERIAL")
+ links.new(final, output.inputs["Surface"])
+
+
def build_material(name, description, has_vertex_colors):
material = bpy.data.materials.new(name)
material.use_nodes = True
@@ -385,6 +501,8 @@ def build_material(name, description, has_vertex_colors):
add_texture(material, principled, description, has_vertex_colors)
elif has_vertex_colors:
add_vertex_colors(material, principled, description)
+ if description.get("tag") == "foliage":
+ add_canopy_leaf_model(material, principled)
return material
diff --git a/bench/reference/scenes.js b/bench/reference/scenes.js
index 7b453473..51623842 100644
--- a/bench/reference/scenes.js
+++ b/bench/reference/scenes.js
@@ -169,7 +169,7 @@ export const KHRONOS_SCENES = Object.fromEntries(KHRONOS_ASSET_NAMES.map(name =>
// Grand scenes: the scale people actually want to draw, built from the engine's terrain, vegetation and plain boxes.
// Only what Blender can draw too is used: the terrain colours are vertex colours, the emissive windows are Emission, the
-// fog is a volume box. The built-in leaf translucency of 67js has no Blender counterpart (see the README).
+// fog is a volume box. Blender rebuilds the 67js leaf model for foliage materials (see the README).
function seededRandom(seed) {
let state = seed >>> 0;
return () => {
diff --git a/src/shaders.js b/src/shaders.js
index 5bfa8a60..1d9baea5 100644
--- a/src/shaders.js
+++ b/src/shaders.js
@@ -883,7 +883,7 @@ ${featureCode("probeSurroundings")} radiance += surroundings * reflectance *
if (USES_LIGHT_MAP) radiance += color * (1.0 - metallic) * texture(lightMap, lightmapUV).rgb * lightMapScale;
vec3 keyResponse = keyColor / mix(vec3(5.2, 4.7, 4.1), vec3(3.7, 3.4, 2.9), worldMode);
keyResponse *= mix(daylight, 1.0, worldMode);
- if (CANOPY_TAG) radiance += color * max(dot(-normal, normalize(lightDirection)), 0.0) * 0.3 * keyResponse;
+ if (CANOPY_TAG) radiance += color * max(dot(-normal, normalize(lightDirection)), 0.0) * 0.3 * keyResponse * visibility;
if (mossCover > 0.001) {
float sheen = pow(1.0 - max(dot(normal, viewDirection), 0.0), 4.0);
float wrap = max(dot(normal, normalize(lightDirection)) + 0.5, 0.0) / 1.5;