Tone curve: fitted monotone curve with a small black lift replaces the additive lift and the 0.47 day floor; night ambient
Per-sky monotone cubic through five knots (black lift, p5/p50/p95 of the sky grid to target luminances, white or the night ceiling), shared by sky and cards. Night gets a fixed-knot curve and a RidgeLook.nightAmbient term. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
fa8f2a163b
commit
3232c866ca
7 changed files with 274 additions and 126 deletions
|
|
@ -15,7 +15,7 @@ import type { RidgeLook } from './lightCards';
|
|||
import { parseManifest } from './manifest';
|
||||
import { wireManifest } from './manifest.fixture';
|
||||
import type { PhysicalSky } from './physicalSky';
|
||||
import { ACES_FIT, ACES_IN, ACES_OUT } from './toneMap';
|
||||
import { ACES_FIT, ACES_IN, ACES_OUT, LIFT_ONLY } from './toneMap';
|
||||
|
||||
const manifest = parseManifest(wireManifest(), '/d/');
|
||||
const RAD = Math.PI / 180;
|
||||
|
|
@ -93,13 +93,13 @@ describe('the lighting inputs', () => {
|
|||
({
|
||||
sun: { azimuth: 214, elevation },
|
||||
atm: a,
|
||||
fit: { display: { exposure: 12, lift: 0.01 } },
|
||||
fit: { display: { exposure: 12, curve: LIFT_ONLY } },
|
||||
view: manifest.view,
|
||||
} as unknown as PhysicalSky);
|
||||
|
||||
it("takes the display of the sky's own fit, so terrain and sky share one exposure", () => {
|
||||
const l = lightingOf(manifest, sky(30));
|
||||
expect([l.exposure, l.lift]).toEqual([12, 0.01]);
|
||||
expect([l.exposure, l.curve]).toEqual([12, LIFT_ONLY]);
|
||||
expect(l.ambient).toEqual([0.1, 0.2, 0.3]);
|
||||
expect(l.logDMin).toBe(4.6);
|
||||
expect(l.logDSpan).toBeCloseTo(11.5 - 4.6, 12);
|
||||
|
|
@ -193,7 +193,7 @@ describe('the look reaches the shader', () => {
|
|||
irradiance: { direct: [0, 0, 0], ambient: [0.1, 0.2, 0.3] },
|
||||
radiance: (_a: number, _b: number, o: number[]) => o.fill(0),
|
||||
},
|
||||
fit: { display: { exposure: 1, lift: 0 } },
|
||||
fit: { display: { exposure: 1, curve: LIFT_ONLY } },
|
||||
view: manifest.view,
|
||||
} as unknown as PhysicalSky;
|
||||
const one = { ...manifest, cards: manifest.cards.slice(0, 1) };
|
||||
|
|
@ -258,6 +258,36 @@ function sky30(): PhysicalSky {
|
|||
transmittanceToSpace: (_m: number, o: number[]) => o.fill(1),
|
||||
irradiance: { direct: [0, 0, 0], ambient: [0, 0, 0] },
|
||||
},
|
||||
fit: { display: { exposure: 1, lift: 0 } },
|
||||
fit: { display: { exposure: 1, curve: LIFT_ONLY } },
|
||||
} as unknown as PhysicalSky;
|
||||
}
|
||||
|
||||
describe('night ambient', () => {
|
||||
const nightAtm = {
|
||||
observerKm: 0.183,
|
||||
mieScale: 1,
|
||||
transmittanceToSpace: (_m: number, o: number[]) => o.fill(1),
|
||||
irradiance: { direct: [0, 0, 0], ambient: [0.001, 0.001, 0.002] },
|
||||
radiance: (_a: number, _b: number, o: number[]) => o.fill(0),
|
||||
};
|
||||
const night = (state: string, nightAmbient?: [number, number, number]) =>
|
||||
lightingOf(
|
||||
manifest,
|
||||
{
|
||||
state,
|
||||
sun: { azimuth: 0, elevation: -30 },
|
||||
atm: nightAtm,
|
||||
fit: { display: { exposure: 1, curve: LIFT_ONLY } },
|
||||
view: manifest.view,
|
||||
} as unknown as PhysicalSky,
|
||||
nightAmbient ? { ...DEFAULT_LOOK, nightAmbient } : DEFAULT_LOOK
|
||||
);
|
||||
it('lights the ground at night, cool, and only at night', () => {
|
||||
const n = night('night');
|
||||
const base = nightAtm.irradiance.ambient;
|
||||
n.ambient.forEach((a, i) => expect(a).toBeGreaterThan(base[i]));
|
||||
expect(n.ambient[2] - base[2]).toBeGreaterThan(n.ambient[0] - base[0]);
|
||||
expect(night('dusk').ambient).toEqual(base);
|
||||
expect(DEFAULT_LOOK.nightAmbient.every((c) => c > 0)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,13 +6,14 @@
|
|||
// Per pixel: the normal from the geometry card in the column's frame (r along the ray, t across it, up), the sun brought into that frame per column;
|
||||
// radiance = albedo x (sun transmittance x max(n.s, 0) + ambient irradiance) / pi; aerial perspective L T(d) + Lsky (1 - T(d)) with d from the
|
||||
// geometry's log-distance and the sky's own extinction at the ground; contours; then the sky's own display (exposure, ACES with `toneMap.ts`'s
|
||||
// constants written into the shader, black lift, sRGB), so terrain and sky share one exposure. Wall pixels (R = G = 255 in the geometry file, a code
|
||||
// constants written into the shader, the sky's fitted curve, sRGB), so terrain and sky share one exposure. Wall pixels (R = G = 255 in the geometry file, a code
|
||||
// no real normal reaches) are the extruded face under a ridge: they are lit with the face up and wear no crease ink.
|
||||
import { extinctionAt } from './earthAtmosphere';
|
||||
import type { Vec3 } from './earthAtmosphere';
|
||||
import type { RidgeManifest } from './manifest';
|
||||
import type { PhysicalSky } from './physicalSky';
|
||||
import { ACES_FIT, ACES_IN, ACES_OUT } from './toneMap';
|
||||
import { ACES_FIT, ACES_IN, ACES_OUT, KNOTS } from './toneMap';
|
||||
import type { Curve } from './toneMap';
|
||||
import type { View } from './view';
|
||||
|
||||
export interface CardBitmaps {
|
||||
|
|
@ -70,6 +71,8 @@ export interface RidgeLook {
|
|||
groundNoise: number;
|
||||
/** Low-frequency noise darkening of the direct sun (static). 0 = none. */
|
||||
cloudShadows: number;
|
||||
/** Night's moonlight on the ground: the radiance of a white card in it (rgb, at the night exposure, before the curve); added only in the night state, where the atmosphere gives the ground none. A dim cool value. */
|
||||
nightAmbient: readonly [number, number, number];
|
||||
}
|
||||
|
||||
export const DEFAULT_LOOK: RidgeLook = {
|
||||
|
|
@ -89,6 +92,7 @@ export const DEFAULT_LOOK: RidgeLook = {
|
|||
inkScale: 1,
|
||||
groundNoise: 0,
|
||||
cloudShadows: 0,
|
||||
nightAmbient: [0.5, 0.66, 1.1],
|
||||
};
|
||||
|
||||
const RAD = Math.PI / 180;
|
||||
|
|
@ -132,10 +136,12 @@ export interface Lighting {
|
|||
sunTransmittance: Vec3;
|
||||
ambient: Vec3;
|
||||
exposure: number;
|
||||
lift: number;
|
||||
/** The sky's fitted curve (`toneMap.ts`), applied per channel after ACES. */
|
||||
curve: Curve;
|
||||
}
|
||||
|
||||
const scale3 = (v: Vec3, k: number): Vec3 => [v[0] * k, v[1] * k, v[2] * k];
|
||||
const add3 = (a: Vec3, b: Vec3): Vec3 => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
|
||||
|
||||
export function lightingOf(
|
||||
manifest: RidgeManifest,
|
||||
|
|
@ -153,9 +159,12 @@ export function lightingOf(
|
|||
logDSpan: manifest.encoding.logDMax - manifest.encoding.logDMin,
|
||||
extinction: scale3(extinctionAt(sky.atm.observerKm, sky.atm.mieScale), look.hazeScale),
|
||||
sunTransmittance: lit ? [trans[0], trans[1], trans[2]] : [0, 0, 0],
|
||||
ambient: scale3(sky.atm.irradiance.ambient, look.ambientScale),
|
||||
ambient: add3(
|
||||
scale3(sky.atm.irradiance.ambient, look.ambientScale),
|
||||
sky.state === 'night' ? scale3(look.nightAmbient as Vec3, Math.PI) : [0, 0, 0]
|
||||
),
|
||||
exposure: sky.fit.display.exposure,
|
||||
lift: sky.fit.display.lift,
|
||||
curve: sky.fit.display.curve,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -206,7 +215,10 @@ uniform vec2 uLogD;
|
|||
uniform vec3 uExt;
|
||||
uniform vec3 uTSun;
|
||||
uniform vec3 uAmbient;
|
||||
uniform vec2 uDisplay;
|
||||
uniform float uExposure;
|
||||
uniform float uCx[${KNOTS}];
|
||||
uniform float uCy[${KNOTS}];
|
||||
uniform float uCm[${KNOTS}];
|
||||
uniform vec2 uInkGeometry; // occludeLogD, creaseCos
|
||||
uniform vec3 uInkKeep; // edge, occlude, crease
|
||||
uniform float uInkScale;
|
||||
|
|
@ -247,6 +259,20 @@ vec3 aces(vec3 x) {
|
|||
return clamp(vec3(dot(OUT_R0, p), dot(OUT_R1, p), dot(OUT_R2, p)), 0.0, 1.0);
|
||||
}
|
||||
|
||||
// toneMap.ts applyCurve: the monotone cubic Hermite through the sky's five knots.
|
||||
float curve1(float v) {
|
||||
float x = clamp(v, 0.0, 1.0);
|
||||
int k = 0;
|
||||
for (int i = 1; i < ${KNOTS - 1}; i++) if (x > uCx[i]) k = i;
|
||||
float h = uCx[k + 1] - uCx[k];
|
||||
float t = (x - uCx[k]) / h;
|
||||
float t2 = t * t;
|
||||
float t3 = t2 * t;
|
||||
return (2.0 * t3 - 3.0 * t2 + 1.0) * uCy[k] + (t3 - 2.0 * t2 + t) * h * uCm[k]
|
||||
+ (-2.0 * t3 + 3.0 * t2) * uCy[k + 1] + (t3 - t2) * h * uCm[k + 1];
|
||||
}
|
||||
vec3 curve(vec3 c) { return vec3(curve1(c.r), curve1(c.g), curve1(c.b)); }
|
||||
|
||||
vec3 skyAt(vec2 uv) {
|
||||
vec2 f = uv * vec2(uSkySize) - 0.5;
|
||||
vec2 b = floor(f);
|
||||
|
|
@ -329,8 +355,7 @@ void main() {
|
|||
}
|
||||
radiance *= 1.0 - (1.0 - keep) * uInkScale;
|
||||
|
||||
vec3 lin = aces(radiance * uDisplay.x);
|
||||
vec3 rgb = toSrgb(uDisplay.y + (1.0 - uDisplay.y) * lin);
|
||||
vec3 rgb = toSrgb(curve(aces(radiance * uExposure)));
|
||||
outColor = vec4(rgb * c.a, c.a);
|
||||
}
|
||||
`;
|
||||
|
|
@ -426,7 +451,10 @@ export function lightCards(
|
|||
gl.uniform3f(u('uExt'), ...lit.extinction);
|
||||
gl.uniform3f(u('uTSun'), ...lit.sunTransmittance);
|
||||
gl.uniform3f(u('uAmbient'), ...lit.ambient);
|
||||
gl.uniform2f(u('uDisplay'), lit.exposure, lit.lift);
|
||||
gl.uniform1f(u('uExposure'), lit.exposure);
|
||||
gl.uniform1fv(u('uCx'), lit.curve.x);
|
||||
gl.uniform1fv(u('uCy'), lit.curve.y);
|
||||
gl.uniform1fv(u('uCm'), lit.curve.m);
|
||||
gl.uniform2f(u('uInkGeometry'), look.occludeLogD, look.creaseCos);
|
||||
gl.uniform3f(u('uInkKeep'), look.inkKeep.edge, look.inkKeep.occlude, look.inkKeep.crease);
|
||||
gl.uniform1f(u('uInkScale'), look.inkScale);
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { FALLBACK_VIEW } from './view';
|
|||
import { ELSEWHERE_VIEW } from './manifest.fixture';
|
||||
import { sunForState } from './sunAt';
|
||||
import { makePhysicalSky, paintSky } from './physicalSky';
|
||||
import { skyBackdrop } from '../skyState';
|
||||
import { ridgeDrawn } from '../skyForState';
|
||||
import type { FixedSkyState } from '../skyState';
|
||||
import { windowFor } from './view';
|
||||
import type { PhysicalSky } from './physicalSky';
|
||||
|
|
@ -38,7 +38,7 @@ describe.each(['day', 'dusk', 'night'] as FixedSkyState[])(
|
|||
'the %s sky stays inside its backdrop bounds',
|
||||
(state) => {
|
||||
it('over weather, season and viewport shape', () => {
|
||||
const b = skyBackdrop(state);
|
||||
const b = ridgeDrawn(state);
|
||||
let lo = 2;
|
||||
let hi = -1;
|
||||
for (const day of DAYS) {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import type { Atmosphere } from './earthAtmosphere';
|
|||
import { domeSize, renderDome, SUN_RADIUS, sunOnScreen } from './skyDome';
|
||||
import type { Dome } from './skyDome';
|
||||
import type { Srgb } from './skyColour';
|
||||
import { display8, fitDisplay, holdBudget, MARGIN } from './toneMap';
|
||||
import { display8, fitDisplay, MARGIN } from './toneMap';
|
||||
import type { Display, Fit } from './toneMap';
|
||||
import { sunForState } from './sunAt';
|
||||
import type { SunSample } from './sunAt';
|
||||
|
|
@ -99,7 +99,7 @@ export function paintSky(p: PhysicalSky, cssW: number, cssH: number, camera: Cam
|
|||
const dome = renderDome(p.atm, p.sun, camera, w, h, p.weather.cover);
|
||||
const data = new Uint8ClampedArray(w * h * 4);
|
||||
const out = [0, 0, 0];
|
||||
const display = holdBudget(p.state, dome.hdr, p.fit.display);
|
||||
const display = p.fit.display;
|
||||
for (let i = 0; i < w * h; i += 1) {
|
||||
display8(dome.hdr[i * 3], dome.hdr[i * 3 + 1], dome.hdr[i * 3 + 2], display, out);
|
||||
data[i * 4] = out[0];
|
||||
|
|
|
|||
65
src/app/features/scene/ridge/toneMap.test.ts
Normal file
65
src/app/features/scene/ridge/toneMap.test.ts
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import { FALLBACK_VIEW } from './view';
|
||||
import { makePhysicalSky } from './physicalSky';
|
||||
import { renderDome } from './skyDome';
|
||||
import { windowFor } from './view';
|
||||
import { applyCurve, BLACK_LIFT, BUDGET, NIGHT_LIFT, displayLumaOf, fitDisplay, makeCurve } from './toneMap';
|
||||
import type { FixedSkyState } from '../skyState';
|
||||
|
||||
const sweep = (c: ReturnType<typeof makeCurve>) =>
|
||||
Array.from({ length: 401 }, (_, i) => applyCurve(c, i / 400));
|
||||
|
||||
describe('the tone curve', () => {
|
||||
it('is monotone, starts at the black lift and ends at the top knot, for a spread of knots', () => {
|
||||
const cases: [number[], number[]][] = [
|
||||
[[0, 0.01, 0.2, 0.7, 1], [BLACK_LIFT, 0.12, 0.5, 0.9, 1]],
|
||||
[[0, 0.0005, 0.003, 0.05, 1], [BLACK_LIFT, 0.0225, 0.04, 0.043, 0.043]],
|
||||
[[0, 0.3, 0.31, 0.32, 1], [BLACK_LIFT, 0.2, 0.5, 0.8, 1]],
|
||||
];
|
||||
for (const [x, y] of cases) {
|
||||
const c = makeCurve(x, y);
|
||||
const v = sweep(c);
|
||||
expect(v[0]).toBeCloseTo(BLACK_LIFT, 12);
|
||||
expect(v[400]).toBeCloseTo(y[4], 12);
|
||||
for (let i = 1; i < v.length; i += 1) expect(v[i]).toBeGreaterThanOrEqual(v[i - 1] - 1e-12);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe.each(['day', 'dusk', 'night'] as FixedSkyState[])('the %s curve fitted to a sky', (state) => {
|
||||
const sky = makePhysicalSky(state, new Date('2026-06-21T10:40:00Z'), 0, FALLBACK_VIEW);
|
||||
const { camera } = windowFor(sky.view, 64, 36);
|
||||
const dome = renderDome(sky.atm, sky.sun, camera, 64, 36, sky.weather.cover);
|
||||
const b = BUDGET[state];
|
||||
|
||||
it('has the black lift constant at black and every texel inside the state budget', () => {
|
||||
const c = sky.fit.display.curve;
|
||||
expect(applyCurve(c, 0)).toBe(b.lift);
|
||||
expect(b.lift).toBe(state === 'night' ? NIGHT_LIFT : BLACK_LIFT);
|
||||
expect(sky.fit.lo).toBeGreaterThanOrEqual(b.floor);
|
||||
expect(sky.fit.hi).toBeLessThanOrEqual(b.ceiling + 0.004);
|
||||
const v = sweep(c);
|
||||
for (let i = 1; i < v.length; i += 1) expect(v[i]).toBeGreaterThanOrEqual(v[i - 1] - 1e-12);
|
||||
});
|
||||
|
||||
it('puts the curve through the targets at the fixed knots (a night sky carries nothing to fit)', () => {
|
||||
if (!b.knots) return;
|
||||
b.knots.forEach((k, i) => expect(applyCurve(sky.fit.display.curve, k)).toBeCloseTo(b.targets[i], 9));
|
||||
});
|
||||
|
||||
it.skipIf(b.knots !== undefined)('puts the sky percentiles on the targets (as far as the gain limit allows)', () => {
|
||||
const n = dome.hdr.length / 3;
|
||||
const ls: number[] = [];
|
||||
for (let i = 0; i < n; i += 1)
|
||||
ls.push(displayLumaOf(dome.hdr[i * 3], dome.hdr[i * 3 + 1], dome.hdr[i * 3 + 2], sky.fit.display));
|
||||
ls.sort((p, q) => p - q);
|
||||
const at = (q: number) => ls[Math.floor(q * n)];
|
||||
const flat = fitDisplay(state, dome.hdr, dome.meanLuma);
|
||||
expect(flat.display).toEqual(sky.fit.display);
|
||||
// The curve is fitted on luminance and applied per channel, so the byte luminance is near its targets, not on them. The median is never limited; the outer two are at or below their targets by the gain limit only.
|
||||
expect(at(0.5)).toBeGreaterThan(b.targets[1] * 0.75);
|
||||
expect(at(0.5)).toBeLessThan(b.targets[1] * 1.25);
|
||||
expect(at(0.95)).toBeLessThanOrEqual(b.targets[2] * 1.25 + 0.004);
|
||||
expect(at(0.05)).toBeLessThan(at(0.5));
|
||||
});
|
||||
});
|
||||
|
|
@ -3,9 +3,12 @@
|
|||
// matrix), then the sRGB encode. A per-pixel function with no state, applied once to the baked sky (CPU: the scene is a 2D
|
||||
// canvas, there is no float render target to fall back from).
|
||||
//
|
||||
// Exposure is chosen per sky state (`exposureFor`): the sky is physical radiance, so brightness is the camera's, not a
|
||||
// shaping of the result. Day and dusk expose to the sky's own luminance (adaptation, as an eye or auto-exposure does) so the
|
||||
// darkest of the sky stays where the state's dark ink needs a backdrop; night is fixed, the physical dark.
|
||||
// Exposure is chosen per sky state: the sky is physical radiance, so brightness is the camera's. Day and dusk expose to the
|
||||
// sky's own luminance (adaptation); night is the physical dark. After ACES comes one more curve, per channel in display-linear:
|
||||
// a monotone cubic through five knots, fitted per sky from the luminance distribution of the sky grid so that its 5th, 50th and
|
||||
// 95th percentiles land on the state's target display luminances. The curve starts at a small black lift (BLACK_LIFT, the only
|
||||
// absolute lift) and ends at white (day, dusk) or at the night ceiling (light ink needs a dark ground). It is fitted on the sky
|
||||
// grid alone: the cards are not sampled, they ride the same curve.
|
||||
import type { FixedSkyState } from '../skyState';
|
||||
|
||||
type Rgb = readonly [number, number, number];
|
||||
|
|
@ -56,46 +59,111 @@ export function toneMap8(r: number, g: number, b: number, exposure: number, out:
|
|||
export const displayLuma = (c: ArrayLike<number>): number =>
|
||||
0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
|
||||
|
||||
/** What a sky state needs of its backdrop's display luminance, from the ink it wears (skyState.ts, skyInk.ts). */
|
||||
/** What a sky state needs of its backdrop's display luminance, and the targets its curve is fitted to. */
|
||||
export interface Budget {
|
||||
/** The darkest texel may not be darker than this (dark ink needs a light backdrop). 0: no floor. */
|
||||
/** The darkest texel the curve can draw (its black lift, less one 8-bit step). */
|
||||
floor: number;
|
||||
/** The lightest texel may not be lighter than this (light ink needs a dark one). 1: no ceiling. */
|
||||
/** What black is drawn as, display-linear: the curve's first knot. */
|
||||
lift: number;
|
||||
/** The lightest texel may not be lighter than this (light ink needs a dark one); it is the curve's top knot. 1: white. */
|
||||
ceiling: number;
|
||||
/** Display luminance the log-mean of the sky is exposed to, before the budget is applied; undefined: a fixed exposure. */
|
||||
/** Display luminance the log-mean of the sky is exposed to before the curve; undefined: a fixed exposure. */
|
||||
key?: number;
|
||||
/** Exposure range, in multiples of scene radiance (per unit sun irradiance). */
|
||||
min: number;
|
||||
max: number;
|
||||
/** Display luminance the 5th, 50th and 95th percentile of the sky's luminance are mapped to. */
|
||||
targets: readonly [number, number, number];
|
||||
/** Fixed knot positions (ACES output) for the three targets instead of the sky's percentiles: for a state whose sky carries no signal to fit (night: the sky is black, the cards are what the curve is for). */
|
||||
knots?: readonly [number, number, number];
|
||||
/** The most the curve may steepen the sky's own spread about its median (display luminance per unit of ACES output): a nearly flat overcast sky is not blown up into a gradient. */
|
||||
maxGain: number;
|
||||
}
|
||||
|
||||
/** Display luminance slack for the 8-bit rounding and the smooth upscale of the baked grid. */
|
||||
export const MARGIN = 0.004;
|
||||
|
||||
/**
|
||||
* Day and Dusk wear the same dark ink, so one floor: its bound plus margin, 0.4744, which is what the dark link needs (about 0.4554 for 4.5:1, skyTheme.test.ts); a lower day floor (0.317 was tried) leaves the link at 3.3:1. Night wears light ink, and its
|
||||
* dimmer foldout headers (L 0.386 on screen, rat-crest-a7c95ce-20261111) need a backdrop of at most 0.047.
|
||||
*/
|
||||
/** The one absolute lift of day and dusk: what black is drawn as, display-linear. The rest of the range is made by the curve. */
|
||||
export const BLACK_LIFT = 0.02;
|
||||
/** Night's black lift: the whole night range is under its ceiling (0.043), so its black is lifted less. */
|
||||
export const NIGHT_LIFT = 0.004;
|
||||
|
||||
/** Night wears light ink, whose dimmer foldout headers (L 0.386 on screen, rat-crest-a7c95ce-20261111) need a backdrop of at most 0.047. */
|
||||
export const BUDGET: Record<FixedSkyState, Budget> = {
|
||||
day: { floor: 0.4744 + MARGIN, ceiling: 1, key: 0.45, min: 4, max: 60 },
|
||||
dusk: { floor: 0.4744 + MARGIN, ceiling: 1, key: 0.5, min: 4, max: 400 },
|
||||
night: { floor: 0, ceiling: 0.047 - MARGIN, key: 0.03, min: 8, max: 3000 },
|
||||
day: { floor: BLACK_LIFT - 0.001, lift: BLACK_LIFT, ceiling: 1, key: 0.45, min: 4, max: 60, targets: [0.22, 0.58, 0.9], maxGain: 4 },
|
||||
dusk: { floor: BLACK_LIFT - 0.001, lift: BLACK_LIFT, ceiling: 1, key: 0.5, min: 4, max: 400, targets: [0.12, 0.45, 0.85], maxGain: 4 },
|
||||
night: {
|
||||
floor: NIGHT_LIFT - 0.001,
|
||||
lift: NIGHT_LIFT,
|
||||
ceiling: 0.047 - MARGIN,
|
||||
min: 1,
|
||||
max: 1,
|
||||
knots: [0.002, 0.02, 0.15],
|
||||
targets: [0.008, 0.018, 0.032],
|
||||
maxGain: 1e6,
|
||||
},
|
||||
};
|
||||
|
||||
/** The luma one 8-bit code moves at the day floor (about 0.0037): a lift step smaller than this can leave the encoded luma where it was, so the floor search never steps less. */
|
||||
const CODE_STEP = 0.0045;
|
||||
/** Knots of the curve: black, the 5th, 50th, 95th percentile, white. */
|
||||
export const KNOTS = 5;
|
||||
|
||||
/** The display transform of a sky: exposure, then ACES, then a black lift (veiling glare) in display-linear. */
|
||||
/** A monotone piecewise-cubic Hermite curve over display-linear 0..1: knot positions, values and slopes. */
|
||||
export interface Curve {
|
||||
x: number[];
|
||||
y: number[];
|
||||
m: number[];
|
||||
}
|
||||
|
||||
/** The monotone cubic (Fritsch-Carlson, Brodlie's weighted harmonic mean) through strictly increasing `x` and non-decreasing `y`. */
|
||||
export function makeCurve(x: number[], y: number[]): Curve {
|
||||
const n = x.length;
|
||||
const h = x.slice(1).map((v, i) => v - x[i]);
|
||||
const d = h.map((hk, i) => (y[i + 1] - y[i]) / hk);
|
||||
const m = new Array<number>(n).fill(0);
|
||||
m[0] = d[0];
|
||||
m[n - 1] = d[n - 2];
|
||||
for (let k = 1; k < n - 1; k += 1) {
|
||||
if (d[k - 1] * d[k] <= 0) continue;
|
||||
const w1 = 2 * h[k] + h[k - 1];
|
||||
const w2 = h[k] + 2 * h[k - 1];
|
||||
m[k] = (w1 + w2) / (w1 / d[k - 1] + w2 / d[k]);
|
||||
}
|
||||
return { x, y, m };
|
||||
}
|
||||
|
||||
/** The curve at `v` (clamped to 0..1). `lightCards.ts` writes the same evaluation into its shader. */
|
||||
export function applyCurve(c: Curve, v: number): number {
|
||||
const t0 = Math.min(1, Math.max(0, v));
|
||||
let k = 0;
|
||||
for (let i = 1; i < c.x.length - 1; i += 1) if (t0 > c.x[i]) k = i;
|
||||
const h = c.x[k + 1] - c.x[k];
|
||||
const t = (t0 - c.x[k]) / h;
|
||||
const t2 = t * t;
|
||||
const t3 = t2 * t;
|
||||
return (
|
||||
(2 * t3 - 3 * t2 + 1) * c.y[k] +
|
||||
(t3 - 2 * t2 + t) * h * c.m[k] +
|
||||
(-2 * t3 + 3 * t2) * c.y[k + 1] +
|
||||
(t3 - t2) * h * c.m[k + 1]
|
||||
);
|
||||
}
|
||||
|
||||
/** The straight line from the black lift to white: what a display with no fitted curve does. */
|
||||
export const LIFT_ONLY: Curve = makeCurve(
|
||||
[0, 0.25, 0.5, 0.75, 1],
|
||||
[0, 0.25, 0.5, 0.75, 1].map((v) => BLACK_LIFT + (1 - BLACK_LIFT) * v)
|
||||
);
|
||||
|
||||
/** The display transform of a sky: exposure, then ACES, then the fitted curve in display-linear. */
|
||||
export interface Display {
|
||||
exposure: number;
|
||||
/** Added to every channel after the curve, then rescaled so white stays white: out = lift + (1 - lift) * aces. */
|
||||
lift: number;
|
||||
curve: Curve;
|
||||
}
|
||||
|
||||
/** The 8-bit sRGB of linear radiance through a display. */
|
||||
export function display8(r: number, g: number, b: number, d: Display, out: number[]): void {
|
||||
acesLinear(r, g, b, d.exposure, out);
|
||||
for (let i = 0; i < 3; i += 1) out[i] = Math.round(encode(d.lift + (1 - d.lift) * out[i]));
|
||||
for (let i = 0; i < 3; i += 1) out[i] = Math.round(encode(applyCurve(d.curve, out[i])));
|
||||
}
|
||||
|
||||
const decode = (v: number): number => {
|
||||
|
|
@ -119,15 +187,16 @@ export interface Fit {
|
|||
/** Darkest and lightest display luminance of the grid under `display`. */
|
||||
lo: number;
|
||||
hi: number;
|
||||
/** The exposure before the budget moved it, and what the budget took (for the numbers a report cites). */
|
||||
/** The exposure the state's key chose (the curve does the rest). */
|
||||
keyExposure: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Choose the display of a sky, the one place brightness is decided. `hdr` is the grid's radiance (3 floats a texel) and
|
||||
* `meanLuma` its log-mean luminance. Exposure starts at the state's key (adaptation); a ceiling lowers it until the
|
||||
* lightest texel is inside the budget (bisection: luminance is monotone in exposure); a floor raises the black lift until the
|
||||
* darkest is (luminance is monotone in lift, and linear: the lift needed is solved in one step).
|
||||
* Choose the display of a sky, the one place brightness is decided. `hdr` is the grid's radiance (3 floats a texel) and `meanLuma` its
|
||||
* log-mean luminance. Exposure is the state's key (adaptation). The curve is then fitted: the 5th, 50th and 95th percentile of the
|
||||
* grid's luminance after ACES go to the state's targets (the outer two held to the budget's `maxGain` times the sky's own spread about the median),
|
||||
* between a black lift below and white (or the night ceiling) above. Monotone by construction, so the ceiling holds for every texel
|
||||
* of every grid and card, and no pass over another viewport's grid is needed.
|
||||
*/
|
||||
export function fitDisplay(state: FixedSkyState, hdr: Float32Array, meanLuma: number): Fit {
|
||||
const b = BUDGET[state];
|
||||
|
|
@ -136,83 +205,41 @@ export function fitDisplay(state: FixedSkyState, hdr: Float32Array, meanLuma: nu
|
|||
? b.min
|
||||
: Math.min(b.max, Math.max(b.min, (b.key * 1.6) / Math.max(meanLuma, 1e-9)));
|
||||
const n = hdr.length / 3;
|
||||
const tmp = [0, 0, 0];
|
||||
const xs: number[] = [];
|
||||
for (let i = 0; i < n; i += 1) {
|
||||
acesLinear(hdr[i * 3], hdr[i * 3 + 1], hdr[i * 3 + 2], keyExposure, tmp);
|
||||
xs.push(displayLuma(tmp));
|
||||
}
|
||||
xs.sort((p, q) => p - q);
|
||||
const pct = (q: number) => xs[Math.min(n - 1, Math.floor(q * n))];
|
||||
const x = [0, ...(b.knots ?? [pct(0.05), pct(0.5), pct(0.95)]), 1];
|
||||
x[1] = Math.max(x[1], 1e-7);
|
||||
x[2] = Math.max(x[2], x[1] * 1.001 + 1e-7);
|
||||
x[3] = Math.max(x[3], x[2] * 1.001 + 1e-7);
|
||||
if (x[3] > 0.97) {
|
||||
x[3] = 0.97;
|
||||
x[2] = Math.min(x[2], 0.965);
|
||||
x[1] = Math.min(x[1], 0.96);
|
||||
}
|
||||
const [t0, t1, t2] = b.targets;
|
||||
const y = [
|
||||
b.lift,
|
||||
Math.max(b.lift + 1e-4, t0, t1 - b.maxGain * (x[2] - x[1])),
|
||||
t1,
|
||||
Math.min(b.ceiling - 1e-4, t2, t1 + b.maxGain * (x[3] - x[2])),
|
||||
b.ceiling,
|
||||
];
|
||||
y[1] = Math.min(y[1], y[2] - 1e-4);
|
||||
y[3] = Math.max(y[3], y[2] + 1e-4);
|
||||
const display: Display = { exposure: keyExposure, curve: makeCurve(x, y) };
|
||||
let lo = 2;
|
||||
let hi = -1;
|
||||
const stride = Math.max(1, Math.floor(n / 4000));
|
||||
const tmp = [0, 0, 0];
|
||||
const range = (d: Display) => {
|
||||
let lo = 2;
|
||||
let hi = -1;
|
||||
for (let i = 0; i < n; i += stride) {
|
||||
const l = displayLumaOf(hdr[i * 3], hdr[i * 3 + 1], hdr[i * 3 + 2], d, tmp);
|
||||
if (l < lo) lo = l;
|
||||
if (l > hi) hi = l;
|
||||
}
|
||||
return { lo, hi };
|
||||
};
|
||||
let exposure = keyExposure;
|
||||
let r = range({ exposure, lift: 0 });
|
||||
if (r.hi > b.ceiling) {
|
||||
let lo = 0;
|
||||
let hiE = exposure;
|
||||
for (let k = 0; k < 24; k += 1) {
|
||||
const mid = (lo + hiE) / 2;
|
||||
if (range({ exposure: mid, lift: 0 }).hi > b.ceiling) hiE = mid;
|
||||
else lo = mid;
|
||||
}
|
||||
exposure = lo;
|
||||
r = range({ exposure, lift: 0 });
|
||||
for (let i = 0; i < n; i += stride) {
|
||||
const l = displayLumaOf(hdr[i * 3], hdr[i * 3 + 1], hdr[i * 3 + 2], display, tmp);
|
||||
if (l < lo) lo = l;
|
||||
if (l > hi) hi = l;
|
||||
}
|
||||
let lift = 0;
|
||||
if (r.lo < b.floor) {
|
||||
// Linear in the displayed linear luminance: L' = lift + (1 - lift) L, but through the 8-bit encode; solve in linear then check once.
|
||||
lift = (b.floor - r.lo) / (1 - r.lo);
|
||||
let probe = range({ exposure, lift });
|
||||
for (let k = 0; k < 6 && probe.lo < b.floor; k += 1) {
|
||||
lift += Math.max((b.floor - probe.lo) * 1.05, CODE_STEP);
|
||||
probe = range({ exposure, lift });
|
||||
}
|
||||
r = probe;
|
||||
}
|
||||
return { display: { exposure, lift }, lo: r.lo, hi: r.hi, keyExposure };
|
||||
}
|
||||
|
||||
/**
|
||||
* The display, moved only as far as the budget needs, for a grid other than the one it was fitted on (another viewport's shape,
|
||||
* its own texels): the fit is made once on a canonical grid so that resizing repaints the same sky, and this keeps a texel the canonical
|
||||
* grid did not have inside the state's bounds. A display that already holds them is returned as it is.
|
||||
*/
|
||||
export function holdBudget(state: FixedSkyState, hdr: Float32Array, d: Display): Display {
|
||||
const b = BUDGET[state];
|
||||
const n = hdr.length / 3;
|
||||
const tmp = [0, 0, 0];
|
||||
const range = (x: Display) => {
|
||||
let lo = 2;
|
||||
let hi = -1;
|
||||
for (let i = 0; i < n; i += 1) {
|
||||
const l = displayLumaOf(hdr[i * 3], hdr[i * 3 + 1], hdr[i * 3 + 2], x, tmp);
|
||||
if (l < lo) lo = l;
|
||||
if (l > hi) hi = l;
|
||||
}
|
||||
return { lo, hi };
|
||||
};
|
||||
let cur = d;
|
||||
let r = range(cur);
|
||||
if (r.hi > b.ceiling) {
|
||||
let lo = 0;
|
||||
let hiE = cur.exposure;
|
||||
for (let k = 0; k < 20; k += 1) {
|
||||
const mid = (lo + hiE) / 2;
|
||||
if (range({ exposure: mid, lift: cur.lift }).hi > b.ceiling) hiE = mid;
|
||||
else lo = mid;
|
||||
}
|
||||
cur = { exposure: lo, lift: cur.lift };
|
||||
r = range(cur);
|
||||
}
|
||||
for (let k = 0; k < 8 && r.lo < b.floor; k += 1) {
|
||||
cur = {
|
||||
exposure: cur.exposure,
|
||||
lift: cur.lift + ((b.floor - r.lo) / (1 - r.lo)) * 1.05 + 1e-4,
|
||||
};
|
||||
r = range(cur);
|
||||
}
|
||||
return cur;
|
||||
return { display, lo, hi, keyExposure };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import { ridgeDrawn, ridgeSkyFor, ridgeSkyKey, loadWeather } from './skyForState
|
|||
import { DAY_FROM, setSkyClock, skyBackdrop } from './skyState';
|
||||
import type { FixedSkyState } from './skyState';
|
||||
import { skyThemeId } from './skyTheme';
|
||||
import { BUDGET } from './ridge/toneMap';
|
||||
|
||||
const STATES = ['day', 'dusk', 'night'] as const;
|
||||
const SKY = { 'midnight-sky': baked.midnight, 'noonday-sky': baked.noonday } as const;
|
||||
|
|
@ -91,10 +92,10 @@ describe('ridgeSkyFor', () => {
|
|||
expect(ridgeSkyKey('day', FALLBACK_VIEW, 0.3)).not.toBe(ridgeSkyKey('night', FALLBACK_VIEW, 0.3));
|
||||
});
|
||||
|
||||
it.each(STATES)('%s: the display is fitted to the budget its text ink needs', (state) => {
|
||||
const { floor, ceiling } = needs(state);
|
||||
it.each(STATES)('%s: the display is the curve of its budget: black lift at the floor, the ceiling the light ink needs', (state) => {
|
||||
const { ceiling } = needs(state);
|
||||
const r = ridgeDrawn(state);
|
||||
expect(r.darkest).toBeGreaterThanOrEqual(floor);
|
||||
expect(r.darkest).toBe(BUDGET[state].floor);
|
||||
expect(r.lightest).toBeLessThanOrEqual(ceiling);
|
||||
const fit = ridgeSkyFor(state, FALLBACK_VIEW, 0.3).physical?.fit;
|
||||
expect(fit?.lo).toBeGreaterThanOrEqual(r.darkest - 1e-9);
|
||||
|
|
@ -103,11 +104,8 @@ describe('ridgeSkyFor', () => {
|
|||
});
|
||||
|
||||
describe('skyBackdrop holds the ridge sky', () => {
|
||||
it.each(STATES)('%s: the backdrop bound contains the ridge display budget', (state) => {
|
||||
const r = ridgeDrawn(state);
|
||||
const b = skyBackdrop(state);
|
||||
expect(b.darkest).toBeLessThanOrEqual(r.darkest);
|
||||
expect(b.lightest).toBeGreaterThanOrEqual(r.lightest);
|
||||
it.each(STATES)('%s: the backdrop bound contains the ridge display ceiling', (state) => {
|
||||
expect(skyBackdrop(state).lightest).toBeGreaterThanOrEqual(ridgeDrawn(state).lightest);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue