feat: render the black hole and sky with a WebGL2 ray tracer

Replace the SVG core and backdrop with one shader that traces each pixel
through Schwarzschild spacetime. The shadow, the arch of the far side of the
disk, the second image and the Einstein ring come from the physics rather than
artwork. The disk is a thin disk seen from a low angle, about five times the
shadow's radius, with Doppler beaming and a filtered, cloud-like texture.

- Sky: a procedural galaxy baked once into a cubemap. Point sources are small,
  few and brightness-capped so they never rival the orbit markers, which now
  carry a dark backing disc and outline.
- Quality: tiers from high to still, a startup calibration cached per canvas
  size, an fps cap, and an idle sleep. A locked realistic map draws nothing.
- Integration: the canvas follows the galaxy frame through Big Screen, draws in
  Motion's post-render step, takes the disk clock and capture absorption from
  the galaxy loop, and falls back to the SVG core without WebGL2.
- Radar view: a toolbar toggle switches to a radar look that spreads outward
  from the hole as a wave, with a sweep on its own clock. The choice is
  remembered per browser; reduced motion switches at once and holds the sweep.
- Tests for the geodesics, quality governor, look spring and layout, plus a
  standalone preview page (web/blackhole-proto.html) for tuning the look.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 21:51:35 +07:00
parent 6320b6f244
commit 34e9afc8a4
19 changed files with 2067 additions and 6 deletions

View File

@@ -0,0 +1,29 @@
import assert from "node:assert/strict";
import test from "node:test";
import { layoutFor } from "../src/lib/blackhole/layout.ts";
import { SCENE_WIDTH } from "../src/lib/galaxyLayout.ts";
import { CRITICAL_IMPACT } from "../src/lib/blackhole/geodesic.ts";
const panel = { left: 100, top: 50, width: 800, height: 600 };
test("the hole sits at the centre of the galaxy frame, relative to the panel", () => {
const frame = { left: 100 + 70, top: 50 + 60, width: 660, height: 480 };
const { centerX, centerY } = layoutFor(frame, panel);
assert.ok(Math.abs(centerX - (70 + 330) / 800) < 1e-9);
assert.ok(Math.abs(centerY - (60 + 240) / 600) < 1e-9);
});
test("the shadow has a fixed radius in scene units and scales with the frame", () => {
const atNative = layoutFor({ left: 0, top: 0, width: SCENE_WIDTH, height: 480 }, panel);
assert.ok(Math.abs(atNative.pxPerM * CRITICAL_IMPACT - 34) < 1e-9);
const doubled = layoutFor({ left: 0, top: 0, width: SCENE_WIDTH * 2, height: 960 }, panel);
assert.ok(Math.abs(doubled.pxPerM - atNative.pxPerM * 2) < 1e-9);
});
test("a frame moving under a transition moves the hole with it", () => {
const before = layoutFor({ left: 200, top: 100, width: 600, height: 436 }, panel);
const after = layoutFor({ left: 240, top: 140, width: 600, height: 436 }, panel);
assert.ok(Math.abs(after.centerX - before.centerX - 40 / 800) < 1e-9);
assert.ok(Math.abs(after.centerY - before.centerY - 40 / 600) < 1e-9);
assert.equal(after.pxPerM, before.pxPerM);
});

View File

@@ -0,0 +1,40 @@
import assert from "node:assert/strict";
import test from "node:test";
import { CRITICAL_IMPACT, HORIZON, deflection, traceImpact, traceRay } from "../src/lib/blackhole/geodesic.ts";
test("a ray inside the critical impact parameter is captured and one outside escapes", () => {
assert.equal(traceImpact(CRITICAL_IMPACT * 0.94).outcome, "captured");
assert.equal(traceImpact(CRITICAL_IMPACT * 1.06).outcome, "escaped");
});
test("the shadow edge sits at the analytic 3√3 within the step error", () => {
let captured = 0;
let escaped = 10;
for (let i = 0; i < 24; i++) {
const mid = (captured + escaped) / 2;
const { outcome } = traceImpact(mid, 48, { maxSteps: 900 });
if (outcome === "escaped") escaped = mid;
else captured = mid;
}
assert.ok(Math.abs(escaped - CRITICAL_IMPACT) / CRITICAL_IMPACT < 0.03, `edge at ${escaped}`);
});
test("weak-field deflection follows 4M/b plus the second-order term", () => {
const b = 20;
const expected = 4 / b + (15 * Math.PI) / 4 / (b * b);
const angle = deflection(traceImpact(b, 400, { maxSteps: 600 }));
assert.ok(Math.abs(angle - expected) / expected < 0.1, `${angle} vs ${expected}`);
});
test("a ray that falls radially is captured at the horizon", () => {
const trace = traceRay([0, 0, 30], [0, 0, -1]);
assert.equal(trace.outcome, "captured");
assert.ok(trace.minRadius < HORIZON);
});
test("a ray in the disk plane never crosses it and a skewed ray crosses it once", () => {
assert.equal(traceImpact(9).crossings.length, 0);
const skew = traceRay([9, 14, 40], [0, -0.3, -0.95]);
assert.equal(skew.crossings.length, 1);
assert.equal(skew.crossings[0].point[1], 0);
});

108
web/scripts/look.test.mjs Normal file
View File

@@ -0,0 +1,108 @@
import assert from "node:assert/strict";
import test from "node:test";
import { createLook, jumpLook, requestLook, stepLook, styleAt, waveParams, WAVE_FEATHER } from "../src/lib/blackhole/look.ts";
import { farthestCornerM } from "../src/lib/blackhole/layout.ts";
const run = (look, seconds, fps = 60) => {
const progress = [];
for (let i = 0; i < seconds * fps; i++) {
stepLook(look, 1 / fps);
progress.push(look.progress);
}
return progress;
};
test("the wave expands from the hole, never overshoots, and settles on the new look", () => {
const look = createLook(0);
requestLook(look, 1);
assert.equal(look.running, true);
assert.equal(look.progress, 0);
const progress = run(look, 3);
for (let i = 1; i < progress.length; i++) {
assert.ok(progress[i] >= progress[i - 1] - 1e-12, "progress never goes backwards");
assert.ok(progress[i] <= 1, "progress never overshoots");
}
assert.equal(look.running, false);
assert.equal(look.from, 1);
assert.equal(look.to, 1);
const finishedAt = progress.findIndex((value) => value === 1);
assert.ok(finishedAt > 20 && finishedAt < 150, `settled after ${finishedAt} frames`);
});
test("a request made mid-wave waits for the wave to finish instead of reversing it", () => {
const look = createLook(0);
requestLook(look, 1);
run(look, 0.3);
const before = look.progress;
requestLook(look, 0);
assert.equal(look.queued, 0);
assert.equal(look.to, 1, "the running wave keeps its target");
assert.ok(look.progress >= before);
run(look, 4);
assert.equal(look.running, false);
assert.equal(look.to, 0, "the queued look ran afterwards");
});
test("asking for the look that is already showing does nothing, and the queue follows the latest request", () => {
const look = createLook(1);
requestLook(look, 1);
assert.equal(look.running, false);
requestLook(look, 0);
assert.equal(look.running, true);
requestLook(look, 1);
assert.equal(look.queued, 1, "asking for the old look mid-wave queues it");
requestLook(look, 0);
assert.equal(look.queued, undefined, "asking for the running target again cancels the queue");
assert.equal(look.to, 0);
});
test("pixels inside the front show the new look and pixels outside keep the old one", () => {
const look = createLook(0);
requestLook(look, 1);
run(look, 0.4);
const wave = waveParams(look, 50);
assert.equal(styleAt(wave, 0), 1);
assert.equal(styleAt(wave, wave.waveRadius + 1), 0);
const middle = styleAt(wave, wave.waveRadius - WAVE_FEATHER / 2);
assert.ok(middle > 0 && middle < 1, "the front is soft");
});
test("the front covers the whole picture by the time the wave finishes, and fades as it goes", () => {
const look = createLook(0);
requestLook(look, 1);
const fronts = [];
const radii = [];
for (let i = 0; i < 240 && look.running; i++) {
stepLook(look, 1 / 60);
const wave = waveParams(look, 50);
fronts.push(wave.waveFront);
radii.push(wave.waveRadius);
}
for (let i = 1; i < fronts.length; i++) {
assert.ok(fronts[i] <= fronts[i - 1] + 1e-12);
assert.ok(radii[i] >= radii[i - 1] - 1e-12);
}
const settled = waveParams(look, 50);
assert.equal(settled.waveFront, 0);
assert.equal(settled.styleFrom, 1);
assert.equal(styleAt(settled, 49), 1);
});
test("the wave has to travel as far as the farthest corner", () => {
const radius = farthestCornerM({ x: 300, y: 200 }, { width: 800, height: 600 }, 10);
assert.ok(Math.abs(radius - Math.hypot(500, 400) / 10) < 1e-9);
});
test("jumping to a look drops any wave and any queued request", () => {
const look = createLook(0);
requestLook(look, 1);
run(look, 0.2);
requestLook(look, 0);
jumpLook(look, 1);
assert.equal(look.running, false);
assert.equal(look.from, 1);
assert.equal(look.to, 1);
assert.equal(look.queued, undefined);
assert.equal(waveParams(look, 50).waveFront, 0);
assert.equal(styleAt(waveParams(look, 50), 40), 1);
});

View File

@@ -0,0 +1,41 @@
import assert from "node:assert/strict";
import test from "node:test";
import { FRAME_BUDGET_MS, FREEZE_BUDGET_MS, QUALITY_TIERS, chooseTier, initialTier } from "../src/lib/blackhole/quality.ts";
const samples = (ms, count = 30) => Array.from({ length: count }, () => ms);
test("the governor steps down one tier when the median render time exceeds the budget", () => {
assert.equal(chooseTier({ tier: 0, samples: samples(FRAME_BUDGET_MS + 4), sinceChangeMs: 5000 }), 1);
assert.equal(chooseTier({ tier: 0, samples: samples(FRAME_BUDGET_MS - 4), sinceChangeMs: 5000 }), 0);
});
test("the governor ignores a lone spike, too few samples and a tier that has not settled", () => {
const spiky = [...samples(4, 29), 90];
assert.equal(chooseTier({ tier: 1, samples: spiky, sinceChangeMs: 5000 }), 1);
assert.equal(chooseTier({ tier: 1, samples: samples(40, 5), sinceChangeMs: 5000 }), 1);
assert.equal(chooseTier({ tier: 1, samples: samples(40), sinceChangeMs: 200 }), 1);
});
test("the governor stops at the lowest tier and never steps back up", () => {
const last = QUALITY_TIERS.length - 1;
assert.equal(chooseTier({ tier: last, samples: samples(80), sinceChangeMs: 5000 }), last);
assert.equal(chooseTier({ tier: 2, samples: samples(0.5), sinceChangeMs: 5000 }), 2);
});
test("motion only freezes when the lowest animated tier is clearly failing", () => {
const minimal = QUALITY_TIERS.findIndex(({ name }) => name === "minimal");
assert.equal(chooseTier({ tier: minimal, samples: samples(FREEZE_BUDGET_MS - 4), sinceChangeMs: 5000 }), minimal);
assert.equal(chooseTier({ tier: minimal, samples: samples(FREEZE_BUDGET_MS + 4), sinceChangeMs: 5000 }), minimal + 1);
assert.equal(QUALITY_TIERS[minimal + 1].maxFps, 0);
});
test("tiers get strictly cheaper and software rendering starts at the bottom", () => {
for (let i = 1; i < QUALITY_TIERS.length; i++) {
assert.ok(QUALITY_TIERS[i].scale <= QUALITY_TIERS[i - 1].scale);
assert.ok(QUALITY_TIERS[i].steps <= QUALITY_TIERS[i - 1].steps);
assert.ok(QUALITY_TIERS[i].maxFps <= QUALITY_TIERS[i - 1].maxFps);
}
assert.equal(initialTier({ software: true, pixels: 1e6 }), QUALITY_TIERS.length - 1);
assert.equal(initialTier({ software: false, cores: 8, memoryGb: 16, pixels: 2e6 }), 0);
assert.equal(initialTier({ software: false, cores: 4, pixels: 2e6 }), 1);
});