3D engine that AI agents drive
Add the light emissive parts spill onto an open scene to the map bounce (part of #392)
Nguyen Xuan An4b07d1a53cd8
diff --git a/bench/reference/harness.js b/bench/reference/harness.jsindex d9451562..d6945ca3 100644--- a/bench/reference/harness.js+++ b/bench/reference/harness.js@@ -195,7 +195,7 @@ async function probeScene(scene, id, drawnParts, mode) {return { parts: placed.parts, report: placed.report, seconds: (performance.now() - started) / 1000 };}-async function renderReferenceScene(id, { describe, ambientOcclusion = 0, ambientOcclusionRadius = 0.5, bake = null, cascades = 0, backgroundOnly = false, probes = false, ambient = null, fill = null, key = null, viewTransform = null }) {+async function renderReferenceScene(id, { describe, ambientOcclusion = 0, ambientOcclusionRadius = 0.5, bake = null, cascades = 0, backgroundOnly = false, probes = false, ambient = null, fill = null, key = null, viewTransform = null, bloom = null }) {const scene = await SCENES[id]();// A view transform (agx, neutral) replaces the ACES fit in the shaders, as environment.viewTransform does in a recipe.if (viewTransform) scene.environment = { ...scene.environment, viewTransform };@@ -211,7 +211,7 @@ async function renderReferenceScene(id, { describe, ambientOcclusion = 0, ambienconst renderer = createSceneRenderer(backgroundOnly ? [] : withFloorSky(id, probed ? probed.parts : baked ? baked.parts : scene.parts), scene, cascades);renderer.resize();// Blender draws a still scene. The renderer's default wind (0.3) would move the leaf cards by up to four pixels at time 0.- const frame = { ...scene.camera, environment: scene.environment, time: 0, wind: 0, exposure: 1, effects: { ambientOcclusion, ambientOcclusionRadius } };+ const frame = { ...scene.camera, environment: scene.environment, time: 0, wind: 0, exposure: 1, effects: { ambientOcclusion, ambientOcclusionRadius, ...(bloom ? { bloom } : {}) } };renderer.render(frame);renderer.render(frame);const debug = renderer.context.getExtension('WEBGL_debug_renderer_info');@@ -223,6 +223,7 @@ async function renderReferenceScene(id, { describe, ambientOcclusion = 0, ambienbake: baked ? { seconds: baked.seconds, report: baked.report } : null,probes: probed ? { seconds: probed.seconds, report: probed.report } : null,floorSky: floorSkyReports[id] ?? null,+ bounce: scene.environment.map?.bounce ?? null,};renderer.dispose();return result;diff --git a/bench/reference/render-blender.py b/bench/reference/render-blender.pyindex a2b42bf1..3136ef92 100644--- a/bench/reference/render-blender.py+++ b/bench/reference/render-blender.py@@ -533,7 +533,8 @@ def build_material(name, description, has_vertex_colors):# 67js adds emissiveColor (the base colour when none is given) times emission to the pixel.emission_color = description.get("emissiveColor", description["color"])set_linear_color(principled.inputs["Emission Color"], emission_color)- principled.inputs["Emission Strength"].default_value = description["emission"]+ # NO_EMISSION=1 draws the scene without its emission, to measure how much of a Cycles picture is light from emissive parts (issue #392).+ principled.inputs["Emission Strength"].default_value = 0.0 if os.environ.get("NO_EMISSION") else description["emission"]if "emissiveMap" in description:add_emissive_map(material, principled, description, emission_color)if "graph" in description:diff --git a/bench/reference/render-gpu.mjs b/bench/reference/render-gpu.mjsindex ee2bce71..222dabef 100644--- a/bench/reference/render-gpu.mjs+++ b/bench/reference/render-gpu.mjs@@ -1,7 +1,8 @@// Draws chosen reference scenes with 67js on the GPU only, into a directory of your choice, with measurement switches. No Blender, no scoring.// node bench/reference/render-gpu.mjs <output directory> <scene id>,<scene id> [--ambient <scale>] [--fill <scale>] [--key <scale>]-// [--ambient-occlusion <strength>] [--ambient-occlusion-radius <units>] [--reversed-depth] [--cpu]+// [--bloom <strength,threshold,radius>] [--ambient-occlusion <strength>] [--ambient-occlusion-radius <units>] [--reversed-depth] [--cpu]// Files are <output directory>/<scene id>-gpu.png, the names measure-depth.py and score.py read. Used to find what makes shadows light (issue #197).+// --bloom <strength,threshold,radius> draws with that bloom; --bake-preset <draft|standard|fine> bakes as a recipe does (and prints the refusal, e.g. over 256 parts); a scene whose map has a bounce colour prints it.import { mkdir, writeFile } from 'node:fs/promises';import { constants, setPriority } from 'node:os';import { resolve } from 'node:path';@@ -20,6 +21,18 @@ const waterIndex = argumentsGiven.indexOf('--water');const waterOption = waterIndex >= 0 ? argumentsGiven[waterIndex + 1] : null;const reversedDepth = argumentsGiven.includes('--reversed-depth');+function bakePresetOption() {+ const index = argumentsGiven.indexOf('--bake-preset');+ return index >= 0 ? { preset: argumentsGiven[index + 1] } : null;+}++function bloomOption() {+ const index = argumentsGiven.indexOf('--bloom');+ if (index < 0) return null;+ const [strength, threshold, radius] = argumentsGiven[index + 1].split(',').map(Number);+ return { strength, threshold, radius };+}+function numberAfter(flag) {const index = argumentsGiven.indexOf(flag);return index >= 0 ? Number(argumentsGiven[index + 1]) : null;@@ -31,6 +44,8 @@ const options = {key: numberAfter('--key'),ambientOcclusion: numberAfter('--ambient-occlusion') ?? 0,ambientOcclusionRadius: numberAfter('--ambient-occlusion-radius') ?? 0.5,+ bloom: bloomOption(),+ bake: bakePresetOption(),};setPriority(0, constants.priority.PRIORITY_BELOW_NORMAL);@@ -48,6 +63,8 @@ try {const result = await page.evaluate(([sceneId, renderOptions]) => window.renderReferenceScene(sceneId, { describe: false, ...renderOptions }), [id, options]);if (result.glError !== 0) throw new Error(`${id} gave WebGL error ${result.glError}.`);await writeFile(`${outputDirectory}/${id}-gpu.png`, Buffer.from(result.image, 'base64'));+ if (result.bounce) console.log('bounce', JSON.stringify(result.bounce));+ if (result.bake) console.log(JSON.stringify(result.bake).slice(0, 600));console.log(`${id} drawn`);}} finally {diff --git a/changes/392.md b/changes/392.mdnew file mode 100644index 00000000..8a49eb9d--- /dev/null+++ b/changes/392.md@@ -0,0 +1 @@+- Add the light that emissive parts send to the rest of an open scene to the preview environment (`groundBounceEnvironment`, issue #392). Their emitted power, per colour channel, over the area of the scene without its ground, times the mean albedo over one minus it, is added to the map's `bounce` colour, so windows and lamps warm the facades and streets around them with no per-frame or shader cost. A closed room, a baked scene, a scene with only mapped emission or with next to no emission is unchanged, and `environment.emissiveSpill: false` turns it off. On `megacity-dusk` (SwiftShader against a fresh Cycles render) FLIP goes from 0.1580 to 0.1364 and the facade slices of the picture from 0.75-0.77 to 0.96-1.07 of Cycles; the seven other scenes checked draw the same pixels.diff --git a/docs/lighting.md b/docs/lighting.mdindex dd1e9c22..f3f77994 100644--- a/docs/lighting.md+++ b/docs/lighting.md@@ -119,6 +119,8 @@ A closed room lit by point, spot or area lights gets the light its walls bounceIt is left out when the scene has a bake (its light map holds the bounce), a sun (`keyColor` above zero or unset, as in `temple-hall` and `dark-shaft`, which already read lighter than Cycles in their shadows), no map, or `environment.roomBounce` is `false`. It is one average colour for the whole room, with no brighter corner beside a lamp and no colour bleeding next to a wall; a bake gives those, and `scene_inspect` names it (`closed-room-without-bake`, with the `standard` preset) for a closed room lit by lamps only.+Emissive parts light their surroundings too (issue #392). In an open scene without a bake, `groundBounceEnvironment` adds their emitted power (emissive colour times `emission` times area, per channel) over the area of everything but the ground, times the mean albedo over one minus it, times a gain of 1.6, to the same `bounce` colour. Mapped emission, a closed room and a faint emitter (under 0.002) add nothing, and `environment.emissiveSpill: false` turns it off. On `megacity-dusk` against a Cycles render with its own emission on, the facades are 0.96 to 1.07 of Cycles' luminance where they were 0.75 to 0.77, and FLIP goes from 0.1580 to 0.1364 (SwiftShader). One colour for the whole scene cannot follow the fall-off from a window, so the darkest fifth of the picture ends 1.4 times too light; a per-position grid would fix that.+Luminance of the 67js picture over Cycles (linear, whole frame and 20th percentile, `bench/reference/luminance-ratio.py`, GPU renders against the committed Cycles panels):| Scene | Mean before | Mean after | p20 before | p20 after |diff --git a/src/ground-bounce.js b/src/ground-bounce.jsindex 858c2f96..cc5925c1 100644--- a/src/ground-bounce.js+++ b/src/ground-bounce.js@@ -64,9 +64,19 @@ function groundAlbedo(material) {* texture's albedo is its mean in linear space times the material colour.*/export function findGroundSurface(parts) {- if (parts.length < 2) {+ const index = findGroundIndex(parts)+ if (index < 0) {return null}+ const albedo = groundAlbedo(parts[index].material)+ return albedo ? { albedo } : null+}++// The index of the lowest, thin part that spans the scene, or -1.+function findGroundIndex(parts) {+ if (parts.length < 2) {+ return -1+ }const boundsList = parts.map(part => worldBounds(part))const scene = sceneBoundsOf(boundsList)const widthX = scene.maximum[0] - scene.minimum[0]@@ -79,11 +89,10 @@ export function findGroundSurface(parts) {const isThin = partHeight <= GROUND_THINNESS * Math.max(partX, partZ)const isLowest = bounds.minimum[1] <= scene.minimum[1] + GROUND_LOWEST_TOLERANCEif (spansScene && isThin && isLowest) {- const albedo = groundAlbedo(parts[index].material)- return albedo ? { albedo } : null+ return index}}- return null+ return -1}/**@@ -322,6 +331,99 @@ function hasLocalLights(environment) {* `environment.groundBounce: false` turns it off.*/export function groundBounceEnvironment(parts, environment, { automaticAlbedo = null } = {}) {+ const bounced = surfaceBounceEnvironment(parts, environment, automaticAlbedo)+ return bounced.map ? emissiveSpillEnvironment(parts, bounced) : bounced+}++// Emissive parts of an open scene light the surfaces around them: windows light facades, lamps light the street. Their+// emitted power, radiance L over area A, is exitance pi * L, so the light that reaches an average surface of the scene after+// one bounce is L * A_emissive * rho / (A * (1 - rho)) in the unit of an environment map's diffuse light, as in the room term.+// The open sky takes most of it, which the gain stands for (fitted on `megacity-dusk` against Cycles with its emission set to 0+// and on, and checked on the other reference scenes). A closed room or a baked scene has this light already, and a scene with+// next to no emission keeps its environment.+const SPILL_GAIN = 1.6+const SPILL_MINIMUM = 2e-3++function surfaceArea(part) {+ const { positions, indices } = part.geometry+ if (!positions) {+ return 0+ }+ const count = indices ? indices.length : positions.length / 3+ const first = [0, 0, 0]+ const second = [0, 0, 0]+ const third = [0, 0, 0]+ let area = 0+ for (let corner = 0; corner + 2 < count; corner += 3) {+ transformPoint(part.matrix, positions, indices ? indices[corner] : corner, first)+ transformPoint(part.matrix, positions, indices ? indices[corner + 1] : corner + 1, second)+ transformPoint(part.matrix, positions, indices ? indices[corner + 2] : corner + 2, third)+ const normalX = (second[1] - first[1]) * (third[2] - first[2]) - (second[2] - first[2]) * (third[1] - first[1])+ const normalY = (second[2] - first[2]) * (third[0] - first[0]) - (second[0] - first[0]) * (third[2] - first[2])+ const normalZ = (second[0] - first[0]) * (third[1] - first[1]) - (second[1] - first[1]) * (third[0] - first[0])+ area += 0.5 * Math.hypot(normalX, normalY, normalZ)+ }+ return area+}++/**+ * The light the emissive parts of an open scene send to the rest of it, as a linear colour in the unit of an environment+ * map's diffuse light, or null: none of it for a scene with a bake, a closed room, no emissive part or a negligible one.+ */+export function emissiveSpill(parts) {+ if (isDrawnWithBakedLight(parts) || isClosedRoom(parts)) {+ return null+ }+ const emitted = [0, 0, 0]+ const reflected = [0, 0, 0]+ let totalArea = 0+ const groundIndex = findGroundIndex(parts)+ for (const [index, part] of parts.entries()) {+ const material = part.material ?? {}+ if (index === groundIndex || material.alphaMode === 'blend' || !part.geometry?.positions) {+ continue+ }+ const area = surfaceArea(part)+ totalArea += area+ if (material.emission > 0 && !material.emissiveMap) {+ const color = material.emissiveColor ?? material.color ?? [1, 1, 1]+ for (let channel = 0; channel < 3; channel++) {+ emitted[channel] += color[channel] * material.emission * area+ }+ } else {+ const albedo = partDiffuseAlbedo(material)+ for (let channel = 0; channel < 3; channel++) {+ reflected[channel] += albedo[channel] * area+ }+ }+ }+ if (totalArea === 0) {+ return null+ }+ const spill = emitted.map((power, channel) => {+ const albedo = Math.min(reflected[channel] / totalArea, ROOM_MAXIMUM_ALBEDO)+ return SPILL_GAIN * power * albedo / (totalArea * (1 - albedo))+ })+ return spill.some(channel => channel >= SPILL_MINIMUM) ? spill : null+}++const spillOfParts = new WeakMap()++function emissiveSpillEnvironment(parts, environment) {+ if (environment.emissiveSpill === false) {+ return environment+ }+ const remembered = spillOfParts.get(parts)+ const spill = remembered?.length === parts.length ? remembered.spill : emissiveSpill(parts)+ spillOfParts.set(parts, { length: parts.length, spill })+ if (!spill) {+ return environment+ }+ const bounce = environment.map.bounce ? environment.map.bounce.map((channel, index) => channel + spill[index]) : spill+ return { ...environment, map: { ...environment.map, bounce, irradianceWithBounce } }+}++function surfaceBounceEnvironment(parts, environment, automaticAlbedo) {if (!environment.map) {return environment}diff --git a/tests/ground-bounce.test.js b/tests/ground-bounce.test.jsindex 34abf761..99758a59 100644--- a/tests/ground-bounce.test.js+++ b/tests/ground-bounce.test.js@@ -1,7 +1,7 @@import test from 'node:test';import assert from 'node:assert/strict';import { createEnvironmentMap, computeIrradianceCoefficients, evaluateIrradiance } from '../src/environment.js';-import { findGroundSurface, groundBounceEnvironment, withGroundBounce } from '../src/ground-bounce.js';+import { emissiveSpill, findGroundSurface, groundBounceEnvironment, withGroundBounce } from '../src/ground-bounce.js';const WIDTH = 8;const HEIGHT = 4;@@ -143,3 +143,25 @@ test('the mean of a texture is found once per image', () => {map.image.data.fill(0);assert.ok(findGroundSurface(openScene({ ...GRASS, map })).albedo[0] > 0.1);});++const LAMP_WINDOW = { tag: 'surface', color: [1, 0.7, 0.35], emission: 4 };++test('emissive parts of an open scene add their spill to the map bounce, tinted by their colour', () => {+ const environment = environmentOf(uniformSky(0.5));+ const lit = [...openScene(), slab('windows', [0, 2, 1], [4, 2, 0.2], LAMP_WINDOW)];+ const spill = emissiveSpill(lit);+ assert.ok(spill[0] > spill[1] && spill[1] > spill[2] && spill[2] > 0);+ const bounced = groundBounceEnvironment(lit, environment);+ assert.deepEqual(bounced.map.bounce, spill);+ assert.ok(bounced.map.irradianceSource);+});++test('a scene with no emission, a faint one, a mapped one or emissiveSpill false keeps its map bounce', () => {+ const environment = environmentOf(uniformSky(0.5));+ assert.equal(emissiveSpill(openScene()), null);+ assert.equal(groundBounceEnvironment(openScene(), environment).map.bounce, undefined);+ assert.equal(emissiveSpill([...openScene(), slab('faint', [0, 2, 1], [1, 1, 0.1], { ...LAMP_WINDOW, emission: 0.001 })]), null);+ assert.equal(emissiveSpill([...openScene(), slab('sign', [0, 2, 1], [4, 2, 0.2], { ...LAMP_WINDOW, emissiveMap: {} })]), null);+ const lit = [...openScene(), slab('windows', [0, 2, 1], [4, 2, 0.2], LAMP_WINDOW)];+ assert.equal(groundBounceEnvironment(lit, { ...environment, emissiveSpill: false }).map.bounce, undefined);+});