summaryrefslogtreecommitdiff
path: root/alligator_render/shaders
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2022-10-20 20:39:44 -0400
committerMicha White <botahamec@outlook.com>2022-10-20 20:39:44 -0400
commit93347346e8bd8f7412ae03a0858dd307a1df2e0d (patch)
tree17805956857c76b5fed3f47a821fcdf6141cf7a6 /alligator_render/shaders
parente337741969160603f06a7f2b30cda375eeef99fb (diff)
Moved files into workspace
Diffstat (limited to 'alligator_render/shaders')
-rw-r--r--alligator_render/shaders/sprite.wgsl62
1 files changed, 62 insertions, 0 deletions
diff --git a/alligator_render/shaders/sprite.wgsl b/alligator_render/shaders/sprite.wgsl
new file mode 100644
index 0000000..60b5773
--- /dev/null
+++ b/alligator_render/shaders/sprite.wgsl
@@ -0,0 +1,62 @@
+
+@group(0) @binding(0)
+var<uniform> camera: mat4x4<f32>;
+
+struct VertexInput {
+ @location(0) position: vec2<f32>
+}
+
+struct InstanceInput {
+ @location(1) position: vec2<f32>,
+ @location(2) size: vec2<f32>,
+ @location(3) texture_coordinates: vec2<f32>,
+ @location(4) texture_size: vec2<f32>,
+ @location(5) texture_atlas_index: u32,
+ @location(6) rotation: f32,
+ @location(7) z_index: f32,
+}
+
+struct VertexOutput {
+ @builtin(position) clip_position: vec4<f32>,
+ @location(0) texture_coordinates: vec2<f32>,
+ @location(1) texture_atlas_index: u32
+}
+
+@vertex
+fn vs_main(model: VertexInput, instance: InstanceInput) -> VertexOutput {
+ var out: VertexOutput;
+
+ // rotate the sprite
+ let rotation = -instance.rotation;
+ let a = vec2<f32>(cos(rotation), sin(rotation));
+ let b = vec2<f32>(-a[1], a[0]);
+ let rotation = mat2x2<f32>(a, b);
+ let rotated = rotation * model.position;
+
+ // scale the sprite
+ let scaled = rotated * instance.size;
+
+ // move the sprite
+ let position2d = scaled + instance.position;
+
+ // camera stuff
+ let position4d = vec4<f32>(position2d, instance.z_index, 1.0);
+ let position = camera * position4d;
+
+ let tex_coords = vec2<f32>(model.position[0] + 0.5, 1.0 - (model.position[1] + 0.5));
+
+ out.clip_position = position;
+ out.texture_atlas_index = instance.texture_atlas_index;
+ out.texture_coordinates = tex_coords * instance.texture_size + instance.texture_coordinates;
+ return out;
+}
+
+@group(1) @binding(0)
+var t_diffuse: texture_2d<f32>;
+@group(1) @binding(1)
+var s_diffuse: sampler;
+
+@fragment
+fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
+ return textureSample(t_diffuse, s_diffuse, in.texture_coordinates);
+} \ No newline at end of file