medieval-kit/scripts/audit.ts
CRT 74068b13f5 Enforce "nothing floats", and rebuild the barrel from a reference photo
Two rules were missing, and each one had been quietly failing for a while.

The first is structural. Every check in this repo asked about surfaces --
winding, coplanarity, triangle counts -- and none of them could see a piece
of a model hanging in the air with nothing holding it up. A chest lid
separated from its chest passed everything we had. scripts/support.ts
voxelises the model, finds the connected components of occupied space and
requires each one to reach the floor; scripts/audit.ts runs that across
default, min, max, both sweep ends, actions and the midpoint of a morph, so
a variation cannot break what the default gets right. That audit started at
22 failing cases: the bell and the sign had no supporting structure at all
and now have a frame and a post, the lantern was in four pieces, the sack's
collar floated above its body, the basket's fruit hung level with the rim
instead of resting on the base, and the ladder's rungs were sized by a
formula that disagreed with where the rails actually were. It now reports 0.

The second is that we had no target. The feedback loop was an offline
render of our own output, which can only answer "is this broken", never
"is this right". Given a reference photograph of a cask, five defects were
obvious in seconds that three rounds of self-review had missed: four hoops
where there should be six, no rivets at all, a chime cut so shallow it did
not read, and a stave count low enough to leave a visibly polygonal
silhouette. All four are fixed here.

Two things the rivets taught us, both kept as comments rather than as
scars: a stud that tapers outward tilts its side normals back towards the
axis, which the radial check correctly reports as reversed winding -- a
real rivet head flares, so the honest shape is also the passing one. And
once seated deep enough to look right, the stud's buried inner cap falls
inside the shell the check inspects, so it is not generated at all, exactly
as bandGeometry already declines to build the inside of a hoop.

27 models, 16973 triangles, all checks pass, 0 floating cases.
2026-08-21 12:23:11 +00:00

69 lines
2.6 KiB
TypeScript

/** Floating check: default, action state, config extremes and MID-MORPH. */
import { CATALOG, SHOWCASE_ORDER, controlsFor } from '@/catalog.ts'
import { findFloating } from './support.ts'
import { blend, capture, compatible } from '@/morph.ts'
const HANGING = new Set(['tavern-sign', 'bronze-bell'])
function atFraction(id: string, f: number): Record<string, number> {
const patch: Record<string, number> = {}
for (const c of controlsFor(id)) {
if (c.key === 'seed') continue
const raw = c.min + (c.max - c.min) * f
patch[c.key] = c.step >= 1 ? Math.round(raw) : raw
}
return patch
}
/** The showcase sweeps continuous sliders between 18% and 82% of their range. */
function sweepEnds(id: string): [Record<string, number>, Record<string, number>] {
const from: Record<string, number> = {}
const to: Record<string, number> = {}
for (const c of controlsFor(id)) {
const lo = c.min + (c.max - c.min) * 0.18
const hi = c.max - (c.max - c.min) * 0.18
if (c.step >= 1 || c.key === 'seed') {
const v = Math.round((lo + hi) / 2)
from[c.key] = v; to[c.key] = v
} else { from[c.key] = lo; to[c.key] = hi }
}
return [from, to]
}
let bad = 0
for (const id of SHOWCASE_ORDER) {
const lines: string[] = []
const check = (label: string, root: Parameters<typeof findFloating>[0]) => {
const r = findFloating(root, { support: HANGING.has(id) ? 'hanging' : 'ground', resolution: 72 })
if (r.floating.length === 0) return
bad += 1
lines.push(` ${label.padEnd(10)} ${r.floating
.map((f) => `${f.parts.join('+').replaceAll(id + '/', '')} ${f.voxels}vx @${f.clearance}m`).join(' | ')}`)
}
for (const [label, f] of [['default', undefined], ['min', 0.02], ['max', 0.98], ['sweep-lo', 0.18], ['sweep-hi', 0.82]] as const) {
const b = CATALOG[id]!.build()
if (f !== undefined) b.params?.apply(atFraction(id, f))
check(label, b.root); b.dispose()
}
const act = CATALOG[id]!.build()
if (act.action) { act.action.run(); for (let i = 0; i < 240; i++) act.update?.(1 / 60) ; check('action', act.root) }
act.dispose()
// Mid-morph: exactly what the showcase renders between two sweep ends.
const m = CATALOG[id]!.build()
const [a, z] = sweepEnds(id)
m.params?.apply(z); const end = capture(m.root)
m.params?.apply(a); const start = capture(m.root)
if (compatible(start, end)) {
blend(m.root, start, end, 0.5)
check('morph-mid', m.root)
} else {
lines.push(' morph-mid INCOMPATIBLE (falls back to rebuilds)')
}
m.dispose()
if (lines.length) console.log(`${id}\n${lines.join('\n')}`)
}
console.log(`\n${bad} failing case(s)`)