3D engine that AI agents drive
Keep the agent scene core from growing: tighter graph node docs, graph factors reuse graphFactor (issue #609)
Nguyen Xuan An5991b2df4548
diff --git a/src/agent-scene-capabilities.js b/src/agent-scene-capabilities.jsindex 7abbe355..d39f8080 100644--- a/src/agent-scene-capabilities.js+++ b/src/agent-scene-capabilities.js@@ -69,22 +69,20 @@ export const CURVE_TEXT_DETAILS = Object.freeze({});export const MATERIAL_MAPPINGS = Object.freeze(['box', 'uv']);export const MATERIAL_GRAPH_NODES = Object.freeze({- outputs: 'color (linear RGB; a number is grey), roughness, metallic, emission (colour; defaults to 1), height (becomes a normal map), opacity (base texture alpha; the material blends unless alphaMode says otherwise), clearcoat and transmission (per-pixel masks of the material factors, which default to 1; the path tracer reads both, the rasterizer reads clearcoat only).',- node: 'A number, [x, y, z], or an object with exactly one operator.',+ outputs: 'color (linear RGB; a number is grey), roughness, metallic, emission (defaults to 1), height (normal map), opacity (alpha), clearcoat, transmission (masks).',textures: Object.freeze({noise: '{"noise":{"scale":8,"seed":1,"octaves":4}}',voronoi: '{"voronoi":{"scale":6,"output":"distance"|"edge"|"cell"}}',- wave: 'ripple along z (normal true: along normals) from start [0,0] over axes xy, x or y: height 0.5, width 1.5, narrowness 0-50 (1.5), speed 0.25 per frame, time 0 (frame), timeOffset 0, cyclic true.',- checker: '{"checker":{"count":8}} (even count)',+ wave: '{"wave":{"frequency":4,"direction":"u"|"v"|"diagonal","shape":"sine"|"saw"|"triangle"}}',+ checker: '{"checker":{"count":8}} (even)',gradient: '{"gradient":{"type":"u"|"v"|"radial"}}',- note: 'Each takes "at", a vector such as {"uv":"both"}, to warp it. Scales are whole numbers 1 to 64 so it tiles.',+ note: 'Each takes "at", a vector such as {"uv":"both"}. Scales are whole numbers 1-64.',}),- uv: '{"uv":"u"|"v"|"both"} are the coordinates.',- ramp: '{"ramp":{"value":node,"stops":[[0,[r,g,b]],[1,[r,g,b]]]}} maps a number to colours, 2 to 8 stops rising from 0 to 1.',- mixing: '{"layer":{"base":node,"top":node,"mask":number node,"edge":0.5,"softness":0.1}} puts top over base where mask passes edge (paint over rust); {"hue":{"color":node,"shift":number node (turns),"saturation":1,"value":1}}; {"curve":{"value":node,"points":[[x,y],...]}} remaps by 2 to 8 points with rising x.',- 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]}. Numbers combine with vectors per component.',- example: '{"color":{"ramp":{"value":{"noise":{"scale":8}},"stops":[[0,[0.1,0.05,0.02]],[1,[0.6,0.4,0.2]]]}},"height":{"noise":{"scale":8}}}',- mapping: 'box (default) projects along the object axes and needs no UVs; uv follows geometry UVs, so the mesh needs a uv_unwrap node or geometry.uvs. size and axis only apply to box.',+ uv: '{"uv":"u"|"v"|"both"} coordinates.',+ ramp: '{"ramp":{"value":node,"stops":[[0,[r,g,b]],[1,[r,g,b]]]}} 2-8 stops rising from 0 to 1.',+ 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.',});// Recipe textures are opaque patterns, so cutout masking has nothing to cut.diff --git a/src/agent-scene.js b/src/agent-scene.jsindex 0fd4bc1d..88c909a3 100644--- a/src/agent-scene.js+++ b/src/agent-scene.js@@ -200,11 +200,6 @@ function copyMaterialLayers(value, result) {if (value[field] === undefined) continue;result[field] = Array.isArray(value[field]) ? vector(value[field], 3, field) : value[field];}- // A graph that outputs clearcoat or transmission is the per-pixel mask, so the factor defaults to 1 (full strength).- const graph = value.pattern?.type === 'graph' ? value.pattern.graph : null;- for (const field of ['clearcoat', 'transmission']) {- if (value[field] === undefined && graph && Object.hasOwn(graph, field)) result[field] = 1;- }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) });@@ -220,6 +215,7 @@ function materialFrom(value) {// A graph that makes roughness, metallic or emission passes it through as written, so the factors default to 1.const graphFactor = (output, fallback) => (Object.hasOwn(graph ?? {}, output) ? 1 : fallback);const result = { id: value.id, tag: defaultValue(value.tag, 'surface'), color: vector(defaultValue(value.color, defaultColor), 3, 'color'),+ ...(graphFactor('clearcoat', 0) && { clearcoat: 1 }), ...(graphFactor('transmission', 0) && { transmission: 1 }),metallic: defaultValue(value.metallic, graphFactor('metallic', 0)), roughness: defaultValue(value.roughness, graphFactor('roughness', 0.5)), emission: defaultValue(value.emission, graphFactor('emission', 0)) };if (!AGENT_SCENE_CAPABILITIES.materialTags.includes(result.tag)) {throw new RangeError(`Material tag must be one of ${AGENT_SCENE_CAPABILITIES.materialTags.join(', ')}.`);@@ -239,8 +235,7 @@ function materialFrom(value) {throw new RangeError(`alphaMode must be one of ${RECIPE_ALPHA_MODES.join(', ')}.`);}// The renderer ignores opacity on opaque surfaces, so any opacity below one means blending (additive stays additive).- // A graph that outputs opacity writes it into the base texture alpha, which only blend, add and mask read.- const graphHasOpacity = value.pattern?.type === 'graph' && Object.hasOwn(value.pattern.graph ?? {}, 'opacity');+ const graphHasOpacity = graphFactor('opacity', 0);result.alphaMode = (result.opacity < 1 || graphHasOpacity) && requestedAlphaMode === 'opaque' ? 'blend' : requestedAlphaMode;result.doubleSided = defaultValue(value.doubleSided, false);if (typeof result.doubleSided !== 'boolean') {