diff options
| -rw-r--r-- | shaders/sprite.wgsl | 13 | ||||
| -rw-r--r-- | src/instance.rs | 15 |
2 files changed, 23 insertions, 5 deletions
diff --git a/shaders/sprite.wgsl b/shaders/sprite.wgsl index f8b9e7e..22225f7 100644 --- a/shaders/sprite.wgsl +++ b/shaders/sprite.wgsl @@ -9,12 +9,17 @@ struct VertexInput { struct InstanceInput { @location(1) position: vec2<f32>, @location(2) size: vec2<f32>, - @location(3) rotation: f32, - @location(4) z_layer: u32 + @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_layer: u32 } struct VertexOutput { - @builtin(position) clip_position: vec4<f32> + @builtin(position) clip_position: vec4<f32>, + @location(0) texture_coordinates: vec2<f32>, + @location(1) texture_atlas_index: u32 } @vertex @@ -38,6 +43,8 @@ fn vs_main(model: VertexInput, instance: InstanceInput) -> VertexOutput { let position = camera * position4d; out.clip_position = position; + out.texture_atlas_index = instance.texture_atlas_index; + out.texture_coordinates = (model.position + 0.5) * instance.texture_size + instance.texture_coordinates; return out; } diff --git a/src/instance.rs b/src/instance.rs index 6ea5321..eded8cf 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -12,6 +12,12 @@ pub struct Instance { pub position: [f32; 2], /// Relative size pub size: [f32; 2], + /// The location of the texture in the texture atlas + pub texture_coordinates: [f32; 2], + /// The size of the sprite's texture + pub texture_size: [f32; 2], + /// The index of the texture atlas to use + pub texture_atlas_index: u32, /// Rotation, in radians pub rotation: f32, /// z-index @@ -25,14 +31,19 @@ impl Default for Instance { size: [1.0; 2], rotation: 0.0, z_index: 0, + texture_coordinates: [0.0; 2], + texture_size: [1.0; 2], + texture_atlas_index: 0, } } } impl Instance { // whenever this is updated, please also update `sprite.wgsl` - const ATTRIBUTES: [wgpu::VertexAttribute; 4] = - wgpu::vertex_attr_array![1 => Float32x2, 2 => Float32x2, 3 => Float32, 4 => Uint32]; + const ATTRIBUTES: [wgpu::VertexAttribute; 7] = wgpu::vertex_attr_array![ + 1 => Float32x2, 2 => Float32x2, 3 => Float32x2, 4 => Float32x2, + 5 => Uint32, 6 => Float32, 7 => Uint32 + ]; pub(crate) fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { // make sure these two don't conflict |
