diff options
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; } |
