Skip to content
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

Closed
StarArawn opened this issue Sep 21, 2022 · 4 comments
Closed

Sprites do not respect Z layering properly. #6048

StarArawn opened this issue Sep 21, 2022 · 4 comments
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior

Comments

@StarArawn
Copy link
Contributor

StarArawn commented Sep 21, 2022

Bevy version

0.8.1

What you did

Attempted to render Text2D on top of a separate non-batched Transparent2D phase item.

What went wrong

The order of draw calls is random and a Text2D can draw under a lower z axis Transparent2D 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 by transform.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 to SpriteBatch.

I've tested this fix with a local copy of bevy and this resolved my issue.

@StarArawn StarArawn added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Sep 21, 2022
@rparrett
Copy link
Contributor

#5942 was merged recently and seems potentially related. Could you test on main?

@StarArawn
Copy link
Contributor Author

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()
    });
}

@StarArawn
Copy link
Contributor Author

#5942 was merged recently and seems potentially related. Could you test on main?

Yeah I'll take a look with the latest bevy.

@StarArawn
Copy link
Contributor Author

@rparrett Yes that does appear to also fix the issue.

I think if that's the case then the Z sort here is pointless:
https://github.com/bevyengine/bevy/blob/main/crates/bevy_sprite/src/render/mod.rs#L412
As the phase sort will already be sorting correctly.

@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen and removed S-Needs-Triage This issue needs to be labelled labels Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

3 participants