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.

4.3 KB
Raw Blame
<!doctype html>
<html>
	<head>
		<meta charset="utf-8" />
		<style>
			html, body { margin: 0; height: 100%; background: #0e0f16; }
			model-viewer {
				width: 100vw; height: 100vh;
				--poster-color: transparent;
				background: radial-gradient(120% 120% at 50% 20%, #1a1b26, #0a0b12);
			}
		</style>
		<script type="module" src="https://cdn.jsdelivr.net/npm/@google/[email protected]/dist/model-viewer.min.js"></script>
	</head>
	<body>
		<model-viewer
			id="mv"
			camera-controls="false"
			interaction-prompt="none"
			disable-zoom
			environment-image="neutral"
			shadow-intensity="0.6"
			shadow-softness="1"
			exposure="1.1"
			camera-orbit="25deg 82deg auto"
			field-of-view="30deg"
		></model-viewer>
		<script type="module">
			const mv = document.getElementById('mv');

			// The capture has a transparent ground, so a frame with the model in it has
			// opaque pixels and an empty one has none. Checked on a 48px copy: cheap, and
			// any model big enough to be a poster survives the downscale.
			async function hasPixels(blob) {
				const bitmap = await createImageBitmap(blob);
				const probe = new OffscreenCanvas(48, 48);
				const ctx = probe.getContext('2d');
				ctx.drawImage(bitmap, 0, 0, 48, 48);
				bitmap.close?.();
				const { data } = ctx.getImageData(0, 0, 48, 48);
				for (let i = 3; i < data.length; i += 4) if (data[i] > 8) return true;
				return false;
			}

			// theta: swing round so the longer horizontal side faces the camera instead
			// of pointing at it. phi: look down on something flat, across at something
			// tall. "auto" radius still fits the whole bounding sphere.
			function orbitFor({ x, y, z }) {
				const footprint = Math.max(x, z) || 1;
				const flat = y < 0.35 * footprint;
				const phi = flat ? 42 : y > 1.6 * footprint ? 80 : 68;
				const theta = z > 1.4 * x ? 62 : 28;
				return `${theta}deg ${phi}deg auto`;
			}

			// Load a GLB by (same-origin) URL, wait for it to render, frame it, and
			// return a PNG data URL. Called by the playwright driver via evaluate().
			window.__renderGlb = async (url) => {
				return new Promise((resolve, reject) => {
					const to = setTimeout(() => reject(new Error('load timeout')), 45000);
					const onError = (e) => { clearTimeout(to); reject(new Error('model-viewer error: ' + (e?.detail?.type || 'unknown'))); };
					mv.addEventListener('error', onError, { once: true });
					mv.addEventListener('load', async () => {
						clearTimeout(to);
						mv.removeEventListener('error', onError);
						try {
							// Choose the camera from the model's own shape. One fixed, nearly
							// side-on view works for a chair and fails for anything flat or
							// long: a wrench or a sword lying on the ground was shot edge-on
							// and came out as a sliver a few pixels wide.
							mv.cameraOrbit = orbitFor(mv.getDimensions());
							mv.jumpCameraToGoal();
							// Let a couple of frames settle so lighting/framing is stable.
							await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
							mv.dismissPoster?.();
							await new Promise((r) => setTimeout(r, 250));
							// Always the full square frame. Cropping to the model's "ideal
							// aspect" is what turned a thin object into a 73px-wide poster,
							// and every card in a gallery wants the same shape anyway.
							// A fixed pause is a guess about how long the first frame takes, and at
							// double resolution on a software renderer the guess was wrong for some
							// models: the capture came back with nothing drawn. So look at the frame,
							// and wait for one that actually has the model in it.
							let blob = await mv.toBlob({ mimeType: 'image/png', idealAspect: false });
							for (let attempt = 0; attempt < 8 && !(await hasPixels(blob)); attempt++) {
								await new Promise((r) => setTimeout(r, 400 + attempt * 300));
								await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
								blob = await mv.toBlob({ mimeType: 'image/png', idealAspect: false });
							}
							const reader = new FileReader();
							reader.onload = () => resolve(reader.result);
							reader.onerror = () => reject(new Error('blob read failed'));
							reader.readAsDataURL(blob);
						} catch (err) {
							reject(err);
						}
					}, { once: true });
					mv.src = url;
				});
			};
			window.__ready = true;
		</script>
	</body>
</html>