pixeltation/src/meditations/tibetan-monastery/timeline.ts

243 lines
8.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { add, gradient, hex, mul, scale, type RGB } from '../../engine/color';
import { colorTrack, ease, progress, track } from '../../engine/Timeline';
import { noise1 } from '../../engine/rng';
import { computeClouds, computeRayTable, openness, shadeClouds, type CloudField } from './clouds';
import { WINDOWS } from './world';
/** All timings for the Monastery Above the Clouds meditation, in seconds. */
export const DURATION = 300;
export const TIMES = {
fadeIn: [0, 6],
fadeOut: [291, 300],
vulture: [8, 82],
/** Monks start their circuit; see monks.ts for the laps. */
kora: 12,
leopard: [90, 140],
/** The lamp-lighter reaches the first window, then one window every `lampStep` seconds. */
lamp0: 188,
lampStep: 5.6,
} as const;
/** Light phases: clear late afternoon → clouding over → grey mist → blue-grey stormy dusk. */
const P = [0, 105, 175, 250] as const;
const phase = (a: string, b: string, c: string, d: string) =>
colorTrack([
[P[0], hex(a)],
[P[1], hex(b)],
[P[2], hex(c)],
[P[3], hex(d)],
]);
/** Sky gradient stops from zenith to horizon. */
const SKY = [
phase('#2856a6', '#46679a', '#59637a', '#1d2537'),
phase('#3c74bc', '#6283ae', '#6a7388', '#263045'),
phase('#6496cc', '#88a0bc', '#7c8597', '#2f3a51'),
phase('#9ebedc', '#adbccc', '#8c94a3', '#39445e'),
phase('#e2e0d4', '#d6d2cc', '#9aa1ab', '#434e69'),
];
/** Light falling on everything in the scene, as a per-channel multiplier. */
const AMBIENT = phase('#fff2e0', '#e6dedc', '#9298ae', '#4a5472');
export const CLOUD_LIT = phase('#ffffff', '#f2ecea', '#a6aab8', '#505a76');
export const CLOUD_MID = phase('#d6e0ee', '#c8c8d2', '#8a909f', '#404962');
export const CLOUD_SHADE = phase('#98a8c2', '#8e92a8', '#6c7284', '#323a52');
/** Cloud sea and summit mist. */
const FOG = phase('#eef0f6', '#dcdae0', '#a0a6b6', '#4e5874');
const sunX = track([[0, 286], [180, 274]], ease.linear);
const sunY = track([[0, 24], [180, 80]], ease.linear);
const sunColor = colorTrack([
[0, hex('#fff6dc')],
[100, hex('#ffe4ae')],
[145, hex('#ffb676')],
[180, hex('#ff946c')],
]);
/** Strength of direct sun before clouds, fading as it drops toward the haze. */
const sunPower = track([[0, 1], [110, 0.9], [160, 0.75], [185, 0]]);
const cover = track([
[0, 0.1],
[40, 0.18],
[80, 0.45],
[115, 0.8],
[165, 0.95],
[195, 1.1],
]);
/** The last sun break: a hole tears open around the sun, then closes for good. */
const gap = track([[120, 0], [134, 1], [150, 1], [164, 0]]);
/** Top row of the cloud sea in the valleys. */
const sea = track([
[0, 172],
[50, 165],
[80, 146],
[105, 127],
[130, 119],
[180, 111],
[300, 104],
]);
/** How far each range (far → near) has disappeared into the cloud. */
const VEIL = [track([[125, 0], [178, 1]]), track([[100, 0], [140, 1]]), track([[78, 0], [108, 1]])];
const mist = track([[148, 0], [185, 0.35], [235, 0.5], [300, 0.62]]);
const windBase = track([[0, 0.22], [100, 0.32], [160, 0.55], [220, 0.85], [300, 1]]);
const snow = track([[166, 0], [200, 0.35], [240, 0.7], [291, 1]]);
const snowCover = track([[172, 0], [290, 1]]);
/** Wind with slow gusts layered on the rising base, 0–~1.3. */
export function windAt(t: number): number {
const g = 0.7 * noise1(t * 0.3, 3) + 0.3 * noise1(t * 1.1, 4);
return windBase(t) * (0.72 + 0.56 * g);
}
/**
* How far the air has moved since t = 0, in pixels at the snow's depth. Integrated from a table so
* that particles riding the wind never jump when it changes, while staying a pure function of t.
*/
const DRIFT_DT = 0.05;
const DRIFT_SPEED = 24;
const DRIFT: Float64Array = (() => {
const n = Math.ceil(DURATION / DRIFT_DT) + 2;
const out = new Float64Array(n);
for (let i = 1; i < n; i++) out[i] = out[i - 1] + windAt((i - 0.5) * DRIFT_DT) * DRIFT_SPEED * DRIFT_DT;
return out;
})();
export function driftAt(t: number): number {
const u = Math.max(0, t) / DRIFT_DT;
const i = Math.min(DRIFT.length - 2, Math.floor(u));
return DRIFT[i] + (DRIFT[i + 1] - DRIFT[i]) * (u - i);
}
/** When the lamp-lighter reaches a window. */
export function lampTime(order: number): number {
return TIMES.lamp0 + order * TIMES.lampStep;
}
export const SUN_R = 6;
export const SKY_BANDS = 22;
export interface Frame {
t: number;
/** Ambient light multiplier, 0–1 per channel. */
amb: RGB;
skyBands: RGB[];
horizonColor: RGB;
sunX: number;
sunY: number;
sunColor: RGB;
/** How much of the sun is unobstructed by cloud, 0–1. */
sunOpen: number;
/** Strength of warm direct sunlight on the scene (0 once the sun is gone). */
warm: number;
/** Pink light on the snow peaks during the sun break. */
alpenglow: number;
/** Sunbeams fanning down through the gap. */
rays: number;
cover: number;
clouds: CloudField;
/** Cached per-pixel cloud tones (see shadeClouds). */
cloudTone: Uint8Array;
/** Beam brightness by angle from the sun (see clouds.ts). */
rayTable: Float32Array;
/** Top row of the cloud sea. */
sea: number;
/** Per range (far → near), 0 clear to 1 lost in cloud. */
veil: number[];
/** Lit color of the cloud sea and mist. */
fog: RGB;
/** Mist streaming over the summit, 0–1. */
mist: number;
wind: number;
/** Total wind travel in pixels (see driftAt). */
drift: number;
/** Falling snow density, 0–1. */
snow: number;
/** Snow settled on ledges and rocks, 0–1. */
snowCover: number;
/** Lamp glow behind each window in WINDOWS, including flicker. */
windows: number[];
/** Overall brightness for the fade in/out. */
fade: number;
}
/**
* Clouds move about a pixel a second, so the field 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 CLOUD_STEP = 1 / 12;
let cache: { key: number; clouds: CloudField; tone: Uint8Array; rays: Float32Array; sunOpen: number } | undefined;
function cloudsAt(t: number) {
const key = Math.round(t / CLOUD_STEP);
if (cache?.key === key) return cache;
const tq = key * CLOUD_STEP;
const sx = sunX(tq);
const sy = sunY(tq);
const cv = cover(tq);
const clouds = computeClouds(tq * 0.9 + driftAt(tq) * 0.22, cv, gap(tq), sx, sy, cache?.clouds);
let open = 0;
for (let i = 0; i < 9; i++) {
const a = (i / 9) * Math.PI * 2;
open += openness(clouds, sx + Math.cos(a) * (i ? 4 : 0), sy + Math.sin(a) * (i ? 4 : 0));
}
cache = {
key,
clouds,
tone: shadeClouds(clouds, sx, cv, cache?.tone),
rays: computeRayTable(clouds, sx, sy, tq, cache?.rays),
sunOpen: open / 9,
};
return cache;
}
export function frameAt(t: number): Frame {
const sky = SKY.map((c) => c(t));
const { clouds, tone: cloudTone, rays: rayTable, sunOpen } = cloudsAt(t);
const warm = sunPower(t) * (0.15 + 0.85 * sunOpen);
const g = gap(t);
const amb = scale(AMBIENT(t), 1 / 255);
return {
t,
// The sun break lifts the light a little, warmly.
amb: add(amb, [0.12, 0.07, 0.02], g * sunOpen),
skyBands: Array.from({ length: SKY_BANDS }, (_, i) => gradient(sky, i / (SKY_BANDS - 1))),
horizonColor: sky[4],
sunX: sunX(t),
sunY: sunY(t),
sunColor: sunColor(t),
sunOpen,
warm,
alpenglow: g * sunOpen,
rays: g * sunOpen,
cover: cover(t),
clouds,
cloudTone,
rayTable,
sea: sea(t),
veil: VEIL.map((v) => v(t)),
fog: FOG(t),
mist: mist(t),
wind: windAt(t),
drift: driftAt(t),
snow: snow(t),
snowCover: snowCover(t),
windows: WINDOWS.map((w, i) => {
const flicker = 0.84 + 0.1 * noise1(t * 3.1 + i * 7, 50 + i) + 0.06 * noise1(t * 9 + i, 60 + i);
const lamp = progress(t, lampTime(w.order), lampTime(w.order) + 3.5);
return Math.max(w.shrine ? 0.3 : 0, lamp) * flicker;
}),
fade: progress(t, TIMES.fadeIn[0], TIMES.fadeIn[1]) * (1 - progress(t, TIMES.fadeOut[0], TIMES.fadeOut[1])),
};
}
/** Apply the frame's ambient light to a base (daylight) color. */
export function lit(f: Frame, base: RGB): RGB {
return mul(base, f.amb);
}
/** Ambient light plus a share of warm direct sun, for surfaces facing the sun. */
export function sunlit(f: Frame, base: RGB, k = 0.25): RGB {
return add(mul(base, f.amb), mul(base, scale(f.sunColor, 1 / 255)), k * f.warm);
}