From 797d3f5c398f32238fe8649e5fb185dfa6ea686d Mon Sep 17 00:00:00 2001 From: Jakub Arnold Date: Mon, 1 Jan 2024 17:00:22 +0100 Subject: [PATCH] Add y_sort_offset --- comfy-core/src/lib.rs | 1 + comfy-core/src/quad.rs | 5 +++++ comfy-wgpu/src/batching.rs | 4 +++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/comfy-core/src/lib.rs b/comfy-core/src/lib.rs index 9f690d0..a7f9751 100644 --- a/comfy-core/src/lib.rs +++ b/comfy-core/src/lib.rs @@ -633,6 +633,7 @@ pub struct Mesh { pub indices: SmallVec<[u32; 6]>, pub z_index: i32, pub texture: Option, + pub y_sort_offset: f32, } #[repr(C)] diff --git a/comfy-core/src/quad.rs b/comfy-core/src/quad.rs index b58b63c..ee19b55 100644 --- a/comfy-core/src/quad.rs +++ b/comfy-core/src/quad.rs @@ -185,6 +185,10 @@ pub struct DrawTextureProParams { pub blend_mode: BlendMode, /// Rotation around the x axis for creating a 3d effect. pub rotation_x: f32, + /// Offset for y-sorting in world-space. This is useful for when sprites have different + /// sizes and pivots, for example when a tree should be "behind" something based on its bottom-most + /// pixel, and not based on its size. + pub y_sort_offset: f32, } impl Default for DrawTextureProParams { @@ -199,6 +203,7 @@ impl Default for DrawTextureProParams { flip_y: false, blend_mode: Default::default(), rotation_x: 0.0, + y_sort_offset: 0.0, } } } diff --git a/comfy-wgpu/src/batching.rs b/comfy-wgpu/src/batching.rs index 139a0d6..60d4629 100644 --- a/comfy-wgpu/src/batching.rs +++ b/comfy-wgpu/src/batching.rs @@ -40,7 +40,9 @@ pub fn run_batched_render_passes( // TODO: add this back later if get_y_sort(key.z_index) { - meshes.sort_by_key(|mesh| OrderedFloat::(-mesh.origin.y)); + meshes.sort_by_key(|mesh| { + OrderedFloat::(-(mesh.origin.y + mesh.y_sort_offset)) + }); } render_meshes(