diff options
| author | Micha White <botahamec@outlook.com> | 2022-09-18 16:45:05 -0400 |
|---|---|---|
| committer | Micha White <botahamec@outlook.com> | 2022-09-18 16:45:05 -0400 |
| commit | 83295e01008bdf25e03f6b5aa18b93b735a5e326 (patch) | |
| tree | 7010e0f2fb8994a7168e3a11b4fa996804a49342 /shaders | |
| parent | 3e913d329ebdb36b66ccb8ac7ccea203db266d20 (diff) | |
instancing
Diffstat (limited to 'shaders')
| -rw-r--r-- | shaders/sprite.wgsl | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/shaders/sprite.wgsl b/shaders/sprite.wgsl index d126cae..b8ce42b 100644 --- a/shaders/sprite.wgsl +++ b/shaders/sprite.wgsl @@ -3,14 +3,35 @@ struct VertexInput { @location(0) position: vec2<f32> } +struct InstanceInput { + @location(1) position: vec2<f32>, + @location(2) size: vec2<f32>, + @location(3) rotation: f32, + @location(4) z_layer: i32 +} + struct VertexOutput { @builtin(position) clip_position: vec4<f32> } @vertex -fn vs_main(model: VertexInput) -> VertexOutput { +fn vs_main(model: VertexInput, instance: InstanceInput) -> VertexOutput { var out: VertexOutput; - out.clip_position = vec4<f32>(model.position, 0.0, 1.0); + + // rotate the sprite + let a = vec2<f32>(cos(instance.rotation), sin(instance.rotation)); + let b = vec2<f32>(-a[1], a[0]); + let rotation = mat2x2<f32>(a, b); + let rotated = rotation * model.position; + + // scale the sprite + let x = rotated[0] * instance.size[0]; + let y = rotated[1] * instance.size[1]; + + // move the sprite + let position = vec2<f32>(x, y) + instance.position; + + out.clip_position = vec4<f32>(position, f32(instance.z_layer), 1.0); return out; } |
