-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add ZIndex, YSort, and SortBias 2d components
#19463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
c5c596a
2ca0b82
c4d529e
2cb7c01
ca4b74b
ee142c5
88c5ee7
96ccfde
c0cc713
9c17a77
b0e028d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,6 +137,26 @@ impl Plugin for Mesh2dRenderPlugin { | |
| } | ||
| } | ||
|
|
||
| /// Describes the layer in which the sprite or mesh should be rendered. Higher values are rendered | ||
| /// on top of lower values, with a default value of 0. This is useful for controlling the rendering | ||
| /// order of sprites and always takes precedence over [`YSort`] or [`SortBias`]. | ||
| #[derive(Component, Deref, DerefMut, Default, Debug, Clone)] | ||
| pub struct ZIndex(pub i32); | ||
|
|
||
| /// A marker component that enables Y-sorting (depth sorting) for sprites and meshes. | ||
| /// | ||
| /// When attached to an entity, this component indicates that the entity should be rendered | ||
| /// in draw order based on its Y position. Entities with lower Y values (higher on screen) | ||
| /// are drawn first, creating a depth illusion where objects lower on the screen appear | ||
| /// in front of objects higher on the screen. | ||
| #[derive(Component, Default, Debug, Clone)] | ||
| pub struct YSort; | ||
|
Comment on lines
+146
to
+153
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now, for entities on the same z index layer, an entity with y sort enabled will always sort before one without it. Not sure if this is intuitive but it is consistent. |
||
|
|
||
| /// An arbitrary bias value that can be applied to the sorting order of sprites and meshes and is | ||
| /// applied after the [`ZIndex`] or added to the Y position of the entity if [`YSort`] is enabled. | ||
| #[derive(Component, Deref, DerefMut, Default, Debug, Clone)] | ||
| pub struct SortBias(pub f32); | ||
|
|
||
| #[derive(Resource, Deref, DerefMut, Default, Debug, Clone)] | ||
| pub struct ViewKeyCache(MainEntityHashMap<Mesh2dPipelineKey>); | ||
|
|
||
|
|
@@ -228,6 +248,9 @@ pub struct RenderMesh2dInstance { | |
| pub material_bind_group_id: Material2dBindGroupId, | ||
| pub automatic_batching: bool, | ||
| pub tag: u32, | ||
| pub z_index: Option<i32>, | ||
| pub y_sort: bool, | ||
| pub sort_bias: Option<f32>, | ||
| } | ||
|
|
||
| #[derive(Default, Resource, Deref, DerefMut)] | ||
|
|
@@ -245,13 +268,27 @@ pub fn extract_mesh2d( | |
| &GlobalTransform, | ||
| &Mesh2d, | ||
| Option<&MeshTag>, | ||
| Option<&ZIndex>, | ||
| Has<YSort>, | ||
| Option<&SortBias>, | ||
| Has<NoAutomaticBatching>, | ||
| )>, | ||
| >, | ||
| ) { | ||
| render_mesh_instances.clear(); | ||
|
|
||
| for (entity, view_visibility, transform, handle, tag, no_automatic_batching) in &query { | ||
| for ( | ||
| entity, | ||
| view_visibility, | ||
| transform, | ||
| handle, | ||
| tag, | ||
| z_index, | ||
| y_sort, | ||
| sort_bias, | ||
| no_automatic_batching, | ||
| ) in &query | ||
| { | ||
| if !view_visibility.get() { | ||
| continue; | ||
| } | ||
|
|
@@ -266,6 +303,9 @@ pub fn extract_mesh2d( | |
| material_bind_group_id: Material2dBindGroupId::default(), | ||
| automatic_batching: !no_automatic_batching, | ||
| tag: tag.map_or(0, |i| **i), | ||
| z_index: z_index.cloned().map(|x| x.0), | ||
| y_sort, | ||
| sort_bias: sort_bias.cloned().map(|sb| sb.0), | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -238,6 +238,9 @@ pub fn extract_text2d_sprite( | |
| kind: bevy_sprite::ExtractedSpriteKind::Slices { | ||
| indices: start..end, | ||
| }, | ||
| z_index: 0, | ||
| y_sort: false, | ||
| sort_bias: None, | ||
|
Comment on lines
+241
to
+243
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }); | ||
| start = end; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.