A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D
The building panel pictures an upgraded market or chapel with its model, from the town's camera
SandroMaglione94c2092dd0bc
diff --git a/docs/service-upgrade-assets.md b/docs/service-upgrade-assets.mdindex 20985cc..d505590 100644--- a/docs/service-upgrade-assets.md+++ b/docs/service-upgrade-assets.md@@ -34,8 +34,9 @@ model's eaves (for surface effects and fire) and its top (for the problemmarker). The market hall drops the market's flag. The model cache key includesthe tier.-The panel's picture of a building is still the 2D drawing of the tier 1-building.+The building panel pictures an upgraded building with its model+(`src/render3d/portrait.ts`): drawn once from the town's own camera, at the+town's scale and cropped to the model, beside the 2D pictures of the rest.oes not activate upgrades.diff --git a/src/host.ts b/src/host.tsindex 3871649..6f7ebcb 100644--- a/src/host.ts+++ b/src/host.ts@@ -60,6 +60,8 @@ import { blobOf, cardFacts, drawCard, metaOf, scaled, verdictOf } from "./share/import { handheld, offerLink, offerPicture, upload } from "./share/client.ts"import { PICTURE_HEIGHT, PICTURE_QUALITY, PICTURE_WIDTH } from "./share/format.ts"import { MapInput } from "./render3d/input.ts"+import { portrait } from "./render3d/portrait.ts"+import { serviceBuilding } from "./asset-lab/service-upgrades.ts"const el = <K extends keyof HTMLElementTagNameMap>(tag: K, className: string, text = "") => {const node = document.createElement(tag)@@ -645,13 +647,21 @@ export const createHost = async (// ── Pictures of buildings, for the build menu and the panel ──const pictures = new Map<string, Promise<string>>()- /** A building (or a home of a tier) drawn as the town draws it, as an image. */- const picture = (what: KindId | Tier | `park:${GroundId | PropId}`): HTMLImageElement => {+ /**+ * A building (or a home of a tier) drawn as the town draws it, as an image.+ * An upgraded market or chapel has no 2D drawing: its model is pictured+ * from the town's camera instead (see `portrait`).+ */+ const picture = (what: KindId | Tier | `park:${GroundId | PropId}`, upgraded = false): HTMLImageElement => {const img = document.createElement("img")img.className = "thumb"img.alt = ""- const id = String(what)+ const id = `${String(what)}${upgraded ? "/upgraded" : ""}`let url = pictures.get(id)+ if (!url && upgraded && (what === "market" || what === "chapel")) {+ url = Promise.resolve(portrait(serviceBuilding(what, 2, { doors: { right: true, left: true } })))+ pictures.set(id, url)+ }if (!url) {const g = new Graphics()if (typeof what === "string" && what.startsWith("park:")) drawParkPiece(g, what.slice(5) as GroundId | PropId)@@ -1237,7 +1247,7 @@ export const createHost = async ()const below = el("div", "")below.append(status, about)- panel.append(header(title, p.type === "ruin" ? iconEl("condition", 30) : p.type === "park" ? iconEl("decor", 30) : picture(p.type === "building" ? p.kind : p.tier), below))+ panel.append(header(title, p.type === "ruin" ? iconEl("condition", 30) : p.type === "park" ? iconEl("decor", 30) : picture(p.type === "building" ? p.kind : p.tier, p.type === "building" && p.upgraded), below))const box = section(p.type === "building" ? "at work" : p.type === "ruin" ? "what is left" : p.type === "park" ? "what makes it" : "household", p.type === "building" ? "jobs" : p.type === "park" ? "appeal" : "homes")box.classList.add("lines")const rows: Array<HTMLElement> = []diff --git a/src/render3d/portrait.ts b/src/render3d/portrait.tsnew file mode 100644index 0000000..8448c17--- /dev/null+++ b/src/render3d/portrait.ts@@ -0,0 +1,46 @@+/**+ * A model drawn as a still picture, for the page's panels: from the town's+ * own camera (its first heading and elevation, see `GameScene`), at the+ * town's scale (one unit a pixel at zoom 1), cropped to the model, so it+ * sits beside the 2D pictures the models were built from.+ */+import * as THREE from "three"+import { buildAsset } from "../asset-lab/renderer.ts"+import type { InkAsset } from "../asset-lab/primitives.ts"++let renderer: THREE.WebGLRenderer | null = null++/** A PNG data URL of `asset`, `scale` pixels to a unit, with `margin` units around it. */+export function portrait(asset: InkAsset, scale = 2, margin = 3): string {+ renderer ??= new THREE.WebGLRenderer({ antialias: true, alpha: true, preserveDrawingBuffer: true })+ renderer.setClearColor(0x000000, 0)+ renderer.outputColorSpace = THREE.SRGBColorSpace+ renderer.toneMapping = THREE.NoToneMapping+ const model = buildAsset(asset, { batchFaces: true })+ const scene = new THREE.Scene()+ scene.add(model.group)+ const heading = Math.PI / 4, elevation = Math.PI / 6, distance = 6000+ const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 14000)+ camera.position.set(Math.sin(heading) * Math.cos(elevation) * distance, Math.sin(elevation) * distance, Math.cos(heading) * Math.cos(elevation) * distance)+ camera.lookAt(0, 0, 0)+ camera.updateMatrixWorld(true)+ // The model's extent as the camera sees it: its bounding box's corners in view space.+ const box = new THREE.Box3().setFromObject(model.group)+ const view = new THREE.Box3()+ for (const x of [box.min.x, box.max.x]) for (const y of [box.min.y, box.max.y]) for (const z of [box.min.z, box.max.z])+ view.expandByPoint(new THREE.Vector3(x, y, z).applyMatrix4(camera.matrixWorldInverse))+ camera.left = view.min.x - margin+ camera.right = view.max.x + margin+ camera.bottom = view.min.y - margin+ camera.top = view.max.y + margin+ camera.updateProjectionMatrix()+ const width = Math.ceil(camera.right - camera.left), height = Math.ceil(camera.top - camera.bottom)+ renderer.setPixelRatio(scale)+ renderer.setSize(width, height, false)+ for (const { material, width: line } of model.lineMaterials) material.linewidth = line+ model.updateContours(camera)+ renderer.render(scene, camera)+ const url = renderer.domElement.toDataURL("image/png")+ model.dispose()+ return url+}