Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Draw the jelly presets on a 0.3 bevel box so the dent bulges into a belly, and step dent and rebound clearly (#852)

Nguyen Xuan An221baa8b7c6d
diff --git a/docs/physics.md b/docs/physics.md
index 328c1b85..62733c51 100644
--- a/docs/physics.md
+++ b/docs/physics.md
@@ -450,17 +450,17 @@ Jelly feel (`tests/soft-body-bounce.test.js`, measured): the 0.5 m rounded cube
| 0.02 | 0.9 | 0.065 m | 0.000 m | 0.093 m | 0.441 m |
| 0.02 | 0.1 | 0.099 m | 0.000 m | 0.100 m | 0.447 m |
-Before `bounce` existed, `shape` and `damping` were the only knobs that changed the rebound, and none gave a controlled one. Lowering `shape` or `stiffness` deepens the dent (0.026 to 0.099 m), so softness needed no new field. A very low `shape` has a price: with `shape` 0.02 and `stiffness` 0.1, 132 of the cube's 588 triangles turn inside out in the worst frame and the dent shows ragged, doubled edges. It is a real surface fold, not shading, and `selfCollision`, `bend`, more solver iterations and a gentler volume step do not remove it (measured). Keep `shape` at 0.13 or more for a jelly that has to look clean.
+Before `bounce` existed, `shape` and `damping` were the only knobs that changed the rebound, and none gave a controlled one. Lowering `shape` or `stiffness` deepens the dent (0.026 to 0.099 m), so softness needed no new field. A very low `shape` has a price: with `shape` 0.02 and `stiffness` 0.1, 132 of the cube's 588 triangles turn inside out in the worst frame. That is a real surface fold, and `selfCollision`, `bend`, more solver iterations and a gentler volume step do not remove it (measured). A separate cause made a clean dent look ragged: the film's box has a 0.12 bevel, so each flat side is a single quad between two rim rows, the bulge can only sit on the rims, and at the dent the top rim juts past the sunken top face (a ledge and a stepped outline, with no fold and no inverted triangle). A box with a 0.3 bevel has rows of vertices up the sides, and the bulge becomes a belly.
-Three presets that step clearly (`JELLY_PRESETS` in `tests/support/jelly-cube.mjs`, previewed by `scripts/render-jelly-bounce.mjs` and labelled by `scripts/label-jelly-bounce.py`; the preview is 960 by 540 at 60 fps, 4 seconds, from a long lens so the cubes carry no perspective skew, and each cube has a name label and its own colour):
+Three presets that step clearly, drawn on a box with `bevel` 0.3 and `subdivisions` 4 (`JELLY_PRESETS` and `JELLY_LOOK` in `tests/support/jelly-cube.mjs`, previewed by `scripts/render-jelly-bounce.mjs` and labelled by `scripts/label-jelly-bounce.py`; the preview is 960 by 540 at 60 fps, 4 seconds, from a long lens so the cubes carry no perspective skew, and each cube has a name label and its own colour):
| Preset (cube colour in the preview) | `shape` | `stiffness` | `bounce` | Dent | Sideways bulge | Rebound | Stops moving at step |
| --- | --- | --- | --- | --- | --- | --- | --- |
-| firm (blue) | 1 | 0.9 | 0 | 0.026 m | 0.020 m | 0.102 m | 96 |
-| medium (orange) | 0.3 | 0.05 | 0.5 | 0.041 m | 0.038 m | 0.184 m | 126 |
-| soft (pink) | 0.13 | 0.01 | 0.9 | 0.049 m | 0.064 m | 0.377 m | 186 |
+| firm (blue) | 1 | 0.9 | 0 | 0.029 m | 0.022 m | 0.147 m | 110 |
+| medium (orange) | 0.2 | 0.05 | 0.5 | 0.054 m | 0.068 m | 0.223 m | 156 |
+| soft (pink) | 0.06 | 0.02 | 0.9 | 0.085 m | 0.106 m | 0.349 m | 210 |
-A dent without folds stops near 5 cm, so the dent steps 1.6 and 1.2 times while the rebound steps 1.8 and 2.0 times; the soft cube also keeps wobbling about twice as long as the firm one. All three keep every triangle facing out (0 inverted in the bake), the top face is level within 0.05 degrees at the deepest dent and within 0.1 degrees at the end, and the sides bulge past the top at the dent (the soft cube is 0.564 m wide at the dent against a top face 0.385 m wide, equal on the left and right), so the lit side of that rim can look like a separate sliver in a still.
+The dent steps 1.9 and 1.6 times and the rebound 1.5 and 1.6 times, so each step shows in the dent and in the air under the cube. Every triangle keeps facing out (0 inverted in the bake, 0 folded at the dent), the top face is level within 0.03 degrees at the deepest dent and at the end, and the widest row of the soft cube at its dent sits at 5 percent of its height (a belly low on the body), not at the top rim. In flight the soft cube is 43.3 to 53.1 cm tall and 48.3 to 58.1 cm wide (the firm one 48.3 to 50.6 cm tall), so it stretches and wobbles while it is airborne.
With `damping: 0` and the default shape, `bounce` 1 sends the cube back to 1.85 m of its 1.95 m drop.
diff --git a/scripts/render-jelly-bounce.mjs b/scripts/render-jelly-bounce.mjs
index 513dd39c..cccdc235 100644
--- a/scripts/render-jelly-bounce.mjs
+++ b/scripts/render-jelly-bounce.mjs
@@ -11,7 +11,7 @@ import { Client } from '@modelcontextprotocol/client';
import { StdioClientTransport } from '@modelcontextprotocol/client/stdio';
import '../tests/support/load-agent-scene-modules.mjs';
import { createAgentScene } from '../src/agent-scene.js';
-import { invertedTriangles, JELLY_FRAMES_PER_SECOND, JELLY_PRESETS, jellyCubeRecipe, measureBounce, topFaceTilt } from '../tests/support/jelly-cube.mjs';
+import { invertedTriangles, JELLY_FRAMES_PER_SECOND, JELLY_LOOK, JELLY_PRESETS, jellyCubeRecipe, measureBounce, topFaceTilt, widestRowHeight } from '../tests/support/jelly-cube.mjs';
const PREVIEW = { width: 960, height: 540, fps: JELLY_FRAMES_PER_SECOND };
const FRAME_COUNT = 240;
@@ -23,7 +23,7 @@ const POSITIONS = { firm: { x: -0.85, color: [0.08, 0.3, 0.85] }, medium: { x: 0
const CUBES = JELLY_PRESETS.map(preset => ({ ...preset, ...POSITIONS[preset.id] }));
function threeCubeRecipe() {
- const single = jellyCubeRecipe({ duration: FRAME_COUNT / JELLY_FRAMES_PER_SECOND });
+ const single = jellyCubeRecipe({ duration: FRAME_COUNT / JELLY_FRAMES_PER_SECOND, ...JELLY_LOOK });
const cubeNodes = CUBES.map(cube => ({ id: cube.id, geometry: 'jelly', material: `${cube.id}-gel`, translation: [cube.x, 2.2, 0], scale: [0.5, 0.5, 0.5] }));
return {
...single,
@@ -73,10 +73,11 @@ function restAndShape(id, frames, bounce) {
for (let index = 0; index < frames[step].length; index++) move = Math.max(move, Math.abs(frames[step][index] - frames[step - 1][index]));
if (move > 0.0015) settledStep = step;
}
- const single = jellyCubeRecipe({ softBody: { pressure: 1, ...preset.softBody }, sheet: preset.sheet, duration: FRAME_COUNT / JELLY_FRAMES_PER_SECOND });
+ const single = jellyCubeRecipe({ softBody: { pressure: 1, ...preset.softBody }, sheet: preset.sheet, duration: FRAME_COUNT / JELLY_FRAMES_PER_SECOND, ...JELLY_LOOK });
return {
bulgeMeters: Number((horizontalExtent(frames[bounce.deepestFrame]) - horizontalExtent(frames[0])).toFixed(4)),
tiltAtDeepestDentDegrees: Number(topFaceTilt(frames[bounce.deepestFrame]).toFixed(2)),
+ widestRowAtDeepestDent: Number(widestRowHeight(frames[bounce.deepestFrame]).toFixed(2)),
tiltAtLastFrameDegrees: Number(topFaceTilt(frames.at(-1)).toFixed(2)),
lastMovingStep: settledStep,
invertedTriangles: invertedTriangles(single, FRAME_COUNT),
diff --git a/tests/soft-body-bounce.test.js b/tests/soft-body-bounce.test.js
index c5a879ad..c57f1574 100644
--- a/tests/soft-body-bounce.test.js
+++ b/tests/soft-body-bounce.test.js
@@ -2,7 +2,7 @@ import test from 'node:test';
import { createHash } from 'node:crypto';
import assert from 'node:assert/strict';
import { createCloth } from '../src/cloth.js';
-import { foldedTriangles, frameHashes, invertedTriangles, JELLY_PRESETS, jellyCubeRecipe, measureBounce, recordJelly, topFaceTilt } from './support/jelly-cube.mjs';
+import { foldedTriangles, frameHashes, invertedTriangles, JELLY_LOOK, JELLY_PRESETS, jellyCubeRecipe, measureBounce, recordJelly, topFaceTilt, widestRowHeight } from './support/jelly-cube.mjs';
const FRAME_COUNT = 110;
@@ -77,28 +77,44 @@ test('a recipe sheet rejects a bad bounce at the boundary', async () => {
assert.throws(() => createAgentScene(jellyCubeRecipe({ softBody: { bounce: 2 } })), /softBody\.bounce.*0.*1/s);
});
-// The preview's three presets (`JELLY_PRESETS`): each step must be clearly visible, and the surface must not fold.
+// The preview's three presets (`JELLY_PRESETS`) drawn with `JELLY_LOOK`: each step must be clearly visible, and the surface must not fold.
+const presetRecipe = (preset, duration) => jellyCubeRecipe({ softBody: preset.softBody, sheet: preset.sheet, duration, ...JELLY_LOOK });
+
test('the firm, medium and soft presets step clearly in dent and rebound without folding', () => {
- const results = JELLY_PRESETS.map(preset => measure(preset.softBody, preset.sheet));
+ const results = JELLY_PRESETS.map(preset => measureBounce(recordJelly(presetRecipe(preset, 2), FRAME_COUNT)));
const [firm, medium, soft] = results;
- assert.ok(medium.dent > firm.dent * 1.4 && soft.dent > medium.dent * 1.1, `dents ${results.map(result => result.dent.toFixed(3)).join(', ')}`);
- assert.ok(medium.reboundHeight > firm.reboundHeight * 1.6 && soft.reboundHeight > medium.reboundHeight * 1.8, `rebounds ${results.map(result => result.reboundHeight.toFixed(3)).join(', ')}`);
- assert.ok(soft.dent > 0.045 && soft.reboundHeight > 0.35, `soft dent ${soft.dent}, rebound ${soft.reboundHeight}`);
+ assert.ok(medium.dent > firm.dent * 1.6 && soft.dent > medium.dent * 1.4, `dents ${results.map(result => result.dent.toFixed(3)).join(', ')}`);
+ assert.ok(medium.reboundHeight > firm.reboundHeight * 1.3 && soft.reboundHeight > medium.reboundHeight * 1.4, `rebounds ${results.map(result => result.reboundHeight.toFixed(3)).join(', ')}`);
+ assert.ok(soft.dent > 0.08 && soft.reboundHeight > 0.3, `soft dent ${soft.dent}, rebound ${soft.reboundHeight}`);
for (const preset of JELLY_PRESETS) {
- const folded = foldedTriangles(jellyCubeRecipe({ softBody: preset.softBody, sheet: preset.sheet }), FRAME_COUNT);
- assert.ok(folded <= 8, `${preset.id} folded ${folded} of 588 triangles in one frame`);
+ const folded = foldedTriangles(presetRecipe(preset, 2), FRAME_COUNT);
+ assert.ok(folded <= 8, `${preset.id} folded ${folded} triangles in one frame`);
}
});
-test('a near-zero shape folds the surface on impact, which is why the soft preset keeps 0.13 or more', () => {
- const folded = foldedTriangles(jellyCubeRecipe({ softBody: { shape: 0.02, bounce: 1 }, sheet: { stiffness: 0.1 } }), FRAME_COUNT);
- assert.ok(folded > 50, `shape 0.02 should fold more than 50 triangles, got ${folded}`);
+test('a near-zero shape folds the surface on impact, which is why the soft preset keeps 0.06 or more', () => {
+ const folded = foldedTriangles(jellyCubeRecipe({ softBody: { shape: 0.02, bounce: 1 }, sheet: { stiffness: 0.1 }, ...JELLY_LOOK }), FRAME_COUNT);
+ assert.ok(folded > 20, `shape 0.02 should fold more than 20 triangles, got ${folded}`);
+});
+
+// The ragged, doubled edge of the soft cube at its deepest dent was the mesh, not a fold: a flat side is one quad, so the
+// bulge sat on the top rim. The bevel decides where the widest row is.
+test('the soft dent bulges into a belly with the preview bevel and into a top ledge with the thin bevel', () => {
+ const soft = JELLY_PRESETS.find(preset => preset.id === 'soft');
+ const widestAtDent = options => {
+ const frames = recordJelly(jellyCubeRecipe({ softBody: soft.softBody, sheet: soft.sheet, ...options }), FRAME_COUNT);
+ return widestRowHeight(frames[measureBounce(frames).deepestFrame]);
+ };
+ const belly = widestAtDent(JELLY_LOOK);
+ const ledge = widestAtDent({});
+ assert.ok(belly < 0.5, `the belly sits in the lower half, got ${belly.toFixed(2)} of the height`);
+ assert.ok(ledge > 0.8, `the thin bevel puts the widest row at the top rim, got ${ledge.toFixed(2)}`);
});
// The frames the preview shows: every preset keeps its surface the right way out, and each rests level once it has settled.
test('the presets never invert a triangle and rest level after 4 seconds', () => {
for (const preset of JELLY_PRESETS) {
- const recipe = jellyCubeRecipe({ softBody: preset.softBody, sheet: preset.sheet, duration: 4 });
+ const recipe = presetRecipe(preset, 4);
const inverted = invertedTriangles(recipe, 240);
assert.ok(inverted <= 2, `${preset.id} inverted ${inverted} triangles in one frame`);
const frames = recordJelly(recipe, 240);
diff --git a/tests/support/jelly-cube.mjs b/tests/support/jelly-cube.mjs
index f6cc3e2a..24c8cd72 100644
--- a/tests/support/jelly-cube.mjs
+++ b/tests/support/jelly-cube.mjs
@@ -9,10 +9,10 @@ const FLOOR_THICKNESS = 0.01;
const LIFT_OFF_HEIGHT = 0.02;
/** The recipe for one jelly cube; `softBody` and `sheet` override the film's softBody fields and sheet fields. */
-export function jellyCubeRecipe({ softBody = {}, sheet = {}, x = 0, duration = 2 } = {}) {
+export function jellyCubeRecipe({ softBody = {}, sheet = {}, x = 0, duration = 2, bevel = 0.12, subdivisions = 3 } = {}) {
return {
format: '67js.scene', schemaVersion: 1,
- geometries: [{ id: 'jelly', type: 'box', bevel: 0.12, subdivisions: 3 }, { id: 'floor', type: 'box', bevel: 0 }],
+ geometries: [{ id: 'jelly', type: 'box', bevel, subdivisions }, { id: 'floor', type: 'box', bevel: 0 }],
materials: [{ id: 'gel', color: [0.25, 0.85, 0.45] }, { id: 'stone', color: [0.55, 0.55, 0.58] }],
nodes: [
{ id: 'cube', geometry: 'jelly', material: 'gel', translation: [x, 2.2, 0], scale: [0.5, 0.5, 0.5] },
@@ -80,16 +80,44 @@ export function measureBounce(frames) {
}
/**
- * The three jellies of the preview, from firm to soft, with the colour each is drawn in. The soft one keeps `shape` at
- * 0.13 or more: below about 0.1 the surface folds over itself on impact (see `foldedTriangles`), which is how a deeper
- * dent was paid for before. A fold-free dent stops near 5 cm, so the steps between presets show most in the rebound.
+ * The geometry the preview draws the jelly with. A 0.3 bevel gives the side faces rows of vertices at many heights, so the
+ * dent bulges into a belly. With the film's 0.12 bevel each side is one flat quad between two rim rows, the bulge can only
+ * sit on the rims, and the top rim juts past the sunken top face: a ledge and a stepped outline (see `widestRowHeight`).
+ */
+export const JELLY_LOOK = { bevel: 0.3, subdivisions: 4 };
+
+/**
+ * The three jellies of the preview, from firm to soft, with the colour each is drawn in. Measured with `JELLY_LOOK`:
+ * dents of about 2.9, 5.4 and 8.5 cm and rebounds of about 15, 22 and 35 cm. Shape below about 0.05 folds or inverts triangles on
+ * impact (see `foldedTriangles`), so 0.06 with stiffness 0.02 is the softest preset that also inverts no triangle.
*/
export const JELLY_PRESETS = [
{ id: 'firm', colorName: 'blue', softBody: { shape: 1, bounce: 0 }, sheet: {} },
- { id: 'medium', colorName: 'orange', softBody: { shape: 0.3, bounce: 0.5 }, sheet: { stiffness: 0.05 } },
- { id: 'soft', colorName: 'pink', softBody: { shape: 0.13, bounce: 0.9 }, sheet: { stiffness: 0.01 } },
+ { id: 'medium', colorName: 'orange', softBody: { shape: 0.2, bounce: 0.5 }, sheet: { stiffness: 0.05 } },
+ { id: 'soft', colorName: 'pink', softBody: { shape: 0.06, bounce: 0.9 }, sheet: { stiffness: 0.02 } },
];
+/**
+ * Where the widest vertex sits in a frame, as a fraction of the body height from the floor (0 bottom, 1 top). A belly has
+ * its widest row low or in the middle; a ledge puts it at the top rim.
+ */
+export function widestRowHeight(frame) {
+ let low = Infinity;
+ let high = -Infinity;
+ let widestHeight = 0;
+ let widest = -Infinity;
+ for (let index = 0; index < frame.length; index += 3) {
+ low = Math.min(low, frame[index + 1]);
+ high = Math.max(high, frame[index + 1]);
+ const reach = Math.max(Math.abs(frame[index]), Math.abs(frame[index + 2]));
+ if (reach > widest) {
+ widest = reach;
+ widestHeight = frame[index + 1];
+ }
+ }
+ return (widestHeight - low) / (high - low);
+}
+
/** Angle in degrees of the plane through the highest vertices of a frame, against the horizontal, along x then z. */
export function topFaceTilt(frame) {
let highest = -Infinity;