// Draws the field in one of five views: // combined: acoustic pressure, with vorticity magnitude overlaid in white // acoustic: pressure minus its running mean (a high-pass, ~2 ms), which hides the slow // mean-pressure shifts and shows sound waves; same diverging map as pressure // pressure: p' = c_s^2 * drho, diverging (zero near black, compression warm, rarefaction cool) // vorticity: curl u, diverging (counterclockwise green, clockwise magenta) // speed: |u|, sequential // Walls are gray, mouth cells green, the sponge layer is tinted, the listener is a white ring. export const VIEW_MODES = ["combined", "acoustic", "pressure", "vorticity", "speed"] as const; export type ViewMode = (typeof VIEW_MODES)[number]; const renderWGSL = /* wgsl */ ` struct View { nx: u32, ny: u32, mode: u32, spongeWidth: f32, pRange: f32, wRange: f32, sRange: f32, aRange: f32, lx: f32, ly: f32, p0: f32, p1: f32, }; @group(0) @binding(0) var V: View; @group(0) @binding(1) var moments: array; @group(0) @binding(2) var cells: array; struct VOut { @builtin(position) pos: vec4f, @location(0) uv: vec2f }; @vertex fn vs(@builtin(vertex_index) k: u32) -> VOut { var p = array(vec2f(-1.0, -1.0), vec2f(3.0, -1.0), vec2f(-1.0, 3.0)); var o: VOut; o.pos = vec4f(p[k], 0.0, 1.0); o.uv = vec2f(0.5 * (p[k].x + 1.0), 0.5 * (p[k].y + 1.0)); return o; } const BG = vec3f(0.04, 0.045, 0.06); fn diverging(v: f32, pos: vec3f, posHi: vec3f, neg: vec3f, negHi: vec3f) -> vec3f { let a = clamp(abs(v), 0.0, 1.0); if (v >= 0.0) { return mix(mix(BG, pos, clamp(2.0 * a, 0.0, 1.0)), posHi, clamp(2.0 * a - 1.0, 0.0, 1.0)); } return mix(mix(BG, neg, clamp(2.0 * a, 0.0, 1.0)), negHi, clamp(2.0 * a - 1.0, 0.0, 1.0)); } fn pressureColor(v: f32) -> vec3f { return diverging(v, vec3f(0.85, 0.25, 0.05), vec3f(1.0, 0.95, 0.6), vec3f(0.05, 0.35, 0.85), vec3f(0.7, 0.95, 1.0)); } fn sequential(v: f32) -> vec3f { let a = clamp(v, 0.0, 1.0); let c1 = vec3f(0.15, 0.1, 0.45); let c2 = vec3f(0.85, 0.3, 0.35); let c3 = vec3f(1.0, 0.95, 0.7); if (a < 0.33) { return mix(BG, c1, a / 0.33); } if (a < 0.66) { return mix(c1, c2, (a - 0.33) / 0.33); } return mix(c2, c3, (a - 0.66) / 0.34); } fn vel(x: i32, y: i32) -> vec2f { let xc = u32(clamp(x, 0, i32(V.nx) - 1)); let yc = u32(clamp(y, 0, i32(V.ny) - 1)); return moments[yc * V.nx + xc].yz; } @fragment fn fs(vin: VOut) -> @location(0) vec4f { let x = min(u32(vin.uv.x * f32(V.nx)), V.nx - 1u); let y = min(u32(vin.uv.y * f32(V.ny)), V.ny - 1u); let i = y * V.nx + x; let rl = distance(vin.uv * vec2f(f32(V.nx), f32(V.ny)), vec2f(V.lx + 0.5, V.ly + 0.5)); if (abs(rl - 5.0) < 0.9 || rl < 1.0) { return vec4f(1.0, 1.0, 1.0, 1.0); } let t = cells[i]; if (t == 1u) { return vec4f(0.55, 0.56, 0.6, 1.0); } if (t >= 2u) { return vec4f(0.2, 0.75, 0.4, 1.0); } let m = moments[i]; let xi = i32(x); let yi = i32(y); let w = 0.5 * ((vel(xi + 1, yi).y - vel(xi - 1, yi).y) - (vel(xi, yi + 1).x - vel(xi, yi - 1).x)); var c: vec3f; if (V.mode == 0u) { c = pressureColor((m.x - m.w) / (3.0 * V.aRange)); c = mix(c, vec3f(0.95), 0.85 * clamp(abs(w) / V.wRange, 0.0, 1.0)); } else if (V.mode == 1u) { c = pressureColor((m.x - m.w) / (3.0 * V.aRange)); } else if (V.mode == 2u) { c = pressureColor(m.x / (3.0 * V.pRange)); } else if (V.mode == 3u) { c = diverging(w / V.wRange, vec3f(0.1, 0.6, 0.3), vec3f(0.75, 1.0, 0.8), vec3f(0.6, 0.15, 0.55), vec3f(1.0, 0.75, 0.95)); } else { c = sequential(length(m.yz) / V.sRange); } let d = f32(min(min(x, V.nx - 1u - x), min(y, V.ny - 1u - y))); if (d < V.spongeWidth) { c = mix(c, vec3f(0.18, 0.18, 0.2), 0.35); } return vec4f(c, 1.0); } `; export type ViewRanges = { pressure: number; acoustic: number; vorticity: number; speed: number }; // lattice units export class Renderer { private pipeline: GPURenderPipeline; private view: GPUBuffer; private group: GPUBindGroup; constructor( private device: GPUDevice, private context: GPUCanvasContext, format: GPUTextureFormat, moments: GPUBuffer, cells: GPUBuffer, private nx: number, private ny: number, private spongeWidth: number, ) { const module = device.createShaderModule({ code: renderWGSL }); this.pipeline = device.createRenderPipeline({ layout: "auto", vertex: { module, entryPoint: "vs" }, fragment: { module, entryPoint: "fs", targets: [{ format }] }, primitive: { topology: "triangle-list" }, }); this.view = device.createBuffer({ size: 48, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST }); this.group = device.createBindGroup({ layout: this.pipeline.getBindGroupLayout(0), entries: [ { binding: 0, resource: { buffer: this.view } }, { binding: 1, resource: { buffer: moments } }, { binding: 2, resource: { buffer: cells } }, ], }); } // ranges (lattice units): pressure and acoustic c_s^2 * drho, vorticity per step, speed in // cells per step. listener: cell coordinates. encode(enc: GPUCommandEncoder, mode: ViewMode, r: ViewRanges, listener: { x: number; y: number }) { const b = new ArrayBuffer(48); new Uint32Array(b, 0, 3).set([this.nx, this.ny, VIEW_MODES.indexOf(mode)]); new Float32Array(b, 12, 7).set([this.spongeWidth, r.pressure, r.vorticity, r.speed, r.acoustic, listener.x, listener.y]); this.device.queue.writeBuffer(this.view, 0, b); const pass = enc.beginRenderPass({ colorAttachments: [ { view: this.context.getCurrentTexture().createView(), loadOp: "clear", storeOp: "store", clearValue: { r: 0, g: 0, b: 0, a: 1 } }, ], }); pass.setPipeline(this.pipeline); pass.setBindGroup(0, this.group); pass.draw(3); pass.end(); } }