summaryrefslogtreecommitdiff
path: root/src/shader/instance.wgsl
blob: 4b921ab267203e32d6fd0761c6506fa0ff418e51 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const AMBIENT_LIGHT = vec4f(0.4, 0.4, 0.4, 1.0);
const LIGHT_RESOLUTION = 1.0;

struct Camera {
    scale: vec2f,
    centre: vec2f,
};

struct Lighting {
    origin: vec2f,
    _padding: vec2f,
};

struct Instance {
    centre: vec2f,
    cos_sin: vec2f,
    half_size: vec2f,
    dims: vec2u,
    cell_offset: u32,
};

@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> instances: array<Instance>;
@group(0) @binding(3) var<storage, read> cells: array<u32>;
@group(0) @binding(4) var lighting: texture_2d_array<f32>;
@group(0) @binding(5) var light_sampler: sampler;
@group(0) @binding(6) var<uniform> light_params: Lighting;

struct VertexOutput {
    @builtin(position) clip_position: vec4f,
    @location(0) world: vec2f,
    @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) vec4f {
    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;
    }

    var total = vec3f(0.0);
    let light_dims = vec2f(textureDimensions(lighting));
    let light_texel = (in.world - light_params.origin) * LIGHT_RESOLUTION;
    let light_uv = light_texel / light_dims;
    for (var d = 0u; d < 4u; d++) {
        total += textureSample(lighting, light_sampler, light_uv, d).rgb;
    }
    let light = vec4f(total.r, total.g, total.b, 1.0);

    return palette[material] * (light + AMBIENT_LIGHT);
}