pixeltation/src/meditations/morning-flight/clouds.ts

141 lines
5.0 KiB
TypeScript

import { ditherStep, hex, mix, type RGB } from '../../engine/color';
import { progress } from '../../engine/Timeline';
import { noise2 } from '../../engine/rng';
import type { Surface } from '../../engine/Surface';
import { CLOUD_EDGE, CLOUD_LIT, CLOUD_SHADE, frameAt, type Frame } from './timeline';
import { clarity, CX, F, HORIZON, smoothstep, urbanAt, W } from './world';
/**
* Two broken cloud decks overhead, seen from below: a high, thin one that catches the dawn colors
* first, and a patchier low one that follows. Each is a sheet of noise in world coordinates, so it streams
* overhead from the horizon as the plane flies on, and it is filtered by how much ground each pixel
* spans so the far rows don't shimmer.
*/
interface Deck {
alt: number;
/** Size of the big cloud masses, m. */
size: number;
/** Density above which a pixel is cloud. */
th: number;
/** Wind drift across the path, m/s. */
wind: number;
seed: number;
/** Seconds ahead of the low deck in the dawn's color changes: higher clouds see the sun sooner. */
lead: number;
}
const HIGH: Deck = {
alt: 2600,
size: 1100,
th: 0.55,
wind: 5,
seed: 80,
lead: 18,
};
const LOW: Deck = {
alt: 1100,
size: 620,
th: 0.6,
wind: 3,
seed: 90,
lead: 0,
};
const CITY_UNDERLIGHT = hex('#7a4a5a');
function density(d: Deck, X: number, Z: number, fp: number, th: number): number {
const s = d.size;
// Detail that would be smaller than a pixel is faded to its average.
const k2 = smoothstep(s * 0.5, s * 0.15, fp);
const k3 = smoothstep(s * 0.16, s * 0.05, fp);
const base = 0.5 + 0.62 * (noise2(X / (s * 1.7), Z / s, d.seed) - 0.5);
// Clear sky that the finer octaves can't push over the threshold needs nothing more.
if (base + 0.15 * k2 + 0.07 * k3 < th - 0.02) return 0;
let v = base;
if (k2 > 0) v += 0.3 * k2 * (noise2(X / (s * 0.6) + 11, Z / (s * 0.38), d.seed + 1) - 0.5);
if (k3 > 0) v += 0.14 * k3 * (noise2(X / (s * 0.17), Z / (s * 0.12), d.seed + 2) - 0.5);
return v;
}
/** The cloud layer over the sky: color and coverage per pixel, both decks combined. */
const LAYER_C = new Float32Array(W * HORIZON * 3);
const LAYER_A = new Float32Array(W * HORIZON);
function computeDeck(f: Frame, d: Deck): void {
const cam = f.cam;
const h = d.alt - cam.y;
const shade = CLOUD_SHADE(f.t + d.lead);
const litC = CLOUD_LIT(f.t + d.lead);
const edgeC = CLOUD_EDGE(f.t + d.lead);
const core: RGB = [shade[0] * 0.82, shade[1] * 0.82, shade[2] * 0.85];
// Town light on the low deck at night, gone once the dawn colors take over.
const city = d === LOW ? f.lights * (1 - progress(f.t, 50, 110)) : 0;
const drift = f.t * d.wind;
for (let y = 0; y < HORIZON; y++) {
const r = HORIZON - (y + 0.5);
const dist = (h * F) / r;
const far = smoothstep(30000, 12000, dist);
if (far <= 0) continue;
const fp = Math.max(dist / F, dist / r);
const Z = cam.z + dist;
const haze = 1 - clarity(dist * 0.6);
const low = y / HORIZON;
for (let x = 0; x < W; x++) {
const X = cam.x + ((x + 0.5 - CX) * dist) / F + drift;
const e = density(d, X, Z, fp, d.th) - d.th;
if (e <= -0.02) continue;
const a = ditherStep(Math.min(1, (e + 0.02) / 0.07) * far, x, y, 5);
if (a <= 0) continue;
// Light from the east reaches under the clouds, strongest near the sun and the horizon.
const sunward = Math.exp(-(((x - f.sunX) / 130) ** 2)) * (0.4 + 0.6 * low);
const g = 0.25 + 0.75 * sunward;
let c = mix(shade, litC, g);
c = mix(c, edgeC, (1 - Math.min(1, Math.max(0, e) / 0.08)) * 0.6);
const thick = Math.min(1, Math.max(0, e) / 0.24);
c = mix(c, core, thick * 0.4 * (1 - g * 0.5));
if (city > 0) {
const u = urbanAt(X - drift, Z);
if (u > 0) c = mix(c, CITY_UNDERLIGHT, u * city * 0.35);
}
c = mix(c, f.horizonColor, haze * 0.55);
// Lay this deck over what the decks above have left in the layer.
const j = y * W + x;
const below = LAYER_A[j];
const out = a + below * (1 - a);
const kb = (below * (1 - a)) / out;
LAYER_C[j * 3] = c[0] * (1 - kb) + LAYER_C[j * 3] * kb;
LAYER_C[j * 3 + 1] = c[1] * (1 - kb) + LAYER_C[j * 3 + 1] * kb;
LAYER_C[j * 3 + 2] = c[2] * (1 - kb) + LAYER_C[j * 3 + 2] * kb;
LAYER_A[j] = out;
}
}
}
/**
* The decks move at most a couple of pixels a second, so the layer is recomputed only every 1/12 s
* of scene time. Keying the cache on quantized time keeps the scene a pure function of t.
*/
const STEP = 1 / 12;
let cachedKey = NaN;
export function drawClouds(s: Surface, f: Frame): void {
const key = Math.round(f.t / STEP);
if (key !== cachedKey) {
cachedKey = key;
LAYER_A.fill(0);
const ft = key * STEP === f.t ? f : frameAt(key * STEP);
computeDeck(ft, HIGH);
computeDeck(ft, LOW);
}
const col: [number, number, number] = [0, 0, 0];
for (let j = 0; j < W * HORIZON; j++) {
const a = LAYER_A[j];
if (a <= 0) continue;
col[0] = LAYER_C[j * 3];
col[1] = LAYER_C[j * 3 + 1];
col[2] = LAYER_C[j * 3 + 2];
s.blend(j % W, (j / W) | 0, col, a);
}
}