import { bayer } from '../../engine/color'; import { noise1, noise2 } from '../../engine/rng'; import { HORIZON, W } from './world'; /** Cloud density sampled once per cached step over the sky. */ export interface CloudField { /** Density per sky pixel (row-major, W x HORIZON). */ d: Float32Array; /** Density above which a pixel is cloud. */ th: number; } /** * Clouds blow in from the east (left) on the rising wind. `gap` tears a ragged opening around the * sun for the last sun break. */ export function computeClouds( drift: number, cover: number, gap: number, sunX: number, sunY: number, out?: CloudField, ): CloudField { const d = out?.d ?? new Float32Array(W * HORIZON); for (let y = 0; y < HORIZON; y++) { // Clouds flatten toward the horizon and drift more slowly there. const p = y / HORIZON; const sx = 52 * (1 - 0.5 * p); const sy = 13 * (1 - 0.6 * p); const xd = drift * (1 - 0.45 * p); for (let x = 0; x < W; x++) { const xs = x - xd; let v = 0.56 * noise2(xs / sx, y / sy, 11) + 0.3 * noise2((xs * 1.3 + 40) / (sx * 0.45), y / (sy * 0.5), 12) + 0.14 * noise2(x / 6 - drift * 0.3, y / 4, 13); // Low cloud piles up over the far ranges first. v += p * p * 0.12; if (gap > 0) { const dx = (x - sunX) * 0.8; const dy = (y - sunY) * 1.3; v -= gap * 0.75 * Math.exp(-(dx * dx + dy * dy) / (2 * 34 * 34)); } d[y * W + x] = v; } } return { d, th: 0.24 + (1 - cover) * 0.56 }; } export function cloudAt(c: CloudField, x: number, y: number): number { const xi = Math.max(0, Math.min(W - 1, Math.round(x))); const yi = Math.max(0, Math.min(HORIZON - 1, Math.round(y))); return c.d[yi * W + xi]; } /** 0 = solid cloud, 1 = clear sky at a point (soft edge). */ export function openness(c: CloudField, x: number, y: number): number { if (y >= HORIZON) return 1; return Math.max(0, Math.min(1, (c.th - cloudAt(c, x, y)) * 9 + 0.5)); } /** Per-pixel cloud tone: 0 clear, 1 lit, 2 mid, 3 shadowed; +4 marks a thin, translucent edge. */ export function shadeClouds(c: CloudField, sunX: number, cover: number, out?: Uint8Array): Uint8Array { const tone = out ?? new Uint8Array(W * HORIZON); for (let y = 0; y < HORIZON; y++) { for (let x = 0; x < W; x++) { const e = c.d[y * W + x] - c.th; if (e <= 0) { tone[y * W + x] = 0; continue; } // Tops and sun-facing sides are lit; undersides and thick middles fall into shade. const toward = Math.sign(sunX - x) || 1; const vertical = cloudAt(c, x, y + 3) - cloudAt(c, x, y - 3); const facing = cloudAt(c, x - toward * 3, y) - cloudAt(c, x + toward * 3, y); const score = 0.5 + vertical * 2.6 + facing * 1.6 - e * 1.3 - cover * 0.1 + (bayer(x, y) - 0.5) * 0.35; tone[y * W + x] = (score > 0.62 ? 1 : score > 0.36 ? 2 : 3) + (e < 0.03 ? 4 : 0); } } return tone; } /** Angles (y down, so 0..π fans downward) covered by the sunbeam table. */ export const RAY_A0 = -0.4; export const RAY_A1 = Math.PI + 0.4; const RAY_BINS = 720; /** Brightness of the beam leaving the sun at each angle: lit where the clouds around the sun are open. */ export function computeRayTable(c: CloudField, sunX: number, sunY: number, t: number, out?: Float32Array): Float32Array { const table = out ?? new Float32Array(RAY_BINS); for (let i = 0; i < RAY_BINS; i++) { const a = RAY_A0 + ((RAY_A1 - RAY_A0) * i) / (RAY_BINS - 1); const ca = Math.cos(a); const sa = Math.sin(a); const open = Math.min(openness(c, sunX + ca * 16, sunY + sa * 16), openness(c, sunX + ca * 28, sunY + sa * 28)); table[i] = open * (0.45 + 0.55 * noise1(a * 36 + t * 0.04, 17)); } return table; } export function rayAt(table: Float32Array, a: number): number { if (a < RAY_A0 || a > RAY_A1) return 0; return table[Math.round(((a - RAY_A0) / (RAY_A1 - RAY_A0)) * (RAY_BINS - 1))]; }