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

Gizmos are rendered behind meshes with positive z-coordinate on 2d #17053

Closed
Dentosal opened this issue Dec 30, 2024 · 1 comment · Fixed by #17085
Closed

Gizmos are rendered behind meshes with positive z-coordinate on 2d #17053

Dentosal opened this issue Dec 30, 2024 · 1 comment · Fixed by #17085
Labels
A-Gizmos Visual editor and debug gizmos C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!

Comments

@Dentosal
Copy link
Contributor

Bevy version

Bevy 0.15.0 from crates.io. The result is same with 9cebc66 (latest main at the time of writing).

$ cargo --version
cargo 1.83.0
$ cargo run
[...]
SystemInfo { os: "MacOS 14.6.1 ", kernel: "23.6.0", cpu: "Apple M3 Max", core_count: "16", memory: "128.0 GiB" }
AdapterInfo { name: "Apple M3 Max", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }

What you did

I'm trying to use gizmos with 2d camera, while using z coordinate to maintain draw ordering of my objects.

use bevy::{color::palettes::css::*, prelude::*};
fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .insert_resource(ClearColor(WHITE.into()))
        .add_systems(Startup, setup)
        .add_systems(Update, show_gizmo)
        .run();
}
fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    commands.spawn(Camera2d);
    commands.spawn((
        Mesh2d(meshes.add(Rectangle::new(50.0, 50.0))),
        MeshMaterial2d(materials.add(Color::from(BLACK))),
        Transform::from_xyz(-50.0, 0.0, 0.0),
    ));
    commands.spawn((
        Mesh2d(meshes.add(Rectangle::new(50.0, 50.0))),
        MeshMaterial2d(materials.add(Color::from(BLACK))),
        Transform::from_xyz(50.0, 0.0, 1.0),
    ));
}
fn show_gizmo(mut gizmos: Gizmos) {
    gizmos.line_2d(Vec2::new(-200.0, 0.0), Vec2::new(200.0, 0.0), RED);
}
Image

What went wrong

I was expecting the gizmos to be rendered on top. However, meshes with z-coordinate > 0 are rendered on top of gizmos.

Curiously the behavior on bevy 0.14.2 is what I expected. (Click to show code)
use bevy::{color::palettes::css::*, prelude::*, sprite::MaterialMesh2dBundle};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .insert_resource(ClearColor(WHITE.into()))
        .add_systems(Startup, setup)
        .add_systems(Update, show_gizmo)
        .run();
}
fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    commands.spawn(Camera2dBundle::default());
    commands.spawn(MaterialMesh2dBundle {
        mesh: meshes.add(Rectangle::new(50.0, 50.0)).into(),
        transform: Transform::from_xyz(-50.0, 0.0, 0.0),
        material: materials.add(Color::from(BLACK)),
        ..default()
    });
    commands.spawn(MaterialMesh2dBundle {
        mesh: meshes.add(Rectangle::new(50.0, 50.0)).into(),
        transform: Transform::from_xyz(50.0, 0.0, 1.0),
        material: materials.add(Color::from(BLACK)),
        ..default()
    });
}
fn show_gizmo(mut gizmos: Gizmos) {
    gizmos.line_2d(Vec2::new(-200.0, 0.0), Vec2::new(200.0, 0.0), RED);
}
Image
@Dentosal Dentosal added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Dec 30, 2024
@BenjaminBrienen BenjaminBrienen added S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! A-Gizmos Visual editor and debug gizmos D-Straightforward Simple bug fixes and API improvements, docs, test and examples and removed S-Needs-Triage This issue needs to be labelled labels Jan 1, 2025
@skimmedsquare
Copy link
Contributor

This change in behavior seems to have been introduced with 5abc32c - comparing it and the parent commit with the example snippets provided shows the breakage.

I think I have a potential fix for this, though I'm relatively unfamiliar with rendering.

github-merge-queue bot pushed a commit that referenced this issue Jan 5, 2025
# Objective

- As stated in the linked issue, if a Mesh2D is drawn with elements with
a positive Z value, resulting gizmos get drawn behind instead of in
front of them.
- Fixes #17053

## Solution

- Similar to the change done for the `SpritePipeline` in the relevant
commit (5abc32c), this PR changes both
line gizmos to avoid writing to the depth buffer and always pass the
depth test to ensure they are not filtered out.

## Testing

- Tested with the provided snippet in #17053 
- I looked over the `2d_gizmos` example, but it seemed like adding more
elements there to demonstrate this might not be the best idea? Looking
for guidance here on if that should be updated or if a new gizmo example
needs to be made.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Gizmos Visual editor and debug gizmos C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants