diff --git a/models.lock.json b/models.lock.json index 85e6c33..4e52a4b 100644 --- a/models.lock.json +++ b/models.lock.json @@ -67,7 +67,7 @@ "address": "@medieval-kit/wooden-barrel", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.979Z", + "installedAt": "2026-08-23T08:46:30.620Z", "files": [ { "path": "src\\models\\medieval-kit\\wooden-barrel\\model.ts", @@ -82,7 +82,7 @@ "address": "@medieval-kit/core", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.954Z", + "installedAt": "2026-08-23T08:46:30.590Z", "files": [ { "path": "src\\models\\medieval-kit\\core\\geometry.ts", @@ -129,7 +129,7 @@ "address": "@medieval-kit/wooden-crate", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.984Z", + "installedAt": "2026-08-23T08:46:30.624Z", "files": [ { "path": "src\\models\\medieval-kit\\wooden-crate\\model.ts", @@ -144,7 +144,7 @@ "address": "@medieval-kit/kit", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.990Z", + "installedAt": "2026-08-23T08:46:30.630Z", "files": [], "dependencies": [] }, @@ -152,7 +152,7 @@ "address": "@medieval-kit/iron-anvil", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.963Z", + "installedAt": "2026-08-23T08:46:30.600Z", "files": [ { "path": "src\\models\\medieval-kit\\iron-anvil\\model.ts", @@ -167,7 +167,7 @@ "address": "@medieval-kit/wooden-bucket", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.982Z", + "installedAt": "2026-08-23T08:46:30.622Z", "files": [ { "path": "src\\models\\medieval-kit\\wooden-bucket\\model.ts", @@ -182,7 +182,7 @@ "address": "@medieval-kit/wooden-fence", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.985Z", + "installedAt": "2026-08-23T08:46:30.625Z", "files": [ { "path": "src\\models\\medieval-kit\\wooden-fence\\model.ts", @@ -197,7 +197,7 @@ "address": "@medieval-kit/wooden-hoe", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.986Z", + "installedAt": "2026-08-23T08:46:30.626Z", "files": [ { "path": "src\\models\\medieval-kit\\wooden-hoe\\model.ts", @@ -212,7 +212,7 @@ "address": "@medieval-kit/wooden-ladder", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.987Z", + "installedAt": "2026-08-23T08:46:30.627Z", "files": [ { "path": "src\\models\\medieval-kit\\wooden-ladder\\model.ts", @@ -227,7 +227,7 @@ "address": "@medieval-kit/wooden-pitchfork", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.988Z", + "installedAt": "2026-08-23T08:46:30.628Z", "files": [ { "path": "src\\models\\medieval-kit\\wooden-pitchfork\\model.ts", @@ -242,7 +242,7 @@ "address": "@medieval-kit/wooden-shovel", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.989Z", + "installedAt": "2026-08-23T08:46:30.629Z", "files": [ { "path": "src\\models\\medieval-kit\\wooden-shovel\\model.ts", @@ -257,7 +257,7 @@ "address": "@medieval-kit/wooden-stool", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.990Z", + "installedAt": "2026-08-23T08:46:30.630Z", "files": [ { "path": "src\\models\\medieval-kit\\wooden-stool\\model.ts", @@ -272,7 +272,7 @@ "address": "@medieval-kit/cart-wheel", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.957Z", + "installedAt": "2026-08-23T08:46:30.593Z", "files": [ { "path": "src\\models\\medieval-kit\\cart-wheel\\model.ts", @@ -287,7 +287,7 @@ "address": "@medieval-kit/log-pile", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.968Z", + "installedAt": "2026-08-23T08:46:30.606Z", "files": [ { "path": "src\\models\\medieval-kit\\log-pile\\model.ts", @@ -302,7 +302,7 @@ "address": "@medieval-kit/trestle-table", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.975Z", + "installedAt": "2026-08-23T08:46:30.616Z", "files": [ { "path": "src\\models\\medieval-kit\\trestle-table\\model.ts", @@ -317,7 +317,7 @@ "address": "@medieval-kit/wooden-chest", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.983Z", + "installedAt": "2026-08-23T08:46:30.623Z", "files": [ { "path": "src\\models\\medieval-kit\\wooden-chest\\model.ts", @@ -332,7 +332,7 @@ "address": "@medieval-kit/hay-bale", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.962Z", + "installedAt": "2026-08-23T08:46:30.599Z", "files": [ { "path": "src\\models\\medieval-kit\\hay-bale\\model.ts", @@ -347,7 +347,7 @@ "address": "@medieval-kit/pitch-torch", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.970Z", + "installedAt": "2026-08-23T08:46:30.608Z", "files": [ { "path": "src\\models\\medieval-kit\\pitch-torch\\model.ts", @@ -362,7 +362,7 @@ "address": "@medieval-kit/wooden-bench", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.980Z", + "installedAt": "2026-08-23T08:46:30.621Z", "files": [ { "path": "src\\models\\medieval-kit\\wooden-bench\\model.ts", @@ -377,7 +377,7 @@ "address": "@medieval-kit/linen-sack", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.967Z", + "installedAt": "2026-08-23T08:46:30.604Z", "files": [ { "path": "src\\models\\medieval-kit\\linen-sack\\model.ts", @@ -392,7 +392,7 @@ "address": "@medieval-kit/oak-tankard", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.969Z", + "installedAt": "2026-08-23T08:46:30.607Z", "files": [ { "path": "src\\models\\medieval-kit\\oak-tankard\\model.ts", @@ -407,7 +407,7 @@ "address": "@medieval-kit/straw-broom", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.973Z", + "installedAt": "2026-08-23T08:46:30.613Z", "files": [ { "path": "src\\models\\medieval-kit\\straw-broom\\model.ts", @@ -422,7 +422,7 @@ "address": "@medieval-kit/bronze-bell", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.956Z", + "installedAt": "2026-08-23T08:46:30.592Z", "files": [ { "path": "src\\models\\medieval-kit\\bronze-bell\\model.ts", @@ -437,7 +437,7 @@ "address": "@medieval-kit/iron-lantern", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.965Z", + "installedAt": "2026-08-23T08:46:30.602Z", "files": [ { "path": "src\\models\\medieval-kit\\iron-lantern\\model.ts", @@ -452,7 +452,7 @@ "address": "@medieval-kit/tavern-sign", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.974Z", + "installedAt": "2026-08-23T08:46:30.615Z", "files": [ { "path": "src\\models\\medieval-kit\\tavern-sign\\model.ts", @@ -467,7 +467,7 @@ "address": "@medieval-kit/coin-pouch", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.958Z", + "installedAt": "2026-08-23T08:46:30.594Z", "files": [ { "path": "src\\models\\medieval-kit\\coin-pouch\\model.ts", @@ -482,7 +482,7 @@ "address": "@medieval-kit/glass-phial", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.960Z", + "installedAt": "2026-08-23T08:46:30.597Z", "files": [ { "path": "src\\models\\medieval-kit\\glass-phial\\model.ts", @@ -497,7 +497,7 @@ "address": "@medieval-kit/leather-book", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.966Z", + "installedAt": "2026-08-23T08:46:30.603Z", "files": [ { "path": "src\\models\\medieval-kit\\leather-book\\model.ts", @@ -512,7 +512,7 @@ "address": "@medieval-kit/wicker-basket", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.978Z", + "installedAt": "2026-08-23T08:46:30.618Z", "files": [ { "path": "src\\models\\medieval-kit\\wicker-basket\\model.ts", @@ -527,7 +527,7 @@ "address": "@medieval-kit/post-mill", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.971Z", + "installedAt": "2026-08-23T08:46:30.609Z", "files": [ { "path": "src\\models\\medieval-kit\\post-mill\\model.ts", @@ -542,7 +542,7 @@ "address": "@medieval-kit/stone-well", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.972Z", + "installedAt": "2026-08-23T08:46:30.612Z", "files": [ { "path": "src\\models\\medieval-kit\\stone-well\\model.ts", @@ -557,7 +557,7 @@ "address": "@medieval-kit/hand-cart", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.961Z", + "installedAt": "2026-08-23T08:46:30.598Z", "files": [ { "path": "src\\models\\medieval-kit\\hand-cart\\model.ts", @@ -572,7 +572,7 @@ "address": "@medieval-kit/iron-cauldron", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.964Z", + "installedAt": "2026-08-23T08:46:30.601Z", "files": [ { "path": "src\\models\\medieval-kit\\iron-cauldron\\model.ts", @@ -587,7 +587,7 @@ "address": "@medieval-kit/vegetables", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-23T08:18:39.977Z", + "installedAt": "2026-08-23T08:46:30.617Z", "files": [ { "path": "src\\models\\medieval-kit\\vegetables\\model.ts", @@ -597,6 +597,36 @@ "dependencies": [ "three@>=0.185.0" ] + }, + "@medieval-kit/forge-hearth": { + "address": "@medieval-kit/forge-hearth", + "source": "file:my-registry/dist/registry.json", + "version": "workspace", + "installedAt": "2026-08-23T08:46:30.595Z", + "files": [ + { + "path": "src\\models\\medieval-kit\\forge-hearth\\model.ts", + "sourceHash": "7166bb0f728b7b1945b64f18565d48dd60ecf47be4e9d4048874e358e96abcbe" + } + ], + "dependencies": [ + "three@>=0.185.0" + ] + }, + "@medieval-kit/round-shield": { + "address": "@medieval-kit/round-shield", + "source": "file:my-registry/dist/registry.json", + "version": "workspace", + "installedAt": "2026-08-23T08:46:30.611Z", + "files": [ + { + "path": "src\\models\\medieval-kit\\round-shield\\model.ts", + "sourceHash": "a2d1166c9f54298e8d0f7e556082cfbcd6cbb591b0c5f4782fa3a8284aa1e438" + } + ], + "dependencies": [ + "three@>=0.185.0" + ] } } } diff --git a/my-registry/meta.ts b/my-registry/meta.ts index 6d1732c..c286164 100644 --- a/my-registry/meta.ts +++ b/my-registry/meta.ts @@ -450,6 +450,42 @@ export const MODEL_META: Readonly> = { materialSlots: ['oak'], parts: ['rails', 'rungs'], }, + 'round-shield': { + title: 'Round Shield', + description: + 'Planked round shield with a rawhide rim and an iron boss, quartered in paint and leaning on its edge.', + category: 'Arms', + tags: ['medieval', 'lowpoly', 'arms', 'procedural'], + controls: { + radius: { type: 'number', label: 'Radius', min: 0.2, max: 0.6, step: 0.01, unit: 'm' }, + planks: { type: 'number', label: 'Planks', min: 3, max: 15, step: 1 }, + thickness: { type: 'number', label: 'Board thickness', min: 0.008, max: 0.04, step: 0.002, unit: 'm' }, + lean: { type: 'number', label: 'Lean back', min: 0, max: 0.6, step: 0.02, unit: 'rad' }, + hue: { type: 'number', label: 'Paint hue', min: 0, max: 1, step: 0.01 }, + rivets: { type: 'number', label: 'Boss rivets', min: 0, max: 16, step: 1 }, + seed: { type: 'number', label: 'Variation seed', min: 1, max: 64, step: 1 }, + }, + materialSlots: ['oak', 'leather', 'iron'], + parts: ['boards', 'rim', 'boss'], + }, + 'forge-hearth': { + title: 'Forge Hearth', + description: + "Smith's forge: a raised stone hearth with a chimney back, a bed of embers that can be put out, and a leather bellows.", + category: 'Smithy', + tags: ['medieval', 'lowpoly', 'smithy', 'procedural'], + controls: { + length: { type: 'number', label: 'Hearth length', min: 0.7, max: 1.8, step: 0.02, unit: 'm' }, + depth: { type: 'number', label: 'Hearth depth', min: 0.5, max: 1.3, step: 0.02, unit: 'm' }, + height: { type: 'number', label: 'Working height', min: 0.5, max: 1.1, step: 0.02, unit: 'm' }, + chimney: { type: 'number', label: 'Chimney height', min: 0.3, max: 2, step: 0.05, unit: 'm' }, + kerbBlocks: { type: 'number', label: 'Kerb blocks', min: 8, max: 28, step: 1 }, + lit: { type: 'number', label: 'Lit', min: 0, max: 1, step: 1 }, + seed: { type: 'number', label: 'Variation seed', min: 1, max: 64, step: 1 }, + }, + materialSlots: ['stone', 'char', 'ember', 'oak', 'leather', 'iron'], + parts: ['hearth', 'fire', 'chimney', 'bellows'], + }, 'vegetables': { title: 'Vegetables', description: diff --git a/my-registry/models/forge-hearth/model.ts b/my-registry/models/forge-hearth/model.ts new file mode 100644 index 0000000..3d9d518 --- /dev/null +++ b/my-registry/models/forge-hearth/model.ts @@ -0,0 +1,379 @@ +/** + * @medieval-kit/forge-hearth + * + * A smith's forge: a raised stone hearth with a chimney back and a bellows + * beside it. + * + * The kit has had an anvil since early on and nothing to heat anything in. An + * anvil alone is a lump of iron; what makes a corner a smithy is the fire, and + * what makes the fire a forge rather than a hearth is the bellows blowing + * through the side of it. + * + * The parts follow the way it is used: + * + * - `hearth` — the stone block, waist high, with a kerb round its top. The + * height is the whole point: a smith works standing, and a fire + * on the floor is a cooking fire. + * - `fire` — charcoal and embers in the bed. `setLit` puts it out without + * taking the charcoal with it, the same split the cauldron uses. + * - `chimney` — the tapering stack over the back. It draws the smoke off the + * work, which is the only reason a smith can stand at the fire + * at all. + * - `bellows` — two boards with leather between them, a nozzle into the side + * of the hearth, and a lever above to work it by. + * + * The masonry is a compromise the well taught. Laying every course as separate + * blocks reads far better than a smooth box, and it also costs 12 triangles a + * block — at four faces and four courses that is most of the budget. So the + * blocks go where they are read, which is the kerb around the fire and the top + * course of the walls, and the body below is a roughened slab. + */ +import { type BufferGeometry } from 'three' + +import { + createKitModel, + dishedSheetGeometry, + createTinter, + jitter, + latheGeometry, + mergeColoured, + prismGeometry, + roughenGeometry, + taperedBoxGeometry, + type Level, + type SheetLevel, +} from '../core/index.ts' + +export interface ForgeHearthConfig { + /** Length of the hearth block along the smith's side (metres). */ + readonly length: number + /** Depth of the hearth block (metres). */ + readonly depth: number + /** Height of the block to the top of its kerb (metres). */ + readonly height: number + /** Height of the chimney above the hearth's top (metres). */ + readonly chimney: number + /** Blocks around the kerb of the fire. */ + readonly kerbBlocks: number + /** Whether the fire is lit (0/1). */ + readonly lit: number + readonly seed: number +} + +export const forgeHearthDefaults: ForgeHearthConfig = { + length: 1.15, + depth: 0.82, + height: 0.78, + chimney: 1.1, + kerbBlocks: 16, + lit: 1, + seed: 37, +} + +export type ForgeHearthParts = 'hearth' | 'fire' | 'chimney' | 'bellows' + +export interface ForgeHearthActions { + setLit(on: boolean): void + isLit(): boolean +} + +export function createModel(overrides: Partial = {}) { + let lit = true + + return createKitModel({ + id: 'forge-hearth', + defaults: forgeHearthDefaults, + slots: ['stone', 'char', 'ember', 'oak', 'leather', 'iron'], + + build: ({ config, random }) => { + const tint = createTinter(random) + const L = config.length + const D = config.depth + const H = config.height + // The forge stands on the ground rather than about its own centre: it has + // one face that matters and that face is the top, so measuring from the + // floor is what keeps the working height honest. + const floor = 0 + const wall = Math.min(L, D) * 0.17 + const kerbH = H * 0.14 + const bodyTop = H - kerbH + + // --- Hearth block -------------------------------------------------- + const masonry: BufferGeometry[] = [] + // Four wall slabs rather than four walls of blocks. The saving is what + // pays for the kerb, which is the course anyone actually looks at. + for (const [dx, dz, sx, sz] of [ + [0, -(D - wall) / 2, L, wall], + [0, (D - wall) / 2, L, wall], + [-(L - wall) / 2, 0, wall, D - wall * 2], + [(L - wall) / 2, 0, wall, D - wall * 2], + ] as const) { + masonry.push(taperedBoxGeometry( + [sx, sz], + [sx * 0.99, sz * 0.99], + bodyTop, + [dx, floor + bodyTop / 2, dz], + tint('stone', jitter(random, 0.07)), + )) + } + // The floor of the fire bed, so the hearth is not open to the ground. + masonry.push(taperedBoxGeometry( + [L - wall * 1.6, D - wall * 1.6], + [L - wall * 1.8, D - wall * 1.8], + wall * 0.7, + [0, floor + bodyTop - wall * 0.3, 0], + tint('stone', -0.05), + )) + + // Kerb: individual blocks all the way round the top, staggered where the + // corners meet so the ring does not read as four sticks. + const kerbCount = Math.max(8, Math.round(config.kerbBlocks)) + const halfL = L / 2 - wall / 2 + const halfD = D / 2 - wall / 2 + const perimeter = (halfL + halfD) * 4 + for (let i = 0; i < kerbCount; i += 1) { + const t = ((i + 0.5) / kerbCount) * perimeter + // Walk the rectangle rather than a circle: a forge is square and the + // blocks have to turn its corners. + let x = 0 + let z = 0 + let along: 'x' | 'z' = 'x' + let s = t + if (s < halfL * 2) { x = -halfL + s; z = -halfD; along = 'x' } + else if ((s -= halfL * 2) < halfD * 2) { x = halfL; z = -halfD + s; along = 'z' } + else if ((s -= halfD * 2) < halfL * 2) { x = halfL - s; z = halfD; along = 'x' } + else { s -= halfL * 2; x = -halfL; z = halfD - s; along = 'z' } + const run = perimeter / kerbCount + const size: [number, number] = along === 'x' + ? [run * (1.05 + jitter(random, 0.06)), wall * (1.02 + jitter(random, 0.05))] + : [wall * (1.02 + jitter(random, 0.05)), run * (1.05 + jitter(random, 0.06))] + masonry.push(taperedBoxGeometry( + size, + [size[0] * 0.94, size[1] * 0.94], + kerbH * (1.04 + jitter(random, 0.05)), + [x, floor + bodyTop + kerbH * 0.46, z], + tint('stone', 0.03 + jitter(random, 0.08)), + )) + } + const hearth = mergeColoured(masonry) + roughenGeometry(hearth, wall * 0.05, { salt: 23 }) + + // --- Fire ------------------------------------------------------------ + const bedR = Math.min(L, D) * 0.5 - wall * 0.9 + const bedY = floor + bodyTop + const coal = latheGeometry( + [ + { y: bedY, radius: bedR * 1.02 }, + { y: bedY + kerbH * 0.5, radius: bedR * 0.86 }, + ] as Level[], + // Darker than the stone by a wide margin. At +0.03 the charcoal came + // out the same value as the masonry around it and the fire bed read as + // another slab of rock. + 9, [0, 0, 0], tint('char', -0.02), + ) + coal.scale(L / (Math.min(L, D)), 1, D / (Math.min(L, D))) + + const embers: BufferGeometry[] = [] + for (let i = 0; i < 16; i += 1) { + const a = i * 2.399963 + const r = bedR * 0.7 * Math.sqrt((i + 0.4) / 16) + const size = bedR * (0.12 + random() * 0.09) + // Bedded INTO the charcoal, with no bottom face and no two of them + // lying flat. + // + // Sitting on the bed at one height, every ember presented a horizontal + // cap in the same plane as the charcoal's own top face, and the z-fight + // check found them at once. Burying the base removes the face that was + // colliding -- it is inside the fire and nobody can see it -- and the + // tilt means what is left is not horizontal to begin with. Coals in a + // fire are half-sunk and lying every way anyhow. + const lump = prismGeometry( + size, size * 0.55, size * 0.7, 5, [0, 0, 0], + tint(i % 3 === 0 ? 'emberTip' : 'ember', jitter(random, 0.05), 0.4), + { capBottom: false }, + ) + lump.rotateX(jitter(random, 0.45)) + lump.rotateZ(jitter(random, 0.45)) + lump.rotateY(random() * Math.PI * 2) + lump.translate( + Math.sin(a) * r * (L / Math.min(L, D)), + bedY + kerbH * 0.3, + Math.cos(a) * r * (D / Math.min(L, D)), + ) + embers.push(lump) + } + + // --- Chimney ---------------------------------------------------------- + // Rises from the BACK of the hearth and tapers. Its foot reaches down + // inside the kerb rather than standing on it, so the joint is an overlap. + // A stack, not a post. At 1.5 wall thicknesses deep it was a slab seen + // edge-on from most angles; a forge chimney is a chest of masonry that + // gathers the whole back of the hearth. + const stackW = L * 0.66 + const stackD = D * 0.42 + const stack = taperedBoxGeometry( + [stackW, stackD], + [stackW * 0.55, stackD * 0.62], + config.chimney, + [0, floor + bodyTop + config.chimney / 2 - kerbH * 0.3, -(D / 2 - stackD * 0.46)], + tint('stone', -0.03), + ) + roughenGeometry(stack, wall * 0.05, { salt: 29 }) + const cap = taperedBoxGeometry( + [stackW * 0.68, stackD * 0.72], + [stackW * 0.66, stackD * 0.7], + wall * 0.5, + [0, floor + bodyTop + config.chimney - kerbH * 0.3, -(D / 2 - stackD * 0.46)], + tint('stone', 0.06), + ) + + // --- Bellows ---------------------------------------------------------- + // On the +X side, blowing in through the wall. Two boards with the + // leather between them, a nozzle, a post and the lever that works it. + const bx = L / 2 + const bellowsL = D * 0.86 + const bellowsW = D * 0.55 + const bellowsY = floor + bodyTop - H * 0.14 + const timber: BufferGeometry[] = [] + const hide: BufferGeometry[] = [] + const metal: BufferGeometry[] = [] + + // The boards and the leather are SHAPED PLATES, laid flat. + // + // They were built as tapered boxes and turned into place with two + // rotations, and the pair of them put the thickness along Z and the + // length along Y -- the boards ended up standing on edge, both in nearly + // the same plane, with every face coplanar with its neighbour. The + // z-fight check found them immediately and kept finding them while I + // rearranged the leather, because the leather was never what was wrong. + // + // `dishedSheetGeometry` is the right tool and its own docstring says so: + // a plate whose width varies along its length, which is what a bellows + // board is. It builds in the XY plane with its thickness along Z, so one + // tilt lays it flat and one turn points it out from the hearth. + const lay = (g: BufferGeometry): BufferGeometry => { + g.rotateX(-Math.PI / 2) + g.rotateY(-Math.PI / 2) + return g + } + // `w` scales the LENGTH as well as the width, and it has to. + // + // Sized only across, every plate -- both boards, the gusset and both ribs + // -- ran to exactly the same back edge, and five end caps in one plane is + // five coplanar faces. Scaling both ways staggers those edges and is also + // the truer shape: a rib that swells wider swells longer with it. + const teardrop = (w: number, t: number): SheetLevel[] => [ + { y: 0, halfWidth: bellowsW * 0.15 * w, thickness: t, curve: 0 }, + { y: bellowsL * 0.28 * w, halfWidth: bellowsW * 0.4 * w, thickness: t, curve: 0 }, + { y: bellowsL * 0.72 * w, halfWidth: bellowsW * 0.5 * w, thickness: t * 0.95, curve: 0 }, + { y: bellowsL * w, halfWidth: bellowsW * 0.38 * w, thickness: t * 0.9, curve: 0 }, + ] + + for (const [board, at] of [[0, -1], [1, 1]] as const) { + timber.push(lay(dishedSheetGeometry( + teardrop(board === 0 ? 1 : 0.97, D * 0.035), 5, + tint('oak', (board === 0 ? 0.03 : -0.04) + jitter(random, 0.04)), + // The nose is staggered as well as the tail. Scaling the plates only + // spread their BACK edges; they all still began at y = 0, so after + // being laid down every one of them started at the same x and the five + // nose caps were coplanar in their turn. A few millimetres apart is + // invisible and is the difference between five faces in a plane and + // none. + )).translate(bx + board * D * 0.014, bellowsY + at * D * 0.11, 0)) + } + + // One gusset of leather spanning board to board, and two ribs on it at + // different sizes so no two of their faces can line up. + hide.push(lay(dishedSheetGeometry( + teardrop(0.94, D * 0.2), 5, tint('leather', -0.06, 0.9), + )).translate(bx + D * 0.032, bellowsY, 0)) + for (const [i, at] of [-0.34, 0.34].entries()) { + hide.push(lay(dishedSheetGeometry( + teardrop(1.04 + i * 0.07, D * 0.035), 5, + tint('leather', -0.01 + i * 0.04, 0.9), + )).translate(bx + D * (0.022 + i * 0.019), bellowsY + at * D * 0.11, 0)) + } + + // Nozzle, through the hearth wall. + metal.push(latheGeometry( + [ + { y: -bellowsL * 0.16, radius: D * 0.055 }, + { y: bellowsL * 0.06, radius: D * 0.038 }, + { y: bellowsL * 0.2, radius: D * 0.028 }, + ] as Level[], + 8, [0, 0, 0], tint('iron', 0.02, 0.7), + ).rotateZ(Math.PI / 2).translate(bx - wall * 0.4, bellowsY, 0)) + + // Post and lever. + const postX = bx + bellowsL * 0.16 + const postTop = floor + bodyTop + H * 0.42 + timber.push(taperedBoxGeometry( + [D * 0.07, D * 0.07], + [D * 0.06, D * 0.06], + postTop - floor, + [postX, (floor + postTop) / 2, 0], + tint('oak', -0.06), + )) + // The lever, laid along X with ONE rotation. + // + // Chaining rotateZ and rotateY to get it there put its length along Z -- + // running across the bellows instead of over it, reaching the post at + // neither end, which the support check reported as a floating bar. This + // is the third time in this one model that a two-rotation chain has put + // a piece on the wrong axis, so it is built the plain way: the helper + // gives a bar along Y, and a single turn about Z lays it along X with + // its tilt folded into the same angle. + const leverLength = bellowsL * 1.02 + const leverTilt = 0.26 + const lever = taperedBoxGeometry( + [D * 0.055, D * 0.05], + [D * 0.045, D * 0.045], + leverLength, + [0, 0, 0], + tint('oak', 0.05), + ) + lever.rotateZ(Math.PI / 2 + leverTilt) + // Pivots ON the post and reaches back over the bellows' broad end. + lever.translate( + postX + Math.cos(leverTilt) * leverLength * 0.46, + postTop - D * 0.06 - Math.sin(leverTilt) * leverLength * 0.46, + 0, + ) + timber.push(lever) + + return { + hearth: { slot: 'stone' as const, geometry: hearth }, + fire: { + slot: 'char' as const, + geometry: coal, + extras: [{ slot: 'ember' as const, geometry: mergeColoured(embers) }], + }, + chimney: { slot: 'stone' as const, geometry: mergeColoured([stack, cap]) }, + bellows: { + slot: 'oak' as const, + geometry: mergeColoured(timber), + extras: [ + { slot: 'leather' as const, geometry: mergeColoured(hide) }, + { slot: 'iron' as const, geometry: mergeColoured(metal) }, + ], + }, + } + }, + + actions: ({ parts, getConfig }) => { + lit = getConfig().lit >= 0.5 + // The embers are the fire part's SECOND body; hiding the part itself + // would take the charcoal with it and leave an empty stone box. + const glow = parts.fire.anchor.children[1] + if (glow) glow.visible = lit + return { + setLit: (on) => { + lit = on + const body = parts.fire.anchor.children[1] + if (body) body.visible = on + }, + isLit: () => lit, + } + }, + }, overrides) +} diff --git a/my-registry/models/round-shield/model.ts b/my-registry/models/round-shield/model.ts new file mode 100644 index 0000000..566b561 --- /dev/null +++ b/my-registry/models/round-shield/model.ts @@ -0,0 +1,221 @@ +/** + * @medieval-kit/round-shield + * + * A planked round shield with a rawhide rim and an iron boss. + * + * The kit had no arms or armour at all, which for a medieval catalogue is a + * whole category missing rather than a gap. This is the cheapest way in: a + * shield is the one piece of war gear that stands on its own in a scene — + * leaning on a wall, stacked by a door — without needing a person to hold it. + * + * Three things make it a shield rather than a disc, and all three are + * structural rather than decorative: + * + * - It is BOARDS, not a plate. A round shield is planks butted edge to edge + * and held by the rim and the boss; the plank lines are the first thing + * the eye finds and a smooth face reads as a lid. + * - The rim binds the ENDS of those planks. Without it the boards would + * split off one by one at the first blow, and without it in the model the + * silhouette is a wafer. + * - The boss is a dome over a HOLE. The hand goes behind it, gripping a bar + * across the back, so the boss is not ornament — it is the knuckle guard, + * and it is why the centre of the face is iron. + * + * The face is quartered because that is what a painted shield looks like and + * because it costs nothing: each plank is built in two halves anyway, so the + * upper and lower halves simply take different colours. `hue` turns both + * quarters together and keeps them opposite, so the slider always produces a + * pair that belongs together. + */ +import { Color, type BufferGeometry } from 'three' + +import { + bandGeometry, + boxGeometry, + createKitModel, + createTinter, + jitter, + latheGeometry, + mergeColoured, + prismGeometry, + taperedBoxGeometry, + type Level, +} from '../core/index.ts' + +export interface RoundShieldConfig { + /** Radius of the shield (metres). */ + readonly radius: number + /** Planks across the face. */ + readonly planks: number + /** Thickness of the boards (metres). */ + readonly thickness: number + /** How far it leans back from upright (radians). */ + readonly lean: number + /** Paint colour, 0–1 around the wheel. The second quarter sits opposite. */ + readonly hue: number + /** Rivets around the boss. */ + readonly rivets: number + readonly seed: number +} + +export const roundShieldDefaults: RoundShieldConfig = { + radius: 0.36, + planks: 9, + thickness: 0.016, + lean: 0.22, + hue: 0.02, + rivets: 10, + seed: 83, +} + +export type RoundShieldParts = 'boards' | 'rim' | 'boss' + +export function createModel(overrides: Partial = {}) { + return createKitModel({ + id: 'round-shield', + defaults: roundShieldDefaults, + slots: ['oak', 'leather', 'iron'], + + build: ({ config, random }) => { + const tint = createTinter(random) + const R = config.radius + const T = config.thickness + const planks = Math.max(3, Math.round(config.planks)) + const hue = ((config.hue % 1) + 1) % 1 + + const paint = (h: number, dark: number): Color => + new Color().setHSL( + (h + jitter(random, 0.01) + 1) % 1, + 0.42 + jitter(random, 0.06), + dark + jitter(random, 0.04), + ) + + // --- Boards --------------------------------------------------------- + // Each plank is built in two halves so the face can be quartered without + // any extra geometry: the split the paint needs is a split the boards + // already have to make anyway. + const boards: BufferGeometry[] = [] + const step = (R * 2) / planks + const upper = paint(hue, 0.34) + const lower = paint((hue + 0.55) % 1, 0.3) + + for (let i = 0; i < planks; i += 1) { + const x = -R + step * (i + 0.5) + // The plank is a chord of the circle, so its length is set by how far + // out it sits. Squaring off every plank at full width would give a + // square with a rim drawn on it. + const halfSpan = Math.sqrt(Math.max(0, R * R - Math.pow(Math.abs(x) + step * 0.5, 2))) + if (halfSpan <= step * 0.2) continue + for (const half of [-1, 1]) { + // The two halves of one plank are quartered on the DIAGONAL: the + // left-hand planks take one colour above and the other below, and + // the right-hand ones the reverse. That is what makes a quartered + // shield read as quartered rather than as banded. + const left = x < 0 + const colour = (half < 0) === left ? upper : lower + boards.push(taperedBoxGeometry( + [step * 0.97, T], + [step * 0.94, T * 0.92], + halfSpan, + [x, half * halfSpan * 0.5, 0], + new Color(colour), + )) + } + } + + // --- Rim ------------------------------------------------------------ + // A rawhide binding folded over the edge. It is a band whose thickness + // reaches INSIDE the boards' own radius, so it grips them rather than + // sitting against their ends. + const rim = bandGeometry( + R * 1.02, 0, T * 2.6, R * 0.055, planks * 3, + tint('leather', -0.04, 0.9), { inner: true }, + ) + rim.rotateX(Math.PI / 2) + + // --- Boss ----------------------------------------------------------- + const bossR = R * 0.26 + const iron: BufferGeometry[] = [] + // The dome. Its back is open, because behind it is the hole the hand + // goes into, and a cap there would be a face nobody can see. + iron.push(latheGeometry( + [ + { y: 0, radius: bossR * 0.98 }, + { y: bossR * 0.42, radius: bossR * 0.86 }, + { y: bossR * 0.78, radius: bossR * 0.54 }, + { y: bossR * 0.94, radius: bossR * 0.16 }, + ] as Level[], + 11, [0, 0, 0], tint('iron', 0.05, 0.7), { capBottom: false }, + // +90, not -90. The dome is turned about Y, and -90 sends its point + // to -Z -- into the back of the shield, where the only thing visible + // from the front was the open rim of it. That is why the boss read as a + // flat black ring. + ).rotateX(Math.PI / 2).translate(0, 0, T * 0.5)) + // The flange it is riveted through. + iron.push(bandGeometry( + bossR * 1.34, 0, T * 1.1, bossR * 0.36, 11, + tint('iron', -0.02, 0.7), { inner: true }, + ).rotateX(Math.PI / 2).translate(0, 0, T * 0.55)) + + const rivets = Math.max(0, Math.round(config.rivets)) + for (let i = 0; i < rivets; i += 1) { + const a = (i / rivets) * Math.PI * 2 + 0.2 + // Flared outwards, like the barrel's: a stud that tapers away from the + // surface tilts its side normals back towards it, which the radial + // check reads as reversed winding, and a hammered rivet flares anyway. + const stud = prismGeometry( + R * 0.016, R * 0.022, T * 1.4, 4, [0, 0, 0], + tint('iron', 0.09, 0.6), { capBottom: false }, + ) + stud.rotateX(-Math.PI / 2) + iron.push(stud.translate( + Math.sin(a) * bossR * 1.16, + Math.cos(a) * bossR * 1.16, + T * 0.8, + )) + } + + // --- Back ------------------------------------------------------------ + // The grip bar across the hole, and two battens holding the planks. Cheap + // and invisible from the front, but a shield seen from behind with + // nothing to hold is worse than one with no back at all. + const back: BufferGeometry[] = [] + back.push(boxGeometry( + [R * 1.2, R * 0.09, T * 1.6], + [0, 0, -T * 1.1], + new Color(tint('oak', -0.1)), + )) + for (const at of [-0.46, 0.46]) { + back.push(boxGeometry( + [R * 1.5, R * 0.07, T * 1.2], + [0, at * R, -T * 0.9], + new Color(tint('oak', -0.14)), + )) + } + + const face = mergeColoured([...boards, ...back]) + const boss = mergeColoured(iron) + + // Leaned back from UPRIGHT, not tipped up from flat. + // + // Everything is authored in the XY plane facing +Z, which is already a + // shield standing on its edge. Rotating by -90 degrees and then adding + // the lean laid it face-up on the floor with a slight tilt, which is a + // shield somebody dropped. The lean is the whole rotation. + for (const g of [face, rim, boss]) g.rotateX(config.lean) + // Set down by measuring, not by trigonometry: the boss stands proud of + // the face and the rim wraps past it, so which piece is lowest depends on + // the lean and is not worth deriving. + const all = mergeColoured([face, rim, boss]) + all.computeBoundingBox() + const drop = all.boundingBox?.min.y ?? 0 + for (const g of [face, rim, boss]) g.translate(0, -drop, 0) + + return { + boards: { slot: 'oak' as const, geometry: face }, + rim: { slot: 'leather' as const, geometry: rim }, + boss: { slot: 'iron' as const, geometry: boss }, + } + }, + }, overrides) +} diff --git a/scripts/verify-model.ts b/scripts/verify-model.ts index 4435b8a..355fc72 100644 --- a/scripts/verify-model.ts +++ b/scripts/verify-model.ts @@ -54,6 +54,8 @@ import { createModel as createWell } from '@/models/medieval-kit/stone-well/mode import { createModel as createCauldron } from '@/models/medieval-kit/iron-cauldron/model.ts' import { createModel as createCart } from '@/models/medieval-kit/hand-cart/model.ts' import { createModel as createVeg } from '@/models/medieval-kit/vegetables/model.ts' +import { createModel as createShield } from '@/models/medieval-kit/round-shield/model.ts' +import { createModel as createForge } from '@/models/medieval-kit/forge-hearth/model.ts' const failures: string[] = [] function expect(label: string, condition: boolean): void { @@ -308,6 +310,14 @@ const CASES: readonly Case[] = [ { id: 'wooden-ladder', make: as(createLadder), patch: { rungCount: 12 }, parts: 2, ownSlot: 'oak', borrowSlot: 'oak', closed: true, variants: [{ rungCount: 3 }, { taper: 0 }] , maxSize: [0.6, 2.4, 0.12] }, + { id: 'round-shield', make: as(createShield), patch: { planks: 13, rivets: 14 }, + parts: 3, ownSlot: 'leather', borrowSlot: 'oak', + variants: [{ planks: 3 }, { lean: 0 }, { rivets: 0 }], + maxSize: [1.3, 1.3, 0.9] }, + { id: 'forge-hearth', make: as(createForge), patch: { kerbBlocks: 24, chimney: 1.6 }, + parts: 4, ownSlot: 'stone', borrowSlot: 'oak', + variants: [{ lit: 0 }, { kerbBlocks: 8 }, { chimney: 0.3 }], + maxSize: [2.6, 2.6, 1.6] }, { id: 'vegetables', make: as(createVeg), patch: { count: 22, kinds: 3, lead: 4 }, parts: 2, ownSlot: 'produce', borrowSlot: 'produce', variants: [{ kinds: 1 }, { count: 1 }, { lead: 3 }, { spread: 5 }], diff --git a/src/catalog.ts b/src/catalog.ts index 7740b47..d18cb4b 100644 --- a/src/catalog.ts +++ b/src/catalog.ts @@ -48,6 +48,8 @@ import { createModel as createWell } from '@/models/medieval-kit/stone-well/mode import { createModel as createCauldron } from '@/models/medieval-kit/iron-cauldron/model.ts' import { createModel as createCart } from '@/models/medieval-kit/hand-cart/model.ts' import { createModel as createVeg } from '@/models/medieval-kit/vegetables/model.ts' +import { createModel as createShield } from '@/models/medieval-kit/round-shield/model.ts' +import { createModel as createForge } from '@/models/medieval-kit/forge-hearth/model.ts' export interface ParamSpec { readonly key: string @@ -157,6 +159,8 @@ const FACTORIES: Readonly KitModel>> = { 'iron-cauldron': as(createCauldron), 'hand-cart': as(createCart), 'vegetables': as(createVeg), + 'round-shield': as(createShield), + 'forge-hearth': as(createForge), 'wooden-ladder': as(createLadder), 'wooden-fence': as(createFence), 'wooden-hoe': as(createHoe), @@ -311,7 +315,7 @@ const MEDIEVAL_ORDER = [ 'wooden-hoe', 'wooden-shovel', 'wooden-pitchfork', // Last, because it is the landmark: a 6 m mill after a row of hand tools is // the order that makes the scale of it land. - 'iron-cauldron', 'hand-cart', 'vegetables', + 'iron-cauldron', 'hand-cart', 'vegetables', 'round-shield', 'forge-hearth', 'stone-well', 'post-mill', ] as const diff --git a/src/models/medieval-kit/forge-hearth/model.ts b/src/models/medieval-kit/forge-hearth/model.ts new file mode 100644 index 0000000..3d9d518 --- /dev/null +++ b/src/models/medieval-kit/forge-hearth/model.ts @@ -0,0 +1,379 @@ +/** + * @medieval-kit/forge-hearth + * + * A smith's forge: a raised stone hearth with a chimney back and a bellows + * beside it. + * + * The kit has had an anvil since early on and nothing to heat anything in. An + * anvil alone is a lump of iron; what makes a corner a smithy is the fire, and + * what makes the fire a forge rather than a hearth is the bellows blowing + * through the side of it. + * + * The parts follow the way it is used: + * + * - `hearth` — the stone block, waist high, with a kerb round its top. The + * height is the whole point: a smith works standing, and a fire + * on the floor is a cooking fire. + * - `fire` — charcoal and embers in the bed. `setLit` puts it out without + * taking the charcoal with it, the same split the cauldron uses. + * - `chimney` — the tapering stack over the back. It draws the smoke off the + * work, which is the only reason a smith can stand at the fire + * at all. + * - `bellows` — two boards with leather between them, a nozzle into the side + * of the hearth, and a lever above to work it by. + * + * The masonry is a compromise the well taught. Laying every course as separate + * blocks reads far better than a smooth box, and it also costs 12 triangles a + * block — at four faces and four courses that is most of the budget. So the + * blocks go where they are read, which is the kerb around the fire and the top + * course of the walls, and the body below is a roughened slab. + */ +import { type BufferGeometry } from 'three' + +import { + createKitModel, + dishedSheetGeometry, + createTinter, + jitter, + latheGeometry, + mergeColoured, + prismGeometry, + roughenGeometry, + taperedBoxGeometry, + type Level, + type SheetLevel, +} from '../core/index.ts' + +export interface ForgeHearthConfig { + /** Length of the hearth block along the smith's side (metres). */ + readonly length: number + /** Depth of the hearth block (metres). */ + readonly depth: number + /** Height of the block to the top of its kerb (metres). */ + readonly height: number + /** Height of the chimney above the hearth's top (metres). */ + readonly chimney: number + /** Blocks around the kerb of the fire. */ + readonly kerbBlocks: number + /** Whether the fire is lit (0/1). */ + readonly lit: number + readonly seed: number +} + +export const forgeHearthDefaults: ForgeHearthConfig = { + length: 1.15, + depth: 0.82, + height: 0.78, + chimney: 1.1, + kerbBlocks: 16, + lit: 1, + seed: 37, +} + +export type ForgeHearthParts = 'hearth' | 'fire' | 'chimney' | 'bellows' + +export interface ForgeHearthActions { + setLit(on: boolean): void + isLit(): boolean +} + +export function createModel(overrides: Partial = {}) { + let lit = true + + return createKitModel({ + id: 'forge-hearth', + defaults: forgeHearthDefaults, + slots: ['stone', 'char', 'ember', 'oak', 'leather', 'iron'], + + build: ({ config, random }) => { + const tint = createTinter(random) + const L = config.length + const D = config.depth + const H = config.height + // The forge stands on the ground rather than about its own centre: it has + // one face that matters and that face is the top, so measuring from the + // floor is what keeps the working height honest. + const floor = 0 + const wall = Math.min(L, D) * 0.17 + const kerbH = H * 0.14 + const bodyTop = H - kerbH + + // --- Hearth block -------------------------------------------------- + const masonry: BufferGeometry[] = [] + // Four wall slabs rather than four walls of blocks. The saving is what + // pays for the kerb, which is the course anyone actually looks at. + for (const [dx, dz, sx, sz] of [ + [0, -(D - wall) / 2, L, wall], + [0, (D - wall) / 2, L, wall], + [-(L - wall) / 2, 0, wall, D - wall * 2], + [(L - wall) / 2, 0, wall, D - wall * 2], + ] as const) { + masonry.push(taperedBoxGeometry( + [sx, sz], + [sx * 0.99, sz * 0.99], + bodyTop, + [dx, floor + bodyTop / 2, dz], + tint('stone', jitter(random, 0.07)), + )) + } + // The floor of the fire bed, so the hearth is not open to the ground. + masonry.push(taperedBoxGeometry( + [L - wall * 1.6, D - wall * 1.6], + [L - wall * 1.8, D - wall * 1.8], + wall * 0.7, + [0, floor + bodyTop - wall * 0.3, 0], + tint('stone', -0.05), + )) + + // Kerb: individual blocks all the way round the top, staggered where the + // corners meet so the ring does not read as four sticks. + const kerbCount = Math.max(8, Math.round(config.kerbBlocks)) + const halfL = L / 2 - wall / 2 + const halfD = D / 2 - wall / 2 + const perimeter = (halfL + halfD) * 4 + for (let i = 0; i < kerbCount; i += 1) { + const t = ((i + 0.5) / kerbCount) * perimeter + // Walk the rectangle rather than a circle: a forge is square and the + // blocks have to turn its corners. + let x = 0 + let z = 0 + let along: 'x' | 'z' = 'x' + let s = t + if (s < halfL * 2) { x = -halfL + s; z = -halfD; along = 'x' } + else if ((s -= halfL * 2) < halfD * 2) { x = halfL; z = -halfD + s; along = 'z' } + else if ((s -= halfD * 2) < halfL * 2) { x = halfL - s; z = halfD; along = 'x' } + else { s -= halfL * 2; x = -halfL; z = halfD - s; along = 'z' } + const run = perimeter / kerbCount + const size: [number, number] = along === 'x' + ? [run * (1.05 + jitter(random, 0.06)), wall * (1.02 + jitter(random, 0.05))] + : [wall * (1.02 + jitter(random, 0.05)), run * (1.05 + jitter(random, 0.06))] + masonry.push(taperedBoxGeometry( + size, + [size[0] * 0.94, size[1] * 0.94], + kerbH * (1.04 + jitter(random, 0.05)), + [x, floor + bodyTop + kerbH * 0.46, z], + tint('stone', 0.03 + jitter(random, 0.08)), + )) + } + const hearth = mergeColoured(masonry) + roughenGeometry(hearth, wall * 0.05, { salt: 23 }) + + // --- Fire ------------------------------------------------------------ + const bedR = Math.min(L, D) * 0.5 - wall * 0.9 + const bedY = floor + bodyTop + const coal = latheGeometry( + [ + { y: bedY, radius: bedR * 1.02 }, + { y: bedY + kerbH * 0.5, radius: bedR * 0.86 }, + ] as Level[], + // Darker than the stone by a wide margin. At +0.03 the charcoal came + // out the same value as the masonry around it and the fire bed read as + // another slab of rock. + 9, [0, 0, 0], tint('char', -0.02), + ) + coal.scale(L / (Math.min(L, D)), 1, D / (Math.min(L, D))) + + const embers: BufferGeometry[] = [] + for (let i = 0; i < 16; i += 1) { + const a = i * 2.399963 + const r = bedR * 0.7 * Math.sqrt((i + 0.4) / 16) + const size = bedR * (0.12 + random() * 0.09) + // Bedded INTO the charcoal, with no bottom face and no two of them + // lying flat. + // + // Sitting on the bed at one height, every ember presented a horizontal + // cap in the same plane as the charcoal's own top face, and the z-fight + // check found them at once. Burying the base removes the face that was + // colliding -- it is inside the fire and nobody can see it -- and the + // tilt means what is left is not horizontal to begin with. Coals in a + // fire are half-sunk and lying every way anyhow. + const lump = prismGeometry( + size, size * 0.55, size * 0.7, 5, [0, 0, 0], + tint(i % 3 === 0 ? 'emberTip' : 'ember', jitter(random, 0.05), 0.4), + { capBottom: false }, + ) + lump.rotateX(jitter(random, 0.45)) + lump.rotateZ(jitter(random, 0.45)) + lump.rotateY(random() * Math.PI * 2) + lump.translate( + Math.sin(a) * r * (L / Math.min(L, D)), + bedY + kerbH * 0.3, + Math.cos(a) * r * (D / Math.min(L, D)), + ) + embers.push(lump) + } + + // --- Chimney ---------------------------------------------------------- + // Rises from the BACK of the hearth and tapers. Its foot reaches down + // inside the kerb rather than standing on it, so the joint is an overlap. + // A stack, not a post. At 1.5 wall thicknesses deep it was a slab seen + // edge-on from most angles; a forge chimney is a chest of masonry that + // gathers the whole back of the hearth. + const stackW = L * 0.66 + const stackD = D * 0.42 + const stack = taperedBoxGeometry( + [stackW, stackD], + [stackW * 0.55, stackD * 0.62], + config.chimney, + [0, floor + bodyTop + config.chimney / 2 - kerbH * 0.3, -(D / 2 - stackD * 0.46)], + tint('stone', -0.03), + ) + roughenGeometry(stack, wall * 0.05, { salt: 29 }) + const cap = taperedBoxGeometry( + [stackW * 0.68, stackD * 0.72], + [stackW * 0.66, stackD * 0.7], + wall * 0.5, + [0, floor + bodyTop + config.chimney - kerbH * 0.3, -(D / 2 - stackD * 0.46)], + tint('stone', 0.06), + ) + + // --- Bellows ---------------------------------------------------------- + // On the +X side, blowing in through the wall. Two boards with the + // leather between them, a nozzle, a post and the lever that works it. + const bx = L / 2 + const bellowsL = D * 0.86 + const bellowsW = D * 0.55 + const bellowsY = floor + bodyTop - H * 0.14 + const timber: BufferGeometry[] = [] + const hide: BufferGeometry[] = [] + const metal: BufferGeometry[] = [] + + // The boards and the leather are SHAPED PLATES, laid flat. + // + // They were built as tapered boxes and turned into place with two + // rotations, and the pair of them put the thickness along Z and the + // length along Y -- the boards ended up standing on edge, both in nearly + // the same plane, with every face coplanar with its neighbour. The + // z-fight check found them immediately and kept finding them while I + // rearranged the leather, because the leather was never what was wrong. + // + // `dishedSheetGeometry` is the right tool and its own docstring says so: + // a plate whose width varies along its length, which is what a bellows + // board is. It builds in the XY plane with its thickness along Z, so one + // tilt lays it flat and one turn points it out from the hearth. + const lay = (g: BufferGeometry): BufferGeometry => { + g.rotateX(-Math.PI / 2) + g.rotateY(-Math.PI / 2) + return g + } + // `w` scales the LENGTH as well as the width, and it has to. + // + // Sized only across, every plate -- both boards, the gusset and both ribs + // -- ran to exactly the same back edge, and five end caps in one plane is + // five coplanar faces. Scaling both ways staggers those edges and is also + // the truer shape: a rib that swells wider swells longer with it. + const teardrop = (w: number, t: number): SheetLevel[] => [ + { y: 0, halfWidth: bellowsW * 0.15 * w, thickness: t, curve: 0 }, + { y: bellowsL * 0.28 * w, halfWidth: bellowsW * 0.4 * w, thickness: t, curve: 0 }, + { y: bellowsL * 0.72 * w, halfWidth: bellowsW * 0.5 * w, thickness: t * 0.95, curve: 0 }, + { y: bellowsL * w, halfWidth: bellowsW * 0.38 * w, thickness: t * 0.9, curve: 0 }, + ] + + for (const [board, at] of [[0, -1], [1, 1]] as const) { + timber.push(lay(dishedSheetGeometry( + teardrop(board === 0 ? 1 : 0.97, D * 0.035), 5, + tint('oak', (board === 0 ? 0.03 : -0.04) + jitter(random, 0.04)), + // The nose is staggered as well as the tail. Scaling the plates only + // spread their BACK edges; they all still began at y = 0, so after + // being laid down every one of them started at the same x and the five + // nose caps were coplanar in their turn. A few millimetres apart is + // invisible and is the difference between five faces in a plane and + // none. + )).translate(bx + board * D * 0.014, bellowsY + at * D * 0.11, 0)) + } + + // One gusset of leather spanning board to board, and two ribs on it at + // different sizes so no two of their faces can line up. + hide.push(lay(dishedSheetGeometry( + teardrop(0.94, D * 0.2), 5, tint('leather', -0.06, 0.9), + )).translate(bx + D * 0.032, bellowsY, 0)) + for (const [i, at] of [-0.34, 0.34].entries()) { + hide.push(lay(dishedSheetGeometry( + teardrop(1.04 + i * 0.07, D * 0.035), 5, + tint('leather', -0.01 + i * 0.04, 0.9), + )).translate(bx + D * (0.022 + i * 0.019), bellowsY + at * D * 0.11, 0)) + } + + // Nozzle, through the hearth wall. + metal.push(latheGeometry( + [ + { y: -bellowsL * 0.16, radius: D * 0.055 }, + { y: bellowsL * 0.06, radius: D * 0.038 }, + { y: bellowsL * 0.2, radius: D * 0.028 }, + ] as Level[], + 8, [0, 0, 0], tint('iron', 0.02, 0.7), + ).rotateZ(Math.PI / 2).translate(bx - wall * 0.4, bellowsY, 0)) + + // Post and lever. + const postX = bx + bellowsL * 0.16 + const postTop = floor + bodyTop + H * 0.42 + timber.push(taperedBoxGeometry( + [D * 0.07, D * 0.07], + [D * 0.06, D * 0.06], + postTop - floor, + [postX, (floor + postTop) / 2, 0], + tint('oak', -0.06), + )) + // The lever, laid along X with ONE rotation. + // + // Chaining rotateZ and rotateY to get it there put its length along Z -- + // running across the bellows instead of over it, reaching the post at + // neither end, which the support check reported as a floating bar. This + // is the third time in this one model that a two-rotation chain has put + // a piece on the wrong axis, so it is built the plain way: the helper + // gives a bar along Y, and a single turn about Z lays it along X with + // its tilt folded into the same angle. + const leverLength = bellowsL * 1.02 + const leverTilt = 0.26 + const lever = taperedBoxGeometry( + [D * 0.055, D * 0.05], + [D * 0.045, D * 0.045], + leverLength, + [0, 0, 0], + tint('oak', 0.05), + ) + lever.rotateZ(Math.PI / 2 + leverTilt) + // Pivots ON the post and reaches back over the bellows' broad end. + lever.translate( + postX + Math.cos(leverTilt) * leverLength * 0.46, + postTop - D * 0.06 - Math.sin(leverTilt) * leverLength * 0.46, + 0, + ) + timber.push(lever) + + return { + hearth: { slot: 'stone' as const, geometry: hearth }, + fire: { + slot: 'char' as const, + geometry: coal, + extras: [{ slot: 'ember' as const, geometry: mergeColoured(embers) }], + }, + chimney: { slot: 'stone' as const, geometry: mergeColoured([stack, cap]) }, + bellows: { + slot: 'oak' as const, + geometry: mergeColoured(timber), + extras: [ + { slot: 'leather' as const, geometry: mergeColoured(hide) }, + { slot: 'iron' as const, geometry: mergeColoured(metal) }, + ], + }, + } + }, + + actions: ({ parts, getConfig }) => { + lit = getConfig().lit >= 0.5 + // The embers are the fire part's SECOND body; hiding the part itself + // would take the charcoal with it and leave an empty stone box. + const glow = parts.fire.anchor.children[1] + if (glow) glow.visible = lit + return { + setLit: (on) => { + lit = on + const body = parts.fire.anchor.children[1] + if (body) body.visible = on + }, + isLit: () => lit, + } + }, + }, overrides) +} diff --git a/src/models/medieval-kit/round-shield/model.ts b/src/models/medieval-kit/round-shield/model.ts new file mode 100644 index 0000000..566b561 --- /dev/null +++ b/src/models/medieval-kit/round-shield/model.ts @@ -0,0 +1,221 @@ +/** + * @medieval-kit/round-shield + * + * A planked round shield with a rawhide rim and an iron boss. + * + * The kit had no arms or armour at all, which for a medieval catalogue is a + * whole category missing rather than a gap. This is the cheapest way in: a + * shield is the one piece of war gear that stands on its own in a scene — + * leaning on a wall, stacked by a door — without needing a person to hold it. + * + * Three things make it a shield rather than a disc, and all three are + * structural rather than decorative: + * + * - It is BOARDS, not a plate. A round shield is planks butted edge to edge + * and held by the rim and the boss; the plank lines are the first thing + * the eye finds and a smooth face reads as a lid. + * - The rim binds the ENDS of those planks. Without it the boards would + * split off one by one at the first blow, and without it in the model the + * silhouette is a wafer. + * - The boss is a dome over a HOLE. The hand goes behind it, gripping a bar + * across the back, so the boss is not ornament — it is the knuckle guard, + * and it is why the centre of the face is iron. + * + * The face is quartered because that is what a painted shield looks like and + * because it costs nothing: each plank is built in two halves anyway, so the + * upper and lower halves simply take different colours. `hue` turns both + * quarters together and keeps them opposite, so the slider always produces a + * pair that belongs together. + */ +import { Color, type BufferGeometry } from 'three' + +import { + bandGeometry, + boxGeometry, + createKitModel, + createTinter, + jitter, + latheGeometry, + mergeColoured, + prismGeometry, + taperedBoxGeometry, + type Level, +} from '../core/index.ts' + +export interface RoundShieldConfig { + /** Radius of the shield (metres). */ + readonly radius: number + /** Planks across the face. */ + readonly planks: number + /** Thickness of the boards (metres). */ + readonly thickness: number + /** How far it leans back from upright (radians). */ + readonly lean: number + /** Paint colour, 0–1 around the wheel. The second quarter sits opposite. */ + readonly hue: number + /** Rivets around the boss. */ + readonly rivets: number + readonly seed: number +} + +export const roundShieldDefaults: RoundShieldConfig = { + radius: 0.36, + planks: 9, + thickness: 0.016, + lean: 0.22, + hue: 0.02, + rivets: 10, + seed: 83, +} + +export type RoundShieldParts = 'boards' | 'rim' | 'boss' + +export function createModel(overrides: Partial = {}) { + return createKitModel({ + id: 'round-shield', + defaults: roundShieldDefaults, + slots: ['oak', 'leather', 'iron'], + + build: ({ config, random }) => { + const tint = createTinter(random) + const R = config.radius + const T = config.thickness + const planks = Math.max(3, Math.round(config.planks)) + const hue = ((config.hue % 1) + 1) % 1 + + const paint = (h: number, dark: number): Color => + new Color().setHSL( + (h + jitter(random, 0.01) + 1) % 1, + 0.42 + jitter(random, 0.06), + dark + jitter(random, 0.04), + ) + + // --- Boards --------------------------------------------------------- + // Each plank is built in two halves so the face can be quartered without + // any extra geometry: the split the paint needs is a split the boards + // already have to make anyway. + const boards: BufferGeometry[] = [] + const step = (R * 2) / planks + const upper = paint(hue, 0.34) + const lower = paint((hue + 0.55) % 1, 0.3) + + for (let i = 0; i < planks; i += 1) { + const x = -R + step * (i + 0.5) + // The plank is a chord of the circle, so its length is set by how far + // out it sits. Squaring off every plank at full width would give a + // square with a rim drawn on it. + const halfSpan = Math.sqrt(Math.max(0, R * R - Math.pow(Math.abs(x) + step * 0.5, 2))) + if (halfSpan <= step * 0.2) continue + for (const half of [-1, 1]) { + // The two halves of one plank are quartered on the DIAGONAL: the + // left-hand planks take one colour above and the other below, and + // the right-hand ones the reverse. That is what makes a quartered + // shield read as quartered rather than as banded. + const left = x < 0 + const colour = (half < 0) === left ? upper : lower + boards.push(taperedBoxGeometry( + [step * 0.97, T], + [step * 0.94, T * 0.92], + halfSpan, + [x, half * halfSpan * 0.5, 0], + new Color(colour), + )) + } + } + + // --- Rim ------------------------------------------------------------ + // A rawhide binding folded over the edge. It is a band whose thickness + // reaches INSIDE the boards' own radius, so it grips them rather than + // sitting against their ends. + const rim = bandGeometry( + R * 1.02, 0, T * 2.6, R * 0.055, planks * 3, + tint('leather', -0.04, 0.9), { inner: true }, + ) + rim.rotateX(Math.PI / 2) + + // --- Boss ----------------------------------------------------------- + const bossR = R * 0.26 + const iron: BufferGeometry[] = [] + // The dome. Its back is open, because behind it is the hole the hand + // goes into, and a cap there would be a face nobody can see. + iron.push(latheGeometry( + [ + { y: 0, radius: bossR * 0.98 }, + { y: bossR * 0.42, radius: bossR * 0.86 }, + { y: bossR * 0.78, radius: bossR * 0.54 }, + { y: bossR * 0.94, radius: bossR * 0.16 }, + ] as Level[], + 11, [0, 0, 0], tint('iron', 0.05, 0.7), { capBottom: false }, + // +90, not -90. The dome is turned about Y, and -90 sends its point + // to -Z -- into the back of the shield, where the only thing visible + // from the front was the open rim of it. That is why the boss read as a + // flat black ring. + ).rotateX(Math.PI / 2).translate(0, 0, T * 0.5)) + // The flange it is riveted through. + iron.push(bandGeometry( + bossR * 1.34, 0, T * 1.1, bossR * 0.36, 11, + tint('iron', -0.02, 0.7), { inner: true }, + ).rotateX(Math.PI / 2).translate(0, 0, T * 0.55)) + + const rivets = Math.max(0, Math.round(config.rivets)) + for (let i = 0; i < rivets; i += 1) { + const a = (i / rivets) * Math.PI * 2 + 0.2 + // Flared outwards, like the barrel's: a stud that tapers away from the + // surface tilts its side normals back towards it, which the radial + // check reads as reversed winding, and a hammered rivet flares anyway. + const stud = prismGeometry( + R * 0.016, R * 0.022, T * 1.4, 4, [0, 0, 0], + tint('iron', 0.09, 0.6), { capBottom: false }, + ) + stud.rotateX(-Math.PI / 2) + iron.push(stud.translate( + Math.sin(a) * bossR * 1.16, + Math.cos(a) * bossR * 1.16, + T * 0.8, + )) + } + + // --- Back ------------------------------------------------------------ + // The grip bar across the hole, and two battens holding the planks. Cheap + // and invisible from the front, but a shield seen from behind with + // nothing to hold is worse than one with no back at all. + const back: BufferGeometry[] = [] + back.push(boxGeometry( + [R * 1.2, R * 0.09, T * 1.6], + [0, 0, -T * 1.1], + new Color(tint('oak', -0.1)), + )) + for (const at of [-0.46, 0.46]) { + back.push(boxGeometry( + [R * 1.5, R * 0.07, T * 1.2], + [0, at * R, -T * 0.9], + new Color(tint('oak', -0.14)), + )) + } + + const face = mergeColoured([...boards, ...back]) + const boss = mergeColoured(iron) + + // Leaned back from UPRIGHT, not tipped up from flat. + // + // Everything is authored in the XY plane facing +Z, which is already a + // shield standing on its edge. Rotating by -90 degrees and then adding + // the lean laid it face-up on the floor with a slight tilt, which is a + // shield somebody dropped. The lean is the whole rotation. + for (const g of [face, rim, boss]) g.rotateX(config.lean) + // Set down by measuring, not by trigonometry: the boss stands proud of + // the face and the rim wraps past it, so which piece is lowest depends on + // the lean and is not worth deriving. + const all = mergeColoured([face, rim, boss]) + all.computeBoundingBox() + const drop = all.boundingBox?.min.y ?? 0 + for (const g of [face, rim, boss]) g.translate(0, -drop, 0) + + return { + boards: { slot: 'oak' as const, geometry: face }, + rim: { slot: 'leather' as const, geometry: rim }, + boss: { slot: 'iron' as const, geometry: boss }, + } + }, + }, overrides) +}