diff --git a/src/meditations/desert-sunrise/canyon.ts b/src/meditations/desert-sunrise/canyon.ts index ec2f1a5..cea3986 100644 --- a/src/meditations/desert-sunrise/canyon.ts +++ b/src/meditations/desert-sunrise/canyon.ts @@ -5,6 +5,7 @@ import type { Surface } from '../../engine/Surface'; import type { Frame } from './timeline'; import { ARCH, + archDepth, archOpening, archRock, CAM_H, @@ -96,7 +97,7 @@ 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)); + const c = rockColor(x, y, ARCH_Z + x * 0.1, -1, h, h + (archDepth(x, y) * ARCH_Z) / F); BASE.set(c, (y * W + x) * 3); HEIGHT[y * W + x] = 99; } diff --git a/src/meditations/desert-sunrise/creatures.ts b/src/meditations/desert-sunrise/creatures.ts index dde5836..159148a 100644 --- a/src/meditations/desert-sunrise/creatures.ts +++ b/src/meditations/desert-sunrise/creatures.ts @@ -20,7 +20,7 @@ const OWL_UP: SpriteArt = ['b...........b', '.b.........b.', '..bbb.k.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_X = ARCH.cx - 6; const PERCH_Y = ARCH.base - 4; const owlPath = pathTrack([ [OWL_T0, 262, 8], diff --git a/src/meditations/desert-sunrise/deer.ts b/src/meditations/desert-sunrise/deer.ts index 5a08ebd..9128960 100644 --- a/src/meditations/desert-sunrise/deer.ts +++ b/src/meditations/desert-sunrise/deer.ts @@ -16,20 +16,38 @@ import { F, project, streamHalf, streamX } from './world'; /** 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]; +type Pose = 'look' | 'graze'; + interface Move { t0: number; t1: number; pts: [number, number][]; + /** Brief stops along the way: [start (s into the move), length (s), what the deer does]. */ + pauses: [number, number, Pose][]; } 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, + t1: T + 46, + pts: [bank(74, 1.2), bank(52, 1.0), bank(38, 0.7), bank(29, 0.18)], + pauses: [ + [13, 3.5, 'look'], + [29, 4, 'graze'], + ], + }, + { + t0: T + 71, + t1: T + 108, + pts: [bank(29, 0.18), bank(20, 0.9), bank(12, 0.8), bank(7.2, 0.12)], + pauses: [[13, 3, 'look']], + }, { 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]], + pauses: [], }, ]; @@ -54,46 +72,97 @@ const DRINK = track([ [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); +// --------------------------------------------------------------------------- +// Pacing along each move +// --------------------------------------------------------------------------- + +const PACE_DT = 0.05; + +/** How much of the way through a pause the deer is, 0 (walking) – 1 (stopped), easing in and out. */ +function pauseAmount(m: Move, tm: number): number { + let v = 0; + for (const [p0, len] of m.pauses) { + const ramp = 0.8; + const k = Math.min(clamp01((tm - p0 + ramp) / ramp), clamp01((p0 + len + ramp - tm) / ramp)); + v = Math.max(v, ease.sine(k)); + } + return v; } +/** + * Progress 0–1 along each move: speed eases up at the start, down at the end and around each + * pause, and the walking in between runs a touch quicker so every move still arrives on time. + */ +const PACE: Float32Array[] = MOVES.map((m) => { + const d = m.t1 - m.t0; + const n = Math.ceil(d / PACE_DT) + 1; + const out = new Float32Array(n); + for (let i = 1; i < n; i++) { + const tm = (i - 0.5) * PACE_DT; + const ends = Math.min(1, tm / 1.2, (d - tm) / 1.2); + out[i] = out[i - 1] + ease.sine(clamp01(ends)) * (1 - pauseAmount(m, tm)) * PACE_DT; + } + const total = out[n - 1]; + for (let i = 0; i < n; i++) out[i] /= total; + return out; +}); + +function along(t: number, move: number): number { + const m = MOVES[move]; + const tab = PACE[move]; + const f = (clamp01((t - m.t0) / (m.t1 - m.t0)) * (m.t1 - m.t0)) / PACE_DT; + const i = Math.min(tab.length - 2, Math.floor(f)); + return tab[i] + (tab[i + 1] - tab[i]) * (f - i); +} + +/** The pose a deer is holding during a pause, with its strength 0–1. */ +function pausePose(t: number, move: number): [Pose, number] | undefined { + const m = MOVES[move]; + const tm = t - m.t0; + for (const [p0, len, pose] of m.pauses) { + const k = Math.min(clamp01((tm - p0) / 0.9), clamp01((p0 + len - tm) / 0.9)); + if (k > 0) return [pose, ease.sine(k)]; + } + return undefined; +} + +// --------------------------------------------------------------------------- +// The herd +// --------------------------------------------------------------------------- + interface Deer { lag: number; - dz: number; - dOff: number; size: number; coat: RGB; paths: ((u: number) => [number, number])[]; + /** Distance walked (m) along each move, sampled evenly in its path parameter. */ + lengths: Float32Array[]; seed: number; } +const LENGTH_STEPS = 256; + 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 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 lengths = paths.map((path) => { + const out = new Float32Array(LENGTH_STEPS + 1); + let [px, pz] = path(0); + for (let i = 1; i <= LENGTH_STEPS; i++) { + const [x, z] = path(i / LENGTH_STEPS); + out[i] = out[i - 1] + Math.hypot(x - px, z - pz); + px = x; + pz = z; + } + return out; + }); + return { lag, size, coat: hex(coat), paths, lengths, seed }; } const HERD: Deer[] = [ @@ -103,34 +172,49 @@ const HERD: Deer[] = [ 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; +/** Which move a deer is on (or last finished) at its own time tt. */ +function moveAt(tt: number): number { 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 }; + return move; +} + +/** Where a deer is (world X, z). */ +function place(d: Deer, t: number): { X: number; z: number; move: number } { + const tt = t - d.lag; + const move = moveAt(tt); + const [X, z] = d.paths[move](along(tt, move)); + return { X, z, move }; +} + +/** Total distance a deer has walked by time t (m); the walk cycle is driven by this, so hooves never skate. */ +function walked(d: Deer, t: number): number { + const tt = t - d.lag; + let s = 0; + for (let i = 0; i < MOVES.length && tt > MOVES[i].t0; i++) { + const tab = d.lengths[i]; + const f = along(tt, i) * LENGTH_STEPS; + const j = Math.min(LENGTH_STEPS - 1, Math.floor(f)); + s += tab[j] + (tab[j + 1] - tab[j]) * (f - j); + } + return s; } /** 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; + return project(b[0], b[1])[0] >= project(a[0], a[1])[0] ? 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; + const move = moveAt(tt); + const m = MOVES[move]; + const dir = moveDir(d, move, Math.min(along(tt, move), 0.98)); + if (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); + const prev = moveDir(d, move - 1, 0.98); return prev + (dir - prev) * ease.sine(clamp01((tt - m.t0 + 0.2) / 1.6)); } @@ -156,64 +240,111 @@ function limb(lx: number, ly: number, ax: number, ay: number, bx: number, by: nu return Math.hypot(lx - ax - vx * u, ly - ay - vy * u) - (r0 + (r1 - r0) * u); } +/** Fraction of each leg's cycle spent with the hoof planted on the ground. */ +const STANCE = 0.62; +/** Distance covered per full walk cycle, in body lengths of 10 cm × the deer's size. */ +const STRIDE = 9.5; +const GROUND = 8.5; + +/** Hoof position relative to its hip at cycle point c: planted and sweeping back, then lifted and swung forward. */ +function hoof(c: number, reach: number, lift: number): [number, number] { + if (c < STANCE) return [reach * (1 - (2 * c) / STANCE), 0]; + const w = (c - STANCE) / (1 - STANCE); + return [reach * (2 * ease.sine(w) - 1), -lift * Math.sin(Math.PI * w)]; +} + +/** + * Two-bone leg: where the joint sits between hip and hoof. `bend` +1 puts it forward (a foreleg's + * knee), -1 behind (a hind leg's hock). + */ +function joint(hx: number, hy: number, fx: number, fy: number, l1: number, l2: number, bend: number): [number, number] { + const dx = fx - hx; + const dy = fy - hy; + const d = Math.max(1e-3, Math.hypot(dx, dy)); + const dd = Math.min(d, (l1 + l2) * 0.999); + const a = (l1 * l1 - l2 * l2 + dd * dd) / (2 * dd); + const h = Math.sqrt(Math.max(0, l1 * l1 - a * a)); + const ux = dx / d; + const uy = dy / d; + return [hx + ux * a + uy * h * bend, hy + uy * a - ux * h * bend]; +} + +interface Gait { + /** Position in the walk cycle (cycles walked). */ + phase: number; + /** How much the deer is walking, 0 standing – 1 full stride. */ + amp: number; +} + +/** The four legs in lateral-sequence walk order: left hind, left fore, right hind, right fore. */ +const LEGS = [ + { fore: true, off: 0.25, far: true }, + { fore: false, off: 0, far: true }, + { fore: true, off: 0.75, far: false }, + { fore: false, off: 0.5, far: false }, +] as const; + /** * 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. + * `drink` lowers the head to the water, `alert` raises it high to look around. */ -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; +function deerSampler(p: DeerPal, g: Gait, drink: number, alert: number, flick: number, earTwitch: number) { + // The body rises and dips a little with each footfall; the hooves stay on the ground. + const bob = 0.3 * g.amp * Math.cos(g.phase * Math.PI * 4); + const nod = 0.07 * g.amp * Math.sin(g.phase * Math.PI * 4 + 1); + // The neck rises forward from the shoulders, carried a little lower while walking; to drink it + // swings down and the shoulders dip. + const baseY = -0.6 + drink * 1.2 + bob; + const neckA = -0.9 + drink * 2.1 + 0.15 * g.amp - 0.3 * alert + nod; + const neckL = 4 + drink * 2.2 + 0.3 * alert; const nx = 4.4 + Math.cos(neckA) * neckL; const ny = baseY + Math.sin(neckA) * neckL; - const headA = 0.45 + drink * 1.0; + const headA = 0.45 + drink * 1.0 - 0.15 * alert + nod; 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), - }; + // Drinking, the forelegs splay forward a little to let the head reach the water. + const splay = drink * 0.9; + const legs = LEGS.map((l) => { + const hipX = l.fore ? 3.7 : -4; + const hipY = (l.fore ? 0.9 : 0.4) + bob; + const [fx, fy] = hoof((((g.phase + l.off) % 1) + 1) % 1, 2.95 * g.amp, (l.fore ? 1.3 : 1) * g.amp); + const footX = hipX + fx + (l.fore ? splay : -splay * 0.3); + const footY = GROUND + fy; + const [kx, ky] = l.fore ? joint(hipX, hipY, footX, footY, 3.9, 3.9, 1) : joint(hipX, hipY, footX, footY, 4.5, 4.0, -1); + return { ...l, hipX, hipY, footX, footY, kx, ky }; }); 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; - } + // Hind legs carry a heavy ham; forelegs are slimmer. + const upper = l.fore ? limb(lx, ly, l.hipX, l.hipY, l.kx, l.ky, 1.15, 0.6) : limb(lx, ly, l.hipX, l.hipY, l.kx, l.ky, 1.7, 0.65); + const lower = limb(lx, ly, l.kx, l.ky, l.footX, l.footY, 0.52, 0.38); + if (upper > 0 && lower > 0) continue; + let c = Math.hypot(lx - l.footX, ly - l.footY) < 0.75 ? p.hoof : lower <= 0 ? mix(p.coat, p.light, 0.3) : p.coat; + if (upper <= 0 && ly > l.hipY + 1.2 && lx < l.hipX - 0.4 && !l.fore) c = p.light; + 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; + if (lx < -8.4 || lx > 11.5 || ly < -9.5 || 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 al = 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; + const taper = 1.25 - Math.max(0, al) * 0.18; + if (nearHead && al > -1.6 && al < 2.4 && Math.abs(across) < taper) { + if (al > 1.7) return p.nose; + if (al > 0.9) return p.muzzle; + if (al > -0.6 && al < -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 ea = headA - 2.1 - side * 0.45 - earTwitch * (side ? 0.4 : 0) - alert * 0.2; 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; @@ -221,14 +352,16 @@ function deerSampler(p: DeerPal, phase: number, stride: number, drink: number, f 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; + // Body parts ride up and down with the bob. + const by = ly - bob; // 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 (lx < -5.5 && limb(lx, by, -6.3, -1.2, -7.1 - flick * 0.4, 0.8 - flick * 2.6, 0.55, 0.4) <= 0) return by > -1.2 + (1 - flick) * 1.6 ? p.nose : p.light; + const body = (lx / 6.4) ** 2 + ((by + (lx < 0 ? 0.15 : 0)) / 2.8) ** 2; + const chest = Math.hypot(lx - 4.2, by - 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; + if (lx < -5 && by < 0.6) return p.light; + if (by > 1.7) return p.light; + return by < -1.8 ? mix(p.coat, p.light, 0.25) : p.coat; } return ly > 0 ? leg(lx, ly, true) : undefined; }; @@ -254,18 +387,21 @@ function drawDeer(s: Surface, f: Frame, d: Deer): void { 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); + // Smaller deer take shorter, quicker steps over the same ground. + const dist = walked(d, t); + const speed = (walked(d, t + 0.1) - walked(d, t - 0.1)) / 0.2; + const gait: Gait = { phase: dist / (STRIDE * 0.1 * d.size) + d.seed * 0.37, amp: clamp01(speed / 0.5) }; + const tt = t - d.lag; + const pose = pausePose(tt, moveAt(tt)); + let drink = DRINK(t - d.lag * 0.85 - d.seed * 0.6); + let alert = 0; + if (pose?.[0] === 'graze') drink = Math.max(drink, 0.8 * pose[1]); + if (pose?.[0] === 'look') alert = pose[1]; 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); + sampleShape(stamp, v, 11.5, deerSampler(pal, gait, drink, alert, flick, twitch)).draw(s, 0.72); + drawRipples(s, f, p.X, p.z, sc, gait.amp, d.seed); } /** Rings spreading on the water around a deer's legs while it wades or drinks. */ @@ -281,7 +417,7 @@ function drawRipples(s: Surface, f: Frame, X: number, z: number, sc: number, str 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; + if (Math.abs(cx - 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)); diff --git a/src/meditations/desert-sunrise/index.ts b/src/meditations/desert-sunrise/index.ts index f92710e..c21871f 100644 --- a/src/meditations/desert-sunrise/index.ts +++ b/src/meditations/desert-sunrise/index.ts @@ -4,10 +4,10 @@ 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 { drawFloor, drawMist, drawStream, streamRockThings } from './stream'; import { DURATION, frameAt, type Thing } from './timeline'; -const FLORA = [...floraThings(), lizardThing()]; +const FLORA = [...floraThings(), ...streamRockThings(), lizardThing()]; const scene: Scene = { render(s, t) { diff --git a/src/meditations/desert-sunrise/stream.ts b/src/meditations/desert-sunrise/stream.ts index b082d4f..2f3d822 100644 --- a/src/meditations/desert-sunrise/stream.ts +++ b/src/meditations/desert-sunrise/stream.ts @@ -2,8 +2,8 @@ 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'; +import { sunlit, type Frame, type Thing } from './timeline'; +import { canyonX, F, FOOT, H, halfWidth, HORIZON, floorZ, LEDGES, project, SIDE, STREAM_ROCKS, streamHalf, streamX, VPX, W } from './world'; const SAND = hex('#d4a47c'); const SAND_DARK = hex('#c08e68'); @@ -14,6 +14,7 @@ const CRUST = hex('#6e5a4c'); const GRASS = hex('#7c8650'); const WATER_TINT = hex('#2e4a60'); const FOAM = hex('#e8f0f4'); +const WHITE_WATER = hex('#f4f8fa'); const MIST = hex('#c8d4e8'); const MIST_GOLD = hex('#ffd6a0'); @@ -76,6 +77,47 @@ for (let y = HORIZON + 1; y < H; y++) { } } +/** + * How churned up the water is at each stream pixel (0 calm – 1 white water): a cushion piled up + * against the upstream side of each rock, a V of broken water trailing downstream (toward the + * camera), and a band of foam where the stream pours over each ledge. Glassy water slides + * smoothly toward the lip of each ledge. + */ +const WHITE = new Float32Array(W * H); +const GLASSY = new Uint8Array(W * H); + +for (let i = 0; i < W * H; i++) { + if (KIND[i] !== 2) continue; + const X = FX[i]; + const z = FZ[i]; + let v = 0; + for (const r of STREAM_ROCKS) { + const down = r.z - z; + if (down < -r.r * 2 || down > 3) continue; + const lat = Math.abs(X - r.X); + if (down < 0) { + // Upstream cushion, hugging the rock's face. + if (lat < r.r * 1.3) v = Math.max(v, 0.95 * (1 - -down / (r.r * 2))); + continue; + } + const len = 1.2 + r.r * 6; + const width = r.r * 1.1 + down * 0.3; + if (down > len || lat > width) continue; + // Strongest right behind the rock and along the arms of the V. + const arm = 0.6 + 0.4 * Math.min(1, lat / width / 0.75); + v = Math.max(v, (1 - down / len) * arm * (lat < r.r * 1.2 && down < r.r ? 1 : 0.85)); + } + for (const zl of LEDGES) { + const down = zl - z; + if (down > -0.06 && down < 0.12) v = 1; + else if (down >= 0.12 && down < 1.1) v = Math.max(v, 0.9 * (1 - (down - 0.12) / 0.98)); + else if (down <= -0.06 && down > -1) GLASSY[i] = 1; + } + // The banks churn the water a little as it brushes past. + if (EDGE[i] > -0.08) v = Math.max(v, 0.35); + WHITE[i] = v; +} + export function drawFloor(s: Surface, f: Frame): void { const amb = f.amb; const sun = f.sunColor; @@ -115,6 +157,10 @@ 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); + // White water is pale even by starlight, and warms once the sun is on it. + const foamShade = mix(scale(WHITE_WATER, Math.min(1, Math.max(f.amb[0], f.amb[1], f.amb[2]) * 1.1 + 0.04)), [150, 170, 200], 0.2 * (1 - f.daylight)); + const foamSun = sunlit(f, WHITE_WATER, 1, 0.35); + const d = s.data; for (let y = HORIZON + 1; y < H; y++) { const depth = (y - HORIZON) / (H - HORIZON); for (let x = 0; x < W; x++) { @@ -122,24 +168,101 @@ export function drawStream(s: Surface, f: Frame): void { if (KIND[i] !== 2) continue; const X = FX[i]; const z = FZ[i]; + const churn = WHITE[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)); + // Glassy water above a ledge barely ripples; broken water scatters the reflection. + const flow = noise2(X * 2.2, (z + t * 1.1) * 1.6, 61); + const rough = GLASSY[i] ? 0.3 : 1 + churn * 2; + const wob = Math.round((flow - 0.5) * (1 + depth * 3) * rough); + const src = s.get(x + wob, 2 * HORIZON - y + Math.round((flow - 0.5) * 2 * rough)); 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); + if (flow > 0.7 && !GLASSY[i]) c = mix(c, [255, 255, 255], 0.08 + 0.1 * f.daylight); else if (flow < 0.25) c = mix(c, tint, 0.3); + // Streaks of current stretched out along the flow. + const streak = noise2(X * 7, (z + t * 1.3) * 1.1, 65); + if (streak > 0.74) c = mix(c, foamShade, 0.18 * (1 + churn)); // 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); + // Bubbly white water, its texture tumbling downstream. + if (churn > 0) { + const bub = noise2(X * 9, (z + t * 1.5) * 7, 63) * 0.7 + noise2(X * 19, (z + t * 1.8) * 15, 64) * 0.3; + const a = clamp01((churn * 0.88 - (1 - bub)) * 3); + if (a > 0) { + const sun = f.daylight > 0 ? floorSunAt(f, X, z) : 0; + c = mix(c, mix(foamShade, foamSun, sun * f.daylight), Math.min(1, a)); + } + } + const o = i * 4; + d[o] = c[0]; + d[o + 1] = c[1]; + d[o + 2] = c[2]; + d[o + 3] = 255; // 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); + if (g > 0.8 - churn * 0.12 && hash(x, y, Math.floor(t * 6)) > 0.4) s.blend(x, y, sparkle, (g - 0.8 + churn * 0.12) * 4 * f.daylight * sunHere); } } } + drawSplashes(s, f, foamShade, foamSun); +} + +/** Droplets hopping up where the water tumbles over each ledge. */ +function drawSplashes(s: Surface, f: Frame, shade: RGB, sun: RGB): void { + for (const zl of LEDGES) { + const k = F / zl; + const half = streamHalf(zl); + const n = Math.max(3, Math.round(half * 6)); + for (let j = 0; j < n; j++) { + const period = 0.55 + hash(j, Math.round(zl), 71) * 0.5; + const u = (f.t / period + hash(j, Math.round(zl), 72)) % 1; + const cycle = Math.floor(f.t / period + hash(j, Math.round(zl), 72)); + const X = streamX(zl) + (hash(j, cycle, 73) * 2 - 1) * half * 0.9; + const [x, y0] = project(X, zl + 0.1); + const hgt = (0.05 + hash(j, cycle, 74) * 0.08) * k; + const y = y0 - hgt * 4 * u * (1 - u); + const c = mix(shade, sun, f.daylight * floorSunAt(f, X, zl)); + s.blend(Math.round(x), Math.round(y), c, 0.85 * (1 - u * 0.5)); + } + } +} + +const ROCK_TONES = [hex('#b48a70'), hex('#86624e'), hex('#4e3a32')] as const; +const ROCK_WET = hex('#3a2c28'); +const MOSS = hex('#5a6a3c'); + +/** A wet, rounded rock sitting in the stream with its base awash. */ +function drawStreamRock(s: Surface, f: Frame, r: (typeof STREAM_ROCKS)[number]): void { + const k = F / r.z; + const [fx, fy] = project(r.X, r.z); + const rx = r.r * k; + if (rx < 0.6) return; + const ry = rx * 0.95; + const cx = Math.round(fx); + const by = Math.round(fy); + const sun = floorSunAt(f, r.X, r.z); + const tones = ROCK_TONES.map((c) => sunlit(f, c, sun)); + const wet = sunlit(f, ROCK_WET, sun); + const moss = sunlit(f, MOSS, sun); + for (let y = Math.floor(by - ry - 1); y <= by; y++) { + const v = (by + 0.5 - y) / ry; + for (let x = Math.floor(cx - rx - 1); x <= cx + rx + 1; x++) { + const u = (x + 0.5 - cx) / rx; + const n = noise2(((x - cx) / k) * 9 + r.seed, ((by - y) / k) * 9, r.seed); + if (u * u + v * v > 1 - 0.22 * n) continue; + // Lit on top, shaded underneath, and dark where the water keeps it wet. + const l = v - u * 0.35 + hash(x, y, r.seed) * 0.35; + let c = tones[l > 0.7 ? 0 : l > 0.15 ? 1 : 2]; + if (v < 0.2) c = wet; + else if (v < 0.45 && hash(x, y, r.seed + 1) > 0.55) c = moss; + s.set(x, y, c); + } + } +} + +export function streamRockThings(): Thing[] { + return STREAM_ROCKS.map((r) => ({ z: r.z, draw: (s, f) => drawStreamRock(s, f, r) })); } /** Mist density per floor pixel, refilled each frame while there is mist, and its extent per row. */ diff --git a/src/meditations/desert-sunrise/world.ts b/src/meditations/desert-sunrise/world.ts index 7496be9..0db332c 100644 --- a/src/meditations/desert-sunrise/world.ts +++ b/src/meditations/desert-sunrise/world.ts @@ -1,4 +1,4 @@ -import { hash, noise1 } from '../../engine/rng'; +import { hash, noise1, Rng } from '../../engine/rng'; /** Fixed layout of the scene on the 320x180 canvas. */ export const W = 320; @@ -47,6 +47,35 @@ export function wallHeight(z: number, side: -1 | 1): number { 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; +/** The deer wade across the stream here (see deer.ts), so it is kept clear of rocks. */ +const CROSSING: readonly [number, number] = [5.2, 9.6]; + +/** Rocks in the stream bed, each with the white water it kicks up. Radius r in meters. */ +export const STREAM_ROCKS: readonly { X: number; z: number; r: number; seed: number }[] = (() => { + const r = new Rng(404); + const out: { X: number; z: number; r: number; seed: number }[] = []; + const add = (z: number, across: number, rad: number) => + out.push({ X: streamX(z) + across * streamHalf(z), z, r: rad, seed: r.int(1, 1e6) }); + // A few big ones close to the camera, then smaller ones scattered up the stream. + // A low step of rocks across the foreground that the water spills between. + add(4.62, -0.62, 0.22); + add(4.68, 0.18, 0.15); + add(4.6, 0.78, 0.17); + add(4.25, -0.05, 0.09); + add(10.4, 0.5, 0.22); + add(11.2, -0.55, 0.15); + add(13.5, 0.1, 0.18); + for (let i = 0; i < 30; i++) { + const z = 1 / (1 / 9.6 + r.next() * (1 / 70 - 1 / 9.6)); + if (z > CROSSING[0] && z < CROSSING[1]) continue; + add(z, r.range(-0.85, 0.85), r.range(0.1, 0.26)); + } + return out; +})(); + +/** Little ledges where the whole stream pours over a step in the bed. */ +export const LEDGES: readonly number[] = [4.6, 10.9, 16, 24, 38]; + /** 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); @@ -96,8 +125,10 @@ export const MESA: readonly number[] = Array.from({ length: W }, (_, x) => { }); /** - * 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. + * A weathered sandstone arch on the left rim near the far end: a massive buttress on the left + * that runs back behind the canyon wall, a slender right leg pinched at the waist, and a crown + * that steps down along its bedding. The sun rises behind it, so at sunrise it first shines + * through the opening. Shapes are in pixels relative to the middle of the sill (dy is up). */ export const ARCH = (() => { const cx = 146; @@ -105,36 +136,59 @@ export const ARCH = (() => { return { cx, base, - outerRx: 21, - outerRy: 25, - innerRx: 14, - innerRy: 17, + /** Height of the crown above the sill. */ + outerRy: 26, /** Center of the opening, where the sun shines through. */ - openY: base - 7, + openY: base - 8, }; })(); +/** Height above the sill of the arch's top, by column offset: highest over the buttress, falling away to the right. */ +function archTop(dx: number): number { + const dome = dx < -6 ? 26 + (-6 - dx) * 0.35 : 26 - 0.024 * (dx + 6) ** 2; + // The crown breaks along the bedding planes into little stepped ledges. + return Math.floor(dome + (noise1(dx / 3.5, 33) - 0.5) * 3.2); +} + +/** Outer edge of the right leg at a height above the sill: a flared foot below a pinched waist. */ +function archRight(dy: number): number { + const waist = 2 * Math.exp(-(((dy - 9) / 4.5) ** 2)); + const foot = Math.max(0, 4 - dy) * 1.1; + return 18.5 - waist + foot + (noise1(dy / 2.2, 34) - 0.5) * 1.6; +} + +/** The opening: a tall window with a rounded head leaning left, a flat sill and rough, scalloped sides. */ +function inOpening(dx: number, dy: number): boolean { + const u = (dx - 3 + dy * 0.16) / 13; + const v = dy / 18; + if (v > 1.2 || Math.abs(u) > 1.3) return false; + const rough = (noise1(Math.atan2(v, u) * 6, 35) - 0.5) * 0.22; + return Math.abs(u) ** 2.4 + v ** 2.2 <= 1 + rough; +} + /** 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)? */ +/** Is this screen pixel part of the arch's rock? */ 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; + const dx = x + 0.5 - ARCH.cx; + const dy = ARCH.base - (y + 0.5); + if (dx < -26) return false; + // Below the sill the arch is a solid fin running down to the canyon floor, flaring at its foot. + if (dy < 0) return y <= HORIZON && dx < archRight(0) + 0.5 - dy * 0.25 + (hash(x, y, 32) - 0.5) * 1.5; + if (dx > archRight(dy) || dy > archTop(dx)) return false; + return !inOpening(dx, dy); } 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; + const dx = x + 0.5 - ARCH.cx; + const dy = ARCH.base - (y + 0.5); + return dy >= -1 && inOpening(dx, Math.max(0, dy)); +} + +/** Rows (in pixels) from a point on the arch up to its crown, for coloring the pale cap rock. */ +export function archDepth(x: number, y: number): number { + return archTop(x + 0.5 - ARCH.cx) - (ARCH.base - (y + 0.5)); } /** Everything solid that can hide the sun: walls, the arch and the far mesas. */