3D engine that AI agents drive
Make the flag film read as fabric: finer grid, fast drop with a zig-zag drag, framed whole, darker look (#696)
Nguyen Xuan An907075efbc75
diff --git a/docs/physics.md b/docs/physics.mdindex fe92f335..a67ebabf 100644--- a/docs/physics.md+++ b/docs/physics.md@@ -443,7 +443,7 @@ A recipe `cloth` section records deformable sheets with a position-based solverSoft body films (`scripts/soft-body-film.mjs`, `tests/soft-body-film.test.js`): a jelly cube landing and a flag lowered into folds on itself, each one recipe that the test bakes twice and the preview script renders.- Jelly cube: a rounded box 0.5 m across (384 particles) dropped from 1.95 m, `softBody { shape: 0.15, pressure: 1 }`. It first rests on the floor at frame 48 (0.8 s) and is dented 0.0503 m (10 percent of its height) at frame 49, one step later, while its width grows from 0.5 m to 0.547 m; the frame before contact has no dent.-- Flag: a 30 by 30 grid, 1 m tall, hung by its top edge from a pole (`pinTo`). The pole swings 0.3 m from side to side while it falls for 1.3 s so the sheet ripples, lands, then pulls the cloth 1.1 m across the floor, which gathers a crumpled ridge near the pole and flattens the rest. The sheet sets `bend: 0.001`, `stiffness: 0.6` and `thickness: 0.003`, so its self collision layers stack 6 mm apart (the first film used the mesh-based 3.7 cm and piled a block 0.547 m tall; the second was a flat card in the air and a smooth sheet at rest). Measured in the test: first touch at step 70; normal spread (0 flat, toward 1 scattered) is 0 at the start, 0.597 mid fall (step 49) and 0.300 at rest, the sheet bends 0.3 m or more out of its plane mid fall, and the final frame (step 239) has every vertex under 0.0491 m above the floor. With `selfCollision: true` the closest pair of particles not joined by an edge is never under 5 mm apart in any sampled frame after touch (0.0060 m); the same film without it comes down to 3 mm. A 36 by 36 grid exceeds the cloth work limit for 4 s. The look is a dark red cloth on a mid grey floor under a low side key light (`environment.lightDirection`), so folds cast shadows.+- Flag: a 38 by 38 grid (1444 particles, the most that fits the cloth work limit for a 3 s film), 1 m tall, hung by its top edge from a pole (`pinTo`). The pole swings 0.3 m from side to side while it falls for 0.7 s, so the sheet ripples and folds in the air, then zig-zags 0.8 m from side to side while it moves 2 m across the floor, which tosses the cloth into several crumpled folds on a thin layer. The sheet sets `bend: 0.0002`, `stiffness: 0.6`, `damping: 1.5`, `friction: 0.4` and `thickness: 0.003`, so its self collision layers stack 6 mm apart (the first film used the mesh-based 3.7 cm and piled a block 0.547 m tall; the second was a flat card in the air and a smooth sheet at rest; the third ended as one smooth ridge). Measured in the test: first touch at step 42; normal spread (0 flat, toward 1 scattered) is 0 at the start, 0.469 mid fall (step 29) and 0.715 at rest (the test fails below 0.5), the sheet bends 0.61 m out of its plane mid fall, and at the final step (179) the highest vertex is 0.0451 m above the floor, half of the cloth lies under 0.0036 m and a tenth of it stands above 0.030 m in folds. With `selfCollision: true` the closest pair of particles not joined by an edge is never under 5 mm apart in any sampled frame after touch (0.0051 m); the same film without it comes down to 0.4 mm. Those numbers come from the same bake the preview renders, and the film JSON (`soft-body-film.json`) records the highest, median and 90th percentile height at rest. The bake takes 10 to 17 s on the loaded dev machine (about 55 to 90 ms a step with self collision), well above the about 3 s of the old 30 by 30 film: the finer grid is what makes the ripples fine enough to read as fabric. A 44 by 44 grid exceeds the 2000 particle self collision limit and a 4 s film at 38 by 38 exceeds the cloth work limit. The look is a dark red cloth on a dark grey floor under a low side key light (`environment.lightDirection`), so folds cast shadows, framed from 7.5 m so the whole fall and the landed layer stay in the picture.- 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 6 to 9 ms a step over 120 steps, flag 27 to 29 ms a step over 240 steps (16 ms a step without self collision, 625 particles). Sampling a recorded frame for playback costs about 1.5 to 2 ms.- `node scripts/render-soft-body-film.mjs <folder> [--flag-only]` (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 for the jelly and at start, mid fall, first touch, settling and final rest for the flag (`--flag-only` skips the jelly cube), `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.diff --git a/scripts/render-soft-body-film.mjs b/scripts/render-soft-body-film.mjsindex fca7945b..868d6879 100644--- a/scripts/render-soft-body-film.mjs+++ b/scripts/render-soft-body-film.mjs@@ -63,6 +63,15 @@ function filmNumbers(film, nodeId, frameCount) {}const meters = value => Number(value.toFixed(4));++/** Height above the floor of the highest vertex and of the median and 90th percentile vertex: a thin layer with folds has a low median and a higher tail. */+function restHeights(vertices) {+ const heights = [];+ for (let index = 1; index < vertices.length; index += 3) heights.push(vertices[index]);+ heights.sort((low, high) => low - high);+ const at = share => meters(heights[Math.floor(share * (heights.length - 1))]);+ return { highest: meters(heights[heights.length - 1]), median: at(0.5), percentile90: at(0.9) };+}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. */@@ -156,6 +165,7 @@ async function main() {midFallStep: flagMidFall,firstTouchStep: flagTouch,pileHeightMeters: meters(axisRange(flag.record.frames[lastFlagStep], 1).extent),+ restHeightsMeters: restHeights(flag.record.frames[lastFlagStep]),normalSpread: {start: meters(normalSpread(flagStart, flagIndicesForSpread)),midFall: meters(normalSpread(flag.record.frames[flagMidFall], flagIndicesForSpread)),diff --git a/scripts/soft-body-film.mjs b/scripts/soft-body-film.mjsindex 0b9ef9fd..9457264e 100644--- a/scripts/soft-body-film.mjs+++ b/scripts/soft-body-film.mjs@@ -42,22 +42,24 @@ export function jellyCubeRecipe() {});}-// The pole's path: a side to side swing on the way down so the sheet ripples, a landing, then a pull across the floor that gathers-// the cloth into a crumpled ridge and flattens the rest into a thin layer.+// The pole's path: a fast side to side swing on the way down so the sheet ripples, a landing at 0.7 s, then a zig-zag drag across the floor+// that tosses the cloth from side to side, so it piles in several crumpled folds instead of one ridge.const POLE_KEYFRAMES = [{ time: 0, value: [0, 2.6, 0] },- { time: 0.2, value: [0.1, 2.5, 0.3] },- { time: 0.4, value: [-0.1, 2.1, -0.3] },- { time: 0.6, value: [0.12, 1.7, 0.35] },- { time: 0.8, value: [-0.1, 1.2, -0.3] },- { time: 1.0, value: [0.1, 0.7, 0.3] },- { time: 1.3, value: [0, 0.012, 0.9] },- { time: 2.2, value: [0, 0.012, 2] },- { time: 4, value: [0, 0.012, 2] },+ { time: 0.2, value: [0.15, 2.3, 0.3] },+ { time: 0.4, value: [-0.15, 1.5, -0.3] },+ { time: 0.6, value: [0.1, 0.5, 0.4] },+ { time: 0.8, value: [0, 0.012, 0.6] },+ { time: 1.0, value: [0.4, 0.012, 1.0] },+ { time: 1.3, value: [-0.4, 0.012, 1.5] },+ { time: 1.6, value: [0.4, 0.012, 2.0] },+ { time: 1.9, value: [-0.4, 0.012, 2.5] },+ { time: 2.3, value: [0, 0.012, 2.8] },+ { time: 3, value: [0, 0.012, 2.8] },];-export const FLAG_FILM_SECONDS = 4;+export const FLAG_FILM_SECONDS = 3;// A dark saturated cloth on a mid grey floor, lit by a low key from the side so every fold casts a shadow.-const FLAG_LOOK = { lightDirection: [-0.9, 0.45, 0.55], keyIntensity: 1.6, fillIntensity: 0.15, ambientIntensity: 0.25 };+const FLAG_LOOK = { lightDirection: [-0.9, 0.45, 0.55], keyIntensity: 1.3, fillIntensity: 0.15, ambientIntensity: 0.25 };/*** A flag hung from a pole by its top edge. The pole swings from side to side while it falls, so the sheet ripples, lands on the floor and@@ -66,19 +68,19 @@ const FLAG_LOOK = { lightDirection: [-0.9, 0.45, 0.55], keyIntensity: 1.6, fillI*/export function foldingFlagRecipe({ selfCollision = true } = {}) {return baseRecipe({- geometries: [{ id: 'flag', type: 'grid', segments: 30 }],- materials: [{ id: 'fabric', color: [0.62, 0.05, 0.1], doubleSided: true }],+ geometries: [{ id: 'flag', type: 'grid', segments: 38 }],+ materials: [{ id: 'fabric', color: [0.5, 0.03, 0.07], doubleSided: true }],nodes: [{ id: 'flag', geometry: 'flag', material: 'fabric', translation: [0, 2.1, 0], scale: [1.6, 1, 1] },{ id: 'pole', translation: [0, 2.6, 0] },],motion: { duration: FLAG_FILM_SECONDS, loop: false, tracks: [{ kind: 'node', id: 'pole', property: 'translation', interpolation: 'LINEAR', keyframes: POLE_KEYFRAMES }] },- camera: { eye: [1.1, 1.1, 4.3], target: [0, 0.75, 0.9] },- floorColor: [0.4, 0.4, 0.42],+ camera: { eye: [2.6, 1.8, 7.5], target: [0, 1.0, 1.2] },+ floorColor: [0.28, 0.28, 0.3],environment: FLAG_LOOK,cloth: {duration: FLAG_FILM_SECONDS,- sheets: [{ node: 'flag', pin: ['top'], pinTo: { node: 'pole' }, selfCollision, stiffness: 0.6, bend: 0.001, damping: 2.5, friction: 0.25, thickness: 0.003 }],+ sheets: [{ node: 'flag', pin: ['top'], pinTo: { node: 'pole' }, selfCollision, stiffness: 0.6, bend: 0.0002, damping: 1.5, friction: 0.4, thickness: 0.003 }],colliders: [{ node: 'ground', shape: 'box' }],},});diff --git a/tests/soft-body-film.test.js b/tests/soft-body-film.test.jsindex 97910999..857381d8 100644--- a/tests/soft-body-film.test.js+++ b/tests/soft-body-film.test.js@@ -49,7 +49,7 @@ test('a flag dropped by its pole falls to the floor and ends as a thin layer whoconst touch = measureLanding(first.frames, restHeight).contact;assert.ok(touch > 30 && touch < 90, `the hanging flag first touches the floor within frames 30 to 90, got ${touch}`);const midFall = Math.round(touch * 0.7);- assert.ok(axisRange(first.frames[midFall], 1).extent > restHeight * 0.6, 'mid fall the flag is still hanging in the air');+ assert.ok(axisRange(first.frames[midFall], 1).low > 0.1, `mid fall the flag is still in the air, lowest point ${axisRange(first.frames[midFall], 1).low} m`);// A stiff card would stay one plane: its normals agree and it has no depth. Cloth ripples, so both measures are well above zero.const spreadAtStart = normalSpread(first.frames[0], indices);const spreadMidFall = normalSpread(first.frames[midFall], indices);@@ -61,7 +61,14 @@ test('a flag dropped by its pole falls to the floor and ends as a thin layer whoconst finalRange = axisRange(first.frames[last], 1);assert.ok(finalRange.high < 0.05, `the flag ends no more than 5 cm above the floor, got ${finalRange.high} m`);const spreadAtRest = normalSpread(first.frames[last], indices);- assert.ok(spreadAtRest > 0.2, `the flag rests crumpled, not as a flat sheet (spread ${spreadAtRest})`);+ assert.ok(spreadAtRest > 0.5, `the flag rests crumpled, not as a flat sheet (spread ${spreadAtRest})`);+ // A thin layer with folds in it: most of the cloth lies within a centimetre or two of the floor, a tenth of it stands in folds above 2 cm.+ const restHeights = [];+ for (let index = 1; index < first.frames[last].length; index += 3) restHeights.push(first.frames[last][index]);+ restHeights.sort((low, high) => low - high);+ const heightAt = share => restHeights[Math.floor(share * (restHeights.length - 1))];+ assert.ok(heightAt(0.5) < 0.01, `half of the cloth lies within 1 cm of the floor, got ${heightAt(0.5)} m`);+ assert.ok(heightAt(0.9) > 0.02, `a tenth of the cloth stands in folds above 2 cm, got ${heightAt(0.9)} m`);assert.ok(axisRange(first.frames[last], 2).extent > 0.7, 'the flag lies spread over the floor, not stacked in one place');const without = recordFilm(foldingFlagRecipe({ selfCollision: false }), 'flag', FLAG_FRAMES);@@ -75,6 +82,6 @@ test('a flag dropped by its pole falls to the floor and ends as a thin layer whoassert.ok(closestWithout < closestWith * 0.9, `without self collision the layers come closer (${closestWithout} m)`);const bake = first.bakeMilliseconds;- context.diagnostic(`flag: touch at frame ${touch}, final height ${finalRange.high.toFixed(4)} m from ${restHeight.toFixed(3)} m, spread mid fall ${spreadMidFall.toFixed(3)}, at rest ${spreadAtRest.toFixed(3)}, closest unjoined pair ${closestWith.toFixed(4)} m (${closestWithout.toFixed(4)} m without self collision)`);+ context.diagnostic(`flag: touch at frame ${touch}, final height ${finalRange.high.toFixed(4)} m from ${restHeight.toFixed(3)} m, spread mid fall ${spreadMidFall.toFixed(3)}, at rest ${spreadAtRest.toFixed(3)}, p50 height ${heightAt(0.5).toFixed(4)} m, p90 ${heightAt(0.9).toFixed(4)} m, closest unjoined pair ${closestWith.toFixed(4)} m (${closestWithout.toFixed(4)} m without self collision)`);context.diagnostic(`flag: bake ${milliseconds(bake)} ms for ${FLAG_FRAMES} steps (${milliseconds(bake / FLAG_FRAMES)} ms a step with self collision, ${milliseconds(without.bakeMilliseconds / FLAG_FRAMES)} ms without)`);});