3D engine that AI agents drive
Soft body film previews at 960x540 and 60 fps, side-by-side dent still, every number in the JSON with unit and step (part of #696)
Nguyen Xuan An2afeb9bd779a
diff --git a/docs/physics.md b/docs/physics.mdindex f50092fc..e717a500 100644--- a/docs/physics.md+++ b/docs/physics.md@@ -446,7 +446,7 @@ Soft body films (`scripts/soft-body-film.mjs`, `tests/soft-body-film.test.js`):- Flag: a 24 by 24 grid, 1 m tall, hung by its top edge from a pole (`pinTo`) that drops 2.35 m, so the cloth piles on the floor with `selfCollision: true`. The pile is 0.547 m tall. The closest pair of particles not joined by an edge ends 7.9 mm apart; the same film without self collision ends 0.5 mm apart (layers merged).- Both films bake to the same bytes in every frame across two runs; the test compares every frame's vertex buffer.- Frame time, in milliseconds per solver step (the bake is one call, so this is its total over the steps, on a machine shared with other sessions, so read it as a range): jelly 2.5 to 6.8 ms a step over 120 steps, flag 20 to 37 ms a step over 150 steps (5.5 ms a step without self collision, 625 particles). Sampling a recorded frame for playback costs about 1 to 1.5 ms.-- `node scripts/render-soft-body-film.mjs <folder>` (run it through gpu-lock) writes a 480 by 270 preview MP4 of each film, stills at contact and at the deepest dent, and `soft-body-film.json` with the same numbers. It needs `npm run build:lib` first. No default changes.+- `node scripts/render-soft-body-film.mjs <folder>` (run it through gpu-lock) writes a 960 by 540, 60 fps preview MP4 of each film (one video frame per simulation step, so frame N is step N), stills at contact and at the deepest dent, `jelly-cube-compare.png` (both side by side with the measured width and height drawn on it) and `soft-body-film.json` with every number, its unit and its step. It needs `npm run build:lib` first. No default changes.Blender comparison (`scripts/blender-cloth.py`, Blender 5.2.2, default stiffness, mass set from the same kg/m2; `tests/cloth-blender.test.js`):diff --git a/scripts/render-soft-body-film.mjs b/scripts/render-soft-body-film.mjsindex da5d96d7..aa1f9807 100644--- a/scripts/render-soft-body-film.mjs+++ b/scripts/render-soft-body-film.mjs@@ -1,16 +1,19 @@-// Renders a low-resolution preview MP4 of each soft body film, stills at contact and at the deepest dent, and one JSON of-// the measured numbers. Usage: node scripts/render-soft-body-film.mjs <output directory>+// Renders a preview MP4 of each soft body film (960 by 540, 60 fps, one video frame per simulation step), stills at contact+// and at the deepest dent, a side-by-side still of the two with the measured size drawn on it, and one JSON of the numbers. Usage: node scripts/render-soft-body-film.mjs <output directory>// Run it through gpu-lock on a shared machine. It needs `npm run build:lib` first.+import { execFileSync } from 'node:child_process';import { copyFile, mkdir, writeFile } from 'node:fs/promises';-import { existsSync } from 'node:fs';+import { existsSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join, resolve } from 'node:path';import { setTimeout as delay } from 'node:timers/promises';+import { createAgentScene } from '../src/agent-scene.js';import { Client } from '@modelcontextprotocol/client';import { StdioClientTransport } from '@modelcontextprotocol/client/stdio';-import { FILM_FRAMES_PER_SECOND, axisRange, foldingFlagRecipe, jellyCubeRecipe, measureLanding, recordFilm } from './soft-body-film.mjs';+import { FILM_FRAMES_PER_SECOND, axisRange, foldingFlagRecipe, jellyCubeRecipe, measureLanding, recordFilm, unjoinedMinimumDistance } from './soft-body-film.mjs';-const PREVIEW = { width: 480, height: 270, fps: 30 };+const PREVIEW = { width: 960, height: 540, fps: FILM_FRAMES_PER_SECOND };+const FONT_FILE = 'C\\:/Windows/Fonts/arial.ttf';const serverEntry = new URL('../bin/67js-mcp.mjs', import.meta.url).pathname.replace(/^\/(\w:)/, '$1');async function renderJob(call, sessionId, settings) {@@ -59,6 +62,29 @@ function filmNumbers(film, nodeId, frameCount) {};}+const meters = value => Number(value.toFixed(4));+const seconds = step => Number((step / FILM_FRAMES_PER_SECOND).toFixed(4));++/** Size of the cube in one frame: width along x (what the side camera sees) and height. */+function cubeSize(frame) {+ return { widthMeters: meters(axisRange(frame, 0).extent), heightMeters: meters(axisRange(frame, 1).extent) };+}++/** Joins the contact and max dent stills left and right, each captioned with its step, time and measured size. */+function writeComparisonStill(outputDirectory, files, captions) {+ const inputs = [];+ const filters = [];+ Object.keys(captions).forEach((label, index) => {+ inputs.push('-i', join(outputDirectory, files[label]));+ const captionFile = join(tmpdir(), `67js-caption-${process.pid}-${index}.txt`);+ writeFileSync(captionFile, captions[label]);+ const escapedCaption = captionFile.replaceAll('\\', '/').replace(':', '\\:');+ filters.push(`[${index}:v]drawbox=x=0:y=0:w=iw:h=124:[email protected]:t=fill,drawtext=fontfile='${FONT_FILE}':textfile='${escapedCaption}':fontcolor=white:fontsize=28:line_spacing=8:x=20:y=12[caption${index}]`);+ });+ const filterGraph = `${filters.join(';')};[caption0][caption1]hstack=inputs=2`;+ execFileSync('ffmpeg', ['-y', '-loglevel', 'error', ...inputs, '-filter_complex', filterGraph, '-frames:v', '1', join(outputDirectory, 'jelly-cube-compare.png')]);+}+async function main() {if (!existsSync(new URL('../build/mcp/server.mjs', import.meta.url))) throw new Error('Run npm run build:lib first: the MCP server is not built.');const outputDirectory = resolve(process.argv[2] ?? '');@@ -86,10 +112,45 @@ async function main() {name: 'folding-flag', recipe: foldingFlagRecipe(), nodeId: 'flag', frameCount: flagFrames, videoSeconds: flagFrames / FILM_FRAMES_PER_SECOND,stillTimes: { folded: (flagFrames - 1) / FILM_FRAMES_PER_SECOND },});+ const restFrame = jelly.record.frames[0];+ const atContact = cubeSize(jelly.record.frames[landing.contact]);+ const atMaxDent = cubeSize(jelly.record.frames[landing.maxDentFrame]);+ const withoutSelfCollision = recordFilm(foldingFlagRecipe({ selfCollision: false }), 'flag', flagFrames);+ const flagIndices = createAgentScene(foldingFlagRecipe()).parts.find(part => part.name === 'flag').geometry.indices;+ const lastFlagStep = flagFrames - 1;+ writeComparisonStill(outputDirectory, jellyFiles.files, {+ contact: `contact: step ${landing.contact} (${seconds(landing.contact).toFixed(3)} s)+width ${atContact.widthMeters.toFixed(4)} m, height ${atContact.heightMeters.toFixed(4)} m`,+ 'max-dent': `max dent: step ${landing.maxDentFrame} (${seconds(landing.maxDentFrame).toFixed(3)} s)+width ${atMaxDent.widthMeters.toFixed(4)} m, height ${atMaxDent.heightMeters.toFixed(4)} m+dent ${landing.maxDentDepth.toFixed(4)} m`,+ });+ jellyFiles.files.compare = 'jelly-cube-compare.png';const summary = {- preview: PREVIEW,- jellyCube: { steps: jellyFrames, contactFrame: landing.contact, contactSeconds: landing.contact / FILM_FRAMES_PER_SECOND, maxDentFrame: landing.maxDentFrame, maxDentDepthMeters: Number(landing.maxDentDepth.toFixed(4)), restHeightMeters: Number(landing.restHeight.toFixed(4)), frameTimes: jelly.frameTimes, files: jellyFiles.files },- foldingFlag: { steps: flagFrames, finalFrameSha256: flagHash, frameTimes: flag.frameTimes, files: flagFiles.files },+ preview: { ...PREVIEW, videoFrameToStep: 'one video frame is one simulation step: frame N is step N, at N / 60 s' },+ jellyCube: {+ simulationSteps: jellyFrames,+ restSize: { widthMeters: meters(axisRange(restFrame, 0).extent), heightMeters: meters(landing.restHeight) },+ contact: { step: landing.contact, timeSeconds: seconds(landing.contact), ...atContact },+ maxDent: { step: landing.maxDentFrame, timeSeconds: seconds(landing.maxDentFrame), depthMeters: meters(landing.maxDentDepth), ...atMaxDent },+ frameTimes: jelly.frameTimes,+ files: jellyFiles.files,+ },+ foldingFlag: {+ simulationSteps: flagFrames,+ finalStep: lastFlagStep,+ finalTimeSeconds: seconds(lastFlagStep),+ restHeightMeters: meters(axisRange(flag.record.frames[0], 1).extent),+ pileHeightMeters: meters(axisRange(flag.record.frames[lastFlagStep], 1).extent),+ closestUnjoinedGapMeters: {+ atStep: lastFlagStep,+ withSelfCollision: meters(unjoinedMinimumDistance(flag.record.frames[lastFlagStep], flagIndices)),+ withoutSelfCollision: meters(unjoinedMinimumDistance(withoutSelfCollision.frames[lastFlagStep], flagIndices)),+ },+ finalFrameSha256: flagHash,+ frameTimes: flag.frameTimes,+ files: flagFiles.files,+ },};await writeFile(join(outputDirectory, 'soft-body-film.json'), `${JSON.stringify(summary, null, 2)}\n`);console.log(JSON.stringify(summary, null, 2));diff --git a/scripts/soft-body-film.mjs b/scripts/soft-body-film.mjsindex 4e7b0446..2bc00b74 100644--- a/scripts/soft-body-film.mjs+++ b/scripts/soft-body-film.mjs@@ -32,7 +32,7 @@ export function jellyCubeRecipe() {],// A recipe needs authored motion before a render job accepts a start time; the cloth bake reads the same clock.motion: { duration: 2, loop: false, tracks: [{ kind: 'node', id: 'clock', property: 'translation', interpolation: 'LINEAR', keyframes: [{ time: 0, value: [0, 0, 0] }, { time: 2, value: [0, 0, 0] }] }] },- camera: { eye: [1.2, 0.5, 1.7], target: [0, 0.35, 0] },+ camera: { eye: [0.45, 0.22, 1.75], target: [0, 0.3, 0] },cloth: {duration: 2,sheets: [{ node: 'cube', softBody: { shape: 0.15, pressure: 1 }, stiffness: 0.9, bend: 0, damping: 2 }],@@ -51,7 +51,7 @@ export function foldingFlagRecipe({ selfCollision = true } = {}) {{ id: 'pole', translation: [0, 2.6, 0] },],motion: { duration: 3, loop: false, tracks: [{ kind: 'node', id: 'pole', property: 'translation', interpolation: 'LINEAR', keyframes: [{ time: 0, value: [0, 2.6, 0] }, { time: 0.3, value: [0, 2.6, 0] }, { time: 1.6, value: [0, 0.25, 0] }, { time: 3, value: [0, 0.25, 0] }] }] },- camera: { eye: [0.4, 1.4, 4.2], target: [0, 1.1, 0] },+ camera: { eye: [0.7, 0.8, 3.0], target: [0, 0.5, 0] },cloth: {duration: 3,sheets: [{ node: 'flag', pin: ['top'], pinTo: { node: 'pole' }, selfCollision, stiffness: 0.9, bend: 0.2, damping: 1 }],@@ -120,3 +120,25 @@ export function measureLanding(frames, restHeight) {}return { contact, maxDentFrame: deepest, maxDentDepth: restHeight - deepestHeight, restHeight };}++/** Smallest distance between two vertices that no triangle edge joins: the closest two layers of a fold come. */+export function unjoinedMinimumDistance(vertices, indices) {+ const count = vertices.length / 3;+ const joined = new Set();+ for (let corner = 0; corner < indices.length; corner += 3) {+ for (let side = 0; side < 3; side++) {+ const first = indices[corner + side];+ const second = indices[corner + (side + 1) % 3];+ joined.add(Math.min(first, second) * count + Math.max(first, second));+ }+ }+ let minimum = Infinity;+ for (let first = 0; first < count; first++) {+ for (let second = first + 1; second < count; second++) {+ if (joined.has(first * count + second)) continue;+ const distance = Math.hypot(vertices[first * 3] - vertices[second * 3], vertices[first * 3 + 1] - vertices[second * 3 + 1], vertices[first * 3 + 2] - vertices[second * 3 + 2]);+ minimum = Math.min(minimum, distance);+ }+ }+ return minimum;+}diff --git a/tests/soft-body-film.test.js b/tests/soft-body-film.test.jsindex b65e33c2..dda1b7ad 100644--- a/tests/soft-body-film.test.js+++ b/tests/soft-body-film.test.js@@ -1,32 +1,12 @@import test from 'node:test';import assert from 'node:assert/strict';import { createAgentScene } from '../src/agent-scene.js';-import { FILM_FRAMES_PER_SECOND, axisRange, foldingFlagRecipe, jellyCubeRecipe, measureLanding, recordFilm } from '../scripts/soft-body-film.mjs';+import { FILM_FRAMES_PER_SECOND, axisRange, foldingFlagRecipe, jellyCubeRecipe, measureLanding, recordFilm, unjoinedMinimumDistance } from '../scripts/soft-body-film.mjs';// Film tests for #696: each film is one recipe baked twice. The frames must match byte for byte, and the numbers a viewer// would see (the landing, the dent, the layers of the fold) are measured from the baked vertices, not read off a picture.const JELLY_FRAMES = 120;const FLAG_FRAMES = 150;-function unjoinedMinimumDistance(vertices, indices) {- const count = vertices.length / 3;- const joined = new Set();- for (let corner = 0; corner < indices.length; corner += 3) {- for (let side = 0; side < 3; side++) {- const first = indices[corner + side];- const second = indices[corner + (side + 1) % 3];- joined.add(Math.min(first, second) * count + Math.max(first, second));- }- }- let minimum = Infinity;- for (let first = 0; first < count; first++) {- for (let second = first + 1; second < count; second++) {- if (joined.has(first * count + second)) continue;- minimum = Math.min(minimum, Math.hypot(vertices[first * 3] - vertices[second * 3], vertices[first * 3 + 1] - vertices[second * 3 + 1], vertices[first * 3 + 2] - vertices[second * 3 + 2]));- }- }- return minimum;-}-function milliseconds(value) {return Number(value.toFixed(2));}