Skip to content

Commit e3798fe

Browse files
committed
Allow not overwriting entity visibility with debug renderer
1 parent fb99129 commit e3798fe

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

src/debug_render/configuration.rs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,8 @@ pub struct PhysicsGizmos {
8383
pub shapecast_point_color: Option<Color>,
8484
/// The color used for the hit normals in [shapecasts](spatial_query#shapecasting).
8585
pub shapecast_normal_color: Option<Color>,
86-
/// Determines if the visibility of entities with [colliders](Collider) should be set to `Visibility::Hidden`,
87-
/// which will only show the debug renders.
88-
pub hide_meshes: bool,
86+
/// Determines if the visibility of entities with [colliders](Collider).
87+
pub mesh_visibility: MeshVisibility,
8988
}
9089

9190
impl Default for PhysicsGizmos {
@@ -107,7 +106,7 @@ impl Default for PhysicsGizmos {
107106
shapecast_shape_color: Some(Color::srgb(0.4, 0.6, 1.0)),
108107
shapecast_point_color: Some(YELLOW.into()),
109108
shapecast_normal_color: Some(PINK.into()),
110-
hide_meshes: false,
109+
mesh_visibility: MeshVisibility::Ignore,
111110
}
112111
}
113112
}
@@ -130,6 +129,32 @@ impl Default for ContactGizmoScale {
130129
}
131130
}
132131

132+
/// Determines if the visibility of entities with [colliders](Collider) should
133+
/// be overwritten. Setting this to `MeshVisibility::Overwrite(Visibility::Hidden)`,
134+
/// will only show the debug renders.
135+
#[derive(Reflect, Clone, Copy, PartialEq, Default)]
136+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
137+
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
138+
#[reflect(PartialEq)]
139+
pub enum MeshVisibility {
140+
/// Do not change the visibility of the entity's mesh.
141+
#[default]
142+
Ignore,
143+
/// Always overwrite the visibility of the entity's mesh.
144+
Overwrite(Visibility),
145+
}
146+
147+
impl MeshVisibility {
148+
/// Returns the first non-ignore visibility.
149+
pub fn or(self, other: Self) -> Self {
150+
if self == Self::Ignore {
151+
other
152+
} else {
153+
self
154+
}
155+
}
156+
}
157+
133158
impl PhysicsGizmos {
134159
/// Creates a [`PhysicsGizmos`] configuration with all rendering options enabled.
135160
pub fn all() -> Self {
@@ -150,7 +175,7 @@ impl PhysicsGizmos {
150175
shapecast_shape_color: Some(Color::srgb(0.4, 0.6, 1.0)),
151176
shapecast_point_color: Some(YELLOW.into()),
152177
shapecast_normal_color: Some(PINK.into()),
153-
hide_meshes: true,
178+
mesh_visibility: MeshVisibility::Overwrite(Visibility::Hidden),
154179
}
155180
}
156181

@@ -175,7 +200,7 @@ impl PhysicsGizmos {
175200
shapecast_shape_color: None,
176201
shapecast_point_color: None,
177202
shapecast_normal_color: None,
178-
hide_meshes: false,
203+
mesh_visibility: MeshVisibility::Ignore,
179204
}
180205
}
181206

@@ -314,8 +339,8 @@ impl PhysicsGizmos {
314339
}
315340

316341
/// Sets the visibility of the entity's visual mesh.
317-
pub fn with_mesh_visibility(mut self, is_visible: bool) -> Self {
318-
self.hide_meshes = !is_visible;
342+
pub fn with_mesh_visibility(mut self, visibility: MeshVisibility) -> Self {
343+
self.mesh_visibility = visibility;
319344
self
320345
}
321346

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

428453
impl Default for DebugRender {
@@ -435,7 +460,7 @@ impl Default for DebugRender {
435460
aabb_color: None,
436461
collider_color: Some(ORANGE.into()),
437462
sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
438-
hide_mesh: false,
463+
mesh_visibility: MeshVisibility::Ignore,
439464
}
440465
}
441466
}
@@ -451,7 +476,7 @@ impl DebugRender {
451476
aabb_color: Some(Color::srgb(0.8, 0.8, 0.8)),
452477
collider_color: Some(ORANGE.into()),
453478
sleeping_color_multiplier: Some([1.0, 1.0, 0.4, 1.0]),
454-
hide_mesh: true,
479+
mesh_visibility: MeshVisibility::Overwrite(Visibility::Hidden),
455480
}
456481
}
457482

@@ -462,7 +487,7 @@ impl DebugRender {
462487
aabb_color: None,
463488
collider_color: None,
464489
sleeping_color_multiplier: None,
465-
hide_mesh: false,
490+
mesh_visibility: MeshVisibility::Ignore,
466491
}
467492
}
468493

@@ -518,8 +543,8 @@ impl DebugRender {
518543
}
519544

520545
/// Sets the visibility of the entity's visual mesh.
521-
pub fn with_mesh_visibility(mut self, is_visible: bool) -> Self {
522-
self.hide_mesh = !is_visible;
546+
pub fn with_mesh_visibility(mut self, visibility: MeshVisibility) -> Self {
547+
self.mesh_visibility = visibility;
523548
self
524549
}
525550

src/debug_render/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,17 @@ fn change_mesh_visibility(
482482
let config = store.config::<PhysicsGizmos>();
483483
if store.is_changed() {
484484
for (mut visibility, render_config) in &mut meshes {
485-
let hide_mesh =
486-
config.0.enabled && render_config.map_or(config.1.hide_meshes, |c| c.hide_mesh);
487-
if hide_mesh {
488-
*visibility = Visibility::Hidden;
489-
} else {
490-
*visibility = Visibility::Visible;
485+
if !config.0.enabled {
486+
continue;
487+
}
488+
489+
let mesh_visibility = render_config
490+
.map(|global_config| global_config.mesh_visibility)
491+
.unwrap_or_default()
492+
.or(config.1.mesh_visibility);
493+
494+
if let MeshVisibility::Overwrite(value) = mesh_visibility {
495+
*visibility = value;
491496
}
492497
}
493498
}

0 commit comments

Comments
 (0)