diff --git a/harness/scene/README.md b/harness/scene/README.md index c7d42007..9fc5de49 100644 --- a/harness/scene/README.md +++ b/harness/scene/README.md @@ -36,11 +36,12 @@ behind a cell is the app's own (`makePhysicalSky` + `paintSky`, upscaled), witho `looks=` is a url-encoded or base64 JSON array of `{label, ...partial RidgeLook}` and replaces the preset rows (`inkKeep` and `reliefLight` merge by key). A cell costs one WebGL2 context and a card decode, so the full sheet takes tens of seconds (much longer in a background tab). -Presets (`PRESETS` in `ridgeSheet.ts`; every key not named is `DEFAULT_LOOK`), chosen by reasoning, not by eye, for Self to tune: +Presets (`PRESETS` in `ridgeSheet.ts`; swiss and poster take every key not named from `PHYSICAL_LOOK`, the rest from `DEFAULT_LOOK`, the painterly cel look), chosen by reasoning, not by eye, for Self to tune: | preset | values | | --- | --- | -| physical | DEFAULT_LOOK | +| physical | PHYSICAL_LOOK (Lambert, no noise, sky haze, full ink) | | swiss | reliefLight {mix 0.7, azimuthDeg -45, elevationDeg 40}, contrastByDistance 0.9, hazeScale 6, hazeTint [0, 0.05, 0.25], nearWarm 0.5, inkScale 0.6 | | poster | reliefWeight 0.15, cardToneStep 0.25, cardGradient 0.5, inkKeep {edge 0.25, occlude 0.9, crease 1}, hazeScale 4 | -| painterly | groundNoise 0.35, cloudShadows 0.5, nearWarm 0.6, hazeScale 2, inkScale 0.4 | +| painterly | DEFAULT_LOOK: groundNoise 0.35, cloudShadows 0.5, nearWarm 0.6, hazeScale 2, inkScale 0.4, celBands 3, celSoftness 0.05, celAmbientAway 0.7, nightAmbient [0.5, 0.66, 1.1] | +| painterly-cel-2 / -3 / -4 | painterly with celBands 2 / 3 / 4 | diff --git a/harness/scene/ridgeSheet.ts b/harness/scene/ridgeSheet.ts index 6516e98e..d5cf3d00 100644 --- a/harness/scene/ridgeSheet.ts +++ b/harness/scene/ridgeSheet.ts @@ -1,8 +1,8 @@ // Look sheet for the lit ridge cards: the Vienna bake lit for a grid of sky states x looks, each cell a small canvas at heading 0. Not shipped. -// `?preset=all|physical|swiss|poster|painterly` (default all: the four rows); `?looks=` replaces the rows with +// `?preset=all|physical|swiss|poster|painterly|painterly-cel-2|painterly-cel-3|painterly-cel-4` (default all: one row each; painterly is DEFAULT_LOOK); `?looks=` replaces the rows with // the given `{label, ...partial RidgeLook}` entries (inkKeep and reliefLight merge by key). `?w=480` sets the cell width (16:9). // The sky behind a cell is the app's own: `makePhysicalSky` + `paintSky` upscaled smoothly, without the sun disc, stars or clouds. -import { DEFAULT_LOOK, lightCards } from '../../src/app/features/scene/ridge/lightCards'; +import { DEFAULT_LOOK, PHYSICAL_LOOK, lightCards } from '../../src/app/features/scene/ridge/lightCards'; import type { RidgeLook } from '../../src/app/features/scene/ridge/lightCards'; import { parseManifest } from '../../src/app/features/scene/ridge/manifest'; import { makePhysicalSky, paintSky } from '../../src/app/features/scene/ridge/physicalSky'; @@ -14,6 +14,8 @@ const DIR = '/public/ridge/vienna-6815bb9dca99/'; type Override = Partial> & { label: string; + /** The look the unnamed keys come from (DEFAULT_LOOK, the painterly cel look, if absent). */ + base?: 'physical'; inkKeep?: Partial; reliefLight?: Partial; }; @@ -30,9 +32,10 @@ const SKIES = [ /** The art directions, chosen by reasoning, not by eye: Self tunes them (values listed in harness/scene/README.md). */ const PRESETS: Record = { - physical: { label: 'physical' }, + physical: { label: 'physical', base: 'physical' }, swiss: { label: 'swiss', + base: 'physical', reliefLight: { mix: 0.7, azimuthDeg: -45, elevationDeg: 40 }, contrastByDistance: 0.9, hazeScale: 6, @@ -42,31 +45,29 @@ const PRESETS: Record = { }, poster: { label: 'poster', + base: 'physical', reliefWeight: 0.15, cardToneStep: 0.25, cardGradient: 0.5, inkKeep: { edge: 0.25, occlude: 0.9, crease: 1 }, hazeScale: 4, }, - painterly: { - label: 'painterly', - groundNoise: 0.35, - cloudShadows: 0.5, - nearWarm: 0.6, - hazeScale: 2, - inkScale: 0.4, - }, + painterly: { label: 'painterly (default, cel 3)' }, + 'painterly-cel-2': { label: 'painterly-cel-2', celBands: 2 }, + 'painterly-cel-3': { label: 'painterly-cel-3', celBands: 3 }, + 'painterly-cel-4': { label: 'painterly-cel-4', celBands: 4 }, }; const merge = (o: Override): { label: string; look: RidgeLook } => { - const { label, inkKeep, reliefLight, ...rest } = o; + const { label, base: baseName, inkKeep, reliefLight, ...rest } = o; + const base = baseName === 'physical' ? PHYSICAL_LOOK : DEFAULT_LOOK; return { label, look: { - ...DEFAULT_LOOK, + ...base, ...rest, - inkKeep: { ...DEFAULT_LOOK.inkKeep, ...inkKeep }, - reliefLight: { ...DEFAULT_LOOK.reliefLight, ...reliefLight }, + inkKeep: { ...base.inkKeep, ...inkKeep }, + reliefLight: { ...base.reliefLight, ...reliefLight }, }, }; }; diff --git a/src/app/features/scene/ridge/lightCards.test.ts b/src/app/features/scene/ridge/lightCards.test.ts index 3c85e859..f9144a2c 100644 --- a/src/app/features/scene/ridge/lightCards.test.ts +++ b/src/app/features/scene/ridge/lightCards.test.ts @@ -2,6 +2,9 @@ import { describe, expect, it, vi } from 'vitest'; import { decodeNormal, DEFAULT_LOOK, + PHYSICAL_LOOK, + celShade, + celLevels, FRAGMENT, lightCards, lightingOf, @@ -240,7 +243,7 @@ describe('the look reaches the shader', () => { it('scales the haze optical depth and the ambient light, and moves the sun cut-off', () => { const base = run(); - const look = run({ hazeScale: 2, ambientScale: 0.5 }); + const look = run({ hazeScale: 2 * DEFAULT_LOOK.hazeScale, ambientScale: 0.5 }); look.uExt.forEach((e, k) => expect(e).toBeCloseTo(2 * base.uExt[k], 12)); expect(look.uAmbient[2]).toBeCloseTo(0.15, 12); expect( @@ -291,3 +294,44 @@ describe('night ambient', () => { expect(DEFAULT_LOOK.nightAmbient.every((c) => c > 0)).toBe(true); }); }); + +describe('cel shading', () => { + const sweep = (bands: number, softness: number, levels?: number[]) => { + const out = new Set(); + for (let i = -20; i <= 100; i += 1) { + const x = i / 100; + // away from the band edges (i / bands), where a softened edge is between two levels + if (Array.from({ length: bands }, (_, k) => k / bands).some((e) => e > 0 && Math.abs(x - e) <= softness / 2 + 1e-9)) continue; + out.add(Math.round(celShade(x, bands, softness, levels) * 1e9)); + } + return out; + }; + it.each([1, 2, 3, 4, 6])('quantizes a normal sweep to exactly %i levels away from the edges', (n) => { + expect(sweep(n, 0.05).size).toBe(n); + expect(sweep(n, 0).size).toBe(n); + }); + it('is monotone in n.s, from the lowest level to the highest, and softens an edge without a jump', () => { + let prev = -1; + let maxStep = 0; + for (let i = -20; i <= 100; i += 1) { + const v = celShade(i / 100, 3, 0.1); + expect(v).toBeGreaterThanOrEqual(prev - 1e-12); + if (prev >= 0) maxStep = Math.max(maxStep, v - prev); + prev = v; + } + expect(maxStep).toBeLessThan(0.3); + expect(celShade(-0.5, 3, 0)).toBe(0); + expect(celShade(1, 3, 0)).toBe(1); + }); + it('takes the band levels it is given', () => { + expect(celLevels(3, [0.2, 0.5, 0.9]).slice(0, 3)).toEqual([0.2, 0.5, 0.9]); + expect(celShade(0.9, 3, 0, [0.2, 0.5, 0.9])).toBe(0.9); + expect(celShade(-1, 3, 0, [0.2, 0.5, 0.9])).toBe(0.2); + }); + it('is on in the default look and off in the physical one; the shader carries the ramp', () => { + expect(DEFAULT_LOOK.celBands).toBeGreaterThan(0); + expect(PHYSICAL_LOOK.celBands).toBe(0); + expect(FRAGMENT).toContain('float cel(float x)'); + expect(FRAGMENT).toContain('uBandLevels[6]'); + }); +}); diff --git a/src/app/features/scene/ridge/lightCards.ts b/src/app/features/scene/ridge/lightCards.ts index 0a44a421..29745834 100644 --- a/src/app/features/scene/ridge/lightCards.ts +++ b/src/app/features/scene/ridge/lightCards.ts @@ -73,9 +73,19 @@ export interface RidgeLook { 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]; + /** Cel shading: the lit side of the relief (n.s) in this many flat bands instead of Lambert's ramp (0 = Lambert; at most `CEL_MAX`). */ + celBands: number; + /** Width of each band edge in n.s units (0 = hard). */ + celSoftness: number; + /** The level of each band, 0..1, darkest first (default: evenly from 0 to 1). Length `celBands`. */ + celBandLevels?: readonly number[]; + /** The ambient is coarse too: surfaces facing the sky (the normal's up component past `celAmbientEdge`) keep it whole, the rest keep this fraction (1 = no step). */ + celAmbientAway: number; + celAmbientEdge: number; } -export const DEFAULT_LOOK: RidgeLook = { +/** The first art pass's look: Lambert relief, no procedural variation, the sky's own haze, full ink. Kept for comparison in the look sheet. */ +export const PHYSICAL_LOOK: RidgeLook = { occludeLogD: 0.35, creaseCos: 0.85, inkKeep: { edge: 0.45, occlude: 0.55, crease: 0.8 }, @@ -93,8 +103,58 @@ export const DEFAULT_LOOK: RidgeLook = { groundNoise: 0, cloudShadows: 0, nightAmbient: [0.5, 0.66, 1.1], + celBands: 0, + celSoftness: 0, + celAmbientAway: 1, + celAmbientEdge: 0.7, }; +/** The default: painterly (ground variation, cloud shadows, warm near, moderate haze, light ink) with cel-shaded relief. */ +export const DEFAULT_LOOK: RidgeLook = { + ...PHYSICAL_LOOK, + groundNoise: 0.35, + cloudShadows: 0.5, + nearWarm: 0.6, + hazeScale: 2, + inkScale: 0.4, + celBands: 3, + celSoftness: 0.05, + celAmbientAway: 0.7, +}; + +/** Most bands the shader's uniform holds. */ +export const CEL_MAX = 6; + +/** The level of each of `bands` cel bands, darkest first: `custom` where given, else evenly 0..1 (one band is fully lit). Padded to `CEL_MAX`. */ +export function celLevels(bands: number, custom?: readonly number[]): number[] { + const n = Math.min(CEL_MAX, Math.max(1, Math.round(bands))); + return Array.from({ length: CEL_MAX }, (_, i) => { + const k = Math.min(i, n - 1); + return custom?.[k] ?? (n === 1 ? 1 : k / (n - 1)); + }); +} + +const smooth = (a: number, b: number, v: number): number => { + const t = Math.min(1, Math.max(0, (v - a) / (b - a))); + return t * t * (3 - 2 * t); +}; + +/** + * The cel ramp: the light of a surface whose normal makes `ndl` with the sun, in `bands` flat bands, edges at i / bands (i = 1..bands-1), each + * edge `softness` wide. The lowest band holds the unlit side too (its level is what ambient light alone leaves). The shader writes the same. + */ +export function celShade(ndl: number, bands: number, softness: number, levels?: readonly number[]): number { + const n = Math.min(CEL_MAX, Math.max(1, Math.round(bands))); + const lv = celLevels(n, levels); + let out = lv[0]; + for (let i = 1; i < n; i += 1) { + const e = i / n; + const w = softness / 2; + out += (lv[i] - lv[i - 1]) * (w > 0 ? smooth(e - w, e + w, ndl) : ndl >= e ? 1 : 0); + } + return out; +} + const RAD = Math.PI / 180; /** The sun in the frame of a column looking at azimuth `columnAzDeg`: along the ray, across it (clockwise, +90 degrees), up. Unit length. */ @@ -228,6 +288,8 @@ uniform vec3 uHazeTint; uniform vec2 uDist; // nearWarm, contrastByDistance uniform vec2 uCard; // value of this card, gradient uniform vec2 uGround; // groundNoise, cloudShadows +uniform vec4 uCel; // bands (0 = Lambert), softness, ambientAway, ambientEdge +uniform float uBandLevels[${CEL_MAX}]; uniform float uRadPerPx; // radians of azimuth per plane pixel out vec4 outColor; @@ -273,6 +335,19 @@ float curve1(float v) { } vec3 curve(vec3 c) { return vec3(curve1(c.r), curve1(c.g), curve1(c.b)); } +// lightCards.ts celShade: flat bands of n.s, edges at i / bands. +float cel(float x) { + int n = int(uCel.x); + float w = uCel.y * 0.5; + float l = uBandLevels[0]; + for (int i = 1; i < ${CEL_MAX}; i++) { + if (i >= n) break; + float e = float(i) / float(n); + l += (uBandLevels[i] - uBandLevels[i - 1]) * (w > 0.0 ? smoothstep(e - w, e + w, x) : step(e, x)); + } + return l; +} + vec3 skyAt(vec2 uv) { vec2 f = uv * vec2(uSkySize) - 0.5; vec2 b = floor(f); @@ -327,8 +402,10 @@ void main() { vec3 albedo = toLinear(c.rgb); if (uGround.x > 0.0) albedo *= max(1.0 + uGround.x * (2.0 * fbm(world / 250.0) - 1.0), 0.0); float cloud = uGround.y > 0.0 ? 1.0 - uGround.y * smoothstep(0.5, 0.72, fbm(world / 4000.0 + 17.0)) : 1.0; - float ndl = mix(max(s.z, 0.0), max(dot(g.n, s), 0.0), uRelief.x); - vec3 radiance = albedo * (uTSun * ndl * cloud + uAmbient) / PI; + float lit = uCel.x > 0.5 ? cel(dot(g.n, s)) : max(dot(g.n, s), 0.0); + float ndl = mix(max(s.z, 0.0), lit, uRelief.x); + float ambStep = uCel.x > 0.5 ? mix(uCel.z, 1.0, smoothstep(uCel.w - uCel.y * 0.5, uCel.w + uCel.y * 0.5 + 1e-4, g.n.z)) : 1.0; + vec3 radiance = albedo * (uTSun * ndl * cloud + uAmbient * mix(1.0, ambStep, uRelief.x)) / PI; float dn = clamp((g.logD - uLogD.x) / uLogD.y, 0.0, 1.0); if (uDist.y != 0.0) { vec3 flatTone = albedo * (uTSun * max(s.z, 0.0) * cloud + uAmbient) / PI; @@ -469,6 +546,8 @@ export function lightCards( gl.uniform3f(u('uHazeTint'), ...look.hazeTint); gl.uniform2f(u('uDist'), look.nearWarm, look.contrastByDistance); gl.uniform2f(u('uGround'), look.groundNoise, look.cloudShadows); + gl.uniform4f(u('uCel'), look.celBands, look.celSoftness, look.celAmbientAway, look.celAmbientEdge); + gl.uniform1fv(u('uBandLevels'), celLevels(look.celBands, look.celBandLevels)); gl.uniform1f(u('uRadPerPx'), (manifest.view.windowDeg * RAD) / lit.planeW); gl.uniform2i(u('uSkySize'), SKY_LUT.w, SKY_LUT.h); const sunTex = floatTexture(gl, 2, lit.planeW, 1, sunColumns(manifest.view, sky.sun));