130 lines
4.9 KiB
TypeScript
130 lines
4.9 KiB
TypeScript
import { bayer } from '../../engine/color';
|
|
import { noise1, noise2 } from '../../engine/rng';
|
|
import { H, HORIZON, W } from './world';
|
|
|
|
/** Cloud density sampled once per frame over the sky, shared by the sky, god rays and sea. */
|
|
export interface CloudField {
|
|
/** Density per sky pixel (row-major, W x HORIZON). */
|
|
d: Float32Array;
|
|
/** Density above which a pixel is cloud. */
|
|
th: number;
|
|
}
|
|
|
|
export function computeClouds(t: number, cover: number, sunX: number, sunY: number, out?: CloudField): CloudField {
|
|
const d = out?.d ?? new Float32Array(W * HORIZON);
|
|
const drift = t * 1.1;
|
|
for (let y = 0; y < HORIZON; y++) {
|
|
// Clouds flatten and shrink toward the horizon, and drift more slowly there.
|
|
const p = y / HORIZON;
|
|
const sx = 46 * (1 - 0.55 * p);
|
|
const sy = 14 * (1 - 0.7 * p);
|
|
const xd = drift * (1 - 0.5 * p);
|
|
for (let x = 0; x < W; x++) {
|
|
const xs = x + xd;
|
|
let v =
|
|
0.58 * noise2(xs / sx, y / sy, 1) +
|
|
0.3 * noise2((xs * 1.3 + 40) / (sx * 0.45), y / (sy * 0.5), 2) +
|
|
0.12 * noise2(x / 6 + drift * 0.3, y / 4, 3);
|
|
// A ragged opening around the sun lets the light break through.
|
|
const dx = x - sunX;
|
|
const dy = (y - sunY) * 1.4;
|
|
v -= 0.13 * Math.exp(-(dx * dx + dy * dy) / (2 * 26 * 26));
|
|
d[y * W + x] = v;
|
|
}
|
|
}
|
|
return { d, th: 0.32 + (1 - cover) * 0.5 };
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
/** Angles (y down, so 0..π fans downward) covered by the god-ray table. */
|
|
export const RAY_A0 = -0.4;
|
|
export const RAY_A1 = Math.PI + 0.4;
|
|
const RAY_BINS = 720;
|
|
|
|
/**
|
|
* Brightness of the god ray leaving the sun at each angle: lit where the clouds just
|
|
* around the sun are open, broken into streaks.
|
|
*/
|
|
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 gap = Math.min(openness(c, sunX + ca * 18, sunY + sa * 18), openness(c, sunX + ca * 30, sunY + sa * 30));
|
|
table[i] = gap * (0.5 + 0.5 * noise1(a * 40 + t * 0.05, 7));
|
|
}
|
|
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))];
|
|
}
|
|
|
|
/** 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.4 - cover * 0.12 + (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;
|
|
}
|
|
|
|
/**
|
|
* Cloud light and shadow on the water and sand below the horizon: sampled coarsely (every 4 px)
|
|
* and smoothed to full resolution, once per cached cloud step.
|
|
*/
|
|
const STEP = 4;
|
|
const CW = W / STEP + 1;
|
|
const CH = Math.ceil((H - HORIZON) / STEP) + 1;
|
|
const coarse = new Float32Array(CW * CH);
|
|
|
|
export function computeGroundLight(c: CloudField, out?: Float32Array): Float32Array {
|
|
const rows = H - HORIZON;
|
|
const light = out ?? new Float32Array(W * rows);
|
|
for (let gy = 0; gy < CH; gy++) {
|
|
for (let gx = 0; gx < CW; gx++) {
|
|
const x = gx * STEP;
|
|
const y = HORIZON + gy * STEP;
|
|
coarse[gy * CW + gx] = 0.86 + 0.2 * openness(c, x * 0.8 + 30, 18 + ((y - HORIZON) / rows) * 46);
|
|
}
|
|
}
|
|
for (let y = 0; y < rows; y++) {
|
|
const gy = Math.floor(y / STEP);
|
|
const fy = (y % STEP) / STEP;
|
|
for (let x = 0; x < W; x++) {
|
|
const gx = Math.floor(x / STEP);
|
|
const fx = (x % STEP) / STEP;
|
|
const i = gy * CW + gx;
|
|
const top = coarse[i] + (coarse[i + 1] - coarse[i]) * fx;
|
|
const bottom = coarse[i + CW] + (coarse[i + CW + 1] - coarse[i + CW]) * fx;
|
|
light[y * W + x] = top + (bottom - top) * fy;
|
|
}
|
|
}
|
|
return light;
|
|
}
|