diff --git a/src/app/features/scene/terrain/terrainShaders.ts b/src/app/features/scene/terrain/terrainShaders.ts index 12428d70..5fd01d7b 100644 --- a/src/app/features/scene/terrain/terrainShaders.ts +++ b/src/app/features/scene/terrain/terrainShaders.ts @@ -9,7 +9,7 @@ import phacelle from '../shaders/CultMath.Phacelle.glsl?raw'; import { trimGlsl } from '../cultmathTrim'; import { ALBEDO } from './terrainAlbedo'; import { AERIAL_GLSL, AP_DISTANCE_SCALE, SCATTER_GLSL } from './terrainScatter'; -import { GLOW_ZENITH, MOON_HAZE, SKY_EXPOSURE, STAR_FLOOR } from './terrainSky'; +import { GLOW_ZENITH, MOON_HAZE, SKY_EXPOSURE, SKY_FADE, STAR_FLOOR } from './terrainSky'; import { TERRAIN } from './terrainWorld'; const HEADER = @@ -203,7 +203,10 @@ void main() { vec3 haze = 1.0 - exp(-${lit(SKY_EXPOSURE)} * L) + uNight * ap.a * vec3(${STAR_FLOOR.map( lit ).join(', ')}); - o = vec4(pow(lit * (1.0 - ap.a) + haze, vec3(1.0 / 2.2)), 1.0); + // Past SKY_FADE the surface is the sky at the horizon in this ray's azimuth, the colour the sky pass draws there (terrainSky.aerialPerspective). + float toSky = smoothstep(${lit(SKY_FADE[0])}, ${lit(SKY_FADE[1])}, dist); + vec3 horizon = skyColour(vec3(ray.x, 0.0, ray.z)); + o = vec4(pow(mix(lit * (1.0 - ap.a) + haze, horizon, toSky), vec3(1.0 / 2.2)), 1.0); }`; // The sky behind: the baked scattering tables by the view ray's elevation and azimuth from the sun, a sun disc, and a diff --git a/src/app/features/scene/terrain/terrainSky.test.ts b/src/app/features/scene/terrain/terrainSky.test.ts index 5a8e0502..016ee1ce 100644 --- a/src/app/features/scene/terrain/terrainSky.test.ts +++ b/src/app/features/scene/terrain/terrainSky.test.ts @@ -15,6 +15,7 @@ import { skyNameFor, sunDirection, aerialPerspective, + SKY_FADE, metresToKm, groundLight, HAZE_K, @@ -195,7 +196,7 @@ describe('aerial perspective', () => { it('opacity grows with distance from nothing at the eye toward the far value, never past 1', () => { let prev = along(SKY.noonday, -2, 200, 0).alpha; expect(prev).toBe(0); - for (const m of [10, 100, 400, 1000, 2000, 2500]) { + for (const m of [10, 100, 400, 800, 1000, 1300]) { const a = along(SKY.noonday, -2, 200, m).alpha; expect(a).toBeGreaterThan(prev); expect(a).toBeLessThanOrEqual(1); @@ -205,34 +206,34 @@ describe('aerial perspective', () => { it('leaves the near ground its own colour and takes the far ridges', () => { expect(along(SKY.noonday, -2, 200, 100).alpha).toBeLessThan(0.15); - expect(along(SKY.noonday, -2, 200, 2000).alpha).toBeGreaterThan(0.3); + expect(along(SKY.noonday, -2, 200, SKY_FADE[0]).alpha).toBeGreaterThan(0.1); }); it('the in-scatter is a function of the baked volume: its colour is bluer than the sun far by day', () => { - const far = along(SKY.noonday, -2, 90, 2400).colour; + const far = along(SKY.noonday, -2, 90, SKY_FADE[0]).colour; expect(far[2]).toBeGreaterThan(far[0]); }); it('haze toward a setting sun is brighter than away from it, and red, not blue (the sun is reddened on its way)', () => { const dusk = skyAtSun(2, 270); - const toward = along(dusk, -1, 270, 2000).colour; - const away = along(dusk, -1, 90, 2000).colour; + const toward = along(dusk, -1, 270, SKY_FADE[0]).colour; + const away = along(dusk, -1, 90, SKY_FADE[0]).colour; expect(luma(toward)).toBeGreaterThan(3 * luma(away)); expect(toward[0]).toBeGreaterThan(toward[2]); }); it('night haze is a deep blue floor, not grey: dark, blue over red by a wide margin, brighter than black', () => { - const c = along(SKY.midnight, -2, 0, 2400).colour; - expect(luma(c)).toBeGreaterThan(0.001); + const c = along(SKY.midnight, -2, 0, SKY_FADE[0]).colour; + expect(luma(c)).toBeGreaterThan(0.0003); expect(luma(c)).toBeLessThan(0.08); expect(c[2]).toBeGreaterThan(2.5 * c[0]); - expect(luma(along(SKY.noonday, -2, 0, 2400).colour)).toBeGreaterThan(5 * luma(c)); + expect(luma(along(SKY.noonday, -2, 0, SKY_FADE[0]).colour)).toBeGreaterThan(5 * luma(c)); }); it('moonlight lights the night haze toward the moon more than away from it', () => { const moon = groundLight(SKY.midnight).dir; - const toward = aerialPerspective(SKY.midnight, [moon[0], -0.02, moon[2]], 2400).colour; - const away = aerialPerspective(SKY.midnight, [-moon[0], -0.02, -moon[2]], 2400).colour; + const toward = aerialPerspective(SKY.midnight, [moon[0], -0.02, moon[2]], SKY_FADE[0]).colour; + const away = aerialPerspective(SKY.midnight, [-moon[0], -0.02, -moon[2]], SKY_FADE[0]).colour; expect(luma(toward)).toBeGreaterThan(luma(away)); }); @@ -240,16 +241,32 @@ describe('aerial perspective', () => { const d = SKY.noonday; const view = ray(-4, 200); const dark = (x: number) => luma(shade(d, [0.02, 0.02, 0.02], UP, x, view)); - expect(dark(2000)).toBeGreaterThan(dark(100)); + expect(dark(SKY_FADE[0])).toBeGreaterThan(dark(100)); const own = (x: number) => 1 - aerialPerspective(d, view, x).alpha; - expect(own(2000)).toBeLessThan(0.7 * own(100)); + expect(own(SKY_FADE[0])).toBeLessThan(0.9 * own(100)); }); it('is continuous in the sun elevation through dusk: no step between neighbours', () => { - const at = (e: number) => luma(along(skyAtSun(e, 270), -1, 270, 2000).colour); + const at = (e: number) => luma(along(skyAtSun(e, 270), -1, 270, SKY_FADE[0]).colour); for (let e = -12; e < 12; e += 0.5) expect(Math.abs(at(e + 0.5) - at(e))).toBeLessThan(0.45); }); + it('far terrain is the sky: past the fade it is the horizon sky colour in its azimuth, by day, dusk and night, whatever it was', () => { + for (const [d, az] of [ + [SKY.midnight, 0], + [skyAtSun(-1, 90), 90], + [skyAtSun(2, 270), 200], + [SKY.noonday, 120], + ] as const) { + const view = ray(-0.5, az); + const hz = skyColour(d, [view[0], 0, view[2]]); + const far = aerialPerspective(d, view, SKY_FADE[1]); + expect(far.alpha).toBe(1); + for (let i = 0; i < 3; i += 1) expect(far.colour[i]).toBeCloseTo(hz[i], 9); + expect(shade(d, [0.5, 0.5, 0.5], UP, SKY_FADE[1] + 500, view)).toEqual(far.colour); + } + }); + it('the metres-to-km law is the scene metres times the baked distance scale', () => { expect(metresToKm(1000)).toBeCloseTo(AP_DISTANCE_SCALE, 9); expect(metresToKm(-5)).toBe(0); diff --git a/src/app/features/scene/terrain/terrainSky.ts b/src/app/features/scene/terrain/terrainSky.ts index 2efef429..4034fd93 100644 --- a/src/app/features/scene/terrain/terrainSky.ts +++ b/src/app/features/scene/terrain/terrainSky.ts @@ -149,6 +149,13 @@ export const MOON_HAZE: Vec3 = [0.0004, 0.0007, 0.0018]; /** Starlight and airglow scattered by the air, added to the in-scatter in proportion to alpha, at full night: a deep blue floor, not grey. EYEBALLED. */ export const STAR_FLOOR: Vec3 = [0.0006, 0.0012, 0.0045]; +/** + * Metres from the eye over which the terrain gives itself up to the sky: past the far end a surface is the sky's own + * colour at the horizon in its azimuth (`skyColour` flat along the horizon, night glow included), so the patch's rim + * meets the sky above and beyond it with no seam. The volume alone is only ~40% opaque by then. EYEBALLED. + */ +export const SKY_FADE: readonly [number, number] = [600, 1350]; + /** Haze in front of a surface seen along `dir` (unit, eye to surface) `distance` metres away: the colour it adds and the surface's loss. */ export function aerialPerspective( d: SkyData, @@ -172,10 +179,14 @@ export function aerialPerspective( const moon = aerialAt(Math.abs(d.elevation), el, az, km); for (let i = 0; i < 3; i += 1) L[i] += MOON_HAZE[i] * night * moon.L[i]; } + const horizon = skyColour(d, [dir[0], 0, dir[2]]); + const f = smooth(SKY_FADE[0], SKY_FADE[1], distance); const colour = [0, 1, 2].map( - (i) => 1 - Math.exp(-SKY_EXPOSURE * L[i]) + STAR_FLOOR[i] * night * day.alpha + (i) => + (1 - f) * (1 - Math.exp(-SKY_EXPOSURE * L[i]) + STAR_FLOOR[i] * night * day.alpha) + + f * horizon[i] ) as unknown as Vec3; - return { colour, alpha: day.alpha }; + return { colour, alpha: (1 - f) * day.alpha + f }; } /** Added to the ambient on a dark sky, scaled by `nightness`: the baked night floor is too dark to show any form on a phone. */