103 lines
3.8 KiB
TypeScript
103 lines
3.8 KiB
TypeScript
|
|
// A village scene in buffers for WebGL. The SVG drew everything back to front; here all
|
||
|
|
// that stands still goes into one mesh in that same order, and every place where something
|
||
|
|
// moves (people, animals, smoke, crows) or a ground ring may show gets a depth step of its
|
||
|
|
// own. The figures are drawn afterwards at their step, so the depth test puts them behind
|
||
|
|
// what was drawn after them and in front of what came before: the painter's order, kept.
|
||
|
|
|
||
|
|
import earcut from 'earcut'
|
||
|
|
import type { Poly } from './iso'
|
||
|
|
import { type Actor, animal, crow, rgbOf, smoke, standing, worker } from './figures'
|
||
|
|
import { SHIRTS, SMOKE, UNIFORM } from './models'
|
||
|
|
import type { Scene } from './scene'
|
||
|
|
|
||
|
|
export interface GlScene {
|
||
|
|
/** Corners (x, -y, z) and colours (0..255) of everything that stands still, with triangle indices. */
|
||
|
|
positions: Float32Array
|
||
|
|
colors: Uint8Array
|
||
|
|
indices: Uint32Array
|
||
|
|
/** What moves, each with its depth step, back to front. */
|
||
|
|
actors: { actor: Actor; z: number }[]
|
||
|
|
/** Where each building's ground ring goes, and its depth step: just under the building. */
|
||
|
|
rings: Map<number, { z: number; ring: { cx: number; cy: number; rx: number; ry: number } }>
|
||
|
|
/** The nearest depth step in use. */
|
||
|
|
top: number
|
||
|
|
}
|
||
|
|
|
||
|
|
export function buildGl(scene: Scene): GlScene {
|
||
|
|
const positions: number[] = []
|
||
|
|
const colors: number[] = []
|
||
|
|
const indices: number[] = []
|
||
|
|
const flat: number[] = []
|
||
|
|
|
||
|
|
function add(polys: Poly[], z: number) {
|
||
|
|
for (const p of polys) {
|
||
|
|
flat.length = 0
|
||
|
|
for (const pair of p.points.split(' ')) {
|
||
|
|
const comma = pair.indexOf(',')
|
||
|
|
flat.push(Number(pair.slice(0, comma)), Number(pair.slice(comma + 1)))
|
||
|
|
}
|
||
|
|
const tris = earcut(flat)
|
||
|
|
if (!tris.length) continue
|
||
|
|
const first = positions.length / 3
|
||
|
|
const [r, g, b] = rgbOf(p.fill)
|
||
|
|
for (let i = 0; i < flat.length; i += 2) {
|
||
|
|
positions.push(flat[i]!, -flat[i + 1]!, z)
|
||
|
|
colors.push(r * 255, g * 255, b * 255)
|
||
|
|
}
|
||
|
|
for (const i of tris) indices.push(first + i)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const actors: GlScene['actors'] = []
|
||
|
|
const rings: GlScene['rings'] = new Map()
|
||
|
|
let z = 0
|
||
|
|
add(scene.terrain, z)
|
||
|
|
add(scene.decals, z)
|
||
|
|
|
||
|
|
// Villagers on their way to work and animals walk under every building.
|
||
|
|
z++
|
||
|
|
scene.walks.forEach((w, i) => {
|
||
|
|
for (let n = 1; n <= w.count; n++) {
|
||
|
|
const shirt = SHIRTS[(i * 3 + n) % SHIRTS.length]!
|
||
|
|
const begin = -((n - 1) * (w.walk * 2 + w.work)) / w.count - i * 3
|
||
|
|
actors.push({ z, actor: worker({ d: w.d, walk: w.walk, work: w.work, tool: w.tool, carry: w.carry, shirt, size: 1.6, begin }) })
|
||
|
|
}
|
||
|
|
})
|
||
|
|
for (const a of scene.herds) actors.push({ z, actor: animal({ ...a, size: a.kind === 'sheep' ? 1.7 : 1.4 }) })
|
||
|
|
z++
|
||
|
|
|
||
|
|
for (const o of scene.objects) {
|
||
|
|
if (o.building) {
|
||
|
|
if (o.building.ring.rx) rings.set(o.building.id, { z: ++z, ring: o.building.ring })
|
||
|
|
add(o.polys, ++z)
|
||
|
|
} else if (o.life) {
|
||
|
|
const l = o.life
|
||
|
|
const shirt = l.guard ? UNIFORM : SHIRTS[l.shirt % SHIRTS.length]!
|
||
|
|
actors.push({ z: ++z, actor: standing({ at: l.at, facing: l.facing, tool: l.tool, shirt, drill: l.drill, guard: l.guard, bob: 2.2 + (l.shirt % 3) * 0.4 }) })
|
||
|
|
z++
|
||
|
|
} else if (o.walker) {
|
||
|
|
const w = o.walker
|
||
|
|
const shirt = w.soldier ? UNIFORM : SHIRTS[w.shirt % SHIRTS.length]!
|
||
|
|
actors.push({ z: ++z, actor: worker({ ...w, shirt, size: 1.6 }) })
|
||
|
|
z++
|
||
|
|
} else {
|
||
|
|
add(o.polys, z)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Smoke over the roofs, crows over everything.
|
||
|
|
z++
|
||
|
|
scene.smoke.forEach((at, index) => actors.push({ z, actor: smoke({ at, index, fill: SMOKE.fill, edge: SMOKE.edge }) }))
|
||
|
|
z++
|
||
|
|
for (const b of scene.birds) actors.push({ z, actor: crow({ ...b, size: 1.6 }) })
|
||
|
|
|
||
|
|
return {
|
||
|
|
positions: new Float32Array(positions),
|
||
|
|
colors: new Uint8Array(colors),
|
||
|
|
indices: new Uint32Array(indices),
|
||
|
|
actors,
|
||
|
|
rings,
|
||
|
|
top: z,
|
||
|
|
}
|
||
|
|
}
|