383 lines
15 KiB
TypeScript
383 lines
15 KiB
TypeScript
// The village's people, animals, crows and smoke for the WebGL diorama: the same flat
|
|
// panels as VillageWalker, VillageAnimal and VillageBird, triangulated once, and posed every
|
|
// frame from the same timing (app/diorama/life.ts) the SVG animations ran on.
|
|
|
|
import earcut from 'earcut'
|
|
import { scale } from './iso'
|
|
import { type Cycle, drillThrust, type Tool, workCycle, workInPlace } from './life'
|
|
import { polylines, Track } from './svgpath'
|
|
|
|
export type RGB = [number, number, number]
|
|
const colors = new Map<string, RGB>()
|
|
export function rgbOf(hex: string): RGB {
|
|
let c = colors.get(hex)
|
|
if (!c) {
|
|
const n = Number.parseInt(hex.slice(1), 16)
|
|
c = [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255]
|
|
colors.set(hex, c)
|
|
}
|
|
return c
|
|
}
|
|
|
|
/** Triangles of filled outlines, as x,y pairs per corner. */
|
|
export function triangles(...rings: [number, number][][]): Float32Array {
|
|
const out: number[] = []
|
|
for (const ring of rings) {
|
|
const flat = ring.flat()
|
|
for (const i of earcut(flat)) out.push(flat[i * 2]!, flat[i * 2 + 1]!)
|
|
}
|
|
return new Float32Array(out)
|
|
}
|
|
const path = (d: string) => triangles(...polylines(d))
|
|
const rect = (x: number, y: number, w: number, h: number) => triangles([[x, y], [x + w, y], [x + w, y + h], [x, y + h]])
|
|
const circle = (cx: number, cy: number, r: number, n = 12) =>
|
|
triangles(Array.from({ length: n }, (_, i): [number, number] => [cx + r * Math.cos((i / n) * Math.PI * 2), cy + r * Math.sin((i / n) * Math.PI * 2)]))
|
|
|
|
// Affine maps as SVG writes them: (x, y) -> (a x + c y + e, b x + d y + f).
|
|
export type M = [number, number, number, number, number, number]
|
|
export const IDENTITY: M = [1, 0, 0, 1, 0, 0]
|
|
export function mul(p: M, q: M): M {
|
|
return [p[0] * q[0] + p[2] * q[1], p[1] * q[0] + p[3] * q[1], p[0] * q[2] + p[2] * q[3], p[1] * q[2] + p[3] * q[3], p[0] * q[4] + p[2] * q[5] + p[4], p[1] * q[4] + p[3] * q[5] + p[5]]
|
|
}
|
|
export const move = (x: number, y: number): M => [1, 0, 0, 1, x, y]
|
|
export const stretch = (x: number, y = x): M => [x, 0, 0, y, 0, 0]
|
|
/** SVG's rotate(deg cx cy): clockwise on screen. */
|
|
export function turn(deg: number, cx: number, cy: number): M {
|
|
const a = (deg * Math.PI) / 180
|
|
const c = Math.cos(a)
|
|
const s = Math.sin(a)
|
|
return [c, s, -s, c, cx - c * cx + s * cy, cy - s * cx - c * cy]
|
|
}
|
|
|
|
/** Corners written per frame: positions (x, -y, z) and colours, for one dynamic mesh. */
|
|
export class Batch {
|
|
readonly positions: Float32Array
|
|
readonly colors: Float32Array
|
|
count = 0
|
|
|
|
constructor(readonly capacity: number) {
|
|
this.positions = new Float32Array(capacity * 3)
|
|
this.colors = new Float32Array(capacity * 3)
|
|
}
|
|
|
|
put(tris: Float32Array, color: RGB, m: M, z: number) {
|
|
const n = Math.min(tris.length / 2, this.capacity - this.count)
|
|
for (let i = 0; i < n; i++) {
|
|
const x = tris[i * 2]!
|
|
const y = tris[i * 2 + 1]!
|
|
const k = (this.count + i) * 3
|
|
this.positions[k] = m[0] * x + m[2] * y + m[4]
|
|
this.positions[k + 1] = -(m[1] * x + m[3] * y + m[5])
|
|
this.positions[k + 2] = z
|
|
this.colors[k] = color[0]
|
|
this.colors[k + 1] = color[1]
|
|
this.colors[k + 2] = color[2]
|
|
}
|
|
this.count += n
|
|
}
|
|
}
|
|
|
|
/** Something that moves: draws itself into the batch at time `t` (seconds). */
|
|
export interface Actor {
|
|
/** Corners it writes at most. */
|
|
readonly corners: number
|
|
/** Only seen while things move (not under reduced motion). */
|
|
readonly moving: boolean
|
|
draw(b: Batch, t: number, motion: boolean, z: number): void
|
|
}
|
|
|
|
// SMIL value lists, sampled like the browser does.
|
|
|
|
interface Keys {
|
|
values: number[][]
|
|
times: number[]
|
|
}
|
|
function keys(values: string, keyTimes: string): Keys {
|
|
return {
|
|
values: values.split(';').map((v) => (v === 'visible' ? [1] : v === 'hidden' ? [0] : v.trim().split(/[\s,]+/).map(Number))),
|
|
times: keyTimes.split(';').map(Number),
|
|
}
|
|
}
|
|
function linear(k: Keys, f: number): number[] {
|
|
const { values, times } = k
|
|
if (f <= times[0]!) return values[0]!
|
|
for (let i = 1; i < times.length; i++) {
|
|
if (f <= times[i]!) {
|
|
const span = times[i]! - times[i - 1]!
|
|
const t = span > 0 ? (f - times[i - 1]!) / span : 1
|
|
return values[i - 1]!.map((v, j) => v + (values[i]![j]! - v) * t)
|
|
}
|
|
}
|
|
return values[values.length - 1]!
|
|
}
|
|
function discrete(k: Keys, f: number): number[] {
|
|
let i = 0
|
|
while (i + 1 < k.times.length && f >= k.times[i + 1]!) i++
|
|
return k.values[i]!
|
|
}
|
|
/** Where in its cycle a clock that began at `begin` is, 0..1. */
|
|
const phase = (t: number, begin: number, dur: number) => ((((t - begin) % dur) + dur) % dur) / dur
|
|
|
|
// People.
|
|
|
|
const METAL = rgbOf('#868b8e')
|
|
const HANDLE = rgbOf('#5f4c3a')
|
|
const HEADS: Record<Tool, string> = {
|
|
axe: 'M3.3 -11.4L5.5 -10.5L5 -9.3L3.6 -10.1z',
|
|
pick: 'M2.1 -11.7L5.7 -10.1L5.4 -9.7L3.8 -10.5L2.3 -11.1z',
|
|
shovel: 'M3.3 -10.5L4.8 -12L5.7 -11.1L4.2 -9.7z',
|
|
sickle: 'M3.6 -10.9Q6.6 -12.3 5.7 -9.1Q5.3 -10.8 3.9 -10.4z',
|
|
hammer: 'M2.9 -11.2L4.6 -10.3L4.1 -9.3L2.4 -10.1z',
|
|
spear: 'M1.2 -15.6L1.8 -13.6L0.6 -13.6z',
|
|
}
|
|
let person: ReturnType<typeof personShapes> | null = null
|
|
function personShapes() {
|
|
return {
|
|
legA: rect(-1.5, -2.4, 1.2, 2.4),
|
|
legB: rect(0.3, -2.4, 1.2, 2.4),
|
|
shirtA: path('M-1.9 -6.9L0 -7.3V-2.2H-1.7z'),
|
|
shirtB: path('M0 -7.3L1.9 -6.9L1.7 -2.2H0z'),
|
|
head: circle(0, -8.9, 1.65),
|
|
helmet: path('M-1.85 -9Q-1.8 -11.2 0 -11.3Q1.8 -11.2 1.85 -9z'),
|
|
helmetShade: path('M0 -11.3Q1.8 -11.2 1.85 -9H0z'),
|
|
strap: path('M0.75 -9H1.15V-7.9H0.75z'),
|
|
hair: path('M-1.7 -9.1Q0 -11.5 1.7 -9.1Q0 -9.9 -1.7 -9.1z'),
|
|
shaft: path('M0.95 -0.6H1.45V-13.8H0.95z'),
|
|
handle: path('M0.85 -6.6L1.35 -6.2L4.15 -10.7L3.65 -11.1z'),
|
|
heads: Object.fromEntries(Object.entries(HEADS).map(([tool, d]) => [tool, path(d)])) as Record<Tool, Float32Array>,
|
|
shield: path('M-0.1 -6.9H2.5V-4.7Q2.5 -3.1 1.2 -2.1Q-0.1 -3.1 -0.1 -4.7z'),
|
|
shieldShade: path('M1.2 -6.9H2.5V-4.7Q2.5 -3.1 1.2 -2.1z'),
|
|
boss: path('M1 -6.9H1.4V-2.3L1.2 -2.1L1 -2.3z'),
|
|
loadTop: path('M-0.6 -9.3L2.2 -9.8L3.8 -9L1 -8.5z'),
|
|
loadA: path('M-0.6 -9.3L1 -8.5V-6.9L-0.6 -7.7z'),
|
|
loadB: path('M1 -8.5L3.8 -9V-7.4L1 -6.9z'),
|
|
}
|
|
}
|
|
const PERSON_CORNERS = 400
|
|
|
|
interface Look {
|
|
shirt: string
|
|
carry?: string | null
|
|
tool?: Tool | null
|
|
soldier?: boolean
|
|
}
|
|
interface Pose {
|
|
/** 1 facing right, -1 left. */
|
|
fx: number
|
|
legA: number
|
|
legB: number
|
|
/** Tool angle about the shoulder, degrees. */
|
|
swing: number
|
|
load: boolean
|
|
}
|
|
|
|
/** One villager as VillageWalker draws it: legs, shirt, head, tool, shield, load. */
|
|
function drawPerson(b: Batch, look: Look, pose: Pose, base: M, z: number) {
|
|
const s = (person ??= personShapes())
|
|
const shirt = rgbOf(look.shirt)
|
|
const m = mul(base, stretch(pose.fx, 1))
|
|
b.put(s.legA, rgbOf('#3a322a'), mul(m, move(pose.legA, 0)), z)
|
|
b.put(s.legB, rgbOf('#2f2822'), mul(m, move(pose.legB, 0)), z)
|
|
b.put(s.shirtA, shirt, m, z)
|
|
b.put(s.shirtB, rgbOf(scale(look.shirt, 0.72)), m, z)
|
|
b.put(s.head, rgbOf('#be9d82'), m, z)
|
|
if (look.soldier) {
|
|
b.put(s.helmet, METAL, m, z)
|
|
b.put(s.helmetShade, rgbOf('#62676a'), m, z)
|
|
b.put(s.strap, rgbOf('#62676a'), m, z)
|
|
} else {
|
|
b.put(s.hair, rgbOf('#3a2e24'), m, z)
|
|
}
|
|
if (look.tool) {
|
|
const t = pose.swing ? mul(m, turn(pose.swing, 1.1, -6.4)) : m
|
|
b.put(look.tool === 'spear' ? s.shaft : s.handle, HANDLE, t, z)
|
|
b.put(s.heads[look.tool], METAL, t, z)
|
|
}
|
|
if (look.soldier) {
|
|
b.put(s.shield, rgbOf('#6e1d16'), m, z)
|
|
b.put(s.shieldShade, rgbOf('#4f140f'), m, z)
|
|
b.put(s.boss, rgbOf('#9a8f78'), m, z)
|
|
}
|
|
if (look.carry && pose.load) {
|
|
b.put(s.loadTop, rgbOf(scale(look.carry, 1.15)), m, z)
|
|
b.put(s.loadA, rgbOf(look.carry), m, z)
|
|
b.put(s.loadB, rgbOf(scale(look.carry, 0.7)), m, z)
|
|
}
|
|
}
|
|
|
|
/** A villager going out along `d` to work and bringing the load home, as VillageWorker. */
|
|
export function worker(o: Look & { d: string; walk: number; work: number; rest?: number; indoors?: boolean; size: number; begin: number }): Actor {
|
|
const track = new Track(o.d)
|
|
const [from, to] = track.ends
|
|
const cycle: Cycle = workCycle(o.walk, o.work, o.rest ?? 1.2, to[0] < from[0] ? -1 : 1, o.indoors)
|
|
const k = {
|
|
motion: keys(cycle.motion.keyPoints, cycle.motion.keyTimes),
|
|
step: keys(cycle.step.values, cycle.step.keyTimes),
|
|
mirrored: keys(cycle.step.mirrored, cycle.step.keyTimes),
|
|
swing: keys(cycle.swing.values, cycle.swing.keyTimes),
|
|
load: keys(cycle.load.values, cycle.load.keyTimes),
|
|
face: keys(cycle.face.values, cycle.face.keyTimes),
|
|
presence: cycle.presence ? keys(cycle.presence.values, cycle.presence.keyTimes) : null,
|
|
}
|
|
// A spear rides on the shoulder while walking; other tools swing at work.
|
|
const swings = !!o.tool && o.tool !== 'spear'
|
|
return {
|
|
corners: PERSON_CORNERS,
|
|
moving: true,
|
|
draw(b, t, motion, z) {
|
|
if (!motion) return
|
|
const f = phase(t, o.begin, cycle.dur)
|
|
if (k.presence && !discrete(k.presence, f)[0]) return
|
|
const [x, y] = track.point(linear(k.motion, f)[0]!)
|
|
const pose: Pose = {
|
|
fx: discrete(k.face, f)[0]!,
|
|
legA: linear(k.step, f)[0]!,
|
|
legB: linear(k.mirrored, f)[0]!,
|
|
swing: swings ? linear(k.swing, f)[0]! : 0,
|
|
load: !!discrete(k.load, f)[0],
|
|
}
|
|
drawPerson(b, o, pose, mul(move(x, y), stretch(o.size)), z)
|
|
},
|
|
}
|
|
}
|
|
|
|
const BOB = keys('0 0;0 -0.5;0 0;0 0', '0;0.15;0.3;1')
|
|
|
|
/** A villager who stays put: talking (bobbing a little), hammering, drilling, or on guard. */
|
|
export function standing(o: { at: [number, number]; facing: 1 | -1; tool: Tool | null; shirt: string; drill?: boolean; guard?: boolean; bob: number }): Actor {
|
|
const spec = o.guard || !o.tool ? null : o.drill ? drillThrust() : workInPlace()
|
|
const swing = spec ? keys(spec.values, spec.keyTimes) : null
|
|
const look: Look = o.guard ? { shirt: o.shirt, tool: 'spear', soldier: true } : { shirt: o.shirt, tool: o.tool }
|
|
return {
|
|
corners: PERSON_CORNERS,
|
|
moving: false,
|
|
draw(b, t, motion, z) {
|
|
const lift = motion && !o.tool && !o.guard ? linear(BOB, phase(t, 0, o.bob))[1]! : 0
|
|
const angle = motion && swing && spec ? linear(swing, phase(t, 0, spec.dur))[0]! : 0
|
|
drawPerson(b, look, { fx: o.facing, legA: 0, legB: 0, swing: angle, load: true }, mul(move(o.at[0], o.at[1]), mul(stretch(1.6), move(0, lift))), z)
|
|
},
|
|
}
|
|
}
|
|
|
|
// Animals and crows.
|
|
|
|
let animals: ReturnType<typeof animalShapes> | null = null
|
|
function animalShapes() {
|
|
return {
|
|
sheep: {
|
|
body: [
|
|
{ tris: path('M-1 -0.9h0.6V0h-0.6zM1.2 -0.9h0.6V0h-0.6z'), color: rgbOf('#2e2722') },
|
|
{ tris: path('M-2.2 -3.4Q-2.4 -1 -0.6 -0.7H2Q3 -1.6 2.4 -3.4Q0.2 -4.6 -2.2 -3.4z'), color: rgbOf('#b5ae9c') },
|
|
{ tris: path('M0.2 -0.7H2Q3 -1.6 2.4 -3.4Q1.4 -3.9 0.2 -3.9z'), color: rgbOf('#968f80') },
|
|
],
|
|
head: [{ tris: path('M2.1 -3.3L3.7 -3.1L3.6 -1.9L2.3 -2.1z'), color: rgbOf('#2e2722') }],
|
|
pivot: [2.2, -2.6] as [number, number],
|
|
peck: { k: keys('0;28;28;0;0', '0;0.15;0.6;0.75;1'), dur: 2.2 },
|
|
},
|
|
chicken: {
|
|
body: [
|
|
{ tris: path('M-0.3 -0.8h0.3V0h-0.3zM0.4 -0.8h0.3V0h-0.3z'), color: rgbOf('#a07e3a') },
|
|
{ tris: path('M-1.4 -2.6Q-1.2 -0.7 0.4 -0.8Q1.4 -1.2 1.2 -2.2Q0 -3.2 -1.4 -2.6z'), color: rgbOf('#86663f') },
|
|
],
|
|
head: [
|
|
{ tris: path('M0.8 -3.4L1.7 -3.2L1.6 -2.2L0.9 -2.3z'), color: rgbOf('#86663f') },
|
|
{ tris: path('M1.7 -3L2.3 -2.7L1.7 -2.5z'), color: rgbOf('#a07e3a') },
|
|
{ tris: path('M0.9 -3.6L1.4 -3.9L1.4 -3.3z'), color: rgbOf('#7e2218') },
|
|
],
|
|
pivot: [1, -2.2] as [number, number],
|
|
peck: { k: keys('0;40;0;0', '0;0.2;0.4;1'), dur: 0.7 },
|
|
},
|
|
crow: path('M0 0L-3.8 -1.6L-1.6 0.3zM0 0L3.8 -1.6L1.6 0.3zM-0.5 -0.3H0.5V0.6H-0.5z'),
|
|
}
|
|
}
|
|
// Four stretches of the loop, each followed by a stop to graze or peck.
|
|
const WANDER = keys('0;0.25;0.25;0.5;0.5;0.75;0.75;1;1', '0;0.12;0.3;0.42;0.55;0.67;0.8;0.92;1')
|
|
|
|
/** A sheep or a chicken wandering its loop, head going down now and then, as VillageAnimal. */
|
|
export function animal(o: { kind: 'sheep' | 'chicken'; d: string; dur: number; begin: number; size: number }): Actor {
|
|
const track = new Track(o.d)
|
|
return {
|
|
corners: 120,
|
|
moving: true,
|
|
draw(b, t, motion, z) {
|
|
if (!motion) return
|
|
const a = (animals ??= animalShapes())[o.kind]
|
|
const [x, y] = track.point(linear(WANDER, phase(t, o.begin, o.dur))[0]!)
|
|
const m = mul(move(x, y), stretch(o.size))
|
|
for (const p of a.body) b.put(p.tris, p.color, m, z)
|
|
const head = mul(m, turn(linear(a.peck.k, phase(t, o.begin, a.peck.dur))[0]!, a.pivot[0], a.pivot[1]))
|
|
for (const p of a.head) b.put(p.tris, p.color, head, z)
|
|
},
|
|
}
|
|
}
|
|
|
|
const FLAP = keys('1 1;1 -0.6;1 1', '0;0.5;1')
|
|
const BLACK = rgbOf('#121212')
|
|
|
|
/** A crow crossing or circling at an even speed, wings beating, as VillageBird. */
|
|
export function crow(o: { d: string; dur: number; begin: number; size: number }): Actor {
|
|
const track = new Track(o.d)
|
|
return {
|
|
corners: 30,
|
|
moving: true,
|
|
draw(b, t, motion, z) {
|
|
if (!motion) return
|
|
const [x, y] = track.point(phase(t, o.begin, o.dur))
|
|
const flap = linear(FLAP, phase(t, o.begin, 0.45))[1]!
|
|
b.put((animals ??= animalShapes()).crow, BLACK, mul(move(x, y), mul(stretch(o.size), stretch(1, flap))), z)
|
|
},
|
|
}
|
|
}
|
|
|
|
// Smoke: three flat puffs per chimney that rise, drift and shrink away (3.9 s each).
|
|
|
|
const PUFF = { dur: 3.9, r: 5.5, edge: 0.25, k: keys('0 0 0.25;-6 -15 1;-13 -27 0', '0;0.55;1') }
|
|
let puffShapes: { edge: Float32Array; fill: Float32Array } | null = null
|
|
|
|
export function smoke(o: { at: [number, number]; index: number; fill: string; edge: string }): Actor {
|
|
const fill = rgbOf(o.fill)
|
|
const edge = rgbOf(o.edge)
|
|
return {
|
|
corners: 3 * 2 * 14 * 3,
|
|
moving: true,
|
|
draw(b, t, motion, z) {
|
|
if (!motion) return
|
|
puffShapes ??= { edge: circle(0, 0, PUFF.r + PUFF.edge, 14), fill: circle(0, 0, PUFF.r - PUFF.edge, 14) }
|
|
for (let n = 1; n <= 3; n++) {
|
|
const [dx, dy, s] = linear(PUFF.k, phase(t, -(n * 1.3 + o.index * 0.9), PUFF.dur))
|
|
if (!s) continue
|
|
const m = mul(move(o.at[0] + dx!, o.at[1] + dy!), stretch(s!))
|
|
b.put(puffShapes.edge, edge, m, z)
|
|
b.put(puffShapes.fill, fill, m, z)
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
// The ground ring under a picked building: solid when clicked, dashed under keyboard focus.
|
|
|
|
const RING = rgbOf('#7b1e16')
|
|
export function ring(b: Batch, e: { cx: number; cy: number; rx: number; ry: number }, dashed: boolean, z: number) {
|
|
const n = 96
|
|
const at = (i: number, grow: number): [number, number] => {
|
|
const a = (i / n) * Math.PI * 2
|
|
return [e.cx + (e.rx + grow) * Math.cos(a), e.cy + (e.ry + grow) * Math.sin(a)]
|
|
}
|
|
let walked = 0
|
|
for (let i = 0; i < n; i++) {
|
|
const [ax, ay] = at(i, 0)
|
|
const [bx, by] = at(i + 1, 0)
|
|
const piece = Math.hypot(bx - ax, by - ay)
|
|
// Dashes of 4 with gaps of 3, like the SVG's stroke-dasharray.
|
|
const show = !dashed || (walked + piece / 2) % 7 < 4
|
|
walked += piece
|
|
if (!show) continue
|
|
const [ix, iy] = at(i, -1)
|
|
const [ox, oy] = at(i, 1)
|
|
const [jx, jy] = at(i + 1, -1)
|
|
const [px, py] = at(i + 1, 1)
|
|
b.put(new Float32Array([ix, iy, ox, oy, px, py, ix, iy, px, py, jx, jy]), RING, IDENTITY, z)
|
|
}
|
|
}
|
|
export const RING_CORNERS = 96 * 6
|