From 533da47f2cc03a8b60a7c6c0bb5a938a7e523a03 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Thu, 20 Aug 2026 23:20:21 -0700 Subject: particles --- src/shader/instance.wgsl | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ src/shader/particle.wgsl | 46 +++++++++++++++++++++++++++++++ src/shader/shader.wgsl | 71 ------------------------------------------------ 3 files changed, 117 insertions(+), 71 deletions(-) create mode 100644 src/shader/instance.wgsl create mode 100644 src/shader/particle.wgsl delete mode 100644 src/shader/shader.wgsl (limited to 'src/shader') diff --git a/src/shader/instance.wgsl b/src/shader/instance.wgsl new file mode 100644 index 0000000..fb1e0b1 --- /dev/null +++ b/src/shader/instance.wgsl @@ -0,0 +1,71 @@ +struct Camera { + scale: vec2f, + centre: vec2f, +}; + +struct Instance { + centre: vec2f, + cos_sin: vec2f, + half_size: vec2f, + dims: vec2u, + cell_offset: u32, +}; + +@group(0) @binding(0) var camera: Camera; +@group(0) @binding(1) var palette: array; +@group(0) @binding(2) var instances: array; +@group(0) @binding(3) var cells: array; + +struct VertexOutput { + @builtin(position) clip_position: vec4, + @location(0) world: vec2, + @location(1) @interpolate(flat) instance: u32, +}; + +@vertex +fn vs_main( + @builtin(vertex_index) i: u32, + @builtin(instance_index) n: u32, +) -> VertexOutput { + let instance = instances[n]; + + // (-1,-1),(1,-1),(-1,1),(1,1) + let corner = vec2f(f32(i & 1u), f32(i >> 1u)) * 2.0 - 1.0; + let local = corner * (instance.half_size + 2.0); + let world = instance.centre + vec2f( + local.x * instance.cos_sin.x - local.y * instance.cos_sin.y, + local.x * instance.cos_sin.y + local.y * instance.cos_sin.x, + ); + + var out: VertexOutput; + out.clip_position = vec4f((world - camera.centre) * camera.scale, 0.0, 1.0); + out.world = world; + out.instance = n; + + return out; +} + +@fragment +fn fs_main(in: VertexOutput) -> @location(0) vec4 { + let instance = instances[in.instance]; + + let d = in.world - instance.centre; + let q = floor(d) + vec2f(0.5); + let local = vec2f( + q.x * instance.cos_sin.x + q.y * instance.cos_sin.y, + -q.x * instance.cos_sin.y + q.y * instance.cos_sin.x, + ) + instance.half_size; + + let cell = vec2i(floor(local)); + if any(cell < vec2i(0)) || any(cell >= vec2i(instance.dims)) { + discard; + } + + let idx = instance.cell_offset + u32(cell.y) * instance.dims.x + u32(cell.x); + let material = (cells[idx >> 2u] >> ((idx & 3u) * 8u)) & 0xFFu; + if material == 0u { + discard; + } + + return palette[material]; +} diff --git a/src/shader/particle.wgsl b/src/shader/particle.wgsl new file mode 100644 index 0000000..704cfbe --- /dev/null +++ b/src/shader/particle.wgsl @@ -0,0 +1,46 @@ +struct Camera { + scale: vec2f, + centre: vec2f, +}; + +struct Particle { + position: vec2f, + data: u32, +}; + +@group(0) @binding(0) var camera: Camera; +@group(0) @binding(1) var palette: array; +@group(0) @binding(2) var particles: array; + +struct VertexOutput { + @builtin(position) clip_position: vec4, + @location(0) world: vec2, + @location(1) @interpolate(flat) instance: u32, +}; + +@vertex +fn vs_main( + @builtin(vertex_index) i: u32, + @builtin(instance_index) n: u32, +) -> VertexOutput { + let particle = particles[n]; + + // (0,0),(1,0),(0,1),(1,1) + let corner = vec2(f32(i & 1u), f32((i >> 1u) & 1u)); + let world = particle.position + corner; + + var out: VertexOutput; + out.clip_position = vec4f((world - camera.centre) * camera.scale, 0.0, 1.0); + out.world = world; + out.instance = n; + + return out; +} + +@fragment +fn fs_main(in: VertexOutput) -> @location(0) vec4 { + let particle = particles[in.instance]; + // TODO can pack more in here and shift + let c = palette[particle.data]; + return c; +} diff --git a/src/shader/shader.wgsl b/src/shader/shader.wgsl deleted file mode 100644 index fb1e0b1..0000000 --- a/src/shader/shader.wgsl +++ /dev/null @@ -1,71 +0,0 @@ -struct Camera { - scale: vec2f, - centre: vec2f, -}; - -struct Instance { - centre: vec2f, - cos_sin: vec2f, - half_size: vec2f, - dims: vec2u, - cell_offset: u32, -}; - -@group(0) @binding(0) var camera: Camera; -@group(0) @binding(1) var palette: array; -@group(0) @binding(2) var instances: array; -@group(0) @binding(3) var cells: array; - -struct VertexOutput { - @builtin(position) clip_position: vec4, - @location(0) world: vec2, - @location(1) @interpolate(flat) instance: u32, -}; - -@vertex -fn vs_main( - @builtin(vertex_index) i: u32, - @builtin(instance_index) n: u32, -) -> VertexOutput { - let instance = instances[n]; - - // (-1,-1),(1,-1),(-1,1),(1,1) - let corner = vec2f(f32(i & 1u), f32(i >> 1u)) * 2.0 - 1.0; - let local = corner * (instance.half_size + 2.0); - let world = instance.centre + vec2f( - local.x * instance.cos_sin.x - local.y * instance.cos_sin.y, - local.x * instance.cos_sin.y + local.y * instance.cos_sin.x, - ); - - var out: VertexOutput; - out.clip_position = vec4f((world - camera.centre) * camera.scale, 0.0, 1.0); - out.world = world; - out.instance = n; - - return out; -} - -@fragment -fn fs_main(in: VertexOutput) -> @location(0) vec4 { - let instance = instances[in.instance]; - - let d = in.world - instance.centre; - let q = floor(d) + vec2f(0.5); - let local = vec2f( - q.x * instance.cos_sin.x + q.y * instance.cos_sin.y, - -q.x * instance.cos_sin.y + q.y * instance.cos_sin.x, - ) + instance.half_size; - - let cell = vec2i(floor(local)); - if any(cell < vec2i(0)) || any(cell >= vec2i(instance.dims)) { - discard; - } - - let idx = instance.cell_offset + u32(cell.y) * instance.dims.x + u32(cell.x); - let material = (cells[idx >> 2u] >> ((idx & 3u) * 8u)) & 0xFFu; - if material == 0u { - discard; - } - - return palette[material]; -} -- cgit v1.3.1