diff --git a/src/app/features/scene/ridge/Ridge.test.ts b/src/app/features/scene/ridge/Ridge.test.ts index 30136dcb..d463fff7 100644 --- a/src/app/features/scene/ridge/Ridge.test.ts +++ b/src/app/features/scene/ridge/Ridge.test.ts @@ -174,9 +174,10 @@ function ctx() { else draws.push('fill'); }, drawImage(...a: unknown[]) { - images.push(a); + images.push(Object.assign(a, { alpha: c.globalAlpha, op: c.globalCompositeOperation })); }, globalAlpha: 1, + globalCompositeOperation: 'source-over', set fillStyle(_v: unknown) {}, }; return c; @@ -580,17 +581,33 @@ describe('the ridge planes in the scene', () => { s.dispose(); }); - it('lays frame b over frame a at the blend weight', () => { + it('crossfades a into b: a at 1 - w, b added at w, in a group of its own', () => { const { parent, canvas } = host(); - const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: BLEND })!; + const BLEND3 = backdropAt(4, 130); // w = 0.2, not the symmetric half + const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: BLEND3 })!; + const { w } = BLEND3.choice; + expect(w).toBeCloseTo(0.2, 6); const g = rigOf(parent).children[1]; - expect(g.children.map((i: any) => [i.src, i.style.opacity])).toEqual([ - ['dawn-mid', '1'], - ['morning-mid', String(BLEND.choice.w)], + expect(g.style.isolation).toBe('isolate'); + expect( + g.children.map((i: any) => [i.src, i.style.opacity, i.style.mixBlendMode ?? null]) + ).toEqual([ + ['dawn-mid', String(1 - w), null], + ['morning-mid', String(w), 'plus-lighter'], ]); s.dispose(); }); + it('a lone frame is drawn at full opacity with no blend mode', () => { + const { parent, canvas } = host(); + const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: NOON })!; + const g = rigOf(parent).children[1]; + expect( + g.children.map((i: any) => [i.src, i.style.opacity, i.style.mixBlendMode ?? null]) + ).toEqual([['noon-mid', '1', null]]); + s.dispose(); + }); + it('adds no rAF or timer callback of its own: the drift is Element.animate, the stars alone loop', () => { const { canvas } = host(); const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: NOON })!; @@ -622,28 +639,50 @@ describe('the ridge planes in the scene', () => { anims.forEach((a: any) => expect(a.cancel).toHaveBeenCalled()); }); - it("a still draws the planes into the host canvas at windowFor's placement and leaves no animation", () => { + it("a still sums each blended plane in an offscreen (a at 1 - w, then b lighter at w) and draws it at windowFor's placement", () => { const { parent, canvas } = host(); - createRidgeScene({ canvas, tier: 'still', sky: DAY, backdrop: BLEND }); + const BLEND3 = backdropAt(4, 130); + createRidgeScene({ canvas, tier: 'still', sky: DAY, backdrop: BLEND3 }); + const { w } = BLEND3.choice; const win = windowFor(manifest.view, 400, 800); - const planesDrawn = images.filter((a) => (a[0] as any).src); - expect(planesDrawn).toHaveLength(6); - expect(planesDrawn.map((a) => (a[0] as any).src)).toEqual([ - 'dawn-far', - 'morning-far', - 'dawn-mid', - 'morning-mid', - 'dawn-near', - 'morning-near', - ]); - const [, x, y, w, h] = planesDrawn[0] as number[]; - expect([x, y, w, h]).toEqual([ + const planesDrawn = images.filter((a) => (a[0] as any).src) as any[]; + expect(planesDrawn.map((a) => [a[0].src, a.alpha, a.op])).toEqual( + ['far', 'mid', 'near'].flatMap((n) => [ + [`dawn-${n}`, 1 - w, 'source-over'], + [`morning-${n}`, w, 'lighter'], + ]) + ); + // Each sum then meets the host canvas once, plainly, at the plane's placement. + const summed = images.filter((a) => !(a[0] as any).src && (a[0] as any).width) as any[]; + expect(summed).toHaveLength(3); + summed.forEach((a) => { + expect([a[1], a[2], a[3], a[4]]).toEqual([ + (800 - win.plane.w * 2) / 2, + 1600 - win.plane.h * 2, + win.plane.w * 2, + win.plane.h * 2, + ]); + expect([a.alpha, a.op]).toEqual([1, 'source-over']); + expect([a[0].width, a[0].height]).toEqual([win.plane.w * 2, win.plane.h * 2]); + }); + expect(parent.children).toEqual([canvas]); + }); + + it('a lone frame in a still goes straight to the host canvas at full alpha', () => { + const { canvas } = host(); + createRidgeScene({ canvas, tier: 'still', sky: DAY, backdrop: NOON }); + const win = windowFor(manifest.view, 400, 800); + const planesDrawn = images.filter((a) => (a[0] as any).src) as any[]; + expect(planesDrawn.map((a) => [a[0].src, a.alpha, a.op])).toEqual( + ['far', 'mid', 'near'].map((n) => [`noon-${n}`, 1, 'source-over']) + ); + const [, x, y, dw, dh] = planesDrawn[0]; + expect([x, y, dw, dh]).toEqual([ (800 - win.plane.w * 2) / 2, 1600 - win.plane.h * 2, win.plane.w * 2, win.plane.h * 2, ]); - expect(parent.children).toEqual([canvas]); }); it('settles to a still with the planes painted when the frame rate is poor', () => { diff --git a/src/app/features/scene/ridge/Ridge.ts b/src/app/features/scene/ridge/Ridge.ts index 732d7f04..89eb4442 100644 --- a/src/app/features/scene/ridge/Ridge.ts +++ b/src/app/features/scene/ridge/Ridge.ts @@ -4,6 +4,7 @@ import { FrameSampler, FrameStats } from './probe'; import { skyColours, SkyColours, Srgb } from './skyColour'; import { paintSky as paintPhysicalSky } from './physicalSky'; import { windowFor } from './view'; +import { crossfade } from './frameChoice'; import { activeAt, drawStars, drawTwinkle as paintTwinkle, makeStars, Star } from './stars'; import { drawRainTile, RAIN_TILES } from './rain'; import type { Backdrop, Planes } from './Backdrop'; @@ -237,7 +238,7 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null { height: `${win.plane.h}px`, willChange: 'transform', }); - const layer = (planes: Planes, opacity: number) => { + const layer = (planes: Planes, opacity: number, blend?: string) => { const img = planes[name].cloneNode() as HTMLImageElement; Object.assign(img.style, { position: 'absolute', @@ -247,10 +248,14 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null { height: '100%', opacity: String(opacity), }); + if (blend) img.style.mixBlendMode = blend; group.appendChild(img); }; - layer(bd.a, 1); - if (bd.b) layer(bd.b, bd.choice.w); + // One rule (`crossfade`): a at 1 - w, b added to it at w; the group is its own isolated backdrop, so the sum meets the sky once. + const fade = crossfade(bd.choice); + group.style.isolation = 'isolate'; + layer(bd.a, fade.a); + if (bd.b) layer(bd.b, fade.b, 'plus-lighter'); el.appendChild(group); const reach = win.driftPx * DRIFT_DEPTH[k]; anims.push( @@ -272,7 +277,7 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null { return { el, anims }; }; - /** Paint the ridge planes into the host canvas, a over b, far to near: the still scene's one image. */ + /** Paint the ridge planes into the host canvas, a and b crossfaded, far to near: the still scene's one image. */ const paintPlanes = (bd: Backdrop) => { const ctx = context2d(canvas); if (!ctx) return; @@ -282,15 +287,28 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null { const dh = win.plane.h * scale; const x = (canvas.width - dw) / 2; const y = canvas.height - dh - win.plane.bottom * scale; + const fade = crossfade(bd.choice); + // A blend is summed in an offscreen of its own, as the rig sums it in an isolated group: 'lighter' on the host canvas would add to the sky. + const sum = bd.b ? document.createElement('canvas') : null; + if (sum) { + sum.width = dw; + sum.height = dh; + } + const sctx = sum ? context2d(sum) : null; PLANE_NAMES.forEach((name) => { - ctx.globalAlpha = 1; - ctx.drawImage(bd.a[name], x, y, dw, dh); - if (bd.b) { - ctx.globalAlpha = bd.choice.w; - ctx.drawImage(bd.b[name], x, y, dw, dh); + if (!bd.b || !sum || !sctx) { + ctx.drawImage(bd.a[name], x, y, dw, dh); + return; } + sctx.clearRect(0, 0, dw, dh); + sctx.globalCompositeOperation = 'source-over'; + sctx.globalAlpha = fade.a; + sctx.drawImage(bd.a[name], 0, 0, dw, dh); + sctx.globalCompositeOperation = 'lighter'; + sctx.globalAlpha = fade.b; + sctx.drawImage(bd.b[name], 0, 0, dw, dh); + ctx.drawImage(sum, x, y, dw, dh); }); - ctx.globalAlpha = 1; }; /** Paint the sky, the stars and the rain. Returns false when a 2D context is missing or there is no area yet. */ diff --git a/src/app/features/scene/ridge/frameChoice.test.ts b/src/app/features/scene/ridge/frameChoice.test.ts index be81d8f6..82b9f1a9 100644 --- a/src/app/features/scene/ridge/frameChoice.test.ts +++ b/src/app/features/scene/ridge/frameChoice.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { frameChoice } from './frameChoice'; +import { BLEND_EDGE, crossfade, frameChoice } from './frameChoice'; import { parseManifest } from './manifest'; import { wireManifest } from './manifest.fixture'; @@ -55,4 +55,63 @@ describe('frameChoice', () => { }) ); }); + + it("splits the halves at the noon frame's azimuth, wherever the bake put it", () => { + const w = wireManifest() as any; + const shifted = parseManifest( + { + ...w, + frames: w.frames.map((f: any) => + f.key === 'noon' ? { ...f, sun: { ...f.sun, az: 170 } } : f + ), + }, + '/d/' + ); + const pick = (az: number) => frameChoice(shifted.frames, { elevation: 10, azimuth: az }); + expect([pick(169).a.key, pick(169).b?.key]).toEqual(['dawn', 'morning']); + expect([pick(171).a.key, pick(171).b?.key]).toEqual(['golden', 'noon']); + }); + + it('collapses exactly at BLEND_EDGE and no further: a blend of 3% still has two frames', () => { + expect(BLEND_EDGE).toBe(0.02); + // Morning half dawn 0 .. morning 20: w = el / 20. + expect(keys(at(0.6, 130))).toEqual(['dawn', 'morning']); // w = 0.03 + expect(keys(at(19.4, 130))).toEqual(['dawn', 'morning']); // w = 0.97 + expect(keys(at(0.2, 130))).toEqual(['dawn', null]); // w = 0.01 + expect(keys(at(19.8, 130))).toEqual(['morning', null]); // w = 0.99 + }); +}); + +describe('crossfade: the one rule of a blend', () => { + // A baked plane is haze as alpha: no pixel opaque, the same alpha in every frame. Frames as (premultiplied) colour and alpha. + const planeA = { c: 0.2, alpha: 0.6 }; + const planeB = { c: 0.5, alpha: 0.6 }; + const sum = (c: ReturnType) => { + const f = crossfade(c); + const b = c.b ? planeB : { c: 0, alpha: 0 }; + return { c: planeA.c * f.a + b.c * f.b, alpha: planeA.alpha * f.a + b.alpha * f.b }; + }; + + it('is a lerp of the two frames: colour and haze alike, never denser than either', () => { + [0.03, 0.2, 0.5, 0.8, 0.97].forEach((w) => { + const c = { a: frames[0], b: frames[1], w }; + const got = sum(c); + expect(got.c).toBeCloseTo(planeA.c * (1 - w) + planeB.c * w, 12); + expect(got.alpha).toBeCloseTo(0.6, 12); // equal alphas stay equal: the haze is not doubled + expect(got.alpha).toBeLessThanOrEqual(Math.max(planeA.alpha, planeB.alpha) + 1e-12); + }); + }); + + it('has no jump where the choice collapses: the blend either side of an edge is within the edge of the lone frame', () => { + const gap = Math.abs(planeA.c - planeB.c); + const high = sum({ a: frames[0], b: frames[1], w: 1 - BLEND_EDGE }); + const low = sum({ a: frames[0], b: frames[1], w: BLEND_EDGE }); + expect(Math.abs(high.c - planeB.c)).toBeLessThanOrEqual(BLEND_EDGE * gap + 1e-12); // collapse shows b alone + expect(Math.abs(low.c - planeA.c)).toBeLessThanOrEqual(BLEND_EDGE * gap + 1e-12); // collapse shows a alone + expect(high.alpha).toBeCloseTo(planeB.alpha, 12); + }); + + it('a lone frame is full weight alone', () => { + expect(crossfade({ a: frames[0], b: null, w: 0 })).toEqual({ a: 1, b: 0 }); + }); }); diff --git a/src/app/features/scene/ridge/frameChoice.ts b/src/app/features/scene/ridge/frameChoice.ts index a0f2dec8..1bede129 100644 --- a/src/app/features/scene/ridge/frameChoice.ts +++ b/src/app/features/scene/ridge/frameChoice.ts @@ -4,9 +4,9 @@ import type { Frame, FrameKey } from './manifest'; export interface FrameChoice { - /** The frame shown at full opacity. */ + /** The frame shown alone when b is null, else weighted 1 - w. */ a: Frame; - /** The frame blended over it at `w`, or null when one frame stands alone. */ + /** The frame crossfaded in at `w`, or null when one frame stands alone. */ b: Frame | null; /** Weight of b in 0..1; 0 when b is null. */ w: number; @@ -15,6 +15,15 @@ export interface FrameChoice { /** Blends this close to either frame are not worth a second image. */ export const BLEND_EDGE = 0.02; +/** + * The one rule of a blend, for the rig and the still alike: a true crossfade, a at 1 - w plus b at w, summed (premultiplied) in a group of + * their own. The baked planes carry haze as alpha (no pixel is opaque), so b laid over a at opacity w would double the haze mid-blend + * and jump when the choice collapses to b alone; the sum is a lerp of the two frames and never denser than either. + */ +export function crossfade(choice: FrameChoice): { a: number; b: number } { + return choice.b ? { a: 1 - choice.w, b: choice.w } : { a: 1, b: 0 }; +} + const MORNING: readonly FrameKey[] = ['night', 'dawn', 'morning', 'noon']; const AFTERNOON: readonly FrameKey[] = ['night', 'dusk', 'golden', 'noon'];