Skip to content

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.

7.2 KB
Raw Blame
<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<title>Animation thumbnail harness</title>
		<style>
			html,
			body {
				margin: 0;
				background: transparent;
			}
			canvas {
				display: block;
			}
		</style>
	</head>
	<body>
		<!--
			Offline rendering harness for scripts/build-animation-thumbnails.mjs.

			Served by the Vite dev server (so `three` and /src modules resolve exactly
			as they do in the product) and driven by Playwright: the build script calls
			window.__thumb.renderClip(clipJson, name, opts) per clip and receives a
			WebP data-URL of the preview avatar frozen mid-performance.

			Uses the site's own retarget engine (src/animation-retarget.js), the same
			hipScale rules as AnimationManager._retarget, and the same preview avatar
			as the /animations gallery, so a thumbnail is a faithful still of what the
			clip actually plays like in the product.
		-->
		<script type="module">
			import * as THREE from 'three';
			import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
			import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
			import { getMeshoptDecoder } from '/src/viewer/internal.js';
			import {
				parseClipJSON,
				retargetClip,
				canonicalNodeMapFromObject,
				canonicalRestMapFromObject,
				canonicalWorldRestMapFromObject,
				hipsParentWorldQuat,
				hipRestLocalHeight,
				clipHipBaselineY,
			} from '/src/animation-retarget.js';

			const WIDTH = 480;
			const HEIGHT = 640; // 3:4 — matches the gallery card aspect
			const MODEL_URL = '/avatars/cz.glb';

			const renderer = new THREE.WebGLRenderer({
				antialias: true,
				alpha: true,
				preserveDrawingBuffer: true,
			});
			renderer.setSize(WIDTH, HEIGHT);
			renderer.setPixelRatio(1);
			renderer.shadowMap.enabled = true;
			renderer.shadowMap.type = THREE.PCFSoftShadowMap;
			renderer.toneMapping = THREE.ACESFilmicToneMapping;
			renderer.toneMappingExposure = 1.05;
			document.body.appendChild(renderer.domElement);

			const scene = new THREE.Scene();
			const pmrem = new THREE.PMREMGenerator(renderer);
			scene.environment = pmrem.fromScene(new RoomEnvironment(), 0.04).texture;

			const key = new THREE.DirectionalLight(0xffffff, 1.6);
			key.position.set(2.2, 4.5, 3.2);
			key.castShadow = true;
			key.shadow.mapSize.set(1024, 1024);
			key.shadow.camera.left = -2.5;
			key.shadow.camera.right = 2.5;
			key.shadow.camera.top = 2.5;
			key.shadow.camera.bottom = -2.5;
			key.shadow.bias = -0.0004;
			scene.add(key);
			scene.add(key.target);
			const rim = new THREE.DirectionalLight(0x99bbff, 0.7);
			rim.position.set(-2.5, 2.5, -2.5);
			scene.add(rim);

			const ground = new THREE.Mesh(
				new THREE.PlaneGeometry(30, 30),
				new THREE.ShadowMaterial({ opacity: 0.28 }),
			);
			ground.rotation.x = -Math.PI / 2;
			ground.receiveShadow = true;
			scene.add(ground);

			const camera = new THREE.PerspectiveCamera(33, WIDTH / HEIGHT, 0.05, 100);

			let model = null;
			let mixer = null;
			// Bind-pose snapshot (node → {p,q,s}) so every clip retargets against the
			// true rest pose, not whatever pose the previous clip froze the rig in.
			const restPose = new Map();
			// Canonical maps captured once at bind pose — same recipe as
			// AnimationManager.attach().
			let canonicalToNode = null;
			let canonicalRest = null;
			let canonicalWorldRest = null;
			let hipsParentQuat = null;
			let hipTargetLocalY = 0;
			const bones = [];

			async function boot() {
				const loader = new GLTFLoader();
				loader.setMeshoptDecoder(await getMeshoptDecoder());
				const gltf = await loader.loadAsync(MODEL_URL);
				model = gltf.scene;
				model.traverse((o) => {
					if (o.isMesh || o.isSkinnedMesh) {
						o.castShadow = true;
						o.frustumCulled = false;
					}
					if (o.isBone) bones.push(o);
					restPose.set(o, {
						p: o.position.clone(),
						q: o.quaternion.clone(),
						s: o.scale.clone(),
					});
				});
				scene.add(model);
				model.updateMatrixWorld(true);
				canonicalToNode = canonicalNodeMapFromObject(model);
				canonicalRest = canonicalRestMapFromObject(model);
				canonicalWorldRest = canonicalWorldRestMapFromObject(model);
				hipsParentQuat = hipsParentWorldQuat(model);
				hipTargetLocalY = hipRestLocalHeight(model);
				mixer = new THREE.AnimationMixer(model);
				return { bones: bones.length, canonical: canonicalToNode.size };
			}

			function restoreRestPose() {
				mixer.stopAllAction();
				mixer.uncacheRoot(model);
				for (const [node, t] of restPose) {
					node.position.copy(t.p);
					node.quaternion.copy(t.q);
					node.scale.copy(t.s);
				}
				model.updateMatrixWorld(true);
			}

			// Frame the posed skeleton: bone-position bounding box, padded, camera at a
			// fixed 3/4 angle fitted so the figure fills the frame consistently whether
			// it stands, sprawls, or cartwheels.
			function frameCamera() {
				const box = new THREE.Box3();
				const v = new THREE.Vector3();
				for (const b of bones) box.expandByPoint(b.getWorldPosition(v));
				box.expandByVector(new THREE.Vector3(0.22, 0.14, 0.22)); // flesh beyond joints
				box.min.y = Math.min(box.min.y, 0); // keep the ground contact in frame
				const center = box.getCenter(new THREE.Vector3());
				const size = box.getSize(new THREE.Vector3());
				const fovV = THREE.MathUtils.degToRad(camera.fov);
				const fovH = 2 * Math.atan(Math.tan(fovV / 2) * camera.aspect);
				const distV = size.y / 2 / Math.tan(fovV / 2);
				const distH = Math.max(size.x, size.z) / 2 / Math.tan(fovH / 2);
				const dist = Math.max(distV, distH) * 1.18 + Math.max(size.z, size.x) / 2;
				const azimuth = THREE.MathUtils.degToRad(24);
				const elevation = THREE.MathUtils.degToRad(11);
				camera.position.set(
					center.x + dist * Math.cos(elevation) * Math.sin(azimuth),
					center.y + dist * Math.sin(elevation),
					center.z + dist * Math.cos(elevation) * Math.cos(azimuth),
				);
				camera.lookAt(center);
				key.target.position.copy(center);
				key.target.updateMatrixWorld();
			}

			/**
			 * Pose the avatar at a representative frame of the clip and return a WebP
			 * data-URL. `opts.at` (0..1) picks the sample point in the clip.
			 */
			function renderClip(clipJson, name, opts = {}) {
				restoreRestPose();
				const clip = parseClipJSON(clipJson, name);
				let hipScale = 1;
				const baseline = clipHipBaselineY(clip);
				if (hipTargetLocalY > 0.05 && baseline > 0.05) {
					hipScale = Math.min(200, Math.max(0.2, hipTargetLocalY / baseline));
				}
				const { clip: bound, coverage } = retargetClip(clip, canonicalToNode, {
					targetRest: canonicalRest,
					targetWorldRest: canonicalWorldRest,
					hipsParentWorldQuat: hipsParentQuat,
					hipScale,
					minCoverage: 0.3,
				});
				if (!bound) throw new Error(`retarget failed (coverage ${(coverage * 100).toFixed(0)}%)`);
				const action = mixer.clipAction(bound);
				action.play();
				const at = Math.min(0.98, Math.max(0.02, opts.at ?? 0.4));
				mixer.update(Math.max(0.001, bound.duration * at));
				model.updateMatrixWorld(true);
				frameCamera();
				renderer.render(scene, camera);
				const url = renderer.domElement.toDataURL('image/webp', opts.quality ?? 0.82);
				action.stop();
				mixer.uncacheClip(bound);
				return { dataUrl: url, coverage };
			}

			window.__thumb = {
				ready: boot(),
				renderClip,
			};
		</script>
	</body>
</html>