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.

21.9 KB
Raw Blame
<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<title data-i18n="demos_gallery_picker.meta_title">Avatar Gallery Picker · three.ws lab</title>
		<meta name="robots" content="noindex" />
		<link rel="stylesheet" href="/style.css" />
		<link rel="stylesheet" href="/nav.css" />
		<script
			type="module"
			src="https://cdn.jsdelivr.net/npm/@google/[email protected]/dist/model-viewer.min.js" integrity="sha384-wjsPzEO+lXOf7d9csIDfAQpPt2OKuUU3m0K9qsdKIuzX4N8czSF6hdh23eAbZa3p" crossorigin="anonymous"
		></script>
		<style>
			:root { color-scheme: dark; }
			body { background: #060606; color: #eee; font: 15px/1.5 -apple-system, system-ui, sans-serif; margin: 0; }
			.stage { max-width: 1280px; margin: 0 auto; padding: 48px 24px 80px; }
			.crumb a { color: #8a8d98; text-decoration: none; }
			.crumb a:hover { color: #3dc1ff; }
			h1 { font-size: 28px; font-weight: 600; letter-spacing: -0.015em; margin: 14px 0 8px; }
			.sub { color: #8a8d98; max-width: 72ch; margin: 0 0 24px; }
			code { background: rgba(255,255,255,0.06); padding: 1px 6px; border-radius: 5px; font-size: 12.5px; }

			.toolbar {
				display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
				margin-bottom: 16px;
			}
			.search-wrap { position: relative; flex: 1 1 280px; min-width: 220px; }
			.search-wrap input {
				width: 100%; padding: 10px 36px 10px 14px; border-radius: 9px;
				background: #080808; color: #eee; border: 1px solid #202020;
				font: inherit;
			}
			.search-wrap input:focus { outline: none; border-color: #2e2e2e; }
			.search-clear {
				position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
				background: transparent; border: 0; color: #8a8d98; cursor: pointer;
				padding: 6px 8px; font-size: 16px; line-height: 1;
			}
			.search-clear:hover { color: #3dc1ff; }
			.status-line {
				color: #8a8d98; font-size: 13px; flex: 0 0 auto;
				margin-left: auto;
			}

			.tag-row {
				display: flex; flex-wrap: wrap; gap: 6px;
				margin-bottom: 20px; min-height: 28px;
			}
			.tag-chip {
				background: rgba(255,255,255,0.05); color: #cfd1dc;
				border: 1px solid #202020; border-radius: 999px;
				padding: 5px 11px; font-size: 12.5px; cursor: pointer;
				font: inherit;
			}
			.tag-chip:hover { border-color: #2e2e2e; color: #eee; }
			.tag-chip.is-active { background: #3dc1ff; color: #0a0a0a; border-color: #3dc1ff; }

			.row { display: grid; grid-template-columns: 1fr 420px; gap: 24px; align-items: start; }
			@media (max-width: 980px) { .row { grid-template-columns: 1fr; } }

			.grid {
				display: grid; gap: 14px;
				grid-template-columns: repeat(3, minmax(0, 1fr));
			}
			@media (max-width: 900px) { .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
			@media (max-width: 560px) { .grid { grid-template-columns: 1fr; } }

			.card {
				background: #101010; border: 1.5px solid #202020; border-radius: 12px;
				overflow: hidden; cursor: pointer; display: flex; flex-direction: column;
				transition: border-color var(--duration-fast) var(--ease-standard), transform var(--duration-fast) var(--ease-standard);
				text-align: left;
				color: inherit; font: inherit; padding: 0;
			}
			.card:hover { border-color: #2e2e2e; }
			.card.is-selected { border-color: #3dc1ff; box-shadow: 0 0 0 1px #3dc1ff inset; }
			.thumb {
				aspect-ratio: 1 / 1; background: #161616; position: relative;
				display: flex; align-items: center; justify-content: center;
				overflow: hidden;
			}
			.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
			.thumb-ph {
				width: 100%; height: 100%;
				background: linear-gradient(135deg, #1c1c1c 0%, #161616 100%);
				display: flex; align-items: center; justify-content: center;
				color: #3e3e3e; font-size: 28px;
			}
			.card-body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 6px; }
			.card-name {
				font-size: 14px; font-weight: 600; color: #eee;
				white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
				margin: 0;
			}
			.card-tags { display: flex; flex-wrap: wrap; gap: 4px; }
			.card-tag {
				font-size: 11px; color: #8a8d98;
				background: rgba(255,255,255,0.04); border-radius: 999px;
				padding: 2px 7px; border: 1px solid #202020;
			}
			.card-meta {
				display: flex; justify-content: space-between; align-items: center;
				font-size: 12px; color: #8a8d98; margin-top: 2px;
			}

			.preview {
				position: sticky; top: 20px;
				background: #101010; border: 1.5px solid #202020; border-radius: 14px;
				padding: 16px; display: flex; flex-direction: column; gap: 14px;
			}
			.preview-stage {
				aspect-ratio: 1 / 1; border-radius: 10px; overflow: hidden;
				background: #080808; border: 1px solid #202020;
				display: flex; align-items: center; justify-content: center;
			}
			model-viewer { width: 100%; height: 100%; --poster-color: transparent; background: #080808; }
			.preview-empty { color: #4a4d5e; font-size: 13px; text-align: center; padding: 0 20px; }

			.preview h2 {
				font-size: 16px; font-weight: 600; margin: 0;
				white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
			}
			.preview-sub { color: #8a8d98; font-size: 12.5px; margin: 0; }

			.mode-radio { display: flex; gap: 4px; background: #080808; border: 1px solid #202020; border-radius: 9px; padding: 3px; }
			.mode-radio label {
				flex: 1; text-align: center; padding: 7px 4px;
				font-size: 12px; color: #8a8d98; cursor: pointer;
				border-radius: 7px; transition: background var(--duration-fast) var(--ease-standard), color var(--duration-fast) var(--ease-standard);
			}
			.mode-radio input { display: none; }
			.mode-radio input:checked + span { background: #202020; color: #eee; }
			.mode-radio label span { display: block; padding: 7px 4px; border-radius: 7px; }

			button.cta {
				background: #3dc1ff; color: #0a0a0a; border: 0; border-radius: 9px;
				padding: 11px 14px; font-weight: 600; cursor: pointer; font: inherit;
				width: 100%;
			}
			button.cta:disabled { opacity: 0.4; cursor: not-allowed; }
			button.cta.copied { background: #2dd66a; color: #0a0a0a; }

			.preview-foot {
				font-family: ui-monospace, monospace; font-size: 11.5px;
				color: #8a8d98; background: #080808; border: 1px solid #202020;
				border-radius: 8px; padding: 9px 11px; word-break: break-all;
				max-height: 110px; overflow: auto; white-space: pre-wrap;
			}

			.load-more-wrap { display: flex; justify-content: center; margin-top: 22px; }
			button.load-more {
				background: rgba(255,255,255,0.06); color: #eee; border: 1px solid #202020;
				border-radius: 9px; padding: 10px 22px; cursor: pointer; font: inherit;
			}
			button.load-more:hover { border-color: #2e2e2e; }
			button.load-more:disabled { opacity: 0.5; cursor: not-allowed; }
			.sentinel { height: 1px; }

			.empty, .error {
				grid-column: 1 / -1;
				padding: 40px 16px; text-align: center;
				color: #8a8d98; border: 1.5px dashed #202020; border-radius: 12px;
			}
			.error { color: #ff8888; border-color: #3a1f2b; }
		</style>
		<script src="/model-viewer-meshopt.js"></script>
	</head>
	<body>
		<header><div id="nav-container"></div></header>

		<main class="stage">
			<p class="crumb" data-i18n-html="demos_gallery_picker.lab"><a href="/demos">← lab</a></p>
			<h1 data-i18n="demos_gallery_picker.avatar_gallery_picker">Avatar Gallery Picker</h1>
			<p class="sub" data-i18n-html="demos_gallery_picker.browse_every_public_avatar_on_three_ws">
				Browse every public avatar on three.ws, preview it live, and copy the
				exact handoff your downstream surface needs — a share link, a raw GLB
				URL, or an <code>&lt;agent-3d&gt;</code> embed snippet. Powered by the
				live <code>/api/avatars/public</code> feed; this page never touches the
				production <code>/gallery</code>, <code>/create</code>, or
				<code>/avatars/:id</code> flows.
			</p>

			<div class="toolbar">
				<div class="search-wrap">
					<input type="text" id="search" placeholder="Search avatars…" autocomplete="off" aria-label="Search public avatars" />
					<button type="button" class="search-clear" id="search-clear" hidden aria-label="Clear search" data-i18n-attr="aria-label:demos_gallery_picker.clear_search_arialabel">×</button>
				</div>
				<span class="status-line" id="status" aria-live="polite" data-i18n="demos_gallery_picker.loading">Loading…</span>
			</div>

			<div class="tag-row" id="tags" aria-label="Tag filters"></div>

			<div class="row">
				<div>
					<div class="grid" id="grid"></div>
					<div class="load-more-wrap"><button class="load-more" id="load-more" hidden data-i18n="demos_gallery_picker.load_more">Load more</button></div>
					<div class="sentinel" id="sentinel"></div>
				</div>

				<aside class="preview" id="preview">
					<div class="preview-stage" id="preview-stage">
						<div class="preview-empty" id="preview-empty">Pick an avatar to preview it here.</div>
					</div>

					<div>
						<h2 id="preview-name" data-i18n="demos_gallery_picker.no_avatar_selected">No avatar selected</h2>
						<p class="preview-sub" id="preview-sub" data-i18n="demos_gallery_picker.three_ws_public_catalog">three.ws public catalog</p>
					</div>

					<div class="mode-radio" role="radiogroup" aria-label="Handoff mode">
						<label><input type="radio" name="mode" value="share" checked /><span data-i18n="demos_gallery_picker.share_link">Share link</span></label>
						<label><input type="radio" name="mode" value="glb" /><span data-i18n="demos_gallery_picker.glb_url">GLB URL</span></label>
						<label><input type="radio" name="mode" value="embed" /><span data-i18n="demos_gallery_picker.embed_snippet">Embed snippet</span></label>
					</div>

					<button class="cta" id="cta" disabled data-i18n="demos_gallery_picker.use_this_avatar">Use this avatar</button>

					<div class="preview-foot" id="preview-foot">Selected payload appears here.</div>
				</aside>
			</div>
		</main>

		<script src="/nav.js" defer></script>
		<script type="module">
			const els = {
				search: document.getElementById('search'),
				searchClear: document.getElementById('search-clear'),
				status: document.getElementById('status'),
				tags: document.getElementById('tags'),
				grid: document.getElementById('grid'),
				loadMore: document.getElementById('load-more'),
				sentinel: document.getElementById('sentinel'),
				previewStage: document.getElementById('preview-stage'),
				previewEmpty: document.getElementById('preview-empty'),
				previewName: document.getElementById('preview-name'),
				previewSub: document.getElementById('preview-sub'),
				cta: document.getElementById('cta'),
				previewFoot: document.getElementById('preview-foot'),
				modeInputs: document.querySelectorAll('input[name="mode"]'),
			};

			const state = {
				query: '',
				tag: '',
				cursor: null,
				loading: false,
				total: null,
				totalLoaded: 0,
				selected: null,
				mode: 'share',
				loadedTags: new Set(),
				cardsById: new Map(),
				firstPageDone: false,
			};

			const integer = new Intl.NumberFormat('en');

			function esc(s) {
				return String(s ?? '')
					.replace(/&/g, '&amp;')
					.replace(/</g, '&lt;')
					.replace(/>/g, '&gt;')
					.replace(/"/g, '&quot;');
			}

			function updateStatus() {
				if (state.loading && state.totalLoaded === 0) {
					els.status.textContent = 'Loading…';
					return;
				}
				if (state.total != null) {
					els.status.textContent = `Showing ${integer.format(state.totalLoaded)} of ${integer.format(state.total)} avatars`;
				} else {
					els.status.textContent = `Showing ${integer.format(state.totalLoaded)} avatars`;
				}
			}

			function renderTags() {
				const tags = [...state.loadedTags].sort((a, b) => a.localeCompare(b)).slice(0, 24);
				if (!tags.length && !state.tag) {
					els.tags.innerHTML = '';
					return;
				}
				const all = state.tag && !tags.includes(state.tag) ? [state.tag, ...tags] : tags;
				els.tags.innerHTML = all
					.map(
						(t) =>
							`<button type="button" class="tag-chip${
								t === state.tag ? ' is-active' : ''
							}" data-tag="${esc(t)}">${esc(t)}</button>`,
					)
					.join('');
			}

			els.tags.addEventListener('click', (e) => {
				const btn = e.target.closest('[data-tag]');
				if (!btn) return;
				const tag = btn.dataset.tag === state.tag ? '' : btn.dataset.tag;
				state.tag = tag;
				renderTags();
				resetAndLoad();
			});

			function renderCard(a) {
				const card = document.createElement('button');
				card.type = 'button';
				card.className = 'card';
				card.dataset.id = a.id;

				const thumb = a.thumbnail_url
					? `<img src="${esc(a.thumbnail_url)}" alt="${esc(a.name || 'Avatar')}" loading="lazy" decoding="async" />`
					: `<div class="thumb-ph" aria-hidden="true">▢</div>`;

				const tagChips = (a.tags || [])
					.slice(0, 3)
					.map((t) => `<span class="card-tag">${esc(t)}</span>`)
					.join('');

				card.innerHTML = `
					<div class="thumb">${thumb}</div>
					<div class="card-body">
						<h3 class="card-name">${esc(a.name || 'Untitled')}</h3>
						${tagChips ? `<div class="card-tags">${tagChips}</div>` : ''}
						<div class="card-meta">
							<span>${a.thumbnail_url ? '' : 'no thumb'}</span>
						</div>
					</div>
				`;

				card.addEventListener('click', () => selectAvatar(a));
				return card;
			}

			function selectAvatar(a) {
				state.selected = a;
				for (const node of els.grid.querySelectorAll('.card.is-selected')) {
					node.classList.remove('is-selected');
				}
				const node = els.grid.querySelector(`.card[data-id="${CSS.escape(a.id)}"]`);
				if (node) node.classList.add('is-selected');

				els.previewName.textContent = a.name || 'Untitled';
				const tagBits = (a.tags || []).join(' · ') || 'three.ws public catalog';
				els.previewSub.textContent = tagBits;

				// Render model-viewer fresh so each pick gets a clean load.
				els.previewStage.innerHTML = '';
				if (a.model_url) {
					const mv = document.createElement('model-viewer');
					mv.setAttribute('src', a.model_url);
					mv.setAttribute('alt', a.name || 'three.ws avatar');
					mv.setAttribute('camera-controls', '');
					mv.setAttribute('auto-rotate', '');
					mv.setAttribute('rotation-per-second', '14deg');
					mv.setAttribute('interaction-prompt', 'none');
					mv.setAttribute('exposure', '1.05');
					mv.setAttribute('shadow-intensity', '0.9');
					mv.setAttribute('environment-image', 'neutral');
					if (a.thumbnail_url) mv.setAttribute('poster', a.thumbnail_url);
					els.previewStage.appendChild(mv);
				} else {
					const div = document.createElement('div');
					div.className = 'preview-empty';
					div.textContent = 'No GLB available for this avatar.';
					els.previewStage.appendChild(div);
				}

				els.cta.disabled = false;
				updatePayloadPreview();
				syncUrl();
			}

			function buildPayload() {
				const a = state.selected;
				if (!a) return '';
				if (state.mode === 'share') {
					return `https://three.ws/avatars/${a.id}`;
				}
				if (state.mode === 'glb') {
					return a.model_url || '';
				}
				return [
					`<script type="module" src="https://three.ws/avatar-sdk/dist/index.mjs"><\/script>`,
					`<agent-3d avatar-id="${a.id}" style="width:100%;height:480px"></agent-3d>`,
				].join('\n');
			}

			function updatePayloadPreview() {
				if (!state.selected) {
					els.previewFoot.textContent = 'Selected payload appears here.';
					els.cta.textContent = 'Use this avatar';
					return;
				}
				const payload = buildPayload();
				els.previewFoot.textContent = payload || '(no value for this mode)';
				const labels = {
					share: 'Copy share link',
					glb: 'Copy GLB URL',
					embed: 'Copy embed snippet',
				};
				els.cta.textContent = labels[state.mode];
			}

			for (const input of els.modeInputs) {
				input.addEventListener('change', () => {
					if (!input.checked) return;
					state.mode = input.value;
					updatePayloadPreview();
				});
			}

			let copyResetTimer = null;
			els.cta.addEventListener('click', async () => {
				if (!state.selected) return;
				const payload = buildPayload();
				if (!payload) return;
				try {
					await navigator.clipboard.writeText(payload);
					const prev = els.cta.textContent;
					els.cta.classList.add('copied');
					els.cta.textContent = 'Copied ✓';
					if (copyResetTimer) clearTimeout(copyResetTimer);
					copyResetTimer = setTimeout(() => {
						els.cta.classList.remove('copied');
						updatePayloadPreview();
						copyResetTimer = null;
					}, 1400);
				} catch (err) {
					els.previewFoot.textContent = `Clipboard blocked: ${err.message}\n\n${payload}`;
				}
			});

			let searchDebounce;
			els.search.addEventListener('input', () => {
				els.searchClear.hidden = !els.search.value;
				clearTimeout(searchDebounce);
				searchDebounce = setTimeout(() => {
					state.query = els.search.value.trim();
					resetAndLoad();
				}, 250);
			});

			els.searchClear.addEventListener('click', () => {
				els.search.value = '';
				els.searchClear.hidden = true;
				state.query = '';
				resetAndLoad();
				els.search.focus();
			});

			els.loadMore.addEventListener('click', () => loadPage());

			let io;
			if ('IntersectionObserver' in window) {
				io = new IntersectionObserver(
					(entries) => {
						for (const entry of entries) {
							if (entry.isIntersecting && state.cursor && !state.loading) loadPage();
						}
					},
					{ rootMargin: '480px 0px' },
				);
				io.observe(els.sentinel);
			}

			function resetAndLoad() {
				syncUrl();
				state.cursor = null;
				state.totalLoaded = 0;
				state.total = null;
				state.loadedTags = new Set();
				state.firstPageDone = false;
				els.grid.innerHTML = '';
				els.loadMore.hidden = true;
				loadPage();
			}

			async function loadPage() {
				if (state.loading) return;
				state.loading = true;
				updateStatus();

				const params = new URLSearchParams();
				if (state.query) params.set('q', state.query);
				if (state.tag) params.set('tag', state.tag);
				if (state.cursor) params.set('cursor', state.cursor);
				params.set('limit', '24');
				if (!state.firstPageDone) params.set('totals', '1');

				try {
					const res = await fetch(`/api/avatars/public?${params.toString()}`);
					if (!res.ok) throw new Error(`HTTP ${res.status}`);
					const data = await res.json();
					const avatars = Array.isArray(data.avatars) ? data.avatars : [];

					for (const a of avatars) {
						state.cardsById.set(a.id, a);
						els.grid.appendChild(renderCard(a));
						state.totalLoaded += 1;
						for (const t of a.tags || []) state.loadedTags.add(t);
					}

					if (!state.firstPageDone) {
						if (typeof data.total === 'number') state.total = data.total;
						state.firstPageDone = true;
					}

					state.cursor = data.next_cursor || null;
					els.loadMore.hidden = !state.cursor;

					if (els.grid.children.length === 0) {
						const empty = document.createElement('div');
						empty.className = 'empty';
						empty.textContent =
							state.query || state.tag
								? 'No public avatars match these filters.'
								: 'No public avatars yet.';
						els.grid.appendChild(empty);
					}

					renderTags();
				} catch (err) {
					const errDiv = document.createElement('div');
					errDiv.className = 'error';
					errDiv.textContent = `Failed to load avatars: ${err.message}`;
					if (state.totalLoaded === 0) els.grid.appendChild(errDiv);
				} finally {
					state.loading = false;
					updateStatus();
					// If the URL named a specific avatar that just appeared in this
					// page, auto-select it. Otherwise hydrate it via a direct fetch
					// (the avatar may be outside the current filter/page window).
					await hydrateDeepLink();
				}
			}

			// ── URL state sync ───────────────────────────────────────────────
			// `?q=` and `?tag=` round-trip filter state; `?id=` round-trips the
			// selected avatar. History is updated with replaceState so the back
			// button still leaves the page rather than walking selection history.
			function readUrl() {
				const u = new URL(location.href);
				return {
					q: u.searchParams.get('q') || '',
					tag: u.searchParams.get('tag') || '',
					id: u.searchParams.get('id') || '',
				};
			}

			function syncUrl() {
				const u = new URL(location.href);
				const set = (k, v) => (v ? u.searchParams.set(k, v) : u.searchParams.delete(k));
				set('q', state.query);
				set('tag', state.tag);
				if (state.selected?.id) {
					u.searchParams.set('id', state.selected.id);
				}
				// Leave any existing `?id=` alone when nothing is selected so the
				// initial resetAndLoad() before hydrateDeepLink() does not clobber
				// the deep-link the user navigated in with.
				history.replaceState(null, '', u);
			}

			let deepLinkConsumed = false;
			async function hydrateDeepLink() {
				if (deepLinkConsumed) return;
				const { id } = readUrl();
				if (!id) {
					deepLinkConsumed = true;
					return;
				}
				const inPage = state.cardsById.get(id);
				if (inPage) {
					deepLinkConsumed = true;
					selectAvatar(inPage);
					return;
				}
				// Avatar not in the current page — fetch it directly so deep links
				// to off-screen avatars still resolve on first load.
				try {
					const res = await fetch(`/api/avatars/${encodeURIComponent(id)}`);
					if (!res.ok) {
						deepLinkConsumed = true;
						return;
					}
					const data = await res.json();
					if (data?.avatar) {
						deepLinkConsumed = true;
						state.cardsById.set(data.avatar.id, data.avatar);
						selectAvatar(data.avatar);
					}
				} catch {
					deepLinkConsumed = true;
				}
			}

			// Hydrate filter state from URL before the first fetch so the
			// initial page already reflects any ?q= / ?tag= the user pasted.
			const initial = readUrl();
			if (initial.q) {
				state.query = initial.q;
				els.search.value = initial.q;
				els.searchClear.hidden = false;
			}
			if (initial.tag) state.tag = initial.tag;

			resetAndLoad();
		</script>
		<script src="/brand.js" defer></script>
		<div id="footer-container"></div>
		<script src="/footer.js"></script>
	<!-- Runtime i18n: detects locale, swaps annotated copy, mounts <lang-switcher>. -->
	<script type="module" src="/i18n.js"></script>
</body>
</html>