3D engine that AI agents drive
Merge origin/main into feat/particles
Nguyen Xuan An74ca67b05571
diff --cc CHANGELOG.mdindex d5ec3165,ac2d3096..14501d95--- a/CHANGELOG.md+++ b/CHANGELOG.md@@@ -2,7 -2,8 +2,9 @@@## Unreleased+- Add recipe particles and hair (part of issue #63, no new tool). A `particles` section holds seeded emitter systems (point, box, sphere or disc; gravity, drag, wind, a landing floor, size over life) and hair systems that grow strands from a surface node, loaded on demand with the new `particles` module (`src/particles.js`, `src/hair.js`). Each particle is a closed-form function of its seed, index and time, so a seek never replays: the same time gives the same particles, a seek backwards repeats them and a seeded system gives identical bytes (`npm run smoke:agent-particles` checks PNG bytes through the MCP preview, including a second session). Counts are bounded to 4 systems, 2000 particles each, 4000 in total and 100000 drawn vertices. New `set-particles` operation and a `particles` report in `scene_inspect`; the schema takes one record reference, so the tool schemas stay within the total budget and the fields live in `scene_capabilities`. Cloth and particles now share validation helpers (`src/agent-recipe-checks.js`). Left out: particle collisions and bounce, rotation, colour and alpha, hair collision and combing. The smoke, fire and liquid decision (no browser solver, with measured grid costs) is in `docs/physics.md`.+ - Add `67js/addons/light-bake` (part of issue #55; recipes, MCP, GLB export and reflection probes are not done). `bakeLightmaps(parts, lights, options)` traces cosine-weighted paths on the CPU against a triangle BVH and returns new parts that carry the light that bounces between surfaces (point lights and the sun, shadowed) as a lightmap texture drawn through the emissive map, with lightmap UVs from the `uv_unwrap` unwrap. The renderer and its shaders are untouched, so a scene without a bake renders as before and core size is unchanged. Bounded and deterministic: `samples`, `bounces`, `texelsPerUnit`, `maxResolution` and `maxSeconds` are range checked, texel, triangle, light and ray-work totals are checked before any ray is traced, and the same input gives the same bytes. Parts with their own maps, pattern, emission, skin or transparency are skipped and listed in the report. `npm run reference -- --bake lit-interior` takes `lit-interior` from 0.5055 to 0.1444 against Cycles (6 s bake); no other scene moved by more than 0.002. The package archive grows by 3,525 bytes (717,978 to 721,503). See `docs/light-bake.md`.+- Add recipe cloth (part of issue #62, no new tool). A `cloth` section records sheets with a position-based solver (`src/cloth.js`, loaded on demand with the new `cloth` module, no peer dependency): pinned edges, gravity, steady wind, ball and box colliders, thickness and friction, 60 frames a second, bounded by particle, frame and solver-operation limits. A seek blends two recorded frames, so the same time gives the same positions and a seek backwards repeats them (`npm run smoke:agent-cloth` repeats the PNG bytes through the MCP preview). New `grid` geometry (a flat sheet, 1 to 64 segments), `set-cloth` operation and a `cloth` report in `scene_inspect`. The tool schemas stay within the total budget: `set-physics` now takes the same record reference as `set-motion`, and the physics descriptions are shorter. A hanging sheet matches a Blender cloth bake (`scripts/blender-cloth.py`) to 0.08 percent of its height; a sheet dropped on a ball does not match, and no parity is claimed. No self collision and no soft bodies yet.- Edit the topology of imported GLB parts (part of issue #48, no new tool). `mesh_edit` and `mesh_inspect` take a part ID such as `mesh-10` of an imported asset: the part becomes a welded polygon mesh with axis tags and a `material-<n>` tag, the edit log is kept in the asset recipe as `meshEdits` (checkpoints, export and replay work), and every result passes `validateTopology`. New vertices take skin weights, morph deltas and vertex colours from the nearest original surface point and faces left without UVs read the nearest UV island; on a skinned or morphed part an edit more than a quarter of the part's bounds diagonal from the original surface is rejected with the next action. `mesh_inspect` reports `localToWorldScale` and `localAxesInWorld`. Measured on RobotExpressive through MCP (`npm run smoke:imported-topology`): a through hole in the closed left leg (98 to 210 triangles, still one closed part), a cut in the skinned right hand (124 to 109) and the morphed head (154 to 134), a 3 m extrude on the head rejected, GLB export and checkpoint restore intact. Suite task `robot-leg-hole`.- Make room in the npm package (part of issue #36). The MCP preview page, `build/mcp/preview.js`, is now an entry of the library build and imports the engine chunks that the package entries already ship, instead of bundling its own copy of the engine. The archive drops from 904,975 to 666,214 bytes. `build/mcp/manifest.json` lists those chunks under `previewChunks` with their hashes, the MCP runtime serves them next to `preview.js`, and `bench/size/budgets.json` gains `archiveBytes`, which `npm run smoke:package` checks.diff --cc bench/size/baseline.jsonindex c75cb1a3,3878af7f..0d9b4a2b--- a/bench/size/baseline.json+++ b/bench/size/baseline.json@@@ -14,9 -14,9 +14,9 @@@},"bundles": {"entry:core": {- "min": 283289,- "min": 283270,-- "gzip": 80938,- "brotli": 69005- "brotli": 69059++ "min": 283335,++ "gzip": 80949,++ "brotli": 69068},"entry:model": {"min": 40093,@@@ -129,24 -134,24 +134,24 @@@"brotli": 762},"entry:agent-scene": {- "min": 29330,- "gzip": 12010,- "brotli": 10697- "min": 40965,- "gzip": 12546,- "brotli": 10153++ "min": 203396,++ "gzip": 62055,++ "brotli": 53770},"entry:agent-scene+lazy": {- "min": 577871,- "gzip": 194206,- "brotli": 169059+ "min": 593202,+ "gzip": 200387,+ "brotli": 174845},"entry:agent-asset": {- "min": 29330,- "gzip": 12010,- "brotli": 10697- "min": 40965,- "gzip": 12546,- "brotli": 10153++ "min": 297332,++ "gzip": 92384,++ "brotli": 80417},"entry:agent-asset+lazy": {- "min": 613648,- "gzip": 204603,- "brotli": 177784+ "min": 628877,+ "gzip": 210742,+ "brotli": 183453},"entry:hdri": {"min": 8075,@@@ -174,14 -179,14 +179,14 @@@"brotli": 67034},"scene:agent-scene": {- "min": 29330,- "gzip": 12010,- "brotli": 10697- "min": 40965,- "gzip": 12546,- "brotli": 10153++ "min": 312794,++ "gzip": 91262,++ "brotli": 78388},"scene:agent-scene+lazy": {- "min": 687346,- "gzip": 223454,- "brotli": 193846+ "min": 702600,+ "gzip": 229597,+ "brotli": 199427}}}diff --cc bench/size/budgets.jsonindex 742bcb96,7ec05ccb..9800dda8--- a/bench/size/budgets.json+++ b/bench/size/budgets.json@@@ -1,5 -1,5 +1,5 @@@{- "archiveBytes": 732000,- "archiveBytes": 741100,++ "archiveBytes": 751100,"goals": {"lit-box": 50000,"gltf-box": 80000,@@@ -28,17 -28,18 +28,18 @@@"entry:terrain": 3100,"entry:grass": 4400,"entry:clutter": 5300,+ "entry:light-bake": 25400,"entry:texture-set": 900,- "entry:agent-scene": 12300,- "entry:agent-scene": 12800,- "entry:agent-scene+lazy": 198100,- "entry:agent-asset": 12800,- "entry:agent-asset+lazy": 208700,++ "entry:agent-scene": 63300,+ "entry:agent-scene+lazy": 204400,- "entry:agent-asset": 12300,++ "entry:agent-asset": 94300,+ "entry:agent-asset+lazy": 215000,"entry:hdri": 3300,"entry:camera-effects": 8600,"entry:render-passes": 1700,"scene:lit-box": 60200,"scene:gltf-box": 79700,- "scene:agent-scene": 12300,- "scene:agent-scene": 12800,- "scene:agent-scene+lazy": 228000++ "scene:agent-scene": 93100,+ "scene:agent-scene+lazy": 234200}}diff --cc scripts/measure-size.mjsindex 5ce6f7b4,5ce6f7b4..d72dcf2c--- a/scripts/measure-size.mjs+++ b/scripts/measure-size.mjs@@@ -5,7 -5,7 +5,7 @@@ import { readFile, writeFile } from 'noimport { relative, resolve } from 'node:path';import { brotliCompressSync, gzipSync } from 'node:zlib';import { build } from 'esbuild';--import { budgetsFrom, compareToBudgets, formatRows } from './size-budget.mjs';++import { budgetsFrom, compareToBudgets, formatRows, initialOutputPaths } from './size-budget.mjs';const BUDGETS_PATH = 'bench/size/budgets.json';const BASELINE_PATH = 'bench/size/baseline.json';@@@ -59,17 -59,17 +59,7 @@@ function gzipSize(contents)// statically; the rest loads when a feature asks for it. Both are reported so lazy code never hides.function chunkSizes(result) {const outputsByPath = new Map(result.outputFiles.map(file => [relative(process.cwd(), file.path).replaceAll('\\', '/'), file]));-- const entryPath = Object.keys(result.metafile.outputs).find(path => result.metafile.outputs[path].entryPoint);-- const initialPaths = new Set();-- const pending = [entryPath];-- while (pending.length) {-- const path = pending.pop();-- if (initialPaths.has(path)) continue;-- initialPaths.add(path);-- for (const imported of result.metafile.outputs[path].imports) {-- if (imported.kind === 'import-statement') pending.push(imported.path);-- }-- }++ const initialPaths = initialOutputPaths(result.metafile);const sizeOf = paths => {const parts = [...paths].map(path => outputsByPath.get(path).contents);const bytes = Buffer.concat(parts);diff --cc scripts/size-budget.mjsindex b2e14f77,b2e14f77..d350d2ad--- a/scripts/size-budget.mjs+++ b/scripts/size-budget.mjs@@@ -44,3 -44,3 +44,24 @@@ export function formatRows(rows)].join(''));return [header, ...lines].join('\n');}++++// esbuild marks every dynamically imported module as an entry point too, so the first output with an entryPoint can be++// a lazy chunk. The bundle a page starts with is the one built from the stdin entry.++const STDIN_ENTRY = '<stdin>';++++// Returns the output paths a page loads at start: the entry chunk and every chunk it reaches by static import.++export function initialOutputPaths(metafile) {++ const entryPath = Object.keys(metafile.outputs).find(path => metafile.outputs[path].entryPoint === STDIN_ENTRY);++ if (!entryPath) throw new Error('The size measure found no stdin entry in the esbuild metafile.');++ const reached = new Set();++ const pending = [entryPath];++ while (pending.length) {++ const path = pending.pop();++ if (reached.has(path)) continue;++ reached.add(path);++ for (const imported of metafile.outputs[path].imports) {++ if (imported.kind === 'import-statement') pending.push(imported.path);++ }++ }++ return reached;++}diff --cc tests/size-budget.test.jsindex fb43fea2,fb43fea2..606de253--- a/tests/size-budget.test.js+++ b/tests/size-budget.test.js@@@ -1,7 -1,7 +1,10 @@@import assert from 'node:assert/strict';--import { existsSync, readFileSync } from 'node:fs';++import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import test from 'node:test';--import { budgetFor, compareToBudgets, formatRows } from '../scripts/size-budget.mjs';++import { tmpdir } from 'node:os';++import { join } from 'node:path';++import { build } from 'esbuild';++import { budgetFor, compareToBudgets, formatRows, initialOutputPaths } from '../scripts/size-budget.mjs';const readJson = path => JSON.parse(readFileSync(new URL(path, import.meta.url), 'utf8'));const budgetFile = readJson('../bench/size/budgets.json');@@@ -60,3 -60,3 +63,26 @@@ test('the baseline records the three.jassert.ok(baseline.three['lit-box'].gzip > 0);assert.ok(baseline.three['gltf-box'].gzip > 0);});++++test('the initial total follows static imports into shared chunks and skips lazy modules', async () => {++ const directory = mkdtempSync(join(tmpdir(), 'size-initial-'));++ try {++ writeFileSync(join(directory, 'a-lazy.js'), "import { shared } from './shared.js';\nexport const lazyA = () => shared();\n");++ writeFileSync(join(directory, 'b-lazy.js'), "import { shared } from './shared.js';\nexport const lazyB = () => shared();\n");++ writeFileSync(join(directory, 'shared.js'), 'export const shared = () => [1, 2, 3].map(value => value * 2);\n');++ writeFileSync(join(directory, 'main.js'), "import { shared } from './shared.js';\nexport const load = () => import('./a-lazy.js').then(() => import('./b-lazy.js'));\nexport const run = () => shared();\n");++ const result = await build({++ stdin: { contents: "export * from './main.js';", resolveDir: directory, loader: 'js' },++ bundle: true, minify: true, format: 'esm', write: false, metafile: true, splitting: true, outdir: join(directory, 'out'), logLevel: 'silent',++ });++ const outputs = result.metafile.outputs;++ const lazyEntries = Object.keys(outputs).filter(path => outputs[path].entryPoint && outputs[path].entryPoint !== '<stdin>');++ assert.equal(lazyEntries.length, 2, 'both lazy modules are entry points in the metafile');++ const initial = [...initialOutputPaths(result.metafile)];++ assert.ok(initial.some(path => outputs[path].entryPoint === '<stdin>'), 'the stdin entry is counted');++ assert.ok(initial.some(path => !outputs[path].entryPoint), 'the shared chunk it imports statically is counted');++ for (const lazy of lazyEntries) assert.ok(!initial.includes(lazy), 'a lazy module is not in the initial total');++ } finally {++ rmSync(directory, { recursive: true, force: true });++ }++});