medieval-kit/scripts/support.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

228 lines
8.2 KiB
TypeScript

/**
* "Nothing may float." — the kit's hardest structural rule, made measurable.
*
* Every other check in this repo asks about surfaces: is the winding right, do
* two faces share a plane, is the triangle count sane. None of them can see the
* failure that actually ruins a prop, which is a piece of it hanging in the air
* with nothing holding it up. A chest lid separated from its chest is not a
* subtle shading artefact; it is the model being wrong, and it survived every
* check we had.
*
* The test: voxelise the model, find the connected components of occupied
* space, and require each one to be supported. A component is supported when it
* reaches the ground plane, or — for props that hang from a wall or a beam —
* when the model is a single connected mass, because then whatever holds the
* mass holds all of it.
*
* Two things this deliberately does NOT do:
*
* - It does not care about physics beyond contact. A plank balanced on one
* corner passes. Modelling centre of mass would reject a lot of correct
* medieval carpentry, which leans and props things at angles.
* - It does not treat "touching" as "sharing a vertex". These models are
* built from separately generated solids that interpenetrate on purpose
* (that is the z-fighting rule), so contact is spatial, not topological.
*/
import { Box3, Mesh, Vector3, type Object3D } from 'three/webgpu'
export interface FloatingPiece {
/** Mesh names contributing to this component. */
readonly parts: readonly string[]
/** How many voxels the component occupies — a rough size. */
readonly voxels: number
/** Height of its lowest point above the model's floor, in metres. */
readonly clearance: number
/** Where it is, for putting into a message. */
readonly at: readonly [number, number, number]
}
export interface SupportReport {
/** Every component, floating or not — for diagnosis. */
readonly all: readonly FloatingPiece[]
readonly components: number
readonly floating: readonly FloatingPiece[]
readonly voxel: number
}
export interface SupportOptions {
/**
* `ground` — every component must reach the floor.
* `hanging` — the model hangs from something outside itself (a wall bracket,
* a yoke), so it only has to be ONE connected mass.
*/
readonly support?: 'ground' | 'hanging'
/** Voxels across the model's longest axis. Higher is stricter and slower. */
readonly resolution?: number
}
interface Cell {
readonly key: string
readonly x: number
readonly y: number
readonly z: number
readonly parts: Set<string>
}
/**
* Samples a triangle densely enough that no voxel it crosses is missed.
*
* Sampling by area rather than a fixed count matters: these models mix a
* 0.9 m² table top with 2 mm chamfer slivers, and a fixed count would either
* miss the big faces or spend all its time on the small ones.
*/
function* sampleTriangle(
a: Vector3, b: Vector3, c: Vector3, step: number,
): Generator<Vector3> {
const ab = new Vector3().subVectors(b, a)
const ac = new Vector3().subVectors(c, a)
const rows = Math.max(1, Math.ceil(Math.max(ab.length(), ac.length()) / step))
for (let i = 0; i <= rows; i += 1) {
for (let j = 0; j <= rows - i; j += 1) {
const u = i / rows
const v = j / rows
yield new Vector3(
a.x + ab.x * u + ac.x * v,
a.y + ab.y * u + ac.y * v,
a.z + ab.z * u + ac.z * v,
)
}
}
}
export function findFloating(root: Object3D, options: SupportOptions = {}): SupportReport {
const resolution = options.resolution ?? 88
const mode = options.support ?? 'ground'
root.updateMatrixWorld(true)
const box = new Box3().setFromObject(root)
const size = box.getSize(new Vector3())
const extent = Math.max(size.x, size.y, size.z, 1e-4)
const voxel = extent / resolution
// --- 1. Occupancy ---------------------------------------------------------
const cells = new Map<string, Cell>()
const vertex = new Vector3()
root.traverse((object) => {
if (!(object instanceof Mesh)) return
const position = object.geometry.getAttribute('position')
if (!position) return
const index = object.geometry.getIndex()
const count = index ? index.count : position.count
const name = object.name || 'unnamed'
const at = (i: number): Vector3 => {
const v = index ? index.getX(i) : i
return vertex.fromBufferAttribute(position, v).applyMatrix4(object.matrixWorld).clone()
}
for (let i = 0; i < count; i += 3) {
for (const point of sampleTriangle(at(i), at(i + 1), at(i + 2), voxel * 0.7)) {
const x = Math.floor((point.x - box.min.x) / voxel)
const y = Math.floor((point.y - box.min.y) / voxel)
const z = Math.floor((point.z - box.min.z) / voxel)
const key = `${x},${y},${z}`
const existing = cells.get(key)
if (existing) existing.parts.add(name)
else cells.set(key, { key, x, y, z, parts: new Set([name]) })
}
}
})
// --- 2. Connected components, 26-neighbourhood ----------------------------
const parent = new Map<string, string>()
const find = (key: string): string => {
let root_ = key
while (parent.get(root_) !== root_) root_ = parent.get(root_)!
let walk = key
while (parent.get(walk) !== root_) {
const next = parent.get(walk)!
parent.set(walk, root_)
walk = next
}
return root_
}
const union = (a: string, b: string): void => {
const ra = find(a)
const rb = find(b)
if (ra !== rb) parent.set(ra, rb)
}
for (const key of cells.keys()) parent.set(key, key)
for (const cell of cells.values()) {
for (let dx = -1; dx <= 1; dx += 1) {
for (let dy = -1; dy <= 1; dy += 1) {
for (let dz = -1; dz <= 1; dz += 1) {
if (dx === 0 && dy === 0 && dz === 0) continue
const other = `${cell.x + dx},${cell.y + dy},${cell.z + dz}`
if (cells.has(other)) union(cell.key, other)
}
}
}
}
// --- 3. Support -----------------------------------------------------------
const groups = new Map<string, { voxels: number; lowest: number; parts: Set<string>; at: Cell }>()
for (const cell of cells.values()) {
const key = find(cell.key)
const group = groups.get(key)
if (!group) {
groups.set(key, { voxels: 1, lowest: cell.y, parts: new Set(cell.parts), at: cell })
continue
}
group.voxels += 1
for (const part of cell.parts) group.parts.add(part)
if (cell.y < group.lowest) {
group.lowest = cell.y
// Report the lowest cell: for a floating piece that is the point a reader
// wants to look at, the gap under it.
}
}
const list = [...groups.values()].sort((a, b) => b.voxels - a.voxels)
const floating: FloatingPiece[] = []
if (mode === 'hanging') {
// A hanging prop only has to hold together. Everything after the largest
// mass is detached from it.
for (const group of list.slice(1)) {
floating.push({
parts: [...group.parts].sort(),
voxels: group.voxels,
clearance: +(group.lowest * voxel).toFixed(4),
at: [
+(box.min.x + group.at.x * voxel).toFixed(3),
+(box.min.y + group.at.y * voxel).toFixed(3),
+(box.min.z + group.at.z * voxel).toFixed(3),
],
})
}
} else {
// Grounded: a component is supported when it reaches the bottom of the
// model. One voxel of tolerance, because the floor row is where contact
// rounds to.
for (const group of list) {
if (group.lowest <= 1) continue
floating.push({
parts: [...group.parts].sort(),
voxels: group.voxels,
clearance: +(group.lowest * voxel).toFixed(4),
at: [
+(box.min.x + group.at.x * voxel).toFixed(3),
+(box.min.y + group.at.y * voxel).toFixed(3),
+(box.min.z + group.at.z * voxel).toFixed(3),
],
})
}
}
const all: FloatingPiece[] = list.map((group) => ({
parts: [...group.parts].sort(),
voxels: group.voxels,
clearance: +(group.lowest * voxel).toFixed(4),
at: [
+(box.min.x + group.at.x * voxel).toFixed(3),
+(box.min.y + group.at.y * voxel).toFixed(3),
+(box.min.z + group.at.z * voxel).toFixed(3),
],
}))
return { all, components: groups.size, floating, voxel: +voxel.toFixed(5) }
}