pixeltation/src/meditations/cyberpunk-city/buildings.ts

534 lines
21 KiB
TypeScript

import { hex, mix, scale, type RGB } from '../../engine/color';
import { hash, noise1, Rng } from '../../engine/rng';
import type { Surface } from '../../engine/Surface';
import { AMBER, CYAN, moonSea, NEONS, PINK, RED, shopAt, SHOP_W, signColor, VIOLET, type Sign } from './neon';
import { FOG, haze, type Frame } from './timeline';
import { bindScreen, BOTTOM, FRONT, LEFT, RIGHT, SCREEN, TOP, traceBox, type Box3, type Raster, type Shader } from './trace';
import {
BACK_STREET,
BLOCK,
clarity,
CROSS_HALF,
DEPTH,
F,
FIRST_CROSS,
glowClarity,
gridDist,
H,
HORIZON,
LAST_CROSS,
LINE,
MAT,
M_ROAD,
M_WALK,
PLAZA,
project,
TOWER,
W,
WET,
type Cam,
} from './world';
export interface Building extends Box3 {
wall: RGB;
/** Neon light from the street spilling up the walls. */
spill: RGB;
win?: { lit: number; seed: number };
/** Faces fronting a street, which have shops at street level. */
shops: number[];
signs: Sign[];
/** A glass curtain wall with vertical light strips instead of windows. */
glass?: RGB;
beacon?: boolean;
/** Which side of the avenue: shops on it share lights with the pavement below. */
side: number;
/** Colors worked out once: the wall by height (neon spill fades going up), dark glass, a floor ledge, the far-off average. */
pal?: { wallY: RGB[]; glass: RGB; ledge: RGB; avg: RGB; roof: RGB; under: RGB };
/** Each face's windows, worked out once: a color index per window (0 for dark glass), and which ones switch on and off. */
windows?: { bays: number; floors: number; u0: number; col: Uint8Array; flip: Float32Array }[];
/** Signs sorted by face, and the lowest sign on each face, for a quick miss. */
byFace?: Sign[][];
lowest?: number[];
shopMask?: number;
}
/** Blade signs jutting out over the sidewalk, lettered on the face turned down the avenue. */
export interface Blade extends Box3 {
color: RGB;
seed: number;
}
/** A low roof with a holo-billboard projected above it. */
export interface Roof {
x: number;
z: number;
y: number;
seed: number;
}
const WALLS: readonly RGB[] = ['#1a1a28', '#211c2c', '#1a2230', '#24202a', '#1c1e26', '#2a2232', '#162028'].map(hex);
const SPILLS: readonly RGB[] = [PINK, CYAN, VIOLET, AMBER, PINK, CYAN];
const SHOP_H = 4.4;
export const BUILDINGS: Building[] = [];
export const BLADES: Blade[] = [];
export const ROOFS: Roof[] = [];
/** The glass tower a holographic koi circles, partway up the avenue on the right. */
export const KOI_TOWER = { x0: LINE, x1: LINE + 34, z0: 2 * BLOCK + CROSS_HALF + 12, z1: 2 * BLOCK + CROSS_HALF + 48, h: 150 };
const r = new Rng(2077);
const pick = <T>(a: readonly T[]): T => a[r.int(0, a.length - 1)];
/** Scatter neon signs over one face, between u = ua and ub, from vmin up to the top. */
function addSigns(b: Building, face: number, ua: number, ub: number, vmin: number, top: number, busy = 1): void {
const len = ub - ua;
const out = b.signs;
const add = (sg: Sign): void => {
if (sg.u0 < ua + 0.4 || sg.u1 > ub - 0.4 || sg.v1 > top - 0.8 || sg.v0 < vmin) return;
if (out.some((o) => o.face === face && sg.u0 < o.u1 + 0.8 && sg.u1 > o.u0 - 0.8 && sg.v0 < o.v1 + 0.8 && sg.v1 > o.v0 - 0.8)) return;
out.push(sg);
};
const seed = () => r.int(0, 99999);
if (top - vmin > 30 && len > 16 && r.next() < 0.4 * busy) {
const w = r.range(10, Math.min(len - 3, 22));
const h = w * r.range(0.55, 0.8);
const u0 = ua + r.range(1.5, len - w - 1.5);
const v0 = r.range(vmin + 4, Math.max(vmin + 4, Math.min(top - h - 3, vmin + 40)));
add({ face, u0, u1: u0 + w, v0, v1: v0 + h, kind: 'screen', color: pick(NEONS), seed: seed() });
}
if (top - vmin > 14 && r.next() < 0.3 * busy) {
const v0 = r.range(vmin + 1, Math.min(top - 3, vmin + 26));
add({ face, u0: ua + 0.5, u1: ub - 0.5, v0, v1: v0 + 1.3, kind: 'ticker', color: r.next() < 0.6 ? AMBER : RED, seed: seed() });
}
for (let n = r.int(1, 3) + (busy > 1 ? 2 : 0); n > 0; n--) {
const w = r.range(2.2, 3.4);
const h = r.range(8, Math.min(26, top - vmin - 2));
if (h < 6) break;
const u0 = ua + r.range(0.5, len - w - 0.5);
const v0 = r.range(vmin, Math.max(vmin, Math.min(top - h - 2, vmin + 34)));
add({ face, u0, u1: u0 + w, v0, v1: v0 + h, kind: 'glyph', color: pick(NEONS), seed: seed() });
}
if (r.next() < 0.6 * busy && len > 8) {
const w = r.range(6, Math.min(16, len - 2));
const h = r.range(1.8, 3);
const u0 = ua + r.range(1, len - w - 1);
const v0 = r.range(vmin, Math.max(vmin, Math.min(top - h - 1, vmin + 10)));
add({ face, u0, u1: u0 + w, v0, v1: v0 + h, kind: 'band', color: pick(NEONS), seed: seed() });
}
}
function addBlades(b: Building, side: number, top: number): void {
for (let n = r.int(0, 2); n > 0; n--) {
const z0 = r.range(b.z0 + 1, b.z1 - 2);
const y0 = r.range(5, 8);
const y1 = Math.min(top - 1, y0 + r.range(7, 15));
if (y1 - y0 < 5) continue;
const out = 2.6;
BLADES.push({ x0: side > 0 ? LINE - out : -LINE, x1: side > 0 ? LINE : -LINE + out, y0, y1, z0, z1: z0 + 0.45, color: pick(NEONS), seed: r.int(0, 99999) });
}
}
function building(x0: number, x1: number, z0: number, z1: number, h: number, side: number, shops: number[]): Building {
const b: Building = {
x0,
x1,
y0: 0,
y1: h,
z0,
z1,
wall: pick(WALLS),
spill: pick(SPILLS),
win: { lit: r.range(0.08, 0.28), seed: r.int(0, 9999) },
shops,
signs: [],
side,
beacon: h > 110,
};
BUILDINGS.push(b);
return b;
}
const frontHeight = (k: number): number => {
if (k === LAST_CROSS) return r.range(12, 22);
const p = r.next();
return p < 0.3 ? r.range(14, 32) : p < 0.82 ? r.range(40, 90) : r.range(100, 165);
};
// The avenue: a row of buildings along each sidewalk, block by block, and taller towers behind.
for (const side of [1, -1]) {
const street = side > 0 ? LEFT : RIGHT;
for (let k = FIRST_CROSS; k <= LAST_CROSS; k++) {
const zs = k * BLOCK + CROSS_HALF;
const ze = (k + 1) * BLOCK - CROSS_HALF;
const koi = side > 0 && k === 2;
const plan: [number, number][] = koi
? [
[KOI_TOWER.z0 - zs, 22],
[KOI_TOWER.z1 - KOI_TOWER.z0, KOI_TOWER.h],
[ze - KOI_TOWER.z1, 27],
]
: [];
if (!koi) {
let z = zs;
while (z < ze - 1) {
let len = r.range(15, 30);
if (ze - z - len < 12) len = ze - z;
plan.push([len, frontHeight(k)]);
z += len;
}
}
let z = zs;
for (const [len, h] of plan) {
const depth = koi && h === KOI_TOWER.h ? KOI_TOWER.x1 - KOI_TOWER.x0 : r.range(24, 44);
const x0 = side > 0 ? LINE : -LINE - depth;
const b = building(x0, x0 + depth, z, z + len, h, side, z === zs ? [street, FRONT] : [street]);
if (koi && h === KOI_TOWER.h) {
b.glass = CYAN;
b.win = undefined;
addSigns(b, street, z, z + len, SHOP_H + 0.6, 30);
} else {
addSigns(b, street, z, z + len, SHOP_H + 0.6, h);
if (z === zs) addSigns(b, FRONT, x0, x0 + depth, SHOP_H + 0.6, h, 0.7);
addBlades(b, side, h);
}
if (h < 36 && r.next() < (k === LAST_CROSS ? 0.8 : 0.55)) ROOFS.push({ x: x0 + depth / 2, z: z + len / 2, y: h, seed: r.int(0, 9999) });
z += len;
}
// Towers set back behind the front row, rising over it.
for (let n = r.int(1, 2); n > 0; n--) {
const w = r.range(24, 42);
const d = r.range(22, 50);
const off = LINE + 48 + r.range(0, 30);
const x0 = side > 0 ? off : -off - w;
const z0 = zs + r.range(0, Math.max(0, ze - zs - d));
const h = r.range(95, 250);
const b = building(x0, x0 + w, z0, z0 + d, h, side, []);
addSigns(b, street, z0, z0 + d, r.range(45, 90), h);
addSigns(b, FRONT, x0, x0 + w, r.range(45, 90), h, 0.6);
}
}
}
// The plaza: walls of screens on either side, like a crossroads of giant billboards.
for (const side of [1, -1]) {
const inner = side > 0 ? LEFT : RIGHT;
const segs: [number, number, number][] = [
[PLAZA.z0, 498, 86],
[498, 556, 118],
[556, BACK_STREET.z0, 96],
];
for (const [z0, z1, h] of segs) {
const x0 = side > 0 ? PLAZA.half : -PLAZA.half - 42;
const b = building(x0, x0 + 42, z0, z1, h, side * 2, z0 === PLAZA.z0 ? [inner, FRONT] : [inner]);
const v0 = 15;
const v1 = Math.min(h - 12, v0 + (z1 - z0 - 6) * 0.62);
b.signs.push({ face: inner, u0: z0 + 3, u1: z1 - 3, v0, v1, kind: 'screen', color: pick(NEONS), seed: r.int(0, 99999) });
b.signs.push({ face: inner, u0: z0 + 0.6, u1: z1 - 0.6, v0: 10.5, v1: 12, kind: 'ticker', color: AMBER, seed: r.int(0, 99999) });
addSigns(b, inner, z0, z1, v1 + 2, h, 1.5);
addSigns(b, inner, z0, z1, SHOP_H + 0.8, 10, 1.5);
if (z0 === PLAZA.z0) addSigns(b, FRONT, x0, x0 + 42, SHOP_H + 0.6, h, 2);
}
}
// The screen tower at the head of the plaza, the glow at the end of the avenue the whole way up.
export const MAIN_SCREEN: Sign = { face: FRONT, u0: TOWER.x0 + 4, u1: TOWER.x1 - 4, v0: 34, v1: 96, kind: 'main', color: CYAN, seed: 4242 };
{
const b = building(TOWER.x0, TOWER.x1, TOWER.z0, TOWER.z1, TOWER.h, 0, [FRONT]);
b.win = undefined;
b.signs.push(
MAIN_SCREEN,
{ face: FRONT, u0: TOWER.x0 + 0.5, u1: TOWER.x1 - 0.5, v0: 27, v1: 29.5, kind: 'ticker', color: AMBER, seed: 77 },
{ face: FRONT, u0: TOWER.x0 + 0.5, u1: TOWER.x1 - 0.5, v0: 101, v1: 103.5, kind: 'ticker', color: CYAN, seed: 78 },
{ face: FRONT, u0: TOWER.x0 + 0.6, u1: TOWER.x0 + 3.2, v0: 8, v1: 30, kind: 'glyph', color: PINK, seed: 79 },
{ face: FRONT, u0: TOWER.x1 - 3.2, u1: TOWER.x1 - 0.6, v0: 8, v1: 30, kind: 'glyph', color: VIOLET, seed: 80 },
{ face: FRONT, u0: TOWER.x0 + 6, u1: TOWER.x1 - 6, v0: 108, v1: 118, kind: 'screen', color: PINK, seed: 81 },
{ face: FRONT, u0: TOWER.x0 + 5, u1: TOWER.x0 + 21, v0: 10, v1: 24, kind: 'screen', color: AMBER, seed: 82 },
{ face: FRONT, u0: TOWER.x1 - 21, u1: TOWER.x1 - 5, v0: 10, v1: 24, kind: 'screen', color: CYAN, seed: 83 },
);
}
// A row of buildings closing off the view behind the tower.
for (let x = -230; x < 230; ) {
const w = r.range(40, 70);
const h = r.range(40, 95);
const b = building(x, x + w, BACK_STREET.z1 + 2, BACK_STREET.z1 + 50, h, 3, [FRONT]);
addSigns(b, FRONT, x, x + w, SHOP_H + 0.6, h, 1.5);
x += w;
}
// Megatowers far off in the haze, their signs and lights streaking up into the clouds.
for (let n = 0; n < 12; n++) {
const w = r.range(60, 140);
const z0 = r.range(850, 1900);
const off = r.range(-700, 700);
const h = r.range(220, 480);
const b = building(off - w / 2, off + w / 2, z0, z0 + w, h, 4, []);
b.win = { lit: r.range(0.08, 0.2), seed: r.int(0, 9999) };
for (let i = r.int(1, 3); i > 0; i--) {
const u0 = off - w / 2 + r.range(4, w - 14);
const v0 = r.range(40, h - 120);
b.signs.push({ face: FRONT, u0, u1: u0 + r.range(6, 10), v0, v1: v0 + r.range(50, 110), kind: 'glyph', color: pick(NEONS), seed: r.int(0, 99999) });
}
}
// ---------------------------------------------------------------------------
// Shading
// ---------------------------------------------------------------------------
const WIN_COLORS: readonly RGB[] = [hex('#8a6a48'), hex('#8a6a48'), hex('#56748e'), hex('#84567a'), hex('#5e8a7c')];
const SHUTTER = hex('#26242c');
const ROOF = hex('#18161f');
const ROOF_UNIT = hex('#262430');
const PARAPET = hex('#2e2a38');
const GLASS_DARK = hex('#101a24');
function roofColor(b: Building, X: number, Z: number, fp: number): RGB {
const edge = Math.min(X - b.x0, b.x1 - X, Z - b.z0, b.z1 - Z);
if (edge < 0.7) return PARAPET;
let c = b.pal!.roof;
if (fp < 1.2 && hash(Math.floor(X / 3.2), Math.floor(Z / 3.2), 91) < 0.2) c = ROOF_UNIT;
// Rain pooled on the flat roof, holding a little of the sky's glow.
if (hash(Math.floor(X / 5), Math.floor(Z / 5), 92) < 0.25) c = mix(c, FOG, 0.35);
return c;
}
function shopFront(b: Building, face: number, u: number, Y: number, fp: number): RGB {
const n = Math.floor(u / SHOP_W);
const key = face === FRONT ? b.side * 7 + 3 : b.side;
const col = shopAt(key, n);
if (Y < 0.3) return scale(b.wall, 0.6);
if (!col) return fp < 0.3 && Y % 0.5 < 0.08 ? scale(SHUTTER, 0.7) : SHUTTER;
if (Y > SHOP_H - 1) return fp < 0.4 && Math.floor((u - n * SHOP_W) / 0.6) % 2 === 0 ? col : scale(col, 0.8);
const lu = u - n * SHOP_W;
if (fp < 0.5 && (lu < 0.25 || lu % 1.25 < 0.12)) return scale(b.wall, 0.8);
return scale(col, Y < 1.1 ? 0.55 : 0.75);
}
const SCRATCH: [number, number, number] = [0, 0, 0];
const BAY = 2.2;
const FLOOR = 3.4;
/** Window table for one face: what each window shows, and when the ones that switch do so. */
function windowTable(b: Building, face: number): NonNullable<Building['windows']>[number] {
const w = b.win!;
const u0 = face === FRONT ? b.x0 : b.z0;
const len = face === FRONT ? b.x1 - b.x0 : b.z1 - b.z0;
const bays = Math.ceil(len / BAY) + 2;
const floors = Math.ceil(b.y1 / FLOOR) + 1;
const col = new Uint8Array(bays * floors);
const flip = new Float32Array(bays * floors).fill(Infinity);
for (let fl = 0; fl < floors; fl++) {
for (let wi = 0; wi < bays; wi++) {
const n = fl * bays + wi;
const h = hash(wi, fl, w.seed + face);
if (hash(wi + face * 977, fl, w.seed) < w.lit) col[n] = 1 + Math.floor(((h * 7919) % 1) * WIN_COLORS.length);
// One window in twelve switches on or off at some moment during the meditation.
if (h < 0.08) flip[n] = hash(wi, fl, w.seed + 31) * 300;
}
}
return { bays, floors, u0, col, flip };
}
function windowColor(b: Building, face: number, u: number, Y: number, t: number, wall: RGB, fp: number): RGB {
const pal = b.pal!;
if (fp > 2.1) return pal.avg;
const tab = b.windows![face];
const fu = (u - tab.u0) / BAY;
const fv = (Y - SHOP_H) / FLOOR;
const wi = Math.floor(fu);
const fl = Math.floor(fv);
let c = wall;
// A thin ledge at every floor slab.
if (fl >= 0 && fv - fl < 0.1 && fp < 0.5) return pal.ledge;
if (fl >= 0 && fl < tab.floors && wi >= 0 && wi < tab.bays && fu - wi > 0.25 && fu - wi < 0.75 && fv - fl > 0.35 && fv - fl < 0.8) {
const n = fl * tab.bays + wi;
let ci = tab.col[n];
if (t > tab.flip[n]) ci = ci ? 0 : 1 + (n % WIN_COLORS.length);
c = ci ? WIN_COLORS[ci - 1] : pal.glass;
}
// Far away, a wall averages out to a faint glow of its lit windows.
if (fp <= 0.6) return c;
const k = Math.min(1, (fp - 0.6) / 1.5) * 0.8;
const a = pal.avg;
SCRATCH[0] = c[0] + (a[0] - c[0]) * k;
SCRATCH[1] = c[1] + (a[1] - c[1]) * k;
SCRATCH[2] = c[2] + (a[2] - c[2]) * k;
return SCRATCH;
}
function palette(b: Building): NonNullable<Building['pal']> {
const glass = mix(scale(b.wall, 0.55), FOG, 0.22);
return {
wallY: Array.from({ length: 48 }, (_, y) => mix(b.wall, b.spill, 0.05 + 0.22 * Math.exp(-y / 16))),
glass,
ledge: scale(b.wall, 1.25),
avg: mix(mix(b.wall, glass, 0.3), WIN_COLORS[0], (b.win?.lit ?? 0) * 0.35),
roof: mix(ROOF, b.spill, 0.05),
under: scale(b.wall, 0.6),
};
}
for (const b of BUILDINGS) {
b.pal = palette(b);
if (b.win) b.windows = [0, 1, 2].map((face) => windowTable(b, face));
b.byFace = [0, 1, 2, 3, 4].map((face) => b.signs.filter((sg) => sg.face === face));
b.lowest = b.byFace.map((l) => Math.min(Infinity, ...l.map((sg) => sg.v0)));
b.shopMask = b.shops.reduce((m, face) => m | (1 << face), 0);
}
const NONE: Sign[] = [];
/** One frame's moment, for the shaders below. */
let frame: Frame;
function shade(b: Building, face: number, X: number, Y: number, Z: number, d: number): RGB {
const fp = d / F;
const fog = 1 - clarity(d);
if (face === TOP) return haze(roofColor(b, X, Z, fp), fog);
if (face === BOTTOM) return haze(b.pal!.under, fog);
const u = face === FRONT ? X : Z;
const signs = Y >= b.lowest![face] ? b.byFace![face] : NONE;
for (let n = 0; n < signs.length; n++) {
const sg = signs[n];
if (u < sg.u0 || u > sg.u1 || Y < sg.v0 || Y > sg.v1) continue;
let c: RGB | undefined;
if (sg.kind === 'main') {
const lu = u - sg.u0;
const lv = Y - sg.v0;
const w = sg.u1 - sg.u0;
const h = sg.v1 - sg.v0;
// The moon arrives LED by LED, scattered across the screen.
const moon = hash(Math.floor(lu / 0.6), Math.floor(lv / 0.6), 93) < frame.moon;
c = moon ? moonSea(lu / w, lv / h, frame.t, w / h) : signColor(sg, lu, lv, frame.t, fp);
} else c = signColor(sg, u - sg.u0, Y - sg.v0, frame.t, fp);
if (c) return haze(c, 1 - glowClarity(d));
}
if (Y < SHOP_H && (b.shopMask! >> face) & 1) return haze(shopFront(b, face, u, Y, fp), 1 - glowClarity(d));
// Neon from the street below washes up the lower walls.
const wy = b.pal!.wallY;
let c = wy[Math.min(wy.length - 1, Y | 0)];
if (b.glass) {
const strip = gridDist(u, 4.2) < 0.3;
c = strip ? mix(GLASS_DARK, b.glass, 0.35 + 0.15 * Math.sin(Y * 0.05 - frame.t * 0.4)) : mix(GLASS_DARK, FOG, 0.25 + 0.1 * Math.sin(Y * 0.3 + u));
} else if (b.win) c = windowColor(b, face, u, Y, frame.t, c, fp);
return haze(c, fog);
}
function shadeBlade(bl: Blade, face: number, X: number, Y: number, _Z: number, d: number): RGB {
const fp = d / F;
let c: RGB;
if (face === FRONT) {
const sg: Sign = { face, u0: bl.x0, u1: bl.x1, v0: bl.y0, v1: bl.y1, kind: 'glyph', color: bl.color, seed: bl.seed };
c = signColor(sg, X - bl.x0, Y - bl.y0, frame.t, fp) ?? scale(bl.color, 0.1);
} else if (face === TOP || face === BOTTOM) c = scale(bl.color, 0.2);
else c = scale(bl.color, 0.75);
return haze(c, 1 - glowClarity(d));
}
/** The box being traced, so one shader function serves them all (and the call stays cheap). */
let current: Building;
let currentBlade: Blade;
const shadeCurrent: Shader = (face, X, Y, Z, d) => shade(current, face, X, Y, Z, d);
const shadeCurrentBlade: Shader = (face, X, Y, Z, d) => shadeBlade(currentBlade, face, X, Y, Z, d);
function traceBuilding(r: Raster, cam: Cam, b: Building, mirror: boolean): void {
current = b;
traceBox(r, cam, b, shadeCurrent, mirror);
}
function traceBlade(r: Raster, cam: Cam, b: Blade, mirror: boolean): void {
currentBlade = b;
traceBox(r, cam, b, shadeCurrentBlade, mirror);
}
const AVIATION = hex('#ff3226');
const near = (b: Box3, cam: Cam): number => Math.max(0, b.z0 - cam.z) + 0.5 * Math.max(0, b.x0 - cam.x, cam.x - b.x1);
function byDistance(cam: Cam): Building[] {
return BUILDINGS.filter((b) => b.z1 > cam.z + 0.5).sort((a, b) => near(a, cam) - near(b, cam));
}
export function drawBuildings(s: Surface, f: Frame): void {
frame = f;
bindScreen(s);
const cam = f.cam;
// Nearest first, so the depth test turns away hidden pixels before they are shaded.
for (const b of BLADES) if (b.z1 > cam.z && b.z0 - cam.z < 700) traceBlade(SCREEN, cam, b, false);
for (const b of byDistance(cam)) traceBuilding(SCREEN, cam, b, false);
// Red aviation lights blinking slowly on the tallest roofs.
for (const b of BUILDINGS) {
if (!b.beacon || ((f.t * 0.5 + hash(b.x0 | 0, b.z0 | 0, 94)) % 1) > 0.45) continue;
const p = project(cam, (b.x0 + b.x1) / 2, b.y1 + 1.5, (b.z0 + b.z1) / 2);
if (!p || p.x < 0 || p.x >= W || p.y < 0 || p.y >= H) continue;
if (DEPTH[(p.y | 0) * W + (p.x | 0)] < p.d - 30) continue;
const k = Math.sqrt(glowClarity(p.d));
s.light(p.x, p.y, AVIATION, k);
s.light(p.x - 1, p.y, AVIATION, k * 0.35);
s.light(p.x + 1, p.y, AVIATION, k * 0.35);
}
}
// ---------------------------------------------------------------------------
// Reflections in the wet street
// ---------------------------------------------------------------------------
const RW = W / 2;
const RH = Math.ceil((H - HORIZON) / 2);
const REF = new Float32Array(RW * RH * 3);
const REF_DEPTH = new Float32Array(RW * RH);
/** Half-size buffer the mirrored city is traced into, from the horizon down. */
export const MIRROR: Raster = {
w: RW,
h: RH,
scale: 2,
row0: HORIZON,
depth: REF_DEPTH,
put(i, j, c) {
const k = (j * RW + i) * 3;
REF[k] = c[0];
REF[k + 1] = c[1];
REF[k + 2] = c[2];
},
};
/** Trace the city upside down beneath the street. Other layers may add to MIRROR before compositing. */
export function traceReflections(f: Frame): void {
frame = f;
const sky = scale(FOG, 0.8);
for (let k = 0; k < RW * RH; k++) {
REF[k * 3] = sky[0];
REF[k * 3 + 1] = sky[1];
REF[k * 3 + 2] = sky[2];
}
REF_DEPTH.fill(Infinity);
const cam = f.cam;
for (const b of BLADES) if (b.z1 > cam.z && b.z0 - cam.z < 300) traceBlade(MIRROR, cam, b, true);
for (const b of byDistance(cam)) if (b.z0 - cam.z < 650) traceBuilding(MIRROR, cam, b, true);
}
/** Lay the mirrored city over the wet street, smeared toward the viewer and rippled by the rain. */
export function compositeReflections(s: Surface, f: Frame): void {
const d = s.data;
for (let y = HORIZON; y < H; y++) {
const j = (y - HORIZON) >> 1;
const wob = Math.round((noise1(y * 0.85 + f.t * 1.2, 7) - 0.5) * 3.2 + (noise1(y * 0.23 - f.t * 0.4, 8) - 0.5) * 2);
for (let x = 0; x < W; x++) {
const k = y * W + x;
const m = MAT[k];
if (m !== M_ROAD && m !== M_WALK) continue;
const wet = WET[k];
const i = Math.max(0, Math.min(RW - 1, (x + wob) >> 1));
const r0 = (j * RW + i) * 3;
const r1 = (Math.max(0, j - 1) * RW + i) * 3;
const r2 = (Math.max(0, j - 2) * RW + i) * 3;
const p = k * 4;
for (let c = 0; c < 3; c++) {
const refl = REF[r0 + c] * 0.5 + REF[r1 + c] * 0.3 + REF[r2 + c] * 0.2;
d[p + c] = d[p + c] * (1 - wet * 0.4) + refl * wet;
}
}
}
}