diff --git a/src/meditations/desert-sunrise/deer.ts b/src/meditations/desert-sunrise/deer.ts index 75424df..53dcd90 100644 --- a/src/meditations/desert-sunrise/deer.ts +++ b/src/meditations/desert-sunrise/deer.ts @@ -8,9 +8,10 @@ import { sunlit, TIMES, type Frame, type Thing } from './timeline'; import { F, groundHeight, 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 small family of mule deer, led by an antlered buck, comes 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. Walking toward the camera they + * are seen head-on; side-on whenever they move across the view or stand drinking. */ /** A point on the right bank, `off` meters back from the water's edge. */ @@ -51,7 +52,7 @@ const MOVES: Move[] = [ }, ]; -/** Head down to drink (1) or raised (0), for the doe; the others follow her lead with a lag. */ +/** Head down to drink (1) or raised (0), for the buck; the others follow his lead with a lag. */ const DRINK = track([ [T + 46, 0], [T + 49, 1], @@ -64,7 +65,7 @@ const DRINK = track([ [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. + // The sun breaks through the arch: he lifts his head and looks up the canyon at it. [T + 119, 0], [T + 125, 0], [T + 127, 1], @@ -138,11 +139,12 @@ interface Deer { /** Distance walked (m) along each move, sampled evenly in its path parameter. */ lengths: Float32Array[]; seed: number; + antlers: boolean; } const LENGTH_STEPS = 256; -function makeDeer(lag: number, dz: number, dOff: number, size: number, coat: string, seed: number): Deer { +function makeDeer(lag: number, dz: number, dOff: number, size: number, coat: string, seed: number, antlers = false): Deer { 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. @@ -162,11 +164,12 @@ function makeDeer(lag: number, dz: number, dOff: number, size: number, coat: str } return out; }); - return { lag, size, coat: hex(coat), paths, lengths, seed }; + return { lag, size, coat: hex(coat), paths, lengths, seed, antlers }; } +/** The buck leads, so he is always the deer nearest the camera. */ const HERD: Deer[] = [ - makeDeer(0, 0, 0, 1, '#9c7a5c', 1), + makeDeer(0, 0, 0, 1.08, '#8e7458', 1, true), 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), @@ -207,15 +210,45 @@ function moveDir(d: Deer, move: number, u: number): number { return project(b[0], b[1])[0] >= project(a[0], a[1])[0] ? 1 : -1; } -function facing(d: Deer, t: number): number { +/** Whether a deer is drawn head-on (walking toward the camera) or side-on, facing +1 right or -1 left. */ +interface View { + front: boolean; + facing: number; +} + +/** Standing at the water's edge they turn side-on to face the stream, which is on their left. */ +const AT_WATER: View = { front: false, facing: -1 }; +const TURN = 1.4; + +/** How a deer looks while walking a move: head-on if it is heading mostly toward the camera. */ +function walkingView(d: Deer, move: number, u: number): View { + const a = d.paths[move](Math.max(0, u - 0.01)); + const b = d.paths[move](Math.min(1, u + 0.01)); + const vX = b[0] - a[0]; + const vz = b[1] - a[1]; + if (vz < 0 && Math.abs(vX) < -vz * 0.9) return { front: true, facing: 1 }; + return { front: false, facing: moveDir(d, move, u) }; +} + +/** Turning from one view to another over k = 0–1, swinging through a narrowed side view. */ +function turn(from: View, to: View, k: number): View { + k = clamp01(k); + if (from.front === to.front && from.facing === to.facing) return to; + if (from.front && !to.front) return k < 0.35 ? from : { front: false, facing: to.facing * Math.max(0.36, ease.sine((k - 0.35) / 0.65)) }; + if (!from.front && to.front) return k > 0.65 ? to : { front: false, facing: from.facing * Math.max(0.36, 1 - ease.sine(k / 0.65)) }; + return { front: false, facing: from.facing + (to.facing - from.facing) * ease.sine(k) }; +} + +function view(d: Deer, t: number): View { const tt = t - d.lag; 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, move - 1, 0.98); - return prev + (dir - prev) * ease.sine(clamp01((tt - m.t0 + 0.2) / 1.6)); + // Arrived at the water: turn side-on to drink. + if (tt >= m.t1) return turn(walkingView(d, move, 0.99), AT_WATER, (tt - m.t1) / TURN); + const walk = walkingView(d, move, Math.min(0.99, Math.max(0.01, along(tt, move)))); + // Setting off again from the water. + if (move > 0 && tt < m.t0 + TURN) return turn(AT_WATER, walk, (tt - m.t0) / TURN); + return walk; } // --------------------------------------------------------------------------- @@ -230,6 +263,8 @@ interface DeerPal { nose: RGB; ear: RGB; hoof: RGB; + antler: RGB; + antlerBase: RGB; } /** Distance from p to a tapering limb running from a to b; <= 0 means inside. */ @@ -288,7 +323,7 @@ const LEGS = [ * The deer in local units of 10 cm: x forward, y down, origin mid-body 85 cm above the ground. * `drink` lowers the head to the water, `alert` raises it high to look around. */ -function deerSampler(p: DeerPal, g: Gait, drink: number, alert: number, flick: number, earTwitch: number) { +function deerSampler(p: DeerPal, g: Gait, drink: number, alert: number, flick: number, earTwitch: number, antlers: boolean) { // 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); @@ -304,6 +339,13 @@ function deerSampler(p: DeerPal, g: Gait, drink: number, alert: number, flick: n const hcy = ny + Math.sin(headA) * 1.1; const hdx = Math.cos(headA); const hdy = Math.sin(headA); + // Antler points, in the head's own frame (a forward along the head, b up from it) so they tilt + // as the head lowers. Each beam sweeps up and back, then forks twice the mule-deer way. + const A = (a: number, b: number): [number, number] => [hcx + hdx * a + hdy * b, hcy + hdy * a - hdx * b]; + const beams = antlers + ? ANTLER_SIDE.map(([a0, b0, a1, b1]) => [...A(a0, b0), ...A(a1, b1), b0 < 1.5 ? 1 : 0] as const) + : []; + const box = beamBox(beams, 1.1); // Drinking, the forelegs splay forward a little to let the head reach the water. const splay = drink * 0.9; const legs = LEGS.map((l) => { @@ -329,7 +371,7 @@ function deerSampler(p: DeerPal, g: Gait, drink: number, alert: number, flick: n return undefined; }; return (lx: number, ly: number): RGB | undefined => { - if (lx < -8.4 || lx > 11.5 || ly < -9.5 || ly > 10) return undefined; + if (lx < -8.4 || lx > 11.5 || ly < -13 || ly > 10) return undefined; // Head, ears and neck in front of the body. const rx = lx - hcx; const ry = ly - hcy; @@ -349,6 +391,16 @@ function deerSampler(p: DeerPal, g: Gait, drink: number, alert: number, flick: n 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; } + const inBox = lx >= box[0] && lx <= box[2] && ly >= box[1] && ly <= box[3]; + for (let far = 0; far < 2 && inBox; far++) { + for (const [x0, y0, x1, y1, base] of beams) { + const o = far * 1.1; + if (limb(lx, ly, x0 + o, y0, x1 + o, y1, 0.32, 0.22) <= 0) { + const c = base ? p.antlerBase : p.antler; + return far ? mix(c, p.dark, 0.35) : c; + } + } + } 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; @@ -367,6 +419,129 @@ function deerSampler(p: DeerPal, g: Gait, drink: number, alert: number, flick: n }; } +/** Side-view antler segments [a0, b0, a1, b1] in the head's frame (see deerSampler). */ +const ANTLER_SIDE: readonly (readonly [number, number, number, number])[] = [ + [-0.7, 1.0, -2.0, 3.0], + [-2.0, 3.0, -1.2, 5.0], + [-1.2, 5.0, 0.6, 6.0], + [-1.2, 5.0, -2.0, 6.6], + [-2.0, 3.0, -0.2, 4.0], +]; + +/** Front-view antler segments [x0, y0, x1, y1] for the right beam, up from the top of the head. */ +const ANTLER_FRONT: readonly (readonly [number, number, number, number])[] = [ + [0.5, 0, 1.8, 1.9], + [1.8, 1.9, 3.0, 3.4], + [1.8, 1.9, 1.4, 3.8], + [3.0, 3.4, 3.7, 4.6], + [3.0, 3.4, 2.5, 4.8], +]; + +/** + * The deer seen head-on as it walks toward the camera, in the same units as the side view: x to + * the viewer's right, y down, origin mid-body 85 cm above the ground. + */ +function deerFrontSampler(p: DeerPal, g: Gait, drink: number, alert: number, earTwitch: number, antlers: boolean) { + const bob = 0.3 * g.amp * Math.cos(g.phase * Math.PI * 4); + // The weight rolls over each planted forefoot in turn, and the head nods with the steps. + const sway = 0.3 * g.amp * Math.sin((g.phase + 0.25) * Math.PI * 2); + const nod = 0.3 * g.amp * Math.sin(g.phase * Math.PI * 4 + 1); + const hx = sway * 0.5; + // The neck reaches forward toward us, so the head sits low over the chest. + const hy = -4.4 - 1.1 * alert + drink * 11 + bob + nod; + // Lowered to graze we look down on the top of the head, so the face shortens. + const faceLen = 4.1 - drink * 1.4; + const top = hy - faceLen * 0.45; + const legs = LEGS.map((l) => { + // The deer's left side is on the viewer's right. + const side = l.far ? 1 : -1; + const x0 = (l.fore ? 1.25 : 1.75) * side + sway * 0.7; + const hipY = (l.fore ? 1.8 : 1.4) + bob; + const [fx, fy] = hoof((((g.phase + l.off) % 1) + 1) % 1, 2.95 * g.amp, (l.fore ? 2 : 1.6) * g.amp); + // Hind hooves land further back, so a little higher on screen; a hoof reaching forward comes + // toward us and drops slightly. Lifting it folds the leg and bows the knee outward. + const footY = GROUND - (l.fore ? 0 : 0.7) + fx * 0.1 + fy; + const footX = x0 + (l.fore ? side * drink * 1.1 : 0); + const kx = (x0 + footX) / 2 - side * 0.18 * fy; + const ky = (hipY + footY) / 2 + fy * 0.25; + return { ...l, x0, hipY, footX, footY, kx, ky }; + }); + const leg = (lx: number, ly: number, fore: boolean): RGB | undefined => { + for (const l of legs) { + if (l.fore !== fore) continue; + const upper = limb(lx, ly, l.x0, l.hipY, l.kx, l.ky, 0.62, 0.48); + const lower = limb(lx, ly, l.kx, l.ky, l.footX, l.footY, 0.48, 0.38); + if (upper > 0 && lower > 0) continue; + const c = Math.hypot(lx - l.footX, ly - l.footY) < 0.7 ? p.hoof : lower <= 0 ? mix(p.coat, p.light, 0.3) : p.coat; + return fore ? c : mix(c, p.dark, 0.5); + } + return undefined; + }; + const beams = antlers + ? [-1, 1].flatMap((sd) => + ANTLER_FRONT.map(([a0, b0, a1, b1]) => { + const k = 1 - drink * 0.7; + return [hx + sd * a0, top - b0 * k, hx + sd * a1, top - b1 * k, b0 < 1 ? 1 : 0] as const; + }), + ) + : []; + const box = beamBox(beams); + return (lx: number, ly: number): RGB | undefined => { + if (lx < -8 || lx > 8 || ly < -13 || ly > 10) return undefined; + // Face: a long wedge narrowing to the muzzle, with the mule deer's dark brow and pale muzzle. + const v = (ly - top) / faceLen; + const u = lx - hx; + if (v >= 0 && v <= 1 && Math.abs(u) <= 1.55 - 0.9 * v * (1 - drink * 0.5)) { + if (v > 0.86) return p.nose; + if (v > 0.68) return p.light; + if (Math.abs(Math.abs(u) - 1.0) < 0.34 && Math.abs(v - 0.3) < 0.08) return p.nose; + if (v < 0.32 && Math.abs(u) < 0.65) return p.dark; + return Math.abs(u) > 1.1 ? mix(p.coat, p.dark, 0.25) : p.coat; + } + // Big ears held out to the sides. + for (const sd of [-1, 1]) { + const ang = 0.4 + 0.25 * alert - (sd > 0 ? earTwitch * 0.3 : 0); + const ax = sd * Math.cos(ang); + const ay = -Math.sin(ang); + const dx = lx - (hx + sd * 1.1 + ax * 1.4); + const dy = ly - (top + 0.6 + ay * 1.4); + const a = (dx * ax + dy * ay) / 1.55; + const b = (-dx * ay + dy * ax) / 0.8; + const q = a * a + b * b; + if (q <= 1) return q < 0.35 ? p.ear : p.coat; + } + if (lx >= box[0] && lx <= box[2] && ly >= box[1] && ly <= box[3]) { + for (const [x0, y0, x1, y1, base] of beams) { + if (limb(lx, ly, x0, y0, x1, y1, 0.32, 0.22) <= 0) return base ? p.antlerBase : p.antler; + } + } + // A short, thick neck (foreshortened toward us), with a pale patch at the throat. + const nbx = sway * 0.8; + const nby = -1.6 + bob; + const chin = top + faceLen; + if (limb(lx, ly, nbx, nby, hx, hy, 1.75 - drink * 0.6, 1.35 - drink * 0.4) <= 0) { + return drink < 0.4 && Math.abs(lx - hx) < 0.75 && ly > chin && ly < chin + 1 ? p.light : p.coat; + } + // Chest and the barrel of the body behind it. + const cx = sway * 0.7; + const cy = 0.5 + bob; + if (((lx - cx) / 2.75) ** 2 + ((ly - cy) / 2.6) ** 2 <= 1) { + if (ly > cy + 1.7) return p.light; + return Math.abs(lx - cx) > 2 ? mix(p.coat, p.dark, 0.3) : p.coat; + } + return leg(lx, ly, true) ?? leg(lx, ly, false); + }; +} + +/** Bounding box [x0, y0, x1, y1] around antler segments, padded by the beam radius and `dx`. */ +function beamBox(beams: readonly (readonly number[])[], dx = 0): [number, number, number, number] { + let b: [number, number, number, number] = [Infinity, Infinity, -Infinity, -Infinity]; + for (const [x0, y0, x1, y1] of beams) { + b = [Math.min(b[0], x0, x1) - 0.4, Math.min(b[1], y0, y1) - 0.4, Math.max(b[2], x0, x1) + 0.4 + dx, Math.max(b[3], y0, y1) + 0.4]; + } + return b; +} + const stamp = new Stamp(); function drawDeer(s: Surface, f: Frame, d: Deer): void { @@ -386,6 +561,8 @@ function drawDeer(s: Surface, f: Frame, d: Deer): void { nose: L(hex('#1e1814')), ear: L(hex('#c8a890')), hoof: L(hex('#2c2420')), + antler: L(hex('#e4d6b8')), + antlerBase: L(hex('#8a7458')), }; // Smaller deer take shorter, quicker steps over the same ground. const dist = walked(d, t); @@ -399,8 +576,10 @@ function drawDeer(s: Surface, f: Frame, d: Deer): void { 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, gait, drink, alert, flick, twitch)).draw(s, 0.72); + const vw = view(d, t); + const v = { x, y, scale: sc, facing: vw.facing }; + const sampler = vw.front ? deerFrontSampler(pal, gait, drink, alert, twitch, d.antlers) : deerSampler(pal, gait, drink, alert, flick, twitch, d.antlers); + sampleShape(stamp, v, d.antlers ? 13 : 11.5, sampler).draw(s, 0.72); drawRipples(s, f, p.X, p.z, sc, gait.amp, d.seed); }