-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Sprites do not respect Z layering properly. #6048
Comments
#5942 was merged recently and seems potentially related. Could you test on main? |
Here is a reproducible example: //! Displays a single [`Sprite`], created from an image.
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.run();
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let font = asset_server.load("fonts/FiraSans-Bold.ttf");
let text_style = TextStyle {
font,
font_size: 60.0,
color: Color::WHITE,
};
let text_alignment = TextAlignment::CENTER;
commands.spawn_bundle(Camera2dBundle::default());
commands.spawn_bundle(MaterialMesh2dBundle {
mesh: meshes.add(Mesh::from(shape::Quad::default())).into(),
transform: Transform::from_xyz(0.0, 0.0, 10.0).with_scale(Vec3::splat(128.)),
material: materials.add(ColorMaterial::from(Color::PURPLE)),
..default()
});
// Demonstrate changing translation
commands
.spawn_bundle(Text2dBundle {
text: Text::from_section("translation", text_style.clone())
.with_alignment(text_alignment),
transform: Transform::from_xyz(0.0, 0.0, 12.0),
..default()
});
commands
.spawn_bundle(Text2dBundle {
text: Text::from_section("Hello!", text_style.clone())
.with_alignment(text_alignment),
transform: Transform::from_xyz(-100.0, 0.0, 0.0),
..default()
});
} |
Yeah I'll take a look with the latest bevy. |
@rparrett Yes that does appear to also fix the issue. I think if that's the case then the Z sort here is pointless: |
Bevy version
0.8.1
What you did
Attempted to render
Text2D
on top of a separate non-batchedTransparent2D
phase item.What went wrong
The order of draw calls is random and a
Text2D
can draw under a lower z axisTransparent2D
entity.Additional information
I believe this is due to how batches are handled within the sprite renderer which is what
Text2D
uses.bevy_sprite
sorts extracted sprites bytransform.translation.z
value twice. Once during the phase sort and once here:https://github.com/bevyengine/bevy/blob/main/crates/bevy_sprite/src/render/mod.rs#L412
Sorting
extracted_sprites
with the z axis value is fine if all you are rendering is sprites, but when you have other Transparent2D calls you can't guarantee the correct order of draw calls without batching by z axis as well. A fix would be to batch by z axis. This can be done by adding the sort key toSpriteBatch
.I've tested this fix with a local copy of bevy and this resolved my issue.
The text was updated successfully, but these errors were encountered: