Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Bake graph normal maps at the colour resolution again (part of #50)

Nguyen Xuan An4c0c6f8f5324
diff --git a/bench/reference/README.md b/bench/reference/README.md
index 32bb4e76..802ad58b 100644
--- a/bench/reference/README.md
+++ b/bench/reference/README.md
@@ -1733,3 +1733,7 @@ The world scale is the exact match to Blender and scores worse, and there is no
Where the rest is, measured from the 2048 texel render by the sign of the bump slope at each pixel centre (display luminance, 67js minus Cycles): ledges that tilt up towards the sun are +0.046 and +0.057 too bright, ledges that tilt down -0.078 (gentle) and -0.022 (steep) too dark, and the vertical joints are +0.03 too bright on both sides. EEVEE is 0.27 off Cycles on every wall pixel, flat or not, because its flat wall is darker; 67js is 0.057 on the flat wall and 0.146 to 0.196 on the pixels at or next to a bump edge (0.0705 render; 0.0576 for the same pixels of `brick-flat`), which is where its excess sits. Which side is "up-tilted" is read from the sign of the slope (inferred). Cause (inferred, not isolated): Cycles removes the part of the lobe that falls below the geometric plane, which for an up-tilted ledge is the bright upper sky and for a down-tilted one the dark lower half, while the shader scales the whole sky term by one share. Moving the sky lookup towards the geometric normal helped by 0.0009 at best (table) and moved the render away from EEVEE, so it was not kept; it would also add to `scene:lit-box`.
After the change: `brick-graph` 0.0714 on the GPU (CPU render not redone), `brick-flat` 0.0556, excess 0.0158 against EEVEE's 0.0120. The first Done-when box of #50 stays open by 0.0038. Only `brick-graph` reads a graph with a height output among the suite's scenes (no other scene has a normal map from a graph), so no other scene's pixels changed and the full suite was not run.
+
+### Normal map resolution reverted (2026-11-04, issue #50)
+
+The twice-resolution normal bake above is reverted as a default: 0.0020 of FLIP on `brick-graph` did not justify 4 times the normal map memory per graph pattern. A graph normal map bakes at the colour resolution again (`brick-graph` 0.0734).
diff --git a/changes/50-bump.md b/changes/50-bump.md
index ae089cc4..d903a288 100644
--- a/changes/50-bump.md
+++ b/changes/50-bump.md
@@ -1 +1 @@
-- Bake the normal map of a graph material at twice the colour resolution, up to 1024 texels (issue #50). A joint edge a few texels wide lost tilt to the bilinear blend of unit normals, 3.5 degrees on average against Blender's Bump node; at 1024 texels it is 2.1. `brick-graph` goes from 0.0734 to 0.0714 against Cycles (FLIP, RTX 3060); a graph whose height is too costly to read four times per texel at that size keeps the colour resolution. The map holds 4 times the texels (5.6 MB with mips for a 512 pattern, 1.4 MB before) and the bake takes about 4 times longer (0.35 s for the reference brick wall).
+- Graph normal maps bake at the colour resolution again. The twice-resolution bake from #401 brought `brick-graph` from 0.0734 to 0.0714 against Cycles, but it cost 4 times the texels (5.6 MB against 1.4 MB with mips for a 512 pattern), which is not worth the gain as a default. The measurement stays in `bench/reference/README.md`.
diff --git a/changes/50-bump.size.md b/changes/50-bump.size.md
deleted file mode 100644
index 5d03649a..00000000
--- a/changes/50-bump.size.md
+++ /dev/null
@@ -1 +0,0 @@
-- scene:agent-scene+lazy: the graph bake picks a finer normal map size (per-output cost, resolution and work allowance), 112 bytes in the lazy graph code; scene:lit-box and scene:agent-scene do not grow
diff --git a/src/material-graph.js b/src/material-graph.js
index ee70a2fe..96b81a1e 100644
--- a/src/material-graph.js
+++ b/src/material-graph.js
@@ -377,14 +377,11 @@ export function compileMaterialGraph(graph) {
if (names.length === 0) throw graphError(`graph needs at least one output: ${MATERIAL_GRAPH_OUTPUTS.join(', ')}.`);
const state = { nodeCount: 0, cost: 0 };
const outputs = {};
- const outputCosts = {};
for (const name of names) {
- const costBefore = state.cost;
outputs[name] = compileNode(graph[name], state, 0);
- outputCosts[name] = state.cost - costBefore;
if (SCALAR_OUTPUTS.includes(name) && outputs[name].kind !== 'scalar') throw graphError(`${name} needs a number, not a vector.`);
}
- return { outputs, outputCosts, nodeCount: state.nodeCount, cost: state.cost };
+ return { outputs, nodeCount: state.nodeCount, cost: state.cost };
}
/** Throw when a graph is too costly to bake at this size. Called while normalizing, so a bad scene fails at once. */
@@ -418,31 +415,6 @@ function writeColor(data, offset, channels) {
data[offset + 3] = 255;
}
-// A joint edge is a few texels wide, and bilinear filtering blends the unit normals of its texels, which rounds the edge
-// off where Blender's Bump node differentiates the height at every pixel. Measured against Cycles (bench/reference/README.md,
-// 2026-11-04) a normal map at twice the colour resolution halves the mean angle error of the wall's normals.
-const NORMAL_MAP_RESOLUTION_FACTOR = 2;
-const NORMAL_MAP_RESOLUTION_LIMIT = 1024;
-// Four height reads per texel: half a texel either side along each axis. The work limit counts one evaluation per pixel of
-// every output, and a height read is cheaper than a full graph pixel, so the finer map may cost four times that limit
-// (the brick wall of the reference suite: 84 million units, about 0.35 s in Node).
-const HEIGHT_READS_PER_NORMAL_TEXEL = 4;
-const NORMAL_MAP_WORK_LIMIT = 4 * MATERIAL_GRAPH_LIMITS.maxWork;
-
-/** The size of the baked normal map: finer than the colour, unless the height is too costly to read that often. */
-function normalMapResolution(heightCost, resolution) {
- const finer = Math.min(resolution * NORMAL_MAP_RESOLUTION_FACTOR, NORMAL_MAP_RESOLUTION_LIMIT);
- const work = heightCost * HEIGHT_READS_PER_NORMAL_TEXEL * finer * finer;
- return finer > resolution && work <= NORMAL_MAP_WORK_LIMIT ? finer : resolution;
-}
-
-function bakeNormalMap(heightOutput, heightCost, resolution, bump) {
- const normalResolution = normalMapResolution(heightCost, resolution);
- const heightAt = (u, v) => finiteOrZero(heightOutput.evaluate(u, v));
- const normalData = normalMapFromHeightFunction(heightAt, normalResolution, bump);
- return { image: createImage(normalData, normalResolution, normalResolution), colorSpace: 'linear' };
-}
-
/** Bake a normalized graph into texture descriptors, one for each output the graph has. Deterministic. */
export function bakeMaterialGraph(graph, { resolution, bump }) {
const compiled = compileMaterialGraph(graph);
@@ -473,6 +445,6 @@ export function bakeMaterialGraph(graph, { resolution, bump }) {
map: descriptor(colorData, 'srgb'),
emissiveMap: descriptor(emissionData, 'srgb'),
metallicRoughnessMap: descriptor(surfaceData, 'linear'),
- normalMap: height ? bakeNormalMap(height, compiled.outputCosts.height, resolution, bump) : undefined,
+ normalMap: descriptor(height ? normalMapFromHeightFunction((u, v) => finiteOrZero(height.evaluate(u, v)), resolution, bump) : null, 'linear'),
};
}
diff --git a/tests/material-graph.test.js b/tests/material-graph.test.js
index 059bb71d..388c58e1 100644
--- a/tests/material-graph.test.js
+++ b/tests/material-graph.test.js
@@ -75,10 +75,10 @@ test('height becomes a normal map: flat height is a flat normal, relief tilts it
test('a height ramp tilts the normal by bump / 32 per UV unit at any resolution, the sign pointing downhill', () => {
for (const resolution of [256, 512]) {
const ramp = bakeMaterialPattern(normalizeMaterialPattern({ type: 'graph', graph: { height: { uv: 'u' } }, resolution, bump: 4 }));
- const { data, width } = ramp.normalMap.image;
+ const { data } = ramp.normalMap.image;
const slope = 4 / 32;
const length = Math.hypot(slope, 1);
- const offset = (Math.floor(width / 2) * width + Math.floor(width / 2)) * 4;
+ const offset = (Math.floor(resolution / 2) * resolution + Math.floor(resolution / 2)) * 4;
assert.equal(data[offset], Math.round((-slope / length * 0.5 + 0.5) * 255));
assert.equal(data[offset + 1], 128);
assert.equal(data[offset + 2], Math.round((1 / length * 0.5 + 0.5) * 255));
@@ -89,24 +89,8 @@ test('the slope of a sharp joint edge stays within one texel of the edge instead
const resolution = 256;
const step = { smoothstep: [0.5, 0.5 + 1 / resolution, { uv: 'u' }] };
const edge = bakeMaterialPattern(normalizeMaterialPattern({ type: 'graph', graph: { height: step }, resolution, bump: 1 }));
- const { data, width } = edge.normalMap.image;
- const tiltedColumns = Array.from({ length: width }, (_, column) => column).filter(column => data[(10 * width + column) * 4] !== 128);
- const tiltedColorTexels = tiltedColumns.length * resolution / width;
- assert.ok(tiltedColorTexels <= 2, `${tiltedColumns.length} normal texels are tilted`);
-});
-
-test('the normal map is baked at twice the colour resolution, and at the colour resolution when the height is too costly for that', () => {
- const brick = bakeMaterialPattern(normalizeMaterialPattern(graphPattern()));
- assert.equal(brick.map.image.width, 256);
- assert.equal(brick.normalMap.image.width, 512);
- assert.equal(brick.normalMap.image.height, 512);
- const costly = { height: { add: [{ noise: { scale: 6, octaves: 6 } }, { noise: { scale: 9, octaves: 6, seed: 1 } }] } };
- const small = bakeMaterialPattern(normalizeMaterialPattern({ type: 'graph', graph: costly, resolution: 256 }));
- const large = bakeMaterialPattern(normalizeMaterialPattern({ type: 'graph', graph: costly, resolution: 512 }));
- assert.equal(small.normalMap.image.width, 512);
- assert.equal(large.normalMap.image.width, 512, 'twice 512 texels would be over the work limit for two six octave noises');
- const largest = bakeMaterialPattern(normalizeMaterialPattern({ type: 'graph', graph: { height: { uv: 'u' } }, resolution: 1024 }));
- assert.equal(largest.normalMap.image.width, 1024);
+ const tiltedColumns = Array.from({ length: resolution }, (_, column) => column).filter(column => edge.normalMap.image.data[(10 * resolution + column) * 4] !== 128);
+ assert.ok(tiltedColumns.length <= 2, `${tiltedColumns.length} texels are tilted`);
});
test('every graph texture node tiles across the seam', () => {