3D engine that AI agents drive
Add outlines, toon light and screen-width polylines (#285)
Nguyen Xuan An33a70531c245
diff --git a/package.json b/package.jsonindex 2ed1dd62..0f5b9414 100644--- a/package.json+++ b/package.json@@ -83,6 +83,7 @@"./addons/camera-effects": "./build/camera-effects.js","./addons/water-reflection": "./build/water-reflection.js","./addons/render-passes": "./build/render-passes.js",+ "./addons/line-art": "./build/line-art.js","./addons/material-layer-shaders": "./build/material-layer-shaders.js","./addons/light-linking": "./build/light-linking.js","./addons/cascaded-shadows": "./build/cascaded-shadows.js",diff --git a/scripts/agent-preview-browser.js b/scripts/agent-preview-browser.jsindex 9006d5ad..5daaf6e4 100644--- a/scripts/agent-preview-browser.js+++ b/scripts/agent-preview-browser.js@@ -151,7 +151,7 @@ function draw(record, time = 0, camera = record.scene.camera, frameOptions = {})record.shadowKey = shadowKey;}record.renderer.world = environment.world;- record.renderer.render({ ...camera, environment: litEnvironment(record.scene.parts, environment, Boolean(record.renderer.groundPart)), effects, exposure: effects.exposure, time, ...frameOptions });+ record.renderer.render({ ...camera, environment: litEnvironment(record.scene.parts, environment, Boolean(record.renderer.groundPart)), effects, exposure: effects.exposure, time, lines: environment.lines, ...frameOptions });const error = record.renderer.context.getError();if (error !== 0) {throw new Error(`WebGL render failed with error ${error}.`);@@ -218,7 +218,7 @@ function cascadesFor(scene) {function initializeRenderer(scene, { memoryBudgetMiB, backend }) {const placement = agentGroundPlacement(scene.parts, scene.environment);- return installRenderPasses(installCameraEffects(new Renderer67(canvas, scene.parts, {+ const renderer = installRenderPasses(installCameraEffects(new Renderer67(canvas, scene.parts, {ground: placement ? createAgentGround(placement) : false,hdr: true,depth: true,@@ -232,6 +232,8 @@ function initializeRenderer(scene, { memoryBudgetMiB, backend }) {samples: backend === 'cpu' ? 1 : 4,memoryBudgetMiB,})));+ // Outlines, toon light and polylines draw only for a frame that carries environment.lines.+ return agentSceneModule('lineArt').installLineArt(renderer);}// Camera fields from a timeline shot on top of the sampled camera, checked with the scene's own camera rules.diff --git a/scripts/mcp-server.mjs b/scripts/mcp-server.mjsindex 71659f8d..83ce4b91 100644--- a/scripts/mcp-server.mjs+++ b/scripts/mcp-server.mjs@@ -95,6 +95,7 @@ const materialLayerFields = {iridescence: layerNumber('iridescence', 'Thin-film colour shift.'),iridescenceIor: layerNumber('iridescenceIor', 'Film index of refraction.'),iridescenceThickness: layerNumber('iridescenceThickness', 'Film thickness, nanometres.'),+ toon: layerNumber('toon', 'Light steps (toon); 0 off.'),};@@ -144,6 +145,7 @@ const volumeFog = z.strictObject({shadowAmbient: unit.optional(),});const affects = z.array(z.string()).optional();+const lines = z.record(z.string(), z.unknown()).describe('Outlines, toon light and polylines as plain JSON; fields and limits in scene_capabilities lineArt. null clears.');const environment = z.strictObject({world: z.boolean().optional().describe('Draw the sky and apply surface fog.'),ground: z.boolean().optional().describe('false: no automatic floor.'),@@ -195,6 +197,7 @@ const environment = z.strictObject({fogDensity: bounded(0, 10).optional(),volumeFog: volumeFog.optional().describe('Directional scattering; needs HDR.'),liquid: liquidOptics.optional().describe('Optics for materials tagged liquid or foam.'),+ lines: lines.nullable().optional(),});const effects = z.strictObject({exposure: bounded(0.01, 16).optional(),diff --git a/src/agent-look.js b/src/agent-look.jsindex e5c693df..c2af041d 100644--- a/src/agent-look.js+++ b/src/agent-look.js@@ -4,6 +4,7 @@ import { resolveEffects } from './effects.js';import { resolveVolumeFog } from './volumetric.js';import { resolveLiquidOptics } from './renderer.js';import { createLightingSetupMap, lightingSetupFields } from './lighting-setups.js';+import { agentSceneModule } from './agent-scene-modules.js';import { DEFAULT_LIGHTING_SETUP, LIGHTING_SETUP_NAMES, DEFAULT_AMBIENT_OCCLUSION } from './agent-scene-capabilities.js';// The longest list in a look is the longest light list.@@ -11,7 +12,7 @@ const MAX_LOOK_ARRAY_ENTRIES = Math.max(MAX_POINT_LIGHTS, MAX_SPOT_LIGHTS, MAX_Aconst ENVIRONMENT_FIELDS = ['world', 'ground', 'setup', 'environmentIntensity', 'environmentRotation', 'lightDirection', 'keyColor', 'keyIntensity', 'fillIntensity', 'ambientIntensity',- 'daylight', 'pointLights', 'spotLights', 'areaLights', 'skyTop', 'skyBottom', 'fogColor', 'fogDensity', 'volumeFog', 'liquid', 'shadowCascades', 'keyAffects', 'objectReflections',+ 'daylight', 'pointLights', 'spotLights', 'areaLights', 'skyTop', 'skyBottom', 'fogColor', 'fogDensity', 'volumeFog', 'liquid', 'shadowCascades', 'keyAffects', 'objectReflections', 'lines',];const LIQUID_FIELDS = ['thickness', 'ior', 'refraction', 'scattering', 'foamOpacity', 'foamSoftness'];const EFFECT_FIELDS = ['exposure', 'saturation', 'contrast', 'temperature', 'vignette', 'grain', 'ambientOcclusion', 'ambientOcclusionRadius', 'bloom', 'depthOfField', 'motionBlur'];@@ -271,6 +272,10 @@ function environmentFrom(input = {}) {}environment.objectReflections = input.objectReflections;}+ // Outlines and line geometry; null clears them. The module checks the fields and loads on demand.+ if (input.lines !== undefined && input.lines !== null) {+ environment.lines = copiedJson(agentSceneModule('lineArt').resolveLineArt(input.lines));+ }if (input.spotLights !== undefined) {environment.spotLights = lightListFrom(input.spotLights, MAX_SPOT_LIGHTS, 'spotLights').map(spotLightFrom);}diff --git a/src/agent-scene-capabilities.js b/src/agent-scene-capabilities.jsindex 5b5bcb88..3f116de6 100644--- a/src/agent-scene-capabilities.js+++ b/src/agent-scene-capabilities.js@@ -33,6 +33,7 @@ const MATERIAL_LAYER_NOTES = Object.freeze({iridescence: 'Thin-film colour shift on reflections.',iridescenceIor: 'Film index of refraction.',iridescenceThickness: 'Film nanometres; colours cycle as it grows.',+ toon: 'Light steps (toon); 0 off.',});export const MATERIAL_PATTERN_TYPES =Object.freeze(['noise', 'wood', 'planks', 'bricks', 'tiles', 'marble', 'stone', 'concrete', 'metal', 'rust', 'fabric']);@@ -208,6 +209,7 @@ export const AGENT_SCENE_CAPABILITIES = Object.freeze({localShadows: Object.freeze({ lights: Object.freeze(['pointLights', 'spotLights']), atlasTiles: 24, pointLightTiles: 6, spotLightTiles: 1, tileSize: 512 }),lightLinking: Object.freeze({ fields: Object.freeze(['environment.keyAffects', 'pointLights[].affects', 'spotLights[].affects', 'areaLights[].affects']), unlisted: 'a light without affects lights every node', unknownNode: 'rejected', bake: 'honoured', pathTracer: 'ignored, named in report.ignored' }),shadowCascades: Object.freeze({ field: 'environment.shadowCascades', counts: Object.freeze([0, 1, 2, 3, 4]), zero: 'one shadow map', automatic: 'three cascades when shadow.extent is above 40 and the field is unset', maximumDistance: 150, note: 'volume fog and water read the single map' }),+ lineArt: Object.freeze({ field: 'environment.lines', fields: 'width px 0.25-16 (1.5), color, boundaries, creaseAngle deg (35), depthJump (0.08), toon steps 0-8, polylines [{points [x,y,z] 2-256, color, width, depthTest, closed}] max 64; {} turns on, null clears; material toon 0-8', note: 'docs/line-art.md' }),proceduralMotion: true,motionIk: Object.freeze(['two-bone', 'aim']),// Authored rigs: armatures expand into bone nodes, and a node `skin` binds a mesh with automatic weights (four influences).diff --git a/src/agent-scene-modules.js b/src/agent-scene-modules.jsindex 7f6a43eb..32763370 100644--- a/src/agent-scene-modules.js+++ b/src/agent-scene-modules.js@@ -21,6 +21,7 @@ const MODULE_LOADERS = {navigation: () => import('./agent-navigation.js'),lightLinking: () => import('./light-linking.js'),cascades: () => import('./cascaded-shadows.js'),+ lineArt: () => import('./line-art.js'),};const loadedModules = {};@@ -49,6 +50,7 @@ const MODULE_MARKERS = {navigation: [],lightLinking: ['"affects"', '"keyAffects"'],cascades: ['"shadowCascades"'],+ lineArt: ['"lines":{', '"toon":'],};/** Whether a recipe, operation list or edit list contains a boolean edit, which needs loadMeshBooleans() first. */diff --git a/src/agent-scene.js b/src/agent-scene.jsindex 3bc1912b..c32990d7 100644--- a/src/agent-scene.js+++ b/src/agent-scene.js@@ -193,6 +193,7 @@ function copyMaterialLayers(value, result) {}validateMaterialLayers(result);if (result.anisotropy > 0 || result.iridescence > 0) agentSceneModule('layerShaders');+ if (result.toon > 0) agentSceneModule('lineArt');}function materialFrom(value) {@@ -1347,7 +1348,7 @@ export function createAgentScene(input, inputLimits = {}) {knownKeys(operation, ['type', lookField], 'operation');const patch = operation[lookField];// Spot and area lights, cascades and sun links are absent from a recipe until it sets them.- const optionalFields = lookField === 'environment' ? ['spotLights', 'areaLights', 'shadowCascades', 'keyAffects', 'objectReflections'] : [];+ const optionalFields = lookField === 'environment' ? ['spotLights', 'areaLights', 'shadowCascades', 'keyAffects', 'objectReflections', 'lines'] : [];knownKeys(patch, [...Object.keys(candidate[lookField]), ...optionalFields], lookField);const nestedField = lookField === 'environment' ? 'volumeFog' : lookField === 'effects' ? 'bloom' : null;const merged = { ...candidate[lookField], ...(lookField === 'environment' ? environmentPatchWithSetup(patch) : patch) };diff --git a/src/line-art.js b/src/line-art.jsnew file mode 100644index 00000000..75e94b08--- /dev/null+++ b/src/line-art.js@@ -0,0 +1,574 @@+import './toon-shader.js';++// gl-program.js is not imported on purpose: this module loads on demand, and sharing a module with the scene entry makes+// the bundler hoist the shared code into the entry chunk (measured: about 350 bytes gzip on entry:agent-scene).+const fullscreenVertex = `#version 300 es+precision highp float;+out vec2 textureCoordinate;+void main() {+ vec2 position = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));+ textureCoordinate = position;+ gl_Position = vec4(position * 2.0 - 1.0, 0.0, 1.0);+}`;++function compileShader(context, kind, source) {+ const shader = context.createShader(kind);+ context.shaderSource(shader, source);+ context.compileShader(shader);+ if (!context.getShaderParameter(shader, context.COMPILE_STATUS)) {+ throw new Error(`Line art shader compilation failed: ${context.getShaderInfoLog(shader)}`);+ }+ return shader;+}++function compileProgram(context, vertexSource, fragmentSource) {+ const program = context.createProgram();+ context.attachShader(program, compileShader(context, context.VERTEX_SHADER, vertexSource));+ context.attachShader(program, compileShader(context, context.FRAGMENT_SHADER, fragmentSource));+ context.linkProgram(program);+ if (!context.getProgramParameter(program, context.LINK_STATUS)) {+ throw new Error(`Line art shader linking failed: ${context.getProgramInfoLog(program)}`);+ }+ return program;+}++// Outlines, toon light and screen-width lines as one addon. Nothing here is in the core renderer: installLineArt adds a+// post stage and wraps render, and a frame draws lines only when it carries `frame.lines` (the resolved+// `environment.lines` of a recipe). Without that field render runs untouched, so a scene without lines is unchanged.+//+// Outlines come from three guides: the object ID pass (part boundaries and silhouettes), the normal pass (creases) and+// the depth of the frame (one surface in front of another). The ID and normal guides are drawn by render passes+// (render-passes.js, installed first) just before the frame, which costs two extra draws of the opaque geometry.+// The line width is in output pixels, so a 4K still and a preview carry lines of the same thickness.++export const LINE_ART_DEFAULTS = Object.freeze({+ width: 1.5,+ color: Object.freeze([0.02, 0.02, 0.02]),+ boundaries: true,+ creaseAngle: 35,+ depthJump: 0.08,+ toon: 0,+});++export const LINE_ART_LIMITS = Object.freeze({+ width: Object.freeze([0.25, 16]),+ color: Object.freeze([0, 4]),+ creaseAngle: Object.freeze([0, 180]),+ depthJump: Object.freeze([0, 10]),+ toon: Object.freeze([0, 8]),+ maxPolylines: 64,+ minPolylinePoints: 2,+ maxPolylinePoints: 256,+ maxPolylineSegments: 4096,+ coordinate: 1e6,+});++const LINE_ART_FIELDS = [...Object.keys(LINE_ART_DEFAULTS), 'polylines'];+const POLYLINE_FIELDS = ['points', 'color', 'width', 'depthTest', 'closed'];+const OUTLINE_RING_LIMIT = Math.ceil(LINE_ART_LIMITS.width[1] / 2);++function plainObject(value, label) {+ if (!value || typeof value !== 'object' || Array.isArray(value)) throw new TypeError(`${label} must be a JSON object.`);+}++function onlyKeys(value, allowed, label) {+ const unknown = Object.keys(value).filter(key => !allowed.includes(key));+ if (unknown.length > 0) throw new RangeError(`${label} has unknown ${unknown.length === 1 ? 'field' : 'fields'}: ${unknown.join(', ')}. Allowed: ${allowed.join(', ')}.`);+}++function numberWithin(value, [minimum, maximum], label) {+ if (typeof value !== 'number' || !Number.isFinite(value) || value < minimum || value > maximum) {+ throw new RangeError(`${label} must be a number between ${minimum} and ${maximum}.`);+ }+ return value;+}++function booleanValue(value, label) {+ if (typeof value !== 'boolean') throw new TypeError(`${label} must be true or false.`);+ return value;+}++function colorValue(value, label) {+ if (!Array.isArray(value) || value.length !== 3) throw new TypeError(`${label} must be three linear RGB numbers [red, green, blue].`);+ return value.map((component, index) => numberWithin(component, LINE_ART_LIMITS.color, `${label}[${index}]`));+}++function pointValue(value, label) {+ if (!Array.isArray(value) || value.length !== 3) throw new TypeError(`${label} must be a point [x, y, z].`);+ const limit = LINE_ART_LIMITS.coordinate;+ return value.map((component, index) => numberWithin(component, [-limit, limit], `${label}[${index}]`));+}++function polylineFrom(input, index, defaults) {+ const label = `lines.polylines[${index}]`;+ plainObject(input, label);+ onlyKeys(input, POLYLINE_FIELDS, label);+ const count = Array.isArray(input.points) ? input.points.length : 0;+ if (count < LINE_ART_LIMITS.minPolylinePoints || count > LINE_ART_LIMITS.maxPolylinePoints) {+ throw new RangeError(`${label}.points needs ${LINE_ART_LIMITS.minPolylinePoints} to ${LINE_ART_LIMITS.maxPolylinePoints} points.`);+ }+ return {+ points: input.points.map((point, pointIndex) => pointValue(point, `${label}.points[${pointIndex}]`)),+ color: colorValue(input.color ?? defaults.color, `${label}.color`),+ width: numberWithin(input.width ?? defaults.width, LINE_ART_LIMITS.width, `${label}.width`),+ depthTest: booleanValue(input.depthTest ?? true, `${label}.depthTest`),+ closed: booleanValue(input.closed ?? false, `${label}.closed`),+ };+}++function segmentCount(polyline) {+ return polyline.points.length - 1 + (polyline.closed ? 1 : 0);+}++/**+ * Checks `environment.lines` and returns it with every default filled in, so the same input gives the same lines.+ * Throws a RangeError or TypeError that names the field.+ */+export function resolveLineArt(input = {}) {+ plainObject(input, 'lines');+ onlyKeys(input, LINE_ART_FIELDS, 'lines');+ const resolved = {+ width: numberWithin(input.width ?? LINE_ART_DEFAULTS.width, LINE_ART_LIMITS.width, 'lines.width'),+ color: colorValue(input.color ?? LINE_ART_DEFAULTS.color, 'lines.color'),+ boundaries: booleanValue(input.boundaries ?? LINE_ART_DEFAULTS.boundaries, 'lines.boundaries'),+ creaseAngle: numberWithin(input.creaseAngle ?? LINE_ART_DEFAULTS.creaseAngle, LINE_ART_LIMITS.creaseAngle, 'lines.creaseAngle'),+ depthJump: numberWithin(input.depthJump ?? LINE_ART_DEFAULTS.depthJump, LINE_ART_LIMITS.depthJump, 'lines.depthJump'),+ toon: numberWithin(input.toon ?? LINE_ART_DEFAULTS.toon, LINE_ART_LIMITS.toon, 'lines.toon'),+ };+ if (input.polylines !== undefined) {+ if (!Array.isArray(input.polylines) || input.polylines.length > LINE_ART_LIMITS.maxPolylines) {+ throw new RangeError(`lines.polylines must be a list of at most ${LINE_ART_LIMITS.maxPolylines} polylines.`);+ }+ resolved.polylines = input.polylines.map((polyline, index) => polylineFrom(polyline, index, resolved));+ const segments = resolved.polylines.reduce((total, polyline) => total + segmentCount(polyline), 0);+ if (segments > LINE_ART_LIMITS.maxPolylineSegments) {+ throw new RangeError(`lines.polylines hold ${segments} segments; the limit is ${LINE_ART_LIMITS.maxPolylineSegments}.`);+ }+ }+ return resolved;+}++function drawsOutlines(lines) {+ return lines.boundaries || lines.creaseAngle > 0 || lines.depthJump > 0;+}++// Both guides read the same texel as the frame, so every lookup is a texel fetch at an integer pixel.+const outlineFragment = `#version 300 es+precision highp float;+uniform sampler2D sourceTexture;+uniform sampler2D sceneDepth;+uniform sampler2D partTexture;+uniform sampler2D normalTexture;+uniform vec3 projectionDepth;+uniform float halfWidth;+uniform vec3 lineColor;+uniform float useBoundaries;+uniform float creaseCosine;+uniform float depthJump;+out vec4 outputColor;+ivec2 lastPixel;+vec3 partAt(ivec2 pixel) { return texelFetch(partTexture, clamp(pixel, ivec2(0), lastPixel), 0).rgb; }+vec3 normalAt(ivec2 pixel) { return normalize(texelFetch(normalTexture, clamp(pixel, ivec2(0), lastPixel), 0).rgb * 2.0 - 1.0); }+float depthAt(ivec2 pixel) {+ float ndc = texelFetch(sceneDepth, clamp(pixel, ivec2(0), lastPixel), 0).r * 2.0 - 1.0;+ return projectionDepth.z > 0.5 ? -(ndc - projectionDepth.y) / projectionDepth.x : (ndc + projectionDepth.x) / projectionDepth.y;+}+bool hasPart(vec3 part) { return dot(part, vec3(1.0)) > 0.0; }+bool differs(ivec2 center, ivec2 other, vec3 centerPart, vec3 centerNormal) {+ vec3 otherPart = partAt(other);+ if (useBoundaries > 0.5 && dot(abs(otherPart - centerPart), vec3(1.0)) > 0.001) return true;+ if (creaseCosine > -1.5 && hasPart(centerPart) && hasPart(otherPart) && dot(centerNormal, normalAt(other)) < creaseCosine) return true;+ return false;+}+bool depthEdge(ivec2 center, ivec2 offset, float centerDepth) {+ float jump = abs(depthAt(center + offset) + depthAt(center - offset) - 2.0 * centerDepth);+ return jump > depthJump * abs(centerDepth);+}+bool ringHasEdge(ivec2 center, int ring, vec3 centerPart, vec3 centerNormal, float centerDepth) {+ int diagonal = max(1, int(round(float(ring) * 0.70710678)));+ ivec2 offsets[4] = ivec2[4](ivec2(ring, 0), ivec2(0, ring), ivec2(diagonal, diagonal), ivec2(diagonal, -diagonal));+ for (int index = 0; index < 4; index++) {+ ivec2 offset = offsets[index];+ if (differs(center, center + offset, centerPart, centerNormal) || differs(center, center - offset, centerPart, centerNormal)) return true;+ if (depthJump > 0.0 && depthEdge(center, offset, centerDepth)) return true;+ }+ return false;+}+void main() {+ ivec2 pixel = ivec2(gl_FragCoord.xy);+ lastPixel = textureSize(sourceTexture, 0) - 1;+ vec4 source = texelFetch(sourceTexture, pixel, 0);+ vec3 centerPart = partAt(pixel);+ vec3 centerNormal = normalAt(pixel);+ float centerDepth = depthAt(pixel);+ float fullRings = floor(halfWidth);+ float partial = halfWidth - fullRings;+ float coverage = 0.0;+ for (int ring = 1; ring <= ${OUTLINE_RING_LIMIT}; ring++) {+ if (float(ring) > ceil(halfWidth)) break;+ if (ringHasEdge(pixel, ring, centerPart, centerNormal, centerDepth)) {+ coverage = float(ring) <= fullRings ? 1.0 : partial;+ break;+ }+ }+ outputColor = vec4(source.rgb * (1.0 - coverage) + lineColor * coverage, source.a * (1.0 - coverage) + coverage);+}`;++const polylineVertex = `#version 300 es+precision highp float;+uniform mat4 viewProjection;+uniform vec2 resolution;+uniform float lineWidth;+in vec3 startPoint;+in vec3 endPoint;+out vec2 localPixel;+flat out float segmentLength;+const float NEAR_W = 0.001;+void main() {+ vec4 startClip = viewProjection * vec4(startPoint, 1.0);+ vec4 endClip = viewProjection * vec4(endPoint, 1.0);+ if (startClip.w < NEAR_W && endClip.w < NEAR_W) {+ gl_Position = vec4(2.0, 2.0, 2.0, 1.0);+ localPixel = vec2(0.0);+ segmentLength = 0.0;+ return;+ }+ if (startClip.w < NEAR_W) startClip = mix(startClip, endClip, (NEAR_W - startClip.w) / (endClip.w - startClip.w));+ if (endClip.w < NEAR_W) endClip = mix(endClip, startClip, (NEAR_W - endClip.w) / (startClip.w - endClip.w));+ vec2 startPixel = startClip.xy / startClip.w * resolution * 0.5;+ vec2 endPixel = endClip.xy / endClip.w * resolution * 0.5;+ float segmentPixels = distance(startPixel, endPixel);+ vec2 along = segmentPixels > 0.0001 ? (endPixel - startPixel) / segmentPixels : vec2(1.0, 0.0);+ vec2 across = vec2(-along.y, along.x);+ float margin = lineWidth * 0.5 + 1.0;+ bool atEnd = gl_VertexID == 1 || gl_VertexID == 4 || gl_VertexID == 5;+ bool upper = gl_VertexID == 2 || gl_VertexID == 3 || gl_VertexID == 5;+ float lengthwise = atEnd ? segmentPixels + margin : -margin;+ float sideways = upper ? margin : -margin;+ vec4 anchor = atEnd ? endClip : startClip;+ vec2 pixel = (atEnd ? endPixel : startPixel) + along * (atEnd ? margin : -margin) + across * sideways;+ gl_Position = vec4(pixel / (resolution * 0.5) * anchor.w, anchor.z, anchor.w);+ localPixel = vec2(lengthwise, sideways);+ segmentLength = segmentPixels;+}`;++const polylineFragment = `#version 300 es+precision highp float;+uniform sampler2D sceneDepth;+uniform vec3 projectionDepth;+uniform float lineWidth;+uniform vec3 lineColor;+uniform float depthTest;+in vec2 localPixel;+flat in float segmentLength;+out vec4 outputColor;+float eyeDepth(float depth) {+ float ndc = depth * 2.0 - 1.0;+ return projectionDepth.z > 0.5 ? -(ndc - projectionDepth.y) / projectionDepth.x : projectionDepth.y / (ndc + projectionDepth.x);+}+void main() {+ float distanceToSegment = length(vec2(localPixel.x - clamp(localPixel.x, 0.0, segmentLength), localPixel.y));+ float alpha = clamp(lineWidth * 0.5 - distanceToSegment + 0.5, 0.0, 1.0);+ if (alpha <= 0.0) discard;+ if (depthTest > 0.5) {+ float sceneDistance = eyeDepth(texelFetch(sceneDepth, ivec2(gl_FragCoord.xy), 0).r);+ if (eyeDepth(gl_FragCoord.z) > sceneDistance * 1.005 + 0.002) discard;+ }+ outputColor = vec4(lineColor * alpha, alpha);+}`;++function uniformLocations(context, program, names) {+ return Object.fromEntries(names.map(name => [name, context.getUniformLocation(program, name)]));+}++function createColorTarget(context, width, height, floatColor, filter) {+ const texture = context.createTexture();+ context.bindTexture(context.TEXTURE_2D, texture);+ context.texImage2D(context.TEXTURE_2D, 0, floatColor ? context.RGBA16F : context.RGBA8, width, height, 0, context.RGBA,+ floatColor ? context.HALF_FLOAT : context.UNSIGNED_BYTE, null);+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, filter);+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, filter);+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_S, context.CLAMP_TO_EDGE);+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_T, context.CLAMP_TO_EDGE);+ const framebuffer = context.createFramebuffer();+ context.bindFramebuffer(context.FRAMEBUFFER, framebuffer);+ context.framebufferTexture2D(context.FRAMEBUFFER, context.COLOR_ATTACHMENT0, context.TEXTURE_2D, texture, 0);+ if (context.checkFramebufferStatus(context.FRAMEBUFFER) !== context.FRAMEBUFFER_COMPLETE) {+ throw new Error('Line art framebuffer allocation failed. Choose a lower quality preset.');+ }+ return { texture, framebuffer };+}++function polylineSegments(polyline) {+ const points = polyline.points;+ const segments = new Float32Array(segmentCount(polyline) * 6);+ for (let index = 0; index < segmentCount(polyline); index++) {+ segments.set(points[index], index * 6);+ segments.set(points[(index + 1) % points.length], index * 6 + 3);+ }+ return segments;+}++function createLineArtStage(state) {+ let outlineProgram = null;+ let outlineUniforms = null;+ let polylineProgram = null;+ let polylineUniforms = null;+ let vertexArray = null;+ let outputTarget = null;+ const guideTargets = new Map();+ const segmentBuffers = new Map();++ function guideTarget(renderer, name) {+ const { context } = renderer;+ if (!guideTargets.has(name)) {+ guideTargets.set(name, createColorTarget(context, renderer.canvas.width, renderer.canvas.height, false, context.NEAREST));+ }+ return guideTargets.get(name);+ }++ // The pass target is reused by the next pass, so each guide is copied out of it into a texture of its own.+ function captureGuide(renderer, name) {+ const { context, canvas } = renderer;+ const target = guideTarget(renderer, name);+ context.bindFramebuffer(context.READ_FRAMEBUFFER, renderer.passSupport.framebuffer);+ context.bindFramebuffer(context.DRAW_FRAMEBUFFER, target.framebuffer);+ context.blitFramebuffer(0, 0, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height, context.COLOR_BUFFER_BIT, context.NEAREST);+ }++ function releaseTargets(context) {+ if (outputTarget) {+ context.deleteTexture(outputTarget.texture);+ context.deleteFramebuffer(outputTarget.framebuffer);+ outputTarget = null;+ }+ for (const target of guideTargets.values()) {+ context.deleteTexture(target.texture);+ context.deleteFramebuffer(target.framebuffer);+ }+ guideTargets.clear();+ }++ function releaseSegmentBuffers(context, keep = new Set()) {+ for (const [key, entry] of segmentBuffers) {+ if (keep.has(key)) continue;+ context.deleteBuffer(entry.buffer);+ segmentBuffers.delete(key);+ }+ }++ function dispose(context) {+ releaseTargets(context);+ releaseSegmentBuffers(context);+ if (outlineProgram) context.deleteProgram(outlineProgram);+ if (polylineProgram) context.deleteProgram(polylineProgram);+ if (vertexArray) context.deleteVertexArray(vertexArray);+ outlineProgram = null;+ polylineProgram = null;+ vertexArray = null;+ }++ function projectionDepth(renderer) {+ const projection = renderer.projection;+ const orthographic = projection[15] === 1;+ return [projection[10], projection[14], orthographic ? 1 : 0];+ }++ function drawOutlines(renderer, lines, sourceTexture) {+ const { context } = renderer;+ if (!outlineProgram) {+ outlineProgram = compileProgram(context, fullscreenVertex, outlineFragment);+ outlineUniforms = uniformLocations(context, outlineProgram, ['sourceTexture', 'sceneDepth', 'partTexture', 'normalTexture',+ 'projectionDepth', 'halfWidth', 'lineColor', 'useBoundaries', 'creaseCosine', 'depthJump']);+ }+ context.useProgram(outlineProgram);+ const bindings = [[sourceTexture, 'sourceTexture'], [renderer.depthTexture, 'sceneDepth'], [guideTarget(renderer, 'part').texture, 'partTexture'], [guideTarget(renderer, 'normal').texture, 'normalTexture']];+ bindings.forEach(([texture, uniform], unit) => {+ context.activeTexture(context.TEXTURE0 + unit);+ context.bindTexture(context.TEXTURE_2D, texture);+ context.uniform1i(outlineUniforms[uniform], unit);+ });+ context.uniform3fv(outlineUniforms.projectionDepth, projectionDepth(renderer));+ context.uniform1f(outlineUniforms.halfWidth, lines.width / 2);+ context.uniform3fv(outlineUniforms.lineColor, lines.color);+ context.uniform1f(outlineUniforms.useBoundaries, lines.boundaries ? 1 : 0);+ context.uniform1f(outlineUniforms.creaseCosine, lines.creaseAngle > 0 ? Math.cos(lines.creaseAngle * Math.PI / 180) : -2);+ context.uniform1f(outlineUniforms.depthJump, lines.depthJump);+ context.drawArrays(context.TRIANGLES, 0, 3);+ }++ function segmentBufferFor(context, polyline) {+ const key = JSON.stringify([polyline.points, polyline.closed]);+ let entry = segmentBuffers.get(key);+ if (!entry) {+ const segments = polylineSegments(polyline);+ const buffer = context.createBuffer();+ context.bindBuffer(context.ARRAY_BUFFER, buffer);+ context.bufferData(context.ARRAY_BUFFER, segments, context.STATIC_DRAW);+ entry = { buffer, count: segments.length / 6 };+ segmentBuffers.set(key, entry);+ }+ return { key, ...entry };+ }++ function drawPolylines(renderer, polylines) {+ const { context, canvas } = renderer;+ if (!polylineProgram) {+ polylineProgram = compileProgram(context, polylineVertex, polylineFragment);+ polylineUniforms = uniformLocations(context, polylineProgram, ['viewProjection', 'resolution', 'lineWidth', 'sceneDepth',+ 'projectionDepth', 'lineColor', 'depthTest']);+ vertexArray = context.createVertexArray();+ }+ context.useProgram(polylineProgram);+ context.bindVertexArray(vertexArray);+ context.enable(context.BLEND);+ context.blendFuncSeparate(context.ONE, context.ONE_MINUS_SRC_ALPHA, context.ONE, context.ONE_MINUS_SRC_ALPHA);+ context.activeTexture(context.TEXTURE0);+ context.bindTexture(context.TEXTURE_2D, renderer.depthTexture);+ context.uniform1i(polylineUniforms.sceneDepth, 0);+ context.uniformMatrix4fv(polylineUniforms.viewProjection, false, renderer.viewProjection);+ context.uniform2f(polylineUniforms.resolution, canvas.width, canvas.height);+ context.uniform3fv(polylineUniforms.projectionDepth, projectionDepth(renderer));+ const startLocation = context.getAttribLocation(polylineProgram, 'startPoint');+ const endLocation = context.getAttribLocation(polylineProgram, 'endPoint');+ const used = new Set();+ for (const polyline of polylines) {+ const { key, buffer, count } = segmentBufferFor(context, polyline);+ used.add(key);+ context.bindBuffer(context.ARRAY_BUFFER, buffer);+ for (const [location, offset] of [[startLocation, 0], [endLocation, 12]]) {+ context.enableVertexAttribArray(location);+ context.vertexAttribPointer(location, 3, context.FLOAT, false, 24, offset);+ context.vertexAttribDivisor(location, 1);+ }+ context.uniform1f(polylineUniforms.lineWidth, polyline.width);+ context.uniform3fv(polylineUniforms.lineColor, polyline.color);+ context.uniform1f(polylineUniforms.depthTest, polyline.depthTest ? 1 : 0);+ context.drawArraysInstanced(context.TRIANGLES, 0, 6, count);+ }+ releaseSegmentBuffers(context, used);+ context.disable(context.BLEND);+ context.bindVertexArray(null);+ }++ // Draws the outlines, then the polylines, into a target of its own; null leaves the frame as it is.+ function apply(renderer, frame, effects, sourceTexture) {+ const lines = state.lines;+ if (!lines) return null;+ const outlines = drawsOutlines(lines);+ const polylines = lines.polylines?.length > 0;+ if (!outlines && !polylines) return null;+ if (!renderer.depthTexture) throw new Error('Line art requires depth:true on the renderer.');+ const { context, canvas } = renderer;+ outputTarget ??= createColorTarget(context, canvas.width, canvas.height, renderer.hdr, context.LINEAR);+ context.bindFramebuffer(context.FRAMEBUFFER, outputTarget.framebuffer);+ context.viewport(0, 0, canvas.width, canvas.height);+ context.disable(context.DEPTH_TEST);+ context.disable(context.CULL_FACE);+ context.bindVertexArray(null);+ if (outlines) {+ drawOutlines(renderer, lines, sourceTexture);+ } else {+ copyTexture(renderer, sourceTexture);+ }+ if (polylines) drawPolylines(renderer, lines.polylines);+ return outputTarget.texture;+ }++ const copyFragment = `#version 300 es+precision highp float;+uniform sampler2D sourceTexture;+out vec4 outputColor;+void main() { outputColor = texelFetch(sourceTexture, ivec2(gl_FragCoord.xy), 0); }`;+ let copyProgram = null;+ let copyUniform = null;++ function copyTexture(renderer, sourceTexture) {+ const { context } = renderer;+ if (!copyProgram) {+ copyProgram = compileProgram(context, fullscreenVertex, copyFragment);+ copyUniform = context.getUniformLocation(copyProgram, 'sourceTexture');+ }+ context.useProgram(copyProgram);+ context.activeTexture(context.TEXTURE0);+ context.bindTexture(context.TEXTURE_2D, sourceTexture);+ context.uniform1i(copyUniform, 0);+ context.drawArrays(context.TRIANGLES, 0, 3);+ }++ return {+ name: 'Line art',+ apply,+ captureGuide,+ releaseTargets,+ dispose(context) {+ dispose(context);+ if (copyProgram) context.deleteProgram(copyProgram);+ copyProgram = null;+ },+ };+}++/**+ * Adds outlines, toon light and polylines to a renderer. The renderer needs `depth: true` and installRenderPasses first.+ * A frame turns them on with `frame.lines`, an object from resolveLineArt (a recipe's `environment.lines`); a frame+ * without it draws exactly as before. Outlines draw the opaque and cutout geometry only, like render passes.+ */+export function installLineArt(renderer) {+ if (renderer.lineArt) return renderer;+ if (!renderer.passSupport) throw new Error('Line art needs installRenderPasses(renderer) from 67js/addons/render-passes first.');+ const state = { lines: null };+ const stage = createLineArtStage(state);+ renderer.postStages.push(stage);+ const drawFrame = renderer.render.bind(renderer);+ let toonBands = 0;++ // The toon uniforms are not in the core renderer's list, so the addon finds them itself after each program build.+ function locateToonUniforms() {+ for (const name of ['materialToon', 'toonDefault']) {+ renderer.uniforms[name] = renderer.context.getUniformLocation(renderer.program, name);+ }+ }++ // The default bands reach the main program as a uniform, and a rebuilt program starts again at zero.+ function pushDefaultToon() {+ if (!renderer.shaderFeatures?.has('toon')) return;+ renderer.context.useProgram(renderer.program);+ renderer.context.uniform1f(renderer.uniforms.toonDefault, toonBands);+ }++ const rebuildMainProgram = renderer.rebuildMainProgram.bind(renderer);+ renderer.rebuildMainProgram = () => {+ rebuildMainProgram();+ locateToonUniforms();+ pushDefaultToon();+ };++ function applyDefaultToon(bands) {+ toonBands = bands;+ if (bands > 0) renderer.requireFeatureNames(['toon']);+ pushDefaultToon();+ }++ function drawGuides(frame) {+ for (const [pass, name] of [['objectId', 'part'], ['normal', 'normal']]) {+ drawFrame({ ...frame, pass });+ stage.captureGuide(renderer, name);+ }+ }++ renderer.render = frame => {+ const lines = frame.lines && frame.pass === undefined && !frame.reflectionTarget && !renderer.benchmark ? frame.lines : null;+ applyDefaultToon(lines?.toon ?? 0);+ state.lines = lines;+ if (lines && drawsOutlines(lines)) drawGuides(frame);+ try {+ drawFrame(frame);+ } finally {+ state.lines = null;+ }+ };+ if (renderer.program) locateToonUniforms();+ renderer.lineArt = { resolve: resolveLineArt };+ return renderer;+}diff --git a/src/material-layers.js b/src/material-layers.jsindex 5fdd495f..a9d9488f 100644--- a/src/material-layers.js+++ b/src/material-layers.js@@ -22,6 +22,7 @@ export const MATERIAL_LAYER_DEFAULTS = Object.freeze({iridescence: 0,iridescenceIor: 1.3,iridescenceThickness: 400,+ toon: 0,});export const MATERIAL_LAYER_FIELDS = Object.freeze(Object.keys(MATERIAL_LAYER_DEFAULTS));@@ -39,6 +40,7 @@ export const MATERIAL_LAYER_SCALAR_RANGES = Object.freeze({iridescence: UNIT,iridescenceIor: Object.freeze([1, 3]),iridescenceThickness: Object.freeze([0, 1200]),+ toon: Object.freeze([0, 8]),});export const MATERIAL_LAYER_COLOR_RANGES = Object.freeze({@@ -110,7 +112,7 @@ export function dielectricReflectance(material) {/*** Plain values the renderer batches by: dielectric F0 (3), coat (2), sheen (4), transmission (4), attenuation colour (3),- * anisotropy strength and rotation (2), iridescence factor, film IOR and film thickness in nanometres (3), light mask (1).+ * anisotropy strength and rotation (2), iridescence factor, film IOR and film thickness in nanometres (3), light mask (1), toon bands (1).*/export function materialLayerState(material) {validateMaterialLayers(material);@@ -131,7 +133,8 @@ export function materialLayerState(material) {materialLayerValue(material, 'iridescenceIor'),materialLayerValue(material, 'iridescenceThickness'),material.lightMask ?? ALL_LIGHTS_MASK,+ materialLayerValue(material, 'toon'),];}-export const MATERIAL_LAYER_STATE_LENGTH = 22;+export const MATERIAL_LAYER_STATE_LENGTH = 23;diff --git a/src/material-snapshot.js b/src/material-snapshot.jsindex 3740d70a..3aa30684 100644--- a/src/material-snapshot.js+++ b/src/material-snapshot.js@@ -3,7 +3,7 @@// more than the checks it saves. tests/material-snapshot.test.js keeps the lists in step with the renderer's material fields./** Material fields that the renderer compares with the batch a part was committed to. */-export const SNAPSHOT_VALUE_FIELDS = Object.freeze(['map', 'normalMap', 'metallicRoughnessMap', 'occlusionMap', 'emissiveMap', 'tag', 'alphaMode', 'alphaCutoff', 'opacity', 'doubleSided', 'occlusionStrength', 'moss', 'boxMapScale', 'boxMapAxis', 'lightMapScale', 'layerMaps', 'lightMask', 'clearcoat', 'clearcoatRoughness', 'transmission', 'thickness', 'ior', 'sheenRoughness', 'specular', 'anisotropy', 'anisotropyRotation', 'iridescence', 'iridescenceIor', 'iridescenceThickness', 'attenuationDistance']);+export const SNAPSHOT_VALUE_FIELDS = Object.freeze(['map', 'normalMap', 'metallicRoughnessMap', 'occlusionMap', 'emissiveMap', 'tag', 'alphaMode', 'alphaCutoff', 'opacity', 'doubleSided', 'occlusionStrength', 'moss', 'boxMapScale', 'boxMapAxis', 'lightMapScale', 'layerMaps', 'lightMask', 'clearcoat', 'clearcoatRoughness', 'transmission', 'thickness', 'ior', 'sheenRoughness', 'specular', 'anisotropy', 'anisotropyRotation', 'iridescence', 'iridescenceIor', 'iridescenceThickness', 'attenuationDistance', 'toon']);/** Fields holding an array or descriptor, which can change in place. A material that sets one is never snapshotted. */export const SNAPSHOT_REFERENCE_FIELDS = Object.freeze(['normalScale', 'mossColor', 'lightMap', 'attenuationColor', 'sheenColor', 'specularColor']);@@ -54,6 +54,7 @@ export class MaterialSnapshot {this.iridescenceIor = material.iridescenceIor;this.iridescenceThickness = material.iridescenceThickness;this.attenuationDistance = material.attenuationDistance;+ this.toon = material.toon;}matches(material) {@@ -87,6 +88,7 @@ export class MaterialSnapshot {this.iridescence === material.iridescence &&this.iridescenceIor === material.iridescenceIor &&this.iridescenceThickness === material.iridescenceThickness &&- this.attenuationDistance === material.attenuationDistance;+ this.attenuationDistance === material.attenuationDistance &&+ this.toon === material.toon;}}diff --git a/src/renderer.js b/src/renderer.jsindex 8ec77ca2..cac1b5bf 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -84,6 +84,7 @@ function shaderFeaturesOfParts(parts) {if (material.anisotropy > 0) features.add('anisotropy');if (material.lightMask !== undefined) features.add('lightLinking');if (material.iridescence > 0) features.add('iridescence');+ if (material.toon > 0) features.add('toon');if ((material.alphaMode ?? 'opaque') !== 'opaque') features.add('alphaMode');if (materialLayerValue(material, 'clearcoat') > 0) features.add('clearcoat');if (materialLayerValue(material, 'sheenColor').some(channel => channel !== 0)) features.add('sheen');@@ -2029,6 +2030,7 @@ export class Renderer67 {this.context.uniform2f(this.uniforms.materialAnisotropy, layers[16], layers[17]);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.boxMapScale, batch.mapState[12]);this.context.uniform1i(this.uniforms.boxMapAxis, batch.mapState[13]);this.bindSplat(batch.splat);diff --git a/src/toon-shader.js b/src/toon-shader.jsnew file mode 100644index 00000000..d80915a0--- /dev/null+++ b/src/toon-shader.js@@ -0,0 +1,28 @@+import { registerShaderFeatureCode } from './shaders.js';++// Shader code for the toon material option. It lives outside shaders.js so a scene without banded light never carries it:+// the renderer asks for the `toon` feature only when a part has `toon` above 0, or when the line art addon turns on a+// default for every material (line-art.js).+//+// toonLight turns the cosine of a light into steps. Each light the material receives is stepped on its own, so a+// scene with a sun and a lamp shows two sets of bands. `materialToon` is the step count of the material and+// `toonDefault` the count for a material that sets none; 0 in both leaves the smooth light. The edge between steps is+// 0.02 of the light range wide, which antialiases without derivatives (those are undefined in the light loops).++const TOON_SOFTNESS = '0.02';++const TOON_DECLARATIONS = `uniform float materialToon;+uniform float toonDefault;+float toonLight(float light) {+ float bands = materialToon > 0.0 ? materialToon : toonDefault;+ if (bands <= 0.0) return light;+ float scaled = clamp(light, 0.0, 1.0) * bands;+ float band = floor(scaled);+ return (band + smoothstep(0.0, ${TOON_SOFTNESS} * bands, scaled - band)) / bands;+}+`;++registerShaderFeatureCode('toon', Object.freeze({+ declarations: TOON_DECLARATIONS,+ illuminateReflectance: ' normalLight = toonLight(normalLight);\n',+}));diff --git a/tests/light-linking.test.js b/tests/light-linking.test.jsindex 7146290e..38fc205a 100644--- a/tests/light-linking.test.js+++ b/tests/light-linking.test.js@@ -49,8 +49,8 @@ test('lightMaskFor treats a null list as every part', () => {});test('the layer state carries the mask and defaults to every light', () => {- assert.equal(materialLayerState({}).at(-1), ALL_LIGHTS_MASK);- assert.equal(materialLayerState({ lightMask: 5 }).at(-1), 5);+ assert.equal(materialLayerState({})[21], ALL_LIGHTS_MASK);+ assert.equal(materialLayerState({ lightMask: 5 })[21], 5);assert.equal(AREA_FIRST_BIT + MAX_AREA_LIGHTS, 32);});diff --git a/tests/line-art.test.js b/tests/line-art.test.jsnew file mode 100644index 00000000..fa9035af--- /dev/null+++ b/tests/line-art.test.js@@ -0,0 +1,140 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { LINE_ART_DEFAULTS, LINE_ART_LIMITS, resolveLineArt, installLineArt } from '../src/line-art.js';+import { createAgentScene } from '../src/agent-scene.js';+import { agentSceneModulesNeeded, loadAgentSceneModules } from '../src/agent-scene-modules.js';+import { AGENT_SCENE_CAPABILITIES } from '../src/agent-scene-capabilities.js';+import { createFragmentShader } from '../src/shaders.js';+import { Client } from '@modelcontextprotocol/client';+import { InMemoryTransport } from '@modelcontextprotocol/server';+import { createAgentMcpServer } from '../scripts/mcp-server.mjs';+import { materialLayerState, MATERIAL_LAYER_STATE_LENGTH } from '../src/material-layers.js';++const BASE_RECIPE = {+ format: '67js.scene', schemaVersion: 1,+ geometries: [{ id: 'box', type: 'box' }],+ materials: [{ id: 'gray', color: [0.5, 0.5, 0.5] }],+ nodes: [{ id: 'block', geometry: 'box', material: 'gray' }],+};++test('an empty lines object resolves to the documented defaults', () => {+ const lines = resolveLineArt({});+ assert.deepEqual(lines, { ...LINE_ART_DEFAULTS, color: [...LINE_ART_DEFAULTS.color] });+ assert.equal(lines.polylines, undefined);+});++test('lines fields are checked with messages that name the field', () => {+ assert.throws(() => resolveLineArt({ width: 0 }), /lines\.width must be a number between 0\.25 and 16/);+ assert.throws(() => resolveLineArt({ color: [1, 1] }), /lines\.color must be three/);+ assert.throws(() => resolveLineArt({ creaseAngle: 200 }), /lines\.creaseAngle/);+ assert.throws(() => resolveLineArt({ boundaries: 'yes' }), /lines\.boundaries must be true or false/);+ assert.throws(() => resolveLineArt({ colour: [0, 0, 0] }), /unknown field: colour/);+ assert.throws(() => resolveLineArt([]), /lines must be a JSON object/);+});++test('polylines take defaults from the outline settings and are bounded', () => {+ const lines = resolveLineArt({ width: 3, color: [1, 0, 0], polylines: [{ points: [[0, 0, 0], [1, 1, 1]] }, { points: [[0, 0, 0], [1, 0, 0], [1, 1, 0]], closed: true, depthTest: false, width: 6 }] });+ assert.deepEqual(lines.polylines[0], { points: [[0, 0, 0], [1, 1, 1]], color: [1, 0, 0], width: 3, depthTest: true, closed: false });+ assert.equal(lines.polylines[1].width, 6);+ assert.throws(() => resolveLineArt({ polylines: [{ points: [[0, 0, 0]] }] }), /needs 2 to 256 points/);+ assert.throws(() => resolveLineArt({ polylines: [{ points: [[0, 0, 0], [1, 0, 0]], dashed: true }] }), /unknown field: dashed/);+ const tooMany = Array.from({ length: LINE_ART_LIMITS.maxPolylines + 1 }, () => ({ points: [[0, 0, 0], [1, 0, 0]] }));+ assert.throws(() => resolveLineArt({ polylines: tooMany }), /at most 64 polylines/);+ const longLine = { points: Array.from({ length: 256 }, (_, index) => [index, 0, 0]) };+ assert.throws(() => resolveLineArt({ polylines: Array.from({ length: 17 }, () => longLine) }), /limit is 4096/);+});++test('the recipe markers load the line art module, and the recipe keeps the resolved lines', async () => {+ const recipe = { ...BASE_RECIPE, environment: { lines: { width: 2 } } };+ assert.ok(agentSceneModulesNeeded(recipe).includes('lineArt'));+ assert.ok(agentSceneModulesNeeded({ ...BASE_RECIPE, materials: [{ id: 'gray', toon: 3 }] }).includes('lineArt'));+ assert.ok(!agentSceneModulesNeeded(BASE_RECIPE).includes('lineArt'));+ await loadAgentSceneModules(recipe);+ const scene = createAgentScene(recipe);+ assert.equal(scene.environment.lines.width, 2);+ assert.deepEqual(scene.environment.lines.color, [...LINE_ART_DEFAULTS.color]);+ assert.equal(createAgentScene(BASE_RECIPE).environment.lines, undefined);+});++test('a recipe edit with lines null removes the outlines', async () => {+ const recipe = { ...BASE_RECIPE, environment: { lines: {} } };+ await loadAgentSceneModules(recipe);+ const scene = createAgentScene(recipe);+ scene.apply([{ type: 'set-environment', environment: { lines: { width: 4 } } }], { expectedRevision: scene.revision });+ assert.equal(scene.environment.lines.width, 4);+ scene.apply([{ type: 'set-environment', environment: { lines: null } }], { expectedRevision: scene.revision });+ assert.equal(scene.environment.lines, undefined);+});++test('a material toon value is a layer field with its own range and shows in the batching state', async () => {+ const recipe = { ...BASE_RECIPE, materials: [{ id: 'gray', color: [0.5, 0.5, 0.5], toon: 3 }] };+ await loadAgentSceneModules(recipe);+ assert.equal(createAgentScene(recipe).parts[0].material.toon, 3);+ assert.throws(() => createAgentScene({ ...recipe, materials: [{ id: 'gray', toon: 9 }] }), /toon must be between 0 and 8/);+ const state = materialLayerState({ tag: 'surface', toon: 4 });+ assert.equal(state.length, MATERIAL_LAYER_STATE_LENGTH);+ assert.equal(state.at(-1), 4);+ assert.equal(materialLayerState({ tag: 'surface' }).at(-1), 0);+});++test('the toon feature splices its light steps into illuminate, and no other shader mentions it', () => {+ const toonShader = createFragmentShader(['toon']);+ assert.match(toonShader, /float toonLight\(float light\)/);+ assert.match(toonShader, /normalLight = toonLight\(normalLight\);/);+ assert.doesNotMatch(createFragmentShader([]), /toonLight/);+});++test('scene_capabilities names the field and the guide', () => {+ assert.equal(AGENT_SCENE_CAPABILITIES.lineArt.field, 'environment.lines');+ assert.equal(AGENT_SCENE_CAPABILITIES.lineArt.note, 'docs/line-art.md');+ for (const field of [...Object.keys(LINE_ART_DEFAULTS), 'polylines']) assert.ok(AGENT_SCENE_CAPABILITIES.lineArt.fields.includes(field), `${field} missing`);+ assert.ok(AGENT_SCENE_CAPABILITIES.materialLayers.toon.note);+ assert.deepEqual(AGENT_SCENE_CAPABILITIES.materialLayers.toon.range, [0, 8]);+});++test('installLineArt needs render passes first and leaves a frame without lines untouched', () => {+ assert.throws(() => installLineArt({ postStages: [], render() {} }), /installRenderPasses/);+ const drawn = [];+ const renderer = { postStages: [], passSupport: {}, render: frame => drawn.push(frame), rebuildMainProgram() {} };+ installLineArt(renderer);+ const frame = { eye: [0, 0, 5] };+ renderer.render(frame);+ assert.deepEqual(drawn, [frame]);+ assert.equal(renderer.postStages.length, 1);+ assert.equal(installLineArt(renderer), renderer);+});++test('the MCP scene_create schema accepts lines (or null) and a material toon', async () => {+ const server = createAgentMcpServer({ async close() {} });+ const client = new Client({ name: '67js-line-art-test', version: '1' });+ const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();+ await server.connect(serverTransport);+ await client.connect(clientTransport);+ const { tools } = await client.listTools();+ const environment = tools.find(tool => tool.name === 'scene_create').inputSchema.properties.recipe.properties.environment;+ const options = environment.properties.lines.anyOf;+ assert.deepEqual(options.map(option => option.type).sort(), ['null', 'object']);+ await client.close();+ await server.close();+});++test('the addon finds the toon uniforms after the main program is rebuilt', () => {+ const located = [];+ const context = {+ getUniformLocation: (program, name) => {+ located.push(`${program}:${name}`);+ return name;+ },+ useProgram() {},+ uniform1f() {},+ };+ const renderer = {+ postStages: [], passSupport: {}, render() {}, program: 'first', uniforms: {}, context,+ rebuildMainProgram() { this.program = 'rebuilt'; },+ };+ installLineArt(renderer);+ assert.deepEqual(located, ['first:materialToon', 'first:toonDefault']);+ renderer.rebuildMainProgram();+ assert.equal(renderer.uniforms.materialToon, 'materialToon');+ assert.ok(located.includes('rebuilt:toonDefault'));+});diff --git a/tests/support/preview-harness.mjs b/tests/support/preview-harness.mjsindex 0422eecd..a652118b 100644--- a/tests/support/preview-harness.mjs+++ b/tests/support/preview-harness.mjs@@ -74,7 +74,9 @@ export function previewHarness() {setManifoldModuleLoader: () => {},needsPhysics, loadPhysicsEngine, physicsEngineLoaded, setPhysicsModuleLoader: () => {},parseGltf, Uint8Array, atob, sessionProblem,- applyProjection, mat4, resolveAgentShadow, agentEnvironmentMap, agentSceneModule, BRANCH_LIMITS, createBranchStore, printableLabel,+ applyProjection, mat4, resolveAgentShadow, agentEnvironmentMap, BRANCH_LIMITS,+ // Line art needs a real renderer with render passes, so the fault-injected one gets a no-op install.+ agentSceneModule: name => (name === 'lineArt' ? { installLineArt: renderer => renderer } : agentSceneModule(name)), createBranchStore, printableLabel,Renderer67: FaultInjectedRenderer, skyOpenness: () => 1, fogSkyVisibility: () => null,// The fault-injected renderer has no floor part; the automatic floor is covered by agent-ground.test.js and the live smokes.agentGroundPlacement: () => null, createAgentGround: () => null, placeAgentGround: () => {}, FLOOR_COLOR: [0.22, 0.22, 0.21], groundBounceEnvironment: (parts, environment) => environment,diff --git a/vite.lib.config.js b/vite.lib.config.jsindex d4ffbb63..a0591aa0 100644--- a/vite.lib.config.js+++ b/vite.lib.config.js@@ -50,6 +50,7 @@ export default defineConfig({'camera-effects': 'src/camera-effects.js','water-reflection': 'src/water-reflection.js','render-passes': 'src/render-passes.js',+ 'line-art': 'src/line-art.js','material-layer-shaders': 'src/material-layer-shaders.js','light-linking': 'src/light-linking.js','cascaded-shadows': 'src/cascaded-shadows.js',