Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Measure the foliage crown leak against a sun-only Cycles render (part of #206)

Nguyen Xuan An9d77227c27e8
diff --git a/bench/reference/README.md b/bench/reference/README.md
index 8ef725f3..8da64a25 100644
--- a/bench/reference/README.md
+++ b/bench/reference/README.md
@@ -691,6 +691,27 @@ No engine change came out of this. Method: the stored Cycles array of `foliage`
Open: a sun-only Cycles render (world light 0, as #191 made) is needed to say whether Cycles shadows these leaves by the sun or the stored image darkens them by something else, for instance the Translucent term, which Cycles shadows differently from the 67js term. This needs Blender, which was not run. The second Done-when of #206 stays open.
+## The foliage crown against a sun-only Cycles render (2026-11-03, part of issue #206)
+
+The section above approximated the Cycles shadow mask from the full-light array. This one renders it: `blender -b --factory-startup -t 6 --python bench/reference/render-blender.py -- artifacts/reference --world-strength 0 foliage` draws Cycles only (same camera, 960 x 540, 256 samples, denoised), with the world's light at 0, so only the sun is left, into `foliage-cycles-world0.npy`. The EEVEE render and the timings stay as they were. `python bench/reference/crown-leak.py artifacts/reference <67js --ambient 0 png>` (command in its header) counts the pixels. Branch: the sun-aligned shadow and the world-unit bias (#224, #226) included.
+
+Crown pixels are the green-or-dark pixels of the full-light Cycles image in the top 62 percent of the frame, 56,921 of them (the 52,281 of the section above came from a slightly different colour test). Cycles shadowed is a tone-mapped linear value below 0.02 in the sun-only render; 67js sun-lit is above 0.01 in the `--ambient 0` render.
+
+| | Pixels |
+| --- | --- |
+| Sun-lit in Cycles / in 67js / in both | 19,883 / 30,907 / 17,836 |
+| Lit in 67js, shadowed in Cycles (the leak) | 13,071 |
+| of those, within 2 px of a pixel Cycles lights | 7,424 |
+| of those, farther away | 5,647 |
+| Lit in Cycles only | 2,047 |
+
+The true mask gives a larger leak than the full-light approximation did (11,137 on this crown). The target of fewer than 3,000 is not met, and nothing in the 67js shader moved it.
+
+- Shadow or silhouette: 7,424 of the 13,071 sit beside a leaf Cycles lights, as a sliver along its edge with a matching blue sliver (lit only in Cycles) on the other side. That is leaf placement and the denoiser, not shadow. The other 5,647 are patches inside the lower crown that 67js lights and Cycles shadows.
+- Translucency is not the difference. The Blender leaf adds a Translucent BSDF with the weight of the 67js back-light term (`CANOPY_BACK_LIGHT`), and the 67js term is multiplied by the sun visibility, so both sides shadow it (the comment in `render-blender.py` said the opposite and is corrected). This is read from the two definitions; a Cycles render without the Translucent BSDF would settle it and was not made.
+- A shadow bias grown on back-facing leaves is not the cause either. `shadowVisibility` floors the cosine at 0.05, which gives a back-lit leaf the largest bias; using `abs(dot)` changed the leak from 13,071 to 13,069, so it was not kept.
+- Solid quads in the shadow pass (no cutout) leave 5,418 leaking pixels (2,804 away from a lit edge) but 67js then lights 14,991 crown pixels against 19,883 in Cycles, and 10,310 are lit only in Cycles. The truth is between the two: the cutout shadow lets too much sun through, solid cards block too much, and neither a bias nor a threshold sits between them (see the table of the section above). The cause is still open; the lead is how much of a card Cycles blocks for a shadow ray, which needs a Cycles render with the leaf alpha changed to test.
+
## 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/crown-leak.py b/bench/reference/crown-leak.py
new file mode 100644
index 00000000..7ca33c55
--- /dev/null
+++ b/bench/reference/crown-leak.py
@@ -0,0 +1,73 @@
+# Counts the crown pixels of `foliage` that 67js lights with the sun and Cycles leaves in shadow.
+# uv run --no-project --with numpy --with pillow python bench/reference/crown-leak.py <work directory> <67js --ambient 0 png>
+# Needs <work directory>/foliage-cycles.npy (full light, to find the crown) and foliage-cycles-world0.npy (sun only, the true
+# shadow mask; render it with render-blender.py ... --world-strength 0 foliage). Prints the counts and writes leak.png beside the PNG:
+# red = leak, grey = lit on both sides, blue = lit only in Cycles, dark grey = the rest of the crown.
+import sys
+from pathlib import Path
+
+import numpy as np
+from PIL import Image
+
+GAMMA = 2.2
+CROWN_ROWS = 0.62
+CYCLES_SHADOWED_BELOW = 0.02
+GPU_LIT_ABOVE = 0.01
+SILHOUETTE_REACH = 2
+LUMINANCE_WEIGHTS = np.array([0.2126, 0.7152, 0.0722], np.float32)
+
+
+def tone_map_like_67js(linear):
+ """The curve of src/post.js, as in score.py (which cannot be imported: it scores on import). Returns display values."""
+ color = np.maximum(linear, 0)
+ mapped = np.clip((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), 0, 1)
+ return mapped ** (1 / GAMMA)
+
+
+def tone_mapped_luminance(display):
+ return (display ** GAMMA) @ LUMINANCE_WEIGHTS
+
+
+def near_any(mask, reach):
+ """True where a pixel of mask lies within reach pixels (a square window)."""
+ padded = np.pad(mask, reach)
+ grown = np.zeros_like(mask)
+ for row_shift in range(2 * reach + 1):
+ for column_shift in range(2 * reach + 1):
+ grown |= padded[row_shift:row_shift + mask.shape[0], column_shift:column_shift + mask.shape[1]]
+ return grown
+
+
+def main():
+ work_directory = Path(sys.argv[1])
+ gpu_path = Path(sys.argv[2])
+ cycles_full = tone_map_like_67js(np.load(work_directory / "foliage-cycles.npy"))
+ cycles_sun = tone_map_like_67js(np.load(work_directory / "foliage-cycles-world0.npy"))
+ gpu_sun = np.asarray(Image.open(gpu_path).convert("RGB"), dtype=np.float32) / 255
+
+ red, green, blue = cycles_full[..., 0], cycles_full[..., 1], cycles_full[..., 2]
+ upper_rows = np.zeros(green.shape, bool)
+ upper_rows[: int(green.shape[0] * CROWN_ROWS)] = True
+ crown = upper_rows & (((green >= red) & (green >= blue)) | (cycles_full.max(-1) < 0.25))
+
+ cycles_shadowed = crown & (tone_mapped_luminance(cycles_sun) < CYCLES_SHADOWED_BELOW)
+ cycles_lit = crown & ~cycles_shadowed
+ gpu_lit = crown & (tone_mapped_luminance(gpu_sun) > GPU_LIT_ABOVE)
+ leak = gpu_lit & cycles_shadowed
+ beside_lit_leaf = near_any(cycles_lit, SILHOUETTE_REACH)
+
+ print(f"crown pixels {crown.sum()}")
+ print(f"sun-lit: Cycles {cycles_lit.sum()}, 67js {gpu_lit.sum()}, both {(cycles_lit & gpu_lit).sum()}, Cycles only {(cycles_lit & ~gpu_lit).sum()}")
+ print(f"leak (67js lit, Cycles shadowed) {leak.sum()}")
+ print(f" within {SILHOUETTE_REACH} px of a pixel Cycles lights (silhouette and placement): {(leak & beside_lit_leaf).sum()}")
+ print(f" farther away (shadow that 67js does not cast): {(leak & ~beside_lit_leaf).sum()}")
+
+ picture = np.zeros(leak.shape + (3,), np.uint8)
+ picture[crown] = 60
+ picture[cycles_lit & gpu_lit] = (200, 200, 200)
+ picture[cycles_lit & ~gpu_lit] = (0, 120, 255)
+ picture[leak] = (255, 0, 0)
+ Image.fromarray(picture).save(gpu_path.with_name("leak.png"))
+
+
+main()
diff --git a/bench/reference/render-blender.py b/bench/reference/render-blender.py
index 19ea53ed..417cc48e 100644
--- a/bench/reference/render-blender.py
+++ b/bench/reference/render-blender.py
@@ -1,5 +1,7 @@
# Renders each described scene in Cycles and EEVEE, and saves linear pixels as .npy for the scorer.
-# blender -b --factory-startup --python bench/reference/render-blender.py -- <work directory>
+# blender -b --factory-startup --python bench/reference/render-blender.py -- <work directory> [scene ids]
+# ... -- <work directory> --world-strength <scale> [scene ids] renders Cycles only, with the world's light multiplied by <scale>
+# (0 leaves the sun and the point lights), into <id>-cycles-world<scale>.npy. The EEVEE render and the timings are left alone.
# Light units were measured on a white plane: sun strength equals irradiance, a point or spot light has 4 pi watts for each
# unit of 67js intensity, and an area light has pi watts for each unit.
import base64
@@ -369,7 +371,7 @@ def add_graph(material, principled, description):
# - 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.
+# - light from behind adds colour * max(dot(-N, L), 0) * 0.3 * keyColor / resolve, times the sun shadow (a Translucent BSDF is shadowed too).
# 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
@@ -842,6 +844,12 @@ def import_glb(root, glb_file):
bpy.ops.import_scene.gltf(filepath=os.path.join(root, glb_file))
+def scale_world_light(scene, scale):
+ """A measurement switch: the world's light times scale, like --ambient on the 67js side. The sky seen by the camera scales too."""
+ background = scene.world.node_tree.nodes["Background"]
+ background.inputs[1].default_value *= scale
+
+
def build_scene(description, root):
clear_scene()
scene_images[:] = description.get("images", [])
@@ -865,6 +873,11 @@ def build_scene(description, root):
def main():
arguments = sys.argv[sys.argv.index("--") + 1:]
work_directory = os.path.abspath(arguments[0])
+ world_scale = None
+ if "--world-strength" in arguments:
+ flag_index = arguments.index("--world-strength")
+ world_scale = float(arguments[flag_index + 1])
+ del arguments[flag_index:flag_index + 2]
only = set(arguments[1:])
root = os.getcwd()
timings = {}
@@ -876,6 +889,12 @@ def main():
with open(os.path.join(scene_directory, file_name), encoding="utf-8") as file:
description = json.load(file)
scene = build_scene(description, root)
+ if world_scale is not None:
+ scale_world_light(scene, world_scale)
+ configure_cycles(scene)
+ seconds = render_to_numpy(scene, work_directory, f"{scene_id}-cycles-world{world_scale:g}")
+ print(f"RENDERED {scene_id} with the world at {world_scale:g}: {seconds:.1f} s")
+ continue
configure_cycles(scene)
cycles_seconds = render_to_numpy(scene, work_directory, f"{scene_id}-cycles")
scene = build_scene(description, root)