import { useEffect, useRef } from "react"; type Brush = { x: number; y: number; size: number; r: number; g: number; b: number; alpha: number; energy: number; curve: number; gx: number; gy: number; }; type FieldSample = { width: number; height: number; rgba: Uint8ClampedArray; fieldRgba: Uint8ClampedArray; luma: Float32Array; alpha: Float32Array; analysis: Float32Array; flowX: Float32Array; flowY: Float32Array; fieldStrength: Float32Array; }; type PyramidLevel = { width: number; height: number; values: Float32Array; energy: Float32Array; gx: Float32Array; gy: Float32Array; curve: Float32Array; }; const FIELD_SIZE = 256; const MAX_DEPTH = 6; const MIN_BRUSH_SIZE = 4; const ENERGY_SPLIT_THRESHOLD = 0.038; const PARTICLE_COUNT = 65_536; const PARTICLE_STRIDE_FLOATS = 12; const gpuParticleComputeShader = /* wgsl */ ` struct Particle { position: vec2f, velocity: vec2f, color: vec4f, seed: f32, size: f32, life: f32, pad: f32, }; struct SimUniforms { time: f32, dt: f32, width: f32, height: f32, particleCount: f32, flowGain: f32, alpha: f32, pad: f32, }; @group(0) @binding(0) var particles: array; @group(0) @binding(1) var uniforms: SimUniforms; @group(0) @binding(2) var fieldSampler: sampler; @group(0) @binding(3) var fieldTexture: texture_2d; fn hash(value: f32) -> f32 { return fract(sin(value * 12.9898) * 43758.5453); } fn hash2(cell: vec2f) -> f32 { return hash(dot(cell, vec2f(127.1, 311.7))); } fn wrap2(value: vec2f, size: vec2f) -> vec2f { return fract(value / size) * size; } fn proceduralCurl(point: vec2f, seed: f32) -> vec2f { let angle = sin(point.x * 0.011 + uniforms.time * 0.23 + seed * 6.28318) + cos(point.y * 0.017 - uniforms.time * 0.19 + seed * 4.71) + sin((point.x + point.y) * 0.006 + uniforms.time * 0.11); return vec2f(cos(angle * 3.14159), sin(angle * 3.14159)); } @compute @workgroup_size(128) fn updateParticles(@builtin(global_invocation_id) id: vec3u) { let index = id.x; if (index >= u32(uniforms.particleCount)) { return; } var particle = particles[index]; let canvasSize = vec2f(max(uniforms.width, 1.0), max(uniforms.height, 1.0)); var position = particle.position * canvasSize; let uv = clamp(particle.position, vec2f(0.001), vec2f(0.999)); let packed = textureSampleLevel(fieldTexture, fieldSampler, uv, 0.0); let fieldVelocity = (packed.rg * 2.0 - vec2f(1.0)) * uniforms.flowGain * (0.22 + packed.a * 1.45); let turbulence = proceduralCurl(position, particle.seed) * (7.0 + packed.b * 22.0); let cell = floor(position / 34.0); let jitter = vec2f(hash2(cell + particle.seed), hash2(cell + vec2f(7.0, 13.0) + particle.seed)) - vec2f(0.5); let velocity = fieldVelocity + turbulence + jitter * (3.0 + packed.a * 8.0); position = wrap2(position + velocity * uniforms.dt, canvasSize); particle.position = position / canvasSize; particle.velocity = velocity; particle.life = fract(particle.life + uniforms.dt * (0.07 + hash(particle.seed * 23.0) * 0.05)); let speed = clamp(length(velocity) / 96.0, 0.0, 1.0); let heat = clamp(packed.a + packed.b * 0.72 + speed * 0.38, 0.0, 1.0); particle.color = vec4f( 0.08 + heat * 0.34, 0.46 + heat * 0.42, 0.52 + heat * 0.56, uniforms.alpha * (0.010 + heat * 0.026) ); particle.size = (1.1 + hash(particle.seed * 19.0) * 2.6) * (0.72 + packed.a * 1.75) * (1.0 - abs(particle.life - 0.5) * 0.36); particles[index] = particle; } `; const gpuParticleRenderShader = /* wgsl */ ` struct Particle { position: vec2f, velocity: vec2f, color: vec4f, seed: f32, size: f32, life: f32, pad: f32, }; struct SimUniforms { time: f32, dt: f32, width: f32, height: f32, particleCount: f32, flowGain: f32, alpha: f32, pad: f32, }; @group(0) @binding(0) var renderParticles: array; @group(0) @binding(1) var renderUniforms: SimUniforms; struct VertexOut { @builtin(position) position: vec4f, @location(0) color: vec4f, @location(1) local: vec2f, }; @vertex fn vertexMain(@builtin(vertex_index) vertexIndex: u32, @builtin(instance_index) instanceIndex: u32) -> VertexOut { let corners = array( vec2f(-1.0, -1.0), vec2f(1.0, -1.0), vec2f(-1.0, 1.0), vec2f(-1.0, 1.0), vec2f(1.0, -1.0), vec2f(1.0, 1.0) ); let particle = renderParticles[instanceIndex]; let local = corners[vertexIndex]; let pixelPosition = particle.position * vec2f(renderUniforms.width, renderUniforms.height) + local * particle.size; var out: VertexOut; out.position = vec4f((pixelPosition.x / renderUniforms.width) * 2.0 - 1.0, 1.0 - (pixelPosition.y / renderUniforms.height) * 2.0, 0.0, 1.0); out.color = particle.color * (1.0 - abs(particle.life - 0.5) * 0.44); out.local = local; return out; } @fragment fn fragmentMain(input: VertexOut) -> @location(0) vec4f { let radius2 = dot(input.local, input.local); let edge = exp(-4.25); let gaussian = exp(-4.25 * radius2); let compact = pow(clamp((gaussian - edge) / max(1.0 - edge, 0.000001), 0.0, 1.0), 0.82); return vec4f(input.color.rgb * compact * 1.72, input.color.a * compact); } `; type HuginnFieldCanvasProps = { imageUrl: string; fieldUrl: string; }; export function HuginnFieldCanvas({ imageUrl, fieldUrl }: HuginnFieldCanvasProps) { const canvasRef = useRef(null); useEffect(() => { const canvas = canvasRef.current; if (!canvas) { return; } let cancelled = false; const render = async () => { try { const [image, fieldImage] = await Promise.all([ loadImage(imageUrl), loadImage(fieldUrl), ]); if (cancelled) { return; } const sample = sampleImage(image, fieldImage); const gpuCleanup = await startGpuParticleField(canvas, sample); if (gpuCleanup) { console.info("Huginn WebGPU particle field active."); return gpuCleanup; } console.info("Huginn WebGPU unavailable; using canvas field fallback."); const pyramid = buildCurvaturePyramid(sample); const brushes = buildBrushes(sample, pyramid); const observer = new ResizeObserver(() => drawFieldFallback(canvas, brushes, sample)); observer.observe(canvas); drawFieldFallback(canvas, brushes, sample); return () => observer.disconnect(); } catch { return; } }; let cleanup: (() => void) | undefined; void render().then((nextCleanup) => { cleanup = nextCleanup; }); return () => { cancelled = true; cleanup?.(); }; }, [fieldUrl, imageUrl]); return