From e017b675d96c58924071ca6c739bbd7910a2b2b4 Mon Sep 17 00:00:00 2001 From: Micha White Date: Mon, 17 Oct 2022 22:45:31 -0400 Subject: Depth ordering --- src/instance.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/instance.rs') diff --git a/src/instance.rs b/src/instance.rs index bacad56..2bff797 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -21,7 +21,7 @@ pub struct Instance { /// Rotation, in radians pub rotation: f32, /// z-index - pub z_index: u32, // TODO something more interesting with z-axis + pub z_index: f32, } impl Default for Instance { @@ -30,7 +30,7 @@ impl Default for Instance { position: [0.0; 2], size: [1.0; 2], rotation: 0.0, - z_index: 0, + z_index: 0.0, texture_coordinates: [0.0; 2], texture_size: [1.0; 2], texture_atlas_index: 0, @@ -42,7 +42,7 @@ impl Instance { // whenever this is updated, please also update `sprite.wgsl` const ATTRIBUTES: [wgpu::VertexAttribute; 7] = wgpu::vertex_attr_array![ 1 => Float32x2, 2 => Float32x2, 3 => Float32x2, 4 => Float32x2, - 5 => Uint32, 6 => Float32, 7 => Uint32 + 5 => Uint32, 6 => Float32, 7 => Float32 ]; pub(crate) fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { @@ -135,10 +135,14 @@ impl InstanceBuffer { self.expand_buffer(device); } + // the instances must be sorted by z-index before being handed to the GPU + let mut sorted = self.instances.clone(); + sorted.sort_by(|a, b| a.z_index.total_cmp(&b.z_index)); + queue.write_buffer( &self.instance_buffer, 0 as wgpu::BufferAddress, - bytemuck::cast_slice(&self.instances), + bytemuck::cast_slice(&sorted), ); } } -- cgit v1.2.3