From 718688e791d4475903c276d38079223cbcf14890 Mon Sep 17 00:00:00 2001 From: Asier Illarramendi Date: Mon, 4 Nov 2024 23:18:41 +0100 Subject: [PATCH] Use `stack_z_offsets` in all the cases we create a `TransparentUi` (#16197) # Objective Use same pattern when creating `TransparentUi` items where the `sort_key` is the `UiNode` stack index + some offset. ## Solution Refactored to follow same pattern. ## Testing Ran few UI examples. ## Doubts Maybe `stack_z_offsets::BACKGROUND_COLOR` should be renamed. This is used for `ExtractedUiNode`, which is not only used for "background color" it's also used to render borders, images and text (I think). --- crates/bevy_ui/src/render/box_shadow.rs | 4 ++-- crates/bevy_ui/src/render/mod.rs | 8 +++++--- crates/bevy_ui/src/render/ui_texture_slice_pipeline.rs | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ui/src/render/box_shadow.rs b/crates/bevy_ui/src/render/box_shadow.rs index 7727567a09..d51028b1da 100644 --- a/crates/bevy_ui/src/render/box_shadow.rs +++ b/crates/bevy_ui/src/render/box_shadow.rs @@ -32,7 +32,7 @@ use bevy_render::{ use bevy_transform::prelude::GlobalTransform; use bytemuck::{Pod, Zeroable}; -use super::{QUAD_INDICES, QUAD_VERTEX_POSITIONS}; +use super::{stack_z_offsets, QUAD_INDICES, QUAD_VERTEX_POSITIONS}; pub const BOX_SHADOW_SHADER_HANDLE: Handle = Handle::weak_from_u128(17717747047134343426); @@ -365,7 +365,7 @@ pub fn queue_shadows( pipeline, entity: (*entity, extracted_shadow.main_entity), sort_key: ( - FloatOrd(extracted_shadow.stack_index as f32 - 0.1), + FloatOrd(extracted_shadow.stack_index as f32 + stack_z_offsets::BOX_SHADOW), entity.index(), ), batch_range: 0..0, diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 417a6593d1..c934ed0262 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -75,7 +75,7 @@ pub mod graph { /// When this is _not_ possible, pick a suitably unique index unlikely to clash with other things (ex: `0.1826823` not `0.1`). /// /// Offsets should be unique for a given node entity to avoid z fighting. -/// These should pretty much _always_ be larger than -1.0 and smaller than 1.0 to avoid clipping into nodes +/// These should pretty much _always_ be larger than -0.5 and smaller than 0.5 to avoid clipping into nodes /// above / below the current node in the stack. /// /// A z-index of 0.0 is the baseline, which is used as the primary "background color" of the node. @@ -83,7 +83,9 @@ pub mod graph { /// Note that nodes "stack" on each other, so a negative offset on the node above could clip _into_ /// a positive offset on a node below. pub mod stack_z_offsets { - pub const BACKGROUND_COLOR: f32 = 0.0; + pub const BOX_SHADOW: f32 = -0.1; + pub const TEXTURE_SLICE: f32 = 0.0; + pub const NODE: f32 = 0.0; pub const MATERIAL: f32 = 0.18267; } @@ -861,7 +863,7 @@ pub fn queue_uinodes( pipeline, entity: (*entity, extracted_uinode.main_entity), sort_key: ( - FloatOrd(extracted_uinode.stack_index as f32 + stack_z_offsets::BACKGROUND_COLOR), + FloatOrd(extracted_uinode.stack_index as f32 + stack_z_offsets::NODE), entity.index(), ), // batch_range will be calculated in prepare_uinodes diff --git a/crates/bevy_ui/src/render/ui_texture_slice_pipeline.rs b/crates/bevy_ui/src/render/ui_texture_slice_pipeline.rs index 2824186bbe..6df45d0de2 100644 --- a/crates/bevy_ui/src/render/ui_texture_slice_pipeline.rs +++ b/crates/bevy_ui/src/render/ui_texture_slice_pipeline.rs @@ -365,7 +365,7 @@ pub fn queue_ui_slices( pipeline, entity: (*entity, extracted_slicer.main_entity), sort_key: ( - FloatOrd(extracted_slicer.stack_index as f32), + FloatOrd(extracted_slicer.stack_index as f32 + stack_z_offsets::TEXTURE_SLICE), entity.index(), ), batch_range: 0..0,