3D engine that AI agents drive
Merge remote-tracking branch 'origin/main' into HEAD
Nguyen Xuan An3f55036a74d6
diff --cc CHANGELOG.mdindex 47225bb6,6de0efe4..1802ec53--- a/CHANGELOG.md+++ b/CHANGELOG.md@@@ -2,7 -2,7 +2,8 @@@## Unreleased+- 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.diff --cc bench/size/baseline.jsonindex 27980fbf,7a376cc4..d5b95cd4--- a/bench/size/baseline.json+++ b/bench/size/baseline.json@@@ -129,14 -129,24 +129,24 @@@"brotli": 762},"entry:agent-scene": {- "min": 436033,- "gzip": 138526,- "brotli": 117531- "min": 162579,- "gzip": 48203,- "brotli": 41448++ "min": 183987,++ "gzip": 55346,++ "brotli": 47801+ },+ "entry:agent-scene+lazy": {- "min": 422954,- "gzip": 137805,- "brotli": 120313++ "min": 439633,++ "gzip": 143601,++ "brotli": 125617},"entry:agent-asset": {- "min": 462140,- "gzip": 145796,- "brotli": 123807- "min": 245362,- "gzip": 74061,- "brotli": 64428++ "min": 262033,++ "gzip": 79545,++ "brotli": 69297+ },+ "entry:agent-asset+lazy": {- "min": 448381,- "gzip": 144778,- "brotli": 126295++ "min": 465052,++ "gzip": 150260,++ "brotli": 131233},"entry:hdri": {"min": 8075,@@@ -154,9 -164,14 +164,14 @@@"brotli": 63362},"scene:agent-scene": {- "min": 535231,- "gzip": 164487,- "brotli": 139515- "min": 260808,- "gzip": 74153,- "brotli": 63460++ "min": 282212,++ "gzip": 81177,++ "brotli": 69763+ },+ "scene:agent-scene+lazy": {- "min": 521183,- "gzip": 163754,- "brotli": 142345++ "min": 537858,++ "gzip": 169434,++ "brotli": 147600}}}diff --cc bench/size/budgets.jsonindex ce6b18b2,dde83593..dd3da36c--- a/bench/size/budgets.json+++ b/bench/size/budgets.json@@@ -28,11 -28,14 +28,14 @@@"entry:grass": 4400,"entry:clutter": 5300,"entry:texture-set": 900,- "entry:agent-scene": 141300,- "entry:agent-asset": 148800,- "entry:agent-scene": 49200,- "entry:agent-scene+lazy": 140600,- "entry:agent-asset": 75600,- "entry:agent-asset+lazy": 147700,++ "entry:agent-scene": 56500,++ "entry:agent-scene+lazy": 146500,++ "entry:agent-asset": 81200,++ "entry:agent-asset+lazy": 153300,"entry:hdri": 3300,"scene:lit-box": 56000,"scene:gltf-box": 75300,- "scene:agent-scene": 167800- "scene:agent-scene": 75700,- "scene:agent-scene+lazy": 167100++ "scene:agent-scene": 82900,++ "scene:agent-scene+lazy": 172900}}diff --cc src/agent-scene.jsindex f8554d51,f375a039..f27dade9--- a/src/agent-scene.js+++ b/src/agent-scene.js@@@ -3,22 -3,16 +3,17 @@@ import { box, cylinder, sphere, torusimport { createSceneGraph } from './scene-graph.js';import { applyProjection } from './camera.js';import { normalizeAgentLook } 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 { 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;@@@ -563,7 -508,7 +562,8 @@@ function normalizeRecipe(input, limitsif (distinctPatterns.size > MAX_MATERIAL_PATTERNS) {throw new RangeError(`A scene holds at most ${MAX_MATERIAL_PATTERNS} different material patterns; it has ${distinctPatterns.size}. Reuse a pattern and tint it with color.`);}- recipe.nodes = expandInstancedNodes(withoutArmatureNodes(recipe.nodes), {++ recipe.nodes = withoutArmatureNodes(recipe.nodes);+ if (hasInstancedNodes(recipe.nodes)) recipe.nodes = agentSceneModule('mesh').expandInstancedNodes(recipe.nodes, {maxNodes: limits.maxNodes,resolveSurface: id => {const definition = recipe.geometries.find(geometry => geometry.id === id);@@@ -871,9 -809,7 +871,9 @@@ export function createAgentScene(inputstats: geometryStats(geometryRecords.values()),};stagedState.localBounds = localBounds;+ stagedState.skinBindings = skinBindings;+ stagedState.skinStages = createSkinStages(skinBindings);- stagedState.motionSampler = compileAgentMotion(recipe.motion ?? null, recipe);+ stagedState.motionSampler = recipe.motion ? agentSceneModule('motion').compileAgentMotion(recipe.motion, recipe) : null;stagedState.physicsSampler = stagePhysics(recipe, geometryRecords);const animated = Boolean(stagedState.motionSampler || stagedState.physicsSampler);stagedState.poseGraph = animated ? compile(recipe, geometries, localBounds, limits) : null;@@@ -1158,10 -1073,9 +1158,10 @@@const resident = geometryStats(ownedGeometryRecords());return {revision, topologyRevision,- ...(state.recipe.motion?.ik?.length ? { motionIk: compileAgentMotion(state.recipe.motion, state.recipe).sweepIk() } : {}),+ ...(state.recipe.motion?.ik?.length ? { motionIk: agentSceneModule('motion').compileAgentMotion(state.recipe.motion, state.recipe).sweepIk() } : {}),motion: state.recipe.motion ? { duration: state.recipe.motion.duration, loop: state.recipe.motion.loop, tracks: state.recipe.motion.tracks.length, time: state.motionTime } : null,physics: state.physicsSampler ? state.physicsSampler.report() : null,+ ...(state.recipe.armatures ? { rig: rigReport(state.recipe, state.skinBindings) } : {}),boundsSpace: 'authored-base-world',counts: {nodes: state.recipe.nodes.length, parts: state.parts.length,