Add Huginn signal plate renderer

This commit is contained in:
Meta Vens 2026-05-18 15:07:59 +01:00
commit 7d5858b1b6
7 changed files with 156 additions and 35 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 MiB

View file

@ -20,6 +20,11 @@ type FieldSample = {
rgba: Uint8ClampedArray;
luma: Float32Array;
alpha: Float32Array;
analysis: Float32Array;
flowX: Float32Array;
flowY: Float32Array;
normalX: Float32Array;
normalY: Float32Array;
};
type PyramidLevel = {
@ -32,12 +37,19 @@ type PyramidLevel = {
curve: Float32Array;
};
const FIELD_SIZE = 64;
const FIELD_SIZE = 256;
const MAX_DEPTH = 6;
const MIN_BRUSH_SIZE = 2;
const ENERGY_SPLIT_THRESHOLD = 0.045;
const MIN_BRUSH_SIZE = 4;
const ENERGY_SPLIT_THRESHOLD = 0.038;
export function HuginnFieldCanvas({ imageUrl }: { imageUrl: string }) {
type HuginnFieldCanvasProps = {
imageUrl: string;
curvatureUrl?: string;
flowUrl?: string;
normalUrl?: string;
};
export function HuginnFieldCanvas({ imageUrl, curvatureUrl, flowUrl, normalUrl }: HuginnFieldCanvasProps) {
const canvasRef = useRef<HTMLCanvasElement | null>(null);
useEffect(() => {
@ -47,28 +59,29 @@ export function HuginnFieldCanvas({ imageUrl }: { imageUrl: string }) {
}
let cancelled = false;
const image = new Image();
image.decoding = "async";
image.src = imageUrl;
const render = async () => {
try {
await image.decode();
const [image, curvatureImage, flowImage, normalImage] = await Promise.all([
loadImage(imageUrl),
curvatureUrl ? loadImage(curvatureUrl) : Promise.resolve(undefined),
flowUrl ? loadImage(flowUrl) : Promise.resolve(undefined),
normalUrl ? loadImage(normalUrl) : Promise.resolve(undefined),
]);
if (cancelled) {
return;
}
const sample = sampleImage(image, curvatureImage, flowImage, normalImage);
const pyramid = buildCurvaturePyramid(sample);
const brushes = buildBrushes(sample, pyramid);
const observer = new ResizeObserver(() => drawField(canvas, brushes, sample));
observer.observe(canvas);
drawField(canvas, brushes, sample);
return () => observer.disconnect();
} catch {
return;
}
if (cancelled) {
return;
}
const sample = sampleImage(image);
const pyramid = buildCurvaturePyramid(sample);
const brushes = buildBrushes(sample, pyramid);
const observer = new ResizeObserver(() => drawField(canvas, brushes, sample));
observer.observe(canvas);
drawField(canvas, brushes, sample);
return () => observer.disconnect();
};
let cleanup: (() => void) | undefined;
@ -80,12 +93,56 @@ export function HuginnFieldCanvas({ imageUrl }: { imageUrl: string }) {
cancelled = true;
cleanup?.();
};
}, [imageUrl]);
}, [curvatureUrl, flowUrl, imageUrl, normalUrl]);
return <canvas ref={canvasRef} className="huginn-field-canvas" aria-hidden="true" />;
}
function sampleImage(image: HTMLImageElement): FieldSample {
function loadImage(url: string): Promise<HTMLImageElement> {
const image = new Image();
image.decoding = "async";
image.src = url;
return image.decode()
.catch(() => new Promise<void>((resolve, reject) => {
if (image.complete && image.naturalWidth > 0) {
resolve();
return;
}
image.onload = () => resolve();
image.onerror = () => reject(new Error(`Could not load ${url}`));
}))
.then(() => image);
}
function sampleImage(
image: HTMLImageElement,
curvatureImage?: HTMLImageElement,
flowImage?: HTMLImageElement,
normalImage?: HTMLImageElement,
): FieldSample {
const base = sampleBitmap(image);
const curvature = curvatureImage ? sampleBitmap(curvatureImage) : undefined;
const flow = flowImage ? sampleBitmap(flowImage) : undefined;
const normal = normalImage ? sampleBitmap(normalImage) : undefined;
const analysis = curvature?.luma ?? base.luma;
const flowVectors = flow ? buildFlowVectors(flow) : emptyVectors();
const normalVectors = normal ? buildNormalVectors(normal) : emptyVectors();
return {
width: FIELD_SIZE,
height: FIELD_SIZE,
rgba: base.rgba,
luma: base.luma,
alpha: base.alpha,
analysis,
flowX: flowVectors.x,
flowY: flowVectors.y,
normalX: normalVectors.x,
normalY: normalVectors.y,
};
}
function sampleBitmap(image: HTMLImageElement) {
const canvas = document.createElement("canvas");
canvas.width = FIELD_SIZE;
canvas.height = FIELD_SIZE;
@ -95,7 +152,8 @@ function sampleImage(image: HTMLImageElement): FieldSample {
}
context.clearRect(0, 0, FIELD_SIZE, FIELD_SIZE);
context.imageSmoothingEnabled = false;
context.imageSmoothingEnabled = true;
context.imageSmoothingQuality = "high";
context.drawImage(image, 0, 0, FIELD_SIZE, FIELD_SIZE);
const rgba = context.getImageData(0, 0, FIELD_SIZE, FIELD_SIZE).data;
const luma = new Float32Array(FIELD_SIZE * FIELD_SIZE);
@ -111,14 +169,53 @@ function sampleImage(image: HTMLImageElement): FieldSample {
) / 255 * a;
}
return { width: FIELD_SIZE, height: FIELD_SIZE, rgba, luma, alpha };
return { rgba, luma, alpha };
}
function emptyVectors() {
return {
x: new Float32Array(FIELD_SIZE * FIELD_SIZE),
y: new Float32Array(FIELD_SIZE * FIELD_SIZE),
};
}
function buildFlowVectors(flow: { luma: Float32Array }) {
const x = new Float32Array(FIELD_SIZE * FIELD_SIZE);
const y = new Float32Array(FIELD_SIZE * FIELD_SIZE);
for (let yy = 0; yy < FIELD_SIZE; yy += 1) {
for (let xx = 0; xx < FIELD_SIZE; xx += 1) {
const left = scalarAt(flow.luma, FIELD_SIZE, FIELD_SIZE, xx - 1, yy);
const right = scalarAt(flow.luma, FIELD_SIZE, FIELD_SIZE, xx + 1, yy);
const up = scalarAt(flow.luma, FIELD_SIZE, FIELD_SIZE, xx, yy - 1);
const down = scalarAt(flow.luma, FIELD_SIZE, FIELD_SIZE, xx, yy + 1);
const dx = (right - left) * 0.5;
const dy = (down - up) * 0.5;
const length = Math.max(0.0001, Math.hypot(dx, dy));
const index = yy * FIELD_SIZE + xx;
x[index] = -dy / length;
y[index] = dx / length;
}
}
return { x, y };
}
function buildNormalVectors(normal: { rgba: Uint8ClampedArray; alpha: Float32Array }) {
const x = new Float32Array(FIELD_SIZE * FIELD_SIZE);
const y = new Float32Array(FIELD_SIZE * FIELD_SIZE);
for (let index = 0; index < x.length; index += 1) {
const source = index * 4;
const alpha = normal.alpha[index];
x[index] = (normal.rgba[source] / 255 * 2 - 1) * alpha;
y[index] = (normal.rgba[source + 1] / 255 * 2 - 1) * alpha;
}
return { x, y };
}
function buildCurvaturePyramid(sample: FieldSample): PyramidLevel[] {
const levels: PyramidLevel[] = [];
let width = sample.width;
let height = sample.height;
let values = sample.luma;
let values = sample.analysis;
for (let level = 0; level < 5; level += 1) {
const gx = new Float32Array(width * height);
@ -193,7 +290,7 @@ function splitCell(
return;
}
const field = sampleVector(pyramid, x + size * 0.5, y + size * 0.5, depth);
const field = sampleVector(sample, pyramid, x + size * 0.5, y + size * 0.5, depth);
brushes.push({
x: (x + size * 0.5) / sample.width,
y: (y + size * 0.5) / sample.height,
@ -345,14 +442,21 @@ function cellStats(sample: FieldSample, level: PyramidLevel, x: number, y: numbe
};
}
function sampleVector(pyramid: PyramidLevel[], x: number, y: number, depth: number) {
function sampleVector(sample: FieldSample, pyramid: PyramidLevel[], x: number, y: number, depth: number) {
const level = pyramid[Math.min(pyramid.length - 1, Math.max(0, Math.floor(depth / 2)))] ?? pyramid[0];
const sx = Math.max(0, Math.min(level.width - 1, Math.floor(x / FIELD_SIZE * level.width)));
const sy = Math.max(0, Math.min(level.height - 1, Math.floor(y / FIELD_SIZE * level.height)));
const index = sy * level.width + sx;
const sourceX = Math.max(0, Math.min(sample.width - 1, Math.floor(x)));
const sourceY = Math.max(0, Math.min(sample.height - 1, Math.floor(y)));
const sourceIndex = sourceY * sample.width + sourceX;
const flowX = sample.flowX[sourceIndex] ?? 0;
const flowY = sample.flowY[sourceIndex] ?? 0;
const normalX = sample.normalX[sourceIndex] ?? 0;
const normalY = sample.normalY[sourceIndex] ?? 0;
return {
gx: level.gx[index] * 90,
gy: level.gy[index] * 90,
gx: level.gx[index] * 54 + flowX * 0.28 + normalX * 0.18,
gy: level.gy[index] * 54 + flowY * 0.28 + normalY * 0.18,
curve: level.curve[index],
};
}

View file

@ -26,6 +26,23 @@ type GraphProjection = {
const MAX_EXPANDED_VALUE_NODES = 320;
const assetPath = (name: string) => `${import.meta.env.BASE_URL}${name}`;
const HUGINN_ART = {
ground: assetPath("huginn-groundtruth-alpha.png"),
curvature: assetPath("huginn-curvature.png"),
flow: assetPath("huginn-flow.png"),
normal: assetPath("huginn-normal.png"),
};
function HuginnField() {
return (
<HuginnFieldCanvas
imageUrl={HUGINN_ART.ground}
curvatureUrl={HUGINN_ART.curvature}
flowUrl={HUGINN_ART.flow}
normalUrl={HUGINN_ART.normal}
/>
);
}
const app = document.querySelector<HTMLDivElement>("#app");
if (!app) {
@ -156,7 +173,7 @@ function EmptyWorkspace({
}) {
return (
<section className="empty-workspace">
<HuginnFieldCanvas imageUrl={assetPath("hugin.png")} />
<HuginnField />
<section className="floating-panel brand-panel is-primary">
<BrandBlock />
<FilePicker onInspectFile={onInspectFile} />
@ -233,7 +250,7 @@ function Metric({ label, value }: { label: string; value: string }) {
function NoFile() {
return (
<div className="hero">
<img src={assetPath("hugin.png")} alt="" />
<img src={HUGINN_ART.ground} alt="" />
<h2>No state loaded</h2>
<p>Drag a CultCache <code>.cc</code> store into the window to inspect the snapshot header, schema catalog, records, and decoded MessagePack payloads.</p>
</div>
@ -323,7 +340,7 @@ function InspectionView({
selection={graphSelection}
onSelectionChange={selectGraph}
title="CultCache Structure"
viewportBackdrop={<HuginnFieldCanvas imageUrl={assetPath("hugin.png")} />}
viewportBackdrop={<HuginnField />}
viewportBackground="#03070a"
overlayPanels
showSidebar={false}

View file

@ -198,7 +198,7 @@ code {
z-index: 0;
width: 100%;
height: 100%;
image-rendering: pixelated;
image-rendering: auto;
opacity: 0.96;
pointer-events: none;
}
@ -323,7 +323,7 @@ code {
width: 168px;
height: 168px;
margin-bottom: 18px;
image-rendering: pixelated;
image-rendering: auto;
filter: drop-shadow(0 0 28px var(--cyan-glow));
}