Skip to content

Commit 64b2ce0

Browse files
committed
Add AlwaysRender/NeverRender for ColliderDebug
1 parent e37eb7e commit 64b2ce0

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
- Derive `Debug` for `LockedAxes`.
88
- Expose `is_sliding_down_slope` to both `MoveShapeOutput` and `KinematicCharacterControllerOutput`.
9+
- Added a way to configure which colliders should be debug rendered: `global` parameter for both
10+
`RapierDebugColliderPlugin` and `DebugRenderContext`, as well as individual collider setup via
11+
a `ColliderDebug` component.
912

1013
### Fix
1114

src/render/mod.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@ use std::fmt::Debug;
1414
#[derive(Copy, Clone, Component, PartialEq, Debug)]
1515
pub struct ColliderDebugColor(pub Color);
1616

17-
/// Marker to draw debug lines for colliders if `global` in [`DebugRenderContext`]
18-
/// is set to false.
19-
#[derive(Copy, Clone, Component, PartialEq, Debug)]
20-
pub struct ColliderDebug;
17+
/// Overrides the global [`DebugRenderContext`] for a single collider.
18+
#[derive(Copy, Clone, Component, Eq, PartialEq, Default, Debug)]
19+
pub enum ColliderDebug {
20+
/// Always render the debug gizmos for this collider, regardless of global config.
21+
#[default]
22+
AlwaysRender,
23+
/// Never render the debug gizmos for this collider, regardless of global confing.
24+
NeverRender,
25+
}
2126

2227
/// Plugin rensponsible for rendering (using lines) what Rapier "sees" when performing
2328
/// its physics simulation. This is typically useful to check proper
2429
/// alignment between colliders and your own visual assets.
2530
pub struct RapierDebugRenderPlugin {
26-
/// Is the debug-rendering will be enabled for every entity? Or just for
27-
/// collider entities with [`ColliderDebug`] component.
31+
/// Whether to show debug gizmos for all colliders.
32+
///
33+
/// Can be overriden for individual colliders by adding a [`ColliderDebug`] component.
2834
pub global: bool,
2935
/// Is the debug-rendering enabled?
3036
pub enabled: bool,
@@ -74,8 +80,9 @@ impl RapierDebugRenderPlugin {
7480
pub struct DebugRenderContext {
7581
/// Is the debug-rendering currently enabled?
7682
pub enabled: bool,
77-
/// Is the debug-rendering enabled for every entity? Otherwise it will only work
78-
/// for collider entities with [`ColliderDebug`] component.
83+
/// Whether to show debug gizmos for all colliders.
84+
///
85+
/// Can be overriden for individual colliders by adding a [`ColliderDebug`] component.
7986
pub global: bool,
8087
/// Pipeline responsible for rendering. Access `pipeline.mode` and `pipeline.style`
8188
/// to modify the set of rendered elements, and modify the default coloring rules.
@@ -111,7 +118,8 @@ impl Plugin for RapierDebugRenderPlugin {
111118

112119
struct BevyLinesRenderBackend<'world, 'state, 'a, 'b> {
113120
custom_colors: Query<'world, 'state, &'a ColliderDebugColor>,
114-
visible_colliders: Option<Query<'world, 'state, &'a ColliderDebug>>,
121+
global_visibility: bool,
122+
override_visibility: Query<'world, 'state, &'a ColliderDebug>,
115123
context: &'b RapierContext,
116124
gizmos: Gizmos<'world, 'state>,
117125
}
@@ -132,15 +140,18 @@ impl<'world, 'state, 'a, 'b> BevyLinesRenderBackend<'world, 'state, 'a, 'b> {
132140
}
133141

134142
fn drawing_enabled(&self, object: DebugRenderObject) -> bool {
135-
match (object, &self.visible_colliders) {
136-
(DebugRenderObject::Collider(h, ..), Some(visible_colliders)) => {
137-
let collider = self.context.colliders.get(h);
138-
collider
139-
.map(|co| {
140-
let entity = Entity::from_bits(co.user_data as u64);
141-
visible_colliders.contains(entity)
142-
})
143-
.unwrap_or(false)
143+
match object {
144+
DebugRenderObject::Collider(h, ..) => {
145+
let Some(collider) = self.context.colliders.get(h) else {
146+
return true;
147+
};
148+
let entity = Entity::from_bits(collider.user_data as u64);
149+
150+
if let Ok(collider_override) = self.override_visibility.get(entity) {
151+
matches!(collider_override, ColliderDebug::AlwaysRender)
152+
} else {
153+
self.global_visibility
154+
}
144155
}
145156
_ => true,
146157
}
@@ -194,16 +205,16 @@ fn debug_render_scene<'a>(
194205
mut render_context: ResMut<DebugRenderContext>,
195206
gizmos: Gizmos,
196207
custom_colors: Query<&'a ColliderDebugColor>,
197-
visible_colliders: Query<&'a ColliderDebug>,
208+
override_visibility: Query<&'a ColliderDebug>,
198209
) {
199210
if !render_context.enabled {
200211
return;
201212
}
202213

203214
let mut backend = BevyLinesRenderBackend {
204-
global: render_context.global,
205215
custom_colors,
206-
visible_colliders: render_context.global.then(|| visible_colliders),
216+
global_visibility: render_context.global,
217+
override_visibility,
207218
context: &rapier_context,
208219
gizmos,
209220
};

0 commit comments

Comments
 (0)