diff --git a/media/cover.png b/media/cover.png new file mode 100644 index 0000000..78d5d5e Binary files /dev/null and b/media/cover.png differ diff --git a/media/icon.png b/media/icon.png new file mode 100644 index 0000000..52070d5 Binary files /dev/null and b/media/icon.png differ diff --git a/media/kit.png b/media/kit.png index c517e7d..e7ae833 100644 Binary files a/media/kit.png and b/media/kit.png differ diff --git a/media/models.png b/media/models.png index 023d384..1c01579 100644 Binary files a/media/models.png and b/media/models.png differ diff --git a/my-registry/README.md b/my-registry/README.md index 3333190..1a1134d 100644 --- a/my-registry/README.md +++ b/my-registry/README.md @@ -4,9 +4,9 @@ A lowpoly medieval model library for [Vibe3D](https://github.com/vibe-stack/vibe and Three.js. Install a prop and you own its source: readable TypeScript in your own tree, not an opaque package boundary. -![The whole kit in one scene](https://raw.githubusercontent.com/CRTkafa/medieval-kit/main/media/kit.png) +![The whole kit in one scene](https://medieval.crt.fyi/kit.png) -![Every model](https://raw.githubusercontent.com/CRTkafa/medieval-kit/main/media/models.png) +![Every model](https://medieval.crt.fyi/models.png) ```sh bunx vibe3d add @medieval-kit/wooden-barrel diff --git a/scripts/build-site.ts b/scripts/build-site.ts index d5a3195..b7dc153 100644 --- a/scripts/build-site.ts +++ b/scripts/build-site.ts @@ -41,6 +41,19 @@ await writeFile(`${dist}/_headers`, `/viewer-* Cache-Control: public, max-age=0, must-revalidate `, 'utf8') +/** + * The images the page's own meta tags point at. + * + * `og:image` is an absolute URL to this host, so the file has to BE on this + * host: a card that 404s is a card that shows nothing, and nothing about the + * page itself would look wrong. `kit.png` and `models.png` come too, because + * the published README links to them and a reader of that page should not + * depend on a private repository being readable. + */ +for (const image of ['cover.png', 'icon.png', 'kit.png', 'models.png']) { + await copyFile(`media/${image}`, `${dist}/${image}`) +} + // Listed rather than named. The bundles are hashed, so writing `viewer.js` // here would be a lie the moment the bytes change — which is the same mistake // this script's own first version made, in this very line. diff --git a/scripts/check-docs.ts b/scripts/check-docs.ts index 597550e..e3eea36 100644 --- a/scripts/check-docs.ts +++ b/scripts/check-docs.ts @@ -64,7 +64,21 @@ const WORDS = [ 'seventeen', 'eighteen', 'nineteen', 'twenty', ] -for (const file of ['README.md', 'REFERENCE.md', 'my-registry/README.md']) { +/** + * `viewer.html` is in this list because it is the published page. + * + * Its meta description is what a search result and a shared link show, and it + * quotes the model count and the triangle total — which is the same sentence + * that went stale three times in the READMEs before this check existed. A + * number in a card is worse than a number in a README: nobody reading the card + * can see the kit to know it is wrong. + * + * It carries no table, so the every-model-listed rule does not apply to it, + * any more than it does to the root README. + */ +const CARRIES_A_TABLE = new Set(['REFERENCE.md', 'my-registry/README.md']) + +for (const file of ['README.md', 'REFERENCE.md', 'my-registry/README.md', 'viewer.html']) { const text = readFileSync(file, 'utf8') console.log(file) @@ -74,7 +88,7 @@ for (const file of ['README.md', 'REFERENCE.md', 'my-registry/README.md']) { // Every model has to appear by id, or the document is describing a smaller // kit than the one it ships with. The registry README and the reference each // carry a generated table; the root README does not, so it is exempt. - if (file !== 'README.md') { + if (CARRIES_A_TABLE.has(file)) { const missing = ids.filter((id) => !text.includes(`\`${id}\``)) expect(`${file}: every model listed`, missing.length === 0, missing.join(', ')) } diff --git a/scripts/render.ts b/scripts/render.ts index 8ead120..457c025 100644 --- a/scripts/render.ts +++ b/scripts/render.ts @@ -340,13 +340,51 @@ function frameCamera(root: Object3D, width: number, height: number): { camera: P const box = new Box3().setFromObject(root) const sphere = box.getBoundingSphere(new Sphere()) const camera = new PerspectiveCamera(32, width / height, 0.01, 100) - const distance = (sphere.radius / Math.sin((camera.fov * Math.PI) / 360)) * 1.12 // Three-quarter view: the one angle that shows two faces at once. Looking // straight on hides depth completely. const direction = new Vector3(0.78, 0.5, 1).normalize() + + /** + * Fit the BOX to the frame, not the sphere to the frame's short side. + * + * A sphere fit is one number and it is wrong for anything that is not + * roughly round. The whole-kit scene is wide and flat: its bounding sphere + * is set by the horizontal extent and is far taller than the scene is, so + * fitting that sphere into the vertical field left the models occupying a + * band across the middle with empty air above and below. On a 1.91:1 social + * card it was most of the picture. + * + * The box fit is iterative because perspective is not linear in distance: + * put the camera somewhere, project the eight corners, see how far past the + * frame edge the worst one lands, and move by that ratio. Two passes is + * inside a pixel at these sizes, and it settles on whichever axis is + * actually tight rather than assuming it is the vertical one. + */ + let distance = (sphere.radius / Math.sin((camera.fov * Math.PI) / 360)) * 1.12 + const corner = new Vector3() + for (let pass = 0; pass < 3; pass += 1) { + camera.position.copy(sphere.center).addScaledVector(direction, distance) + camera.lookAt(sphere.center) + camera.updateMatrixWorld(true) + camera.updateProjectionMatrix() + let worst = 0 + for (let i = 0; i < 8; i += 1) { + corner.set( + i & 1 ? box.max.x : box.min.x, + i & 2 ? box.max.y : box.min.y, + i & 4 ? box.max.z : box.min.z, + ).project(camera) + worst = Math.max(worst, Math.abs(corner.x), Math.abs(corner.y)) + } + if (worst <= 0) break + // 0.9 of the frame, so nothing sits on the edge and a shadow has room. + distance *= worst / 0.9 + } + camera.position.copy(sphere.center).addScaledVector(direction, distance) camera.lookAt(sphere.center) camera.updateMatrixWorld(true) + camera.updateProjectionMatrix() return { camera, floor: box.min.y } } @@ -356,6 +394,15 @@ function renderOne( patch?: Record, /** Pre-rotation of the model around the Y axis (radians). For turntables. */ spin = 0, + /** + * Frame height, when it is not the width. + * + * Everything here is square because a contact sheet is a grid, but a social + * card is 1.91:1 and cropping a square down to it either loses the model or + * leaves it small. `newFrame` and `frameCamera` both took a width and a + * height already; only this function was insisting they be the same. + */ + tall = size, ): Frame { const entry = CATALOG[id] if (!entry) throw new Error(`not in catalog: ${id}`) @@ -368,8 +415,8 @@ function renderOne( // stay in a fixed direction, so turntable frames compare one to one. built.root.rotation.y = spin const triangles = collect(built.root) - const frame = newFrame(size, size) - const { camera, floor } = frameCamera(built.root, size, size) + const frame = newFrame(size, tall) + const { camera, floor } = frameCamera(built.root, size, tall) contactShadow(frame, camera, triangles, floor) raster(frame, camera, triangles) built.dispose() @@ -387,6 +434,7 @@ const flag = (name: string): string | undefined => { const outDir = flag('out') ?? 'renders' const one = flag('one') const size = Number(flag('size') ?? (one ? 720 : 300)) +const tall = Number(flag('height') ?? size) const only = flag('ids')?.split(',') @@ -453,7 +501,7 @@ const ids = one ? [one] const frames = new Map() for (const id of ids) { - const frame = renderOne(id, size) + const frame = renderOne(id, size, undefined, 0, tall) frames.set(id, frame) await writeFile(`${outDir}/${id}.png`, encodePng(frame)) console.log(` ${id}`) diff --git a/viewer.html b/viewer.html index 84e2888..bf9c98c 100644 --- a/viewer.html +++ b/viewer.html @@ -3,7 +3,51 @@ - @medieval-kit · procedural model viewer + @medieval-kit · procedural lowpoly medieval models + + + + + + + + + + + + + + + + + + + + +