bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.09k stars 3.56k forks source link

Circle gizmos on sphere become dotted line #12908

Closed lomirus closed 6 months ago

lomirus commented 7 months ago

Bevy version

0.13.1

What you did

I want to draw the circle gizmos on a sphere:

Click to expand the example code ```rust use bevy::prelude::*; use std::f32::consts::PI; pub(crate) const EARTH_RADIUS: f32 = 100.0; fn init( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, ) { commands.spawn(Camera3dBundle { transform: Transform::from_xyz( 0.0, 0.0, EARTH_RADIUS / (std::f32::consts::FRAC_PI_8.sin()), ) .looking_at(Vec3::ZERO, Vec3::Y), ..default() }); commands.spawn(PbrBundle { mesh: meshes.add(Mesh::from(Sphere { radius: EARTH_RADIUS, })), material: materials.add(StandardMaterial { base_color: Color::hex("#007FFF").unwrap(), ..default() }), transform: Transform::from_xyz(0., 0., 0.0), ..default() }); } fn draw_latitudes(mut gizmos: Gizmos) { let mut latitude = -PI / 2.0; while latitude <= PI / 2.0 { gizmos.circle( Vec3::new(0., latitude.sin() * EARTH_RADIUS, 0.), Direction3d::Y, latitude.cos() * EARTH_RADIUS, Color::hex(if latitude == 0.0 { "#ff0000" } else { "#ffffff" }) .unwrap(), ); latitude += PI / 6.0; } } fn draw_longitudes(mut gizmos: Gizmos) { let mut longitude = 0.0; while longitude < PI { gizmos.circle( Vec3::ZERO, Direction3d::new(Vec3::new(longitude.sin(), 0.0, longitude.cos())).unwrap(), EARTH_RADIUS, Color::hex(if longitude == 0.0 { "#ff0000" } else { "#ffffff" }) .unwrap(), ); longitude += PI / 6.0; } } fn main() { App::new() .add_systems(Startup, init) .add_systems(Update, (draw_latitudes, draw_longitudes)) .add_plugins(DefaultPlugins) .run(); } ```

What went wrong

The circle gizmos became dotted line. I think it's because the circle gizmos are not real "circle" and so some segments are behind the sphere surface.

Additional information

image

alice-i-cecile commented 7 months ago

I would generally suggest making gizmos that wrap objects like this a tiny bit larger than their object. That will avoid the z-fighting that you're seeing here. There may be a good engine-level fix too though.

lynn-lumen commented 7 months ago

You could also set the depth_bias in the GizmoConfig to something slightly negative like -0.01. This should fix your problem by rendering all gizmo lines as if they were slightly closer to the camera.

alice-i-cecile commented 6 months ago

I'm content with the depth_bias solution: that's exactly what I had in mind :) Docs for it seem good, although they're not super discoverable.