3D engine that AI agents drive
Merge remote-tracking branch 'origin/main' into HEAD
Nguyen Xuan Ana4a899c6d5bf
diff --cc CHANGELOG.mdindex 547f7090,8c82b3f8..53267afe--- a/CHANGELOG.md+++ b/CHANGELOG.md@@@ -2,7 -2,13 +2,14 @@@## Unreleased+- Make the default look good without tuning. Recipes get an image-based environment: `environment.setup` (`daylight` by default, `overcast`, `sunset`, `studio`) selects a small procedural sky map generated in code, plus its key light, with `environmentIntensity` and `environmentRotation`; no URLs or files from tool input. `effects.ambientOcclusion` (0 to 1) and `ambientOcclusionRadius` add screen-space ambient occlusion from the depth texture. It is off by default: measured with `npm run reference -- --ambient-occlusion`, it improved no score by more than 0.002 (strength 0.8: -0.0019 in foliage, +0.0047 in lit-interior; strength 0.4, radius 0.3: within +0.0013 everywhere). When used it compiles and allocates on first use and costs about 16 ms on a 190 ms SwiftShader frame (8 percent). `scene_capabilities` lists `lightingSetups`, `defaultLightingSetup` and `ambientOcclusion`. **This changes the look of every existing recipe that sets no lighting**: it now has an environment map, a key color of `[3, 2.8, 2.5]` instead of `[5.2, 4.7, 4.1]`, and a fill intensity of `0` instead of `1`. The engine default is unchanged (occlusion off, no map) so code scenes keep their pixels. Screen-space reflections are not part of this. The `scene:lit-box` size budget rises by 100 bytes gzip for the environment and occlusion code. The reference suite gets `--ambient-occlusion`, and its Blender renderer drops triangles that repeat a vertex, which crashed Blender on the skinned character+ - Measure and cut the cost of the MCP tools (issue #65). `node scripts/measure-tool-cost.mjs` reports each tool schema and each typical result in bytes and estimated tokens (3.5 characters a token). The 22 schemas came to 373,388 bytes (about 107,000 tokens) and are now 173,216 (about 49,500): recipe geometries and asset recipes point to `mesh_edit`, `mesh_stack` and `scene_create` for the mesh edit, stack and motion detail instead of repeating about 45 KB and 35 KB in each tool, and vectors, colours, quaternions and IDs are shared `$ref` definitions. The backend still validates every field. `tests/tool-cost.test.js` fails when the schema total, one tool's schema or a typical result passes `bench/tool-cost-budgets.json`. A small mesh change is 187 bytes as `mesh_edit` against 1,254 as dispose plus a whole-recipe `scene_create` on a 7-node recipe.+ - Return diagnostics that name the next action (issue #66). `mesh_inspect` lint adds `intersecting-parts`, `self-intersection`, `thin-wall`, `floating-part` and `asymmetric` warnings from the BVH, with face budgets and a `skippedChecks` list; they never turn `ok` off and the agent modeling suite does not grade them. `scene_inspect` adds `diagnostics` for material fields the renderer ignores, blended transmission and unused materials or geometries, and asset inspection adds decoder warnings (accepted but not drawn features), skinned meshes with no joints, joints with no skin and assets with no clips. Error messages now list allowed fields, known IDs and the next call: a missing session (the largest group, 21 of 68 recorded errors) names the active session or says to call `scene_create`, and page errors no longer carry the bundle stack. `tests/tool-errors.test.js` walks 27 error paths and rejects a message with no next action.++ - Add authored rigs (#46): a recipe `armatures` list (bones with a name, parent, head and tail, or a `humanoid` or `quadruped` preset fitted to a mesh node's bounds, with `landmarks` to move bones), and a `skin` on a mesh node that binds it with automatic weights, plus a `set-armatures` operation. Each bone becomes a recipe node, so keyframed bone rotation and `motion.ik` run through the same paths as imported rigs, and humanoid bones carry the `HUMANOID_BONES` names so `retargetClip` maps them. Weights are the distance to each bone segment with an inverse-fourth-power falloff, the nearest four kept; they depend only on position, so hard-edge seams never open. `scene_inspect` gains `rig`: unweighted vertices, bones with no vertices, and joint axis problems (zero-length bones, straight limb chains, knees that bend backward). `scene_export_glb` writes the bones and a walk clip baked at 30 frames a second from tracks and IK. Add the `rigged-walker` task to the agent modeling suite, with a reference and three failing variants. Blender 5.2 opens the exported walker as 1 armature, 21 bones, 14 meshes with 21 vertex groups each and 1 action of 24 frames; three.js loads 14 skinned meshes and the clip.+ - Load agent scene code on demand: mesh editing, `lathe`/`extrude`/`tube`, material `pattern` and `motion` now sit behind `loadAgentSceneModules(recipe)` (or `loadAllAgentSceneModules()`), and `createAgentScene` throws an error naming that call when a needed module is missing. A recipe of primitives drops from 141,241 to 73,581 bytes gzip in `scene:agent-scene` (every lazy chunk still totals 145,049). `npm run size` now bundles with code splitting and reports the initial load, plus a `+lazy` row for the total. `npm test` preloads every module. The archive no longer carries 29 docs that the site serves (physics, lighting, terrain and the other addon docs); agent docs stay+ - Add joint limits, node constraints and drivers to motion (issue #58). Two-bone IK takes `limits` per joint and axis in degrees and `bendToward`; `scene_inspect` `motionIk` now carries `limited` (each pose a limit changed) and `warnings` (`hyperextended`, and `wrong-bend` for a knee or elbow bent the unnatural way, which a wrong pole used to hide behind `reached`). `motion.constraints` adds `copy_location`, `copy_rotation`, `copy_scale`, `limit_location`, `limit_rotation`, `track_to`, `child_of` (with an inverse and an influence blend) and `floor`, and `motion.drivers` sets a node component or material property from a bounded JSON expression over time and other nodes (never run as code). The order is tracks, IK, drivers, constraints, with no state between samples, so a seek equals playing. `copy_rotation`, `limit_rotation`, `track_to`, `child_of` and `floor` match Blender 5.2 world matrices in 8 recorded cases (`tests/fixtures/blender/constraints.json`). Joint limits are also in `67js/addons/skeleton-pose`. The `leg-plant` task joins the agent modeling suite. The expression language is a new small one in `src/motion-expression.js`: the field expressions of the mesh addon were not on `main` yet+- Reach Rapier from recipes: a `physics` section (and a `set-physics` operation) puts rigid bodies on node IDs with box, ball, capsule, convex or trimesh colliders fitted to their geometry, joints, gravity and a duration of up to 10 seconds. It is recorded once at 60 steps a second and seeks like `motion`, so a seek back repeats the same frame (checked as identical PNG bytes on SwiftShader through MCP). `scene_inspect` reports per-body final positions and resting times, contacts and sensor enter and leave events. Rapier stays an optional peer, served to the preview from `node_modules` like Manifold, so the MCP server bundle does not grow.- Add trimesh and convex-hull colliders (bounded triangle and point counts, checked before allocation), sensors with enter and leave events, and 16-bit collision groups and masks to `addons/physics`, plus `contactEvents`, `restFrames`, `bakePhysicsScene`, `loadPhysicsEngine` and `setPhysicsModuleLoader`. A collider with `sensor` now bakes instead of throwing.- Add the `tray-drop` task to the agent modeling suite: balls dropped into a pocket cut in an authored mesh, graded on a fixed trimesh body, balls resting on the pocket floor and a repeatable seek. Raise four size budgets for the physics code (`entry:physics` to 17,900, `entry:agent-scene` to 121,100, `entry:agent-asset` to 128,500, `scene:agent-scene` to 147,300 bytes gzip).diff --cc bench/size/baseline.jsonindex f74c23e2,c91043ef..42d25d0d--- a/bench/size/baseline.json+++ b/bench/size/baseline.json@@@ -14,9 -14,9 +14,9 @@@},"bundles": {"entry:core": {- "min": 269229,- "gzip": 76877,- "brotli": 65768- "min": 268850,- "gzip": 76800,- "brotli": 65580++ "min": 274298,++ "gzip": 77983,++ "brotli": 66640},"entry:model": {"min": 40093,@@@ -39,9 -39,9 +39,9 @@@"brotli": 7331},"entry:gltf": {- "min": 151944,- "gzip": 46674,- "brotli": 39985- "min": 151968,- "gzip": 46679,- "brotli": 40033++ "min": 152103,++ "gzip": 46693,++ "brotli": 39999},"entry:modeling": {"min": 15517,@@@ -129,14 -129,24 +129,24 @@@"brotli": 762},"entry:agent-scene": {- "min": 399226,- "gzip": 126629,- "brotli": 107783- "min": 187390,- "gzip": 56350,- "brotli": 48715++ "min": 189885,++ "gzip": 57054,++ "brotli": 49352+ },+ "entry:agent-scene+lazy": {- "min": 471518,- "gzip": 154805,- "brotli": 135128++ "min": 474013,++ "gzip": 155506,++ "brotli": 135715},"entry:agent-asset": {- "min": 425353,- "gzip": 133851,- "brotli": 113836- "min": 267125,- "gzip": 81113,- "brotli": 70586++ "min": 269646,++ "gzip": 81874,++ "brotli": 71244+ },+ "entry:agent-asset+lazy": {- "min": 498593,- "gzip": 162013,- "brotli": 141041++ "min": 501114,++ "gzip": 162775,++ "brotli": 141809},"entry:hdri": {"min": 8075,@@@ -144,19 -154,24 +154,24 @@@"brotli": 2801},"scene:lit-box": {-- "min": 200858,-- "gzip": 54820,-- "brotli": 46863++ "min": 206306,++ "gzip": 56013,++ "brotli": 47870},"scene:gltf-box": {- "min": 258708,- "gzip": 73797,- "min": 258732,- "gzip": 73801,-- "brotli": 63362++ "min": 264183,++ "gzip": 74981,++ "brotli": 64331},"scene:agent-scene": {- "min": 498424,- "gzip": 152612,- "brotli": 129631- "min": 285615,- "gzip": 82185,- "brotli": 70742++ "min": 293270,++ "gzip": 84072,++ "brotli": 72169+ },+ "scene:agent-scene+lazy": {- "min": 569743,- "gzip": 180642,- "brotli": 157169++ "min": 577398,++ "gzip": 182530,++ "brotli": 158566}}}diff --cc bench/size/budgets.jsonindex 3df7a9d1,4b6bde65..2217fefa--- a/bench/size/budgets.json+++ b/bench/size/budgets.json@@@ -5,7 -5,7 +5,7 @@@"agent-scene": 100000},"budgets": {- "entry:core": 78500,- "entry:core": 78400,++ "entry:core": 79600,"entry:model": 14400,"entry:nature": 13200,"entry:motion": 13600,@@@ -28,11 -28,14 +28,14 @@@"entry:grass": 4400,"entry:clutter": 5300,"entry:texture-set": 900,- "entry:agent-scene": 129200,- "entry:agent-asset": 136600,- "entry:agent-scene": 57500,- "entry:agent-scene+lazy": 158000,- "entry:agent-asset": 82800,- "entry:agent-asset+lazy": 165300,++ "entry:agent-scene": 58200,++ "entry:agent-scene+lazy": 158700,++ "entry:agent-asset": 83600,++ "entry:agent-asset+lazy": 166100,"entry:hdri": 3300,- "scene:lit-box": 56100,- "scene:lit-box": 56000,-- "scene:gltf-box": 75300,- "scene:agent-scene": 155700- "scene:agent-scene": 83900,- "scene:agent-scene+lazy": 184300++ "scene:lit-box": 57200,++ "scene:gltf-box": 76500,++ "scene:agent-scene": 85800,++ "scene:agent-scene+lazy": 186200}- }+ }diff --cc bench/tool-cost-budgets.jsonindex 00000000,4436e9d9..10e76192mode 000000,100644..100644--- a/bench/tool-cost-budgets.json+++ b/bench/tool-cost-budgets.json@@@ -1,0 -1,21 +1,21 @@@+ {+ "measured": "2026-10-31, from scripts/measure-tool-cost.mjs; was 373388 schema bytes before shared IDs and recipe references",- "schemaTotalBytes": 178800,++ "schemaTotalBytes": 181400,+ "schemaToolBytes": {- "scene_create": 58600,- "scene_apply": 43500,++ "scene_create": 59400,++ "scene_apply": 44300,+ "mesh_edit": 34000,- "scene_create_asset": 18000,++ "scene_create_asset": 18300,+ "mesh_stack": 11500+ },+ "schemaOtherToolBytes": 4000,+ "resultBytes": {- "scene_capabilities": 3000,++ "scene_capabilities": 3010,+ "scene_create": 1400,+ "scene_inspect": 1400,+ "mesh_inspect": 1200,+ "mesh_edit": 2400,+ "scene_apply": 200,+ "scene_export_recipe": 2800+ }+ }diff --cc src/agent-asset.jsindex 9a962f3e,fa838f22..fb59ae89--- a/src/agent-asset.js+++ b/src/agent-asset.js@@@ -1,6 -1,6 +1,7 @@@import { mat4, mat3 } from 'gl-matrix';import { createAgentScene } from './agent-scene.js';+import { environmentPatchWithSetup } from './agent-look.js';+ import { assetDiagnostics } from './agent-diagnostics.js';import { fitPerspectiveDistance } from './camera.js';import { validateSkinGeometry, validateSkinPose } from './skinning.js';import { validateMorphGeometry, validateMorphTransform } from './morph.js';diff --cc src/agent-scene.jsindex eb469183,f0e1274f..8c5507a1--- a/src/agent-scene.js+++ b/src/agent-scene.js@@@ -1,23 -1,20 +1,20 @@@import { mat4, mat3 } from 'gl-matrix';import { box, cylinder, sphere, torus } from './geometry.js';- import { lathe, extrude, tube } from './modeling.js';import { createSceneGraph } from './scene-graph.js';import { applyProjection } from './camera.js';-import { normalizeAgentLook } from './agent-look.js';+import { normalizeAgentLook, environmentPatchWithSetup } from './agent-look.js';- import { normalizeAgentMotion, compileAgentMotion } from './agent-motion.js';- import { normalizeAgentPhysics, bakeAgentPhysics } from './agent-physics.js';- import { meshReport, generateMeshGeometry, MESH_SMOOTH_ANGLE_DEFAULT } from './agent-mesh.js';- import { normalizeMeshEdits, normalizeMeshStack } from './mesh-recipe.js';- import { expandInstancedNodes } from './mesh-instances.js';- import { AGENT_SCENE_CAPABILITIES, AGENT_SCENE_DEFAULT_LIMITS, MAX_MATERIAL_PATTERNS, RECIPE_ALPHA_MODES } from './agent-scene-capabilities.js';+ import { agentSceneModule } from './agent-scene-modules.js';+ import { MESH_SMOOTH_ANGLE_DEFAULT, AGENT_SCENE_CAPABILITIES, AGENT_SCENE_DEFAULT_LIMITS, MAX_MATERIAL_PATTERNS, RECIPE_ALPHA_MODES } from './agent-scene-capabilities.js';import { MATERIAL_LAYER_FIELDS, validateMaterialLayers } from './material-layers.js';- import { materialPatternTextures, normalizeMaterialPattern } from './material-patterns.js';+ import { recipeDiagnostics } from './agent-diagnostics.js';+ import { armatureNodes, bakeRigExport, bindSkins, createSkinStages, normalizeArmatures, publishSkinPoses, restWorldMatrix, rigReport, stageSkinPoses, transformedBounds, withoutArmatureNodes } from './rig.js';export { AGENT_SCENE_CAPABILITIES, AGENT_SCENE_DEFAULT_LIMITS };export { normalizeAgentLook, resolveAgentShadow } from './agent-look.js';- export { needsMeshBooleans } from './agent-mesh.js';- export { needsPhysics, loadPhysicsEngine, physicsEngineLoaded } from './agent-physics.js';- export { setPhysicsModuleLoader } from './physics.js';- export { loadMeshBooleans, meshBooleansLoaded, setManifoldModuleLoader } from './mesh-boolean.js';+ export {+ needsMeshBooleans, loadMeshBooleans, meshBooleansLoaded, setManifoldModuleLoader,+ needsPhysics, loadPhysicsEngine, physicsEngineLoaded, setPhysicsModuleLoader,+ loadAgentSceneModules, loadAllAgentSceneModules, allAgentSceneModulesLoaded, agentSceneModulesNeeded,+ } from './agent-scene-modules.js';const DEFAULT_LIMITS = AGENT_SCENE_DEFAULT_LIMITS;const MAX_MESH_REPORTS = 16;diff --cc tests/agent-preview.test.jsindex 47ca8b14,42ab04ec..dfc1bab7--- a/tests/agent-preview.test.js+++ b/tests/agent-preview.test.js@@@ -3,9 -3,9 +3,9 @@@ import assert from 'node:assert/strict'import { readFile } from 'node:fs/promises';import { Script, createContext } from 'node:vm';import { mat4 } from 'gl-matrix';- import { createAgentScene, needsMeshBooleans, loadMeshBooleans, meshBooleansLoaded, needsPhysics, loadPhysicsEngine, physicsEngineLoaded } from '../src/agent-scene.js';+ import { createAgentScene, needsMeshBooleans, loadMeshBooleans, meshBooleansLoaded, needsPhysics, loadPhysicsEngine, physicsEngineLoaded, loadAllAgentSceneModules, allAgentSceneModulesLoaded } from '../src/agent-scene.js';import { applyProjection } from '../src/camera.js';-import { resolveAgentShadow } from '../src/agent-look.js';+import { resolveAgentShadow, agentEnvironmentMap } from '../src/agent-look.js';import { createAgentAssetScene } from '../src/agent-asset.js';import { parseGltf } from '../src/gltf.js';import { skinnedStripAsset } from './fixtures/skinned-strip.js';