Skip to content

Allow not overwriting entity visibility with debug renderer #470

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions src/debug_render/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ pub struct PhysicsGizmos {
pub shapecast_point_color: Option<Color>,
/// The color used for the hit normals in [shapecasts](spatial_query#shapecasting).
pub shapecast_normal_color: Option<Color>,
/// Determines if the visibility of entities with [colliders](Collider) should be set to `Visibility::Hidden`,
/// which will only show the debug renders.
pub hide_meshes: bool,
/// Determines if the visibility of entities with [colliders](Collider).
pub mesh_visibility: MeshVisibility,
}

impl Default for PhysicsGizmos {
Expand All @@ -107,7 +106,7 @@ impl Default for PhysicsGizmos {
shapecast_shape_color: Some(Color::srgb(0.4, 0.6, 1.0)),
shapecast_point_color: Some(YELLOW.into()),
shapecast_normal_color: Some(PINK.into()),
hide_meshes: false,
mesh_visibility: MeshVisibility::Ignore,
}
}
}
Expand All @@ -130,6 +129,32 @@ impl Default for ContactGizmoScale {
}
}

/// Determines if the visibility of entities with [colliders](Collider) should
/// be overwritten. Setting this to `MeshVisibility::Overwrite(Visibility::Hidden)`,
/// will only show the debug renders.
#[derive(Reflect, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(PartialEq)]
pub enum MeshVisibility {
/// Do not change the visibility of the entity's mesh.
#[default]
Ignore,
/// Always overwrite the visibility of the entity's mesh.
Overwrite(Visibility),
}

impl MeshVisibility {
/// Returns the first non-ignore visibility.
pub fn or(self, other: Self) -> Self {
if self == Self::Ignore {
other
} else {
self
}
}
}

impl PhysicsGizmos {
/// Creates a [`PhysicsGizmos`] configuration with all rendering options enabled.
pub fn all() -> Self {
Expand All @@ -150,7 +175,7 @@ impl PhysicsGizmos {
shapecast_shape_color: Some(Color::srgb(0.4, 0.6, 1.0)),
shapecast_point_color: Some(YELLOW.into()),
shapecast_normal_color: Some(PINK.into()),
hide_meshes: true,
mesh_visibility: MeshVisibility::Overwrite(Visibility::Hidden),
}
}

Expand All @@ -175,7 +200,7 @@ impl PhysicsGizmos {
shapecast_shape_color: None,
shapecast_point_color: None,
shapecast_normal_color: None,
hide_meshes: false,
mesh_visibility: MeshVisibility::Ignore,
}
}

Expand Down Expand Up @@ -314,8 +339,8 @@ impl PhysicsGizmos {
}

/// Sets the visibility of the entity's visual mesh.
pub fn with_mesh_visibility(mut self, is_visible: bool) -> Self {
self.hide_meshes = !is_visible;
pub fn with_mesh_visibility(mut self, visibility: MeshVisibility) -> Self {
self.mesh_visibility = visibility;
self
}

Expand Down Expand Up @@ -421,8 +446,8 @@ pub struct DebugRender {
/// If the entity is [sleeping](Sleeping), its colors (in HSLA) will be multiplied by this array.
/// If `None`, sleeping will have no effect on the colors.
pub sleeping_color_multiplier: Option<[f32; 4]>,
/// Determines if the entity's visibility should be set to `Visibility::Hidden`, which will only show the debug render.
pub hide_mesh: bool,
/// Determines if the entity's visibility should be overwritten.
pub mesh_visibility: MeshVisibility,
}

impl Default for DebugRender {
Expand All @@ -435,7 +460,7 @@ impl Default for DebugRender {
aabb_color: None,
collider_color: Some(ORANGE.into()),
sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
hide_mesh: false,
mesh_visibility: MeshVisibility::Ignore,
}
}
}
Expand All @@ -451,7 +476,7 @@ impl DebugRender {
aabb_color: Some(Color::srgb(0.8, 0.8, 0.8)),
collider_color: Some(ORANGE.into()),
sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
hide_mesh: true,
mesh_visibility: MeshVisibility::Overwrite(Visibility::Hidden),
}
}

Expand All @@ -462,7 +487,7 @@ impl DebugRender {
aabb_color: None,
collider_color: None,
sleeping_color_multiplier: None,
hide_mesh: false,
mesh_visibility: MeshVisibility::Ignore,
}
}

Expand Down Expand Up @@ -518,8 +543,8 @@ impl DebugRender {
}

/// Sets the visibility of the entity's visual mesh.
pub fn with_mesh_visibility(mut self, is_visible: bool) -> Self {
self.hide_mesh = !is_visible;
pub fn with_mesh_visibility(mut self, visibility: MeshVisibility) -> Self {
self.mesh_visibility = visibility;
self
}

Expand Down
17 changes: 11 additions & 6 deletions src/debug_render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,17 @@ fn change_mesh_visibility(
let config = store.config::<PhysicsGizmos>();
if store.is_changed() {
for (mut visibility, render_config) in &mut meshes {
let hide_mesh =
config.0.enabled && render_config.map_or(config.1.hide_meshes, |c| c.hide_mesh);
if hide_mesh {
*visibility = Visibility::Hidden;
} else {
*visibility = Visibility::Visible;
if !config.0.enabled {
continue;
}

let mesh_visibility = render_config
.map(|global_config| global_config.mesh_visibility)
.unwrap_or_default()
.or(config.1.mesh_visibility);

if let MeshVisibility::Overwrite(value) = mesh_visibility {
*visibility = value;
}
}
}
Expand Down