summaryrefslogtreecommitdiff
path: root/src/shader/shader.wgsl
diff options
context:
space:
mode:
Diffstat (limited to 'src/shader/shader.wgsl')
-rw-r--r--src/shader/shader.wgsl69
1 files changed, 48 insertions, 21 deletions
diff --git a/src/shader/shader.wgsl b/src/shader/shader.wgsl
index 9baa4b7..fb1e0b1 100644
--- a/src/shader/shader.wgsl
+++ b/src/shader/shader.wgsl
@@ -1,44 +1,71 @@
-struct ChunkData {
- origin: vec2<i32>,
-};
-
-var<immediate> chunk: ChunkData;
-
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<uniform> camera: Camera;
-@group(1) @binding(0) var tex: texture_2d<f32>;
+@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>;
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
- @location(0) uv: vec2<f32>,
+ @location(0) world: vec2<f32>,
+ @location(1) @interpolate(flat) instance: u32,
};
@vertex
fn vs_main(
@builtin(vertex_index) i: u32,
+ @builtin(instance_index) n: u32,
) -> VertexOutput {
- var out: VertexOutput;
- // (0,0),(1,0),(0,1),(1,1)
- let corner = vec2f(f32(i & 1u), f32(i >> 1u));
+ let instance = instances[n];
- // CHUNK_SIZE
- let world = (vec2f(chunk.origin) + corner * 128);
- let ndc = (world - camera.centre) * camera.scale;
+ // (-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,
+ );
- out.clip_position = vec4f(ndc, 0.0, 1.0);
- out.uv = 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<f32> {
- // CHUNK_SIZE
- let dims = vec2f(128.0, 128.0);
- return textureLoad(tex, vec2i(in.uv * dims), 0);
- // return vec4f(0.0, 1.0, 0.0, 1.0);
-} \ No newline at end of file
+ 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];
+}