Add Desert Sunrise meditation scene
- 5-minute pixel-art canyon scene: starry night with Milky Way and shooting stars, owl gliding into a sandstone arch, deer drinking at the stream, then sunrise breaking through the arch with sun rays, alpenglow, mist, and morning wildlife (hawk, swallows, lizard) - New world model with pinhole-camera projection for shared perspective across walls, floor, stream, plants, and creatures - Procedural flora (sage, rabbitbrush, yucca, prickly pear, primrose, juniper, cottonwood, willow) placed along the canyon banks - Reflective stream with flowing ripples, glitter, and bank mist that lifts at dawn - Precomputed rock/floor layouts and cached plant shapes for per-frame rendering on a 320x180 buffer - Register the meditation in the menu and README; add accompanying audio track
This commit is contained in:
parent
50526614dc
commit
27ec480682
|
|
@ -38,6 +38,7 @@ so edits on the host reload in the browser; don't run it and `npm run dev` on th
|
|||
| `mediterranean-coast` | Mediterranean Coast — clearing skies over a jungle cove | 5 min |
|
||||
| `the-reef` | The Reef — drifting along a sunlit coral reef | 5 min |
|
||||
| `ringed-planet` | The Ringed Planet — drifting in high orbit above a ringed giant | 5 min |
|
||||
| `desert-sunrise` | Desert Sunrise — dawn breaks over a canyon stream | 5 min |
|
||||
|
||||
Each meditation lives in `src/meditations/<id>/` and is registered in `src/meditations/registry.ts`.
|
||||
A scene is a pure function of time (`render(surface, t)`), drawn pixel by pixel into a 320×180 buffer
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -0,0 +1,275 @@
|
|||
import { ditherStep, hex, mix, type RGB } from '../../engine/color';
|
||||
import { progress } from '../../engine/Timeline';
|
||||
import { hash, noise1, noise2 } from '../../engine/rng';
|
||||
import type { Surface } from '../../engine/Surface';
|
||||
import type { Frame } from './timeline';
|
||||
import {
|
||||
ARCH,
|
||||
archOpening,
|
||||
archRock,
|
||||
CAM_H,
|
||||
F,
|
||||
FOOT,
|
||||
H,
|
||||
HORIZON,
|
||||
NORMAL_X,
|
||||
NORMAL_Z,
|
||||
RIM,
|
||||
ROCK,
|
||||
SIDE,
|
||||
VPX,
|
||||
W,
|
||||
wallHeight,
|
||||
WINDOW,
|
||||
ZHIT,
|
||||
} from './world';
|
||||
|
||||
// Canyonlands layers: a pale cap rock over red cliffs streaked with desert varnish, on a ledgy talus base.
|
||||
const CAP = hex('#e2bc92');
|
||||
const CAP_SHADE = hex('#c8a07c');
|
||||
const CLIFF = [hex('#c4623c'), hex('#b45634'), hex('#cc7046'), hex('#ba5c38')];
|
||||
const LEDGE = hex('#8a4430');
|
||||
const VARNISH = hex('#4e302a');
|
||||
const TALUS = hex('#a66c4c');
|
||||
const TALUS_ROCK = hex('#7c4c3a');
|
||||
const ALPENGLOW = hex('#ff8c8c');
|
||||
const BEAM = hex('#ffd49a');
|
||||
|
||||
/** Height above the floor (m) of a wall pixel. */
|
||||
const heightAt = (x: number, y: number) => CAM_H + ((HORIZON - (y + 0.5)) * ZHIT[x]) / F;
|
||||
|
||||
function rockColor(x: number, y: number, z: number, side: number, h: number, top: number): RGB {
|
||||
const grain = hash(x, y, 40) * 0.07 - 0.035;
|
||||
// Broad vertical fluting where the cliff face bulges and recedes.
|
||||
const flute = (noise1(z * 1.1, 49 + side) - 0.5) * 0.16 + (noise1(z * 3.7, 50 + side) - 0.5) * 0.08;
|
||||
const tone = (c: RGB): RGB => [c[0] * (1 + grain + flute), c[1] * (1 + grain + flute), c[2] * (1 + grain + flute)];
|
||||
const fromTop = top - h;
|
||||
if (fromTop < 1.6 + noise1(z / 2, 43) * 1.2) return tone(fromTop < 0.5 ? CAP : mix(CAP, CAP_SHADE, 0.5));
|
||||
// A talus slope of fallen blocks at the foot of the cliff.
|
||||
const talusTop = 1.2 + noise1(z / 2.4, 44 + side) * 2;
|
||||
if (h < talusTop) {
|
||||
const b = noise2(z * 2.2, h * 2.6, 45 + side);
|
||||
if (b > 0.58) return tone(noise2(z * 2.2, (h + 0.12) * 2.6, 45 + side) > 0.58 ? mix(TALUS, CAP, 0.25) : TALUS_ROCK);
|
||||
return tone(TALUS);
|
||||
}
|
||||
const strata = h + Math.sin(z / 23 + side) * 0.7 + (noise1(z / 3, 41 + side) - 0.5) * 0.8;
|
||||
const band = Math.floor(strata / 2.6);
|
||||
const frac = strata / 2.6 - band;
|
||||
let c = mix(CLIFF[((band % 4) + 4) % 4], CLIFF[(((band + 1) % 4) + 4) % 4], Math.max(0, frac - 0.7) * 2);
|
||||
// Broken ledges between the beds.
|
||||
if (frac < 0.07 && noise1(z * 0.9, 46 + band) > 0.45) c = mix(c, LEDGE, 0.7);
|
||||
// Varnish hangs in long streaks down from the cap rock.
|
||||
const streak = noise1(z * 1.7 + side * 50, 42);
|
||||
const len = 4 + noise1(z * 0.6, 47) * 9;
|
||||
if (streak > 0.6 && fromTop < len) c = mix(c, VARNISH, Math.min(0.8, (streak - 0.6) * 3.5) * (1 - fromTop / len));
|
||||
return tone(c);
|
||||
}
|
||||
|
||||
/** Base (daylight) color of every rock pixel; fixed by the layout. */
|
||||
const BASE = new Uint8ClampedArray(W * H * 3);
|
||||
/** Height above the floor of each rock pixel, for the sun line and alpenglow. */
|
||||
const HEIGHT = new Float32Array(W * H).fill(-1);
|
||||
/** How squarely each column's wall faces the sun, 0–1. */
|
||||
const FACE = new Float32Array(W);
|
||||
/** Columns sitting just behind a nearer wall, outlined to separate the layers. */
|
||||
const EDGE = new Uint8Array(W);
|
||||
|
||||
for (let x = 0; x < W; x++) {
|
||||
const side = SIDE[x];
|
||||
if (side === 0) continue;
|
||||
const z = ZHIT[x];
|
||||
const top = wallHeight(z, side as -1 | 1);
|
||||
for (let y = Math.max(0, RIM[x]); y < FOOT[x]; y++) {
|
||||
const h = heightAt(x, y);
|
||||
const c = rockColor(x, y, z, side, h, top);
|
||||
const i = y * W + x;
|
||||
BASE.set(c, i * 3);
|
||||
HEIGHT[i] = h;
|
||||
}
|
||||
const near = (n: number) => n >= 0 && n < W && SIDE[n] !== 0 && ZHIT[n] < z * 0.85;
|
||||
EDGE[x] = near(x - 1) || near(x + 1) ? 1 : 0;
|
||||
}
|
||||
|
||||
// The arch is colored as if it were part of the wall it stands on.
|
||||
const ARCH_Z = ZHIT[ARCH.cx];
|
||||
for (let y = 0; y <= HORIZON; y++) {
|
||||
for (let x = ARCH.cx - 30; x < ARCH.cx + 30; x++) {
|
||||
if (!archRock(x, y) || (SIDE[x] !== 0 && y >= RIM[x])) continue;
|
||||
const h = CAM_H + ((HORIZON - (y + 0.5)) * ARCH_Z) / F;
|
||||
const c = rockColor(x, y, ARCH_Z + x * 0.1, -1, h, h + (ARCH.base - y < 0 ? 12 : ARCH.base - y < 4 ? 0 : 3));
|
||||
BASE.set(c, (y * W + x) * 3);
|
||||
HEIGHT[y * W + x] = 99;
|
||||
}
|
||||
}
|
||||
|
||||
/** Arch rock touching the opening: it glows when the sun is right behind it. */
|
||||
const ARCH_INNER: [number, number][] = [];
|
||||
for (let y = 0; y < HORIZON; y++) {
|
||||
for (let x = ARCH.cx - 30; x < ARCH.cx + 30; x++) {
|
||||
if (archRock(x, y) && (archOpening(x, y + 1) || archOpening(x - 1, y) || archOpening(x + 1, y))) ARCH_INNER.push([x, y]);
|
||||
}
|
||||
}
|
||||
|
||||
/** Which way the sun shines along the canyon, per column, worked out from its screen position. */
|
||||
function sunFacing(f: Frame): void {
|
||||
const dx = (f.sunX - VPX) / F;
|
||||
const n = Math.hypot(dx, 1);
|
||||
for (let x = 0; x < W; x++) {
|
||||
if (SIDE[x] === 0) continue;
|
||||
const d = (NORMAL_X[x] * dx + NORMAL_Z[x]) / n;
|
||||
FACE[x] = Math.max(0, Math.min(1, 0.35 + d * 2.2));
|
||||
}
|
||||
}
|
||||
|
||||
export function drawWalls(s: Surface, f: Frame): void {
|
||||
sunFacing(f);
|
||||
const amb = f.wallAmb;
|
||||
const sun = f.sunColor;
|
||||
const day = f.daylight;
|
||||
const d = s.data;
|
||||
for (let x = 0; x < W; x++) {
|
||||
const side = SIDE[x];
|
||||
if (side === 0) continue;
|
||||
const z = ZHIT[x];
|
||||
const top = wallHeight(z, side as -1 | 1);
|
||||
const reach = side > 0 ? f.shadowR : f.shadowL;
|
||||
// The sun line creeps down from the rim, ragged where the far rim's shadow falls.
|
||||
const line = top - (top + 1) * reach - 0.6 + (noise1(z / 3.5, 48 + side) - 0.5) * 2.4;
|
||||
const glowBand = top - 3.5;
|
||||
for (let y = Math.max(0, RIM[x]); y < FOOT[x]; y++) {
|
||||
const i = y * W + x;
|
||||
if (!ROCK[i]) continue;
|
||||
const h = HEIGHT[i];
|
||||
const b = i * 3;
|
||||
let r = BASE[b] * amb[0];
|
||||
let g = BASE[b + 1] * amb[1];
|
||||
let bl = BASE[b + 2] * amb[2];
|
||||
if (day > 0) {
|
||||
const u = h - line + 0.5;
|
||||
const lit = (u <= 0 ? 0 : u >= 1 ? 1 : ditherStep(u, x, y, 3)) * day * FACE[x] * 0.95;
|
||||
r += BASE[b] * (sun[0] / 255) * lit;
|
||||
g += BASE[b + 1] * (sun[1] / 255) * lit;
|
||||
bl += BASE[b + 2] * (sun[2] / 255) * lit;
|
||||
}
|
||||
if (f.alpenglow > 0 && h > glowBand) {
|
||||
const a = ditherStep(f.alpenglow * Math.min(1, (h - glowBand) / 2.5) * (side > 0 ? 0.42 : 0.25), x, y, 4);
|
||||
r += (ALPENGLOW[0] * (BASE[b] / 255) - r) * a;
|
||||
g += (ALPENGLOW[1] * (BASE[b + 1] / 255) - g) * a;
|
||||
bl += (ALPENGLOW[2] * (BASE[b + 2] / 255) - bl) * a;
|
||||
}
|
||||
const k = EDGE[x] ? 0.72 : 1;
|
||||
const o = i * 4;
|
||||
d[o] = r * k;
|
||||
d[o + 1] = g * k;
|
||||
d[o + 2] = bl * k;
|
||||
d[o + 3] = 255;
|
||||
}
|
||||
}
|
||||
drawWindow(s, f);
|
||||
drawArch(s, f);
|
||||
}
|
||||
|
||||
/** A small window worn through a fin near the top of the left wall. */
|
||||
function drawWindow(s: Surface, f: Frame): void {
|
||||
const w = WINDOW;
|
||||
for (let y = Math.floor(w.cy - w.ry); y <= w.cy + w.ry; y++) {
|
||||
for (let x = Math.floor(w.cx - w.rx); x <= w.cx + w.rx; x++) {
|
||||
const dx = (x + 0.5 - w.cx) / w.rx;
|
||||
const dy = (y + 0.5 - w.cy) / w.ry;
|
||||
if (dx * dx + dy * dy > 1 || (dy > 0 && Math.abs(dx) > 0.85)) continue;
|
||||
// The sky behind, roughly the color of the sky at the same height.
|
||||
s.set(x, y, f.skyBands[Math.min(f.skyBands.length - 1, Math.round((y / HORIZON) * (f.skyBands.length - 1)))]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawArch(s: Surface, f: Frame): void {
|
||||
const amb = f.wallAmb;
|
||||
// The arch is lit from behind, so it stays mostly in shade apart from its glowing underside.
|
||||
const rimLight = f.daylight * 0.35;
|
||||
for (let y = 0; y <= HORIZON; y++) {
|
||||
for (let x = ARCH.cx - 30; x < ARCH.cx + 30; x++) {
|
||||
if (!archRock(x, y) || (SIDE[x] !== 0 && y >= RIM[x])) continue;
|
||||
const b = (y * W + x) * 3;
|
||||
const base: RGB = [BASE[b], BASE[b + 1], BASE[b + 2]];
|
||||
let c: RGB = [base[0] * amb[0], base[1] * amb[1], base[2] * amb[2]];
|
||||
if (rimLight > 0 && ROCK[(y - 1) * W + x] === 0 && !archOpening(x, y - 1)) c = mix(c, f.sunColor, rimLight);
|
||||
s.set(x, y, c);
|
||||
}
|
||||
}
|
||||
// Light bouncing up off the canyon floor sets the underside of the arch aglow, as at sunrise
|
||||
// on the real arches of canyon country.
|
||||
const glow = Math.max(f.beam, f.alpenglow * 0.35);
|
||||
if (glow > 0) {
|
||||
const col = mix(hex('#ff7a3c'), hex('#ffb060'), f.beam);
|
||||
for (const [x, y] of ARCH_INNER) {
|
||||
s.blend(x, y, col, ditherStep(glow * 0.85, x, y, 4));
|
||||
if (archRock(x, y - 1)) s.blend(x, y - 1, col, ditherStep(glow * 0.45, x, y - 1, 4));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Angular table of how open the way from the sun is, recomputed per frame (rock moves relative to the sun). */
|
||||
const RAY_N = 360;
|
||||
const RAYS = new Float32Array(RAY_N);
|
||||
|
||||
function computeRays(f: Frame): void {
|
||||
for (let i = 0; i < RAY_N; i++) {
|
||||
const a = (i / RAY_N) * Math.PI * 2;
|
||||
const ca = Math.cos(a);
|
||||
const sa = Math.sin(a);
|
||||
let open = 0;
|
||||
for (let r = 9; r <= 15; r += 2) {
|
||||
const x = Math.round(f.sunX + ca * r);
|
||||
const y = Math.round(f.sunY + sa * r);
|
||||
if (x < 0 || x >= W || y < 0 || y >= H || !ROCK[y * W + x]) open += 0.25;
|
||||
}
|
||||
// Alternate bright and faint by angle so the light reads as separate shafts.
|
||||
RAYS[i] = open * (0.55 + 0.45 * Math.sin(a * 38 + f.t * 0.05));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shafts of light fanning down the canyon from the sun. They are strongest while the sun is
|
||||
* framed in the arch, where they also throw a bright patch onto the right-hand wall.
|
||||
*/
|
||||
export function drawSunRays(s: Surface, f: Frame): void {
|
||||
if (f.daylight <= 0 || f.sunVis <= 0) return;
|
||||
computeRays(f);
|
||||
const k0 = f.daylight * (0.12 + 0.3 * f.beam) * (1 - 0.6 * progress(f.t, 268, 296)) * Math.min(1, f.sunVis * 2);
|
||||
const sx = f.sunX;
|
||||
const sy = f.sunY;
|
||||
for (let by = Math.max(0, Math.floor(sy - 20)) & ~1; by < H; by += 2) {
|
||||
for (let bx = 0; bx < W; bx += 2) {
|
||||
const dx = bx + 1 - sx;
|
||||
const dy = by + 1 - sy;
|
||||
const r = Math.hypot(dx, dy);
|
||||
if (r < 10) continue;
|
||||
const fall = k0 * Math.exp(-r / 120) * (by < sy ? 0.35 : 1);
|
||||
if (fall < 0.015) continue;
|
||||
let a = Math.atan2(dy, dx);
|
||||
if (a < 0) a += Math.PI * 2;
|
||||
const k = fall * RAYS[Math.floor((a / (Math.PI * 2)) * RAY_N) % RAY_N] * Math.min(1, (r - 10) / 16);
|
||||
if (k < 0.01) continue;
|
||||
for (let y = by; y < by + 2; y++) for (let x = bx; x < bx + 2; x++) s.light(x, y, f.sunColor, ditherStep(k, x, y, 16));
|
||||
}
|
||||
}
|
||||
if (f.beam > 0) drawBeamPatch(s, f);
|
||||
}
|
||||
|
||||
/** Where the main shaft through the arch lands on the right wall. */
|
||||
const PATCH = { x: 236, y: 112, rx: 17, ry: 10 };
|
||||
|
||||
function drawBeamPatch(s: Surface, f: Frame): void {
|
||||
const drift = (f.sunY - ARCH.openY) * -0.6;
|
||||
const cx = PATCH.x + drift;
|
||||
const cy = PATCH.y - drift * 0.4;
|
||||
for (let y = Math.floor(cy - PATCH.ry); y <= cy + PATCH.ry; y++) {
|
||||
for (let x = Math.floor(cx - PATCH.rx); x <= cx + PATCH.rx; x++) {
|
||||
if (SIDE[x] !== 1 || y < RIM[x] || y >= FOOT[x]) continue;
|
||||
const d = Math.hypot((x - cx) / PATCH.rx, (y - cy) / PATCH.ry);
|
||||
if (d > 1) continue;
|
||||
s.light(x, y, BEAM, ditherStep(f.beam * 0.5 * (1 - d * d), x, y, 12));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,163 @@
|
|||
import { hex, mix, type RGB } from '../../engine/color';
|
||||
import { drawSprite, type SpriteArt } from '../../engine/sprite';
|
||||
import { pathTrack, progress } from '../../engine/Timeline';
|
||||
import { hash } from '../../engine/rng';
|
||||
import type { Surface } from '../../engine/Surface';
|
||||
import { LIZARD_ROCK } from './flora';
|
||||
import { floorSunAt } from './stream';
|
||||
import { lit, sunlit, TIMES, type Frame, type Thing } from './timeline';
|
||||
import { ARCH, F, project, W } from './world';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Great horned owl: glides in from up the canyon and roosts in the arch for the day
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const OWL_PERCH: SpriteArt = ['b...b', 'bbbbb', 'bybyb', 'bbobb', 'bwlwb', 'blwlb', '.bwb.', '.t.t.'];
|
||||
const OWL_BLINK: SpriteArt = ['b...b', 'bbbbb', 'bbbbb', 'bbobb', 'bwlwb', 'blwlb', '.bwb.', '.t.t.'];
|
||||
const OWL_LOOK: SpriteArt = ['.b..b', 'bbbbb', 'bbyby', 'bbbob', 'bwlwb', 'blwlb', '.bwb.', '.t.t.'];
|
||||
const OWL_GLIDE: SpriteArt = ['bb.........bb', '.bbbb.k.bbbb.', '....bbbbb....', '.....bwb.....'];
|
||||
const OWL_UP: SpriteArt = ['b...........b', '.b.........b.', '..bbb.k.bbb..', '.....bbb.....'];
|
||||
const OWL_DOWN: SpriteArt = ['.....bkb.....', '...bbbbbbb...', '.bb.......bb.', 'b...........b'];
|
||||
|
||||
const [OWL_T0, OWL_T1] = TIMES.owl;
|
||||
const PERCH_X = ARCH.cx - 9;
|
||||
const PERCH_Y = ARCH.base - 4;
|
||||
const owlPath = pathTrack([
|
||||
[OWL_T0, 262, 8],
|
||||
[OWL_T0 + 7, 232, 22],
|
||||
[OWL_T0 + 15, 198, 34],
|
||||
[OWL_T0 + 22, 170, 46],
|
||||
[OWL_T1 - 1.5, PERCH_X + 6, PERCH_Y - 6],
|
||||
[OWL_T1, PERCH_X, PERCH_Y],
|
||||
]);
|
||||
|
||||
export function drawOwl(s: Surface, f: Frame): void {
|
||||
const t = f.t;
|
||||
if (t < OWL_T0) return;
|
||||
const pal: Record<string, RGB> = {
|
||||
b: lit(f, hex('#7a6450')),
|
||||
w: lit(f, hex('#d8ccb4')),
|
||||
l: lit(f, hex('#9a8670')),
|
||||
k: lit(f, hex('#4a3a2e')),
|
||||
o: lit(f, hex('#3a3430')),
|
||||
t: lit(f, hex('#c0a060')),
|
||||
// Eyes catch the dawn light.
|
||||
y: mix(lit(f, hex('#f0c040')), hex('#f0c040'), 0.5),
|
||||
};
|
||||
if (t < OWL_T1) {
|
||||
const [x, y] = owlPath(t);
|
||||
// A few slow, silent wingbeats between long glides, and a flare to land.
|
||||
const dt = t - OWL_T0;
|
||||
const landing = t > OWL_T1 - 1.6;
|
||||
const cyc = dt % 5;
|
||||
let art = OWL_GLIDE;
|
||||
if (landing) art = Math.floor(t * 5) % 2 ? OWL_UP : OWL_DOWN;
|
||||
else if (cyc < 1.2) art = [OWL_UP, OWL_GLIDE, OWL_DOWN, OWL_GLIDE][Math.floor(cyc / 0.3) % 4];
|
||||
// Smaller while it is still far up the canyon.
|
||||
const far = progress(t, OWL_T0, OWL_T0 + 12);
|
||||
if (far < 1 && dt < 6) {
|
||||
s.blend(Math.round(x), Math.round(y), pal.b, 1);
|
||||
s.blend(Math.round(x) - 1, Math.round(y), pal.b, 0.8);
|
||||
s.blend(Math.round(x) + 1, Math.round(y), pal.b, 0.8);
|
||||
return;
|
||||
}
|
||||
drawSprite(s, art, Math.round(x - 6), Math.round(y - 2), pal);
|
||||
return;
|
||||
}
|
||||
const blink = hash(Math.floor(t * 1.3), 0, 5) > 0.88 && (t * 1.3) % 1 < 0.25;
|
||||
const look = Math.sin(t * 0.21) > 0.75;
|
||||
// Once the sun is up it is backlit: a dark shape with a bright rim.
|
||||
const back = f.daylight * (1 - f.beam * 0.5);
|
||||
if (back > 0) {
|
||||
pal.b = mix(pal.b, mix(pal.b, f.sunColor, 0.15), back);
|
||||
}
|
||||
drawSprite(s, blink ? OWL_BLINK : look ? OWL_LOOK : OWL_PERCH, PERCH_X - 2, PERCH_Y - 4, pal);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Red-tailed hawk circling high on the morning air
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const HAWK: SpriteArt = ['.k.........k.', 'kwwwk...kwwwk', '..kwwwwwwwk..', '.....wrw.....', '......r......'];
|
||||
const HAWK_TILT: SpriteArt = ['k............', 'kwwk.........', '.kwwwk...kk..', '...kwwwwwwwk.', '.....wrw.....', '......r......'];
|
||||
|
||||
export function drawHawk(s: Surface, f: Frame): void {
|
||||
const dt = f.t - TIMES.hawk;
|
||||
if (dt < 0) return;
|
||||
const pal: Record<string, RGB> = {
|
||||
k: sunlit(f, hex('#5a3a2a'), 1, 0.4),
|
||||
w: sunlit(f, hex('#efe2c8'), 1, 0.4),
|
||||
r: sunlit(f, hex('#c4643c'), 1, 0.5),
|
||||
};
|
||||
// Drifts in from the upper right, then rides a thermal in slow, rising circles.
|
||||
const enter = Math.min(1, dt / 9);
|
||||
const a = dt * ((Math.PI * 2) / 24);
|
||||
const cx = 204 + (1 - enter) * 120;
|
||||
const cy = 30 - dt * 0.12 - (1 - enter) * 30;
|
||||
const x = cx + Math.cos(a) * 26 * enter;
|
||||
const y = cy + Math.sin(a) * 8 * enter;
|
||||
const vx = -Math.sin(a);
|
||||
const banked = Math.abs(Math.cos(a)) > 0.8;
|
||||
drawSprite(s, banked ? HAWK_TILT : HAWK, Math.round(x - 6), Math.round(y - 2), pal, vx < 0);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Cliff swallows darting along the sunlit right wall
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const SWALLOW_UP: SpriteArt = ['k.k', '.w.'];
|
||||
const SWALLOW_DOWN: SpriteArt = ['...', 'kwk'];
|
||||
|
||||
export function drawSwallows(s: Surface, f: Frame): void {
|
||||
const dt = f.t - TIMES.swallows;
|
||||
if (dt < 0) return;
|
||||
const pal: Record<string, RGB> = { k: sunlit(f, hex('#2c3458'), 1, 0.3), w: sunlit(f, hex('#f2e2cc'), 1, 0.4) };
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const t = f.t;
|
||||
const p = i * 1.9;
|
||||
const x = 246 + 46 * Math.sin(t * 0.55 + p) + 18 * Math.sin(t * 1.7 + p * 2) + Math.max(0, 6 + i - dt) * 16;
|
||||
const y = 58 + 22 * Math.sin(t * 0.8 + p * 1.3) + 8 * Math.sin(t * 2.3 + p);
|
||||
if (x > W + 4) continue;
|
||||
const vx = 46 * 0.55 * Math.cos(t * 0.55 + p) + 18 * 1.7 * Math.cos(t * 1.7 + p * 2);
|
||||
const flap = Math.floor(t * 9 + i) % 3 === 0;
|
||||
drawSprite(s, flap ? SWALLOW_DOWN : SWALLOW_UP, Math.round(x), Math.round(y), pal, vx < 0);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// A side-blotched lizard comes up onto its warm rock and does push-ups
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const LIZARD: SpriteArt = ['.....d.dbb.', 'ttttbbsbbbe', '...l....l..'];
|
||||
const LIZARD_UP: SpriteArt = ['.....d.dbbe', 'ttttbbsbbb.', '...l....l..'];
|
||||
|
||||
function drawLizard(s: Surface, f: Frame): void {
|
||||
const dt = f.t - TIMES.lizard;
|
||||
if (dt < 0) return;
|
||||
const { X, z, size } = LIZARD_ROCK;
|
||||
const k = F / z;
|
||||
const [bx, by] = project(X, z);
|
||||
const ry = 0.24 * size * k;
|
||||
const top = by + Math.max(1, 0.08 * size * k) - 2 * ry;
|
||||
// Climbs up the near side of the rock, then rests on top.
|
||||
const climb = progress(dt, 0, 5);
|
||||
const x = bx + 0.3 * size * k * (1 - climb) - 5;
|
||||
const y = by - 1 - (by - top - 2) * climb;
|
||||
if (climb < 0.02) return;
|
||||
const sun = floorSunAt(f, X, z);
|
||||
const pal: Record<string, RGB> = {
|
||||
b: sunlit(f, hex('#7c6648'), sun),
|
||||
d: sunlit(f, hex('#c8b490'), sun),
|
||||
s: sunlit(f, hex('#2c4a7a'), sun),
|
||||
t: sunlit(f, hex('#6a5640'), sun),
|
||||
l: sunlit(f, hex('#4a3a2a'), sun),
|
||||
e: hex('#141210'),
|
||||
};
|
||||
// Push-ups in little sets of two.
|
||||
const bob = [8, 13, 18].some((p) => dt > p && dt < p + 1.6 && (dt - p) % 0.8 < 0.4);
|
||||
drawSprite(s, bob ? LIZARD_UP : LIZARD, Math.round(x), Math.round(y - 2), pal, true);
|
||||
}
|
||||
|
||||
export function lizardThing(): Thing {
|
||||
return { z: LIZARD_ROCK.z - 0.02, draw: drawLizard };
|
||||
}
|
||||
|
|
@ -0,0 +1,298 @@
|
|||
import { hex, mix, type RGB } from '../../engine/color';
|
||||
import { hash } from '../../engine/rng';
|
||||
import { sampleShape, Stamp } from '../../engine/stamp';
|
||||
import type { Surface } from '../../engine/Surface';
|
||||
import { clamp01, ease, pathTrack, track } from '../../engine/Timeline';
|
||||
import { floorSunAt } from './stream';
|
||||
import { sunlit, TIMES, type Frame, type Thing } from './timeline';
|
||||
import { F, project, streamHalf, streamX } from './world';
|
||||
|
||||
/**
|
||||
* A mule deer doe and her family come down the canyon in the blue before dawn. They stop at the
|
||||
* stream to drink, walk on toward the camera, drink again as the sun breaks through the arch,
|
||||
* then wade across and wander off to the left.
|
||||
*/
|
||||
|
||||
/** A point on the right bank, `off` meters back from the water's edge. */
|
||||
const bank = (z: number, off: number): [number, number] => [streamX(z) + streamHalf(z) + off, z];
|
||||
|
||||
interface Move {
|
||||
t0: number;
|
||||
t1: number;
|
||||
pts: [number, number][];
|
||||
}
|
||||
|
||||
const T = TIMES.deer;
|
||||
const MOVES: Move[] = [
|
||||
{ t0: T, t1: T + 46, pts: [bank(74, 1.2), bank(52, 1.0), bank(38, 0.7), bank(29, 0.18)] },
|
||||
{ t0: T + 71, t1: T + 108, pts: [bank(29, 0.18), bank(20, 0.9), bank(12, 0.8), bank(7.2, 0.12)] },
|
||||
{
|
||||
t0: T + 134,
|
||||
t1: T + 156,
|
||||
pts: [bank(7.2, 0.12), [streamX(6.4), 6.4], [streamX(5.9) - streamHalf(5.9) - 0.5, 5.9], [-3.5, 5.6], [-7.5, 5.4]],
|
||||
},
|
||||
];
|
||||
|
||||
/** Head down to drink (1) or raised (0), for the doe; the others follow her lead with a lag. */
|
||||
const DRINK = track([
|
||||
[T + 46, 0],
|
||||
[T + 49, 1],
|
||||
[T + 57, 1],
|
||||
[T + 59, 0.15],
|
||||
[T + 62, 0.15],
|
||||
[T + 64, 1],
|
||||
[T + 69, 1],
|
||||
[T + 71, 0],
|
||||
[T + 108, 0],
|
||||
[T + 111, 1],
|
||||
[T + 117, 1],
|
||||
// The sun breaks through the arch: she lifts her head and looks up the canyon at it.
|
||||
[T + 119, 0],
|
||||
[T + 125, 0],
|
||||
[T + 127, 1],
|
||||
[T + 132, 1],
|
||||
[T + 134, 0],
|
||||
]);
|
||||
|
||||
/** Progress 0–1 along a move, easing in and out over about a second at each end. */
|
||||
function along(t: number, m: Move): number {
|
||||
const d = m.t1 - m.t0;
|
||||
const a = Math.min(1.2, d / 3);
|
||||
const u = clamp01((t - m.t0) / d) * d;
|
||||
// Distance under a trapezoid speed profile.
|
||||
let s: number;
|
||||
if (u < a) s = (u * u) / (2 * a);
|
||||
else if (u > d - a) s = d - a - (d - u) ** 2 / (2 * a);
|
||||
else s = u - a / 2;
|
||||
return s / (d - a);
|
||||
}
|
||||
|
||||
interface Deer {
|
||||
lag: number;
|
||||
dz: number;
|
||||
dOff: number;
|
||||
size: number;
|
||||
coat: RGB;
|
||||
paths: ((u: number) => [number, number])[];
|
||||
seed: number;
|
||||
}
|
||||
|
||||
function makeDeer(lag: number, dz: number, dOff: number, size: number, coat: string, seed: number): Deer {
|
||||
return {
|
||||
lag,
|
||||
dz,
|
||||
dOff,
|
||||
size,
|
||||
coat: hex(coat),
|
||||
seed,
|
||||
paths: MOVES.map((m, mi) => {
|
||||
const pts = m.pts.map(([X, z], i): [number, number] => {
|
||||
// Followers stand a little further up the bank; on the final crossing they spread out.
|
||||
const zz = z + dz * (mi === 2 && i > 0 ? 0.4 : 1);
|
||||
return [X + (streamX(zz) - streamX(z)) + dOff * (mi === 2 ? 0.5 : 1), zz];
|
||||
});
|
||||
return pathTrack(pts.map(([X, z], i) => [i / (pts.length - 1), X, z] as const));
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
const HERD: Deer[] = [
|
||||
makeDeer(0, 0, 0, 1, '#9c7a5c', 1),
|
||||
makeDeer(3.5, 1.6, 0.55, 0.88, '#94765a', 2),
|
||||
makeDeer(6.5, 3.1, 0.15, 0.66, '#b08462', 3),
|
||||
makeDeer(9, 2.3, 1.0, 0.62, '#aa8060', 4),
|
||||
];
|
||||
|
||||
/** Where a deer is (world X, z) and which move it is on or last finished. */
|
||||
function place(d: Deer, t: number): { X: number; z: number; move: number; moving: boolean } {
|
||||
const tt = t - d.lag;
|
||||
let move = 0;
|
||||
for (let i = 0; i < MOVES.length; i++) if (tt >= MOVES[i].t0) move = i;
|
||||
const m = MOVES[move];
|
||||
const [X, z] = d.paths[move](along(tt, m));
|
||||
return { X, z, move, moving: tt > m.t0 && tt < m.t1 };
|
||||
}
|
||||
|
||||
/** Screen-space direction of travel during a move: +1 right, -1 left. */
|
||||
function moveDir(d: Deer, move: number, u: number): number {
|
||||
const a = d.paths[move](Math.max(0, u - 0.02));
|
||||
const b = d.paths[move](Math.min(1, u + 0.02));
|
||||
const xa = project(a[0], a[1])[0];
|
||||
const xb = project(b[0], b[1])[0];
|
||||
return xb >= xa ? 1 : -1;
|
||||
}
|
||||
|
||||
function facing(d: Deer, t: number): number {
|
||||
const tt = t - d.lag;
|
||||
const p = place(d, t);
|
||||
const m = MOVES[p.move];
|
||||
const u = along(tt, m);
|
||||
const dir = moveDir(d, p.move, Math.min(u, 0.98));
|
||||
if (p.move === 0 || tt > m.t0 + 1.6) return dir;
|
||||
// Turn around at the start of a move if it heads the other way.
|
||||
const prev = moveDir(d, p.move - 1, 0.98);
|
||||
return prev + (dir - prev) * ease.sine(clamp01((tt - m.t0 + 0.2) / 1.6));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Drawing
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
interface DeerPal {
|
||||
coat: RGB;
|
||||
dark: RGB;
|
||||
light: RGB;
|
||||
muzzle: RGB;
|
||||
nose: RGB;
|
||||
ear: RGB;
|
||||
hoof: RGB;
|
||||
}
|
||||
|
||||
/** Distance from p to a tapering limb running from a to b; <= 0 means inside. */
|
||||
function limb(lx: number, ly: number, ax: number, ay: number, bx: number, by: number, r0: number, r1: number): number {
|
||||
const vx = bx - ax;
|
||||
const vy = by - ay;
|
||||
const u = Math.max(0, Math.min(1, ((lx - ax) * vx + (ly - ay) * vy) / (vx * vx + vy * vy)));
|
||||
return Math.hypot(lx - ax - vx * u, ly - ay - vy * u) - (r0 + (r1 - r0) * u);
|
||||
}
|
||||
|
||||
/**
|
||||
* The deer in local units of 10 cm: x forward, y down, origin mid-body 85 cm above the ground.
|
||||
* `phase` drives the walk, `stride` its size (0 standing), `drink` lowers the head to the water.
|
||||
*/
|
||||
function deerSampler(p: DeerPal, phase: number, stride: number, drink: number, flick: number, earTwitch: number) {
|
||||
// The neck rises forward from the shoulders; to drink it swings down and the shoulders dip.
|
||||
const baseY = -0.6 + drink * 1.2;
|
||||
const neckA = -0.9 + drink * 2.1;
|
||||
const neckL = 4 + drink * 2.2;
|
||||
const nx = 4.4 + Math.cos(neckA) * neckL;
|
||||
const ny = baseY + Math.sin(neckA) * neckL;
|
||||
const headA = 0.45 + drink * 1.0;
|
||||
const hcx = nx + Math.cos(headA) * 1.1;
|
||||
const hcy = ny + Math.sin(headA) * 1.1;
|
||||
const hdx = Math.cos(headA);
|
||||
const hdy = Math.sin(headA);
|
||||
const legs = [
|
||||
{ hipX: 3.6, ph: 0.25, far: true },
|
||||
{ hipX: -4, ph: 0, far: true },
|
||||
{ hipX: 4, ph: 0.75, far: false },
|
||||
{ hipX: -3.6, ph: 0.5, far: false },
|
||||
].map((l) => {
|
||||
const a = (phase + l.ph) * Math.PI * 2;
|
||||
return {
|
||||
...l,
|
||||
fx: l.hipX + Math.sin(a) * 1.6 * stride,
|
||||
fy: 8.5 - Math.max(0, Math.cos(a)) * 1.1 * stride,
|
||||
kx: l.hipX + Math.sin(a) * 0.9 * stride + (l.hipX > 0 ? -0.3 : 0.4),
|
||||
};
|
||||
});
|
||||
const leg = (lx: number, ly: number, far: boolean): RGB | undefined => {
|
||||
for (const l of legs) {
|
||||
if (l.far !== far) continue;
|
||||
const ky = 4.6;
|
||||
if (limb(lx, ly, l.hipX, 1, l.kx, ky, 1.1, 0.55) <= 0 || limb(lx, ly, l.kx, ky, l.fx, l.fy, 0.55, 0.42) <= 0) {
|
||||
const c = ly > l.fy - 0.7 ? p.hoof : ly > 2.5 && lx < l.kx ? p.light : p.coat;
|
||||
return far ? mix(c, p.dark, 0.5) : c;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
return (lx: number, ly: number): RGB | undefined => {
|
||||
if (lx < -8.4 || lx > 11.5 || ly < -9 || ly > 10) return undefined;
|
||||
// Head, ears and neck in front of the body.
|
||||
const rx = lx - hcx;
|
||||
const ry = ly - hcy;
|
||||
const nearHead = rx * rx + ry * ry < 16;
|
||||
const along = rx * hdx + ry * hdy;
|
||||
const across = -rx * hdy + ry * hdx;
|
||||
const taper = 1.25 - Math.max(0, along) * 0.18;
|
||||
if (nearHead && along > -1.6 && along < 2.4 && Math.abs(across) < taper) {
|
||||
if (along > 1.7) return p.nose;
|
||||
if (along > 0.9) return p.muzzle;
|
||||
if (along > -0.6 && along < -0.1 && across < -0.2 && across > -0.8) return p.nose;
|
||||
return across > 0.5 ? p.light : p.coat;
|
||||
}
|
||||
for (let side = 0; side < 2 && nearHead; side++) {
|
||||
const ea = headA - 2.1 - side * 0.45 - earTwitch * (side ? 0.4 : 0);
|
||||
const ex = hcx - hdx * 0.9 + Math.cos(ea) * 1.4;
|
||||
const ey = hcy - hdy * 0.9 + Math.sin(ea) * 1.4;
|
||||
if (limb(lx, ly, hcx - hdx * 0.9, hcy - hdy * 0.9, ex, ey, 0.55, 0.75) <= 0) return side ? mix(p.coat, p.dark, 0.4) : p.ear;
|
||||
}
|
||||
if (lx > 2 && limb(lx, ly, 4.0, baseY + 0.4, nx, ny, 2, 1.15) <= 0) return ly > ny + 1.2 && lx > 5.4 && drink < 0.5 ? p.light : p.coat;
|
||||
const near = ly > 0 ? leg(lx, ly, false) : undefined;
|
||||
if (near) return near;
|
||||
// Tail: a dark-tipped rope that flicks up to show the white rump.
|
||||
if (lx < -5.5 && limb(lx, ly, -6.3, -1.2, -7.1 - flick * 0.4, 0.8 - flick * 2.6, 0.55, 0.4) <= 0) return ly > -1.2 + (1 - flick) * 1.6 ? p.nose : p.light;
|
||||
const body = (lx / 6.4) ** 2 + ((ly + (lx < 0 ? 0.15 : 0)) / 2.8) ** 2;
|
||||
const chest = Math.hypot(lx - 4.2, ly - 0.3) < 2.5;
|
||||
if (body <= 1 || chest) {
|
||||
if (lx < -5 && ly < 0.6) return p.light;
|
||||
if (ly > 1.7) return p.light;
|
||||
return ly < -1.8 ? mix(p.coat, p.light, 0.25) : p.coat;
|
||||
}
|
||||
return ly > 0 ? leg(lx, ly, true) : undefined;
|
||||
};
|
||||
}
|
||||
|
||||
const stamp = new Stamp();
|
||||
|
||||
function drawDeer(s: Surface, f: Frame, d: Deer): void {
|
||||
const t = f.t;
|
||||
const p = place(d, t);
|
||||
const k = F / p.z;
|
||||
const sc = (k / 10) * d.size;
|
||||
const [x, y] = project(p.X, p.z, 0.85 * d.size);
|
||||
if (x < -40 || x > 360) return;
|
||||
const sun = Math.max(floorSunAt(f, p.X, p.z), 0);
|
||||
const L = (c: RGB) => sunlit(f, c, sun);
|
||||
const pal: DeerPal = {
|
||||
coat: L(d.coat),
|
||||
dark: L(mix(d.coat, hex('#3c2c22'), 0.55)),
|
||||
light: L(hex('#e8dece')),
|
||||
muzzle: L(hex('#5a4a3e')),
|
||||
nose: L(hex('#1e1814')),
|
||||
ear: L(hex('#c8a890')),
|
||||
hoof: L(hex('#2c2420')),
|
||||
};
|
||||
// Walk cycle runs with distance covered, so legs never skate.
|
||||
const pa = place(d, t - 0.15);
|
||||
const pb = place(d, t + 0.15);
|
||||
const speed = Math.hypot(pb.X - pa.X, pb.z - pa.z) / 0.3;
|
||||
const stride = Math.min(1, speed / 0.9);
|
||||
const phase = t * 1.05 + d.seed * 0.37;
|
||||
const drink = DRINK(t - d.lag * 0.85 - d.seed * 0.6);
|
||||
const flick = Math.max(0, Math.sin(t * 0.9 + d.seed * 2) * 6 - 5);
|
||||
const twitch = hash(Math.floor(t * 0.7 + d.seed), d.seed, 9) > 0.85 ? 1 : 0;
|
||||
const v = { x, y, scale: sc, facing: facing(d, t) };
|
||||
sampleShape(stamp, v, 11.5, deerSampler(pal, phase, stride, drink, flick, twitch)).draw(s, 0.72);
|
||||
drawRipples(s, f, p.X, p.z, sc, stride, d.seed);
|
||||
}
|
||||
|
||||
/** Rings spreading on the water around a deer's legs while it wades or drinks. */
|
||||
function drawRipples(s: Surface, f: Frame, X: number, z: number, sc: number, stride: number, seed: number): void {
|
||||
const e = Math.abs(X - streamX(z)) - streamHalf(z);
|
||||
if (e > 0.45) return;
|
||||
const [cx, cy] = project(X, z);
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const age = (f.t * (0.6 + stride * 0.6) + i / 3 + seed * 0.13) % 1;
|
||||
const r = (2 + age * 12) * sc;
|
||||
const a = (1 - age) * 0.35 * (e < 0 ? 1 : 0.5);
|
||||
for (let j = 0; j < 40; j++) {
|
||||
const ang = (j / 40) * Math.PI * 2;
|
||||
const px = cx + Math.cos(ang) * r;
|
||||
const py = cy + Math.sin(ang) * r * 0.28;
|
||||
if (Math.abs(project(X, z)[0] - px) < 1 && py < cy) continue;
|
||||
const pxW = ((px - project(0, z)[0]) * z) / F;
|
||||
if (Math.abs(pxW - streamX(z)) > streamHalf(z)) continue;
|
||||
s.blend(px, py, [235, 240, 248], a * (0.5 + 0.5 * f.daylight));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function deerThings(f: Frame): Thing[] {
|
||||
if (f.t < TIMES.deer - 1) return [];
|
||||
return HERD.map((d) => {
|
||||
const p = place(d, f.t);
|
||||
return { z: p.z - 0.01, draw: (s: Surface, fr: Frame) => drawDeer(s, fr, d) };
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,376 @@
|
|||
import { hex, mix, type RGB } from '../../engine/color';
|
||||
import { progress } from '../../engine/Timeline';
|
||||
import { hash, noise2, Rng } from '../../engine/rng';
|
||||
import type { Surface } from '../../engine/Surface';
|
||||
import { sunReach, floorSunAt } from './stream';
|
||||
import { floorSunProgress, sunlit, type Frame, type Thing } from './timeline';
|
||||
import { canyonX, F, halfWidth, project, streamHalf, streamX, Z_NEAR } from './world';
|
||||
|
||||
type Kind = 'sage' | 'rabbitbrush' | 'grass' | 'yucca' | 'pear' | 'primrose' | 'juniper' | 'cottonwood' | 'boulder' | 'willow';
|
||||
|
||||
interface Plant {
|
||||
kind: Kind;
|
||||
X: number;
|
||||
z: number;
|
||||
size: number;
|
||||
seed: number;
|
||||
/** Prickly pear only: when the sun first reaches it, so its blooms can open. */
|
||||
sunAt: number;
|
||||
}
|
||||
|
||||
const SAGE = [hex('#a4b098'), hex('#84947e'), hex('#5e6c5c')] as const;
|
||||
const RABBIT = [hex('#98a466'), hex('#76844e'), hex('#55613c')] as const;
|
||||
const RABBIT_FLOWER = hex('#ecc23c');
|
||||
const GRASS = [hex('#c8b474'), hex('#a09454'), hex('#7c8248')] as const;
|
||||
const YUCCA = [hex('#9ab878'), hex('#6e8c5a'), hex('#4c6644')] as const;
|
||||
const STALK = hex('#8a6c4c');
|
||||
const PAD = [hex('#86b06a'), hex('#66945a'), hex('#486e44')] as const;
|
||||
const SPINE = hex('#efe6c4');
|
||||
const BLOOM_YELLOW = hex('#f8d448');
|
||||
const BLOOM_PINK = hex('#ec5c86');
|
||||
const BLOOM_CENTER = hex('#c8762c');
|
||||
const LEAF = [hex('#88986a'), hex('#687a52')] as const;
|
||||
const PRIMROSE = hex('#f8f4ea');
|
||||
const PRIMROSE_SHUT = hex('#e6a2b4');
|
||||
const JUNIPER = [hex('#6e8a72'), hex('#506e5a'), hex('#38503e')] as const;
|
||||
const BARK = hex('#6c5444');
|
||||
const COTTON_BARK = hex('#8a7c6c');
|
||||
const GOLD = [hex('#f8d850'), hex('#e8b02c'), hex('#b07a1c')] as const;
|
||||
const ROCK = [hex('#d4936c'), hex('#b07454'), hex('#7a4c3c')] as const;
|
||||
const WILLOW = [hex('#b4a45c'), hex('#8c8a4c'), hex('#6a5a3c')] as const;
|
||||
|
||||
type Tones = readonly [RGB, RGB, RGB];
|
||||
|
||||
/**
|
||||
* The pixels of one clump's shape, relative to its base point: x, y, and a light value (higher
|
||||
* toward the top) per pixel. Plants never move, so each shape is worked out once and reused.
|
||||
*/
|
||||
const SHAPES = new Map<string, Float32Array>();
|
||||
|
||||
function clumpShape(rx: number, ry: number, k: number, seed: number, lump: number, round: boolean): Float32Array {
|
||||
const key = `${rx.toFixed(2)}:${ry.toFixed(2)}:${seed}:${lump}:${round}`;
|
||||
let shape = SHAPES.get(key);
|
||||
if (shape) return shape;
|
||||
const out: number[] = [];
|
||||
const cy = round ? -ry : 0;
|
||||
for (let y = Math.floor(-ry * (round ? 2 : 1) - 1); y < 0; y++) {
|
||||
const v = (cy - (y + 0.5)) / ry;
|
||||
for (let x = Math.floor(-rx - 2); x <= rx + 2; x++) {
|
||||
const u = (x + 0.5) / rx;
|
||||
// Lumpy outline: noise sampled in meters so the texture shrinks with distance.
|
||||
const n = noise2((x / k) * 7 + seed, (-y / k) * 7, seed);
|
||||
if (u * u + v * v > 1 - lump * n) continue;
|
||||
if (!round && v < 0) continue;
|
||||
out.push(x, y, v - u * 0.3 + hash(x, y, seed) * 0.6);
|
||||
}
|
||||
}
|
||||
shape = Float32Array.from(out);
|
||||
SHAPES.set(key, shape);
|
||||
return shape;
|
||||
}
|
||||
|
||||
/** A rounded, lumpy clump of foliage (or rock) whose top catches the light. */
|
||||
function clump(
|
||||
s: Surface,
|
||||
f: Frame,
|
||||
cx: number,
|
||||
by: number,
|
||||
rx: number,
|
||||
ry: number,
|
||||
k: number,
|
||||
seed: number,
|
||||
tones: Tones,
|
||||
sun: number,
|
||||
opts: { lump?: number; shimmer?: number; sway?: number; round?: boolean } = {},
|
||||
): void {
|
||||
if (rx < 0.7) {
|
||||
s.set(cx, by - 1, sunlit(f, tones[1], sun));
|
||||
return;
|
||||
}
|
||||
const lit = tones.map((c) => sunlit(f, c, sun));
|
||||
const sway = opts.sway ?? 0;
|
||||
const h = ry * (opts.round ? 2 : 1);
|
||||
const shape = clumpShape(rx, ry, k, seed, opts.lump ?? 0.3, opts.round ?? false);
|
||||
const x0 = Math.round(cx);
|
||||
const y0 = Math.round(by);
|
||||
for (let i = 0; i < shape.length; i += 3) {
|
||||
const dy = shape[i + 1];
|
||||
const x = x0 + shape[i] + (sway ? Math.round((sway * -dy) / h) : 0);
|
||||
const y = y0 + dy;
|
||||
let l = shape[i + 2];
|
||||
// Leaves turning in the breeze flash light and dark.
|
||||
if (opts.shimmer) l += 0.15 * Math.sin(f.t * 3 + (shape[i] * 7.3 + dy * 3.1)) * opts.shimmer;
|
||||
s.set(x, y, lit[l > 0.95 ? 0 : l > 0.35 ? 1 : 2]);
|
||||
}
|
||||
}
|
||||
|
||||
function line(s: Surface, x0: number, y0: number, x1: number, y1: number, c: RGB): void {
|
||||
const n = Math.max(1, Math.ceil(Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0))));
|
||||
for (let i = 0; i <= n; i++) s.set(Math.round(x0 + ((x1 - x0) * i) / n), Math.round(y0 + ((y1 - y0) * i) / n), c);
|
||||
}
|
||||
|
||||
function drawBlades(s: Surface, f: Frame, p: Plant, bx: number, by: number, k: number, sun: number, tones: Tones, count: number, len: number): void {
|
||||
const r = new Rng(p.seed);
|
||||
for (let i = 0; i < count; i++) {
|
||||
const L = len * k * r.range(0.6, 1.1);
|
||||
const a0 = -Math.PI / 2 + r.range(-0.75, 0.75);
|
||||
const sway = (f.breeze * 0.18 + Math.sin(f.t * 1.3 + i) * 0.04) * Math.min(1, L / 6);
|
||||
const x0 = bx + r.range(-0.08, 0.08) * k;
|
||||
const mid = [x0 + Math.cos(a0) * L * 0.5, by + Math.sin(a0) * L * 0.5] as const;
|
||||
const end = [x0 + Math.cos(a0 + sway) * L, by + Math.sin(a0 + sway) * L] as const;
|
||||
const c = sunlit(f, tones[i % 3], sun);
|
||||
line(s, x0, by - 1, mid[0], mid[1], c);
|
||||
line(s, mid[0], mid[1], end[0], end[1], c);
|
||||
}
|
||||
}
|
||||
|
||||
function drawPear(s: Surface, f: Frame, p: Plant, bx: number, by: number, k: number, sun: number): void {
|
||||
const r = new Rng(p.seed);
|
||||
const lit = PAD.map((c) => sunlit(f, c, sun));
|
||||
const spine = sunlit(f, SPINE, sun);
|
||||
const bloom = progress(f.t, p.sunAt + 1, p.sunAt + 14);
|
||||
const bloomCol = sunlit(f, p.seed % 3 === 0 ? BLOOM_PINK : BLOOM_YELLOW, Math.max(sun, 0.3), 0.4);
|
||||
const centre = sunlit(f, BLOOM_CENTER, sun);
|
||||
const pads: [number, number, number][] = [];
|
||||
// Pads stack on each other's rims, each tilted a little outward.
|
||||
const n = r.int(3, 5);
|
||||
for (let i = 0; i < n; i++) {
|
||||
const parent = i === 0 ? null : pads[r.int(0, pads.length - 1)];
|
||||
const px = parent ? parent[0] + r.range(-0.14, 0.14) : 0;
|
||||
const py = parent ? parent[1] - 0.2 : -0.13;
|
||||
const tilt = parent ? r.range(-0.5, 0.5) : 0;
|
||||
pads.push([px, py, tilt]);
|
||||
}
|
||||
const rx = 0.1 * p.size * k;
|
||||
const ry = 0.14 * p.size * k;
|
||||
for (const [px, py, tilt] of pads) {
|
||||
const cx = bx + px * p.size * k;
|
||||
const cy = by + py * p.size * k;
|
||||
if (rx < 0.8) {
|
||||
s.set(cx, cy, lit[1]);
|
||||
continue;
|
||||
}
|
||||
const ct = Math.cos(tilt);
|
||||
const st = Math.sin(tilt);
|
||||
for (let y = Math.floor(cy - ry - 1); y <= cy + ry + 1; y++) {
|
||||
for (let x = Math.floor(cx - ry - 1); x <= cx + ry + 1; x++) {
|
||||
const dx = x + 0.5 - cx;
|
||||
const dy = y + 0.5 - cy;
|
||||
const u = (dx * ct + dy * st) / rx;
|
||||
const v = (-dx * st + dy * ct) / ry;
|
||||
const d = u * u + v * v;
|
||||
if (d > 1) continue;
|
||||
let c = lit[d > 0.65 ? (u + v < 0 ? 0 : 2) : 1];
|
||||
if (rx > 2 && hash(x, y, p.seed) > 0.93) c = spine;
|
||||
s.set(x, y, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Blooms open along the top rims once the sun has reached the plant.
|
||||
if (bloom > 0) {
|
||||
const top = pads.reduce((a, b) => (b[1] < a[1] ? b : a));
|
||||
for (let j = 0; j < 2; j++) {
|
||||
const fx = bx + (top[0] + (j ? 0.07 : -0.05)) * p.size * k;
|
||||
const fy = by + (top[1] - 0.14) * p.size * k - 0.5;
|
||||
const R = Math.max(0.6, 0.06 * p.size * k * bloom);
|
||||
for (let y = Math.floor(fy - R); y <= fy + R * 0.4; y++) {
|
||||
for (let x = Math.floor(fx - R); x <= fx + R; x++) {
|
||||
const d = Math.hypot(x + 0.5 - fx, y + 0.5 - fy);
|
||||
if (d <= R) s.set(x, y, d < R * 0.35 ? centre : bloomCol);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawPrimrose(s: Surface, f: Frame, p: Plant, bx: number, by: number, k: number, sun: number): void {
|
||||
clump(s, f, bx, by, 0.24 * p.size * k, 0.08 * p.size * k, k, p.seed, [LEAF[0], LEAF[0], LEAF[1]], sun, { lump: 0.5 });
|
||||
// White flowers open through the night and fade pink as they close at dawn.
|
||||
const close = progress(f.t, 185 + (p.seed % 30), 230 + (p.seed % 30));
|
||||
const col = mix(PRIMROSE, PRIMROSE_SHUT, close);
|
||||
// Pale petals catch the starlight, so they stay brighter than everything else at night.
|
||||
const c = sunlit(f, col, sun);
|
||||
const glow = Math.max(f.amb[0], f.amb[1], f.amb[2]) * 1.7;
|
||||
const petal: RGB = [Math.max(c[0], col[0] * glow), Math.max(c[1], col[1] * glow), Math.max(c[2], col[2] * glow)];
|
||||
const eye = sunlit(f, BLOOM_YELLOW, sun);
|
||||
const R = 0.07 * p.size * k * (1 - close * 0.5);
|
||||
const n = 3;
|
||||
for (let i = 0; i < n; i++) {
|
||||
const a = (i / n) * Math.PI * 2 + p.seed;
|
||||
const fx = Math.round(bx + Math.cos(a) * 0.15 * p.size * k);
|
||||
const fy = Math.round(by - 0.08 * p.size * k + Math.sin(a) * 0.04 * p.size * k - 1);
|
||||
if (R < 1.2) {
|
||||
s.set(fx, fy, petal);
|
||||
continue;
|
||||
}
|
||||
// Four rounded petals in a cross around a yellow eye.
|
||||
const r = Math.round(R);
|
||||
for (let dy = -r; dy <= r; dy++) {
|
||||
for (let dx = -r; dx <= r; dx++) {
|
||||
if (Math.abs(dx) + Math.abs(dy) > r + (Math.abs(dx) === Math.abs(dy) ? -1 : 0)) continue;
|
||||
s.set(fx + dx, fy + dy, dx === 0 && dy === 0 ? eye : petal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawTree(s: Surface, f: Frame, p: Plant, bx: number, by: number, k: number, sun: number, gold: boolean): void {
|
||||
const r = new Rng(p.seed);
|
||||
const h = (gold ? 8.5 : 1.7) * p.size;
|
||||
const trunk = sunlit(f, gold ? COTTON_BARK : BARK, sun);
|
||||
const sway = f.breeze * (gold ? 0.6 : 0.2);
|
||||
// Trunk and a few limbs.
|
||||
const tw = Math.max(1, (gold ? 0.32 : 0.14) * p.size * k);
|
||||
const lean = r.range(-0.3, 0.3);
|
||||
const forkY = by - h * 0.4 * k;
|
||||
for (let y = Math.floor(forkY); y < by; y++) {
|
||||
const cx = bx + lean * (by - y) * 0.3;
|
||||
for (let x = Math.floor(cx - tw / 2); x < cx + tw / 2; x++) s.set(x, y, trunk);
|
||||
}
|
||||
const limbs: [number, number][] = [];
|
||||
for (let i = 0; i < (gold ? 5 : 3); i++) {
|
||||
const a = -Math.PI / 2 + r.range(-0.9, 0.9);
|
||||
const L = h * k * r.range(0.3, 0.5);
|
||||
const x0 = bx + lean * (by - forkY) * 0.3;
|
||||
const x1 = x0 + Math.cos(a) * L + sway * 0.3 * k * 0.1;
|
||||
const y1 = forkY + Math.sin(a) * L;
|
||||
line(s, x0, forkY, x1, y1, trunk);
|
||||
limbs.push([x1, y1]);
|
||||
}
|
||||
// Foliage clumps at the ends of the limbs and around the crown.
|
||||
const tones = gold ? GOLD : JUNIPER;
|
||||
const crownSun = gold ? Math.max(sun, progress(f.t, 246, 262)) : sun;
|
||||
const cr = (gold ? 1.6 : 0.42) * p.size * k;
|
||||
for (let i = 0; i < limbs.length + (gold ? 4 : 5); i++) {
|
||||
const [lx, ly] = limbs[i % limbs.length];
|
||||
const ox = i < limbs.length ? 0 : r.range(-1, 1) * cr * 0.9;
|
||||
const oy = i < limbs.length ? 0 : r.range(-0.6, 0.4) * cr;
|
||||
const R = cr * r.range(0.7, 1.05);
|
||||
clump(s, f, lx + ox + sway * (gold ? 0.25 * k * 0.1 : 0), ly + oy + R * 0.7, R, R * 0.75, k, p.seed + i * 7, tones, crownSun, {
|
||||
lump: 0.45,
|
||||
round: true,
|
||||
shimmer: gold ? 1 : 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function drawWillow(s: Surface, f: Frame, p: Plant, bx: number, by: number, k: number, sun: number): void {
|
||||
const r = new Rng(p.seed);
|
||||
const n = 9 + (p.seed % 5);
|
||||
for (let i = 0; i < n; i++) {
|
||||
const L = 0.9 * p.size * k * r.range(0.5, 1);
|
||||
const x0 = bx + r.range(-0.25, 0.25) * p.size * k;
|
||||
const tip = x0 + r.range(-0.15, 0.15) * k + f.breeze * 0.05 * L;
|
||||
const c = sunlit(f, WILLOW[i % 3], sun);
|
||||
line(s, x0, by - 1, tip, by - L, c);
|
||||
if (L > 6) s.set(Math.round(tip), Math.round(by - L - 1), sunlit(f, WILLOW[0], sun));
|
||||
}
|
||||
}
|
||||
|
||||
function drawPlant(s: Surface, f: Frame, p: Plant): void {
|
||||
const k = F / p.z;
|
||||
const [fx, fy] = project(p.X, p.z);
|
||||
const bx = Math.round(fx);
|
||||
const by = Math.round(fy);
|
||||
const sun = floorSunAt(f, p.X, p.z);
|
||||
const sz = p.size;
|
||||
switch (p.kind) {
|
||||
case 'sage':
|
||||
return clump(s, f, bx, by, 0.5 * sz * k, 0.55 * sz * k, k, p.seed, SAGE, sun, { sway: f.breeze * 0.04 * k });
|
||||
case 'rabbitbrush': {
|
||||
clump(s, f, bx, by, 0.45 * sz * k, 0.6 * sz * k, k, p.seed, RABBIT, sun, { sway: f.breeze * 0.05 * k });
|
||||
// Late-season yellow flower heads along the top.
|
||||
const R = 0.45 * sz * k;
|
||||
if (R < 2) return;
|
||||
const c = sunlit(f, RABBIT_FLOWER, sun);
|
||||
for (let i = 0; i < R * 1.6; i++) {
|
||||
const u = hash(p.seed, i, 3) * 2 - 1;
|
||||
const v = Math.sqrt(1 - u * u) * (0.75 + 0.25 * hash(p.seed, i, 4));
|
||||
s.set(Math.round(bx + u * R + f.breeze * 0.05 * k * v), Math.round(by - v * 0.6 * sz * k), c);
|
||||
}
|
||||
return;
|
||||
}
|
||||
case 'grass':
|
||||
return drawBlades(s, f, p, bx, by, k, sun, GRASS, 7, 0.45 * sz);
|
||||
case 'yucca': {
|
||||
drawBlades(s, f, { ...p, seed: p.seed + 1 }, bx, by, k * 0.5, sun, YUCCA, 11, 1.1 * sz);
|
||||
if (p.seed % 2 === 0) line(s, bx, by - 1, bx + f.breeze * 0.02 * k, by - 1.4 * sz * k, sunlit(f, STALK, sun));
|
||||
return;
|
||||
}
|
||||
case 'pear':
|
||||
return drawPear(s, f, p, bx, by, k, sun);
|
||||
case 'primrose':
|
||||
return drawPrimrose(s, f, p, bx, by, k, sun);
|
||||
case 'juniper':
|
||||
return drawTree(s, f, p, bx, by, k, sun, false);
|
||||
case 'cottonwood':
|
||||
return drawTree(s, f, p, bx, by, k, sun, true);
|
||||
case 'willow':
|
||||
return drawWillow(s, f, p, bx, by, k, sun);
|
||||
case 'boulder':
|
||||
return clump(s, f, bx, by + Math.max(1, 0.08 * sz * k), 0.32 * sz * k, 0.24 * sz * k, k, p.seed, ROCK, sun, { lump: 0.18, round: true });
|
||||
}
|
||||
}
|
||||
|
||||
/** When the sweep of sunlight across the floor first reaches a spot (or never, if it doesn't). */
|
||||
function sunArrival(X: number, z: number): number {
|
||||
for (let t = 250; t <= 300; t += 0.5) if (sunReach(floorSunProgress(t), X, z) > 0.5) return t;
|
||||
return 999;
|
||||
}
|
||||
|
||||
/** The deer walk down the right bank and cross the stream near the camera; keep that way clear. */
|
||||
function onDeerPath(X: number, z: number): boolean {
|
||||
const off = X - streamX(z);
|
||||
if (z > 5 && z < 80 && off > -0.2 && off < 2.4) return true;
|
||||
return z > 4.5 && z < 9 && X > -6 && X < 3;
|
||||
}
|
||||
|
||||
export const LIZARD_ROCK = { X: 3.3, z: 4.6, size: 1.5 };
|
||||
|
||||
const PLANTS: Plant[] = (() => {
|
||||
const r = new Rng(303);
|
||||
const out: Plant[] = [];
|
||||
const add = (kind: Kind, X: number, z: number, size: number) =>
|
||||
out.push({ kind, X, z, size, seed: r.int(1, 1e6), sunAt: kind === 'pear' ? sunArrival(X, z) : 999 });
|
||||
|
||||
const zNear = Z_NEAR * 1.04;
|
||||
const zFar = 130;
|
||||
for (let i = 0; i < 260; i++) {
|
||||
// Even spacing on screen, not in depth: 1/z is uniform.
|
||||
const z = 1 / (1 / zNear + r.next() * (1 / zFar - 1 / zNear));
|
||||
const X = canyonX(z) + r.range(-0.93, 0.93) * halfWidth(z);
|
||||
const e = Math.abs(X - streamX(z)) - streamHalf(z);
|
||||
if (e < 0.25 || onDeerPath(X, z)) continue;
|
||||
// Leave the lizard's rock in the open.
|
||||
if (Math.abs(X - LIZARD_ROCK.X) < 1.8 && Math.abs(z - LIZARD_ROCK.z) < 1.6) continue;
|
||||
const pick = r.next();
|
||||
let kind: Kind;
|
||||
if (e < 1.3) kind = pick < 0.45 ? 'grass' : pick < 0.7 ? 'willow' : pick < 0.85 ? 'primrose' : 'boulder';
|
||||
else if (e < 5.5) {
|
||||
kind = pick < 0.24 ? 'rabbitbrush' : pick < 0.44 ? 'sage' : pick < 0.58 ? 'grass' : pick < 0.7 ? 'pear' : pick < 0.8 ? 'yucca' : pick < 0.9 ? 'primrose' : 'boulder';
|
||||
} else kind = pick < 0.42 ? 'sage' : pick < 0.66 ? 'boulder' : pick < 0.74 ? 'juniper' : pick < 0.88 ? 'rabbitbrush' : 'yucca';
|
||||
add(kind, X, z, r.range(0.65, 1.35));
|
||||
}
|
||||
|
||||
// A few placed by hand to frame the view.
|
||||
add('cottonwood', 5.6, 16, 1);
|
||||
add('cottonwood', canyonX(46) - halfWidth(46) + 2.5, 46, 0.9);
|
||||
add('juniper', canyonX(13) - halfWidth(13) + 1.6, 13, 1.1);
|
||||
add('juniper', canyonX(34) + halfWidth(34) - 2.2, 34, 1.2);
|
||||
add('rabbitbrush', -3.6, 4.6, 1.3);
|
||||
add('sage', -2.3, 4.25, 0.9);
|
||||
add('primrose', -1.6, 5.2, 1.2);
|
||||
add('primrose', -0.9, 4.4, 1);
|
||||
add('grass', -0.55, 4.3, 1.1);
|
||||
add('pear', 4.5, 5.7, 1.3);
|
||||
add('yucca', 5.3, 7.2, 1.2);
|
||||
add('grass', 2.1, 4.2, 1);
|
||||
return out;
|
||||
})();
|
||||
|
||||
export function floraThings(): Thing[] {
|
||||
const things: Thing[] = PLANTS.map((p) => ({ z: p.z, draw: (s, f) => drawPlant(s, f, p) }));
|
||||
const rock: Plant = { kind: 'boulder', ...LIZARD_ROCK, seed: 77, sunAt: 999 };
|
||||
things.push({ z: rock.z, draw: (s, f) => drawPlant(s, f, rock) });
|
||||
return things;
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
import type { Meditation, Scene } from '../types';
|
||||
import { drawSunRays, drawWalls } from './canyon';
|
||||
import { drawHawk, drawOwl, drawSwallows, lizardThing } from './creatures';
|
||||
import { deerThings } from './deer';
|
||||
import { floraThings } from './flora';
|
||||
import { drawSky } from './sky';
|
||||
import { drawFloor, drawMist, drawStream } from './stream';
|
||||
import { DURATION, frameAt, type Thing } from './timeline';
|
||||
|
||||
const FLORA = [...floraThings(), lizardThing()];
|
||||
|
||||
const scene: Scene = {
|
||||
render(s, t) {
|
||||
const f = frameAt(t);
|
||||
drawSky(s, f);
|
||||
drawWalls(s, f);
|
||||
drawOwl(s, f);
|
||||
drawHawk(s, f);
|
||||
drawSwallows(s, f);
|
||||
drawFloor(s, f);
|
||||
drawStream(s, f);
|
||||
drawMist(s, f);
|
||||
const things: Thing[] = [...FLORA, ...deerThings(f)];
|
||||
things.sort((a, b) => b.z - a.z);
|
||||
for (const th of things) th.draw(s, f);
|
||||
drawSunRays(s, f);
|
||||
s.darken(f.fade);
|
||||
},
|
||||
};
|
||||
|
||||
export const desertSunrise: Meditation = {
|
||||
id: 'desert-sunrise',
|
||||
title: 'Desert Sunrise',
|
||||
subtitle: 'Dawn breaks over a canyon stream',
|
||||
description:
|
||||
'Wait out the last of a starry night deep in a desert canyon. Shooting stars cross the Milky Way, an owl settles into a sandstone arch and deer come down to drink, until the sun breaks through the arch and fills the canyon with a bright new day.',
|
||||
duration: DURATION,
|
||||
previewTime: 268,
|
||||
music: 'audio/desert-sunrise.mp3',
|
||||
createScene: () => scene,
|
||||
};
|
||||
|
|
@ -0,0 +1,305 @@
|
|||
import { bayer, ditherStep, hex, mix, type RGB } from '../../engine/color';
|
||||
import { ease, progress, track } from '../../engine/Timeline';
|
||||
import { hash, noise1, noise2, Rng } from '../../engine/rng';
|
||||
import type { Surface } from '../../engine/Surface';
|
||||
import { lit, SKY_BANDS, SUN_R, TIMES, type Frame } from './timeline';
|
||||
import { HORIZON, MESA, ROCK, W } from './world';
|
||||
|
||||
/** Rows the sky gradient spans; below this only the far mesas and canyon show. */
|
||||
const SKY_H = HORIZON - 2;
|
||||
|
||||
/** One past the lowest sky row left uncovered by rock in each column. */
|
||||
const SKY_END: Int16Array = Int16Array.from({ length: W }, (_, x) => {
|
||||
let y = SKY_H;
|
||||
while (y > 0 && ROCK[(y - 1) * W + x]) y--;
|
||||
return y;
|
||||
});
|
||||
|
||||
/** Which gradient band each sky pixel uses, with dithered seams; fixed by the layout. */
|
||||
const BAND_MAP: Uint8Array = (() => {
|
||||
const map = new Uint8Array(W * SKY_H);
|
||||
for (let y = 0; y < SKY_H; y++) {
|
||||
for (let x = 0; x < W; x++) {
|
||||
const v = (y / (SKY_H - 1)) * (SKY_BANDS - 1) + (bayer(x, y) - 0.5) * 0.95;
|
||||
map[y * W + x] = Math.max(0, Math.min(SKY_BANDS - 1, Math.round(v)));
|
||||
}
|
||||
}
|
||||
return map;
|
||||
})();
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Stars and the Milky Way
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** The Milky Way runs diagonally across the canyon's slot of sky, from upper right to the far rim. */
|
||||
const MW_A = [250, -10] as const;
|
||||
const MW_B = [120, 100] as const;
|
||||
const MW_LEN = Math.hypot(MW_B[0] - MW_A[0], MW_B[1] - MW_A[1]);
|
||||
|
||||
/** Signed distance across the Milky Way and how far along it (0–1) a pixel lies. */
|
||||
function milkyCoords(x: number, y: number): [number, number] {
|
||||
const ux = (MW_B[0] - MW_A[0]) / MW_LEN;
|
||||
const uy = (MW_B[1] - MW_A[1]) / MW_LEN;
|
||||
const px = x - MW_A[0];
|
||||
const py = y - MW_A[1];
|
||||
return [px * -uy + py * ux, (px * ux + py * uy) / MW_LEN];
|
||||
}
|
||||
|
||||
/** Milky Way brightness per pixel: a soft band with clumps and a dark dust lane; fixed. */
|
||||
const MILKY: Float32Array = (() => {
|
||||
const m = new Float32Array(W * SKY_H);
|
||||
for (let y = 0; y < SKY_H; y++) {
|
||||
for (let x = 0; x < W; x++) {
|
||||
const [d, u] = milkyCoords(x, y);
|
||||
const width = 14 + 6 * u;
|
||||
const band = Math.exp(-((d / width) ** 2));
|
||||
if (band < 0.02) continue;
|
||||
const clumps = 0.45 + 0.75 * noise2(x / 7, y / 7, 81) * noise2(x / 3, y / 3, 82);
|
||||
const lane = Math.exp(-(((d - 3 + 4 * Math.sin(u * 7)) / 3.2) ** 2)) * (0.5 + 0.5 * noise2(x / 5, y / 5, 83));
|
||||
m[y * W + x] = Math.max(0, band * clumps * (1 - 0.8 * lane));
|
||||
}
|
||||
}
|
||||
return m;
|
||||
})();
|
||||
|
||||
interface Star {
|
||||
x: number;
|
||||
y: number;
|
||||
mag: number;
|
||||
/** Time the star has faded into the dawn by; the dimmest go first. */
|
||||
out: number;
|
||||
twinkle: number;
|
||||
phase: number;
|
||||
tint: RGB;
|
||||
}
|
||||
|
||||
const STARS: Star[] = (() => {
|
||||
const r = new Rng(17);
|
||||
const out: Star[] = [];
|
||||
for (let i = 0; i < 420; i++) {
|
||||
const x = r.int(100, 270);
|
||||
const y = r.int(0, HORIZON - 8);
|
||||
// Crowd extra stars into the Milky Way.
|
||||
const inBand = Math.exp(-((milkyCoords(x, y)[0] / 16) ** 2));
|
||||
if (r.next() > 0.35 + 0.65 * inBand) continue;
|
||||
const mag = Math.pow(r.next(), 2.6);
|
||||
out.push({
|
||||
x,
|
||||
y,
|
||||
mag,
|
||||
out: 70 + mag * 150 + r.range(0, 20),
|
||||
twinkle: r.range(0.5, 2.2),
|
||||
phase: r.range(0, Math.PI * 2),
|
||||
tint: r.next() < 0.18 ? hex('#cad8ff') : r.next() < 0.25 ? hex('#ffe4c0') : hex('#ffffff'),
|
||||
});
|
||||
}
|
||||
return out;
|
||||
})();
|
||||
|
||||
const MILKY_COLOR = hex('#c8d0f0');
|
||||
const MILKY_CORE = hex('#f0dcc0');
|
||||
|
||||
function drawMilkyWay(s: Surface, f: Frame): void {
|
||||
if (f.milky <= 0) return;
|
||||
for (let x = 0; x < W; x++) {
|
||||
for (let y = 0; y < SKY_END[x]; y++) {
|
||||
const m = MILKY[y * W + x];
|
||||
if (m <= 0) continue;
|
||||
const c = mix(MILKY_COLOR, MILKY_CORE, Math.min(1, y / SKY_H + 0.2));
|
||||
s.light(x, y, c, m * 0.15 * f.milky);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawStars(s: Surface, f: Frame): void {
|
||||
for (const st of STARS) {
|
||||
const vis = 1 - progress(f.t, st.out - 25, st.out);
|
||||
if (vis <= 0) continue;
|
||||
const tw = 0.7 + 0.3 * Math.sin(f.t * st.twinkle + st.phase);
|
||||
const a = vis * tw * (0.3 + 0.7 * st.mag);
|
||||
s.blend(st.x, st.y, st.tint, a);
|
||||
if (st.mag > 0.8) {
|
||||
const arm = a * 0.3;
|
||||
s.blend(st.x - 1, st.y, st.tint, arm);
|
||||
s.blend(st.x + 1, st.y, st.tint, arm);
|
||||
s.blend(st.x, st.y - 1, st.tint, arm);
|
||||
s.blend(st.x, st.y + 1, st.tint, arm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Shooting stars
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
interface Meteor {
|
||||
t0: number;
|
||||
x0: number;
|
||||
y0: number;
|
||||
dx: number;
|
||||
dy: number;
|
||||
dur: number;
|
||||
}
|
||||
|
||||
const METEORS: Meteor[] = [
|
||||
{ t0: TIMES.meteors[0], x0: 168, y0: 6, dx: -46, dy: 26, dur: 1.4 },
|
||||
{ t0: TIMES.meteors[1], x0: 205, y0: 14, dx: 40, dy: 22, dur: 1.2 },
|
||||
{ t0: TIMES.meteors[2], x0: 150, y0: 22, dx: -30, dy: 30, dur: 1.5 },
|
||||
];
|
||||
|
||||
const METEOR = hex('#f4f8ff');
|
||||
|
||||
function drawMeteors(s: Surface, f: Frame): void {
|
||||
for (const m of METEORS) {
|
||||
const u = (f.t - m.t0) / m.dur;
|
||||
if (u < 0 || u > 1.25) continue;
|
||||
const head = Math.min(1, u);
|
||||
const fadeOut = u > 1 ? 1 - (u - 1) / 0.25 : 1;
|
||||
const len = 0.32;
|
||||
const n = Math.ceil(Math.hypot(m.dx, m.dy) * len * 1.5);
|
||||
for (let i = 0; i <= n; i++) {
|
||||
const k = i / n;
|
||||
const p = head - k * len;
|
||||
if (p < 0) break;
|
||||
const a = (1 - k) * Math.sin(Math.PI * Math.min(1, head * 1.1)) * fadeOut;
|
||||
s.blend(Math.round(m.x0 + m.dx * p), Math.round(m.y0 + m.dy * p), METEOR, a * 0.9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Venus and the old crescent moon, rising ahead of the sun
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const venusX = track([[TIMES.venusRise, 172], [250, 196]], ease.linear);
|
||||
const venusY = track([[TIMES.venusRise, 92], [250, 30]], ease.linear);
|
||||
const moonX = track([[TIMES.moonRise, 150], [240, 174]], ease.linear);
|
||||
const moonY = track([[TIMES.moonRise, 96], [240, 46]], ease.linear);
|
||||
const VENUS = hex('#fffbe8');
|
||||
const MOON = hex('#f4eedc');
|
||||
const MOON_R = 4;
|
||||
|
||||
function drawVenus(s: Surface, f: Frame): void {
|
||||
const vis = 1 - progress(f.t, 218, 252);
|
||||
if (vis <= 0 || f.t < TIMES.venusRise) return;
|
||||
const x = Math.round(venusX(f.t));
|
||||
const y = Math.round(venusY(f.t));
|
||||
const tw = 0.9 + 0.1 * Math.sin(f.t * 1.9);
|
||||
for (let dy = -4; dy <= 4; dy++) {
|
||||
for (let dx = -4; dx <= 4; dx++) {
|
||||
const d = Math.hypot(dx, dy);
|
||||
if (d > 4) continue;
|
||||
s.light(x + dx, y + dy, VENUS, ditherStep(vis * 0.22 * (1 - d / 4) ** 2, x + dx, y + dy, 4));
|
||||
}
|
||||
}
|
||||
s.set(x, y, mix(s.get(x, y), VENUS, vis * tw));
|
||||
const arm = vis * tw * 0.55;
|
||||
s.blend(x - 1, y, VENUS, arm);
|
||||
s.blend(x + 1, y, VENUS, arm);
|
||||
s.blend(x, y - 1, VENUS, arm);
|
||||
s.blend(x, y + 1, VENUS, arm);
|
||||
}
|
||||
|
||||
function drawMoon(s: Surface, f: Frame): void {
|
||||
const vis = 1 - progress(f.t, 196, 236);
|
||||
if (vis <= 0 || f.t < TIMES.moonRise) return;
|
||||
const mx = Math.round(moonX(f.t));
|
||||
const my = Math.round(moonY(f.t));
|
||||
// The lit edge faces the sun, still below the rim to the lower left.
|
||||
const ang = Math.atan2(f.sunY - my, f.sunX - mx);
|
||||
const ox = -Math.cos(ang) * 2.6;
|
||||
const oy = -Math.sin(ang) * 2.6;
|
||||
for (let y = -MOON_R - 8; y <= MOON_R + 8; y++) {
|
||||
for (let x = -MOON_R - 8; x <= MOON_R + 8; x++) {
|
||||
const d = Math.hypot(x, y);
|
||||
if (d > MOON_R + 0.4) {
|
||||
if (d < 12) s.light(mx + x, my + y, MOON, ditherStep(vis * 0.06 * (1 - d / 12), mx + x, my + y, 3));
|
||||
continue;
|
||||
}
|
||||
const inShadow = Math.hypot(x - ox, y - oy) < MOON_R + 0.3;
|
||||
// Faint earthshine on the dark part of the disc.
|
||||
s.blend(mx + x, my + y, MOON, vis * (inShadow ? 0.1 : 0.95));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dawn glow and the sun
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const GLOW_WARM = hex('#ffb070');
|
||||
const GLOW_ROSE = hex('#ff8a8a');
|
||||
const HALO = hex('#fff2cc');
|
||||
|
||||
function drawDawnGlow(s: Surface, f: Frame): void {
|
||||
if (f.glow <= 0) return;
|
||||
const cx = f.sunX;
|
||||
const cy = Math.max(f.sunY, HORIZON - 12);
|
||||
const col = mix(GLOW_ROSE, GLOW_WARM, progress(f.t, 150, 250));
|
||||
const R = 110;
|
||||
for (let x = Math.max(0, Math.floor(cx - R)); x < Math.min(W, cx + R); x++) {
|
||||
for (let y = 0; y < SKY_END[x]; y++) {
|
||||
const d = Math.hypot((x - cx) * 0.8, (y - cy) * 1.5);
|
||||
if (d > R) continue;
|
||||
const q = 1 - d / R;
|
||||
s.blend(x, y, col, ditherStep(f.glow * 0.42 * q * q * Math.sqrt(q), x, y, 16));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawSun(s: Surface, f: Frame): void {
|
||||
if (f.sunY > HORIZON + SUN_R) return;
|
||||
const cx = Math.round(f.sunX);
|
||||
const cy = Math.round(f.sunY);
|
||||
const glowR = 34;
|
||||
const halo = mix(f.sunColor, HALO, 0.35);
|
||||
for (let y = Math.max(0, cy - glowR); y < Math.min(SKY_H, cy + glowR); y++) {
|
||||
for (let x = cx - glowR; x <= cx + glowR; x++) {
|
||||
const d = Math.hypot(x - cx, y - cy);
|
||||
if (d > glowR) continue;
|
||||
s.blend(x, y, halo, ditherStep(Math.pow(1 - d / glowR, 2) * 0.55, x, y, 12));
|
||||
}
|
||||
}
|
||||
const core = mix(f.sunColor, [255, 255, 255], 0.72);
|
||||
for (let y = -SUN_R; y <= SUN_R; y++) {
|
||||
for (let x = -SUN_R; x <= SUN_R; x++) {
|
||||
const d2 = x * x + y * y;
|
||||
if (d2 > SUN_R * SUN_R + SUN_R * 0.8) continue;
|
||||
s.set(cx + x, cy + y, d2 > (SUN_R - 1.5) ** 2 ? mix(core, f.sunColor, 0.5) : core);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Far mesas
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const MESA_ROCK = hex('#b8705a');
|
||||
const MESA_SHADE = hex('#8a5a5e');
|
||||
|
||||
function drawMesas(s: Surface, f: Frame): void {
|
||||
// Backlit by the dawn, so mostly a hazy silhouette with a warm rim once the sun is up.
|
||||
const body = mix(lit(f, MESA_SHADE), f.horizonColor, 0.55);
|
||||
const rim = mix(lit(f, MESA_ROCK), f.sunColor, 0.25 * progress(f.t, 236, 262));
|
||||
for (let x = 0; x < W; x++) {
|
||||
const top = MESA[x];
|
||||
for (let y = top; y <= HORIZON; y++) {
|
||||
const ledge = noise1(x / 9, 91 + (y - top)) > 0.7 && (y - top) % 4 === 2;
|
||||
s.set(x, y, y === top || (ledge && hash(x, y, 92) > 0.3) ? mix(body, rim, 0.6) : body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function drawSky(s: Surface, f: Frame): void {
|
||||
for (let x = 0; x < W; x++) {
|
||||
for (let y = 0; y < SKY_END[x]; y++) s.set(x, y, f.skyBands[BAND_MAP[y * W + x]]);
|
||||
}
|
||||
drawMilkyWay(s, f);
|
||||
drawStars(s, f);
|
||||
drawMeteors(s, f);
|
||||
drawDawnGlow(s, f);
|
||||
drawMoon(s, f);
|
||||
drawVenus(s, f);
|
||||
drawSun(s, f);
|
||||
drawMesas(s, f);
|
||||
}
|
||||
|
|
@ -0,0 +1,207 @@
|
|||
import { ditherStep, hex, mix, scale, type RGB } from '../../engine/color';
|
||||
import { clamp01, progress } from '../../engine/Timeline';
|
||||
import { hash, noise1, noise2 } from '../../engine/rng';
|
||||
import type { Surface } from '../../engine/Surface';
|
||||
import type { Frame } from './timeline';
|
||||
import { canyonX, F, FOOT, H, halfWidth, HORIZON, floorZ, SIDE, streamHalf, streamX, VPX, W } from './world';
|
||||
|
||||
const SAND = hex('#d4a47c');
|
||||
const SAND_DARK = hex('#c08e68');
|
||||
const SAND_PALE = hex('#e0b48e');
|
||||
const DAMP = hex('#a07a5e');
|
||||
const PEBBLE = hex('#9a7460');
|
||||
const CRUST = hex('#6e5a4c');
|
||||
const GRASS = hex('#7c8650');
|
||||
const WATER_TINT = hex('#2e4a60');
|
||||
const FOAM = hex('#e8f0f4');
|
||||
const MIST = hex('#c8d4e8');
|
||||
const MIST_GOLD = hex('#ffd6a0');
|
||||
|
||||
/**
|
||||
* How much sun reaches a spot on the floor (0–1) once the sweep has got `reach` (0–1) of the way
|
||||
* across. The shadow of the left wall pulls back across the canyon from the right bank.
|
||||
*/
|
||||
export function sunReach(reach: number, X: number, z: number): number {
|
||||
if (reach <= 0) return 0;
|
||||
return progress(X, sunEdge(reach, z) - 0.4, sunEdge(reach, z) + 0.4);
|
||||
}
|
||||
|
||||
/** The X beyond which the floor is sunlit, at depth z. */
|
||||
function sunEdge(reach: number, z: number): number {
|
||||
return canyonX(z) + halfWidth(z) * (1 - reach * 2.3) + (noise1(z / 4, 51) - 0.5) * 2.5;
|
||||
}
|
||||
|
||||
export function floorSunAt(f: Frame, X: number, z: number): number {
|
||||
return sunReach(f.floorSun, X, z);
|
||||
}
|
||||
|
||||
/** Per floor pixel: world position, and what is there. Kind 0 = none, 1 = ground, 2 = water. */
|
||||
const KIND = new Uint8Array(W * H);
|
||||
const FX = new Float32Array(W * H);
|
||||
const FZ = new Float32Array(W * H);
|
||||
/** Distance (m) from the water's edge, negative inside the stream. */
|
||||
const EDGE = new Float32Array(W * H);
|
||||
const GROUND = new Uint8ClampedArray(W * H * 3);
|
||||
|
||||
for (let y = HORIZON + 1; y < H; y++) {
|
||||
const z = floorZ(y);
|
||||
for (let x = 0; x < W; x++) {
|
||||
if (SIDE[x] !== 0 && y < FOOT[x]) continue;
|
||||
const i = y * W + x;
|
||||
const X = ((x + 0.5 - VPX) * z) / F;
|
||||
FX[i] = X;
|
||||
FZ[i] = z;
|
||||
// Far rows are thinner than a pixel, so the stream gets a minimum width there.
|
||||
const half = Math.max(streamHalf(z), (0.55 * z) / F);
|
||||
const e = Math.abs(X - streamX(z)) - half;
|
||||
EDGE[i] = e;
|
||||
KIND[i] = e < 0 ? 2 : 1;
|
||||
if (e < 0) continue;
|
||||
const n = noise2(X * 0.9, z * 0.6, 52);
|
||||
let c: RGB = n > 0.62 ? SAND_PALE : n < 0.32 ? SAND_DARK : SAND;
|
||||
// Scattered pebbles, each a little disc somewhere in its own cell.
|
||||
const cx = Math.floor(X * 4);
|
||||
const cz = Math.floor(z * 4);
|
||||
if (hash(cx, cz, 53) > 0.8) {
|
||||
const px = (cx + 0.2 + hash(cx, cz, 57) * 0.6) / 4;
|
||||
const pz = (cz + 0.2 + hash(cx, cz, 58) * 0.6) / 4;
|
||||
if (Math.hypot(X - px, (z - pz) * 0.8) < 0.035 + hash(cx, cz, 59) * 0.04) c = PEBBLE;
|
||||
}
|
||||
else if (noise2(X * 0.5, z * 0.5, 54) > 0.72 && e > 1.5) c = mix(c, CRUST, 0.55);
|
||||
// Damp sand and a fringe of grass along the banks.
|
||||
if (e < 0.35) c = mix(c, DAMP, 1 - e / 0.35);
|
||||
else if (e < 1.1 && noise2(X * 3, z * 3, 55) > 0.5 && hash(x, y, 56) > 0.35) c = mix(c, GRASS, 0.7);
|
||||
// Ordered dither between neighbouring tones keeps the far floor from shimmering.
|
||||
GROUND.set(c, i * 3);
|
||||
}
|
||||
}
|
||||
|
||||
export function drawFloor(s: Surface, f: Frame): void {
|
||||
const amb = f.amb;
|
||||
const sun = f.sunColor;
|
||||
const day = f.daylight * 0.42;
|
||||
const sunny = day > 0 && f.floorSun > 0;
|
||||
const d = s.data;
|
||||
for (let y = HORIZON + 1; y < H; y++) {
|
||||
const edge = sunny ? sunEdge(f.floorSun, floorZ(y)) : 0;
|
||||
for (let x = 0; x < W; x++) {
|
||||
const i = y * W + x;
|
||||
if (KIND[i] !== 1) continue;
|
||||
const b = i * 3;
|
||||
let r = GROUND[b] * amb[0];
|
||||
let g = GROUND[b + 1] * amb[1];
|
||||
let bl = GROUND[b + 2] * amb[2];
|
||||
if (sunny) {
|
||||
const u = (FX[i] - edge + 0.4) / 0.8;
|
||||
const k = (u <= 0 ? 0 : u >= 1 ? 1 : ditherStep(u, x, y, 3)) * day;
|
||||
r += GROUND[b] * (sun[0] / 255) * k;
|
||||
g += GROUND[b + 1] * (sun[1] / 255) * k;
|
||||
bl += GROUND[b + 2] * (sun[2] / 255) * k;
|
||||
}
|
||||
const o = i * 4;
|
||||
d[o] = r;
|
||||
d[o + 1] = g;
|
||||
d[o + 2] = bl;
|
||||
d[o + 3] = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The stream mirrors whatever is above it (walls, dawn sky, stars), broken up by ripples flowing
|
||||
* toward the viewer. Must run after the sky and walls have been drawn.
|
||||
*/
|
||||
export function drawStream(s: Surface, f: Frame): void {
|
||||
const t = f.t;
|
||||
const tint = mix(WATER_TINT, f.horizonColor, 0.25);
|
||||
const sparkle = mix(f.sunColor, [255, 255, 255], 0.5);
|
||||
for (let y = HORIZON + 1; y < H; y++) {
|
||||
const depth = (y - HORIZON) / (H - HORIZON);
|
||||
for (let x = 0; x < W; x++) {
|
||||
const i = y * W + x;
|
||||
if (KIND[i] !== 2) continue;
|
||||
const X = FX[i];
|
||||
const z = FZ[i];
|
||||
// Ripples drift downstream (toward the camera), so their pattern scrolls with z + t.
|
||||
const flow = noise2(X * 2.2, (z + t * 0.9) * 1.6, 61);
|
||||
const wob = Math.round((flow - 0.5) * (1 + depth * 3));
|
||||
const src = s.get(x + wob, 2 * HORIZON - y + Math.round((flow - 0.5) * 2));
|
||||
let c = mix(src, tint, 0.3 + 0.15 * depth);
|
||||
if (flow > 0.7) c = mix(c, [255, 255, 255], 0.08 + 0.1 * f.daylight);
|
||||
else if (flow < 0.25) c = mix(c, tint, 0.3);
|
||||
// A pale lip of moving water along the banks.
|
||||
if (EDGE[i] > -0.06 * (1 + depth)) c = mix(c, FOAM, 0.18 + 0.25 * progress(f.t, 150, 270));
|
||||
s.set(x, y, c);
|
||||
// Glitter where the morning sun catches the ripples.
|
||||
if (f.daylight > 0) {
|
||||
const g = noise2(X * 7, (z + t * 1.1) * 5, 62);
|
||||
const sunHere = Math.max(floorSunAt(f, X, z), 0.35);
|
||||
if (g > 0.8 && hash(x, y, Math.floor(t * 5)) > 0.4) s.blend(x, y, sparkle, (g - 0.8) * 4 * f.daylight * sunHere);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Mist density per floor pixel, refilled each frame while there is mist, and its extent per row. */
|
||||
const MIST_D = new Float32Array(W * (H + 6));
|
||||
const MIST_L = new Int16Array(H + 6);
|
||||
const MIST_R = new Int16Array(H + 6);
|
||||
|
||||
/** Mist lying over the stream before dawn, glowing gold when the sun reaches it, then lifting. */
|
||||
export function drawMist(s: Surface, f: Frame): void {
|
||||
if (f.mist <= 0) return;
|
||||
const t = f.t;
|
||||
const lift = progress(t, 248, 278);
|
||||
MIST_D.fill(0);
|
||||
MIST_L.fill(W);
|
||||
MIST_R.fill(-1);
|
||||
const floor = 0.18 + lift * 0.45;
|
||||
for (let y = HORIZON + 1; y < H; y++) {
|
||||
const z = floorZ(y);
|
||||
if (z > 90) continue;
|
||||
const thick = f.mist * Math.min(1, z / 5);
|
||||
// Thickest over the water, thinning out across the banks.
|
||||
const spread = 1.3 + 1.1 * noise1(z / 6 + t * 0.02, 63);
|
||||
// Mist is soft, so its texture is sampled at half the horizontal resolution.
|
||||
let wisp = -1;
|
||||
for (let x = 0; x < W; x++) {
|
||||
const i = y * W + x;
|
||||
const e = EDGE[i];
|
||||
if (KIND[i] === 0 || e >= spread) {
|
||||
wisp = -1;
|
||||
continue;
|
||||
}
|
||||
if (!(x & 1) || wisp < 0) wisp = clamp01((noise2(FX[i] * 0.7 + t * 0.06, z * 0.3 - t * 0.05, 64) - floor) / (0.7 - floor));
|
||||
MIST_D[i] = wisp * thick * (e < 0 ? 1 : 1 - e / spread);
|
||||
if (x < MIST_L[y]) MIST_L[y] = x;
|
||||
MIST_R[y] = x;
|
||||
}
|
||||
}
|
||||
const gold = f.daylight;
|
||||
const base = mix(MIST, f.horizonColor, 0.3);
|
||||
const glow = Math.min(1, Math.max(f.amb[0], f.amb[1], f.amb[2]) + 0.3);
|
||||
// Mist colors from shaded to fully sunlit, worked out once per frame.
|
||||
const tones = Array.from({ length: 17 }, (_, j) => scale(mix(base, MIST_GOLD, gold * (0.45 + (0.55 * j) / 16)), glow));
|
||||
const sunny = gold > 0 && f.floorSun > 0;
|
||||
const d = s.data;
|
||||
for (let y = HORIZON + 1; y < H; y++) {
|
||||
const z = floorZ(y);
|
||||
// The bank rises off the ground a little, so it is drawn smeared upward from where it lies.
|
||||
const r1 = Math.max(1, Math.round(6 / z));
|
||||
const y1 = Math.min(H + 5, y + r1);
|
||||
const y2 = Math.min(H + 5, y + 2 * r1);
|
||||
const x0 = Math.min(MIST_L[y], MIST_L[y1], MIST_L[y2]);
|
||||
const x1 = Math.max(MIST_R[y], MIST_R[y1], MIST_R[y2]);
|
||||
const edge = sunny ? sunEdge(f.floorSun, z) : 0;
|
||||
for (let x = x0; x <= x1; x++) {
|
||||
const i = y * W + x;
|
||||
const a = Math.max(MIST_D[i], MIST_D[y1 * W + x] * 0.7, MIST_D[y2 * W + x] * 0.4) * 0.62;
|
||||
if (a <= 0.02) continue;
|
||||
const c = tones[sunny ? Math.round(clamp01((FX[i] - edge + 0.4) / 0.8) * 16) : 0];
|
||||
const o = i * 4;
|
||||
d[o] += (c[0] - d[o]) * a;
|
||||
d[o + 1] += (c[1] - d[o + 1]) * a;
|
||||
d[o + 2] += (c[2] - d[o + 2]) * a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,178 @@
|
|||
import { gradient, hex, mul, scale, type RGB } from '../../engine/color';
|
||||
import type { Surface } from '../../engine/Surface';
|
||||
import { colorTrack, ease, progress, track } from '../../engine/Timeline';
|
||||
import { archOpening, ARCH, ROCK, W } from './world';
|
||||
|
||||
/** All timings for the Desert Sunrise meditation, in seconds. */
|
||||
export const DURATION = 300;
|
||||
|
||||
export const TIMES = {
|
||||
fadeIn: [0, 6],
|
||||
fadeOut: [291, 300],
|
||||
meteors: [18, 52, 84],
|
||||
venusRise: 22,
|
||||
moonRise: 44,
|
||||
/** The owl glides in from the far end and settles in the arch. */
|
||||
owl: [96, 124],
|
||||
/** The deer appear far up the canyon (see deer.ts for their walk). */
|
||||
deer: 128,
|
||||
/** The sun first shows through the arch. */
|
||||
sunrise: 240,
|
||||
hawk: 250,
|
||||
swallows: 256,
|
||||
lizard: 268,
|
||||
} as const;
|
||||
|
||||
/** Light phases: starry night → first indigo → blue hour → sunrise → bright morning. */
|
||||
const P = [0, 60, 150, 240, 300] as const;
|
||||
const phase = (...c: [string, string, string, string, string]) => colorTrack(P.map((t, i) => [t, hex(c[i])] as const));
|
||||
|
||||
/** Sky gradient stops from zenith down to the far rim. */
|
||||
const SKY = [
|
||||
phase('#04060e', '#060a1a', '#18234e', '#3d74c0', '#3f86d6'),
|
||||
phase('#060a16', '#0a1028', '#2c3670', '#6d9fd8', '#68a8e6'),
|
||||
phase('#08101f', '#0f1a3a', '#5e4e8a', '#b4c4dc', '#9ccbef'),
|
||||
phase('#0a1428', '#18264a', '#b46e86', '#f4c8a0', '#cfe6f2'),
|
||||
phase('#0e1a32', '#24345e', '#f29a6e', '#ffd88e', '#fff1cf'),
|
||||
];
|
||||
|
||||
/** Light falling on the canyon, as a per-channel multiplier: starlight first, then dawn. */
|
||||
const AMBIENT = phase('#262e50', '#2c3660', '#6a6694', '#b8a6b0', '#e6d4c4');
|
||||
/** Vertical walls catch much less of the night sky than the open floor, so they read as silhouettes. */
|
||||
const wallK = track([[0, 0.42], [150, 0.66], [240, 0.88], [300, 0.95]]);
|
||||
|
||||
const sunX = track([[150, 138], [240, 144], [256, 152], [300, 180]], ease.linear);
|
||||
const sunY = track([[150, 118], [240, 84], [256, 70], [300, 28]], ease.linear);
|
||||
const sunColor = colorTrack([
|
||||
[232, hex('#ff8a44')],
|
||||
[258, hex('#ffb86a')],
|
||||
[300, hex('#ffecbc')],
|
||||
]);
|
||||
const glow = track([[50, 0], [150, 0.45], [240, 1], [300, 0.55]]);
|
||||
const milky = track([[0, 1], [60, 0.85], [125, 0]]);
|
||||
const alpenglow = track([[192, 0], [228, 1], [252, 0.7], [280, 0]]);
|
||||
const shadowR = track([[240, 0], [290, 1]]);
|
||||
const shadowL = track([[256, 0], [300, 0.7]]);
|
||||
const floorSun = track([[262, 0], [298, 1]]);
|
||||
const mist = track([[140, 0], [178, 1], [246, 1], [278, 0]]);
|
||||
|
||||
/** When the sunlight first sweeps across a spot is easier to plan with than the light itself. */
|
||||
export const floorSunProgress = floorSun;
|
||||
|
||||
export const SUN_R = 7;
|
||||
export const SKY_BANDS = 24;
|
||||
|
||||
export interface Frame {
|
||||
t: number;
|
||||
/** Ambient light multiplier on the floor and plants, 0–1 per channel. */
|
||||
amb: RGB;
|
||||
/** Ambient light on the walls. */
|
||||
wallAmb: RGB;
|
||||
skyBands: RGB[];
|
||||
horizonColor: RGB;
|
||||
sunX: number;
|
||||
sunY: number;
|
||||
sunColor: RGB;
|
||||
/** Fraction of the sun's disc not hidden by rock. */
|
||||
sunVis: number;
|
||||
/** Strength of direct sunlight once the sun is up, 0–1. */
|
||||
daylight: number;
|
||||
/** Strength of the shaft of light pouring through the arch, 0–1. */
|
||||
beam: number;
|
||||
/** Warm dawn glow gathered around where the sun will rise. */
|
||||
glow: number;
|
||||
milky: number;
|
||||
alpenglow: number;
|
||||
/** How far the sunlight has crept down each wall, 0 (only the rim) to 1 (the foot). */
|
||||
shadowL: number;
|
||||
shadowR: number;
|
||||
/** How much of the floor the sun has reached, 0–1, sweeping from the right bank. */
|
||||
floorSun: number;
|
||||
mist: number;
|
||||
/** A light breeze that grasses and leaves sway with, -1..1. */
|
||||
breeze: number;
|
||||
/** Starlight washes the color out of things; this fades as the dawn comes up. */
|
||||
desat: number;
|
||||
fade: number;
|
||||
}
|
||||
|
||||
/** Sample points over the sun's disc for working out how much of it the rock hides. */
|
||||
const DISC: [number, number][] = [];
|
||||
for (let y = -SUN_R; y <= SUN_R; y += 2) {
|
||||
for (let x = -SUN_R; x <= SUN_R; x += 2) if (x * x + y * y <= SUN_R * SUN_R) DISC.push([x, y]);
|
||||
}
|
||||
|
||||
function sunVisibility(sx: number, sy: number): { vis: number; inArch: number } {
|
||||
let vis = 0;
|
||||
let inArch = 0;
|
||||
for (const [dx, dy] of DISC) {
|
||||
const x = Math.round(sx + dx);
|
||||
const y = Math.round(sy + dy);
|
||||
if (y < 0 || x < 0 || x >= W) {
|
||||
vis++;
|
||||
continue;
|
||||
}
|
||||
if (ROCK[y * W + x]) continue;
|
||||
vis++;
|
||||
if (archOpening(x, y)) inArch++;
|
||||
}
|
||||
return { vis: vis / DISC.length, inArch: inArch / DISC.length };
|
||||
}
|
||||
|
||||
export function frameAt(t: number): Frame {
|
||||
const sky = SKY.map((c) => c(t));
|
||||
const sx = sunX(t);
|
||||
const sy = sunY(t);
|
||||
const { vis, inArch } = sunVisibility(sx, sy);
|
||||
const amb = scale(AMBIENT(t), 1 / 255);
|
||||
// Once the sun has fully cleared the arch the shaft dissolves into ordinary daylight.
|
||||
const clear = progress(sy, ARCH.base - ARCH.outerRy - 2, ARCH.base - ARCH.outerRy - 16, ease.linear);
|
||||
return {
|
||||
t,
|
||||
amb,
|
||||
wallAmb: scale(amb, wallK(t)),
|
||||
skyBands: Array.from({ length: SKY_BANDS }, (_, i) => gradient(sky, i / (SKY_BANDS - 1))),
|
||||
horizonColor: sky[4],
|
||||
sunX: sx,
|
||||
sunY: sy,
|
||||
sunColor: sunColor(t),
|
||||
sunVis: vis,
|
||||
daylight: progress(t, 238, 258),
|
||||
beam: Math.min(1, inArch * 2.2) * (1 - clear),
|
||||
glow: glow(t),
|
||||
milky: milky(t),
|
||||
alpenglow: alpenglow(t),
|
||||
shadowL: shadowL(t),
|
||||
shadowR: shadowR(t),
|
||||
floorSun: floorSun(t),
|
||||
mist: mist(t),
|
||||
breeze: 0.6 * Math.sin(t * 0.37) + 0.4 * Math.sin(t * 0.91 + 1.7),
|
||||
desat: 0.75 * (1 - progress(t, 90, 225)),
|
||||
fade: progress(t, TIMES.fadeIn[0], TIMES.fadeIn[1]) * (1 - progress(t, TIMES.fadeOut[0], TIMES.fadeOut[1])),
|
||||
};
|
||||
}
|
||||
|
||||
/** Anything standing on the canyon floor; these are drawn far to near so nearer ones overlap. */
|
||||
export interface Thing {
|
||||
z: number;
|
||||
draw(s: Surface, f: Frame): void;
|
||||
}
|
||||
|
||||
/** Apply ambient light to a base (daylight) color. */
|
||||
export function lit(f: Frame, base: RGB): RGB {
|
||||
return nightTone(f, mul(base, f.amb));
|
||||
}
|
||||
|
||||
function nightTone(f: Frame, c: RGB): RGB {
|
||||
if (f.desat <= 0) return c;
|
||||
const l = c[0] * 0.3 + c[1] * 0.55 + c[2] * 0.15;
|
||||
return [c[0] + (l - c[0]) * f.desat, c[1] + (l - c[1]) * f.desat, c[2] + (l * 1.15 - c[2]) * f.desat];
|
||||
}
|
||||
|
||||
/** Ambient light plus warm direct sun, scaled by how much sun reaches this spot (0–1). */
|
||||
export function sunlit(f: Frame, base: RGB, sun: number, k = 0.55): RGB {
|
||||
const c = lit(f, base);
|
||||
if (sun <= 0) return c;
|
||||
const s = sun * k * f.daylight;
|
||||
return [c[0] + base[0] * (f.sunColor[0] / 255) * s, c[1] + base[1] * (f.sunColor[1] / 255) * s, c[2] + base[2] * (f.sunColor[2] / 255) * s];
|
||||
}
|
||||
|
|
@ -0,0 +1,152 @@
|
|||
import { hash, noise1 } from '../../engine/rng';
|
||||
|
||||
/** Fixed layout of the scene on the 320x180 canvas. */
|
||||
export const W = 320;
|
||||
export const H = 180;
|
||||
|
||||
/**
|
||||
* The canyon is seen through a simple pinhole camera standing on the floor and looking straight
|
||||
* down the canyon: world X runs across it (meters, + is right), z away from the camera and h up
|
||||
* from the floor. Everything on the ground (plants, deer, the stream) is placed in these
|
||||
* coordinates so it all shares one perspective.
|
||||
*/
|
||||
export const HORIZON = 98;
|
||||
export const VPX = 168;
|
||||
export const F = 160;
|
||||
export const CAM_H = 2;
|
||||
|
||||
/** Nearest floor depth on screen (the bottom row). */
|
||||
export const Z_NEAR = (CAM_H * F) / (H - HORIZON);
|
||||
/** The canyon walls end here and the far mesas take over. */
|
||||
export const Z_END = 230;
|
||||
|
||||
export function project(X: number, z: number, h = 0): [number, number] {
|
||||
return [VPX + (X * F) / z, HORIZON + ((CAM_H - h) * F) / z];
|
||||
}
|
||||
|
||||
/** Depth of the floor seen at a screen row (rows at or above the horizon never meet it). */
|
||||
export function floorZ(y: number): number {
|
||||
return (CAM_H * F) / (y + 0.5 - HORIZON);
|
||||
}
|
||||
|
||||
/** Center of the canyon floor: it winds gently left and right as it recedes. */
|
||||
export const canyonX = (z: number): number => 4.2 * Math.sin(z / 17 + 0.4) + 2.2 * Math.sin(z / 7.1 + 2) - 2.6;
|
||||
const canyonSlope = (z: number): number => (4.2 / 17) * Math.cos(z / 17 + 0.4) + (2.2 / 7.1) * Math.cos(z / 7.1 + 2);
|
||||
export const halfWidth = (z: number): number => 10.5 + z * 0.02;
|
||||
|
||||
/** Height of each wall's rim: stepped benches and buttes, sinking toward the far end. */
|
||||
export function wallHeight(z: number, side: -1 | 1): number {
|
||||
const seed = side < 0 ? 11 : 12;
|
||||
const bench = Math.floor(noise1(z / 11, seed) * 4) * 1.6;
|
||||
const rough = (noise1(z / 1.6, seed + 2) - 0.5) * 1.2;
|
||||
const sink = Math.max(0, z - 150) * 0.03;
|
||||
return 15 + bench + rough - sink;
|
||||
}
|
||||
|
||||
/** The stream meanders inside the canyon floor. */
|
||||
export const streamX = (z: number): number => canyonX(z) + 1.1 * Math.sin(z / 5.3 + 2.4) + 0.8 * Math.sin(z / 13 + 0.6) - 1.4;
|
||||
export const streamHalf = (z: number): number => 0.62 + z * 0.006;
|
||||
|
||||
/** Per screen column: which wall the view ray meets first and how far away. */
|
||||
export const SIDE = new Int8Array(W);
|
||||
export const ZHIT = new Float32Array(W);
|
||||
/** Inward wall normal (x, z) at the hit, for lighting. */
|
||||
export const NORMAL_X = new Float32Array(W);
|
||||
export const NORMAL_Z = new Float32Array(W);
|
||||
/** Top and bottom screen rows of the wall in each column. */
|
||||
export const RIM = new Int16Array(W);
|
||||
export const FOOT = new Int16Array(W);
|
||||
|
||||
for (let x = 0; x < W; x++) {
|
||||
const dir = (x + 0.5 - VPX) / F;
|
||||
let z = Z_NEAR * 0.9;
|
||||
SIDE[x] = 0;
|
||||
ZHIT[x] = Z_END;
|
||||
while (z < Z_END) {
|
||||
const X = dir * z;
|
||||
const c = canyonX(z);
|
||||
const w = halfWidth(z);
|
||||
if (X <= c - w || X >= c + w) {
|
||||
SIDE[x] = X < c ? -1 : 1;
|
||||
ZHIT[x] = z;
|
||||
break;
|
||||
}
|
||||
z += Math.max(0.02, z * 0.002);
|
||||
}
|
||||
const s = SIDE[x];
|
||||
if (s !== 0) {
|
||||
// Tangent along the wall is (canyonSlope + widening, 1); the normal points into the canyon.
|
||||
const tx = canyonSlope(z) + s * 0.02;
|
||||
const n = Math.hypot(tx, 1);
|
||||
NORMAL_X[x] = -s / n;
|
||||
NORMAL_Z[x] = (s * tx) / n;
|
||||
RIM[x] = Math.round(HORIZON - ((wallHeight(z, s as -1 | 1) - CAM_H) * F) / z);
|
||||
FOOT[x] = Math.min(H, Math.round(HORIZON + (CAM_H * F) / z));
|
||||
} else {
|
||||
RIM[x] = HORIZON + 1;
|
||||
FOOT[x] = HORIZON + 1;
|
||||
}
|
||||
}
|
||||
|
||||
/** Distant mesas closing off the far end of the canyon (top row per column). */
|
||||
export const MESA: readonly number[] = Array.from({ length: W }, (_, x) => {
|
||||
const flat = 88 - Math.floor(noise1(x / 26, 21) * 3) * 2;
|
||||
const butte = Math.abs(x - 204) < 7 ? 80 + Math.abs(x - 204) * 0.3 : 99;
|
||||
return Math.round(Math.min(flat + (noise1(x / 3, 22) - 0.5) * 1.4, butte));
|
||||
});
|
||||
|
||||
/**
|
||||
* A freestanding sandstone arch on the left rim near the far end. The sun rises behind it, so at
|
||||
* sunrise it first shines through the opening.
|
||||
*/
|
||||
export const ARCH = (() => {
|
||||
const cx = 146;
|
||||
const base = RIM[cx];
|
||||
return {
|
||||
cx,
|
||||
base,
|
||||
outerRx: 21,
|
||||
outerRy: 25,
|
||||
innerRx: 14,
|
||||
innerRy: 17,
|
||||
/** Center of the opening, where the sun shines through. */
|
||||
openY: base - 7,
|
||||
};
|
||||
})();
|
||||
|
||||
/** A small window worn through a fin just under the rim of the left wall. */
|
||||
export const WINDOW = { cx: 96, cy: RIM[96] + 9, rx: 3.5, ry: 4.5 };
|
||||
|
||||
/** Is this screen pixel part of the arch's rock (above the rim)? */
|
||||
export function archRock(x: number, y: number): boolean {
|
||||
const a = ARCH;
|
||||
const dx = x + 0.5 - a.cx;
|
||||
const dy = a.base - (y + 0.5);
|
||||
// Below the opening the arch is a solid fin running down to the canyon floor.
|
||||
if (dy < 0) return y <= HORIZON && Math.abs(dx + 1) < a.outerRx - dy * 0.15 + (hash(x, y, 32) - 0.5);
|
||||
const lean = dx + dy * 0.12;
|
||||
const outer = (lean / a.outerRx) ** 2 + (dy / a.outerRy) ** 2;
|
||||
if (outer > 1 + (hash(x, y, 31) - 0.5) * 0.08) return false;
|
||||
return (dx / a.innerRx) ** 2 + (dy / a.innerRy) ** 2 > 1;
|
||||
}
|
||||
|
||||
export function archOpening(x: number, y: number): boolean {
|
||||
const a = ARCH;
|
||||
const dx = x + 0.5 - a.cx;
|
||||
const dy = a.base - (y + 0.5);
|
||||
return dy >= -1 && (dx / a.innerRx) ** 2 + (dy / a.innerRy) ** 2 <= 1;
|
||||
}
|
||||
|
||||
/** Everything solid that can hide the sun: walls, the arch and the far mesas. */
|
||||
export const ROCK: Uint8Array = (() => {
|
||||
const m = new Uint8Array(W * H);
|
||||
for (let x = 0; x < W; x++) {
|
||||
for (let y = 0; y < H; y++) {
|
||||
// The arch's opening is kept clear of the wall behind it, so it always frames open sky.
|
||||
const wall = SIDE[x] !== 0 && y >= RIM[x] && !(y < ARCH.base && archOpening(x, y));
|
||||
const mesa = y >= MESA[x];
|
||||
if (wall || mesa || archRock(x, y) || y > HORIZON) m[y * W + x] = 1;
|
||||
}
|
||||
}
|
||||
return m;
|
||||
})();
|
||||
|
|
@ -1,11 +1,12 @@
|
|||
import type { Meditation } from './types';
|
||||
import { desertSunrise } from './desert-sunrise';
|
||||
import { mediterraneanCoast } from './mediterranean-coast';
|
||||
import { northernCabin } from './northern-cabin';
|
||||
import { ringedPlanet } from './ringed-planet';
|
||||
import { theReef } from './the-reef';
|
||||
|
||||
/** Every meditation listed on the main menu, in display order. Add new ones here. */
|
||||
export const meditations: readonly Meditation[] = [northernCabin, mediterraneanCoast, theReef, ringedPlanet];
|
||||
export const meditations: readonly Meditation[] = [northernCabin, mediterraneanCoast, theReef, ringedPlanet, desertSunrise];
|
||||
|
||||
export function findMeditation(id: string): Meditation | undefined {
|
||||
return meditations.find((m) => m.id === id);
|
||||
|
|
|
|||
Loading…
Reference in New Issue