3D engine that AI agents drive
Add a fresnel factor of emission to the material graph (Part of #860)
Nguyen Xuan An2c27db118239
diff --git a/changes/860-graph-fresnel.md b/changes/860-graph-fresnel.mdnew file mode 100644index 00000000..ffe557b8--- /dev/null+++ b/changes/860-graph-fresnel.md@@ -0,0 +1 @@+- A material graph takes a `fresnel` node as a factor of `emission` (Part of #860): the rasterizer and the CPU path tracer scale the emission by Schlick's Fresnel of the IOR per pixel, so glass-like rims glow toward grazing angles.diff --git a/docs/agent-scene.md b/docs/agent-scene.mdindex a73dbdb0..e232238e 100644--- a/docs/agent-scene.md+++ b/docs/agent-scene.md@@ -190,6 +190,7 @@ Placement and pattern nodes:- `brick` (`columns` 8, `rows` 8 and even, `mortar` 0.05 of a brick, `offset` 0.5 for every other row, `seed`, `output`: `mortar` is 1 in the joint, `edge` is 0 at a joint and 1 at the brick centre, `cell` is one random value per brick) lays a running bond and costs 4. Whole columns and even rows keep it tiling.- `whiteNoise` (`scale` 16, `seed`, `output`: `value` or `color` for three independent values) is one random value per cell of a `scale` by `scale` grid. It tiles and is the same for the same seed.- `image` (`asset`, the 64 hexadecimal characters of a catalog image ID from `scene_asset_catalog`; `output`: `color` is linear RGB decoded from sRGB, or `alpha`; `at` as for the other textures) reads a catalog image with bilinear filtering and repeats it across the tile, and costs 4. The host decodes the image into the same pool as texture sequence frames, so it follows their limits (1024 pixels a side, 64 MiB decoded in all); remote URLs are not accepted. Row 0 of the image is the top of the tile, as with a baked graph. A graph that names an image the host did not load fails when the material is baked, naming the asset.+- `fresnel` (`ior` 1.5, from 1.01 to 3) is Schlick's Fresnel reflectance at the angle between the view and the surface: about 0.04 face on for glass, rising to 1 at grazing angles. It depends on the camera, so no baked texture can hold it, and it works only as a factor of `emission`, alone or in a `mul` with the rest: `"emission": { "mul": [[1, 0.5, 0.2], { "fresnel": { "ior": 1.5 } }] }`. The graph bakes the colour; the rasterizer and the CPU path tracer multiply it by the curve per pixel with the same formula. The GPU path tracers do not read it yet and draw the unscaled emission. Anywhere else, `fresnel` is an error.- `coord` (`"generated"`, `"object"` or `"world"`) is a vector for `at`. `generated` is the unit tile, `object` and `world` are metres across the tile (texture u and v times the pattern `size`). A baked texture is projected onto the mesh, so it knows only the two axes of that projection and the third is 0; `object` and `world` are the same because box mapping reads the placed, scaled position.- `mapping` (`vector`, default `{ "coord": "generated" }`; `scale` a number or `[x, y, z]`; `rotate` radians around Z; `offset` `[x, y, z]`) scales, turns and moves a vector, as Blender's Mapping node does. Feed it to `at`.diff --git a/src/agent-scene-capabilities.js b/src/agent-scene-capabilities.jsindex 45d22b9b..0fdfd7c9 100644--- a/src/agent-scene-capabilities.js+++ b/src/agent-scene-capabilities.js@@ -54,7 +54,7 @@ export const MATERIAL_GRAPH_OUTPUTS = Object.freeze(['color', 'roughness', 'metaexport const MATERIAL_GRAPH_OPERATORS = Object.freeze(['uv', 'coord', 'mapping', 'vec', 'ramp', 'image', 'noise', 'whiteNoise', 'brick', 'voronoi', 'wave', 'checker', 'gradient','add', 'sub', 'mul', 'div', 'min', 'max', 'pow', 'abs', 'floor', 'fract', 'sqrt', 'invert', 'clamp', 'smoothstep', 'mix',- 'layer', 'hue', 'curve',+ 'layer', 'hue', 'curve', 'fresnel',]);export const MATERIAL_GRAPH_LIMITS = Object.freeze({ maxNodes: MAX_FIELD_NODES, maxDepth: MAX_FIELD_DEPTH, maxStops: 8, maxWork: 30_000_000 });// Curve and text geometry. A text geometry is bounded by vertices per curve segment: the bundled font stays under@@ -86,6 +86,7 @@ export const MATERIAL_GRAPH_NODES = Object.freeze({uv: '{"uv":"u"|"v"|"both"} coordinates.',coordinates: '{"coord":"generated"|"object"|"world"} vector of where the texel sits: generated is the unit tile, object and world are metres across the tile (u * size); the third axis is 0 because a baked texture is projected. {"mapping":{"vector":node,"scale":n|[x,y,z],"offset":[x,y,z],"rotate":radians}} scales, turns around Z and moves a vector (default: generated). Feed either to "at". Meshes without UVs use box mapping; vertex_color already multiplies the baked colour. Not in graphs yet: AO, bevel, curvature, fresnel, vertexGroup (per-pixel or per-vertex data a bake does not have).',ramp: '{"ramp":{"value":node,"stops":[[0,[r,g,b]],[1,[r,g,b]]]}} 2-8 stops rising from 0 to 1.',+ fresnel: '{"fresnel":{"ior":1.5}} ior 1.01-3. View dependent, so only as a factor of emission, alone or {"mul":[node,{"fresnel":{...}}]}: emission rises toward grazing angles, rendered per pixel by the rasterizer and the CPU path tracer.',mixing: '{"layer":{"base","top","mask","edge","softness"}}; {"hue":{"color","shift","saturation","value"}}; {"curve":{"value","points":[[x,y]]}}.',maths: 'add sub mul div min max pow take [a,b]; abs floor fract sqrt invert take one node; clamp smoothstep mix take [a,b,c]; {"vec":[r,g,b]}.',mapping: 'box (default) projects along object axes, no UVs; uv follows geometry UVs. size and axis only apply to box.',diff --git a/src/agent-scene-modules.js b/src/agent-scene-modules.jsindex 22814243..7bf097e1 100644--- a/src/agent-scene-modules.js+++ b/src/agent-scene-modules.js@@ -64,7 +64,7 @@ const MODULE_MARKERS = {patterns: ['"pattern"'],relations: ['"relations":'],timeline: ['"timeline":{'],- layerShaders: ['"anisotropy"', '"iridescence"'],+ layerShaders: ['"anisotropy"', '"iridescence"', '"fresnel"'],liquidShader: ['"transmission"', '"liquid"', '"foam"'],subsurfaceShader: ['"subsurface"'],// Any mention loads it, because an edit can set the density through a path or the environment of an operation.diff --git a/src/agent-scene.js b/src/agent-scene.jsindex 72350221..4ab1a4f7 100644--- a/src/agent-scene.js+++ b/src/agent-scene.js@@ -45,7 +45,7 @@ const RENDER_FLAG_FIELDS = ['visible', 'castShadow', 'receiveShadow', 'holdout',const PART_FLAG_FIELDS = RENDER_FLAG_FIELDS.slice(1);const NODE_FIELDS = ['translation', 'rotation', 'scale', 'parent', 'label', 'geometry', 'material', 'skin', 'relations', 'asset', 'vegetation', 'decal', 'depthLayer', 'blend', ...RENDER_FLAG_FIELDS];const MATERIAL_FIELDS = ['color', 'metallic', 'roughness', 'emission', 'emissiveColor', 'opacity', 'alphaMode', 'doubleSided', 'pattern', 'map', 'subsurface', 'snippet', 'waterPreset', 'absorption', 'turbidity', 'aov', 'wear', ...MATERIAL_LAYER_FIELDS];-const PATTERN_TEXTURE_FIELDS = ['map', 'normalMap', 'metallicRoughnessMap', 'emissiveMap', 'layerMaps', 'transmissionMap', 'aovMap', 'boxMapScale', 'boxMapAxis'];+const PATTERN_TEXTURE_FIELDS = ['map', 'normalMap', 'metallicRoughnessMap', 'emissiveMap', 'layerMaps', 'transmissionMap', 'aovMap', 'emissionFresnel', 'boxMapScale', 'boxMapAxis'];const CAMERA_FIELDS = [...Object.keys(CAMERA_DEFAULTS), ...CAMERA_LENS_FIELDS];const LOOK_OPERATION_FIELDS = Object.freeze({'set-environment': 'environment',@@ -216,7 +216,7 @@ function copyMaterialLayers(value, result) {validateMaterialLayers(result);if (value.subsurface !== undefined) result.subsurface = agentSceneModule('subsurfaceShader').copySubsurface(value.subsurface, result.tag);if (value.snippet) result.snippet = agentSceneModule('materialSnippet').normalizeMaterialSnippet(value.snippet, { hasMap: Boolean(value.map || value.pattern) });- if (result.anisotropy > 0 || result.iridescence > 0) agentSceneModule('layerShaders');+ if (result.anisotropy > 0 || result.iridescence > 0 || JSON.stringify(value.pattern?.graph?.emission ?? null).includes('"fresnel"')) agentSceneModule('layerShaders');if (result.toon > 0) agentSceneModule('lineArt');}diff --git a/src/material-graph.js b/src/material-graph.jsindex ca942cac..c51b9caf 100644--- a/src/material-graph.js+++ b/src/material-graph.js@@ -1,4 +1,5 @@import { MATERIAL_GRAPH_LIMITS, MATERIAL_GRAPH_OUTPUTS, MATERIAL_GRAPH_OPERATORS } from './agent-scene-capabilities.js';+import { EMISSION_FRESNEL_IOR_RANGE } from './material-layers.js';import { clampUnit, smoothStep, wrapCell, hashCell, 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@@ -577,6 +578,37 @@ function compileImage(node, state, depth) {return vectorValue(at(0, srgbToLinear), at(1, srgbToLinear), at(2, srgbToLinear));}+// Fresnel depends on the angle between the view and the surface, which a texture baked from u and v cannot hold. The+// renderers evaluate it per pixel instead, so it is accepted only as a factor of emission: {"fresnel":{"ior":1.5}} on+// its own, or in a "mul" with the rest of the emission. The graph bakes the rest; the factor travels as emissionFresnel.+function readFresnelIor(node) {+ const settings = textureSettings(node, 'fresnel', ['ior']);+ const [minimum, maximum] = EMISSION_FRESNEL_IOR_RANGE;+ const ior = settings.ior ?? 1.5;+ if (typeof ior !== 'number' || !Number.isFinite(ior) || ior < minimum || ior > maximum) {+ throw graphError(`fresnel ior must be a number from ${minimum} to ${maximum}, got ${describeKey(ior)}. Use 1.5 for glass.`);+ }+ return ior;+}++function isFresnelNode(node) {+ return isPlainObject(node) && Object.keys(node).length === 1 && Object.hasOwn(node, 'fresnel');+}++function compileFresnelOutsideEmission() {+ throw graphError('fresnel depends on the view angle and renders per pixel, so it only works as a factor of emission, such as {"emission":{"mul":[[1,0.6,0.2],{"fresnel":{"ior":1.5}}]}}.');+}++// Splits an emission node into what bakes and the fresnel IOR the renderer applies, or returns it unchanged.+function splitEmissionFresnel(node) {+ if (isFresnelNode(node)) return { base: 1, ior: readFresnelIor(node) };+ const operands = isPlainObject(node) && Object.keys(node).length === 1 ? node.mul : undefined;+ if (!Array.isArray(operands) || operands.length !== 2) return { base: node };+ const fresnelSlot = operands.findIndex(isFresnelNode);+ if (fresnelSlot < 0) return { base: node };+ return { base: operands[1 - fresnelSlot], ior: readFresnelIor(operands[fresnelSlot]) };+}+const TEXTURE_COMPILERS = new Map([['image', compileImage], ['noise', compileNoise], ['whiteNoise', compileWhiteNoise], ['brick', compileBrick], ['voronoi', compileVoronoi], ['wave', compileWave], ['checker', compileChecker], ['gradient', compileGradient],]);@@ -607,6 +639,7 @@ function compileNode(node, state, depth) {if (name === 'layer') return compileLayer(node, state, depth);if (name === 'hue') return compileHue(node, state, depth);if (name === 'curve') return compileCurve(node, state, depth);+ if (name === 'fresnel') return compileFresnelOutsideEmission();if (TEXTURE_COMPILERS.has(name)) return TEXTURE_COMPILERS.get(name)(node, state, depth);if (MATH_OPERATORS.has(name)) return compileMath(name, node, state, depth);throw graphError(`unknown operator ${describeKey(name)}. Use one of ${MATERIAL_GRAPH_OPERATORS.join(', ')}.`);@@ -616,7 +649,7 @@ const SCALAR_OUTPUTS = ['roughness', 'metallic', 'height', 'opacity', 'clearcoat/*** Check a graph and compile each output. `size` is the metres one texture tile covers, which object and world coordinates report. Limits on nodes, depth and work per pixel are enforced here, before any- * pixel is evaluated. Returns { outputs: { name: compiled }, nodeCount, cost }.+ * pixel is evaluated. Returns { outputs: { name: compiled }, emissionFresnel (an IOR, or undefined), nodeCount, cost }.*/export function compileMaterialGraph(graph, { size = 1, images } = {}) {if (!isPlainObject(graph)) throw graphError(`graph must be an object with outputs: ${MATERIAL_GRAPH_OUTPUTS.join(', ')}.`);@@ -625,11 +658,18 @@ export function compileMaterialGraph(graph, { size = 1, images } = {}) {if (names.length === 0) throw graphError(`graph needs at least one output: ${MATERIAL_GRAPH_OUTPUTS.join(', ')}.`);const state = { nodeCount: 0, cost: 0, size, images };const outputs = {};+ let emissionFresnel;for (const name of names) {- outputs[name] = compileNode(graph[name], state, 0);+ let node = graph[name];+ if (name === 'emission') {+ const split = splitEmissionFresnel(node);+ node = split.base;+ emissionFresnel = split.ior;+ }+ outputs[name] = compileNode(node, state, 0);if (SCALAR_OUTPUTS.includes(name) && outputs[name].kind !== 'scalar') throw graphError(`${name} needs a number, not a vector.`);}- return { outputs, nodeCount: state.nodeCount, cost: state.cost };+ return { outputs, emissionFresnel, 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. */@@ -735,6 +775,8 @@ export function bakeMaterialGraph(graph, { resolution, bump, size = 1 }, images)layerMaps: clearcoatData ? { clearcoatTexture: descriptor(clearcoatData, 'linear') } : undefined,transmissionMap: descriptor(transmissionData, 'linear'),aovMap: descriptor(aovData, 'linear'),+ // Not a texture: the renderers multiply the emission by Schlick's Fresnel of this IOR per pixel.+ emissionFresnel: compiled.emissionFresnel,normalMap: descriptor(height ? normalMapFromHeightFunction((u, v) => finiteOrZero(height.evaluate(u, v)), resolution, bump) : null, 'linear'),};}diff --git a/src/material-layer-shaders.js b/src/material-layer-shaders.jsindex 4e9220ab..eb8097f7 100644--- a/src/material-layer-shaders.js+++ b/src/material-layer-shaders.js@@ -85,6 +85,16 @@ const ANISOTROPY_MAIN_SETUP = ` if (materialAnisotropy.x > 0.0) {}`;+// A material graph's fresnel factor of emission (material-graph.js): Schlick's curve for the IOR in materialEmissionFresnel,+// the same curve as fresnelSchlick in material-layers.js, which the CPU path tracer uses.+registerShaderFeatureCode('emissionFresnel', Object.freeze({+ declarations: 'uniform float materialEmissionFresnel;\n',+ emissionHook: ` float emissionNormalIncidence = (materialEmissionFresnel - 1.0) / (materialEmissionFresnel + 1.0);+ emissionNormalIncidence *= emissionNormalIncidence;+ emission *= emissionNormalIncidence + (1.0 - emissionNormalIncidence) * pow(1.0 - clamp(dot(normal, viewDirection), 0.0, 1.0), 5.0);+`,+}));+registerShaderFeatureCode('anisotropy', Object.freeze({declarations: ANISOTROPY_DECLARATIONS,illuminateSpecular: ANISOTROPY_ILLUMINATE_SPECULAR,diff --git a/src/material-layers.js b/src/material-layers.jsindex a2627162..b8b085de 100644--- a/src/material-layers.js+++ b/src/material-layers.js@@ -92,6 +92,17 @@ export function validateMaterialLayers(material) {}}+// A material graph's {"fresnel":{"ior":n}} factor of emission (material-graph.js) reaches the renderers as `emissionFresnel`:+// the IOR, or 0 for none. The raster shader and the CPU path tracer both evaluate this same curve per pixel.+export const EMISSION_FRESNEL_IOR_RANGE = Object.freeze([1.01, 3]);++/** Schlick's Fresnel reflectance of a dielectric at the cosine between the view and the normal: 1 at grazing angles. */+export function fresnelSchlick(cosine, ior) {+ const normalIncidence = ((ior - 1) / (ior + 1)) ** 2;+ const clamped = Math.min(Math.max(cosine, 0), 1);+ return normalIncidence + (1 - normalIncidence) * (1 - clamped) ** 5;+}+export function materialLayerValue(material, field) {return material[field] ?? MATERIAL_LAYER_DEFAULTS[field];}@@ -135,7 +146,8 @@ export function materialLayerState(material) {material.lightMask ?? ALL_LIGHTS_MASK,materialLayerValue(material, 'toon'),material.subsurface ? JSON.stringify(material.subsurface) : '',+ material.emissionFresnel ?? 0,];}-export const MATERIAL_LAYER_STATE_LENGTH = 24;+export const MATERIAL_LAYER_STATE_LENGTH = 25;diff --git a/src/path-tracer-scene.js b/src/path-tracer-scene.jsindex edad602a..1f79c1e9 100644--- a/src/path-tracer-scene.js+++ b/src/path-tracer-scene.js@@ -135,6 +135,7 @@ function resolveMaterial(material) {roughness: material.roughness ?? 0.5,emission: emissionColor.map(channel => channel * emissionStrength),emissiveTexture: emissionStrength > 0 ? resolveTexture(material.emissiveMap) : null,+ emissionFresnel: material.emissionFresnel ?? 0,clearcoat: material.clearcoat ?? 0,clearcoatTexture: resolveTexture(material.layerMaps?.clearcoatTexture),transmissionTexture: resolveTexture(material.transmissionMap),diff --git a/src/path-tracer.js b/src/path-tracer.jsindex c0e0f166..0ce732b5 100644--- a/src/path-tracer.js+++ b/src/path-tracer.js@@ -1,4 +1,5 @@import { cameraFieldOfView } from './camera.js';+import { fresnelSchlick } from './material-layers.js';import { pointLightFalloff } from './ray-query.js';import { profileGain } from './light-profile.js';import { buildTraceScene, refuseLocalVolumes, SUN_ANGULAR_RADIUS } from './path-tracer-scene.js';@@ -896,7 +897,10 @@ function createPathTracer(scene, options) {recordFeatures(normal, isGlass);featuresRecorded = true;}- addRadiance(depth, throughput[0] * hit.emission[0], throughput[1] * hit.emission[1], throughput[2] * hit.emission[2]);+ const emissionScale = hit.material.emissionFresnel > 0+ ? fresnelSchlick(-(direction[0] * normal[0] + direction[1] * normal[1] + direction[2] * normal[2]), hit.material.emissionFresnel)+ : 1;+ addRadiance(depth, throughput[0] * hit.emission[0] * emissionScale, throughput[1] * hit.emission[1] * emissionScale, throughput[2] * hit.emission[2] * emissionScale);const viewX = -direction[0];const viewY = -direction[1];const viewZ = -direction[2];diff --git a/src/renderer.js b/src/renderer.jsindex 587a7e93..afbfb126 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -73,7 +73,7 @@ const MAP_SHADER_FEATURES = ['baseMap', 'normalMap', 'metallicRoughnessMap', 'oc// Part or material fields that name their shader feature and switch it on when set.const PRESENT_SHADER_FEATURES = ['skin', 'crowd', 'morph', 'lightMap', 'waterReflection'];// Material fields that name their shader feature and switch it on above zero.-const POSITIVE_SHADER_FEATURES = ['moss', 'anisotropy', 'iridescence', 'toon', 'boxMapScale'];+const POSITIVE_SHADER_FEATURES = ['moss', 'anisotropy', 'iridescence', 'toon', 'boxMapScale', 'emissionFresnel'];// Tags whose look the main fragment shader draws with code that other tags never reach.const TAG_SHADER_FEATURES = new Set(['floor', 'water', 'foliage', 'terrain', 'grass', 'fire', 'smoke', 'unlit']);@@ -991,7 +991,7 @@ export class Renderer67 {finishMainProgram(pendingProgram) {this.program = finishProgram(this.context, pendingProgram);- this.uniforms = Object.fromEntries(['materialDielectric', 'materialCoat', 'materialSheen', 'materialAnisotropy', 'materialIridescence', 'lightLinkMask', 'boxMapScale', 'boxMapAxis', 'lightMap', 'lightMapScale', 'hasLightMap', 'viewProjection', 'flushBias', 'clipPerMetre', 'lightProjection', 'eye', 'armorColor', 'armorSurface', 'exposure', 'inspection', 'shadowMap', 'shadowBiasPerSlope', ...MAP_UNIFORMS, ...MAP_FLAGS, ...SKIN_UNIFORMS, ...MORPH_UNIFORMS, 'normalScale', 'occlusionStrength', 'materialMoss', 'wearEnabled', 'wearMetal', 'wearDirt', 'wearShape', 'wearDirtShape', ...SPLAT_UNIFORMS, 'hdrEnabled', 'time', 'wind', 'worldMode', 'daylight', 'fogColor', 'lightDirection', 'fogDensity', 'waterRipple', 'rippleTime', 'explosionProgress', 'explosionCenter', 'assemblyAmount', 'renderPassMode', 'renderPassDepthRange', 'renderPassObjectBase', 'baseColor', 'surface', 'instanceEmission', 'instanceOpacity', 'boxNormalMatrix', 'shadowBlind', 'compositeMode', 'tintShadowMap', 'tintBlind', ...ALPHA_UNIFORMS].map((name) => [name, this.context.getUniformLocation(this.program, name)]));+ this.uniforms = Object.fromEntries(['materialDielectric', 'materialEmissionFresnel', 'materialCoat', 'materialSheen', 'materialAnisotropy', 'materialIridescence', 'lightLinkMask', 'boxMapScale', 'boxMapAxis', 'lightMap', 'lightMapScale', 'hasLightMap', 'viewProjection', 'flushBias', 'clipPerMetre', 'lightProjection', 'eye', 'armorColor', 'armorSurface', 'exposure', 'inspection', 'shadowMap', 'shadowBiasPerSlope', ...MAP_UNIFORMS, ...MAP_FLAGS, ...SKIN_UNIFORMS, ...MORPH_UNIFORMS, 'normalScale', 'occlusionStrength', 'materialMoss', 'wearEnabled', 'wearMetal', 'wearDirt', 'wearShape', 'wearDirtShape', ...SPLAT_UNIFORMS, 'hdrEnabled', 'time', 'wind', 'worldMode', 'daylight', 'fogColor', 'lightDirection', 'fogDensity', 'waterRipple', 'rippleTime', 'explosionProgress', 'explosionCenter', 'assemblyAmount', 'renderPassMode', 'renderPassDepthRange', 'renderPassObjectBase', 'baseColor', 'surface', 'instanceEmission', 'instanceOpacity', 'boxNormalMatrix', 'shadowBlind', 'compositeMode', 'tintShadowMap', 'tintBlind', ...ALPHA_UNIFORMS].map((name) => [name, this.context.getUniformLocation(this.program, name)]));for (const name of LIGHT_UNIFORMS) {this.uniforms[name] = this.context.getUniformLocation(this.program, name);}@@ -2550,6 +2550,7 @@ export class Renderer67 {this.context.uniform3f(this.uniforms.materialIridescence, layers[18], layers[19], layers[20]);this.context.uniform1i(this.uniforms.lightLinkMask, layers[21]);this.context.uniform1f(this.uniforms.materialToon, layers[22]);+ this.context.uniform1f(this.uniforms.materialEmissionFresnel, layers[24]);this.context.uniform1f(this.uniforms.boxMapScale, batch.mapState[12]);this.context.uniform1i(this.uniforms.boxMapAxis, batch.mapState[13]);if (this.shaderFeatures?.has('splat')) requireSplatSupport().bind(this, batch.splat);diff --git a/src/shaders.js b/src/shaders.jsindex 4b4de86a..8be7b463 100644--- a/src/shaders.js+++ b/src/shaders.js@@ -687,7 +687,7 @@ void main() {${featureCode("surfaceHook")} float alpha = surfaceAlpha(textureAlpha);vec3 emission = instanceEmission.rgb * surface.z;if (USES_EMISSIVE_MAP) emission *= (BOX_MAPPED ? sampleBox(emissiveMap) : texture(emissiveMap, mapUV(hasEmissiveMap))).rgb;- if (USES_RENDER_PASS) {+${featureCode("emissionHook")} if (USES_RENDER_PASS) {outputColor = vec4(renderPassValue(color, normal, emission), 1.0);return;}diff --git a/tests/material-graph-fresnel.test.js b/tests/material-graph-fresnel.test.jsnew file mode 100644index 00000000..b9728787--- /dev/null+++ b/tests/material-graph-fresnel.test.js@@ -0,0 +1,102 @@+import './support/load-agent-scene-modules.mjs';+import assert from 'node:assert/strict';+import test from 'node:test';+import { createAgentScene } from '../src/agent-scene.js';+import { compileMaterialGraph, bakeMaterialGraph } from '../src/material-graph.js';+import { fresnelSchlick, materialLayerState } from '../src/material-layers.js';+import { renderPathTracedImage } from '../src/path-tracer.js';+import { createFragmentShader } from '../src/shaders.js';+import '../src/material-layer-shaders.js';++const fresnelNode = (ior) => (ior === undefined ? { fresnel: {} } : { fresnel: { ior } });++// Reference values worked out by hand: F0 = ((n - 1) / (n + 1))^2 and F = F0 + (1 - F0) (1 - cos)^5.+test('fresnelSchlick matches the hand-worked curve', () => {+ assert.ok(Math.abs(fresnelSchlick(1, 1.5) - 0.04) < 1e-12);+ assert.equal(fresnelSchlick(0, 1.5), 1);+ assert.ok(Math.abs(fresnelSchlick(0.5, 1.5) - (0.04 + 0.96 * 0.03125)) < 1e-12);+ assert.ok(Math.abs(fresnelSchlick(1, 2) - 1 / 9) < 1e-12);+ assert.equal(fresnelSchlick(-0.3, 1.5), 1);+ assert.ok(Math.abs(fresnelSchlick(7, 1.5) - 0.04) < 1e-12);+});++test('fresnel alone or in a mul becomes a render-time factor and bakes the rest of the emission', () => {+ assert.equal(compileMaterialGraph({ emission: fresnelNode() }).emissionFresnel, 1.5);+ assert.equal(compileMaterialGraph({ emission: fresnelNode(1.33) }).emissionFresnel, 1.33);+ const product = compileMaterialGraph({ emission: { mul: [[1, 0.5, 0.25], fresnelNode(2)] } });+ assert.equal(product.emissionFresnel, 2);+ assert.deepEqual(product.outputs.emission.parts.map(part => part(0.5, 0.5)), [1, 0.5, 0.25]);+ const reversed = compileMaterialGraph({ emission: { mul: [fresnelNode(2), 0.25] } });+ assert.equal(reversed.emissionFresnel, 2);+ assert.equal(reversed.outputs.emission.evaluate(0.5, 0.5), 0.25);+ assert.equal(compileMaterialGraph({ emission: 1 }).emissionFresnel, undefined);+});++test('a baked graph carries the IOR next to its emission map, and no fresnel leaves the default bake unchanged', () => {+ const withFresnel = bakeMaterialGraph({ emission: { mul: [[1, 0.5, 0.25], fresnelNode(1.8)] } }, { resolution: 8, bump: 1 });+ assert.equal(withFresnel.emissionFresnel, 1.8);+ assert.ok(withFresnel.emissiveMap);+ const plain = bakeMaterialGraph({ emission: [1, 0.5, 0.25] }, { resolution: 8, bump: 1 });+ assert.equal(plain.emissionFresnel, undefined);+ assert.deepEqual(Array.from(plain.emissiveMap.image.data), Array.from(withFresnel.emissiveMap.image.data));+});++test('fresnel is checked at the boundary and refused outside emission', () => {+ assert.throws(() => compileMaterialGraph({ emission: fresnelNode(0.5) }), /fresnel ior must be a number from 1.01 to 3/);+ assert.throws(() => compileMaterialGraph({ emission: fresnelNode(4) }), /fresnel ior must be a number from 1.01 to 3/);+ assert.throws(() => compileMaterialGraph({ emission: { fresnel: { ior: 'glass' } } }), /fresnel ior/);+ assert.throws(() => compileMaterialGraph({ emission: { fresnel: { power: 2 } } }), /power/);+ assert.throws(() => compileMaterialGraph({ roughness: fresnelNode() }), /only works as a factor of emission/);+ assert.throws(() => compileMaterialGraph({ color: { mul: [[1, 1, 1], fresnelNode()] } }), /only works as a factor of emission/);+ assert.throws(() => compileMaterialGraph({ emission: { add: [1, fresnelNode()] } }), /only works as a factor of emission/);+ assert.throws(() => compileMaterialGraph({ emission: { mul: [fresnelNode(), fresnelNode()] } }), /only works as a factor of emission/);+});++function recipeWithFresnel(ior) {+ return {+ format: '67js.scene', schemaVersion: 1,+ geometries: [{ id: 'block', type: 'box', bevel: 0 }],+ materials: [{ id: 'rim', emissiveColor: [1, 1, 1], pattern: { type: 'graph', graph: { emission: { mul: [[1, 1, 1], fresnelNode(ior)] } }, mapping: 'uv', resolution: 256 } }],+ nodes: [{ id: 'part', geometry: 'block', material: 'rim' }],+ };+}++test('a recipe material exposes the factor, the renderer state carries it and the shader splices it in', () => {+ const { material } = createAgentScene(recipeWithFresnel(1.7)).parts[0];+ assert.equal(material.emissionFresnel, 1.7);+ assert.equal(materialLayerState(material)[24], 1.7);+ assert.equal(materialLayerState({}).at(24), 0);+ const shader = createFragmentShader(['emissionFresnel']);+ assert.match(shader, /uniform float materialEmissionFresnel/);+ assert.match(shader, /emission \*= emissionNormalIncidence \+ \(1\.0 - emissionNormalIncidence\) \* pow\(1\.0 - clamp\(dot\(normal, viewDirection\), 0\.0, 1\.0\), 5\.0\)/);+ assert.doesNotMatch(createFragmentShader([]), /materialEmissionFresnel/);+});++function groundParts(emissionFresnel) {+ const half = 20;+ const geometry = {+ positions: new Float32Array([-half, 0, half, half, 0, half, half, 0, -half, -half, 0, -half]),+ normals: new Float32Array([0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]),+ uvs: new Float32Array([0, 1, 1, 1, 1, 0, 0, 0]),+ indices: new Uint32Array([0, 1, 2, 0, 2, 3]),+ };+ return [{ name: 'ground', geometry, material: { tag: 'surface', metallic: 0, roughness: 1, color: [0, 0, 0], emission: 2, emissiveColor: [1, 1, 1], emissionFresnel } }];+}++async function centerRadiance(eye, emissionFresnel) {+ const camera = { eye, target: [0, 0, 0], up: [0, 0, -1], fov: 4 };+ const { linear } = await renderPathTracedImage({ parts: groundParts(emissionFresnel), environment: {}, camera }, { width: 16, height: 16, denoise: false, threads: 1, bounces: 1, samples: 1 });+ return linear[(8 * 16 + 8) * 3];+}++test('the CPU path tracer scales emission by the reference curve at the view angle', async () => {+ const straightDown = await centerRadiance([0, 3, 0], 1.5);+ assert.ok(Math.abs(straightDown - 2 * 0.04) < 1e-5, `facing ${straightDown}`);+ const slanted = await centerRadiance([4, 4, 0], 1.5);+ const expected = 2 * fresnelSchlick(Math.SQRT1_2, 1.5);+ assert.ok(Math.abs(slanted - expected) < 5e-3, `45 degrees ${slanted} against ${expected}`);+ const grazing = await centerRadiance([40, 1, 0], 2);+ assert.ok(grazing > 2 * 0.5, `grazing ${grazing}`);+ const unchanged = await centerRadiance([0, 3, 0], undefined);+ assert.equal(unchanged, 2);+});