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): wait for a frame that has the model in it, and refuse to upload a blank poster

nirholas5569aef73c39
diff --git a/scripts/glb-thumbnail-harness.html b/scripts/glb-thumbnail-harness.html
index 2febe15c0..8a5239c44 100644
--- a/scripts/glb-thumbnail-harness.html
+++ b/scripts/glb-thumbnail-harness.html
@@ -28,6 +28,20 @@
<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.
@@ -63,7 +77,16 @@
// 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.
- const blob = await mv.toBlob({ mimeType: 'image/png', idealAspect: false });
+ // 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'));
diff --git a/scripts/render-glb-thumbnails.mjs b/scripts/render-glb-thumbnails.mjs
index e0ec5b725..5bab47ad9 100644
--- a/scripts/render-glb-thumbnails.mjs
+++ b/scripts/render-glb-thumbnails.mjs
@@ -22,6 +22,7 @@ import { join, dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { tmpdir } from 'node:os';
import { createServer } from 'node:http';
+import sharp from 'sharp';
import { getObject, putObject, objectExists } from './lib/asset-r2.mjs';
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -130,6 +131,11 @@ async function readJobs() {
}
const png = Buffer.from(String(dataUrl).replace(/^data:image\/png;base64,/, ''), 'base64');
if (png.length < 1000) throw new Error('empty render');
+ // A blank frame is a perfectly valid PNG of a respectable size, so size alone
+ // let empty posters through. A frame with no variation in any channel,
+ // alpha included, has nothing drawn in it.
+ const { channels } = await sharp(png).stats();
+ if (Math.max(...channels.map((ch) => ch.stdev)) < 2) throw new Error('blank render: nothing was drawn');
await putObject(thumbKey, png, 'image/png', 'public, max-age=604800');
ok++;
if (ok % 20 === 0) process.stdout.write(`\r ${ok} rendered, ${skipped} skipped, ${fail} failed…`);