Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

3D previews: no seam in a letterboxed tile, and the look for older results pages through

Adarshf12d716fa78c
diff --git a/src/components/model3d/model-tile.tsx b/src/components/model3d/model-tile.tsx
index 49931a56..23413158 100644
--- a/src/components/model3d/model-tile.tsx
+++ b/src/components/model3d/model-tile.tsx
@@ -23,9 +23,31 @@ function turnsAllowed(): boolean {
return !conn?.saveData;
}
-/** The studio colour behind a preview's backdrop (its edges), so a letterboxed tile has no seam. */
+/** The studio colour at a preview's corners: what a tile shows before its still arrives. */
const STUDIO_FILL = "bg-[#dad8d4] dark:bg-[#141417]";
+/**
+ * The preview's studio sweep, continued past a square still in a wider or taller tile (a letterboxed one), so the still
+ * sits in it with no seam: the sweep scripts/model3d/preview3d-core.mjs draws (backdrop()), centred on the still, sized
+ * by the tile's shorter side (container units).
+ */
+const SWEEP_STOPS = [0, 0.25, 0.5, 0.75, 1].map((f) => [f, f ** 1.5] as const);
+const sweep = (c0: number[], c1: number[]) => {
+ const stops = SWEEP_STOPS.map(([f, t]) => `rgb(${c0.map((v, k) => Math.round(v + (c1[k] - v) * t)).join(" ")}) ${f * 100}%`).join(", ");
+ // The still's sweep is centred at 42 % of its height, 0.78 of its side wide and 0.78 / 1.15 high.
+ return `radial-gradient(78cqmin 67.8cqmin at 50% calc(50% - 8cqmin), ${stops})`;
+};
+const SWEEP = { light: sweep([246, 245, 243], [218, 216, 212]), dark: sweep([44, 44, 48], [20, 20, 23]) };
+
+function StudioSweep() {
+ return (
+ <>
+ <div aria-hidden className="absolute inset-0 dark:hidden" style={{ backgroundImage: SWEEP.light }} />
+ <div aria-hidden className="absolute inset-0 hidden dark:block" style={{ backgroundImage: SWEEP.dark }} />
+ </>
+ );
+}
+
/**
* A 3D result as a tile: its preview's still (the light or the dark studio, with the theme), and its turntable playing
* muted and looped over it while a mouse rests on the tile or the keys focus it (on a touch screen: while the tile is
@@ -74,7 +96,8 @@ export function ModelTile({ preview, fit, autoPlay, className }: { preview: Read
}, [autoPlay]);
const { mounted, leaving } = usePresence(on);
return (
- <div ref={root} className={cn("relative size-full overflow-hidden", STUDIO_FILL, className)} data-model-tile="">
+ <div ref={root} className={cn("relative size-full overflow-hidden [container-type:size]", STUDIO_FILL, className)} data-model-tile="">
+ {fit && <StudioSweep />}
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={preview.poster.light} alt="" loading="lazy" decoding="async" className={cn("absolute inset-0 size-full dark:hidden", object)} />
{/* eslint-disable-next-line @next/next/no-img-element */}
diff --git a/src/server/model3d-preview.ts b/src/server/model3d-preview.ts
index 827ef65e..117914fc 100644
--- a/src/server/model3d-preview.ts
+++ b/src/server/model3d-preview.ts
@@ -52,10 +52,18 @@ type Row = {
completed_at: number | null;
};
+/** How many results one look reads, and how long it rests when nothing is left to make or follow. */
+const PAGE = 40;
+const IDLE_MS = 60_000;
+
declare global {
- var __fuobePreview3d: { last: number; here: Map<string, AbortController> } | undefined;
+ var __fuobePreview3d: { last: number; cursor: number; here: Map<string, AbortController> } | undefined;
}
-const state = () => (globalThis.__fuobePreview3d ??= { last: 0, here: new Map() });
+const state = () => {
+ const s = (globalThis.__fuobePreview3d ??= { last: 0, cursor: Infinity, here: new Map() });
+ s.cursor ??= Infinity;
+ return s;
+};
const turntableOf = (o: OutputFile) => (o.meta?.finish as { turntable?: unknown } | undefined)?.turntable;
@@ -171,24 +179,13 @@ function drawsHere(r: Pick<Row, "provider">, account: string | undefined): boole
const accountOf = (r: Pick<Row, "request_json">) => json<GenerationRequest | null>(r.request_json, null)?.account;
/** Start one output's preview: queued for a worker, drawn here, or (neither now) left for a later look. */
-function start(r: Row, i: number, outputs: OutputFile[], tries: number): void {
+/** Start one output's preview. False: nothing could start it now (no worker draws previews yet, and this server doesn't). */
+function start(r: Row, i: number, outputs: OutputFile[], tries: number): boolean {
const o = outputs[i];
const asset = getAsset(o.assetId!);
const s = previewSettings(r, o);
- if (!asset || !s)
- return void setPreview(r.id, o.assetId!, {
- state: "failed",
- at: Date.now(),
- tries,
- why: "no file",
- });
- if (asset.size > PREVIEW_MAX_BYTES)
- return void setPreview(r.id, o.assetId!, {
- state: "failed",
- at: Date.now(),
- tries,
- why: "too big",
- });
+ if (!asset || !s) return setPreview(r.id, o.assetId!, { state: "failed", at: Date.now(), tries, why: "no file" }), true;
+ if (asset.size > PREVIEW_MAX_BYTES) return setPreview(r.id, o.assetId!, { state: "failed", at: Date.now(), tries, why: "too big" }), true;
const account = accountOf(r);
if (workersFinish("preview3d")) {
// A run finished in the last hour keeps its place in line; an older result's preview goes after everyone's.
@@ -234,9 +231,9 @@ function start(r: Row, i: number, outputs: OutputFile[], tries: number): void {
why: "not queued",
});
}
- return;
+ return true;
}
- if (!drawsHere(r, account)) return;
+ if (!drawsHere(r, account)) return false;
const ctl = new AbortController();
state().here.set(`${r.id}:${o.assetId}`, ctl);
setPreview(r.id, o.assetId!, {
@@ -277,6 +274,7 @@ function start(r: Row, i: number, outputs: OutputFile[], tries: number): void {
}
})
.finally(() => state().here.delete(`${r.id}:${o.assetId}`));
+ return true;
}
/** Look at a preview on its way: a worker's job that ended (its files kept, or failed), or a draw here that stopped. */
@@ -367,16 +365,20 @@ export function tickPreviews(now = Date.now()): void {
}
}
if (inFlight >= IN_FLIGHT) return;
- // Results with a model and no preview yet (or one to try again), newest first; a run in the Trash waits.
+ // Results with a model and no preview yet (or one to try again), newest first, a page at a time from where the last
+ // look stopped (results that get none, such as a model with its own turntable, never hold up older ones); a run in the
+ // Trash waits.
const rows = db()
.prepare(
- `SELECT g.id, g.status, g.provider, g.model_id, g.op, g.request_json, g.outputs_json, g.completed_at FROM generations g
+ `SELECT g.id, g.status, g.provider, g.model_id, g.op, g.request_json, g.outputs_json, g.completed_at, COALESCE(g.completed_at, g.created_at) AS at FROM generations g
LEFT JOIN generation_trash t ON t.generation_id = g.id
WHERE g.status = 'succeeded' AND t.generation_id IS NULL AND g.outputs_json LIKE '%"kind":"model3d"%'
AND (g.outputs_json NOT LIKE '%"preview3d"%' OR g.outputs_json LIKE '%"preview3d":{"state":"failed"%')
- ORDER BY COALESCE(g.completed_at, g.created_at) DESC LIMIT 40`,
+ AND COALESCE(g.completed_at, g.created_at) < @cursor
+ ORDER BY at DESC LIMIT ${PAGE}`,
)
- .all() as Row[];
+ .all({ cursor: s.cursor }) as (Row & { at: number })[];
+ const before = inFlight;
for (const r of rows) {
if (inFlight >= IN_FLIGHT) break;
const outputs = json<OutputFile[]>(r.outputs_json, []);
@@ -388,15 +390,20 @@ export function tickPreviews(now = Date.now()): void {
if (p.state !== "failed" || (p.tries ?? 1) >= TRIES || now - p.at < 10 * 60_000 || !RETRY.includes(p.why ?? "")) continue;
const again = outputs.map((x, j) => (j === i ? { ...x, meta: { ...x.meta, preview3d: undefined } } : x));
if (!previewTargets(again).includes(i)) continue;
- start(r, i, outputs, (p.tries ?? 1) + 1);
- inFlight++;
+ if (start(r, i, outputs, (p.tries ?? 1) + 1)) inFlight++;
continue;
}
if (!previewTargets(outputs).includes(i)) continue;
- start(r, i, outputs, 1);
- inFlight++;
+ if (start(r, i, outputs, 1)) inFlight++;
}
}
+ // Nothing to start on this page: the next look reads the page after it; past the oldest, from the newest again,
+ // and, with nothing on its way either, after a longer rest.
+ if (inFlight === before) {
+ const last = rows.at(-1);
+ s.cursor = rows.length === PAGE && last ? last.at : Infinity;
+ if (s.cursor === Infinity && inFlight === 0) s.last = now + IDLE_MS - LOOK_MS;
+ } else s.cursor = Infinity;
} catch (e) {
console.warn(`[fuobe] 3D previews: ${(e as Error).message}`);
}