scene: terrain backdrop range found by refinement, not a grid
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled

terrainDrawn finds the sun glow peak and the darkest patch with a compass
search from the best coarse samples; constants regenerated (Day 0.237309-0.92805,
Dusk 0.016727-0.967912). The drift test sweeps its own grid (golden-angle spiral
plus a 0.05 degree patch around the sun).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Nibu 2026-11-09 22:41:27 +00:00
commit 8a05668d84
3 changed files with 85 additions and 27 deletions

View file

@ -72,31 +72,43 @@ describe('terrainSkyFor', () => {
});
describe('the terrain sky lies inside skyBackdrop', () => {
// A grid offset from the one skyBackdrop samples, so a miss between its samples shows.
// Not the generator's grid: a golden-angle spiral over the upper sphere (no shared rows or columns)
// plus a 0.05 degree grid across the sun, five times finer than the generator's.
const luma = (c: readonly number[]) => {
const [r, g, b] = c.map((v) => Math.min(1, Math.max(0, v)));
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
};
const sweep = (state: 'day' | 'dusk' | 'night') => {
const sky = chooseSky(terrainSkyFor(state));
const rad = Math.PI / 180;
const dirAt = (el: number, az: number): [number, number, number] => [
Math.cos(el * rad) * Math.sin(az * rad),
Math.sin(el * rad),
-Math.cos(el * rad) * Math.cos(az * rad),
];
const out: number[] = [];
for (let el = 1.3; el <= 90; el += 4.1) {
for (let az = 0.7; az < 360; az += 4.3) {
const e = (el * Math.PI) / 180;
const a = (az * Math.PI) / 180;
const c = skyColour(sky, [
Math.cos(e) * Math.sin(a),
Math.sin(e),
-Math.cos(e) * Math.cos(a),
]);
const [r, g, b] = c.map((v) => Math.min(1, Math.max(0, v)));
out.push(0.2126 * r + 0.7152 * g + 0.0722 * b);
const N = 20000;
for (let i = 0; i < N; i += 1) {
const y = (i + 0.5) / N; // sin(elevation): uniform over the upper hemisphere
out.push(luma(skyColour(sky, dirAt(Math.asin(y) / rad, (i * 137.50776405) % 360))));
}
for (let dEl = -3; dEl <= 3; dEl += 0.05) {
for (let dAz = -3; dAz <= 3; dAz += 0.05) {
out.push(luma(skyColour(sky, dirAt(Math.max(0, sky.elevation + dEl), sky.azimuth + dAz))));
}
}
return out;
};
it.each(['day', 'dusk', 'night'] as const)('for %s', (state) => {
it.each(['day', 'dusk', 'night'] as const)('for %s, and no looser than 5e-4 from it', (state) => {
const { darkest, lightest } = skyBackdrop(state);
const l = sweep(state);
expect(Math.min(...l)).toBeGreaterThanOrEqual(darkest - EPS);
expect(Math.max(...l)).toBeLessThanOrEqual(lightest + EPS);
const top = Math.max(Math.max(...l), ridgeDrawn(state)?.lightest ?? 0);
expect(lightest - top).toBeLessThan(5e-4);
const bottom = Math.min(Math.min(...l), ridgeDrawn(state)?.darkest ?? 1);
expect(bottom - darkest).toBeLessThan(5e-4);
});
it('and the terrain dusk reaches the sun glow the ridge dusk does not', () => {

View file

@ -70,6 +70,10 @@ export function ridgeDrawn(state: SkyState): Range | undefined {
}
const SAMPLE_DEG = 3;
const SUN_DEG = 0.25;
const SUN_SPAN_DEG = 8;
const HORIZON_DEG = 0.5;
const REFINE_FROM = 6;
const linearLuminance = (c: Vec3) => {
const [r, g, b] = c.map((v) => Math.min(1, Math.max(0, v)));
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
@ -77,22 +81,64 @@ const linearLuminance = (c: Vec3) => {
/**
* The terrain sky's range: `skyColour` (the CPU twin of its sky shader) over the whole upper sphere, which the camera
* sweeps as it turns, for the sky the scene picks for this state. Sampled every 3 degrees.
* sweeps as it turns, for the sky the scene picks for this state. A 3 degree grid, a 0.5 degree ring of the horizon, the
* zenith and a 0.25 degree patch around the sun find where the extremes are; each of the best few is then refined by a
* compass search whose step halves from 1 degree to under 0.001, because the sun's glow peaks (and the sky's darkest
* patch sits) between any fixed grid's samples.
*/
export function terrainDrawn(state: SkyState): Range {
const sky = chooseSky(terrainSkyFor(state));
let darkest = Infinity;
let lightest = -Infinity;
const rad = Math.PI / 180;
const lum = (el: number, az: number) => {
const e = Math.min(90, Math.max(0, el)) * rad;
const a = az * rad;
return linearLuminance(
skyColour(sky, [Math.cos(e) * Math.sin(a), Math.sin(e), -Math.cos(e) * Math.cos(a)])
);
};
const seen: { el: number; az: number; l: number }[] = [];
const at = (el: number, az: number) => seen.push({ el, az, l: lum(el, az) });
for (let el = 0; el <= 90; el += SAMPLE_DEG) {
for (let az = 0; az < 360; az += SAMPLE_DEG) {
const e = (el * Math.PI) / 180;
const a = (az * Math.PI) / 180;
const l = linearLuminance(
skyColour(sky, [Math.cos(e) * Math.sin(a), Math.sin(e), -Math.cos(e) * Math.cos(a)])
);
darkest = Math.min(darkest, l);
lightest = Math.max(lightest, l);
for (let az = 0; az < 360; az += SAMPLE_DEG) at(el, az);
}
for (let az = 0; az < 360; az += HORIZON_DEG) at(0, az);
at(90, 0);
for (let dEl = -SUN_SPAN_DEG; dEl <= SUN_SPAN_DEG; dEl += SUN_DEG) {
for (let dAz = -SUN_SPAN_DEG; dAz <= SUN_SPAN_DEG; dAz += SUN_DEG) {
at(sky.elevation + dEl, sky.azimuth + dAz);
}
}
type Point = { el: number; az: number; l: number };
const around = (p: Point, step: number): Point[] =>
[
[step, 0],
[-step, 0],
[0, step],
[0, -step],
].map(([dEl, dAz]) => {
const el = Math.min(90, Math.max(0, p.el + dEl));
return { el, az: p.az + dAz, l: lum(el, p.az + dAz) };
});
const refine = (from: Point, sign: 1 | -1) => {
let best = from;
for (let step = 1; step > 0.0005; step /= 2) {
let improved = true;
while (improved) {
const cur = best;
best = around(cur, step).reduce((b, c) => (sign * c.l > sign * b.l ? c : b), cur);
improved = best !== cur;
}
}
return best.l;
};
const byLight = [...seen].sort((p, q) => q.l - p.l);
const lightest = Math.max(
byLight[0].l,
...byLight.slice(0, REFINE_FROM).map((p) => refine(p, 1))
);
const darkest = Math.min(
byLight[byLight.length - 1].l,
...byLight.slice(-REFINE_FROM).map((p) => refine(p, -1))
);
return { darkest, lightest };
}

View file

@ -62,9 +62,9 @@ export interface SkyBackdrop {
// Rounded outward to six places. 'auto' follows the clock (and a URL), so it
// bounds all three fixed states.
const BACKDROPS: Record<SkyState, SkyBackdrop> = {
auto: { darkest: 0.016727, lightest: 0.964723 },
day: { darkest: 0.237514, lightest: 0.919226 },
dusk: { darkest: 0.016727, lightest: 0.964723 },
auto: { darkest: 0.016727, lightest: 0.967912 },
day: { darkest: 0.237309, lightest: 0.92805 },
dusk: { darkest: 0.016727, lightest: 0.967912 },
night: { darkest: 0.058099, lightest: 0.077467 },
};