Skip to content

A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D

15.8 KB
Raw Blame
import { assetById } from "../../render/pixi3d/assets/assets.ts"
import { buildAsset, exportAsset } from "../../render/pixi3d/assets/renderer.ts"
import { kindIds } from "../../game/content/index.ts"
import { ruralIds } from "../../render/pixi3d/assets/rural.ts"
import type { Vec3 } from "../../render/pixi3d/mesh/vec3.ts"
import { aim, frame as fit, labViewer, toward } from "../viewer.ts"
import "./style.css"
import "./handoff.css"

const families = {
  milestones: { title: "Civic milestones", ids: ["townhall", "guildhall", "cityhall"] },
  works: { title: "Reusable building works", ids: ["upgrade-works-15", "upgrade-works-50", "upgrade-works-90", "works-carpenter-0", "works-warehouse-0", "works-quarry-0", "works-farm-0", "works-townhall-1", "works-townhall-2"] },
  handoff: { title: "PR #20 · new models", ids: ["carpenter", "guildhall", "cityhall", "depot", "ovenworks", "masonry", "estate", "fire-brigade", "market-hall", "church", "upgrade-works-15", "upgrade-works-50", "upgrade-works-90", "cart-furniture-4"] },
  town: { title: "All buildings", ids: [...kindIds, "market-hall", "church"] },
  rural: { title: "Final refinements", ids: ruralIds },
  trades: { title: "Trade buildings", ids: ["mill", "warehouse", "fishery", "bakery", "weaver", "watchhouse"] },
  workshops: { title: "Workshop batch", ids: ["mine", "smithy", "sawmill", "townhall"] },
  fixes: { title: "Review fixes", ids: ["mill","bakery","weaver","warehouse","mine","farm","chapel","hospital","smokehouse","builder","townhouse","quarry","stone-full","stone-quarried","ore-full","ore-quarried","stone-full-2x1","stone-full-2x2","stone-full-3x3","ore-full-2x1","ore-full-2x2","ore-full-3x3","problem-marker","smoke","firewatch"] },
  buildings: { title: "Buildings", ids: ["granary", "smokehouse", "firewatch", "healer", "hospital", "watchhouse", "builder"] },
  ornaments: { title: "Parks", ids: ["park-sample", "park-lamp", "park-fountain", "park-statue", "paved-street"] },
  homes: { title: "Homes & neighbourhoods", ids: ["cottage", "citizen", "townhouse", "cottage-fine", "citizen-fine", "townhouse-fine", "cottage-poor", "citizen-worn", "townhouse-worn"] },
  winter: { title: "Winter", ids: ["cottage-winter", "citizen-winter", "townhouse-winter", "granary-winter", "smokehouse-winter", "firewatch-winter", "healer-winter", "hospital-winter", "watchhouse-winter", "builder-winter"] },
  hazards: { title: "Ruins", ids: ["ruin-burnt-1", "ruin-collapsed-1", "ruin-derelict-1"] },
  people: { title: "People & cargo", ids: ["service-watchman", "service-firefighter", "service-physician", "service-constable", "service-builder", "goods-smoked", "cart-smoked-4", "barrow-smoked-4", "goods-furniture", "cart-furniture-4", "barrow-furniture-4"] },
  wear: { title: "Building condition", ids: ["granary-worn", "smokehouse-worn", "firewatch-worn", "healer-worn", "hospital-worn", "watchhouse-worn", "builder-worn"] },
  upgrades: { title: "Upgrades & worked faces", ids: ["market", "market-hall", "chapel", "church", "stone-full", "stone-quarried", "stone-worked", "ore-full", "ore-quarried", "ore-worked"] }
} as const
type Family = keyof typeof families
const introductions: Partial<Record<Family, { heading: string; eyebrow: string; description: string; aside: string }>> = {
  milestones: { heading: "A town's<br>great occasions.", eyebrow: "Town hall · Guildhall · City hall", description: "A familiar town hall becomes a steep-roofed guild house with a clock tower and balcony, then a domed civic palace with a ceremonial stair, colonnade and lower wings. These three views share one camera scale.", aside: "3 civic milestones<br>Original three-cell plot<br><span>Distinct silhouettes,<br>stonework and civic colour.</span>" },
  works: { heading: "One scaffold.<br>Every workplace.", eyebrow: "Reusable construction geometry", description: "The same modular scaffold on a civic hall, open workshop, warehouse, quarry and farm. Plot size, building height and delivered share are its only inputs. Central bays stay open on every side.", aside: "1 shared generator<br>6 different building forms<br><span>Platforms, access ladders,<br>canvas screens and supplies.</span>" },
  handoff: { heading: "New work.<br>The same ink.", eyebrow: "PR #20 · Carpenter and upgrades", description: "An open carpenter's workshop, civic towers and additions to the town's working yards. Turn the models to inspect their tools and supports, then compare them with the existing building collections.", aside: "1 new workshop<br>9 upgrade appearances<br><span>Plus scaffold progress<br>and furniture deliveries.</span>" },
  town: { heading: "The whole town.<br>The same ink.", eyebrow: "The complete building collection", description: "Every building kind, plus the market hall and church. Compare their silhouettes, turn the collection, and open any building for a closer look.", aside: `${kindIds.length} building kinds<br>2 upgraded buildings<br><span>Work yards, homes for trades,<br>stores and town services.</span>` },
  rural: { heading: "Small things.<br>A better town.", eyebrow: "The final building pass", description: "Timber entrances, nursery beds, farm details, a quieter chapel and a connected fire-watch lookout. Inspect all nine refinements together.", aside: "9 refined buildings<br>Original plots<br><span>Connected structures,<br>clear entrances and useful yards.</span>" },
  workshops: { heading: "Room to work.<br>The same ink.", eyebrow: "The first building batch", description: "Open work yards around real tools, and a civic entrance for the town hall. Turn the models to inspect their working space.", aside: "4 revised buildings<br>3 open work yards<br><span>Original plots, pale surfaces,<br>fine ink and purposeful tools.</span>" },
  trades: { heading: "Built for the job.<br>The same ink.", eyebrow: "The second building batch", description: "Loading aprons, a fishing net, an open oven and a working loom. Inspect the equipment and entrances from every side.", aside: "6 revised buildings<br>Original plots<br><span>Timber loading doors, open bays,<br>and equipment for each trade.</span>" }
}
const root = document.querySelector<HTMLElement>("#review")!
root.innerHTML = `<header><a href="/">Inkline Town</a><span>TOWN MODEL REVIEW</span><a href="/map-lab.html">Town map ↗</a></header>
  <section class="intro"><div><p class="eyebrow">A town with a little more life</p><h1>New places.<br>The same ink.</h1><p>Inspect the models together, turn them around, then open any one for a closer look.</p></div><aside>7 buildings<br>3 park furnishings<br>3 home tiers<br><span>Plus people, hazards, paving,<br>snow and signs of wear.</span></aside></section>
  <nav aria-label="Model collections"></nav><section class="camera"><label>Turn the models <input aria-label="Model rotation" type="range" min="-180" max="180" value="0"><output>0°</output></label><button id="rear">See the back</button><button id="reset">Reset view</button><button id="sheet">Save model sheet</button><a id="sheet-link" hidden download>Download sheet ↗</a><label>View <select aria-label="View elevation"><option value="30">Street angle</option><option value="50">From above</option><option value="15">Near the ground</option></select></label></section>
  <section class="grid" aria-label="3D models"></section><footer><a href="/proportion-review.html">Before / after proportions ↗</a> · <a href="/park-lab.html">Built park & running water ↗</a> · <a href="/effects-lab.html">Animated house effects ↗</a> · Drag a model to turn the collection · <a href="/asset-lab.html">Full asset catalogue ↗</a> · <a href="/">Play this town ↗</a></footer>`
const nav = root.querySelector("nav")!, grid = root.querySelector<HTMLElement>(".grid")!, rotation = root.querySelector<HTMLInputElement>("input")!, elevation = root.querySelector<HTMLSelectElement>("select")!
// One canvas over the page; each card's picture is its model alone, drawn into the card's rectangle.
const viewer = await labViewer(document.body, { width: innerWidth, height: innerHeight, className: "models" }), camera = viewer.camera
let family: Family = "buildings", yaw = 0, pitch = 30, renders = 0
let sheetUrl = ""
let models: Array<{ built: ReturnType<typeof buildAsset>; view: HTMLElement; center: Vec3; size: Vec3 }> = []
const boxOf = ([min, max]: readonly [Vec3, Vec3]) => ({ center: [(min[0] + max[0]) / 2, (min[1] + max[1]) / 2, (min[2] + max[2]) / 2] as Vec3, size: [max[0] - min[0], max[1] - min[1], max[2] - min[2]] as Vec3 })
/** Shows `model` alone and aims the camera at it for a picture `width` × `height`. */
function place(model: (typeof models)[number], width: number, height: number) {
  for (const m of models) m.built.group.visible = m === model
  const heading = (45 + yaw) * Math.PI / 180, angle = pitch * Math.PI / 180
  aim(camera, model.center, toward(heading, angle), 1000)
  fit(camera, viewHeight(model.size, heading, angle, width / height) / 2, width, height)
  model.built.updateEffects(performance.now() / 1000)
}
// Include both the tower height and the ground plane projected into the view.
// Taking only the larger dimension clips the mill's foreground at street angle.
function viewHeight(size: Vec3, heading: number, angle: number, aspect: number) {
  const width = Math.abs(Math.cos(heading)) * size[0] + Math.abs(Math.sin(heading)) * size[2]
  const depth = Math.abs(Math.sin(heading)) * size[0] + Math.abs(Math.cos(heading)) * size[2]
  const height = Math.abs(Math.cos(angle)) * size[1] + Math.abs(Math.sin(angle)) * depth
  return Math.max(height, width / aspect, 18) * 1.12
}
function render() {
  viewer.resize(innerWidth, innerHeight)
  let first = true
  for (const model of models) {
    const r = model.view.getBoundingClientRect()
    if (r.bottom <= 0 || r.top >= innerHeight) continue
    place(model, r.width, r.height)
    camera.viewport.set(r.left / innerWidth, r.top / innerHeight, r.width / innerWidth, r.height / innerHeight)
    viewer.draw(first); first = false
  }
  if (first) { for (const m of models) m.built.group.visible = false; viewer.draw(true) }
  camera.viewport.set(0, 0, 1, 1)
  root.querySelector("output")!.textContent = `${Math.round(yaw)}°`; rotation.value = String(yaw); renders++
}
function select(next: Family) {
  if (sheetUrl) URL.revokeObjectURL(sheetUrl)
  sheetUrl = ""; root.querySelector<HTMLAnchorElement>("#sheet-link")!.hidden = true
  family = next; models.forEach(m => m.built.dispose()); models = []; grid.replaceChildren()
  grid.classList.toggle("handoff", family === "handoff")
  const intro = introductions[family] ?? { heading: "New places.<br>The same ink.", eyebrow: "A town with a little more life", description: "Inspect the models together, turn them around, then open any one for a closer look.", aside: "7 buildings<br>3 park furnishings<br>3 home tiers<br><span>Plus people, hazards, paving,<br>snow and signs of wear.</span>" }
  root.querySelector(".intro h1")!.innerHTML = intro.heading
  root.querySelector(".eyebrow")!.textContent = intro.eyebrow
  root.querySelector(".intro > div > p:last-child")!.textContent = intro.description
  root.querySelector(".intro aside")!.innerHTML = intro.aside
  for (const [i, id] of families[family].ids.entries()) {
    const asset = assetById.get(id)!
    if (!asset) throw new Error(`Missing review model: ${id}`)
    const card = document.createElement("article")
    card.innerHTML = `<div class="card-top"><span>${String(i + 1).padStart(2, "0")}</span><a href="/asset-lab.html?asset=${id}">Inspect ↗</a></div><div class="model-view" role="img" aria-label="${asset.title} 3D model"></div><div class="caption"><h2>${asset.title}</h2><p>${asset.note}</p><button class="export">Download GLB</button></div>`
    grid.append(card)
    const built = buildAsset(asset, {batchFaces: true})
    viewer.view.root.addChild(built.group)
    models.push({built, view: card.querySelector<HTMLElement>(".model-view")!, ...boxOf(built.bounds)})
    card.querySelector<HTMLButtonElement>(".export")!.onclick = async () => {
      const url = URL.createObjectURL(new Blob([await exportAsset(asset)], {type: "model/gltf-binary"})), a = document.createElement("a")
      a.href = url; a.download = `inkline-${id}.glb`; a.click(); setTimeout(() => URL.revokeObjectURL(url), 1000)
    }
  }
  if (family === "milestones") {
    const min: Vec3 = [0, 1, 2].map(k => Math.min(...models.map(m => m.built.bounds[0][k]!))) as [number, number, number]
    const max: Vec3 = [0, 1, 2].map(k => Math.max(...models.map(m => m.built.bounds[1][k]!))) as [number, number, number]
    for (const model of models) Object.assign(model, boxOf([min, max]))
  }
  nav.querySelectorAll("button").forEach(b => { b.setAttribute("aria-pressed", String(b.dataset.family === next)) })
  history.replaceState(null, "", `?collection=${next}`); render()
}
for (const [id, group] of Object.entries(families)) {
  const b = document.createElement("button"); b.textContent = group.title; b.dataset.family = id; b.onclick = () => select(id as Family); nav.append(b)
}
rotation.oninput = () => { yaw = Number(rotation.value); render() }
elevation.onchange = () => { pitch = Number(elevation.value); render() }
root.querySelector<HTMLButtonElement>("#rear")!.onclick = () => { yaw = yaw === 180 ? 0 : 180; render() }
root.querySelector<HTMLButtonElement>("#reset")!.onclick = () => { yaw = 0; pitch = 30; elevation.value = "30"; render() }
root.querySelector<HTMLButtonElement>("#sheet")!.onclick = () => {
  const canvas = document.createElement("canvas"), ctx = canvas.getContext("2d")!, columns = family === "handoff" ? 2 : 3, width = family === "handoff" ? 600 : 400, height = 350
  canvas.width = columns * width; canvas.height = 100 + Math.ceil(models.length / columns) * height
  ctx.fillStyle = "#f0f2ec"; ctx.fillRect(0, 0, canvas.width, canvas.height)
  ctx.fillStyle = "#3d4943"; ctx.font = "30px Georgia"; ctx.fillText(`Inkline Town / ${families[family].title}`, 30, 48)
  ctx.font = "13px monospace"; ctx.fillText(`Inkline Town · ${Math.round(yaw)}° rotation · ${pitch}° elevation`, 30, 76)
  models.forEach((model, i) => {
    place(model, width, 270)
    const x = (i % columns) * width, y = 100 + Math.floor(i / columns) * height
    ctx.drawImage(viewer.snapshot(width, 270, Math.min(devicePixelRatio, 2), null), x, y, width, 270)
    ctx.fillStyle = "#3d4943"; ctx.font = "21px Georgia"; ctx.fillText(assetById.get(families[family].ids[i]!)!.title, x + 24, y + 305)
    ctx.strokeStyle = "#cbd1c5"; ctx.strokeRect(x + 12, y + 8, width - 24, height - 20)
  })
  render()
  canvas.toBlob(blob => {
    if (!blob) return
    if (sheetUrl) URL.revokeObjectURL(sheetUrl)
    sheetUrl = URL.createObjectURL(blob)
    const link = root.querySelector<HTMLAnchorElement>("#sheet-link")!
    link.href = sheetUrl; link.download = `inkline-${family}-review.png`; link.hidden = false
  })
}
let drag: { x: number; yaw: number } | null = null
grid.onpointerdown = e => { if (!(e.target as HTMLElement).classList.contains("model-view")) return; drag = {x: e.clientX, yaw}; grid.setPointerCapture(e.pointerId) }
grid.onpointermove = e => { if (drag) { yaw = ((drag.yaw + (e.clientX - drag.x) * 0.5 + 540) % 360) - 180; render() } }
grid.onpointerup = grid.onpointercancel = () => { drag = null }
let frame = 0
const schedule = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(render) }
addEventListener("scroll", schedule, {passive: true}); addEventListener("resize", schedule)
const query = new URLSearchParams(location.search).get("collection")
select(query && query in families ? query as Family : "buildings")
let effectFrame=0
const animateEffects=()=>{if(families[family].ids.some(id=>assetById.get(id)?.smokeEffect||assetById.get(id)?.fountains?.length))render();effectFrame=requestAnimationFrame(animateEffects)}
effectFrame=requestAnimationFrame(animateEffects)
Object.assign(window, { townModelReview: { stats: () => ({ family, models: models.length, renders, yaw, pitch }) } })
if (import.meta.hot) import.meta.hot.dispose(() => { cancelAnimationFrame(effectFrame);models.forEach(m => m.built.dispose()); viewer.dispose(); removeEventListener("scroll", schedule); removeEventListener("resize", schedule) })