Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Read a graph's height slope at half a texel each side so a joint edge stays thin (part of #50)

Nguyen Xuan An01e00f77e2c7
diff --git a/src/material-graph.js b/src/material-graph.js
index 11a7290f..a53d63d1 100644
--- a/src/material-graph.js
+++ b/src/material-graph.js
@@ -1,5 +1,5 @@
import { MATERIAL_GRAPH_LIMITS, MATERIAL_GRAPH_OUTPUTS, MATERIAL_GRAPH_OPERATORS } from './agent-scene-capabilities.js';
-import { clampUnit, smoothStep, fractalNoise, cellularNoise, linearToSrgbByte, createImage, normalMapFromHeights } from './material-texture-kit.js';
+import { clampUnit, smoothStep, fractalNoise, cellularNoise, linearToSrgbByte, createImage, normalMapFromHeightFunction } from './material-texture-kit.js';
// A bounded material node graph. Like a field expression (src/mesh-fields.js), a graph is data and never code: it is
// walked as plain objects against a fixed table of operators, checked against its limits before any pixel is
@@ -338,7 +338,6 @@ export function bakeMaterialGraph(graph, { resolution, bump }) {
const colorData = color ? new Uint8ClampedArray(pixelCount * 4) : null;
const emissionData = emission ? new Uint8ClampedArray(pixelCount * 4) : null;
const surfaceData = roughness || metallic ? new Uint8ClampedArray(pixelCount * 4) : null;
- const heights = height ? new Float32Array(pixelCount) : null;
for (let row = 0; row < resolution; row++) {
for (let column = 0; column < resolution; column++) {
const pixel = row * resolution + column;
@@ -347,7 +346,6 @@ export function bakeMaterialGraph(graph, { resolution, bump }) {
const offset = pixel * 4;
if (color) writeColor(colorData, offset, sampleChannels(color, u, v));
if (emission) writeColor(emissionData, offset, sampleChannels(emission, u, v));
- if (heights) heights[pixel] = finiteOrZero(height.evaluate(u, v));
if (surfaceData) {
surfaceData[offset] = 255;
surfaceData[offset + 1] = roughness ? Math.round(clampUnit(finiteOrZero(roughness.evaluate(u, v))) * 255) : 255;
@@ -361,6 +359,6 @@ export function bakeMaterialGraph(graph, { resolution, bump }) {
map: descriptor(colorData, 'srgb'),
emissiveMap: descriptor(emissionData, 'srgb'),
metallicRoughnessMap: descriptor(surfaceData, 'linear'),
- normalMap: descriptor(heights ? normalMapFromHeights(heights, resolution, bump) : null, 'linear'),
+ normalMap: descriptor(height ? normalMapFromHeightFunction((u, v) => finiteOrZero(height.evaluate(u, v)), resolution, bump) : null, 'linear'),
};
}
diff --git a/src/material-texture-kit.js b/src/material-texture-kit.js
index 19b1cc24..ac4324fe 100644
--- a/src/material-texture-kit.js
+++ b/src/material-texture-kit.js
@@ -108,25 +108,43 @@ export function createImage(data, width, height) {
return typeof ImageData === 'function' ? new ImageData(data, width, height) : { width, height, data };
}
-/** Tangent-space normal map bytes from a height field. Height differences are per texel; scale them so a bump of 1 reads the same at any resolution. */
-export function normalMapFromHeights(heights, resolution, bump) {
+function normalMapFromSlopes(resolution, slopeU, slopeV) {
const normalData = new Uint8ClampedArray(resolution * resolution * 4);
- const slopeScale = bump * resolution / 64;
for (let row = 0; row < resolution; row++) {
- const rowAbove = (row + 1) % resolution;
- const rowBelow = (row + resolution - 1) % resolution;
for (let column = 0; column < resolution; column++) {
- const columnRight = (column + 1) % resolution;
- const columnLeft = (column + resolution - 1) % resolution;
- const slopeU = (heights[row * resolution + columnRight] - heights[row * resolution + columnLeft]) * slopeScale;
- const slopeV = (heights[rowAbove * resolution + column] - heights[rowBelow * resolution + column]) * slopeScale;
- const length = Math.hypot(slopeU, slopeV, 1);
+ const slopeAcross = slopeU(column, row);
+ const slopeUp = slopeV(column, row);
+ const length = Math.hypot(slopeAcross, slopeUp, 1);
const offset = (row * resolution + column) * 4;
- normalData[offset] = Math.round((-slopeU / length * 0.5 + 0.5) * 255);
- normalData[offset + 1] = Math.round((-slopeV / length * 0.5 + 0.5) * 255);
+ normalData[offset] = Math.round((-slopeAcross / length * 0.5 + 0.5) * 255);
+ normalData[offset + 1] = Math.round((-slopeUp / length * 0.5 + 0.5) * 255);
normalData[offset + 2] = Math.round((1 / length * 0.5 + 0.5) * 255);
normalData[offset + 3] = 255;
}
}
return normalData;
}
+
+/** Tangent-space normal map bytes from a height field. Height differences are per texel; scale them so a bump of 1 reads the same at any resolution. */
+export function normalMapFromHeights(heights, resolution, bump) {
+ const slopeScale = bump * resolution / 64;
+ const heightAt = (column, row) => heights[((row + resolution) % resolution) * resolution + ((column + resolution) % resolution)];
+ return normalMapFromSlopes(
+ resolution,
+ (column, row) => (heightAt(column + 1, row) - heightAt(column - 1, row)) * slopeScale,
+ (column, row) => (heightAt(column, row + 1) - heightAt(column, row - 1)) * slopeScale,
+ );
+}
+
+/** The same slope per UV unit (bump / 32) read from a height function of (u, v) by a central difference of half a texel each side.
+ * Blender's Bump node reads a footprint of 0.1 pixel, so a joint edge stays one texel thin where two texels each side widen it to three. */
+export function normalMapFromHeightFunction(heightAt, resolution, bump) {
+ const halfStep = 0.5 / resolution;
+ const slopeScale = bump * resolution / 32;
+ const slopeAt = (column, row, acrossStep, upStep) => {
+ const u = (column + 0.5) / resolution;
+ const v = (row + 0.5) / resolution;
+ return (heightAt(u + acrossStep, v + upStep) - heightAt(u - acrossStep, v - upStep)) * slopeScale;
+ };
+ return normalMapFromSlopes(resolution, (column, row) => slopeAt(column, row, halfStep, 0), (column, row) => slopeAt(column, row, 0, halfStep));
+}
diff --git a/tests/material-graph.test.js b/tests/material-graph.test.js
index 88daa172..77b731ca 100644
--- a/tests/material-graph.test.js
+++ b/tests/material-graph.test.js
@@ -72,6 +72,27 @@ test('height becomes a normal map: flat height is a flat normal, relief tilts it
assert.ok(Math.max(...tilted) - Math.min(...tilted) > 40);
});
+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 } = ramp.normalMap.image;
+ const slope = 4 / 32;
+ const length = Math.hypot(slope, 1);
+ 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));
+ }
+});
+
+test('the slope of a sharp joint edge stays within one texel of the edge instead of spreading over three', () => {
+ 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 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', () => {
const nodes = [{ noise: { scale: 6 } }, { voronoi: { scale: 5, output: 'edge' } }, { wave: { frequency: 3, direction: 'diagonal' } }, { checker: { count: 8 } }];
for (const node of nodes) {