diff --git a/README.md b/README.md index 08bba8d..7dd3805 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ on an opaque package. This repository holds one such registry. ![The whole kit in one scene](media/kit.png) -37 models and one shared library, **32,316 triangles** for the whole kit — less +38 models and one shared library, **32,700 triangles** for the whole kit — less than a single medium-detail character. Every one is procedural source you own and edit, not an asset you load. @@ -137,9 +137,9 @@ viewer.html?showcase=60 # 30, 60 and 90 are offered ``` **60 s is the default**, and the reason is arithmetic: the tour splits its -running time equally between the models, so across 35 of them a 30 s run gives -each 0.86 seconds — a flyby, not a demonstration — while 60 s gives 1.7 s and -90 s gives 2.6 s. +running time equally between the models, so across 38 of them a 30 s run gives +each 0.79 seconds — a flyby, not a demonstration — while 60 s gives 1.6 s and +90 s gives 2.4 s. That first number moves every time a model is added, which is worth saying out loud rather than leaving as a stale sentence in a README: at 27 models, 60 s diff --git a/REFERENCE.md b/REFERENCE.md index ec7957c..a321812 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -79,7 +79,7 @@ src/viewer.ts · src/viewer.css the model viewer (WebGPU, sky, shadow) src/catalog.ts the viewer's catalog — DERIVED from meta.ts src/glb.ts GLB export (shared by viewer and CLI) src/models/scifi-kit/pressure-gauge/ the installed sci-fi model -src/models/medieval-kit/… your OWN installed models (37 models) +src/models/medieval-kit/… your OWN installed models (38 models) my-registry/ meta.ts the SINGLE source of catalog metadata build.ts the registry.json generator @@ -338,7 +338,7 @@ the same, the physical package changes. ## 5. Lowpoly medieval — yes, no problem This is not an assumption: `@medieval-kit` works, is validated and currently -contains **37 models + 1 lib**. The table is generated from the models +contains **38 models + 1 lib**. The table is generated from the models themselves with `bun scripts/catalog-table.ts` — a hand-written list goes stale on the first model you add, and in fact it had gone stale once. @@ -379,10 +379,11 @@ on the first model you add, and in fact it had gone stale once. | `stone-well` | Structure | 1376 | 4 | 1.88×2.00×1.28 | stone, oak, iron, cloth | | | `stone-trough` | Structure | 344 | 2 | 1.52×0.45×0.60 | stone, water | | | `grindstone` | Smithy | 568 | 4 | 1.24×0.95×0.70 | stone, oak, iron, water | | +| `market-stall` | Structure | 384 | 4 | 1.83×2.02×0.97 | oak, cloth | | | `post-mill` | Structure | 1672 | 4 | 6.60×7.23×6.98 | oak, iron | | | `oak-tree` | Nature | 2310 | 3 | 8.57×6.96×9.05 | oak, leaf | | -**32,316 triangles** in total. The whole kit in one scene has a budget smaller +**32,700 triangles** in total. The whole kit in one scene has a budget smaller than a single medium-complexity character model. ### 5.1 One source: `my-registry/meta.ts` diff --git a/media/kit.png b/media/kit.png index e6392e0..5801012 100644 Binary files a/media/kit.png and b/media/kit.png differ diff --git a/media/models.png b/media/models.png index 30caab9..b0e81de 100644 Binary files a/media/models.png and b/media/models.png differ diff --git a/models.lock.json b/models.lock.json index 74da14b..9a48bc2 100644 --- a/models.lock.json +++ b/models.lock.json @@ -82,7 +82,7 @@ "address": "@medieval-kit/core", "source": "file:my-registry/dist/registry.json", "version": "workspace", - "installedAt": "2026-08-26T20:05:11.276Z", + "installedAt": "2026-08-26T20:14:45.008Z", "files": [ { "path": "src\\models\\medieval-kit\\core\\geometry.ts", @@ -672,6 +672,21 @@ "dependencies": [ "three@>=0.185.0" ] + }, + "@medieval-kit/market-stall": { + "address": "@medieval-kit/market-stall", + "source": "file:my-registry/dist/registry.json", + "version": "workspace", + "installedAt": "2026-08-26T20:14:45.011Z", + "files": [ + { + "path": "src\\models\\medieval-kit\\market-stall\\model.ts", + "sourceHash": "31fa88ff1df342465177b5b95b5814575f5691b255fcea5e75e591fc1fd3740e" + } + ], + "dependencies": [ + "three@>=0.185.0" + ] } } } diff --git a/my-registry/README.md b/my-registry/README.md index 7238b28..17417e1 100644 --- a/my-registry/README.md +++ b/my-registry/README.md @@ -28,7 +28,7 @@ Configure `models.json` first so the namespace resolves: ## Models -37 models and one shared lib. Addresses are `@medieval-kit/`. This table is +38 models and one shared lib. Addresses are `@medieval-kit/`. This table is generated from the models themselves with `bun scripts/catalog-table.ts` — a hand-written list goes stale on the first model you add, and this one already did once. @@ -70,10 +70,11 @@ did once. | `stone-well` | Structure | 1376 | 4 | 1.88×2.00×1.28 | stone, oak, iron, cloth | | | `stone-trough` | Structure | 344 | 2 | 1.52×0.45×0.60 | stone, water | | | `grindstone` | Smithy | 568 | 4 | 1.24×0.95×0.70 | stone, oak, iron, water | | +| `market-stall` | Structure | 384 | 4 | 1.83×2.02×0.97 | oak, cloth | | | `post-mill` | Structure | 1672 | 4 | 6.60×7.23×6.98 | oak, iron | | | `oak-tree` | Nature | 2310 | 3 | 8.57×6.96×9.05 | oak, leaf | | -**32 316 triangles** in total. The whole kit in one scene costs less than a +**32 700 triangles** in total. The whole kit in one scene costs less than a single mid-complexity character model. Every model depends on `@medieval-kit/core`, a shared support item holding the diff --git a/my-registry/meta.ts b/my-registry/meta.ts index 160d080..a2d7f47 100644 --- a/my-registry/meta.ts +++ b/my-registry/meta.ts @@ -468,6 +468,24 @@ export const MODEL_META: Readonly> = { materialSlots: ['oak', 'leather', 'iron'], parts: ['boards', 'rim', 'boss'], }, + 'market-stall': { + title: 'Market Stall', + description: + 'Plank counter on a braced trestle under a linen awning slung between four posts, sagging the way cloth does.', + category: 'Structure', + tags: ['medieval', 'lowpoly', 'structure', 'procedural'], + controls: { + length: { type: 'number', label: 'Length', min: 1, max: 2.6, step: 0.05, unit: 'm' }, + depth: { type: 'number', label: 'Depth', min: 0.45, max: 1.1, step: 0.02, unit: 'm' }, + height: { type: 'number', label: 'Counter height', min: 0.55, max: 1, step: 0.02, unit: 'm' }, + awning: { type: 'number', label: 'Awning height', min: 1.4, max: 2.4, step: 0.05, unit: 'm' }, + sag: { type: 'number', label: 'Cloth sag', min: 0, max: 0.4, step: 0.02 }, + planks: { type: 'number', label: 'Counter boards', min: 2, max: 10, step: 1 }, + seed: { type: 'number', label: 'Variation seed', min: 1, max: 64, step: 1 }, + }, + materialSlots: ['oak', 'cloth'], + parts: ['top', 'trestle', 'posts', 'awning'], + }, 'grindstone': { title: 'Grindstone', description: diff --git a/my-registry/models/market-stall/model.ts b/my-registry/models/market-stall/model.ts new file mode 100644 index 0000000..0c3d9eb --- /dev/null +++ b/my-registry/models/market-stall/model.ts @@ -0,0 +1,293 @@ +/** + * @medieval-kit/market-stall + * + * A plank table on a braced trestle, four posts, and a linen awning over it. + * + * This is the piece the kit was assembled around without having: it is where + * the basket, the vegetables, the sack, the crate and the coin pouch all + * belong, and a market with none of it is a row of props on the floor. It is + * also the only thing here with a roof. + * + * The awning SAGS, and that is the whole model. A flat sheet on four posts is + * a table with a lid; cloth slung between four points hangs, and the dip is + * what says it is cloth before the colour does. It costs nothing here: + * `dishedSheetGeometry` bends its cross section by `curve`, and one turn about + * X maps that bend onto the world's vertical — so the sag is the helper's own + * parameter rather than anything hand-built. + * + * The front edge dips further than the back, which is also the reference and + * is the reason the sag is per-level rather than one number: cloth pulled over + * a ridge and left loose at the eaves does not hang symmetrically. + */ +import { Color, type BufferGeometry } from 'three' + +import { + boxGeometry, + createKitModel, + createTinter, + dishedSheetGeometry, + jitter, + mergeColoured, + taperedBoxGeometry, + type SheetLevel, +} from '../core/index.ts' + +export interface MarketStallConfig { + /** Along the counter (metres). */ + readonly length: number + /** Front to back (metres). */ + readonly depth: number + /** Height of the counter top (metres). */ + readonly height: number + /** Height of the awning above the ground (metres). */ + readonly awning: number + /** How far the cloth hangs between the posts, as a fraction of the depth. */ + readonly sag: number + /** Boards in the counter top. */ + readonly planks: number + readonly seed: number +} + +export const marketStallDefaults: MarketStallConfig = { + length: 1.62, + depth: 0.76, + height: 0.78, + awning: 1.94, + // 0.10 of the depth, not 0.17. At 0.17 the cloth dropped a tenth of its own + // span between the posts and read as a hammock; the reference hangs about + // half that. Cloth stretched over a stall is pulled tight and then sags a + // little, which is a different shape from cloth thrown over it. + sag: 0.1, + planks: 6, + seed: 29, +} + +export type MarketStallParts = 'top' | 'trestle' | 'posts' | 'awning' + +export function createModel(overrides: Partial = {}) { + return createKitModel({ + id: 'market-stall', + defaults: marketStallDefaults, + slots: ['oak', 'cloth'], + + build: ({ config, random }) => { + const tint = createTinter(random) + const L = Math.max(0.5, config.length) + const D = Math.max(0.3, config.depth) + const H = Math.max(0.3, config.height) + const roofY = Math.max(H + 0.25, config.awning) + const planks = Math.max(2, Math.round(config.planks)) + const timberSize = D * 0.075 + + /** Weathered, like the grindstone's frame: a stall lives outdoors. */ + const timber = (lift = 0): Color => { + const c = tint('oak', -0.08 + lift, 0.85) + c.offsetHSL(0, -0.09, 0) + return c + } + + // --- Counter top ---------------------------------------------------- + // Boards with a hair of daylight between them. The gap is what makes it + // a counter rather than a slab, and it is why the planks are separate + // boxes rather than one. + const boardT = timberSize * 0.62 + const topY = H - boardT / 2 + const pitch = D / planks + // The counter stops SHORT of the posts, which stand at the two ends. + // Run to the full length the boards had posts driven through them a + // hand's breadth from each end, which is not how a stall is built and + // not what the reference shows: the top lies between the posts. + const postSize = timberSize * 0.72 + const topL = L - postSize * 2.6 + const top: BufferGeometry[] = [] + for (let i = 0; i < planks; i += 1) { + top.push(boxGeometry( + [topL, boardT, pitch * 0.93], + [0, topY + jitter(random, boardT * 0.06), -D / 2 + pitch * (i + 0.5)], + timber(jitter(random, 0.05)), + )) + } + + // --- Trestle -------------------------------------------------------- + const trestle: BufferGeometry[] = [] + const legX = L / 2 - timberSize * 1.6 + const legZ = D / 2 - timberSize * 1.3 + const railY = H - boardT - timberSize * 0.5 + // Two rails under the top, running the length, which is what the legs + // and the posts both fasten to. + for (const sz of [-1, 1] as const) { + // Full length, so they reach the posts at the ends. + trestle.push(boxGeometry( + [L, timberSize, timberSize * 0.85], + [0, railY, sz * legZ], + timber(-0.02), + )) + } + // Legs, splayed a little on the way down so the thing is not a card + // table. Splayed by WIDENING rather than by rotating, which is what lets + // the foot sit flat. + const splay = timberSize * 1.5 + for (const sx of [-1, 1] as const) { + for (const sz of [-1, 1] as const) { + trestle.push(taperedBoxGeometry( + [timberSize * 1.15, timberSize * 1.15], + [timberSize, timberSize], + // Up INTO the rail and stopping there. Run to the rail's own top + // face the leg ends level with it — two upward faces in the plane + // y = 0.745, which the checker found at all four corners before + // anything was rendered. A leg is housed in a rail, not flush + // with it. + railY + timberSize * 0.18, + [ + sx * (legX + splay * 0.5), + (railY + timberSize * 0.18) / 2, + sz * (legZ + splay * 0.35), + ], + timber(-0.05 + jitter(random, 0.04)), + )) + } + } + // The braces from the leg up to the rail. They are the reference's most + // recognisable joint and they are also what stops the top racking. + for (const sx of [-1, 1] as const) { + for (const sz of [-1, 1] as const) { + const foot: readonly [number, number, number] = [ + sx * (legX + splay * 0.42), railY * 0.42, sz * (legZ + splay * 0.2), + ] + const head: readonly [number, number, number] = [ + sx * (legX - L * 0.16), railY, sz * legZ, + ] + const dx = head[0] - foot[0] + const dy = head[1] - foot[1] + const dz = head[2] - foot[2] + const len = Math.hypot(dx, dy, dz) + const brace = taperedBoxGeometry( + [timberSize * 0.7, timberSize * 0.7], + [timberSize * 0.62, timberSize * 0.62], + len, + [0, len / 2, 0], + timber(-0.03), + ) + // Built at the origin and turned, the construction that has never + // gone wrong here. The bearing comes off the two ends, not out of a + // guess: rotateZ then rotateY sends +Y to + // (-sin t cos a, cos t, sin t sin a), so a = atan2(dz, -dx). + brace.rotateZ(Math.acos(Math.max(-1, Math.min(1, dy / len)))) + brace.rotateY(Math.atan2(dz, -dx)) + trestle.push(brace.translate(foot[0], foot[1], foot[2])) + } + } + // The low stretcher and its cross piece. + const tieY = railY * 0.26 + trestle.push(boxGeometry( + [(legX + splay * 0.46) * 2, timberSize * 0.8, timberSize * 0.7], + [0, tieY, 0], + timber(-0.07), + )) + for (const sx of [-1, 1] as const) { + trestle.push(boxGeometry( + [timberSize * 0.7, timberSize * 0.7, (legZ + splay * 0.3) * 2], + [sx * (legX + splay * 0.46), tieY, 0], + timber(-0.06), + )) + } + + // --- Posts ---------------------------------------------------------- + // They run from the RAIL, not from the top: bolted to the frame is how + // the reference carries them, and it is also what puts them beyond doubt + // for the support check — a post standing on a plank top would be held up + // by six millimetres of board. + const posts: BufferGeometry[] = [] + const postX = L / 2 - postSize * 0.5 + + // The awning's shape, worked out here because the POSTS need it. + const overhang = timberSize * 1.8 + const sag = Math.max(0, config.sag) * D + const halfLength = L / 2 + overhang + const halfDepth = D / 2 + overhang + const tilt = 0.055 + + /** + * How high the cloth hangs over a point on the ground. + * + * Every post has to reach it, and each of the four reaches a different + * height: the sheet is a parabola across its length and it is deeper at + * the front than the back, so a single post height leaves some of them + * short. Built with one, the cloth came away as its own floating piece + * 67 mm above the post heads — held up in the drawing and by nothing in + * the geometry. + * + * This is the sheet's own arithmetic rather than a second version of it: + * the same parabola, the same per-level sag, the same quarter turn that + * maps the helper's `curve` onto the world's vertical, and the same + * small tilt after it. + */ + const clothY = (x: number, z: number): number => { + const f = (-z + halfDepth) / (2 * halfDepth) + const u = x / halfLength + const curve = sag * (0.72 + f * 0.55) + return roofY + Math.cos(tilt) * curve * u * u + Math.sin(tilt) * z + } + + for (const sx of [-1, 1] as const) { + for (const sz of [-1, 1] as const) { + // A shade past the cloth's mid surface, so the head is buried in it. + const head = clothY(sx * postX, sz * legZ) + boardT * 0.15 + // Down PAST the rail and finishing as a stub below it, which is both + // the reference's detail and the fix for a real fault: stopped at the + // rail's underside, the post's foot lay in the same plane as it, + // facing the same way — `plane 0,-1,0 | -0.688` at all four corners. + const foot = railY - timberSize * 1.8 + posts.push(taperedBoxGeometry( + [postSize, postSize], + [postSize * 0.88, postSize * 0.88], + head - foot, + [sx * postX, (foot + head) / 2, sz * legZ], + timber(0.02 + jitter(random, 0.03)), + )) + } + } + + // --- Awning --------------------------------------------------------- + /** + * Cloth slung between the four post heads. + * + * `dishedSheetGeometry` builds in the XY plane and bends its cross + * section along Z by `curve`. Turning it a quarter about X maps that + * bend onto the world's vertical, so the parabola the helper already + * draws IS the hang of the cloth: levels run front to back, the half + * width is half the stall's length, and the sag is a parameter rather + * than something modelled. + * + * `curve` lifts the EDGES relative to the middle, which is the right way + * round — the cloth is held at the posts and falls between them. + */ + const levels: SheetLevel[] = [] + const steps = 4 + for (let i = 0; i < steps; i += 1) { + const f = i / (steps - 1) + levels.push({ + y: halfDepth * (f * 2 - 1), + halfWidth: halfLength, + thickness: boardT * 0.2, + // Deeper at the front than the back. Cloth pulled over a ridge and + // left loose at the eaves does not hang symmetrically. + curve: sag * (0.72 + f * 0.55), + }) + } + const awning = dishedSheetGeometry(levels, 6, tint('cloth', 0.04, 0.9)) + awning.rotateX(-Math.PI / 2) + // A shade of fall front to back, on top of the sag. Same axis as the + // turn above, so it is one angle rather than a second rotation. + awning.rotateX(-tilt) + awning.translate(0, roofY, 0) + + return { + top: { slot: 'oak' as const, geometry: mergeColoured(top) }, + trestle: { slot: 'oak' as const, geometry: mergeColoured(trestle) }, + posts: { slot: 'oak' as const, geometry: mergeColoured(posts) }, + awning: { slot: 'cloth' as const, geometry: awning }, + } + }, + }, overrides) +} diff --git a/scripts/verify-model.ts b/scripts/verify-model.ts index 373fbf8..0190cd6 100644 --- a/scripts/verify-model.ts +++ b/scripts/verify-model.ts @@ -59,6 +59,7 @@ import { createModel as createForge } from '@/models/medieval-kit/forge-hearth/m import { createModel as createOak } from '@/models/medieval-kit/oak-tree/model.ts' import { createModel as createTrough } from '@/models/medieval-kit/stone-trough/model.ts' import { createModel as createGrind } from '@/models/medieval-kit/grindstone/model.ts' +import { createModel as createStall } from '@/models/medieval-kit/market-stall/model.ts' const failures: string[] = [] function expect(label: string, condition: boolean): void { @@ -317,6 +318,10 @@ const CASES: readonly Case[] = [ parts: 3, ownSlot: 'leather', borrowSlot: 'oak', variants: [{ planks: 3 }, { lean: 0 }, { rivets: 0 }], maxSize: [1.3, 1.3, 0.9] }, + { id: 'market-stall', make: as(createStall), patch: { planks: 9, sag: 0.3 }, + parts: 4, ownSlot: 'cloth', borrowSlot: 'oak', + variants: [{ sag: 0 }, { planks: 2 }, { length: 2.6 }, { awning: 1.4 }], + maxSize: [3.2, 2.5, 1.6] }, { id: 'grindstone', make: as(createGrind), patch: { diameter: 0.8, splay: 0.4 }, parts: 4, ownSlot: 'stone', borrowSlot: 'oak', variants: [{ water: 0 }, { splay: 0 }, { diameter: 0.35 }, { thickness: 0.18 }], diff --git a/src/catalog.ts b/src/catalog.ts index 98c782a..b60386c 100644 --- a/src/catalog.ts +++ b/src/catalog.ts @@ -53,6 +53,7 @@ import { createModel as createForge } from '@/models/medieval-kit/forge-hearth/m import { createModel as createOak } from '@/models/medieval-kit/oak-tree/model.ts' import { createModel as createTrough } from '@/models/medieval-kit/stone-trough/model.ts' import { createModel as createGrind } from '@/models/medieval-kit/grindstone/model.ts' +import { createModel as createStall } from '@/models/medieval-kit/market-stall/model.ts' export interface ParamSpec { readonly key: string @@ -167,6 +168,7 @@ const FACTORIES: Readonly KitModel>> = { 'oak-tree': as(createOak), 'stone-trough': as(createTrough), 'grindstone': as(createGrind), + 'market-stall': as(createStall), 'wooden-ladder': as(createLadder), 'wooden-fence': as(createFence), 'wooden-hoe': as(createHoe), @@ -325,6 +327,7 @@ const MEDIEVAL_ORDER = [ 'stone-well', 'stone-trough', 'grindstone', + 'market-stall', 'post-mill', // After the mill, because it is taller than the mill and half again as wide. // A tour that ends on the only living thing in the kit ends better than one diff --git a/src/models/medieval-kit/market-stall/model.ts b/src/models/medieval-kit/market-stall/model.ts new file mode 100644 index 0000000..0c3d9eb --- /dev/null +++ b/src/models/medieval-kit/market-stall/model.ts @@ -0,0 +1,293 @@ +/** + * @medieval-kit/market-stall + * + * A plank table on a braced trestle, four posts, and a linen awning over it. + * + * This is the piece the kit was assembled around without having: it is where + * the basket, the vegetables, the sack, the crate and the coin pouch all + * belong, and a market with none of it is a row of props on the floor. It is + * also the only thing here with a roof. + * + * The awning SAGS, and that is the whole model. A flat sheet on four posts is + * a table with a lid; cloth slung between four points hangs, and the dip is + * what says it is cloth before the colour does. It costs nothing here: + * `dishedSheetGeometry` bends its cross section by `curve`, and one turn about + * X maps that bend onto the world's vertical — so the sag is the helper's own + * parameter rather than anything hand-built. + * + * The front edge dips further than the back, which is also the reference and + * is the reason the sag is per-level rather than one number: cloth pulled over + * a ridge and left loose at the eaves does not hang symmetrically. + */ +import { Color, type BufferGeometry } from 'three' + +import { + boxGeometry, + createKitModel, + createTinter, + dishedSheetGeometry, + jitter, + mergeColoured, + taperedBoxGeometry, + type SheetLevel, +} from '../core/index.ts' + +export interface MarketStallConfig { + /** Along the counter (metres). */ + readonly length: number + /** Front to back (metres). */ + readonly depth: number + /** Height of the counter top (metres). */ + readonly height: number + /** Height of the awning above the ground (metres). */ + readonly awning: number + /** How far the cloth hangs between the posts, as a fraction of the depth. */ + readonly sag: number + /** Boards in the counter top. */ + readonly planks: number + readonly seed: number +} + +export const marketStallDefaults: MarketStallConfig = { + length: 1.62, + depth: 0.76, + height: 0.78, + awning: 1.94, + // 0.10 of the depth, not 0.17. At 0.17 the cloth dropped a tenth of its own + // span between the posts and read as a hammock; the reference hangs about + // half that. Cloth stretched over a stall is pulled tight and then sags a + // little, which is a different shape from cloth thrown over it. + sag: 0.1, + planks: 6, + seed: 29, +} + +export type MarketStallParts = 'top' | 'trestle' | 'posts' | 'awning' + +export function createModel(overrides: Partial = {}) { + return createKitModel({ + id: 'market-stall', + defaults: marketStallDefaults, + slots: ['oak', 'cloth'], + + build: ({ config, random }) => { + const tint = createTinter(random) + const L = Math.max(0.5, config.length) + const D = Math.max(0.3, config.depth) + const H = Math.max(0.3, config.height) + const roofY = Math.max(H + 0.25, config.awning) + const planks = Math.max(2, Math.round(config.planks)) + const timberSize = D * 0.075 + + /** Weathered, like the grindstone's frame: a stall lives outdoors. */ + const timber = (lift = 0): Color => { + const c = tint('oak', -0.08 + lift, 0.85) + c.offsetHSL(0, -0.09, 0) + return c + } + + // --- Counter top ---------------------------------------------------- + // Boards with a hair of daylight between them. The gap is what makes it + // a counter rather than a slab, and it is why the planks are separate + // boxes rather than one. + const boardT = timberSize * 0.62 + const topY = H - boardT / 2 + const pitch = D / planks + // The counter stops SHORT of the posts, which stand at the two ends. + // Run to the full length the boards had posts driven through them a + // hand's breadth from each end, which is not how a stall is built and + // not what the reference shows: the top lies between the posts. + const postSize = timberSize * 0.72 + const topL = L - postSize * 2.6 + const top: BufferGeometry[] = [] + for (let i = 0; i < planks; i += 1) { + top.push(boxGeometry( + [topL, boardT, pitch * 0.93], + [0, topY + jitter(random, boardT * 0.06), -D / 2 + pitch * (i + 0.5)], + timber(jitter(random, 0.05)), + )) + } + + // --- Trestle -------------------------------------------------------- + const trestle: BufferGeometry[] = [] + const legX = L / 2 - timberSize * 1.6 + const legZ = D / 2 - timberSize * 1.3 + const railY = H - boardT - timberSize * 0.5 + // Two rails under the top, running the length, which is what the legs + // and the posts both fasten to. + for (const sz of [-1, 1] as const) { + // Full length, so they reach the posts at the ends. + trestle.push(boxGeometry( + [L, timberSize, timberSize * 0.85], + [0, railY, sz * legZ], + timber(-0.02), + )) + } + // Legs, splayed a little on the way down so the thing is not a card + // table. Splayed by WIDENING rather than by rotating, which is what lets + // the foot sit flat. + const splay = timberSize * 1.5 + for (const sx of [-1, 1] as const) { + for (const sz of [-1, 1] as const) { + trestle.push(taperedBoxGeometry( + [timberSize * 1.15, timberSize * 1.15], + [timberSize, timberSize], + // Up INTO the rail and stopping there. Run to the rail's own top + // face the leg ends level with it — two upward faces in the plane + // y = 0.745, which the checker found at all four corners before + // anything was rendered. A leg is housed in a rail, not flush + // with it. + railY + timberSize * 0.18, + [ + sx * (legX + splay * 0.5), + (railY + timberSize * 0.18) / 2, + sz * (legZ + splay * 0.35), + ], + timber(-0.05 + jitter(random, 0.04)), + )) + } + } + // The braces from the leg up to the rail. They are the reference's most + // recognisable joint and they are also what stops the top racking. + for (const sx of [-1, 1] as const) { + for (const sz of [-1, 1] as const) { + const foot: readonly [number, number, number] = [ + sx * (legX + splay * 0.42), railY * 0.42, sz * (legZ + splay * 0.2), + ] + const head: readonly [number, number, number] = [ + sx * (legX - L * 0.16), railY, sz * legZ, + ] + const dx = head[0] - foot[0] + const dy = head[1] - foot[1] + const dz = head[2] - foot[2] + const len = Math.hypot(dx, dy, dz) + const brace = taperedBoxGeometry( + [timberSize * 0.7, timberSize * 0.7], + [timberSize * 0.62, timberSize * 0.62], + len, + [0, len / 2, 0], + timber(-0.03), + ) + // Built at the origin and turned, the construction that has never + // gone wrong here. The bearing comes off the two ends, not out of a + // guess: rotateZ then rotateY sends +Y to + // (-sin t cos a, cos t, sin t sin a), so a = atan2(dz, -dx). + brace.rotateZ(Math.acos(Math.max(-1, Math.min(1, dy / len)))) + brace.rotateY(Math.atan2(dz, -dx)) + trestle.push(brace.translate(foot[0], foot[1], foot[2])) + } + } + // The low stretcher and its cross piece. + const tieY = railY * 0.26 + trestle.push(boxGeometry( + [(legX + splay * 0.46) * 2, timberSize * 0.8, timberSize * 0.7], + [0, tieY, 0], + timber(-0.07), + )) + for (const sx of [-1, 1] as const) { + trestle.push(boxGeometry( + [timberSize * 0.7, timberSize * 0.7, (legZ + splay * 0.3) * 2], + [sx * (legX + splay * 0.46), tieY, 0], + timber(-0.06), + )) + } + + // --- Posts ---------------------------------------------------------- + // They run from the RAIL, not from the top: bolted to the frame is how + // the reference carries them, and it is also what puts them beyond doubt + // for the support check — a post standing on a plank top would be held up + // by six millimetres of board. + const posts: BufferGeometry[] = [] + const postX = L / 2 - postSize * 0.5 + + // The awning's shape, worked out here because the POSTS need it. + const overhang = timberSize * 1.8 + const sag = Math.max(0, config.sag) * D + const halfLength = L / 2 + overhang + const halfDepth = D / 2 + overhang + const tilt = 0.055 + + /** + * How high the cloth hangs over a point on the ground. + * + * Every post has to reach it, and each of the four reaches a different + * height: the sheet is a parabola across its length and it is deeper at + * the front than the back, so a single post height leaves some of them + * short. Built with one, the cloth came away as its own floating piece + * 67 mm above the post heads — held up in the drawing and by nothing in + * the geometry. + * + * This is the sheet's own arithmetic rather than a second version of it: + * the same parabola, the same per-level sag, the same quarter turn that + * maps the helper's `curve` onto the world's vertical, and the same + * small tilt after it. + */ + const clothY = (x: number, z: number): number => { + const f = (-z + halfDepth) / (2 * halfDepth) + const u = x / halfLength + const curve = sag * (0.72 + f * 0.55) + return roofY + Math.cos(tilt) * curve * u * u + Math.sin(tilt) * z + } + + for (const sx of [-1, 1] as const) { + for (const sz of [-1, 1] as const) { + // A shade past the cloth's mid surface, so the head is buried in it. + const head = clothY(sx * postX, sz * legZ) + boardT * 0.15 + // Down PAST the rail and finishing as a stub below it, which is both + // the reference's detail and the fix for a real fault: stopped at the + // rail's underside, the post's foot lay in the same plane as it, + // facing the same way — `plane 0,-1,0 | -0.688` at all four corners. + const foot = railY - timberSize * 1.8 + posts.push(taperedBoxGeometry( + [postSize, postSize], + [postSize * 0.88, postSize * 0.88], + head - foot, + [sx * postX, (foot + head) / 2, sz * legZ], + timber(0.02 + jitter(random, 0.03)), + )) + } + } + + // --- Awning --------------------------------------------------------- + /** + * Cloth slung between the four post heads. + * + * `dishedSheetGeometry` builds in the XY plane and bends its cross + * section along Z by `curve`. Turning it a quarter about X maps that + * bend onto the world's vertical, so the parabola the helper already + * draws IS the hang of the cloth: levels run front to back, the half + * width is half the stall's length, and the sag is a parameter rather + * than something modelled. + * + * `curve` lifts the EDGES relative to the middle, which is the right way + * round — the cloth is held at the posts and falls between them. + */ + const levels: SheetLevel[] = [] + const steps = 4 + for (let i = 0; i < steps; i += 1) { + const f = i / (steps - 1) + levels.push({ + y: halfDepth * (f * 2 - 1), + halfWidth: halfLength, + thickness: boardT * 0.2, + // Deeper at the front than the back. Cloth pulled over a ridge and + // left loose at the eaves does not hang symmetrically. + curve: sag * (0.72 + f * 0.55), + }) + } + const awning = dishedSheetGeometry(levels, 6, tint('cloth', 0.04, 0.9)) + awning.rotateX(-Math.PI / 2) + // A shade of fall front to back, on top of the sag. Same axis as the + // turn above, so it is one angle rather than a second rotation. + awning.rotateX(-tilt) + awning.translate(0, roofY, 0) + + return { + top: { slot: 'oak' as const, geometry: mergeColoured(top) }, + trestle: { slot: 'oak' as const, geometry: mergeColoured(trestle) }, + posts: { slot: 'oak' as const, geometry: mergeColoured(posts) }, + awning: { slot: 'cloth' as const, geometry: awning }, + } + }, + }, overrides) +}