Spaces:
Paused
Paused
Download src/jiangnan/engine/layout.ts from Ethscriptions/video: direct link, hf CLI and curl.
- Browser
- Download file 12.3 kB
-
https://huggingface.co/spaces/Ethscriptions/video/resolve/main/src/jiangnan/engine/layout.ts
- Command line
-
hf download hf://spaces/Ethscriptions/video/src/jiangnan/engine/layout.ts
-
curl -L -o layout.ts https://huggingface.co/spaces/Ethscriptions/video/resolve/main/src/jiangnan/engine/layout.ts
12.3 kB
| // World layout: river path, terrain height field and placement of every | |
| // landmark. Units are metres, +y is up, the sun sets toward -x (west). | |
| import { clamp, fbm2, lerp, mulberry32, ridged2, smoothstep, valueNoise2 } from './rng' | |
| // ---------------------------------------------------------------- river path | |
| const RIVER_CTRL: [number, number][] = [ | |
| [-1500, -330], [-1050, -260], [-760, -150], [-520, -70], [-330, -22], [-190, 6], [-80, 14], | |
| [30, 4], [120, -10], [220, 6], [330, 46], [470, 92], [660, 104], [900, 60], [1300, -40], [1700, -120], | |
| ] | |
| function catmull(p0: number, p1: number, p2: number, p3: number, t: number) { | |
| const t2 = t * t, t3 = t2 * t | |
| return 0.5 * (2 * p1 + (-p0 + p2) * t + (2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 + (-p0 + 3 * p1 - 3 * p2 + p3) * t3) | |
| } | |
| export interface RiverSample { x: number; z: number; tx: number; tz: number; s: number } | |
| function buildRiver(): RiverSample[] { | |
| const raw: [number, number][] = [] | |
| const n = RIVER_CTRL.length | |
| for (let i = 0; i < n - 1; i++) { | |
| const p0 = RIVER_CTRL[Math.max(0, i - 1)], p1 = RIVER_CTRL[i], p2 = RIVER_CTRL[i + 1], p3 = RIVER_CTRL[Math.min(n - 1, i + 2)] | |
| const steps = 80 | |
| for (let k = 0; k < steps; k++) { | |
| const t = k / steps | |
| raw.push([catmull(p0[0], p1[0], p2[0], p3[0], t), catmull(p0[1], p1[1], p2[1], p3[1], t)]) | |
| } | |
| } | |
| raw.push(RIVER_CTRL[n - 1]) | |
| // resample by arc length every 2 m | |
| const cum: number[] = [0] | |
| for (let i = 1; i < raw.length; i++) cum.push(cum[i - 1] + Math.hypot(raw[i][0] - raw[i - 1][0], raw[i][1] - raw[i - 1][1])) | |
| const total = cum[cum.length - 1] | |
| const out: RiverSample[] = [] | |
| let j = 0 | |
| for (let s = 0; s <= total; s += 2) { | |
| while (j < cum.length - 2 && cum[j + 1] < s) j++ | |
| const f = (s - cum[j]) / Math.max(1e-6, cum[j + 1] - cum[j]) | |
| const x = lerp(raw[j][0], raw[j + 1][0], f), z = lerp(raw[j][1], raw[j + 1][1], f) | |
| out.push({ x, z, tx: 0, tz: 0, s }) | |
| } | |
| for (let i = 0; i < out.length; i++) { | |
| const a = out[Math.max(0, i - 2)], b = out[Math.min(out.length - 1, i + 2)] | |
| const l = Math.hypot(b.x - a.x, b.z - a.z) || 1 | |
| out[i].tx = (b.x - a.x) / l | |
| out[i].tz = (b.z - a.z) / l | |
| } | |
| return out | |
| } | |
| export const RIVER = buildRiver() | |
| export const RIVER_LEN = RIVER[RIVER.length - 1].s | |
| /** s (arc length) of the river point nearest to x (river runs roughly along +x). */ | |
| export function riverSAtX(x: number) { | |
| let best = 0, bd = 1e9 | |
| for (let i = 0; i < RIVER.length; i++) { | |
| const d = Math.abs(RIVER[i].x - x) | |
| if (d < bd) { bd = d; best = i } | |
| } | |
| return RIVER[best].s | |
| } | |
| export function riverAt(s: number): RiverSample { | |
| const f = clamp(s / 2, 0, RIVER.length - 1.001) | |
| const i = Math.floor(f), t = f - i | |
| const a = RIVER[i], b = RIVER[i + 1] | |
| const tx = lerp(a.tx, b.tx, t), tz = lerp(a.tz, b.tz, t) | |
| const l = Math.hypot(tx, tz) || 1 | |
| return { x: lerp(a.x, b.x, t), z: lerp(a.z, b.z, t), tx: tx / l, tz: tz / l, s } | |
| } | |
| /** Point at arc length s, offset sideways by `off` (+ = south bank / +z side). */ | |
| export function riverPoint(s: number, off: number) { | |
| const r = riverAt(s) | |
| // left normal of the flow direction (flow is +x, normal points +z = south) | |
| const nx = -r.tz, nz = r.tx | |
| return { x: r.x + nx * off, z: r.z + nz * off, tx: r.tx, tz: r.tz, nx, nz, angle: Math.atan2(r.tx, r.tz) } | |
| } | |
| export const VILLAGE_S0 = riverSAtX(-300) | |
| export const VILLAGE_S1 = riverSAtX(290) | |
| /** Half width of the water surface. Narrow, walled canal inside the village. */ | |
| export function riverHalfWidth(s: number) { | |
| const inV = smoothstep(VILLAGE_S0 - 120, VILLAGE_S0, s) * (1 - smoothstep(VILLAGE_S1, VILLAGE_S1 + 120, s)) | |
| return lerp(13 + 3 * valueNoise2(s * 0.01, 3), 8, inV) | |
| } | |
| export function villageFactor(s: number) { | |
| return smoothstep(VILLAGE_S0 - 30, VILLAGE_S0 + 10, s) * (1 - smoothstep(VILLAGE_S1 - 10, VILLAGE_S1 + 30, s)) | |
| } | |
| // spatial hash of river samples for fast distance queries | |
| const CELL = 24 | |
| const GRID = new Map<number, number[]>() | |
| { | |
| for (let i = 0; i < RIVER.length; i++) { | |
| const cx = Math.floor(RIVER[i].x / CELL), cz = Math.floor(RIVER[i].z / CELL) | |
| for (let dx = -3; dx <= 3; dx++) for (let dz = -3; dz <= 3; dz++) { | |
| const k = (cx + dx) * 100003 + (cz + dz) | |
| let arr = GRID.get(k) | |
| if (!arr) { arr = []; GRID.set(k, arr) } | |
| arr.push(i) | |
| } | |
| } | |
| } | |
| export interface RiverQuery { d: number; s: number; side: number } | |
| const FAR: RiverQuery = { d: 72, s: 0, side: 1 } | |
| /** Distance to the river centreline (capped at ~72 m beyond which it is approximate). */ | |
| export function riverQuery(x: number, z: number): RiverQuery { | |
| const k = Math.floor(x / CELL) * 100003 + Math.floor(z / CELL) | |
| const arr = GRID.get(k) | |
| if (!arr) return riverQueryFar(x, z) | |
| let bd = 1e18, bi = -1 | |
| for (const i of arr) { | |
| const p = RIVER[i] | |
| const d = (p.x - x) ** 2 + (p.z - z) ** 2 | |
| if (d < bd) { bd = d; bi = i } | |
| } | |
| const p = RIVER[bi] | |
| const side = (x - p.x) * -p.tz + (z - p.z) * p.tx >= 0 ? 1 : -1 | |
| return { d: Math.sqrt(bd), s: p.s, side } | |
| } | |
| function riverQueryFar(x: number, z: number): RiverQuery { | |
| // coarse search on every 10th sample | |
| let bd = 1e18, bi = 0 | |
| for (let i = 0; i < RIVER.length; i += 10) { | |
| const p = RIVER[i] | |
| const d = (p.x - x) ** 2 + (p.z - z) ** 2 | |
| if (d < bd) { bd = d; bi = i } | |
| } | |
| const p = RIVER[bi] | |
| const side = (x - p.x) * -p.tz + (z - p.z) * p.tx >= 0 ? 1 : -1 | |
| return { d: Math.max(FAR.d, Math.sqrt(bd)), s: p.s, side } | |
| } | |
| // ---------------------------------------------------------------- terrain | |
| export const GROUND_Y = 1.7 | |
| export const TERRACE = { cx: 330, cz: -470, rx: 230, rz: 170, step: 2.6 } | |
| export function terraceMask(x: number, z: number) { | |
| const u = (x - TERRACE.cx) / TERRACE.rx, v = (z - TERRACE.cz) / TERRACE.rz | |
| const r = Math.sqrt(u * u + v * v) + (valueNoise2(x * 0.02, z * 0.02, 41) - 0.5) * 0.25 | |
| return 1 - smoothstep(0.78, 1.0, r) | |
| } | |
| function baseHeight(x: number, z: number, q: RiverQuery) { | |
| const d = q.d | |
| // gentle undulating fields outside the village | |
| let h = GROUND_Y + (fbm2(x * 0.012, z * 0.012, 3, 5) - 0.5) * 1.6 * smoothstep(50, 140, d) | |
| // valley walls: the further from the river the higher the hills | |
| const far = Math.hypot(x * 0.8, z) | |
| const hd = Math.min(d, far * 0.9 + 40) | |
| const hillT = smoothstep(130, 560, hd) | |
| h += hillT * hillT * (60 + 190 * fbm2(x * 0.0022 + 3, z * 0.0022, 5, 7)) | |
| h += hillT * (ridged2(x * 0.0045, z * 0.0045, 5, 3) - 0.35) * 70 * hillT | |
| // small knolls inside the valley (bamboo groves) | |
| h += smoothstep(70, 160, d) * Math.max(0, fbm2(x * 0.006, z * 0.006, 3, 19) - 0.55) * 60 | |
| return h | |
| } | |
| /** Height of the visible ground (river channel carved in). */ | |
| export function terrainHeight(x: number, z: number, carveTerraces = true) { | |
| const q = riverQuery(x, z) | |
| let h = baseHeight(x, z, q) | |
| // terraces carved into the hillside | |
| const m = terraceMask(x, z) | |
| if (m > 0) { | |
| const hq = terraceQuantize(h) | |
| h = lerp(h, hq, m) | |
| if (carveTerraces) h -= m * 1.2 // the detailed terrace mesh sits on top | |
| } | |
| // river channel | |
| const w = riverHalfWidth(q.s) | |
| const vf = villageFactor(q.s) | |
| if (q.d < w + 30) { | |
| const bankW = lerp(9, 1.2, vf) | |
| const bank = smoothstep(w - bankW * 0.6, w + bankW * 0.4, q.d) | |
| const bed = -2.6 + (1 - smoothstep(0, w, q.d)) * -0.8 | |
| h = lerp(bed, h, bank) | |
| } | |
| return h | |
| } | |
| export function terraceQuantize(h: number) { | |
| const u = h / TERRACE.step | |
| const f = u - Math.floor(u) | |
| // flat paddy (82%), steep riser; slight bund lip at the outer edge | |
| const riser = smoothstep(0.8, 1.0, f) | |
| const lip = Math.exp(-((f - 0.04) ** 2) / 0.0006) * 0.12 | |
| return TERRACE.step * (Math.floor(u) + riser) + lip | |
| } | |
| export function terraceHeight(x: number, z: number) { | |
| const q = riverQuery(x, z) | |
| const h = baseHeight(x, z, q) | |
| const m = terraceMask(x, z) | |
| return lerp(h, terraceQuantize(h), m) | |
| } | |
| /** 0..1 value describing the position inside a terrace step (for water vs riser). */ | |
| export function terraceStepFrac(x: number, z: number) { | |
| const q = riverQuery(x, z) | |
| const h = baseHeight(x, z, q) | |
| const u = h / TERRACE.step | |
| return u - Math.floor(u) | |
| } | |
| /** Far mountain ring height (beyond the detailed terrain). */ | |
| export function farMountainHeight(x: number, z: number) { | |
| const r = Math.hypot(x, z) | |
| const ang = Math.atan2(z, x) | |
| // the sunset side (west-north-west) opens into a broad low valley so the sun | |
| // stays visible until it is almost on the horizon; other sides are higher ranges | |
| let dSun = ang - (Math.PI + 0.2) | |
| dSun = Math.atan2(Math.sin(dSun), Math.cos(dSun)) | |
| const west = Math.exp(-(dSun * dSun) / 0.5) | |
| const gap = 1 - 0.5 * Math.exp(-((ang + 0.05) ** 2) / 0.02) | |
| const rise = smoothstep(700, 2600, r) | |
| let h = rise * (150 + 360 * ridged2(x * 0.0009, z * 0.0009, 6, 77)) * gap * (1 - 0.74 * west) | |
| h += smoothstep(600, 1400, r) * 90 * fbm2(x * 0.002, z * 0.002, 4, 9) * (1 - 0.5 * west) | |
| return h | |
| } | |
| // ---------------------------------------------------------------- placements | |
| export interface HouseSpec { | |
| x: number; z: number; rot: number // rotation about y; local +z faces the street / river | |
| w: number; d: number; floors: number; gableSteps: number | |
| shop: boolean; side: number; s: number; seed: number | |
| chimney: boolean; lantern: boolean; order: number // lantern / window lighting order | |
| } | |
| export interface Landmarks { | |
| bridgeS: number; wheelS: number; treeS: number; marketS0: number; marketS1: number | |
| } | |
| export const BRIDGE_S = riverSAtX(40) | |
| export const WHEEL_S = riverSAtX(-196) | |
| export const TREE_POS = (() => { const p = riverPoint(riverSAtX(-300), -34); return { x: p.x, z: p.z } })() | |
| export const MARKET_S0 = riverSAtX(-130) | |
| export const MARKET_S1 = riverSAtX(30) | |
| export const PAVILION_POS = { x: 205, z: -372 } | |
| function blocked(s: number, side: number, half: number) { | |
| if (Math.abs(s - BRIDGE_S) < half + 8) return true | |
| if (side > 0 && Math.abs(s - WHEEL_S) < half + 10) return true | |
| if (side < 0 && s < riverSAtX(-264) + half) return true // entrance plaza with the camphor tree | |
| return false | |
| } | |
| export function buildHouses(): HouseSpec[] { | |
| const rng = mulberry32(20261022) | |
| const out: HouseSpec[] = [] | |
| const rows = [ | |
| { side: -1, off: 14.6, shop: true, jitter: 0.3 }, // north: market street shops behind a 6 m quay | |
| { side: 1, off: 8.4, shop: false, jitter: 0.2 }, // south: houses standing right on the water | |
| { side: -1, off: 31, shop: false, jitter: 2.5 }, | |
| { side: 1, off: 25, shop: false, jitter: 2.5 }, | |
| { side: -1, off: 50, shop: false, jitter: 5 }, | |
| { side: 1, off: 44, shop: false, jitter: 5 }, | |
| ] | |
| let order = 0 | |
| for (const row of rows) { | |
| let s = VILLAGE_S0 + rng() * 6 + (row.off > 20 ? 10 : 0) | |
| const sEnd = VILLAGE_S1 - (row.off > 40 ? 40 : 0) | |
| while (s < sEnd) { | |
| const w = 8 + rng() * 5 | |
| const half = w / 2 | |
| const sc = s + half | |
| if (row.off > 40 && rng() < 0.3) { s += w + 6; continue } | |
| if (!blocked(sc, row.side, half)) { | |
| const d = 8.5 + rng() * 3.5 | |
| const off = row.side * (row.off + d / 2 + (rng() - 0.5) * row.jitter) | |
| const p = riverPoint(sc, off) | |
| // front faces the river: local +z points toward the centreline | |
| const facing = Math.atan2(-row.side * p.nx, -row.side * p.nz) | |
| const floors = rng() < (row.shop ? 0.75 : 0.55) ? 2 : 1 | |
| out.push({ | |
| x: p.x, z: p.z, rot: facing + (rng() - 0.5) * 0.05 * (row.off > 20 ? 3 : 1), | |
| w, d, floors, gableSteps: floors === 2 ? (rng() < 0.6 ? 3 : 2) : (rng() < 0.5 ? 2 : 1), | |
| shop: row.shop, side: row.side, s: sc, seed: Math.floor(rng() * 1e9), | |
| chimney: rng() < 0.35, lantern: row.off < 20, order: order++, | |
| }) | |
| } | |
| s += w + (row.shop ? 0.2 + rng() * 0.6 : 0.6 + rng() * 3.5) | |
| if (row.side > 0 && row.off < 10 && rng() < 0.18) s += 5 // water steps / alleys between houses | |
| } | |
| } | |
| return out | |
| } | |
| /** Path the ox cart and walkers follow along the north quay (market street). */ | |
| export function quayPoint(s: number, lane = 0) { | |
| return riverPoint(s, -(11 + lane)) | |
| } | |
| export function terrainNormalApprox(x: number, z: number) { | |
| const e = 1.5 | |
| const hx = terrainHeight(x + e, z) - terrainHeight(x - e, z) | |
| const hz = terrainHeight(x, z + e) - terrainHeight(x, z - e) | |
| const l = Math.hypot(hx, 2 * e, hz) | |
| return { x: -hx / l, y: (2 * e) / l, z: -hz / l } | |
| } | |