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 rendering behind 2D objects if their z value is above 0 #9308

Closed
Gambitboy opened this issue Jul 30, 2023 · 11 comments
Closed

Gizmos rendering behind 2D objects if their z value is above 0 #9308

Gambitboy opened this issue Jul 30, 2023 · 11 comments
Labels
A-Gizmos Visual editor and debug gizmos A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior

Comments

@Gambitboy
Copy link

Bevy version

0.11.0

[Optional] Relevant system information

`AdapterInfo { name: "NVIDIA GeForce GTX 1060 6GB", vendor: 4318, device: 7171, device_type: DiscreteGpu, driver: "NVIDIA", driver_info: "535.98", backend: Vulkan }`

What you did

Setup a basic 2D Camera scene.
Draw some Gizmos to the screen.
Draw a rectangle to the screen with a Z value above 0.

What went wrong

The Gizmos appears behind the rectangle.

Is this supposed to happen or is there a way to make sure the gizmos are always on top.
Or at least to choose at which z value to render

Additional information

I tried setting the depth_bias

.insert_resource(GizmoConfig {
  depth_bias: -1.0,
  ..Default::default()
})

gizmos

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
        .insert_resource(GizmoConfig {
            depth_bias: -1.0,
            ..Default::default()
        })
        .add_systems(Startup, (setup, shapes))
        .add_systems(Update, gizmos)
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn(Camera2dBundle::new_with_far(10000.));
}

fn gizmos(mut gizmos: Gizmos) {
    gizmos.line_2d(Vec2::new(-50., 0.), Vec2::new(50., 0.), Color::GREEN);
    gizmos.circle_2d(Vec2::ZERO, 50., Color::RED).segments(64);
}

fn shapes(mut commands: Commands) {
    commands.spawn(SpriteBundle {
        sprite: Sprite {
            color: Color::rgb(0.25, 0.25, 0.75),
            custom_size: Some(Vec2::new(50.0, 200.0)),
            ..default()
        },
        transform: Transform::from_translation(Vec3::new(0., 0., 0.1)),
        ..default()
    });
}
@Gambitboy Gambitboy added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Jul 30, 2023
@Gambitboy
Copy link
Author

Gambitboy commented Jul 30, 2023

Further on this. In my current game project. I use the ecs_tilemap crate to render multiple layers. Im using rapier physics crate for collisions, rigid bodies and the gizmos on the Collision component doesnt not show up. Since 0.11.0, the debug borders are rendered using gizmos. Since the tilemap is being rendered above a Z of 0 for the different layers.

When I walk off my tilemap or disable it I see the debug borders.

@Gambitboy
Copy link
Author

Gambitboy commented Jul 30, 2023

I've been playing around a bit more, diving into the API docs.
And got a weird workaround it seems.

Is this intended behavior?

Using 2 cameras with different rendering layers.

So my first camera I set to render on top, but make it render layer 0 (default render layer).
My second camera is rendered first but is rendering layer 1.

commands.spawn((
    Camera2dBundle {
        camera_2d: Camera2d {
            clear_color: ClearColorConfig::None,
            ..default()
        },
        camera: Camera {
            order: 1,
            ..default()
        },
        ..default()
    },
    RenderLayers::from_layers(&[0]),
));
commands.spawn((
    Camera2dBundle {
        camera: Camera {
            order: 0,
            ..default()
        },
        ..default()
    },
    RenderLayers::from_layers(&[1]),
));

Then I spawn in my Mesh, Sprite and Collider at RenderLayer 1 and the gizmos all appear on top of everything.

commands.spawn((
    SpriteBundle {
        sprite: Sprite {
            color: Color::rgb(0.25, 0.25, 0.75),
            custom_size: Some(Vec2::new(50.0, 200.0)),
            ..default()
        },
        transform: Transform::from_translation(Vec3::new(0., 0., 100.)),
        ..default()
    },
    RenderLayers::layer(1),
));

let sprite_size = 150.0;

commands.spawn((
    SpriteBundle {
        texture: asset_server.load("Grass.png"),
        transform: Transform::from_xyz(0., 0., 50.),
        ..default()
    },
    RigidBody::Fixed,
    Collider::cuboid(sprite_size / 2., sprite_size / 2.),
    Velocity::zero(),
    RenderLayers::layer(1),
));

@nicopap
Copy link
Contributor

nicopap commented Jul 31, 2023

with depth_bias: -1.0 it should indeed render the gizmos in front of the other 2d shapes. I suspect we are doing something incorrect with orthographic projections.

The RenderLayers is working as intended. Your approach is a correct (and "clean") work around the bug you describe.

@nicopap nicopap added A-Rendering Drawing game state to the screen A-Gizmos Visual editor and debug gizmos and removed S-Needs-Triage This issue needs to be labelled labels Jul 31, 2023
@Gambitboy
Copy link
Author

Awesome, thanks.
I'm fairly new to rust. Only picked it up in May and started my game 3 weeks ago. But I'll give it a shot to see if I can figure out why depth_bias isn't working.

@nicopap
Copy link
Contributor

nicopap commented Jul 31, 2023

I took a very cursory glance at the code, and didn't immediately identify the source of the error. But if it is due to orthographic projection, then the bug lies in crates/bevy_gizmos/src/lines.wgsl.

I'm not sure this is true, but when computing the depth, it should account for orthographic cameras when multiplying by the view projection. The function named calculate_view in crates/bevy_pbr/src/render/pbr_functions.wgsl for example accounts for orthographic projection.

But again I'm not 100% this is the source of the bug, I might be sending you on a wild goose chase.

@Gambitboy
Copy link
Author

Its better than not knowing where to start. I'll look into those functions

@nicopap
Copy link
Contributor

nicopap commented Jul 31, 2023

Make sure to enable the debug_asset_server cargo feature when working on bevy shaders, it let you hot-reload shaders without recompiling the code, saves you a lot of hassle.

@Gambitboy
Copy link
Author

Capture

So I cloned the bevy repo. copied my first code snippet in a example and loaded it up and the gizmos render correctly.
Here is a photo of my project on the left and the bevy repo on the right.
Identical code.

@Gambitboy
Copy link
Author

Maybe it has already gotten fixed

@mockersf
Copy link
Member

maybe #9129

@nicopap
Copy link
Contributor

nicopap commented Jul 31, 2023

yeah. Looks like it. Closing

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 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