medieval-kit/scripts/catalog-table.ts
CRT fdb25be320 Land the mill's ladder, and stop the table calling six models static
Two things, both found by pulling on the handcart's thread.

THE LADDER. I said three models had parts floating a few millimetres. One did.
The other two readings were my own measurement error and worth writing down,
because it is the same error as the vertex-sampling ones: I measured each part
against the model's BOUNDING BOX minimum, and a part deliberately bedded into
the ground drags that datum below the floor, so every correctly placed part
beside it appears to float by exactly the bedding depth. The forge's base
stones settle 6.8 mm and the well's drystone kerb 9.3 mm, both on purpose, and
the bellows and the well frame sit exactly on the floor their own source
declares. Nothing wrong with either.

The post mill's tail ladder was real: 14.5 mm clear. It already carried two
analytic corrections for the same corner, both correct about what they
describe, and neither accounting for the chamfer — the corner they compute the
drop of has been cut off, so the residual was very nearly the chamfer itself.
Rather than add a third derivation to a spot that has now been got wrong three
times, the assembled ladder is measured and put on the floor. A bounding box
cannot be wrong about this the way arithmetic can, and it stays right when the
chamfer or the rail section changes.

`restsOn` now covers the mill's trestle and ladder too, and its doc comment
carries the bedding caveat, so the next person to reach for it knows the datum
is the bounding box and knows what that costs.

THE ANIMATED COLUMN. `catalog-table.ts` decided it with `built.action`, which
is the VIEWER's hand-registered button and exists for five models. So the
generated table published the grindstone, the stone well, the post mill, the
cauldron, the forge and the handcart as static — six of the eleven models that
carry actions, including three of the most animated things in the kit.

The obvious replacement is worse. `core/kit.ts` wraps update as
`update: (dt) => options.update?.(dt, runtime)`, so it is a function on every
model whether or not that model does anything with it, and switching to it
marked all thirty-seven animated. One flag too narrow, its replacement too
broad.

So the generator now runs each model — a second of frames, then its action if
it has one — and asks whether anything moved. That catches what moves by
itself. What moves ON DEMAND it cannot catch, because the motion is behind an
interface the catalogue never exposed, so `Entry` now carries `actionNames`
from the model's own actions. Eleven models, and both documents list them.

Worth keeping: this table is generated precisely so it cannot go stale, it says
so in its own prose, and it was wrong for months. Generated is not the same as
correct — it only moves the mistake into the generator, where nobody looks.

Also in the npm README, which is the page npmjs.com renders: `vibe3d init` is
now the first line rather than unmentioned, `@types/three` is called out (three
ships no declarations, so without it the installed source does not typecheck),
the `@/` alias and `allowImportingTsExtensions` have a Project setup block, and
the one relative link is gone — npm resolves those against the repository, and
this one pointed at a directory the package deliberately does not ship.
`homepage` now points at the live site instead of a repository that is private.
2026-08-27 09:36:24 +00:00

126 lines
5.2 KiB
TypeScript

/**
* Generates the model table in REFERENCE.md.
*
* A hand-written table goes stale the moment the first model is added — and it
* had gone stale: the document said "four items" while the kit had twenty-three.
* The table is now read from the models THEMSELVES, so for it to be wrong the
* model itself has to be broken.
*
* bun scripts/catalog-table.ts
*/
import { Box3, Mesh, type Object3D } from 'three/webgpu'
import { MODEL_META } from '../my-registry/meta.ts'
import { CATALOG, type Entry } from '@/catalog.ts'
/**
* Whether a model moves — decided by RUNNING it, not by asking it.
*
* Both of the obvious flags lie. `built.action` is the viewer's hand-registered
* button descriptor from src/catalog.ts and only five models are given one, so
* the column called the grindstone, the stone well and the post mill static.
* `built.update` is worse: core/kit.ts wraps it as
* `update: (dt) => options.update?.(dt, runtime)`, so it is a function on every
* model whether or not that model does anything with it, and swapping to it
* marked all thirty-seven animated. One flag was too narrow, its replacement
* too broad, and neither is what the column claims to mean.
*
* So: take the model, drive a second of frames through it, and see whether
* anything moved. Where a model's motion is gated behind an action — the chest
* lid only eases once something has opened it — fire the action and drive it
* again. Deterministic seeding makes this reproducible, so the table stays
* stable between runs.
*/
function animates(entry: Entry): boolean {
const snapshot = (root: Object3D): string => {
root.updateMatrixWorld(true)
const out: number[] = []
root.traverse((object) => {
out.push(...object.matrixWorld.elements)
if (object instanceof Mesh) {
// Motion can be in the geometry rather than the transform — a flame is
// rebuilt, not moved — so a sample of the vertices comes along too.
const position = object.geometry.getAttribute('position')
if (position) {
const step = Math.max(1, Math.floor(position.count / 24))
for (let i = 0; i < position.count; i += step) {
out.push(position.getX(i), position.getY(i), position.getZ(i))
}
}
}
})
return out.map((n) => n.toFixed(5)).join(',')
}
const built = entry.build()
// A model with typed actions of its own is a model that does something, and
// no amount of running it will show that from the outside: the grindstone
// only turns once something cranks it, and this generator is not going to
// learn each model's interface to find out. Running it catches what moves by
// itself, `actionNames` catches what moves on demand, and between them the
// column says what a reader takes it to say.
if ((built.actionNames?.length ?? 0) > 0) {
built.dispose()
return true
}
const still = snapshot(built.root)
const run = (frames: number): void => {
for (let i = 0; i < frames; i += 1) built.update?.(1 / 60)
}
run(60)
let moved = snapshot(built.root) !== still
if (!moved && built.action) {
built.action.run()
run(120)
moved = snapshot(built.root) !== still
}
built.dispose()
return moved
}
const rows: string[] = []
let total = 0
const slots = new Set<string>()
for (const id of Object.keys(CATALOG)) {
const meta = MODEL_META[id]
if (!meta) continue
const built = CATALOG[id]!.build()
let triangles = 0
built.root.traverse((object) => {
if (!(object instanceof Mesh)) return
const index = object.geometry.getIndex()
triangles += (index ? index.count : object.geometry.getAttribute('position').count) / 3
})
const box = new Box3().setFromObject(built.root)
const size = box.getSize(box.max.clone())
// `update`, not `action`.
//
// `built.action` is the VIEWER's hand-registered button descriptor over in
// src/catalog.ts, and only five models are given one. What this column means
// is "this model animates", which is `update`: a model can carry a per-frame
// update without anyone having wired a button for it in the demo app.
//
// The two lists overlap enough that the column looked right, and the table
// quietly called the grindstone, the stone well and the post mill static —
// three of the most animated things in the kit. Worth noting where it was
// read: not here, but in the generated table, by someone checking the
// document against the models. The table is generated so it cannot go stale,
// and it was still wrong, because the generator was asking the wrong object.
const animated = animates(CATALOG[id]!)
built.dispose()
for (const slot of meta.materialSlots) slots.add(slot)
total += triangles
const dims = `${size.x.toFixed(2)}×${size.y.toFixed(2)}×${size.z.toFixed(2)}`
rows.push(
`| \`${id}\` | ${meta.category} | ${triangles} | ${meta.parts.length} | ${dims} | `
+ `${meta.materialSlots.join(', ')} | ${animated ? '✔' : ''} |`,
)
}
console.log('| Model | Category | Triangles | Parts | Size (m) | Material slots | Animated |')
console.log('| --- | --- | ---: | ---: | --- | --- | :-: |')
console.log(rows.join('\n'))
console.log(`\n${rows.length} models · ${total} triangles · ${slots.size} material slots`)