medieval-kit/scripts/build-cover.ts
CRT 973f673c3f Draw the card with a command instead of a browser
The card a shared link unfurls to was a picture of the models with no title on
it, and the models on it were too small to tell apart. Both are fixed, but the
part worth writing down is how.

The first attempt was an HTML page composed in the site's own fonts and
screenshotted. It looked right and it was wrong: the model count on it becomes
a picture of a number the moment the shutter closes, and `check-docs.ts`
cannot read a picture. That is the exact failure that check exists for — the
READMEs claimed 27 models when there were 37 — and a card is the worse place
to have it, because nobody reading one can see the kit to know it is stale.

So `bun run cover:build` counts the catalogue and draws the numbers it counted.
`site:build` runs it, which means the published card cannot be older than the
models it shows, and the day a model is added the card says 38 without anyone
remembering to make it.

Drawing type needed type. `scripts/text.ts` reads the outlines out of a `.ttf`
and fills them with a scanline pass — four sub-scanlines a row with exact
horizontal coverage, which is more code than sampling a 4x4 grid and visibly
better at the 16px the two mono lines are set in. It is deliberately partial:
format 4 cmap, simple and composite outlines, advances, no kerning or shaping.
What it cannot draw it throws on, because a headline missing a letter is not
something you notice before it is published. Archivo and IBM Plex Mono come
with it under the OFL, the same two faces the site is set in.

The renderer moved to `scripts/raster.ts` so the card and the contact sheet
draw from the same code and cannot disagree about what a barrel looks like;
`render.ts` is now the command line over it, unchanged in what it does. Its
one new flag is `--ground`, a flat background instead of the per-cell
gradient: forty gradients side by side read as banding, and on the page's own
ink the models float on the card as one field instead of sitting on it as a
grey slab.

Ten columns by four, because 1.91:1 and 37 models want 40 cells and at that
shape the cells come out square, which is what lets a barrel look like a barrel
at 116 pixels. The last row is centred — three empty cells left flush is a
notch in the corner, and a short row centred is how a contact sheet ends.

Byte-identical across runs, so it does not churn the diff on every build.
2026-08-27 08:00:15 +00:00

129 lines
5.3 KiB
TypeScript

/**
* Builds `media/cover.png`: the 1200×630 card that every shared link, search
* result and chat unfurl of https://medieval.crt.fyi/ shows.
*
* bun run cover:build
*
* It is a script and not a design file for one reason: the numbers on it are
* COUNTED, not typed. A card that says "37 models" is a picture of a number,
* and a picture cannot be checked the way `check-docs.ts` checks the READMEs —
* so it is built from the catalogue on every site build, and the day a model
* is added the card says so without anyone remembering to make it.
*
* Everything it draws comes from the pieces beside it: the models from
* `raster.ts`, which is the same renderer the contact sheet uses, so the card
* cannot disagree with it about what a barrel looks like; and the type from
* `text.ts`, reading the site's own two faces out of their `.ttf` files, so
* the card is set in the same Archivo and IBM Plex Mono as the page it
* advertises.
*/
import { writeFile } from 'node:fs/promises'
import { Mesh } from 'three/webgpu'
import { blit, encodePng, newFrame, renderOne, toLinear } from './raster.ts'
import { fillText, measure, readFont } from './text.ts'
import { MODEL_META } from '../my-registry/meta.ts'
import { CATALOG } from '@/catalog.ts'
/* ------------------------------------------------------------- the palette */
// src/viewer.css, so the card and the page it links to are the same object.
const INK = toLinear('0b0e12')
const RULE = toLinear('2a333e')
const TEXT = toLinear('c7d1db')
const DIM = toLinear('7a8899')
const AMBER = toLinear('f0a93c')
/* -------------------------------------------------------------- the layout */
const WIDTH = 1200 // 1.91:1, which is what every unfurl crops to
const HEIGHT = 630
const MARGIN = 20
const COLUMNS = 10
const CELL = 116
/* --------------------------------------------------------------- the models */
const ids = Object.keys(CATALOG).filter((id) =>
MODEL_META[id] !== undefined && id !== 'pressure-gauge' && id !== 'kit')
let triangles = 0
for (const id of ids) {
const built = CATALOG[id]!.build()
built.root.traverse((object) => {
if (!(object instanceof Mesh)) return
const index = object.geometry.getIndex()
triangles += (index ? index.count : object.geometry.getAttribute('position').count) / 3
})
built.dispose()
}
const card = newFrame(WIDTH, HEIGHT, INK)
/**
* Ten columns and four rows, because a card is 1.91:1 and 37 models want 40
* cells: at that shape the cells come out square, which is what lets a barrel
* look like a barrel at 116 pixels.
*
* The models are rendered on the card's own background rather than the contact
* sheet's per-cell gradient, so they float on it as one field instead of
* sitting on it as a grey slab — and the three cells the kit does not fill
* stop being a hole. The last row is centred for the same reason: a short row
* left flush with the others is a notch in the bottom right corner, and a
* short row centred is how a contact sheet is supposed to end.
*/
const rows = Math.ceil(ids.length / COLUMNS)
const gridTop = HEIGHT - MARGIN - rows * CELL
ids.forEach((id, index) => {
const row = Math.floor(index / COLUMNS)
const column = index % COLUMNS
const inRow = Math.min(COLUMNS, ids.length - row * COLUMNS)
const rowLeft = (WIDTH - inRow * CELL) / 2
blit(card, renderOne(id, { size: CELL, ground: INK }), rowLeft + column * CELL, gridTop + row * CELL)
console.log(` ${id}`)
})
/* ----------------------------------------------------------------- the type */
const archivo = readFont('media/fonts/Archivo-Bold.ttf')
const mono = readFont('media/fonts/IBMPlexMono-Regular.ttf')
const left = MARGIN + 20
const right = WIDTH - MARGIN - 20
fillText(card, archivo, '@medieval-kit', { x: left - 2, y: 60, size: 52, colour: TEXT })
fillText(card, mono, 'Procedural lowpoly props for three.js. Install the source, not the asset.', {
x: left, y: 92, size: 16, colour: DIM,
})
// The install line is the whole point of a source registry, so it is set as
// something you could type rather than as one more sentence about it.
const prompt = fillText(card, mono, '$ ', { x: left, y: 118, size: 16, colour: AMBER })
fillText(card, mono, `bunx vibe3d add @medieval-kit/${ids[0]}`, { x: prompt, y: 118, size: 16, colour: TEXT })
// Counted, never typed.
const count = `${ids.length} models`
fillText(card, mono, count, { x: right, y: 58, size: 22, colour: AMBER, align: 'right' })
fillText(card, mono, `${triangles.toLocaleString('en-US')} triangles`, {
x: right, y: 86, size: 16, colour: DIM, align: 'right',
})
fillText(card, mono, 'MIT licensed', { x: right, y: 110, size: 16, colour: DIM, align: 'right' })
// A hairline between the type and the models, held inside the margins so it
// reads as a rule rather than as the edge of a box.
for (let x = left; x < right; x += 1) {
const at = ((gridTop - 18) * WIDTH + x) * 3
card.colour[at] = RULE[0]; card.colour[at + 1] = RULE[1]; card.colour[at + 2] = RULE[2]
}
/* ---------------------------------------------------------------------- out */
const png = encodePng(card)
await writeFile('media/cover.png', png)
console.log(
`\nmedia/cover.png — ${WIDTH}×${HEIGHT}, ${Math.round(png.length / 1024)} KB`
+ `\n${count} · ${triangles.toLocaleString('en-US')} triangles`
+ `\nheadline ${Math.round(measure(archivo, '@medieval-kit', 52))} px wide`,
)