3D engine that AI agents drive
Split a hall into light components on both sides (issue #543)
Nguyen Xuan Andea23a93735b
diff --git a/bench/reference/light-components.py b/bench/reference/light-components.pynew file mode 100644index 00000000..4da212ea--- /dev/null+++ b/bench/reference/light-components.py@@ -0,0 +1,165 @@+# Splits a hall into light components on both sides and prints one table per hall (issue #543).+# uv run --no-project --with openexr --with numpy --with pillow python -I bench/reference/light-components.py <passes directory>... -- <variants directory> <out.md>+# Cycles side: <id>-passes.exr from render-blender.py --passes, in linear radiance. 67js side: <variants>/<label>/<id>.png, drawn by+# artifacts/543/variants.sh, turned back into linear radiance by inverting the display fit (tone map and gamma 2.2).+# Components are mean linear luminance over the frame. 67js has no light passes, so its components are differences between renders:+# haze = vE - vE-nofog, sun = vE - vE-nosun, and what is left with sun and haze off is the sky and the bounced light.+import json+import sys+from pathlib import Path++import numpy as np+import OpenEXR+from PIL import Image++GAMMA = 2.2+LUMINANCE = np.array([0.2126, 0.7152, 0.0722], dtype=np.float32)+WIDTH = 960+HEIGHT = 540+HALLS = {+ "temple-hall": "temple-hall",+ "temple-hall-a": "temple-hall-a",+ "temple-hall-b": "temple-hall-b",+ "temple-hall-c": "temple-hall-c",+ "dark-shaft": "dark-shaft",+}+++def tone_map(linear):+ color = np.maximum(linear, 0)+ return np.clip((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), 0, 1) ** (1 / GAMMA)+++_SAMPLES = np.concatenate([[0], np.geomspace(1e-5, 60, 4000)]).astype(np.float64)+_DISPLAY = tone_map(_SAMPLES)+++def to_linear(display):+ """The inverse of the display fit; a clipped pixel (1.0) maps to the largest value the fit reaches."""+ return np.interp(display, _DISPLAY, _SAMPLES).astype(np.float32)+++def load_png_linear(path):+ display = np.asarray(Image.open(path).convert("RGB"), dtype=np.float32) / 255+ return to_linear(display)+++def read_passes(path):+ exr = OpenEXR.File(str(path))+ # Blender writes one EXR part for each pass, named ViewLayer.<pass name>.+ result = {}+ for part in exr.parts:+ for key, channel in part.channels.items():+ result[key.split(".", 1)[-1]] = np.asarray(channel.pixels, dtype=np.float32)[:, :, :3]+ return result+++def luminance_mean(image, mask=None):+ values = image @ LUMINANCE+ return float(values.mean() if mask is None else values[mask].mean()) if (mask is None or mask.any()) else float("nan")+++def cycles_components(passes):+ names = {key.lower(): key for key in passes}+ def get(*candidates):+ for candidate in candidates:+ if candidate.lower() in names:+ return passes[names[candidate.lower()]]+ return np.zeros((HEIGHT, WIDTH, 3), dtype=np.float32)+ diffuse_direct = get("DiffDir", "Diffuse Direct", "DiffuseDirect")+ diffuse_indirect = get("DiffInd", "Diffuse Indirect", "DiffuseIndirect")+ diffuse_color = get("DiffCol", "Diffuse Color", "DiffuseColor")+ glossy_direct = get("GlossDir", "Glossy Direct")+ glossy_indirect = get("GlossInd", "Glossy Indirect")+ glossy_color = get("GlossCol", "Glossy Color")+ return {+ "diffuse direct (sun on surfaces)": diffuse_direct * diffuse_color,+ "diffuse indirect (sky + bounces)": diffuse_indirect * diffuse_color,+ "glossy": (glossy_direct + glossy_indirect) * glossy_color,+ "haze direct": get("VolumeDir", "Volume Direct", "VolumeDirect"),+ "haze indirect": get("VolumeInd", "Volume Indirect", "VolumeIndirect"),+ "emission": get("Emit", "Emission"),+ "sky seen": get("Env", "Environment"),+ "combined": get("Combined", "Composite"),+ }+++def regions(components):+ sun = components["diffuse direct (sun on surfaces)"] @ LUMINANCE+ sky = components["sky seen"] @ LUMINANCE+ combined = components["combined"] @ LUMINANCE+ haze = (components["haze direct"] + components["haze indirect"]) @ LUMINANCE+ surface = sky < 0.02+ return {+ "frame": np.ones_like(sun, dtype=bool),+ "surface in sun": surface & (sun > 0.1),+ "surface in shade": surface & (sun < 0.01) & (haze < 0.5 * combined),+ "haze-dominated air": haze > 0.5 * np.maximum(combined, 1e-6),+ "sky": ~surface,+ }+++def main():+ separator = sys.argv.index("--")+ pass_directories = [Path(item) for item in sys.argv[1:separator]]+ variants = Path(sys.argv[separator + 1])+ output = Path(sys.argv[separator + 2])+ lines = ["# Light components, Cycles vs 67js (issue #543)", "", "Cycles components from render-blender.py (--passes, --no-fog, --sun-strength 0); 67js from the renders of artifacts/543/variants.sh.", ""]+ for hall, passes_name in HALLS.items():+ path = next((directory / f"{passes_name}-passes.exr" for directory in pass_directories if (directory / f"{passes_name}-passes.exr").exists()), None)+ if path is None:+ continue+ passes = read_passes(path)+ cycles = cycles_components(passes)+ label = {}+ for variant in ("main", "vE", "vE-nofog", "vE-nosun", "vE-nosun-nofog", "main-nofog", "main-nosun", "main-nosun-nofog"):+ variant_path = variants / variant / f"{hall}.png"+ if variant_path.exists():+ label[variant] = load_png_linear(variant_path)+ cycles_directory = next(directory for directory in pass_directories if (directory / f"{passes_name}-passes.exr").exists())+ cycles_variant = {+ "total": cycles["combined"],+ "nofog": np.load(cycles_directory / f"{hall}-cycles-nofog.npy"),+ "nosun": np.load(cycles_directory / f"{hall}-cycles-sun0.npy"),+ "base": np.load(cycles_directory / f"{hall}-cycles-nofog-sun0.npy"),+ }+ js_variant = {+ "vE": {"total": label.get("vE"), "nofog": label.get("vE-nofog"), "nosun": label.get("vE-nosun"), "base": label.get("vE-nosun-nofog")},+ "main": {"total": label.get("main"), "nofog": label.get("main-nofog"), "nosun": label.get("main-nosun"), "base": label.get("main-nosun-nofog")},+ }+ def split(variant):+ if any(variant.get(key) is None for key in ("total", "nofog", "nosun", "base")):+ return None+ total, nofog, nosun, base = (variant[key] for key in ("total", "nofog", "nosun", "base"))+ return {+ "total": total,+ "sky + bounces + emission (no sun, no haze)": base,+ "sun on surfaces (haze off)": nofog - base,+ "haze with the sun off (net of its extinction)": nosun - base,+ "sun x haze (the rest)": total - nofog - nosun + base,+ }+ cycles_split = split(cycles_variant)+ vE_split = split(js_variant["vE"])+ main_split = split(js_variant["main"])+ lines += [f"## {hall}", "", "Mean linear luminance. Each row is a difference between renders, on both sides.", "", "| component | Cycles | 67js main | 67js vE | main / Cycles | vE / Cycles |", "|---|---|---|---|---|---|"]+ for name in cycles_split:+ cycles_value = luminance_mean(cycles_split[name])+ main_value = luminance_mean(main_split[name]) if main_split else float("nan")+ fog_value = luminance_mean(vE_split[name]) if vE_split else float("nan")+ lines.append(f"| {name} | {cycles_value:.4f} | {main_value:.4f} | {fog_value:.4f} | {main_value / cycles_value:.2f} | {fog_value / cycles_value:.2f} |")+ lines += ["", "Cycles light passes (frame mean; haze passes count in-scatter only):", "", "| pass | mean |", "|---|---|"]+ for name, image in cycles.items():+ lines.append(f"| {name} | {luminance_mean(image):.4f} |")+ masks = regions(cycles)+ lines += ["", "Regions (masks from Cycles; the same pixels on both sides), mean linear luminance:", "", "| region | pixels | Cycles | 67js main | 67js vE | vE / Cycles | main / Cycles |", "|---|---|---|---|---|---|---|"]+ for name, mask in masks.items():+ cycles_value = luminance_mean(cycles["combined"], mask)+ main_value = luminance_mean(label["main"], mask) if "main" in label else float("nan")+ fog_value = luminance_mean(label["vE"], mask) if "vE" in label else float("nan")+ lines.append(f"| {name} | {mask.mean():.1%} | {cycles_value:.4f} | {main_value:.4f} | {fog_value:.4f} | {fog_value / cycles_value:.2f} | {main_value / cycles_value:.2f} |")+ lines.append("")+ output.write_text("\n".join(lines), encoding="utf-8")+ print("\n".join(lines))+++main()diff --git a/bench/reference/render-blender.py b/bench/reference/render-blender.pyindex 02372e5e..bcccea49 100644--- a/bench/reference/render-blender.py+++ b/bench/reference/render-blender.py@@ -4,6 +4,7 @@# (0 leaves the sun and the point lights), into <id>-cycles-world<scale>.npy. The EEVEE render and the timings are left alone.# ... -- <work directory> --sun-strength <scale> [scene ids] the same for the sun: Cycles only, the sun times <scale> (0 leaves the world and# the point lights), into <id>-cycles-sun<scale>.npy. Both switches together write <id>-cycles-world<a>-sun<b>.npy.+# ... -- <work directory> --no-fog [scene ids] Cycles only, without the fog volume (combines with the switches above), into <id>-cycles-nofog....npy (issue #543).# ... -- <work directory> --view-transform <AgX|Khronos PBR Neutral|...> [scene ids] Cycles only, with that Blender view transform, into <id>-cycles-<name>.png (16-bit sRGB).# ... -- <work directory> --max-bounces <n> [--floor-color <v>] [scene ids] Cycles only, with at most n light bounces (0 is direct light only)# and/or the part named "floor" painted a grey of v (0 is black), into <id>-cycles-bounces<n>-floor<v>.npy. They show how much light the ground bounces.@@ -901,6 +902,28 @@ def render_with_view_transform(scene, work_directory, name, view_transform):return time.perf_counter() - started+def render_passes(scene, work_directory, name):+ """A measurement switch: one undenoised Cycles render whose light components are kept apart in a multilayer EXR (issue #543)."""+ scene.cycles.use_denoising = False+ layer = scene.view_layers[0]+ layer.use_pass_diffuse_direct = True+ layer.use_pass_diffuse_indirect = True+ layer.use_pass_diffuse_color = True+ layer.use_pass_glossy_direct = True+ layer.use_pass_glossy_indirect = True+ layer.use_pass_glossy_color = True+ layer.use_pass_emit = True+ layer.use_pass_environment = True+ layer.cycles.use_pass_volume_direct = True+ layer.cycles.use_pass_volume_indirect = True+ scene.render.image_settings.media_type = "MULTI_LAYER_IMAGE"+ scene.render.image_settings.file_format = "OPEN_EXR_MULTILAYER"+ scene.render.filepath = os.path.join(work_directory, f"{name}.exr")+ started = time.perf_counter()+ bpy.ops.render.render(write_still=True)+ return time.perf_counter() - started++def import_glb(root, glb_file):"""A Khronos sample asset goes through Blender's own glTF importer, so Cycles shades the file as its authors meant."""bpy.ops.import_scene.gltf(filepath=os.path.join(root, glb_file))@@ -1045,6 +1068,12 @@ def main():print_materials = "--print-materials" in argumentsif print_materials:arguments.remove("--print-materials")+ no_fog = "--no-fog" in arguments+ if no_fog:+ arguments.remove("--no-fog")+ passes = "--passes" in arguments+ if passes:+ arguments.remove("--passes")view_transform = Noneif "--view-transform" in arguments:flag_index = arguments.index("--view-transform")@@ -1068,6 +1097,11 @@ def main():render_material_ids(scene, work_directory, f"{scene_id}-materials")print(f"RENDERED {scene_id} material ids")continue+ if passes:+ configure_cycles(scene)+ seconds = render_passes(scene, work_directory, f"{scene_id}-passes")+ print(f"RENDERED {scene_id} as cycles passes: {seconds:.1f} s")+ continueif no_glossy:remove_glossy(scene)configure_cycles(scene)@@ -1079,8 +1113,11 @@ def main():seconds = render_with_view_transform(scene, work_directory, f"{scene_id}-cycles-{view_transform.lower().replace(' ', '-')}", view_transform)print(f"RENDERED {scene_id} as cycles with {view_transform}: {seconds:.1f} s")continue- if world_scale is not None or sun_scale is not None or bounces is not None or floor_grey is not None:+ if world_scale is not None or sun_scale is not None or bounces is not None or floor_grey is not None or no_fog:suffix = ""+ if no_fog and "Fog" in bpy.data.objects:+ bpy.data.objects.remove(bpy.data.objects["Fog"])+ suffix += "-nofog"if bounces is not None:suffix += f"-bounces{bounces}"if floor_grey is not None:diff --git a/bench/reference/run.mjs b/bench/reference/run.mjsindex 4e76eb42..305837ac 100644--- a/bench/reference/run.mjs+++ b/bench/reference/run.mjs@@ -103,6 +103,11 @@ const cascadesFlag = argumentsGiven.indexOf('--cascades');const cascades = cascadesFlag >= 0 ? Number(argumentsGiven[cascadesFlag + 1]) : 0;const ambientFlag = argumentsGiven.indexOf('--ambient');const ambient = ambientFlag >= 0 ? Number(argumentsGiven[ambientFlag + 1]) : null;+// --key <scale> multiplies the sun and --fog-density <density> replaces the volume fog's density (0 removes the haze): measurement switches that split a picture into its light components (issue #543).+const keyFlag = argumentsGiven.indexOf('--key');+const keyScale = keyFlag >= 0 ? Number(argumentsGiven[keyFlag + 1]) : null;+const fogDensityFlag = argumentsGiven.indexOf('--fog-density');+const fogDensity = fogDensityFlag >= 0 ? { density: Number(argumentsGiven[fogDensityFlag + 1]) } : null;const pathTracerFlag = argumentsGiven.includes('--path-tracer');const samplesFlag = argumentsGiven.indexOf('--samples');const pathTracerSamples = samplesFlag >= 0 ? Number(argumentsGiven[samplesFlag + 1]) : 64;@@ -118,7 +123,7 @@ const sceneIdsGiven = lookIds ?? (sceneFlag >= 0 ? argumentsGiven[sceneFlag + 1]// The fresh bake.json lands in the work directory and is checked against the committed one (compare-scores.mjs); --update-baseline replaces// the committed bake.json and images/<id>-baked.jpg with it.const bakeScoreRun = bakedSceneIds !== null && !sceneSet && !pathTracerFlag;-const bakeVariantFlags = ['--no-glossy', '--sky-only', '--bake-preset', '--bake-grid', '--bake-grid-fog', '--no-bake-worker', '--bake-bounces', '--bake-backend', '--bake-samples', '--bake-density', '--probes', '--ambient', '--ambient-occlusion', '--cascades', '--no-object-probes', '--object-probes', '--no-water', '--ssr-water', '--no-ground-bounce', '--floor-sky', '--floor-sky-rooms', '--no-floor-sky'];+const bakeVariantFlags = ['--no-glossy', '--sky-only', '--bake-preset', '--bake-grid', '--bake-grid-fog', '--no-bake-worker', '--bake-bounces', '--bake-backend', '--bake-samples', '--bake-density', '--probes', '--ambient', '--key', '--fog-density', '--ambient-occlusion', '--cascades', '--no-object-probes', '--object-probes', '--no-water', '--ssr-water', '--no-ground-bounce', '--floor-sky', '--floor-sky-rooms', '--no-floor-sky'];const bakeScoreIsBaseline = bakeScoreRun && argumentsGiven.includes('--update-baseline') && !bakeVariantFlags.some(flag => argumentsGiven.includes(flag));const onlyScenes = sceneIdsGiven ?? (bakeScoreRun ? bakedSceneIds : null);const skipCpu = skipCpuFlag || bakeScoreRun || Boolean(viewTransform);@@ -149,7 +154,7 @@ async function renderBackend(backend, serverUrl, graphics) {const objectProbesThisScene = !argumentsGiven.includes('--no-object-probes') && (objectProbeSceneIds === null || objectProbeSceneIds.includes(id));const probes = probesThisScene || (objectProbesThisScene ? 'objects' : false);const bake = bakesThisScene ? { ...skyOnlyOptions, ...(bakePreset ? { preset: bakePreset } : {}), ...(argumentsGiven.includes('--bake-grid') ? { irradianceGrid: Number(argumentsGiven[argumentsGiven.indexOf('--bake-grid') + 1]) || true } : {}), ...(argumentsGiven.includes('--bake-grid-fog') ? { gridFog: true } : {}), ...(argumentsGiven.includes('--bake-bounces') ? { bounces: Number(argumentsGiven[argumentsGiven.indexOf('--bake-bounces') + 1]) } : {}), ...(bakeBackend ? { backend: bakeBackend } : {}), ...(bakeSamples ? { samples: bakeSamples } : {}), ...(bakeDensity ? { texelsPerUnit: bakeDensity } : {}) } : null;- const result = await page.evaluate(([sceneId, shouldDescribe, ambientOcclusion, ambientOcclusionRadius, bakeOptions, cascadeCount, withProbes, ambientScale, selectedViewTransform]) => window.renderReferenceScene(sceneId, { describe: shouldDescribe, ambientOcclusion, ambientOcclusionRadius, bake: bakeOptions, cascades: cascadeCount, probes: withProbes, ambient: ambientScale, viewTransform: selectedViewTransform }), [id, describe, ambientOcclusion, ambientOcclusionRadius, bake, cascades, probes, ambient, viewTransform]);+ const result = await page.evaluate(([sceneId, shouldDescribe, ambientOcclusion, ambientOcclusionRadius, bakeOptions, cascadeCount, withProbes, ambientScale, selectedViewTransform, keyScaleGiven, fogGiven]) => window.renderReferenceScene(sceneId, { describe: shouldDescribe, ambientOcclusion, ambientOcclusionRadius, bake: bakeOptions, cascades: cascadeCount, probes: withProbes, ambient: ambientScale, viewTransform: selectedViewTransform, key: keyScaleGiven, volumeFog: fogGiven }), [id, describe, ambientOcclusion, ambientOcclusionRadius, bake, cascades, probes, ambient, viewTransform, keyScale, fogDensity]);if (result.glError !== 0) throw new Error(`${id} on ${backend} gave WebGL error ${result.glError}.`);graphics[backend] = result.graphics;await writeFile(`${WORK_DIRECTORY}/${id}-${backend}.png`, Buffer.from(result.image, 'base64'));