Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
feat(anatomy): richer shapes and viewer interaction, runtime and guide tweaks, and a fuller browser verification script
nirholas65f7f569bc32
diff --git a/api/_lib/anatomy/guide.js b/api/_lib/anatomy/guide.jsindex 6509a9f5d..1b53bfa0c 100644--- a/api/_lib/anatomy/guide.js+++ b/api/_lib/anatomy/guide.js@@ -31,7 +31,7 @@ An Anatomy spec is one JSON object that describes a machine as named parts, movi\`\`\`- \`title\` (max 80 chars), \`subtitle\` (max 140), \`summary\` (max 600; blank lines split paragraphs).-- \`view.speed\` multiplies every motion (0 to 4, default 1). \`view.explode\` (0 to 1) opens with the parts pulled apart. \`view.section\` (\`"x"\`, \`"y"\` or \`"z"\`) opens with a cutaway along that axis, which removes the half nearest the viewer. Use a cutaway for anything whose interesting parts are inside a casing (engines, pumps, reactors, turbines).+- \`view.speed\` multiplies every motion (0 to 4, default 1). \`view.explode\` (0 to 1) opens with the parts pulled apart. \`view.section\` (\`"x"\`, \`"y"\` or \`"z"\`) opens with a cutaway: a cutting plane perpendicular to that axis through the middle of the machine, removing the half nearest the viewer. To open a machine lengthwise, cut across a short axis: a turbofan lying along x is opened with \`"z"\`, which shows every stage from fan to nozzle. Use a cutaway for anything whose interesting parts are inside a casing (engines, pumps, reactors, turbines).## Coordinates and unitsdiff --git a/scripts/verify-anatomy-browser.mjs b/scripts/verify-anatomy-browser.mjsindex 7f1109e35..a78005852 100644--- a/scripts/verify-anatomy-browser.mjs+++ b/scripts/verify-anatomy-browser.mjs@@ -102,8 +102,15 @@ const shot = (page, name) => page.screenshot({ path: join(out, `${name}.png`) })await page.waitForTimeout(800);await shot(page, '08-clicked');- await page.evaluate(() => document.documentElement.setAttribute('data-theme', 'light'));- await page.waitForTimeout(1000);+ // The site's own switcher, the same path the nav toggle takes, so the+ // light palette is verified exactly as a visitor gets it.+ const lightOk = await page.evaluate(() => {+ window.threeTheme?.set('light');+ return { theme: document.documentElement.getAttribute('data-theme'), supported: window.threeTheme?.supportsLight?.() ?? null };+ });+ if (lightOk.theme !== 'light') problems.push(`[theme] light did not apply (supportsLight=${lightOk.supported})`);+ await page.mouse.move(2, 2);+ await page.waitForTimeout(1500);await shot(page, '09-light');await page.close();}diff --git a/src/anatomy/runtime.js b/src/anatomy/runtime.jsindex 567ac273f..146f9f208 100644--- a/src/anatomy/runtime.js+++ b/src/anatomy/runtime.js@@ -7,7 +7,7 @@// The same runtime powers /anatomy, /anatomy/:id permalinks and the standalone// HTML the agent skill writes (via the /anatomy-runtime.js bundle).-import { AnatomyViewer } from './viewer.js';+import { AnatomyViewer, openingSection } from './viewer.js';import { normalizeSpec } from './spec.js';const ICONS = {@@ -461,7 +461,7 @@ export function mountAnatomy(el, specIn, opts = {}) {if (!progressive) {if (spec.view.explode > 0 && explodeTarget === 0) animateExplode(spec.view.explode);else viewer.setExplode(explodeValue);- if (spec.view.section && !viewer.state.section) applySectionUI(spec.view.section);+ if (spec.view.section && !viewer.state.section) applySectionUI(openingSection(spec.view.section, viewer.modelSize));} else viewer.setExplode(explodeValue);if (selected && !spec.parts.some((p) => p.id === selected)) selected = null;if (step >= spec.steps.length) step = -1;diff --git a/src/anatomy/shapes.js b/src/anatomy/shapes.jsindex a579e0910..9c9951915 100644--- a/src/anatomy/shapes.js+++ b/src/anatomy/shapes.js@@ -167,6 +167,42 @@ function rotorGeometry(s) {return { merged, hubLen };}+/**+ * The rotor's line drawing: each blade as one twisted outline (leading edge,+ * tip, trailing edge) and the hub as its two end circles. Tracing every crease+ * of the solid blade instead quadruples the segment count, and a 60-blade+ * compressor stage repeated down a spool reaches 100K fat-line segments a+ * frame, which stalls integrated GPUs while adding nothing a reader can see.+ */+function rotorLines(s, hubLen) {+ const out = [];+ const span = Math.max(s.radius - s.hubRadius, s.radius * 0.05);+ const x0 = s.hubRadius - s.radius * 0.01;+ const steps = s.blades > 24 ? 3 : 5;+ const point = (t, side, cos, sin) => {+ const x = x0 + span * t;+ const ang = THREE.MathUtils.degToRad(s.pitch - s.twist * ((x - s.hubRadius) / span - 0.5));+ const half = (s.chord / 2) * side * (1 - 0.25 * Math.max(0, (x - s.hubRadius) / span));+ const y = half * Math.cos(ang);+ const z = half * Math.sin(ang);+ return [x * cos + z * sin, y, -x * sin + z * cos];+ };+ for (let b = 0; b < s.blades; b++) {+ const theta = (b / s.blades) * TAU;+ const cos = Math.cos(theta);+ const sin = Math.sin(theta);+ for (const side of [1, -1]) {+ for (let i = 0; i < steps; i++) out.push(...point(i / steps, side, cos, sin), ...point((i + 1) / steps, side, cos, sin));+ }+ out.push(...point(1, 1, cos, sin), ...point(1, -1, cos, sin));+ // Few, wide blades show their root; on a dense stage the hub hides it.+ if (s.blades <= 24) out.push(...point(0, 1, cos, sin), ...point(0, -1, cos, sin));+ }+ circleSegments(out, s.hubRadius, hubLen / 2, 32);+ circleSegments(out, s.hubRadius, -hubLen / 2, 32);+ return new Float32Array(out);+}+function helixCurve(radius, turns, length) {const pts = [];const n = Math.max(24, Math.ceil(turns * 24));@@ -309,9 +345,9 @@ export function buildShape(shape) {return { fill, lines };}case 'rotor': {- const { merged } = rotorGeometry(shape);+ const { merged, hubLen } = rotorGeometry(shape);orient(merged, shape.axis);- return { fill: merged, lines: edgePositions(merged, 28) };+ return { fill: merged, lines: orientPositions(rotorLines(shape, hubLen), shape.axis) };}case 'lathe': {const profile = shape.profile;diff --git a/src/anatomy/viewer.js b/src/anatomy/viewer.jsindex 269b44e66..3afc3a174 100644--- a/src/anatomy/viewer.js+++ b/src/anatomy/viewer.js@@ -37,6 +37,21 @@ const THEMES = {light: { bg: '#f5f6f8', grid: '#dfe3ea', gridCenter: '#c9d0db', accent: '#1f6fff', ink: '#0d1220', inkMix: 0.6, fillMix: 0 },};+/**+ * The axis a spec's opening cutaway should use. A cut perpendicular to a+ * machine's long axis only shows one slice of it, so when the requested axis+ * is clearly the longest one, the request is read as "open it lengthwise" and+ * the cut moves to a short axis, preferring a side cut (z, then x) over a top+ * one. Any other request is honoured as written.+ */+export function openingSection(axis, size) {+ if (!axis || !size) return axis || null;+ const len = size[axis];+ const others = ['x', 'y', 'z'].filter((a) => a !== axis);+ if (!(len > 0) || others.some((a) => !(len >= size[a] * 1.6))) return axis;+ return ['z', 'x', 'y'].find((a) => a !== axis);+}+function hashIndex(s, n) {let h = 0;for (const ch of String(s)) h = (Math.imul(h, 31) + ch.charCodeAt(0)) | 0;@@ -65,7 +80,13 @@ export class AnatomyViewer {this.themeName = resolveTheme(this.themePref);this.renderer = new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: false, powerPreference: 'high-performance' });- this.renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));+ // Adaptive resolution: the ceiling is the screen's own ratio, and a GPU+ // that cannot hold the frame rate gets fewer pixels instead of a stutter.+ this.maxPixelRatio = Math.min(window.devicePixelRatio || 1, 2);+ this.pixelRatio = this.maxPixelRatio;+ this.frameCost = 1 / 60;+ this.frameSamples = 0;+ this.renderer.setPixelRatio(this.pixelRatio);this.renderer.localClippingEnabled = true;this.renderer.toneMapping = THREE.NeutralToneMapping;this.renderer.outputColorSpace = THREE.SRGBColorSpace;@@ -127,7 +148,7 @@ export class AnatomyViewer {this.bindEvents();this.applyTheme();this.resize();- this.clock = new THREE.Clock();+ this.timer = new THREE.Timer();this.renderer.setAnimationLoop(() => this.tick());}@@ -446,12 +467,12 @@ export class AnatomyViewer {if (!progressive || !this.framed) {this.controls.target.set(0, 0, 0);this.camera.position.copy(ISO_DIR).multiplyScalar(dist);- this.camera.zoom = 1;+ this.camera.zoom = this.fitZoom(ISO_DIR);this.camera.updateProjectionMatrix();this.controls.update();this.framed = true;} else {- this.tween = { target: new THREE.Vector3(), zoom: 1 };+ this.tween = { target: new THREE.Vector3(), zoom: this.fitZoom(ISO_DIR) };}this.camera.updateProjectionMatrix();}@@ -475,6 +496,44 @@ export class AnatomyViewer {this.scene.add(this.grid);}+ /**+ * Zoom that fills the stage with the model as seen from `dir`. The frustum+ * fits the bounding sphere so any rotation stays in frame; a long, thin+ * machine seen side-on covers a fraction of that sphere, so the opening+ * zoom fits its box's projected silhouette instead.+ */+ fitZoom(dir, box = this.fitBox(), max = 3, target = new THREE.Vector3()) {+ if (!box || box.isEmpty()) return 1;+ const view = dir.clone().normalize();+ const right = new THREE.Vector3().crossVectors(new THREE.Vector3(0, 1, 0), view);+ if (right.lengthSq() < 1e-6) right.set(1, 0, 0);+ right.normalize();+ const up = new THREE.Vector3().crossVectors(view, right).normalize();+ let w = 0;+ let v = 0;+ const corner = new THREE.Vector3();+ for (const x of [box.min.x, box.max.x]) for (const y of [box.min.y, box.max.y]) for (const z of [box.min.z, box.max.z]) {+ corner.set(x, y, z).sub(target);+ w = Math.max(w, Math.abs(corner.dot(right)));+ v = Math.max(v, Math.abs(corner.dot(up)));+ }+ if (w < 1e-6 && v < 1e-6) return 1;+ const fit = Math.min(this.camera.right / Math.max(w * 1.1, 1e-6), this.camera.top / Math.max(v * 1.18, 1e-6));+ return THREE.MathUtils.clamp(fit, 1, max);+ }++ /** The whole picture for the opening shot: every visible part plus the effects and flows. */+ fitBox() {+ const box = this.modelBox();+ if (box.isEmpty()) return box;+ const tmp = new THREE.Box3();+ for (const e of [...this.effects, ...this.flows]) {+ tmp.setFromObject(e.object);+ if (!tmp.isEmpty()) box.union(tmp);+ }+ return box;+ }+updateFrustum() {const aspect = (this.width || 1) / (this.height || 1);const half = this.radius * 1.12;@@ -490,14 +549,14 @@ export class AnatomyViewer {/** Ease the camera to frame a set of parts, or the whole model when ids is empty. */focusOn(ids) {if (!ids?.length) {- this.tween = { target: new THREE.Vector3(), zoom: 1 };+ this.tween = { target: new THREE.Vector3(), zoom: this.fitZoom(this.camera.position.clone().sub(this.controls.target)) };return;}const box = this.modelBox(ids);if (box.isEmpty()) return;const c = box.getCenter(new THREE.Vector3());- const r = Math.max(box.getSize(new THREE.Vector3()).length() / 2, this.radius * 0.12);- const zoom = THREE.MathUtils.clamp(this.radius / (r * 1.35), 1, 5);+ box.expandByVector(new THREE.Vector3().setScalar(this.radius * 0.06));+ const zoom = this.fitZoom(this.camera.position.clone().sub(this.controls.target), box, 5, c) * 0.8;this.tween = { target: c, zoom };}@@ -506,7 +565,7 @@ export class AnatomyViewer {this.tween = null;this.controls.target.set(0, 0, 0);this.camera.position.copy(ISO_DIR).multiplyScalar(dist);- this.camera.zoom = 1;+ this.camera.zoom = this.fitZoom(ISO_DIR);this.camera.updateProjectionMatrix();this.controls.update();this.dirty = true;@@ -517,6 +576,7 @@ export class AnatomyViewer {const dirs = { iso: ISO_DIR, front: new THREE.Vector3(0, 0, 1), side: new THREE.Vector3(1, 0, 0), top: new THREE.Vector3(0, 1, 0.0001) };const dir = (dirs[name] || ISO_DIR).clone().normalize();this.camera.position.copy(this.controls.target).addScaledVector(dir, this.radius * 6);+ this.camera.zoom = this.fitZoom(dir, this.fitBox(), 3, this.controls.target);this.camera.updateProjectionMatrix();this.controls.update();this.dirty = true;@@ -796,8 +856,13 @@ export class AnatomyViewer {// ----------------------------------------------------------------- looptick() {- const dt = Math.min(this.clock.getDelta(), 0.1);- if (!this.visible || document.hidden) return;+ this.timer.update();+ const raw = this.timer.getDelta();+ const dt = Math.min(raw, 0.1);+ if (!this.visible || document.hidden) {+ this.frameSamples = 0;+ return;+ }if (this.pendingPick) {const { x, y, ev } = this.pendingPick;this.pendingPick = null;@@ -836,9 +901,31 @@ export class AnatomyViewer {this.renderer.render(this.scene, this.camera);if (this.state.labels) this.updateLabels();this.dirty = false;+ if (animating) this.adaptResolution(raw);this.opts.onFrame?.();}+ /**+ * Track the smoothed frame time while the machine animates. Sustained slow+ * frames step the pixel ratio down; sustained headroom steps it back up to+ * the screen's ratio. Steps are spaced out so the image never flickers.+ */+ adaptResolution(dt) {+ if (dt <= 0 || dt > 10) return;+ this.frameCost += (dt - this.frameCost) * 0.1;+ this.frameSamples++;+ if (this.frameSamples < 30) return;+ let next = this.pixelRatio;+ if (this.frameCost > 1 / 30) next = Math.max(0.5, this.pixelRatio * 0.75);+ else if (this.frameCost < 1 / 70) next = Math.min(this.maxPixelRatio, this.pixelRatio / 0.75);+ if (Math.abs(next - this.pixelRatio) < 0.01) return;+ this.pixelRatio = next;+ this.frameSamples = 0;+ this.renderer.setPixelRatio(next);+ this.renderer.setSize(this.width, this.height, false);+ this.dirty = true;+ }+/** Project label anchors to screen and hide the ones that would overlap. */updateLabels() {const placed = [];@@ -893,7 +980,7 @@ export class AnatomyViewer {/** PNG of the current frame at 2x device resolution. */async screenshot() {const prev = this.renderer.getPixelRatio();- this.renderer.setPixelRatio(Math.min(prev * 2, 3));+ this.renderer.setPixelRatio(Math.min(this.maxPixelRatio * 2, 3));this.renderer.setSize(this.width, this.height, false);const frame = { dark: this.themeName === 'dark', pixelScale: (this.renderer.getPixelRatio() * this.height * this.camera.zoom) / (this.camera.top - this.camera.bottom) };for (const e of this.effects) e.update(this.time, 0, frame);@@ -908,6 +995,7 @@ export class AnatomyViewer {dispose() {this.renderer.setAnimationLoop(null);+ this.timer.dispose();this.resizeObserver.disconnect();this.visObserver.disconnect();this.themeObserver.disconnect();