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.

fix(objects): rework the CC0 object gallery cards and their preview and embed paths

nirholas0ae255060502
diff --git a/src/objects-library.js b/src/objects-library.js
index b4be1b443..cf69e5b2e 100644
--- a/src/objects-library.js
+++ b/src/objects-library.js
@@ -1,13 +1,21 @@
-// /objects — the CC0 3D object/prop library gallery.
+// /objects: the CC0 3D object/prop library gallery.
//
// Browses the free, commercial-OK props served by GET /api/objects/library
-// (manifest on R2, mirroring /character-library). Each card renders a live
-// <model-viewer> off the GLB's CDN url and offers:
-// Preview → /app#model=<glb>&kind=object (viewer in object mode: no agent chat)
-// Download → the GLB directly (every object is CC0 — free to reuse)
+// (manifest on R2, mirroring /character-library). Each card offers:
+// Preview -> /app#model=<glb>&kind=object (viewer in object mode: no agent chat)
+// AR -> /ar/studio?src=<glb> (place the prop in your room)
+// Download -> the GLB directly (every object is CC0)
//
-// The library is a few hundred entries, so the whole manifest is fetched once
-// and filtered/sorted client-side — instant search, no pagination round-trips.
+// The library is ~500 entries and the whole manifest is fetched once, so search,
+// sort and category filtering are instant and never cost a round trip. What the
+// manifest CANNOT do is render all of it at once: the props average 2 MB of GLB
+// apiece, and the first version of this page mounted one <model-viewer> per
+// entry, which fired 511 poster requests, built 511 web components before the
+// first card was interactive (18 s to first paint, 3 fps while scrolling), and
+// would have pulled gigabytes if a visitor scrolled. So the grid renders in
+// chunks and a card is a cheap, lazily-fetched still until you point at it:
+// pointing (or tab-focusing) upgrades that one card to a live, auto-rotating
+// <model-viewer>, and at most MAX_LIVE_VIEWERS stay mounted at a time.
const els = {
grid: document.querySelector('[data-role="grid"]'),
@@ -23,9 +31,24 @@ const els = {
clearSearch: document.querySelector('[data-role="clear-search"]'),
retry: document.querySelector('[data-role="retry"]'),
catChips: document.querySelector('[data-role="chips"]'),
+ more: document.querySelector('[data-role="more"]'),
+ loadMore: document.querySelector('[data-role="load-more"]'),
};
-const state = { all: [], query: '', sort: 'az', category: '' };
+const PAGE_SIZE = 48;
+const MAX_LIVE_VIEWERS = 6;
+
+const state = { all: [], query: '', sort: 'az', category: '', view: [], rendered: 0 };
+
+// Poly Haven tags each asset with the scene collection it shipped in
+// ("collection: the_shed"). Those are provenance, not something a visitor
+// browses by, and eight of them padded the filter row with names that mean
+// nothing outside Poly Haven's own site. They stay on the object (and stay
+// searchable through its tags); they just do not earn a chip.
+const isBrowsableCategory = (c) => Boolean(c) && !/^collection\s*:/i.test(c);
+
+const prefersReducedMotion =
+ typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
function escapeHtml(s) {
return String(s ?? '').replace(/[&<>"']/g, (c) =>
@@ -39,57 +62,104 @@ function formatBytes(n) {
return mb >= 1 ? `${mb.toFixed(1)} MB` : `${Math.round(n / 1024)} KB`;
}
+// --- live-viewer budget ------------------------------------------------------
+// Mounted <model-viewer>s, oldest first. Each one holds a WebGL context and a
+// decoded GLB, so the list is capped and the oldest is torn back down to its
+// still image when a seventh card is pointed at.
+const liveThumbs = [];
+
+function retireThumb(thumb) {
+ if (!thumb || thumb.dataset.live !== '1') return;
+ thumb.querySelector('model-viewer')?.remove();
+ thumb.classList.remove('is-live');
+ delete thumb.dataset.live;
+}
+
+function upgradeThumb(thumb) {
+ if (!thumb || thumb.dataset.live === '1') return;
+ const src = thumb.dataset.glb;
+ if (!src) return;
+ thumb.dataset.live = '1';
+ const mv = document.createElement('model-viewer');
+ mv.className = 'ch-card-mv';
+ mv.setAttribute('src', src);
+ mv.setAttribute('alt', thumb.dataset.label || 'Object');
+ mv.setAttribute('reveal', 'auto');
+ mv.setAttribute('disable-zoom', '');
+ mv.setAttribute('disable-pan', '');
+ mv.setAttribute('disable-tap', '');
+ mv.setAttribute('interaction-prompt', 'none');
+ mv.setAttribute('environment-image', 'neutral');
+ mv.setAttribute('shadow-intensity', '0.4');
+ mv.setAttribute('exposure', '1');
+ if (!prefersReducedMotion) {
+ mv.setAttribute('auto-rotate', '');
+ mv.setAttribute('rotation-per-second', '20deg');
+ }
+ mv.addEventListener('load', () => thumb.classList.add('is-live'), { once: true });
+ thumb.prepend(mv);
+ liveThumbs.push(thumb);
+ while (liveThumbs.length > MAX_LIVE_VIEWERS) retireThumb(liveThumbs.shift());
+}
+
+// --- cards -------------------------------------------------------------------
+// A card with no rendered poster has nothing to look at until it goes live, so
+// it upgrades on intersection instead of waiting for a pointer that a touch
+// visitor never sends. It still spends from the same live-viewer budget.
+const blankObserver = 'IntersectionObserver' in window
+ ? new IntersectionObserver((entries, obs) => {
+ for (const e of entries) {
+ if (!e.isIntersecting) continue;
+ obs.unobserve(e.target);
+ upgradeThumb(e.target);
+ }
+ }, { rootMargin: '200px' })
+ : null;
+
function renderCard(o) {
const glbUrl = o.url || '';
- const thumb = o.thumb || '';
const alt = o.label || o.name || 'Object';
- // A prop is not an embodied agent — open the viewer in object mode so it
+ // A prop is not an embodied agent, so the viewer opens in object mode: it
// shows view + modify affordances (Restyle / AR / Download) instead of the
- // "Ask the agent / Why are you embodied?" chat dock. The overlay reads
- // `kind=object` and `title` from the hash.
- const previewUrl = glbUrl
- ? `/app#model=${encodeURIComponent(glbUrl)}&kind=object&title=${encodeURIComponent(alt)}`
- : '#';
- const arUrl = glbUrl
- ? `/ar/studio?src=${encodeURIComponent(glbUrl)}&title=${encodeURIComponent(alt)}`
- : '#';
- const cat = (o.categories && o.categories[0]) || '';
+ // "Ask the agent" chat dock. The overlay reads `kind=object` and `title`
+ // from the hash.
+ const previewUrl = `/app#model=${encodeURIComponent(glbUrl)}&kind=object&title=${encodeURIComponent(alt)}`;
+ const arUrl = `/ar/studio?src=${encodeURIComponent(glbUrl)}&title=${encodeURIComponent(alt)}`;
+ const cat = (o.categories || []).find(isBrowsableCategory) || '';
+ const size = formatBytes(o.bytes);
const card = document.createElement('article');
card.className = 'ch-card';
card.innerHTML = `
- <a class="ch-card-thumb" href="${escapeAttr(previewUrl)}" aria-label="Preview ${escapeAttr(alt)} in 3D">
- <model-viewer
- src="${escapeAttr(glbUrl)}"
- alt="${escapeAttr(alt)}"
- class="ch-card-mv"
- reveal="auto" loading="lazy" disable-zoom disable-pan disable-tap
- interaction-prompt="none" camera-controls="false"
- auto-rotate rotation-per-second="20deg"
- environment-image="neutral" shadow-intensity="0.4" exposure="1"
- ${thumb ? `poster="${escapeAttr(thumb)}"` : ''}
- ></model-viewer>
+ <a class="ch-card-thumb" href="${escapeAttr(previewUrl)}" aria-label="Preview ${escapeAttr(alt)} in 3D"
+ data-glb="${escapeAttr(glbUrl)}" data-label="${escapeAttr(alt)}">
+ ${o.thumb
+ ? `<img class="ch-card-img" src="${escapeAttr(o.thumb)}" alt="" loading="lazy" decoding="async" />`
+ : '<span class="ch-card-img ch-card-img--blank" aria-hidden="true"></span>'}
<span class="ch-card-pill">CC0</span>
- <span class="ch-card-play" aria-hidden="true">▶</span>
+ <span class="ch-card-play" aria-hidden="true">&#9654;</span>
</a>
<div class="ch-card-body">
- <h3 class="ch-card-name">${escapeHtml(o.label || o.name)}</h3>
- <div class="ch-card-meta">
- <span>${escapeHtml(cat)}${cat ? ' · ' : ''}${escapeHtml(formatBytes(o.bytes))}</span>
- </div>
+ <h3 class="ch-card-name">${escapeHtml(alt)}</h3>
+ <div class="ch-card-meta">${escapeHtml(cat)}${cat && size ? ' &middot; ' : ''}${escapeHtml(size)}</div>
<div class="ch-card-actions">
- <a class="ch-btn ch-btn--primary" href="${escapeAttr(previewUrl)}" title="Open in the 3D viewer">Preview</a>
- <a class="ch-btn ch-btn--ghost" href="${escapeAttr(arUrl)}" title="Place this prop in your room with AR Studio">AR</a>
- <a class="ch-btn ch-btn--ghost" href="${escapeAttr(glbUrl)}" download title="Download the GLB (CC0, free to reuse)">Download</a>
+ <a class="ch-btn ch-btn--primary" href="${escapeAttr(previewUrl)}" title="Open ${escapeAttr(alt)} in the 3D viewer">Preview</a>
+ <a class="ch-btn ch-btn--ghost" href="${escapeAttr(arUrl)}" title="Place ${escapeAttr(alt)} in your room with AR Studio">AR</a>
+ <a class="ch-btn ch-btn--ghost" href="${escapeAttr(glbUrl)}" download title="Download the ${escapeAttr(alt)} GLB (CC0, free to reuse)">Download</a>
</div>
</div>
`;
+ // A card with no rendered poster has nothing to look at until it goes live,
+ // so it upgrades as soon as it scrolls into view rather than waiting for a
+ // pointer. It still counts against the same budget.
+ if (!o.thumb) blankObserver?.observe(card.querySelector('.ch-card-thumb'));
return card;
}
+// --- filtering, sorting, chunked rendering -----------------------------------
function categories() {
const set = new Set();
- for (const o of state.all) for (const c of o.categories || []) set.add(c);
+ for (const o of state.all) for (const c of o.categories || []) if (isBrowsableCategory(c)) set.add(c);
return [...set].sort();
}
@@ -98,60 +168,84 @@ function renderChips() {
const cats = categories();
if (!cats.length) return;
els.catChips.innerHTML =
- `<button class="ch-chip${state.category === '' ? ' is-active' : ''}" data-cat="">All</button>` +
- cats.map((c) => `<button class="ch-chip${state.category === c ? ' is-active' : ''}" data-cat="${escapeAttr(c)}">${escapeHtml(c)}</button>`).join('');
- els.catChips.querySelectorAll('.ch-chip').forEach((b) => b.addEventListener('click', () => {
- state.category = b.dataset.cat;
- renderChips();
- applyView();
- }));
+ `<button type="button" class="ch-chip${state.category === '' ? ' is-active' : ''}" data-cat="" aria-pressed="${state.category === ''}">All</button>` +
+ cats.map((c) => `<button type="button" class="ch-chip${state.category === c ? ' is-active' : ''}" data-cat="${escapeAttr(c)}" aria-pressed="${state.category === c}">${escapeHtml(c)}</button>`).join('');
}
-function applyView() {
+function filtered() {
const q = state.query.trim().toLowerCase();
let list = state.all;
if (state.category) list = list.filter((o) => (o.categories || []).includes(state.category));
- if (q) list = list.filter((o) => (o.label || o.name || '').toLowerCase().includes(q) ||
- (o.tags || []).some((t) => t.toLowerCase().includes(q)));
-
- list = list.slice().sort((a, b) => {
+ if (q) {
+ list = list.filter((o) => (o.label || o.name || '').toLowerCase().includes(q) ||
+ (o.tags || []).some((t) => String(t).toLowerCase().includes(q)) ||
+ (o.categories || []).some((c) => String(c).toLowerCase().includes(q)));
+ }
+ return list.slice().sort((a, b) => {
const la = (a.label || a.name || '').toLowerCase(), lb = (b.label || b.name || '').toLowerCase();
if (state.sort === 'za') return lb.localeCompare(la);
if (state.sort === 'largest') return (b.bytes || 0) - (a.bytes || 0);
if (state.sort === 'smallest') return (a.bytes || 0) - (b.bytes || 0);
return la.localeCompare(lb);
});
+}
+
+// The button's own label is static markup so the i18n pass owns it; the running
+// tally lives in the aria-live count above the grid, which announces "48 of 511
+// shown" after every chunk without either surface clobbering the other.
+function syncMore() {
+ show(els.more, state.rendered < state.view.length);
+}
+
+function renderChunk() {
+ const next = state.view.slice(state.rendered, state.rendered + PAGE_SIZE);
+ if (!next.length) { syncMore(); return; }
+ const frag = document.createDocumentFragment();
+ for (const o of next) frag.appendChild(renderCard(o));
+ els.grid.appendChild(frag);
+ state.rendered += next.length;
+ if (els.count) els.count.textContent = `${state.rendered} of ${state.view.length} shown`;
+ syncMore();
+}
+
+function applyView() {
+ state.view = filtered();
+ state.rendered = 0;
+ liveThumbs.length = 0;
+ els.grid.replaceChildren();
if (state.all.length === 0) {
- show(els.loading, false); show(els.grid, false); show(els.emptySearch, false); show(els.error, false); show(els.empty, true);
+ show(els.loading, false); show(els.grid, false); show(els.emptySearch, false);
+ show(els.error, false); show(els.more, false); show(els.empty, true);
if (els.count) els.count.textContent = '';
return;
}
- if (list.length === 0) {
- show(els.loading, false); show(els.grid, false); show(els.empty, false); show(els.error, false); show(els.emptySearch, true);
+ if (state.view.length === 0) {
+ show(els.loading, false); show(els.grid, false); show(els.empty, false);
+ show(els.error, false); show(els.more, false); show(els.emptySearch, true);
if (els.count) els.count.textContent = '0 objects';
return;
}
- const frag = document.createDocumentFragment();
- for (const o of list) frag.appendChild(renderCard(o));
- els.grid.replaceChildren(frag);
- show(els.loading, false); show(els.empty, false); show(els.emptySearch, false); show(els.error, false); show(els.grid, true);
- if (els.count) els.count.textContent = `${list.length} of ${state.all.length}`;
+ show(els.loading, false); show(els.empty, false); show(els.emptySearch, false);
+ show(els.error, false); show(els.grid, true);
+ renderChunk();
}
async function load() {
- show(els.loading, true); show(els.grid, false); show(els.empty, false); show(els.emptySearch, false); show(els.error, false);
+ show(els.loading, true); show(els.grid, false); show(els.empty, false);
+ show(els.emptySearch, false); show(els.error, false); show(els.more, false);
try {
const res = await fetch('/api/objects/library');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
state.all = Array.isArray(data.objects) ? data.objects : [];
- if (els.heroStats) els.heroStats.textContent = state.all.length ? `${state.all.length} CC0 props · free to use` : '';
+ if (els.heroStats) els.heroStats.textContent = state.all.length ? `${state.all.length} CC0 props, free to use` : '';
renderChips();
applyView();
} catch (err) {
- show(els.loading, false); show(els.grid, false); show(els.empty, false); show(els.emptySearch, false); show(els.error, true);
- if (els.errorMsg) els.errorMsg.textContent = `Failed to load objects: ${err?.message || 'network error'}`;
+ show(els.loading, false); show(els.grid, false); show(els.empty, false);
+ show(els.emptySearch, false); show(els.more, false); show(els.error, true);
+ if (els.errorMsg) els.errorMsg.textContent = `Failed to load objects: ${err?.message || 'network error'}. Check your connection and try again.`;
}
}
@@ -162,8 +256,42 @@ function wire() {
t = setTimeout(() => { state.query = v; applyView(); }, 120);
});
els.sort?.addEventListener('change', (e) => { state.sort = e.target.value; applyView(); });
- els.clearSearch?.addEventListener('click', () => { state.query = ''; state.category = ''; if (els.search) els.search.value = ''; renderChips(); applyView(); });
+ els.clearSearch?.addEventListener('click', () => {
+ state.query = ''; state.category = '';
+ if (els.search) els.search.value = '';
+ renderChips(); applyView();
+ els.search?.focus();
+ });
els.retry?.addEventListener('click', load);
+ els.loadMore?.addEventListener('click', renderChunk);
+
+ els.catChips?.addEventListener('click', (e) => {
+ const chip = e.target.closest('.ch-chip');
+ if (!chip) return;
+ state.category = chip.dataset.cat;
+ renderChips();
+ applyView();
+ });
+
+ // Delegated so a chunk appended later needs no extra wiring. `pointerover`
+ // bubbles where `pointerenter` does not, and `focusin` gives a keyboard user
+ // the same live preview a mouse user gets on hover.
+ const upgradeFromEvent = (e) => {
+ const thumb = e.target.closest?.('.ch-card-thumb');
+ if (thumb) upgradeThumb(thumb);
+ };
+ els.grid?.addEventListener('pointerover', upgradeFromEvent);
+ els.grid?.addEventListener('focusin', upgradeFromEvent);
+
+ // Infinite scroll on top of the real button: the sentinel loads the next
+ // chunk as it nears the viewport, and the button stays the accessible,
+ // observer-free way to do the same thing.
+ if (els.more && 'IntersectionObserver' in window) {
+ new IntersectionObserver((entries) => {
+ if (entries.some((e) => e.isIntersecting) && !els.more.hidden) renderChunk();
+ }, { rootMargin: '600px' }).observe(els.more);
+ }
+
document.addEventListener('keydown', (e) => {
if (e.key === '/' && document.activeElement !== els.search) { e.preventDefault(); els.search?.focus(); }
});