pixeltation/src/meditations/the-reef/turtles.ts

224 lines
6.7 KiB
TypeScript

import { hex, scale, type RGB } from '../../engine/color';
import { hash } from '../../engine/rng';
import { sampleShape, Stamp } from '../../engine/stamp';
import type { Surface } from '../../engine/Surface';
import { pathTrack, progress, track } from '../../engine/Timeline';
import type { Frame } from './timeline';
import { TIMES } from './timeline';
import { fog } from './world';
/**
* A baby sea turtle arrives at four minutes and explores the reef. Two adults come to meet it;
* they greet nose to nose, then all three swim off together into the blue.
*/
interface TurtlePal {
shell: RGB;
scute: RGB;
scuteLight: RGB;
seam: RGB;
rim: RGB;
skin: RGB;
skinLight: RGB;
plastron: RGB;
eye: RGB;
}
const ADULT: TurtlePal = {
shell: hex('#6a5430'),
scute: hex('#8c7040'),
scuteLight: hex('#b89654'),
seam: hex('#3e301c'),
rim: hex('#5a4626'),
skin: hex('#7a8c5c'),
skinLight: hex('#b4c48e'),
plastron: hex('#e6d6a2'),
eye: hex('#141410'),
};
const BABY: TurtlePal = {
shell: hex('#4a3c2c'),
scute: hex('#5e4c36'),
scuteLight: hex('#80684a'),
seam: hex('#2a2018'),
rim: hex('#3c3024'),
skin: hex('#4c5a50'),
skinLight: hex('#d4dace'),
plastron: hex('#ecead8'),
eye: hex('#0c0c0c'),
};
/** Distance from p to a tapering limb running from (ax, ay) to (bx, by); <= 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)));
const d = Math.hypot(lx - ax - vx * u, ly - ay - vy * u);
return d - (r0 + (r1 - r0) * u);
}
function turtleSampler(p: TurtlePal, stroke: number, nod: number, rearKick: number) {
// Front flippers sweep from raised-back (stroke -1) to swept-down (stroke 1).
const a = Math.PI - 0.15 - 0.85 * stroke;
const L = 11;
const sx = 5;
const sy = 1.5;
const tx = sx + Math.cos(a) * L;
const ty = sy + Math.sin(a) * L;
const flipper = (lx: number, ly: number, far: boolean): RGB | undefined => {
const d = limb(lx, ly, sx, sy, tx, ty, 2.3, 0.7);
if (d > 0) return undefined;
const c = d > -0.7 ? p.skinLight : hash(Math.round(lx * 1.3), Math.round(ly * 1.3), 9) > 0.75 ? p.skinLight : p.skin;
return far ? scale(c, 0.68) : c;
};
return (lx: number, ly: number): RGB | undefined => {
const near = flipper(lx, ly, false);
if (near) return near;
// Head and neck.
const hy = -0.6 + nod;
if (Math.hypot(lx - 14.2, ly - hy + 0.6) < 0.75) return p.eye;
const head = ((lx - 13) / 3.6) ** 2 + ((ly - hy) / 2.6) ** 2;
const neck = ((lx - 9.5) / 3) ** 2 + ((ly - 0.2) / 2.2) ** 2;
if (head <= 1 || neck <= 1) {
if (head <= 1 && lx > 15.6 && ly > hy) return p.seam;
if (ly > hy + 0.8) return p.skinLight;
return hash(Math.round(lx * 1.2), Math.round(ly * 1.2), 8) > 0.7 ? p.skinLight : p.skin;
}
// Back flipper and tail.
if (limb(lx, ly, -8, 1.8, -12.5, 2.8 + rearKick, 1.4, 0.8) <= 0) return p.skin;
if (limb(lx, ly, -9, 0.8, -12, 1.2, 0.7, 0.3) <= 0) return p.skin;
// Shell: a dome over a flatter belly.
const sy0 = -0.5;
const e = (lx / 10.5) ** 2 + ((ly - sy0) / (ly < sy0 ? 6 : 3)) ** 2;
if (e <= 1) {
if (ly > sy0 + 1.4) return p.plastron;
if (ly > sy0) return p.rim;
if (e > 0.8) return ((lx + 20) * 0.55) % 1 < 0.22 ? p.seam : p.rim;
if (Math.abs(ly + 3.6) < 0.45) return p.seam;
const cell = (((lx + 1.5) % 5) + 5) % 5;
if (Math.abs(cell - 2.5) > 2.05) return p.seam;
const centre = Math.abs(cell - 2.5) < 1.1 && Math.abs(ly - (ly < -3.6 ? -4.9 : -1.9)) < 0.9;
return centre ? p.scuteLight : p.scute;
}
return flipper(lx + 1, ly + 1.4, true);
};
}
interface Turtle {
path: (t: number) => [number, number];
facing: (t: number) => number;
scale: number;
pal: TurtlePal;
/** Seconds per flipper stroke. */
period: number;
start: number;
seed: number;
}
const BABY_TURTLE: Turtle = {
path: pathTrack([
[TIMES.babyTurtle, -34, 96],
[246, 40, 90],
[252, 90, 98],
[257, 125, 92],
[262, 148, 82],
[267, 160, 88],
[271, 168, 90],
[275, 172, 89],
[279, 174, 90],
[283, 182, 88],
[288, 212, 78],
[294, 252, 68],
[300, 300, 58],
]),
facing: () => 1,
scale: 0.65,
pal: BABY,
period: 1.7,
start: TIMES.babyTurtle,
seed: 0,
};
const ADULT_A: Turtle = {
path: pathTrack([
[260, 372, 72],
[266, 280, 78],
[271, 222, 86],
[275, 202, 88],
[278, 198, 89],
[281, 204, 86],
[285, 222, 72],
[290, 252, 60],
[295, 286, 50],
[300, 330, 42],
]),
facing: track([
[0, -1],
[279, -1],
[282.5, 1],
]),
scale: 1.25,
pal: ADULT,
period: 3.2,
start: 260,
seed: 1,
};
const ADULT_B: Turtle = {
path: pathTrack([
[262, 404, 112],
[270, 290, 114],
[276, 226, 108],
[280, 206, 110],
[283, 198, 108],
[286, 210, 100],
[290, 232, 88],
[295, 268, 80],
[300, 316, 72],
]),
facing: track([
[0, -1],
[282, -1],
[285.5, 1],
]),
scale: 1.4,
pal: ADULT,
period: 3.6,
start: 262,
seed: 2,
};
const stamp = new Stamp();
function drawTurtle(s: Surface, f: Frame, tu: Turtle): void {
const t = f.t;
if (t < tu.start) return;
const [x, y0] = tu.path(t);
const [nx, ny] = tu.path(t + 0.2);
// Nuzzling: small bobs while the three are together.
const greet = progress(t, TIMES.turtlesMeet + 2, TIMES.turtlesMeet + 4) * (1 - progress(t, 281, 284));
const y = y0 + Math.sin(t * 2.4 + tu.seed) * 1.2 * greet;
const vx = nx - x;
const vy = ny - y0;
const facing = tu.facing(t);
// Level out while turning around, when the side view is squeezed narrow.
const pitch = Math.max(-0.45, Math.min(0.45, Math.atan2(-vy, Math.abs(vx) + 0.4) * 0.8)) * Math.abs(facing);
const phase = ((t - tu.start) / tu.period + tu.seed * 0.3) % 1;
// Strong down-stroke, slower recovery, then a short glide.
const sw = Math.sin(phase * Math.PI * 2);
const stroke = Math.sign(sw) * Math.abs(sw) ** 0.7;
const nod = Math.sin(t * 1.3 + tu.seed) * 0.4 + greet * Math.sin(t * 3) * 0.5;
// As they leave they shrink into the distance and fade into the blue.
const away = progress(t, 286, 300);
const sc = tu.scale * (1 - 0.3 * away);
sampleShape(stamp, { x, y, scale: sc, facing, pitch }, 24, turtleSampler(tu.pal, stroke, nod, Math.sin(phase * Math.PI * 2 + 1) * 0.8));
const haze = 0.4 * away + (tu === ADULT_B ? 0.08 : 0);
stamp.draw(s, 0.6, 1, haze > 0 ? (c, _x, yy) => fog(c, yy, haze) : undefined);
}
export function drawTurtles(s: Surface, f: Frame): void {
drawTurtle(s, f, ADULT_B);
drawTurtle(s, f, BABY_TURTLE);
drawTurtle(s, f, ADULT_A);
}