3D engine that AI agents drive
Seek cloth in place in the preview instead of replacing the part (part of #430)
Nguyen Xuan An7d445e2731e7
diff --git a/changes/430.md b/changes/430.mdindex 2425d34f..409134c4 100644--- a/changes/430.md+++ b/changes/430.md@@ -1,2 +1,3 @@- Cloth steps live: `createCloth(positions, triangles, options)` returns a stepper with `step(dt)`, runtime `pin`/`unpin` with moving targets, `pinForce`, per-particle extra mass and tearing (`tearStretch`, `tearForce`, a readable `broken` list). `bakeCloth` is now a loop over it and records the same frames as before (closes #430).- New addon `@codepawlhq/67js/addons/update-part-vertices` exports `updatePartVertices(renderer, part, positions, normals)`, which rewrites a rigid part's vertex buffers with `bufferSubData` instead of `replaceParts`. The core bundle does not change.+- A cloth seek in the MCP preview rewrites one geometry per sheet in place and uploads its positions and normals with `updatePartVertices`, instead of replacing the part (part of #430). One 33 x 33 seek and draw measured 1.53 ms with `replaceParts` and 1.07 ms in place.diff --git a/changes/430.size.md b/changes/430.size.mdindex e4107799..4178c444 100644--- a/changes/430.size.md+++ b/changes/430.size.md@@ -1 +1,2 @@-- scene:agent-scene+lazy: the live cloth stepper (pins, pin forces, extra mass, tearing) lives in the lazy cloth code that the baked cloth already loads, about 930 bytes gzip. No core entry or goal scene grew; the update-part-vertices addon is separate.+- scene:agent-scene+lazy: the live cloth stepper (pins, pin forces, extra mass, tearing) lives in the lazy cloth code that the baked cloth already loads, about 930 bytes gzip. No core entry grew; the update-part-vertices addon is separate.+- scene:agent-scene: a cloth seek reuses the previous parts when their geometry is unchanged, so the renderer sees no topology change; the rest of the logic lives in the lazy cloth module.diff --git a/scripts/agent-preview-browser.js b/scripts/agent-preview-browser.jsindex 423a36ff..37fdfa09 100644--- a/scripts/agent-preview-browser.js+++ b/scripts/agent-preview-browser.js@@ -17,6 +17,7 @@ import { parseGltf } from '../src/gltf.js';import { assetCopyKey, recipeAssetNeeds } from '../src/agent-scene-assets.js';import { exportGlb } from '../src/export.js';import '../src/texture-update.js';+import { updatePartVertices } from '../src/update-part-vertices.js';import { hasPatternTextures } from './agent-preview-textures.js';import { TEXTURE_SEQUENCE_LIMITS } from '../src/texture-sequence-limits.js';import { weightTracksFromGltfAnimation } from '../src/export-skeleton.js';@@ -220,6 +221,13 @@ function draw(record, time = 0, camera = record.scene.camera, frameOptions = {})} else {record.renderer.updateParts(record.scene.parts);}+ // Cloth rewrites its geometry in place, so a seek only uploads the new positions and normals.+ if (agentSceneModuleLoaded('cloth')) {+ const { isLiveClothGeometry } = agentSceneModule('cloth');+ for (const part of record.scene.parts) {+ if (isLiveClothGeometry(part.geometry)) updatePartVertices(record.renderer, part, part.geometry.positions, part.geometry.normals);+ }+ }syncAutomaticGround(record);syncShadowCascades(record);syncSequenceTextures(record);diff --git a/src/agent-cloth.js b/src/agent-cloth.jsindex c8210d51..512750e3 100644--- a/src/agent-cloth.js+++ b/src/agent-cloth.js@@ -357,21 +357,35 @@ function bakeSheet(sheet, cloth, node, geometry, colliders, frameCount, movingSedensity: sheet.density, damping: sheet.damping, friction: sheet.friction, thickness: sheet.thickness,colliders, frameCount, framesPerSecond: FRAMES_PER_SECOND,}], bakeCloth) });- return { recording, welded, inverse: mat4.invert(mat4.create(), matrix), geometry, particleScratch: new Float32Array(recording.particleCount * 3) };+ return { recording, welded, inverse: mat4.invert(mat4.create(), matrix), geometry, particleScratch: new Float32Array(recording.particleCount * 3), live: liveGeometry(geometry) };}-function frozenGeometry(baked, time) {- const { recording, welded, inverse, geometry, particleScratch } = baked;+// One geometry per sheet whose arrays every seek rewrites, so a renderer keeps the same part and only uploads new values.+const liveGeometries = new WeakSet();++function liveGeometry(geometry) {+ const live = Object.freeze({ ...geometry, positions: Float32Array.from(geometry.positions), normals: Float32Array.from(geometry.normals) });+ liveGeometries.add(live);+ return live;+}++/** True for a sheet geometry that every cloth sample rewrites in place; a renderer uploads its positions and normals after a seek. */+export function isLiveClothGeometry(geometry) {+ return liveGeometries.has(geometry);+}++function sampleLiveGeometry(baked, time) {+ const { recording, welded, inverse, geometry, particleScratch, live } = baked;sampleClothFrames(recording, time, particleScratch);- const positions = new Float32Array(geometry.positions.length);+ const positions = live.positions;for (let vertex = 0; vertex < welded.particleOfVertex.length; vertex++) {const source = welded.particleOfVertex[vertex] * 3;for (let axis = 0; axis < 3; axis++) {positions[vertex * 3 + axis] = inverse[axis] * particleScratch[source] + inverse[4 + axis] * particleScratch[source + 1] + inverse[8 + axis] * particleScratch[source + 2] + inverse[12 + axis];}}- const normals = recomputeNormals(geometry, positions);- return Object.freeze({ ...geometry, positions, normals });+ live.normals.set(recomputeNormals(geometry, positions));+ return live;}function sheetReport(baked, node, sheet) {@@ -396,7 +410,8 @@ function sheetReport(baked, node, sheet) {/*** Bake the cloth section. The result is a pure function of time: `sample(t)` returns the deformed geometry of each- * sheet node (null at time zero, where the sheet is at rest), and a seek never runs the solver again.+ * sheet node (null at time zero, where the sheet is at rest), and a seek never runs the solver again. The geometry of a+ * sheet is one object whose positions and normals are rewritten by every sample.*/export function bakeAgentCloth(cloth, recipe, geometries, motionSampler = null) {const nodes = new Map(recipe.nodes.map(node => [node.id, node]));@@ -415,7 +430,7 @@ export function bakeAgentCloth(cloth, recipe, geometries, motionSampler = null)sample(time) {if (time <= 0) return null;const clamped = Math.min(time, cloth.duration);- return new Map([...baked].map(([nodeId, sheetBake]) => [nodeId, frozenGeometry(sheetBake, clamped)]));+ return new Map([...baked].map(([nodeId, sheetBake]) => [nodeId, sampleLiveGeometry(sheetBake, clamped)]));},/** Particle counts and where each sheet ends, as numbers an agent can read without a picture. */report() {diff --git a/src/agent-scene.js b/src/agent-scene.jsindex 3da4a1cc..100ef793 100644--- a/src/agent-scene.js+++ b/src/agent-scene.js@@ -894,15 +894,17 @@ function sampledGeometries(stagedState, time) {return geometries.size ? geometries : null;}-// Deformed cloth is a new geometry per sampled time, so those parts are replaced and the renderer sees a topology change.-function partsWithCloth(restParts, clothGeometries) {+// Cloth keeps one geometry per sheet that every sample rewrites, so the parts of a later seek are the previous ones and the+// renderer sees no topology change. Particles still bring a new geometry per time, so those parts are replaced.+function partsWithCloth(restParts, clothGeometries, previousParts) {if (!clothGeometries) {return restParts;}- return Object.freeze(restParts.map(part => {+ const next = restParts.map((part, index) => {const geometry = clothGeometries.get(part.name);- return geometry ? Object.freeze({ ...part, geometry }) : part;- }));+ return !geometry ? part : previousParts[index]?.geometry === geometry ? previousParts[index] : Object.freeze({ ...part, geometry });+ });+ return next.every((part, index) => part === previousParts[index]) ? previousParts : Object.freeze(next);}/** Returns true when the parts handed to a renderer changed, which the caller counts as a topology change. */@@ -924,7 +926,7 @@ function publishPose(stagedState, pose) {for (const sequence of stagedState.sequences) sequence.seek(pose.time);stagedState.renderCamera = pose.camera;stagedState.motionTime = pose.time;- const nextParts = partsWithCloth(stagedState.restParts, pose.clothGeometries);+ const nextParts = partsWithCloth(stagedState.restParts, pose.clothGeometries, stagedState.parts);const partsChanged = nextParts !== stagedState.parts;stagedState.parts = nextParts;return partsChanged;diff --git a/tests/agent-cloth-in-place.test.js b/tests/agent-cloth-in-place.test.jsnew file mode 100644index 00000000..55bd77bb--- /dev/null+++ b/tests/agent-cloth-in-place.test.js@@ -0,0 +1,67 @@+import './support/load-agent-scene-modules.mjs';+import test from 'node:test';+import assert from 'node:assert/strict';+import { createAgentScene } from '../src/agent-scene.js';+import { updatePartVertices } from '../src/update-part-vertices.js';+import { isLiveClothGeometry } from '../src/agent-cloth.js';++function flagRecipe() {+ return {+ format: '67js.scene', schemaVersion: 1,+ geometries: [{ id: 'sheet', type: 'grid', segments: 10 }],+ materials: [{ id: 'fabric', color: [0.7, 0.2, 0.2], doubleSided: true }],+ nodes: [{ id: 'flag', geometry: 'sheet', material: 'fabric', translation: [0, 2, 0], scale: [2, 1.5, 1] }],+ cloth: { duration: 2, wind: [0, 0, 6], sheets: [{ node: 'flag', pin: ['top'] }] },+ };+}++/** A WebGL2 stand-in that counts uploads, with the part's geometry already on the card. */+function fakeRenderer(part) {+ const writes = [];+ const renderer = {+ context: {+ ARRAY_BUFFER: 34962,+ bindBuffer: () => {},+ bufferSubData: (target, offset, data) => writes.push(Array.from(data)),+ },+ shadowsDirty: false,+ partLocations: new Map([[part, { batch: { parts: [part], bounds: null }, geometry: part.geometry }]]),+ geometryBuffers: new Map([[part.geometry, new Map([['positions', 'positions'], ['normals', 'normals']])]]),+ };+ return { renderer, writes };+}++test('a cloth seek keeps the part, makes no new batch and draws the same positions as a fresh scene', () => {+ const scene = createAgentScene(flagRecipe());+ scene.sampleMotion(0.5);+ const [part] = scene.parts;+ const topologyAfterFirstSeek = scene.topologyRevision;+ const positionsAtHalf = Array.from(part.geometry.positions);+ const { renderer, writes } = fakeRenderer(part);++ scene.sampleMotion(1.2);+ assert.equal(scene.parts[0], part, 'the renderer keeps the same part');+ assert.equal(scene.parts[0].geometry, part.geometry, 'and the same geometry object');+ assert.equal(scene.topologyRevision, topologyAfterFirstSeek, 'no topology change, so no replaceParts and no new batch');+ assert.equal(isLiveClothGeometry(part.geometry), true);+ assert.notDeepEqual(Array.from(part.geometry.positions), positionsAtHalf, 'the sheet moved');++ updatePartVertices(renderer, part, part.geometry.positions, part.geometry.normals);+ assert.equal(writes.length, 2, 'one positions write and one normals write');+ assert.deepEqual(writes[0], Array.from(part.geometry.positions), 'the uploaded positions are the sampled ones');++ const fresh = createAgentScene(flagRecipe());+ fresh.sampleMotion(1.2);+ assert.deepEqual(Array.from(part.geometry.positions), Array.from(fresh.parts[0].geometry.positions), 'the same values as a scene seeked straight there');+ assert.deepEqual(Array.from(part.geometry.normals), Array.from(fresh.parts[0].geometry.normals));++ scene.sampleMotion(0.5);+ assert.deepEqual(Array.from(part.geometry.positions), positionsAtHalf, 'a seek back reproduces the earlier frame');+});++test('a scene without cloth has no live cloth geometry', () => {+ const recipe = flagRecipe();+ delete recipe.cloth;+ const scene = createAgentScene(recipe);+ assert.equal(scene.parts.some(part => isLiveClothGeometry(part.geometry)), false);+});