3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Merge remote-tracking branch 'origin/main' into w2057m
Nguyen Xuan An0f9144dec72a
diff --cc docs/agent-scene.mdindex fc7ebd04,c9854cda..9fbefebe--- a/docs/agent-scene.md+++ b/docs/agent-scene.md@@@ -107,7 -107,9 +107,10 @@@ The defaults are `surface`, `[0.5, 0.5The tag can be `surface`, `unlit`, `liquid`, `foam`, `water`, `foliage` or `grass` (the last two sway in `environment.wind`) and remains fixed during editing.`liquid` refracts and tints what lies behind it: its color is the light one metre lets through, so `[0.56, 0.84, 0.88]` is clear water. `foam` draws soft blended droplets.`water` is a flat mirror sheet (a plane or thin box). The largest one sets the water level: everything below it fades by `exp(-absorption * depth)` per colour channel (`absorption` is RGB per metre), and its shadows are lifted and softened with depth times `turbidity` (0 clear to 1 murky). `waterPreset` fills both: `lake` (default), `sea` or `river`. A shadow on the surface is faint and never darkens the reflection; give the sheet an `opacity` below one when the bed should show. The sun's leg to a bed is bent at n = 1.33 and absorbed over `depth / cos(theta_t)`, so a clear lake keeps little shadow contrast on a bed 4 m down, and a murky river keeps about a quarter.+`caustics` on a water material (`{ scale, speed, intensity, depthFade }`, off unless given; `null` or `intensity: 0` switches it off) draws a moving net of bright lines on every opaque surface below the water level. Two rotated Voronoi nets, warped by a slow pan, are projected from the sun along the refracted ray onto the surface and added to the sun's light, so a shadow still blocks them and a surface turned from the sun gets none. `scale` is metres across one cell (0.05 to 50, default 1.2), `speed` how fast the net drifts (0 to 10, default 0.5), `intensity` the extra sun on a line at the surface (0 to 10, default 2) and `depthFade` the loss per metre of depth, `exp(-depthFade * depth)` (0 to 10, default 0.35). It follows the renderer time the water waves use (`rendererTime` of a render job), so a still at one time is a frozen net. Raster only: the path tracer draws no caustics. The level comes from the largest flat water part, as for absorption.+ `waterRamp` (off by default) paints the sea in flat anime tones: `{ colors: [3 to 5 RGB, deep to bright], drivers: { height, facing, sun } (weights 0 to 1, default height 1), heightRange: [low, high] (world y, default -1 to 1), steps: hard (default) or soft, crestTint: { color, strength } }`. A weighted mix of the drivers picks the tone: `height` is world y across `heightRange`, `facing` rises toward the horizon, `sun` is how squarely a wave face meets the key light. The ramp owns the colour, so the sky reflection and the lit shading are not drawn; foam and the sun glint still add on top. `crestTint` colours the folded crests the `foam` group marks, wider than the foam itself. It needs the water tag; depth and a shallow and deep pair are not drivers yet.++ Ramp colours are display colours. A stop given as `#rrggbb` shows on screen as that hex; three numbers show as the sRGB encoding of those linear values (`[0.04, 0.4, 0.5]` shows as `#38aabc`). The shader maps each stop back through the exposure and the view transform (ACES, AgX or Neutral), so the tone curve does not shift it, and the distance haze (`fogDensity`) stays off the ramp. Measured on 640x360 stills under the default view: `#19617c` and `#38aabc` measured exactly as requested; a bright stop `#95edf3` measured `#99eef4` on lit faces. Things that run after the shader still change the picture: bloom, grade, vignette, grain, a look and the volume fog step, and foam, the sun glint and the crest tint add on top by design.Any `opacity` below one draws the material blended after opaque geometry, which suits glass, windows and visors. `alphaMode` accepts `opaque`, `blend` or `add` (additive light for sparks, trails and glow); the MCP tools accept the same three, and `scene_capabilities` topic `alphaModes` lists them. Cutout masking (`mask`) needs a texture and is not available in procedural recipes.`doubleSided` draws and lights both faces of thin sheets.Changing the tag, opacity, blending, sidedness or pattern replaces the affected parts in the renderer; color, metallic, roughness and emission edits keep them.diff --cc scripts/mcp-server.mjsindex b595397d,58ba0ee3..fcd48e64--- a/scripts/mcp-server.mjs+++ b/scripts/mcp-server.mjs@@@ -162,7 -162,7 +162,8 @@@ const waterFields =waterPreset: z.enum(['lake', 'sea', 'river']).optional().describe('Water tag.'),absorption: z.array(z.number().min(0).max(20)).length(3).optional().describe('Water tag: RGB per metre.'),turbidity: unit.optional().describe('Water tag: 0 clear to 1 murky.'),+ caustics: z.record(z.string(), z.unknown()).nullable().optional().describe('Water tag: sun caustics on the bed, {scale, speed, intensity, depthFade}; scene_capabilities water.'),+ waterRamp: z.record(z.string(), z.unknown()).optional().describe('Water tag: flat colour ramp, see scene_capabilities water.'),};const subsurfaceTriple = ([minimum, maximum]) => z.tuple([0, 1, 2].map(() => z.number().min(minimum).max(maximum)));const subsurface = z.strictObject({@@@ -198,7 -198,7 +199,7 @@@ const materialFields =};const materialTag = z.enum(['surface', 'unlit', 'liquid', 'foam', 'foliage', 'grass', 'water']).optional().describe('Surface kind; liquid, foam, foliage, grass and water change how it renders: see scene_capabilities materialTags and water.');const materialCore = coreOf(materialFields, ['color', 'metallic', 'roughness', 'emission', 'opacity']);- const materialMore = 'More fields: tag, alphaMode, doubleSided, emissiveColor, map, pattern, subsurface, snippet, wear, retro, waterPreset, absorption, turbidity, caustics and the layers clearcoat, clearcoatRoughness, transmission, thickness, ior, dispersion, attenuationColor, attenuationDistance, sheenColor, sheenRoughness, specular, specularColor, anisotropy, anisotropyRotation, iridescence, iridescenceIor, iridescenceThickness, toon, toonColors; see scene_capabilities materialFields.';-const materialMore = 'More fields: tag, alphaMode, doubleSided, emissiveColor, map, pattern, subsurface, snippet, wear, retro, waterPreset, absorption, turbidity, waterRamp and the layers clearcoat, clearcoatRoughness, transmission, thickness, ior, dispersion, attenuationColor, attenuationDistance, sheenColor, sheenRoughness, specular, specularColor, anisotropy, anisotropyRotation, iridescence, iridescenceIor, iridescenceThickness, toon, toonColors; see scene_capabilities materialFields.';++const materialMore = 'More fields: tag, alphaMode, doubleSided, emissiveColor, map, pattern, subsurface, snippet, wear, retro, waterPreset, absorption, turbidity, caustics, waterRamp and the layers clearcoat, clearcoatRoughness, transmission, thickness, ior, dispersion, attenuationColor, attenuationDistance, sheenColor, sheenRoughness, specular, specularColor, anisotropy, anisotropyRotation, iridescence, iridescenceIor, iridescenceThickness, toon, toonColors; see scene_capabilities materialFields.';const materialListed = (strictSchema, description, extraCore) => listed('materialFields', strictSchema, `${description} ${materialMore}`, { ...extraCore, ...materialCore });const liquidOptics = z.strictObject({thickness: z.number().min(0).max(100).optional().describe('Metres of liquid where it faces the camera.'),diff --cc src/agent-scene-capabilities.jsindex 0de43129,8289d521..5e6d5c64--- a/src/agent-scene-capabilities.js+++ b/src/agent-scene-capabilities.js@@@ -269,7 -269,7 +269,7 @@@ export const AGENT_SCENE_CAPABILITIES]),materialTags: Object.freeze(['surface', 'unlit', 'liquid', 'foam', 'foliage', 'grass', 'water']),// A flat water sheet; the largest sets the level. A bed d metres down keeps exp(-absorption * d) per channel (RGB per metre).- water: Object.freeze({ presets: WATER_PRESETS_SUMMARY, default: 'lake', note: 'absorption 0..20, turbidity 0..1 softens shadows below; opacity under 1 shows the bed. caustics {scale m 0.05-50, speed 0-10, intensity 0-10, depthFade 0-10} adds a moving sun net on surfaces below (off unless given, raster only).' }),- water: Object.freeze({ presets: WATER_PRESETS_SUMMARY, default: 'lake', note: 'absorption 0..20, turbidity 0..1 softens shadows below; opacity under 1 shows the bed. waterRamp {colors [3-5 display colours, deep to bright: #rrggbb shows as that hex, rgb numbers as their sRGB encoding], drivers {height, facing, sun: weights 0-1; default height 1}, heightRange [low, high] world y (-1, 1), steps hard|soft (hard), crestTint {color, strength 0-1}} paints flat tones that own the colour (no sky reflection; foam and glint stay), off by default.' }),++ water: Object.freeze({ presets: WATER_PRESETS_SUMMARY, default: 'lake', note: 'absorption 0..20, turbidity 0..1 softens shadows below; opacity under 1 shows the bed. caustics {scale m 0.05-50, speed 0-10, intensity 0-10, depthFade 0-10} adds a moving sun net on surfaces below (off unless given, raster only). waterRamp {colors [3-5 display colours, deep to bright: #rrggbb shows as that hex, rgb numbers as their sRGB encoding], drivers {height, facing, sun: weights 0-1; default height 1}, heightRange [low, high] world y (-1, 1), steps hard|soft (hard), crestTint {color, strength 0-1}} paints flat tones that own the colour (no sky reflection; foam and glint stay), off by default.' }),materialLayers: Object.freeze(Object.fromEntries(MATERIAL_LAYER_FIELDS.map(field => [field, Object.freeze({range: MATERIAL_LAYER_SCALAR_RANGES[field] ?? MATERIAL_LAYER_COLOR_RANGES[field] ?? 'above 0',note: MATERIAL_LAYER_NOTES[field],diff --cc src/agent-scene-modules.jsindex 109a8eac,99c29a23..d5d4f503--- a/src/agent-scene-modules.js+++ b/src/agent-scene-modules.js@@@ -87,7 -87,7 +87,7 @@@ const MODULE_MARKERS =glitterShader: ['"waterGlint"'],// Any mention loads it, because an edit can set the density through a path or the environment of an operation.fogShader: ['volumeFog', 'localVolumes', '"add-explosion"'],- worldMaterials: ['"tag":"water"', '"waterPreset"', '"absorption"', '"turbidity"', '"caustics"', '"vegetation":{', '"tag":"foliage"', '"tag":"grass"', '"tag":"terrain"', '"tag":"fire"', '"tag":"smoke"', '"moss"'],- worldMaterials: ['"tag":"water"', '"waterPreset"', '"absorption"', '"turbidity"', '"waterRamp"', '"vegetation":{', '"tag":"foliage"', '"tag":"grass"', '"tag":"terrain"', '"tag":"fire"', '"tag":"smoke"', '"moss"'],++ worldMaterials: ['"tag":"water"', '"waterPreset"', '"absorption"', '"turbidity"', '"caustics"', '"waterRamp"', '"vegetation":{', '"tag":"foliage"', '"tag":"grass"', '"tag":"terrain"', '"tag":"fire"', '"tag":"smoke"', '"moss"'],terrain: ['"type":"terrain"'],// No recipe text asks for it, only the path option of scene_inspect does, so a host loads it with loadAllAgentSceneModules.navigation: [],diff --cc src/agent-scene.jsindex 76dc2e28,ab95198a..aa798e2d--- a/src/agent-scene.js+++ b/src/agent-scene.js@@@ -61,7 -61,7 +61,7 @@@ const CAMERA_DEFAULTS = Object.freeze(const RENDER_FLAG_FIELDS = ['visible', 'castShadow', 'receiveShadow', 'holdout', 'shadowCatcher', 'shadowOnly'];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', 'rayVisibility', ...RENDER_FLAG_FIELDS];- const MATERIAL_FIELDS = ['color', 'metallic', 'roughness', 'emission', 'emissiveColor', 'opacity', 'alphaMode', 'doubleSided', 'pattern', 'map', 'subsurface', 'snippet', 'waterPreset', 'absorption', 'turbidity', 'caustics', 'aov', 'wear', 'retro', 'emissionMask', 'toonColors', ...MATERIAL_LAYER_FIELDS];-const MATERIAL_FIELDS = ['color', 'metallic', 'roughness', 'emission', 'emissiveColor', 'opacity', 'alphaMode', 'doubleSided', 'pattern', 'map', 'subsurface', 'snippet', 'waterPreset', 'absorption', 'turbidity', 'waterRamp', 'aov', 'wear', 'retro', 'emissionMask', 'toonColors', ...MATERIAL_LAYER_FIELDS];++const MATERIAL_FIELDS = ['color', 'metallic', 'roughness', 'emission', 'emissiveColor', 'opacity', 'alphaMode', 'doubleSided', 'pattern', 'map', 'subsurface', 'snippet', 'waterPreset', 'absorption', 'turbidity', 'caustics', 'waterRamp', 'aov', 'wear', 'retro', 'emissionMask', 'toonColors', ...MATERIAL_LAYER_FIELDS];const PATTERN_TEXTURE_FIELDS = ['map', 'normalMap', 'metallicRoughnessMap', 'emissiveMap', 'layerMaps', 'transmissionMap', 'aovMap', 'emissionFresnel', 'boxMapScale', 'boxMapAxis'];const CAMERA_FIELDS = [...Object.keys(CAMERA_DEFAULTS), ...CAMERA_LENS_FIELDS];// Fields of an optional shader module join the list here, so the long list above stays a line each material feature does not rewrite.diff --cc src/world-material-shader.jsindex 375f553b,bd925ca9..da8410ff--- a/src/world-material-shader.js+++ b/src/world-material-shader.js@@@ -47,12 -46,93 +47,97 @@@ export function copyWaterFields(value,}const absorption = value.absorption === undefined ? undefined : toVector(value.absorption, 3, 'absorption');Object.assign(result, resolveWaterMaterial({ ...value, absorption }));+ const caustics = resolveWaterCaustics(value.caustics);+ if (caustics) {+ result.caustics = caustics;+ }}+ export const WATER_RAMP_COLOR_COUNT = Object.freeze([3, 5]);+ const WATER_RAMP_DRIVERS = Object.freeze(['height', 'facing', 'sun']);+ const WATER_RAMP_FIELDS = Object.freeze(['colors', 'drivers', 'heightRange', 'steps', 'crestTint']);+ const WATER_RAMP_STEPS = Object.freeze(['hard', 'soft']);+ const WATER_RAMP_DEFAULT_HEIGHT_RANGE = Object.freeze([-1, 1]);++ const HEX_COLOR = /^#[0-9a-f]{6}$/i;++ function srgbByteToLinear(byte) {+ const encoded = byte / 255;+ return encoded <= 0.04045 ? encoded / 12.92 : ((encoded + 0.055) / 1.055) ** 2.4;+ }++ /** A ramp colour: three linear numbers (0 to 16) whose sRGB encoding is the colour shown, or a `#rrggbb` hex shown as that hex. */+ function rampColor(value, name, toVector) {+ if (typeof value === 'string' && HEX_COLOR.test(value)) {+ return Object.freeze([1, 3, 5].map(offset => srgbByteToLinear(Number.parseInt(value.slice(offset, offset + 2), 16))));+ }+ const color = toVector(value, 3, name);+ if (color.some(component => !Number.isFinite(component) || component < 0 || component > 16)) {+ throw new RangeError(`${name} must be three numbers from 0 to 16, such as [0.1, 0.5, 0.6].`);+ }+ return Object.freeze([...color]);+ }++ function rampDrivers(drivers) {+ const given = drivers ?? { height: 1 };+ const unknown = Object.keys(given).filter(name => !WATER_RAMP_DRIVERS.includes(name));+ if (unknown.length > 0) {+ throw new RangeError(`waterRamp.drivers has no ${unknown.join(', ')}; pass weights for ${WATER_RAMP_DRIVERS.join(', ')}, such as { "height": 1, "sun": 0.5 }.`);+ }+ const weights = WATER_RAMP_DRIVERS.map(name => given[name] ?? 0);+ if (weights.some(weight => !Number.isFinite(weight) || weight < 0 || weight > 1) || weights.every(weight => weight === 0)) {+ throw new RangeError('waterRamp.drivers weights must be numbers from 0 to 1 with at least one above 0, such as { "height": 1, "sun": 0.5 }.');+ }+ return Object.freeze(Object.fromEntries(WATER_RAMP_DRIVERS.map((name, index) => [name, weights[index]])));+ }++ /**+ * The stylized colour ramp of a water material. `colors` (3 to 5, deep to bright) are picked by a weighted mix of drivers: `height`+ * (world y across `heightRange`, default -1 to 1), `facing` (grazing view, 1 at the horizon) and `sun` (how squarely the wave face meets+ * the sun). `steps` hard (default) keeps flat tones, soft blends them. `crestTint` { color, strength 0-1 } colours the folded crests.+ */+ export function resolveWaterRamp(ramp, toVector) {+ if (typeof ramp !== 'object' || ramp === null || Array.isArray(ramp)) {+ throw new RangeError('waterRamp must be an object such as { "colors": [[0.02, 0.2, 0.3], [0.1, 0.6, 0.7], [0.5, 0.95, 0.95]] }.');+ }+ const unknown = Object.keys(ramp).filter(name => !WATER_RAMP_FIELDS.includes(name));+ if (unknown.length > 0) {+ throw new RangeError(`waterRamp has no ${unknown.join(', ')}; use ${WATER_RAMP_FIELDS.join(', ')}.`);+ }+ const [minimumColors, maximumColors] = WATER_RAMP_COLOR_COUNT;+ if (!Array.isArray(ramp.colors) || ramp.colors.length < minimumColors || ramp.colors.length > maximumColors) {+ throw new RangeError(`waterRamp.colors must list ${minimumColors} to ${maximumColors} colours from deep to bright, such as [[0.02, 0.2, 0.3], [0.1, 0.6, 0.7], [0.5, 0.95, 0.95]].`);+ }+ const steps = ramp.steps ?? 'hard';+ if (!WATER_RAMP_STEPS.includes(steps)) {+ throw new RangeError(`waterRamp.steps must be one of ${WATER_RAMP_STEPS.join(', ')}, such as hard.`);+ }+ const heightRange = ramp.heightRange ?? WATER_RAMP_DEFAULT_HEIGHT_RANGE;+ if (!Array.isArray(heightRange) || heightRange.length !== 2 || !heightRange.every(Number.isFinite) || heightRange[0] >= heightRange[1]) {+ throw new RangeError('waterRamp.heightRange must be [low, high] world heights with low below high, such as [-1, 1].');+ }+ const resolved = { colors: Object.freeze(ramp.colors.map((color, index) => rampColor(color, `waterRamp.colors[${index}]`, toVector))), drivers: rampDrivers(ramp.drivers), heightRange: Object.freeze([...heightRange]), steps };+ if (ramp.crestTint !== undefined) {+ const { color, strength = 1, ...extra } = ramp.crestTint ?? {};+ if (color === undefined || Object.keys(extra).length > 0 || !Number.isFinite(strength) || strength < 0 || strength > 1) {+ throw new RangeError('waterRamp.crestTint must be { "color": [r, g, b], "strength": 0 to 1 }, such as { "color": [0.7, 1, 0.9], "strength": 0.8 }.');+ }+ resolved.crestTint = Object.freeze({ color: rampColor(color, 'waterRamp.crestTint.color', toVector), strength });+ }+ return Object.freeze(resolved);+ }++ /** Copies a recipe material's `waterRamp`: it needs the water tag. */+ export function copyWaterRamp(value, result, toVector) {+ if (value.waterRamp === undefined) {+ return;+ }+ if (result.tag !== 'water') {+ throw new RangeError('waterRamp needs the water tag.');+ }+ result.waterRamp = resolveWaterRamp(value.waterRamp, toVector);+ }+function partBounds(part) {const { positions } = part.geometry;const { matrix } = part;@@@ -174,7 -253,11 +259,12 @@@ export const worldMaterialSlots =// Shadow of a surface under murky water: the classic shadow map read over a wider disc. A cascaded map is read through// its own macro and keeps its filter.declarations: `uniform vec4 waterBody;+${causticsDeclarations}+ uniform vec4 waterRampHead;+ uniform vec3 waterRampWeights;+ uniform vec2 waterRampHeightRange;+ uniform vec4 waterRampCrest;+ uniform vec3 waterRampColors[5];uniform vec3 waterAbsorption;uniform sampler2D waterScene;uniform mat4 viewProjection;