diff --git a/src/app/features/scene/skyForState.test.ts b/src/app/features/scene/skyForState.test.ts index fb95dc0a..aec50c12 100644 --- a/src/app/features/scene/skyForState.test.ts +++ b/src/app/features/scene/skyForState.test.ts @@ -5,6 +5,7 @@ import { skyColours } from './ridge/skyColour'; import { chooseSky, skyColour } from './terrain/terrainSky'; import { DUSK_SUN_ELEVATION, + TERRAIN_REACH_DEG, ridgeDrawn, ridgeSkyFor, terrainDrawn, @@ -13,6 +14,7 @@ import { import { displayedLuminance, SKY_BAND } from './skyBand'; import type { FixedSkyState } from './skyBand'; import { setSkyClock, skyBackdrop } from './skyState'; +import { view } from './terrain/terrainWorld'; import { skyThemeId } from './skyTheme'; const hex = (c: readonly number[]) => `#${c.map((v) => v.toString(16).padStart(2, '0')).join('')}`; @@ -80,6 +82,27 @@ describe('ridgeSkyFor', () => { }); }); +describe('the terrain camera reach', () => { + it('covers the highest ray the frozen camera sees, at any aspect, with a small margin', () => { + const tanHalf = Math.tan((55 * Math.PI) / 360); + const tops = [0.4, 0.5, 1, 16 / 9, 2.4].flatMap((aspect) => + [0, 0.125, 0.25, 0.5, 0.75].flatMap((turns) => { + const v = view(aspect, turns); + return [-1, 0, 1].map((nx) => { + const d = [0, 1, 2].map( + (i) => v.forward[i] + nx * tanHalf * aspect * v.right[i] + tanHalf * v.up[i] + ); + return (Math.asin(d[1] / Math.hypot(d[0], d[1], d[2])) * 180) / Math.PI; + }); + }) + ); + const top = Math.max(...tops); + expect(top).toBeCloseTo(14.5, 1); + expect(TERRAIN_REACH_DEG).toBeGreaterThanOrEqual(top); + expect(TERRAIN_REACH_DEG - top).toBeLessThan(2); + }); +}); + describe('terrainSkyFor', () => { it('draws the sky auto resolves to, with that state band', () => { setSkyClock(() => new Date(2026, 8, 22, 12, 0)); @@ -108,11 +131,11 @@ const dirAt = (el: number, az: number): [number, number, number] => { -Math.cos(el * rad) * Math.cos(az * rad), ]; }; -const sweepOf = (sky: ReturnType, N = 4000) => { +const sweepOf = (sky: ReturnType, N = 4000, maxEl = TERRAIN_REACH_DEG) => { const rad = Math.PI / 180; const out: number[] = []; for (let i = 0; i < N; i += 1) { - const y = (i + 0.5) / N; // sin(elevation): uniform over the upper hemisphere + const y = ((i + 0.5) / N) * Math.sin(maxEl * rad); // sin(elevation): uniform from the horizon to maxEl out.push( displayedLuminance(skyColour(sky, dirAt(Math.asin(y) / rad, (i * 137.50776405) % 360))) ); @@ -121,7 +144,10 @@ const sweepOf = (sky: ReturnType, N = 4000) => { for (let dAz = -3; dAz <= 3; dAz += 0.05) { out.push( displayedLuminance( - skyColour(sky, dirAt(Math.max(0, sky.elevation + dEl), sky.azimuth + dAz)) + skyColour( + sky, + dirAt(Math.min(maxEl, Math.max(0, sky.elevation + dEl)), sky.azimuth + dAz) + ) ) ); } @@ -132,6 +158,13 @@ const sweepOf = (sky: ReturnType, N = 4000) => { describe('the terrain sky lies in the band its text ink needs', () => { const sweep = (state: FixedSkyState) => sweepOf(chooseSky(terrainSkyFor(state))); + it('dusk needs its lift: the scatter sky alone dips below the text bound within the reach', () => { + const { sun } = terrainSkyFor('dusk'); + const l = sweepOf(chooseSky({ sun })); + expect(Math.min(...l)).toBeLessThan(needs('dusk').floor - 0.02); + expect(Math.min(...sweep('dusk'))).toBeGreaterThanOrEqual(needs('dusk').floor); + }); + it.each(STATES)('%s', (state) => { const { floor, ceiling } = needs(state); const l = sweep(state); @@ -142,7 +175,7 @@ describe('the terrain sky lies in the band its text ink needs', () => { it.each(STATES)('%s holds for a sun at any elevation, as ?sun= can put it', (state) => { const { floor, ceiling } = needs(state); for (let e = -90; e <= 90; e += 10) { - const l = sweepOf(chooseSky({ sun: { elevation: e }, band: SKY_BAND[state] })); + const l = sweepOf(chooseSky({ sun: { elevation: e }, band: SKY_BAND[state] }), 4000, 90); expect(Math.min(...l)).toBeGreaterThanOrEqual(floor); expect(Math.max(...l)).toBeLessThanOrEqual(ceiling); } diff --git a/src/app/features/scene/skyForState.ts b/src/app/features/scene/skyForState.ts index 0de0dbe0..e7abcd73 100644 --- a/src/app/features/scene/skyForState.ts +++ b/src/app/features/scene/skyForState.ts @@ -13,6 +13,7 @@ import type { SkyState } from './skyState'; import { displayedLuminance, SKY_BAND } from './skyBand'; import type { SkyBand } from './skyBand'; import { luminance } from '../../../sky/skyTokens.js'; +import { CAMERA } from './terrain/terrainWorld'; const rgb = (hex: string): Srgb => [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16)) as unknown as Srgb; @@ -89,6 +90,14 @@ export function ridgeDrawn(state: SkyState): Range { return { darkest: Math.min(...l), lightest: Math.max(...l) }; } +/** + * How high the terrain camera can see, in degrees of elevation: it is frozen at a downward pitch and only turns + * (no pull-back), so the top edge of its view, at the centre column, is the pitch down subtracted from half the + * field of view (14.5 degrees; off-centre columns look lower). Plus a 1.5 degree margin. The terrain's range is + * over 0 to this, all azimuths, not the whole upper sphere; below the horizon the sky is the horizon's colour. + */ +export const TERRAIN_REACH_DEG = Math.ceil((CAMERA.fov / 2 - CAMERA.pitch) * (180 / Math.PI) + 1.5); + const SAMPLE_DEG = 3; const SUN_DEG = 0.25; const SUN_SPAN_DEG = 8; @@ -96,9 +105,8 @@ const HORIZON_DEG = 0.5; const REFINE_FROM = 6; /** - * The terrain sky's range, as displayed (`displayedLuminance`): `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. 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 + * The terrain sky's range, as displayed (`displayedLuminance`): `skyColour` (the CPU twin of its sky shader) over the part of the sky the camera reaches (0 to TERRAIN_REACH_DEG, every azimuth: it turns), for the sky the scene picks for this state. A 3 degree grid, a 0.5 degree ring of the horizon, the + * top of the reach 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. */ @@ -106,7 +114,7 @@ export function terrainDrawn(state: SkyState): Range { const sky = chooseSky(terrainSkyFor(state)); const rad = Math.PI / 180; const lum = (el: number, az: number) => { - const e = Math.min(90, Math.max(0, el)) * rad; + const e = Math.min(TERRAIN_REACH_DEG, Math.max(0, el)) * rad; const a = az * rad; return displayedLuminance( skyColour(sky, [Math.cos(e) * Math.sin(a), Math.sin(e), -Math.cos(e) * Math.cos(a)]) @@ -114,11 +122,11 @@ export function terrainDrawn(state: SkyState): Range { }; 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 el = 0; el <= TERRAIN_REACH_DEG; el += SAMPLE_DEG) { 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); + at(TERRAIN_REACH_DEG, 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); @@ -132,7 +140,7 @@ export function terrainDrawn(state: SkyState): Range { [0, step], [0, -step], ].map(([dEl, dAz]) => { - const el = Math.min(90, Math.max(0, p.el + dEl)); + const el = Math.min(TERRAIN_REACH_DEG, 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) => { diff --git a/src/app/features/scene/skyState.ts b/src/app/features/scene/skyState.ts index ce31fb23..c27c10a4 100644 --- a/src/app/features/scene/skyState.ts +++ b/src/app/features/scene/skyState.ts @@ -117,11 +117,12 @@ export interface SkyBackdrop { // The union of every sky a scene draws for the state: the ridge sky and the terrain sky, each from // the function its scene uses (`ridgeDrawn`, `terrainDrawn` in skyForState.ts). Held as constants so // this module, which the main chunk loads, does not pull in the terrain sky and the baked scattering -// tables with it; skyForState.test.ts fails when they drift from the computed union. +// tables with it; skyForState.test.ts fails when they drift from the computed union. The ridge sky counts +// over its whole range, the terrain sky over what its frozen camera reaches (TERRAIN_REACH_DEG, skyForState.ts). // Rounded outward to six places. const BACKDROPS: Record = { - day: { darkest: 0.304139, lightest: 0.925759 }, - dusk: { darkest: 0.303833, lightest: 0.967298 }, + day: { darkest: 0.466725, lightest: 0.892734 }, + dusk: { darkest: 0.303835, lightest: 0.967298 }, night: { darkest: 0.05987, lightest: 0.079657 }, }; diff --git a/src/app/features/scene/skyTheme.test.ts b/src/app/features/scene/skyTheme.test.ts index 027647da..4f237fc6 100644 --- a/src/app/features/scene/skyTheme.test.ts +++ b/src/app/features/scene/skyTheme.test.ts @@ -49,12 +49,12 @@ describe('skyThemeId', () => { describe('ink of the theme a state picks, on bare sky', () => { // Per state: the picked theme, its text ink and link, and their WCAG ratios on the sky the scenes - // draw (skyBackdrop, the worst case over its band). Text clears 4.5 in every state. Link does not: - // the links are derived in src/sky/skyTokens.js, outside the band, and these numbers are recorded - // as they are measured (Night's #609385 would clear 4.5 at a luminance of about 0.53 or more). + // draw (skyBackdrop, the worst case over its band). Text clears 4.5 in every state. Link clears + // on day and not on dusk or night: the links are derived in src/sky/skyTokens.js, outside the band, and these + // numbers are recorded as they are measured (Night's #609385 would clear 4.5 at a luminance of about 0.53 or more). // 'auto' resolves from the clock to one of these three and has the same numbers. const cells: [FixedSkyState, string, string, string, string, string][] = [ - ['day', 'noonday-sky', '#342d25', '4.58', '#2a4f27', '3.15'], + ['day', 'noonday-sky', '#342d25', '6.68', '#2a4f27', '4.60'], ['dusk', 'noonday-sky', '#342d25', '4.57', '#2a4f27', '3.15'], ['night', 'midnight-sky', '#aeccff', '4.97', '#609385', '2.32'], ]; @@ -70,9 +70,15 @@ describe('ink of the theme a state picks, on bare sky', () => { } ); - it.each(cells)('%s: text clears AA, link is the known shortfall', (state, id, ink, _w, link) => { + it.each(cells)('%s: text clears AA', (state, _id, ink) => { expect(ratio(ink, skyBackdrop(state))).toBeGreaterThanOrEqual(AA); - expect(ratio(link, skyBackdrop(state))).toBeLessThan(AA); + }); + + it('link clears AA on day (the terrain camera sees only the lighter sky) and falls short on dusk and night', () => { + const link = (s: FixedSkyState, id: keyof typeof SKY) => ratio(SKY[id].link, skyBackdrop(s)); + expect(link('day', 'noonday-sky')).toBeGreaterThanOrEqual(AA); + expect(link('dusk', 'noonday-sky')).toBeLessThan(AA); + expect(link('night', 'midnight-sky')).toBeLessThan(AA); }); it.each([