/ simulated_instruments / aerophone
Sign in
simulated_instruments / aerophone
aerophone / src / render.ts
151 lines · 5.7 KBCodeBlameHistory
b8aa175aerophone: 2D wind instruments with lattice Boltzmann on WebGPUJeremy Magland 1// Draws the field in one of five views:
2// combined: acoustic pressure, with vorticity magnitude overlaid in white
3// acoustic: pressure minus its running mean (a high-pass, ~2 ms), which hides the slow
4// mean-pressure shifts and shows sound waves; same diverging map as pressure
5// pressure: p' = c_s^2 * drho, diverging (zero near black, compression warm, rarefaction cool)
6// vorticity: curl u, diverging (counterclockwise green, clockwise magenta)
7// speed: |u|, sequential
8// Walls are gray, mouth cells green, the sponge layer is tinted, the listener is a white ring.
10export const VIEW_MODES = ["combined", "acoustic", "pressure", "vorticity", "speed"] as const;
11export type ViewMode = (typeof VIEW_MODES)[number];
13const renderWGSL = /* wgsl */ `
14struct View {
15 nx: u32, ny: u32, mode: u32, spongeWidth: f32,
16 pRange: f32, wRange: f32, sRange: f32, aRange: f32,
17 lx: f32, ly: f32, p0: f32, p1: f32,
18};
19@group(0) @binding(0) var<uniform> V: View;
20@group(0) @binding(1) var<storage, read> moments: array<vec4f>;
21@group(0) @binding(2) var<storage, read> cells: array<u32>;
23struct VOut { @builtin(position) pos: vec4f, @location(0) uv: vec2f };
25@vertex
26fn vs(@builtin(vertex_index) k: u32) -> VOut {
27 var p = array<vec2f, 3>(vec2f(-1.0, -1.0), vec2f(3.0, -1.0), vec2f(-1.0, 3.0));
28 var o: VOut;
29 o.pos = vec4f(p[k], 0.0, 1.0);
30 o.uv = vec2f(0.5 * (p[k].x + 1.0), 0.5 * (p[k].y + 1.0));
31 return o;
34const BG = vec3f(0.04, 0.045, 0.06);
36fn diverging(v: f32, pos: vec3f, posHi: vec3f, neg: vec3f, negHi: vec3f) -> vec3f {
37 let a = clamp(abs(v), 0.0, 1.0);
38 if (v >= 0.0) {
39 return mix(mix(BG, pos, clamp(2.0 * a, 0.0, 1.0)), posHi, clamp(2.0 * a - 1.0, 0.0, 1.0));
40 }
41 return mix(mix(BG, neg, clamp(2.0 * a, 0.0, 1.0)), negHi, clamp(2.0 * a - 1.0, 0.0, 1.0));
44fn pressureColor(v: f32) -> vec3f {
45 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));
48fn sequential(v: f32) -> vec3f {
49 let a = clamp(v, 0.0, 1.0);
50 let c1 = vec3f(0.15, 0.1, 0.45);
51 let c2 = vec3f(0.85, 0.3, 0.35);
52 let c3 = vec3f(1.0, 0.95, 0.7);
53 if (a < 0.33) { return mix(BG, c1, a / 0.33); }
54 if (a < 0.66) { return mix(c1, c2, (a - 0.33) / 0.33); }
55 return mix(c2, c3, (a - 0.66) / 0.34);
58fn vel(x: i32, y: i32) -> vec2f {
59 let xc = u32(clamp(x, 0, i32(V.nx) - 1));
60 let yc = u32(clamp(y, 0, i32(V.ny) - 1));
61 return moments[yc * V.nx + xc].yz;
64@fragment
65fn fs(vin: VOut) -> @location(0) vec4f {
66 let x = min(u32(vin.uv.x * f32(V.nx)), V.nx - 1u);
67 let y = min(u32(vin.uv.y * f32(V.ny)), V.ny - 1u);
68 let i = y * V.nx + x;
69 let rl = distance(vin.uv * vec2f(f32(V.nx), f32(V.ny)), vec2f(V.lx + 0.5, V.ly + 0.5));
70 if (abs(rl - 5.0) < 0.9 || rl < 1.0) { return vec4f(1.0, 1.0, 1.0, 1.0); }
71 let t = cells[i];
72 if (t == 1u) { return vec4f(0.55, 0.56, 0.6, 1.0); }
73 if (t >= 2u) { return vec4f(0.2, 0.75, 0.4, 1.0); }
74 let m = moments[i];
75 let xi = i32(x);
76 let yi = i32(y);
77 let w = 0.5 * ((vel(xi + 1, yi).y - vel(xi - 1, yi).y) - (vel(xi, yi + 1).x - vel(xi, yi - 1).x));
78 var c: vec3f;
79 if (V.mode == 0u) {
80 c = pressureColor((m.x - m.w) / (3.0 * V.aRange));
81 c = mix(c, vec3f(0.95), 0.85 * clamp(abs(w) / V.wRange, 0.0, 1.0));
82 } else if (V.mode == 1u) {
83 c = pressureColor((m.x - m.w) / (3.0 * V.aRange));
84 } else if (V.mode == 2u) {
85 c = pressureColor(m.x / (3.0 * V.pRange));
86 } else if (V.mode == 3u) {
87 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));
88 } else {
89 c = sequential(length(m.yz) / V.sRange);
90 }
91 let d = f32(min(min(x, V.nx - 1u - x), min(y, V.ny - 1u - y)));
92 if (d < V.spongeWidth) {
93 c = mix(c, vec3f(0.18, 0.18, 0.2), 0.35);
94 }
95 return vec4f(c, 1.0);
97`;
99export type ViewRanges = { pressure: number; acoustic: number; vorticity: number; speed: number }; // lattice units
101export class Renderer {
102 private pipeline: GPURenderPipeline;
103 private view: GPUBuffer;
104 private group: GPUBindGroup;
106 constructor(
107 private device: GPUDevice,
108 private context: GPUCanvasContext,
109 format: GPUTextureFormat,
110 moments: GPUBuffer,
111 cells: GPUBuffer,
112 private nx: number,
113 private ny: number,
114 private spongeWidth: number,
115 ) {
116 const module = device.createShaderModule({ code: renderWGSL });
117 this.pipeline = device.createRenderPipeline({
118 layout: "auto",
119 vertex: { module, entryPoint: "vs" },
120 fragment: { module, entryPoint: "fs", targets: [{ format }] },
121 primitive: { topology: "triangle-list" },
122 });
123 this.view = device.createBuffer({ size: 48, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });
124 this.group = device.createBindGroup({
125 layout: this.pipeline.getBindGroupLayout(0),
126 entries: [
127 { binding: 0, resource: { buffer: this.view } },
128 { binding: 1, resource: { buffer: moments } },
129 { binding: 2, resource: { buffer: cells } },
130 ],
131 });
132 }
134 // ranges (lattice units): pressure and acoustic c_s^2 * drho, vorticity per step, speed in
135 // cells per step. listener: cell coordinates.
136 encode(enc: GPUCommandEncoder, mode: ViewMode, r: ViewRanges, listener: { x: number; y: number }) {
137 const b = new ArrayBuffer(48);
138 new Uint32Array(b, 0, 3).set([this.nx, this.ny, VIEW_MODES.indexOf(mode)]);
139 new Float32Array(b, 12, 7).set([this.spongeWidth, r.pressure, r.vorticity, r.speed, r.acoustic, listener.x, listener.y]);
140 this.device.queue.writeBuffer(this.view, 0, b);
141 const pass = enc.beginRenderPass({
142 colorAttachments: [
143 { view: this.context.getCurrentTexture().createView(), loadOp: "clear", storeOp: "store", clearValue: { r: 0, g: 0, b: 0, a: 1 } },
144 ],
145 });
146 pass.setPipeline(this.pipeline);
147 pass.setBindGroup(0, this.group);
148 pass.draw(3);
149 pass.end();
150 }
↑↓ move↵openescclose