summaryrefslogtreecommitdiff
path: root/src/shader/particle.wgsl
blob: 227468b2deadd44c7ebbca6d8ad6fea63cdc5711 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
struct Camera {
    scale: vec2f,
    centre: vec2f,
};

struct Particle {
    position: vec2f,
    data: u32,
    life: f32,
};

@group(0) @binding(0) var<uniform> camera: Camera;
@group(0) @binding(1) var<storage, read> palette: array<vec4f>;
@group(0) @binding(2) var<storage, read> particles: array<Particle>;

struct VertexOutput {
    @builtin(position) clip_position: vec4f,
    @location(0) world: vec2f,
    @location(1) @interpolate(flat) instance: u32,
    @location(2) @interpolate(flat) material: u32,
    @location(3) @interpolate(flat) life: f32,
};

@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 = vec2f(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;
    out.material = particle.data & 0xFF;
    out.life = particle.life;

    return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4f {
    let particle = particles[in.instance];
    // TODO can pack more in here and shift
    let c = palette[in.material];
    // if life is less than 1, smoothly reduce alpha to 0
    return vec4f(c.r, c.g, c.b, c.a * smoothstep(0.0, 1, in.life));
}