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(forge): cut the fused floor plate from every generated model, tracing the subject's real footprint

nirholas1945ac5fbb89
diff --git a/api/_lib/forge-store.js b/api/_lib/forge-store.js
index 19869046c..aae3d79e1 100644
--- a/api/_lib/forge-store.js
+++ b/api/_lib/forge-store.js
@@ -22,6 +22,7 @@ import { putObject, publicUrl, deleteObject, keyFromPublicUrl, objectStorageConf
import { recordDailyActivity, maybeAwardFirstCreation } from './streaks.js';
import { recordGenerationEvent } from './forge-events.js';
import { scoreGlbQuality } from './glb-quality.js';
+import { stripFloorSlab } from './glb-floor-slab.js';
import { compressGlb, deliveryCompressionOptions } from './glb-compress.js';
import { classifyModelCategory } from './forge-classify.js';
import { cleanupGlb } from './glb-cleanup.js';
@@ -347,6 +348,19 @@ async function scoreAndCompress(buf, { computeQuality, compress, cleanup = false
} catch (err) {
console.warn('[forge-store] geometry cleanup failed, delivering as-is:', err?.message);
}
+ // The reconstructors fuse a floor plate under a resting subject even from a
+ // clean cutout (glb-floor-slab.js). Cut it here, on geometry alone, so the
+ // fix never depends on a vision provider answering, and before scoring so
+ // the quality signal describes the model users actually receive.
+ try {
+ const r = await stripFloorSlab(outBuf);
+ if (r.stripped) {
+ outBuf = r.buffer;
+ cleaned = { ...(cleaned || {}), floor_slab_removed_tris: r.removedTriangles };
+ }
+ } catch (err) {
+ console.warn('[forge-store] floor slab strip failed, delivering as-is:', err?.message);
+ }
}
// Material completion SECOND: it reads the albedo the lane baked and writes
// derived maps onto the geometry cleanup just settled, so it has to run after
diff --git a/api/_lib/glb-floor-slab.js b/api/_lib/glb-floor-slab.js
index 68a63c755..ebae5d535 100644
--- a/api/_lib/glb-floor-slab.js
+++ b/api/_lib/glb-floor-slab.js
@@ -36,8 +36,10 @@ export const SLAB_BAND = 0.05;
const SUBJECT_FLOOR = 0.08;
// The subject's contact footprint: the part of it below this fraction of the
// height, which is where it rests on the plate. The cut keeps the plate only
-// under this, so a teapot keeps a patch under its body, not spout to spout.
-const CONTACT_CEILING = 0.2;
+// under this, so a teapot keeps a sliver under its foot rather than a ring under
+// its flared body, and a tripod keeps a small pad under each foot. Measured on
+// live results: 0.2 left a visible ragged ring round a teapot, 0.08 does not.
+const CONTACT_CEILING = 0.08;
// The band must fill at least this share of its bounding rectangle, seen from
// above, to be a sheet rather than feet, legs or a rounded foot.
export const SLAB_FILL = 0.9;
@@ -45,6 +47,8 @@ export const SLAB_FILL = 0.9;
export const SLAB_FOOTPRINT_RATIO = 1.25;
// Resolution of the top-down coverage grid the fill is measured on.
const FILL_GRID = 48;
+// Resolution of the contact mask the cut follows.
+const CONTACT_GRID = 64;
// Minimum shares of all triangles for the band and for the subject.
const MIN_BAND_SHARE = 0.01;
const MIN_SUBJECT_SHARE = 0.2;
@@ -89,21 +93,32 @@ function area(box) {
return box.maxX > box.minX && box.maxZ > box.minZ ? (box.maxX - box.minX) * (box.maxZ - box.minZ) : 0;
}
-// Share of the band's bounding rectangle its triangles cover, seen from above:
-// each grid cell whose centre falls inside any band triangle counts.
-function coverage(parts, isBand, box) {
+// Rasterize the triangles `pick` selects onto a g x g grid over `box`, seen from
+// above: a cell is covered when its centre falls inside any picked triangle.
+// With `edges`, a triangle's edges mark the cells they cross too, because a
+// vertical wall has no area from above and would otherwise vanish.
+function rasterize(parts, pick, box, g, { edges = false } = {}) {
const w = box.maxX - box.minX;
const d = box.maxZ - box.minZ;
- if (!(w > 0 && d > 0)) return 0;
- const g = FILL_GRID;
+ if (!(w > 0 && d > 0)) return null;
const cells = new Uint8Array(g * g);
const cell = (v, lo, span) => Math.min(g - 1, Math.max(0, Math.floor(((v - lo) / span) * g)));
for (const { positions: p, indices } of parts) {
for (let t = 0; t + 2 < indices.length; t += 3) {
- if (!isBand(p, indices, t)) continue;
+ if (!pick(p, indices, t)) continue;
const ax = p[indices[t] * 3], az = p[indices[t] * 3 + 2];
const bx = p[indices[t + 1] * 3], bz = p[indices[t + 1] * 3 + 2];
const cx = p[indices[t + 2] * 3], cz = p[indices[t + 2] * 3 + 2];
+ if (edges) {
+ for (const [x0e, z0e, x1e, z1e] of [[ax, az, bx, bz], [bx, bz, cx, cz], [cx, cz, ax, az]]) {
+ const steps = Math.max(1, Math.ceil(Math.max(Math.abs(x1e - x0e) / w, Math.abs(z1e - z0e) / d) * g * 2));
+ for (let k = 0; k <= steps; k++) {
+ const ex = x0e + ((x1e - x0e) * k) / steps;
+ const ez = z0e + ((z1e - z0e) * k) / steps;
+ cells[cell(ez, box.minZ, d) * g + cell(ex, box.minX, w)] = 1;
+ }
+ }
+ }
const den = (bz - cz) * (ax - cx) + (cx - bx) * (az - cz);
if (Math.abs(den) < 1e-12) continue;
const x0 = cell(Math.min(ax, bx, cx), box.minX, w), x1 = cell(Math.max(ax, bx, cx), box.minX, w);
@@ -120,9 +135,72 @@ function coverage(parts, isBand, box) {
}
}
}
+ return cells;
+}
+
+// Share of the band's bounding rectangle its triangles cover, seen from above.
+function coverage(parts, isBand, box) {
+ const cells = rasterize(parts, isBand, box, FILL_GRID);
+ if (!cells) return 0;
let covered = 0;
for (const c of cells) covered += c;
- return covered / (g * g);
+ return covered / cells.length;
+}
+
+// Where the subject touches down, as a top-down mask: the contact layer
+// rasterized over its own bounds (plus the margin), with everything it encloses
+// filled in (a box's walls enclose its whole bottom face, a teapot's lower curve
+// its foot), then grown by one cell so the plate directly under an edge is kept
+// rather than nibbled.
+function contactMask(parts, isContact, box) {
+ const g = CONTACT_GRID;
+ const cells = rasterize(parts, isContact, box, g, { edges: true });
+ if (!cells) return null;
+ // Flood the open floor in from the border; any uncovered cell it cannot
+ // reach is enclosed by the subject and belongs to its footprint.
+ const outside = new Uint8Array(cells.length);
+ const queue = [];
+ for (let i = 0; i < g; i++) {
+ for (const c of [i, (g - 1) * g + i, i * g, i * g + g - 1]) {
+ if (!cells[c] && !outside[c]) {
+ outside[c] = 1;
+ queue.push(c);
+ }
+ }
+ }
+ while (queue.length) {
+ const c = queue.pop();
+ const x = c % g, z = (c - x) / g;
+ for (const [nx, nz] of [[x - 1, z], [x + 1, z], [x, z - 1], [x, z + 1]]) {
+ if (nx < 0 || nz < 0 || nx >= g || nz >= g) continue;
+ const n = nz * g + nx;
+ if (!cells[n] && !outside[n]) {
+ outside[n] = 1;
+ queue.push(n);
+ }
+ }
+ }
+ for (let c = 0; c < cells.length; c++) if (!outside[c]) cells[c] = 1;
+ const grown = new Uint8Array(cells.length);
+ for (let z = 0; z < g; z++) {
+ for (let x = 0; x < g; x++) {
+ if (!cells[z * g + x]) continue;
+ for (let dz = -1; dz <= 1; dz++) {
+ for (let dx = -1; dx <= 1; dx++) {
+ const nz = z + dz, nx = x + dx;
+ if (nz >= 0 && nz < g && nx >= 0 && nx < g) grown[nz * g + nx] = 1;
+ }
+ }
+ }
+ }
+ const w = box.maxX - box.minX;
+ const d = box.maxZ - box.minZ;
+ return (px, pz) => {
+ if (px < box.minX || px > box.maxX || pz < box.minZ || pz > box.maxZ) return false;
+ const gx = Math.min(g - 1, Math.floor(((px - box.minX) / w) * g));
+ const gz = Math.min(g - 1, Math.floor(((pz - box.minZ) / d) * g));
+ return grown[gz * g + gx] === 1;
+ };
}
/**
@@ -149,6 +227,8 @@ export function detectFloorSlab(parts) {
const contactTop = minY + CONTACT_CEILING * height;
const inBand = (p, idx, t) =>
Math.max(p[idx[t] * 3 + 1], p[idx[t + 1] * 3 + 1], p[idx[t + 2] * 3 + 1]) <= bandTop;
+ const inContact = (p, idx, t) =>
+ !inBand(p, idx, t) && Math.min(p[idx[t] * 3 + 1], p[idx[t + 1] * 3 + 1], p[idx[t + 2] * 3 + 1]) <= contactTop;
let total = 0;
let bandTris = 0;
@@ -188,7 +268,10 @@ export function detectFloorSlab(parts) {
const rest = area(contact) > 0 ? contact : subject;
const mx = (rest.maxX - rest.minX) * FOOTPRINT_MARGIN;
const mz = (rest.maxZ - rest.minZ) * FOOTPRINT_MARGIN;
- const keep = { minX: rest.minX - mx, maxX: rest.maxX + mx, minZ: rest.minZ - mz, maxZ: rest.maxZ + mz };
+ const keepBox = { minX: rest.minX - mx, maxX: rest.maxX + mx, minZ: rest.minZ - mz, maxZ: rest.maxZ + mz };
+ const underSubject =
+ (area(contact) > 0 && contactMask(parts, inContact, keepBox)) ||
+ ((x, z) => x >= keepBox.minX && x <= keepBox.maxX && z >= keepBox.minZ && z <= keepBox.maxZ);
let removedTriangles = 0;
const remove = parts.map(({ positions: p, indices }) => {
const drop = new Set();
@@ -199,8 +282,7 @@ export function detectFloorSlab(parts) {
if (Math.max(p[a + 1], p[b + 1], p[c + 1]) > bandTop) continue;
const cx = (p[a] + p[b] + p[c]) / 3;
const cz = (p[a + 2] + p[b + 2] + p[c + 2]) / 3;
- const inside = cx >= keep.minX && cx <= keep.maxX && cz >= keep.minZ && cz <= keep.maxZ;
- if (!inside) {
+ if (!underSubject(cx, cz)) {
drop.add(t);
removedTriangles++;
}
diff --git a/tests/api/glb-floor-slab.test.js b/tests/api/glb-floor-slab.test.js
new file mode 100644
index 000000000..321f43a01
--- /dev/null
+++ b/tests/api/glb-floor-slab.test.js
@@ -0,0 +1,160 @@
+/**
+ * Floor-slab strip (api/_lib/glb-floor-slab.js).
+ *
+ * TRELLIS and Hunyuan3D fuse a thin, wide plate under a resting subject (a
+ * teapot on a marble square). These scenes are real GLBs built with the same
+ * @gltf-transform the pass reads them with, one per rule: a subject on a fused
+ * plate loses the plate, while a flat subject, a thick plinth, a round lamp foot
+ * and a tripod's feet are all left alone.
+ */
+import { describe, it, expect } from 'vitest';
+import { Document, NodeIO } from '@gltf-transform/core';
+import { stripFloorSlab, detectFloorSlab } from '../../api/_lib/glb-floor-slab.js';
+import { scoreGlbQuality } from '../../api/_lib/glb-quality.js';
+
+// Geometry builders. Every face is subdivided, as a reconstructor's mesh is, so
+// the lowest ring of a wall never reaches down into the floor band.
+function quad(tris, a, b, c, d) {
+ tris.push([a, b, c], [a, c, d]);
+}
+function plate(tris, x0, x1, z0, z1, y, n = 12) {
+ for (let i = 0; i < n; i++) {
+ for (let j = 0; j < n; j++) {
+ const xa = x0 + ((x1 - x0) * i) / n, xb = x0 + ((x1 - x0) * (i + 1)) / n;
+ const za = z0 + ((z1 - z0) * j) / n, zb = z0 + ((z1 - z0) * (j + 1)) / n;
+ quad(tris, [xa, y, za], [xb, y, za], [xb, y, zb], [xa, y, zb]);
+ }
+ }
+}
+function box(tris, x0, x1, y0, y1, z0, z1, n = 10) {
+ plate(tris, x0, x1, z0, z1, y0, n);
+ plate(tris, x0, x1, z0, z1, y1, n);
+ for (let i = 0; i < n; i++) {
+ const ya = y0 + ((y1 - y0) * i) / n, yb = y0 + ((y1 - y0) * (i + 1)) / n;
+ for (let j = 0; j < n; j++) {
+ const xa = x0 + ((x1 - x0) * j) / n, xb = x0 + ((x1 - x0) * (j + 1)) / n;
+ const za = z0 + ((z1 - z0) * j) / n, zb = z0 + ((z1 - z0) * (j + 1)) / n;
+ quad(tris, [xa, ya, z0], [xb, ya, z0], [xb, yb, z0], [xa, yb, z0]);
+ quad(tris, [xa, ya, z1], [xb, ya, z1], [xb, yb, z1], [xa, yb, z1]);
+ quad(tris, [x0, ya, za], [x0, ya, zb], [x0, yb, zb], [x0, yb, za]);
+ quad(tris, [x1, ya, za], [x1, ya, zb], [x1, yb, zb], [x1, yb, za]);
+ }
+ }
+}
+function disk(tris, r, y, segs = 96, rings = 6) {
+ for (let k = 0; k < rings; k++) {
+ const r0 = (r * k) / rings, r1 = (r * (k + 1)) / rings;
+ for (let s = 0; s < segs; s++) {
+ const a0 = (2 * Math.PI * s) / segs, a1 = (2 * Math.PI * (s + 1)) / segs;
+ const p = (rr, a) => [rr * Math.cos(a), y, rr * Math.sin(a)];
+ quad(tris, p(r0, a0), p(r1, a0), p(r1, a1), p(r0, a1));
+ }
+ }
+}
+
+// One node, one indexed primitive: the shape cleanup leaves a forge mesh in.
+async function toGlb(tris) {
+ const doc = new Document();
+ const buffer = doc.createBuffer();
+ const positions = new Float32Array(tris.flat(2));
+ const indices = new Uint32Array(tris.length * 3).map((_, i) => i);
+ const prim = doc
+ .createPrimitive()
+ .setAttribute('POSITION', doc.createAccessor().setType('VEC3').setArray(positions).setBuffer(buffer))
+ .setIndices(doc.createAccessor().setType('SCALAR').setArray(indices).setBuffer(buffer));
+ const node = doc.createNode().setMesh(doc.createMesh().addPrimitive(prim));
+ doc.createScene().addChild(node);
+ return Buffer.from(await new NodeIO().writeBinary(doc));
+}
+
+function parts(tris) {
+ return [{ positions: Float64Array.from(tris.flat(2)), indices: tris.map((_, i) => [i * 3, i * 3 + 1, i * 3 + 2]).flat() }];
+}
+
+function subjectOnPlate() {
+ const plateTris = [];
+ plate(plateTris, -3, 3, -3, 3, 0, 24);
+ plate(plateTris, -3, 3, -3, 3, 0.02, 24);
+ const subjectTris = [];
+ box(subjectTris, -0.5, 0.5, 0.02, 1.02, -0.5, 0.5);
+ return { plateTris, subjectTris, all: [...plateTris, ...subjectTris] };
+}
+
+describe('detectFloorSlab', () => {
+ it('finds a subject fused onto a wide, solid plate', () => {
+ const v = detectFloorSlab(parts(subjectOnPlate().all));
+ expect(v.slab).toBe(true);
+ expect(v.fill).toBeGreaterThan(0.95);
+ expect(v.footprintRatio).toBeGreaterThan(10);
+ });
+
+ it('leaves a genuinely flat subject alone', () => {
+ const tris = [];
+ plate(tris, -2, 2, -2, 2, 0, 16);
+ plate(tris, -2, 2, -2, 2, 0.02, 16);
+ expect(detectFloorSlab(parts(tris)).slab).toBe(false);
+ });
+
+ it('leaves a single flat sheet alone: there is nothing standing on it', () => {
+ const tris = [];
+ plate(tris, -2, 2, -2, 2, 0, 16);
+ expect(detectFloorSlab(parts(tris)).slab).toBe(false);
+ });
+
+ it('leaves a thick plinth alone, because its top widens the subject', () => {
+ const tris = [];
+ box(tris, -1, 1, 0, 0.3, -1, 1);
+ box(tris, -0.5, 0.5, 0.3, 1.3, -0.5, 0.5);
+ expect(detectFloorSlab(parts(tris))).toMatchObject({ slab: false, reason: 'base_fits_subject' });
+ });
+
+ it('leaves a round lamp foot alone, because it is not a filled sheet', () => {
+ const tris = [];
+ disk(tris, 1.5, 0);
+ disk(tris, 1.5, 0.02);
+ box(tris, -0.05, 0.05, 0.02, 1.5, -0.05, 0.05);
+ box(tris, -0.6, 0.6, 1.5, 2, -0.6, 0.6);
+ const v = detectFloorSlab(parts(tris));
+ expect(v).toMatchObject({ slab: false, reason: 'band_not_a_sheet' });
+ expect(v.fill).toBeLessThan(0.85);
+ });
+
+ it("leaves a tripod's feet alone", () => {
+ const tris = [];
+ for (const [x, z] of [[-1, -1], [1, -1], [0, 1]]) box(tris, x - 0.05, x + 0.05, 0, 1.5, z - 0.05, z + 0.05);
+ box(tris, -0.3, 0.3, 1.5, 1.8, -0.3, 0.3);
+ expect(detectFloorSlab(parts(tris)).slab).toBe(false);
+ });
+});
+
+describe('stripFloorSlab', () => {
+ it('cuts the plate outside the subject and keeps every subject triangle', async () => {
+ const { plateTris, subjectTris, all } = subjectOnPlate();
+ const input = await toGlb(all);
+ const before = scoreGlbQuality(input).metrics;
+ const r = await stripFloorSlab(input);
+ expect(r.stripped).toBe(true);
+ expect(r.removedTriangles).toBeGreaterThan(plateTris.length * 0.8);
+ const after = scoreGlbQuality(r.buffer).metrics;
+ expect(after.triangleCount).toBe(before.triangleCount - r.removedTriangles);
+ expect(after.triangleCount).toBeGreaterThanOrEqual(subjectTris.length);
+ // The model no longer reads as a pancake to the cheap scorer.
+ expect(before.planar).toBe(true);
+ expect(after.planar).toBe(false);
+ expect(r.buffer.length).toBeLessThan(input.length);
+ });
+
+ it('returns the original bytes when there is no slab', async () => {
+ const tris = [];
+ box(tris, -1, 1, 0, 0.3, -1, 1);
+ box(tris, -0.5, 0.5, 0.3, 1.3, -0.5, 0.5);
+ const input = await toGlb(tris);
+ const r = await stripFloorSlab(input);
+ expect(r.stripped).toBe(false);
+ expect(r.buffer).toBe(input);
+ });
+
+ it('throws on a non-GLB buffer so the caller ships the original', async () => {
+ await expect(stripFloorSlab(Buffer.from('not a glb'))).rejects.toThrow();
+ });
+});