pixeltation/src/meditations/alpine-afternoon/trees.ts

179 lines
7.0 KiB
TypeScript

import { add, hex, mix, scale, type RGB } from '../../engine/color';
import { progress } from '../../engine/Timeline';
import { hash, noise1, noise2 } from '../../engine/rng';
import type { Surface } from '../../engine/Surface';
import { lit, type Frame } from './timeline';
import { H, HORIZON, SHORE, W } from './world';
const SPRUCE_DARK = hex('#1c3628');
const SPRUCE_MID = hex('#2c4c36');
const SPRUCE_LIGHT = hex('#466a40');
const TRUNK = hex('#3a2a22');
const BARK = hex('#ece6d6');
const BARK_SHADE = hex('#aca494');
const BARK_EYE = hex('#3a3028');
const LEAF_LIGHT = hex('#ffe060');
const LEAF = hex('#eeae2a');
const LEAF_SHADE = hex('#b0701c');
const LEAF_DEEP = hex('#7a4a18');
const LEAF_RED = hex('#e0682a');
const SUN_WARM = hex('#ffc890');
interface TreeOpts {
seed: number;
/** Darkening for near silhouettes. */
dim?: number;
/** How quickly a spruce's branches widen toward its base. */
spread?: number;
}
const spruceHalf = (r: number, h: number, spread: number) => {
if (r < 0) return -1;
const tiers = Math.max(3, Math.round(h / 7));
const th = h / tiers;
const local = (r % th) / th;
return Math.floor((r * spread + 1) * (0.4 + 0.6 * local));
};
/** A narrow subalpine spruce, lit warm on its sunward (left) side. */
function drawSpruce(s: Surface, f: Frame, cx: number, baseY: number, h: number, o: TreeOpts): void {
const dim = o.dim ?? 1;
const spread = o.spread ?? 0.26;
const tone = (c: RGB, k: number) => scale(add(lit(f, c), SUN_WARM, f.warm * k), dim);
const dark = tone(SPRUCE_DARK, 0);
const mid = tone(SPRUCE_MID, 0.05);
const light = tone(SPRUCE_LIGHT, 0.22);
const trunk = tone(TRUNK, 0);
const top = baseY - h - 3;
for (let r = 0; r < h; r++) {
const hw = spruceHalf(r, h, spread);
const sway = Math.round(Math.sin(f.t * 0.6 + o.seed) * (r < h * 0.2 ? 0.6 : 0) * (1 + f.rain));
for (let dx = -hw; dx <= hw; dx++) {
const x = cx + dx + sway;
const y = top + r;
const edge = hw > 0 ? dx / hw : 0;
let c = edge < -0.45 && hash(x, y, o.seed) > 0.3 ? light : edge > 0.2 ? dark : mid;
if (hash(x, y, o.seed + 1) > 0.8) c = dark;
s.set(x, y, c);
}
}
for (let y = baseY - 3; y < baseY; y++) {
s.set(cx - 1, y, trunk);
s.set(cx, y, trunk);
}
}
/**
* A quaking aspen: a pale trunk with dark "eyes", and a golden autumn crown whose leaves shimmer
* as they catch the breeze.
*/
function drawAspen(s: Surface, f: Frame, cx: number, baseY: number, h: number, o: TreeOpts): void {
const dim = o.dim ?? 1;
const t = f.t;
const tone = (c: RGB, k: number) => scale(add(lit(f, c), SUN_WARM, f.warm * k), dim);
const bark = tone(BARK, 0.15);
const barkShade = tone(BARK_SHADE, 0);
const eye = tone(BARK_EYE, 0);
const red = hash(o.seed, 0, 1) > 0.6;
const leafLight = tone(red ? mix(LEAF_LIGHT, LEAF_RED, 0.3) : LEAF_LIGHT, 0.2);
const leaf = tone(red ? mix(LEAF, LEAF_RED, 0.45) : LEAF, 0.15);
const leafShade = tone(LEAF_SHADE, 0.05);
const leafDeep = tone(LEAF_DEEP, 0);
const wide = h >= 34;
const lean = (hash(o.seed, 1, 2) - 0.5) * 4;
const trunkTop = baseY - Math.round(h * 0.9);
for (let y = trunkTop; y < baseY; y++) {
const x = cx + Math.round((lean * (baseY - y)) / h);
s.set(x, y, hash(x, y, o.seed + 3) > 0.9 ? eye : bark);
if (wide) s.set(x + 1, y, hash(x + 1, y, o.seed + 4) > 0.88 ? eye : barkShade);
}
const ccx = cx + lean * 0.6;
const ccy = baseY - h * 0.62;
const rx = h * 0.22;
const ry = h * 0.4;
const breeze = 1 + f.rain * 2;
for (let y = Math.floor(ccy - ry - 1); y <= ccy + ry + 1; y++) {
for (let x = Math.floor(ccx - rx - 1); x <= ccx + rx + 1; x++) {
const u = (x + 0.5 - ccx) / rx;
const v = (y + 0.5 - ccy) / ry;
const d = u * u + v * v + (noise2(x / 2.6, y / 2.6, o.seed + 5) - 0.5) * 0.9;
if (d > 1) continue;
// Gaps toward the edge let the trunk and sky show through.
if (hash(x, y, o.seed + 6) < d * 0.45 - 0.05) continue;
const k = -u * 0.45 - v * 0.35 + (noise2(x / 3, y / 3, o.seed + 7) - 0.5) * 0.8;
let c = k > 0.3 ? leafLight : k > -0.15 ? leaf : k > -0.5 ? leafShade : leafDeep;
// Quaking: individual leaves flip and flash in the breeze.
const q = hash(x, y, o.seed + 8);
if (q > 0.72 && Math.sin(t * (4 + q * 5) * breeze + q * 60) > 0.55) c = c === leafDeep ? leafShade : leafLight;
s.set(x, y, c);
}
}
}
/** Trees behind the man and the camp. */
export function drawBackTrees(s: Surface, f: Frame): void {
drawAspen(s, f, 24, 134, 44, { seed: 11 });
drawSpruce(s, f, 50, 130, 30, { seed: 12 });
drawAspen(s, f, 40, 131, 32, { seed: 13 });
drawAspen(s, f, 212, 128, 28, { seed: 14 });
drawSpruce(s, f, 240, 126, 36, { seed: 15 });
drawAspen(s, f, 228, 129, 42, { seed: 16 });
drawAspen(s, f, 282, 126, 34, { seed: 17 });
drawSpruce(s, f, 300, 129, 54, { seed: 18 });
}
/** Big near trees framing the edges of the view. */
export function drawFrontTrees(s: Surface, f: Frame): void {
drawSpruce(s, f, 6, 186, 150, { seed: 21, dim: 0.5, spread: 0.15 });
drawSpruce(s, f, 22, 184, 60, { seed: 22, dim: 0.62, spread: 0.24 });
drawAspen(s, f, 302, 186, 86, { seed: 23, dim: 0.78 });
drawSpruce(s, f, 316, 186, 140, { seed: 24, dim: 0.5, spread: 0.15 });
}
/** Crowns that shed leaves [x, y, spread]. */
const SOURCES: readonly (readonly [number, number, number])[] = [
[24, 106, 8],
[40, 112, 6],
[228, 102, 8],
[282, 104, 7],
[302, 132, 15],
];
const LEAVES = 16;
/** Golden leaves let go, flutter down on the breeze, and settle on the water or the grass. */
export function drawLeaves(s: Surface, f: Frame): void {
const t = f.t;
const rate = 1 - progress(t, 205, 228);
if (rate <= 0) return;
const gold = add(lit(f, LEAF), SUN_WARM, f.warm * 0.2);
const light = add(lit(f, LEAF_LIGHT), SUN_WARM, f.warm * 0.2);
for (let i = 0; i < LEAVES; i++) {
const period = 11 + hash(i, 0, 31) * 9;
const p = t / period + hash(i, 1, 31);
const cycle = Math.floor(p);
if (hash(i, cycle, 32) > rate * 0.85) continue;
const age = (p - cycle) * period;
const [sx, sy, sp] = SOURCES[Math.floor(hash(i, cycle, 33) * SOURCES.length)];
const x0 = sx + (hash(i, cycle, 34) - 0.5) * sp * 2;
const y0 = sy + (hash(i, cycle, 35) - 0.5) * sp;
const fallFor = 5 + hash(i, cycle, 36) * 4;
const fall = Math.min(age, fallFor);
let x = x0 + fall * (3 + noise1(t * 0.2, 37) * 3) + Math.sin(fall * 2.2 + i) * 3;
let y = y0 + fall * (6 + hash(i, 2, 38) * 3);
// Once down, it rests (drifting slowly if it landed on the water) and fades.
const ground = Math.round(x) >= 0 && Math.round(x) < W ? SHORE[Math.round(x)] : H;
const onWater = y < ground && y >= HORIZON + 4;
if (age > fallFor) {
if (onWater) x += (age - fallFor) * 0.8;
y = Math.round(y);
}
const fadeOut = age > fallFor ? 1 - Math.min(1, (age - fallFor) / Math.min(3, period - fallFor)) : 1;
if (fadeOut <= 0) continue;
const flip = age < fallFor && Math.sin(fall * 6 + i * 2) > 0;
s.blend(x, y, flip ? light : gold, 0.9 * fadeOut);
if (!flip && age < fallFor) s.blend(x + 1, y, gold, 0.6 * fadeOut);
}
}