summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/bmp.rs4
-rw-r--r--shaders/sprite.wgsl4
-rw-r--r--src/camera.rs2
-rw-r--r--src/instance.rs12
-rw-r--r--src/lib.rs2
-rw-r--r--src/renderer.rs1
6 files changed, 15 insertions, 10 deletions
diff --git a/examples/bmp.rs b/examples/bmp.rs
index 7b649da..f68d573 100644
--- a/examples/bmp.rs
+++ b/examples/bmp.rs
@@ -33,7 +33,8 @@ fn main() {
renderer.instances_mut().push_instance(Instance {
position: [-0.5, 0.5],
- size: [0.75; 2],
+ size: [1.5; 2],
+ z_index: 1.0,
texture_size: [gator_width, gator_height],
texture_coordinates: [gator_x, gator_y],
..Default::default()
@@ -55,6 +56,7 @@ fn main() {
position: [0.5, 0.5],
size: [0.75; 2],
rotation: 0.5,
+ z_index: 1.0,
texture_size: [icon_width, icon_height],
texture_coordinates: [icon_x, icon_y],
..Default::default()
diff --git a/shaders/sprite.wgsl b/shaders/sprite.wgsl
index 43f494e..60b5773 100644
--- a/shaders/sprite.wgsl
+++ b/shaders/sprite.wgsl
@@ -13,7 +13,7 @@ struct InstanceInput {
@location(4) texture_size: vec2<f32>,
@location(5) texture_atlas_index: u32,
@location(6) rotation: f32,
- @location(7) z_layer: u32
+ @location(7) z_index: f32,
}
struct VertexOutput {
@@ -40,7 +40,7 @@ fn vs_main(model: VertexInput, instance: InstanceInput) -> VertexOutput {
let position2d = scaled + instance.position;
// camera stuff
- let position4d = vec4<f32>(position2d, 0.0, 1.0);
+ 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));
diff --git a/src/camera.rs b/src/camera.rs
index 3ae33b4..e77da40 100644
--- a/src/camera.rs
+++ b/src/camera.rs
@@ -175,7 +175,7 @@ impl Camera {
let projection_matrix = Matrix4::new(
self.inverse_aspect_ratio * self.zoom, 0.0, 0.0, 0.0,
0.0, self.zoom, 0.0, 0.0,
- 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 1.0 / 256.0, 0.0,
0.0, 0.0, 0.0, 1.0
);
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),
);
}
}
diff --git a/src/lib.rs b/src/lib.rs
index aefcb70..f5403f2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,7 +5,7 @@
#![warn(clippy::nursery)]
#![allow(clippy::module_name_repetitions)]
-pub mod camera;
+mod camera;
pub mod config;
pub mod instance;
pub mod renderer;
diff --git a/src/renderer.rs b/src/renderer.rs
index 544b01b..e97b0fd 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -114,7 +114,6 @@ fn sprite_render_pipeline(
})
}
-// TODO make this more complete
impl Renderer {
/// Initializes the renderer
///