@@ -14,17 +14,23 @@ use std::fmt::Debug;
14
14
#[ derive( Copy , Clone , Component , PartialEq , Debug ) ]
15
15
pub struct ColliderDebugColor ( pub Color ) ;
16
16
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
+ }
21
26
22
27
/// Plugin rensponsible for rendering (using lines) what Rapier "sees" when performing
23
28
/// its physics simulation. This is typically useful to check proper
24
29
/// alignment between colliders and your own visual assets.
25
30
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.
28
34
pub global : bool ,
29
35
/// Is the debug-rendering enabled?
30
36
pub enabled : bool ,
@@ -74,8 +80,9 @@ impl RapierDebugRenderPlugin {
74
80
pub struct DebugRenderContext {
75
81
/// Is the debug-rendering currently enabled?
76
82
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.
79
86
pub global : bool ,
80
87
/// Pipeline responsible for rendering. Access `pipeline.mode` and `pipeline.style`
81
88
/// to modify the set of rendered elements, and modify the default coloring rules.
@@ -111,7 +118,8 @@ impl Plugin for RapierDebugRenderPlugin {
111
118
112
119
struct BevyLinesRenderBackend < ' world , ' state , ' a , ' b > {
113
120
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 > ,
115
123
context : & ' b RapierContext ,
116
124
gizmos : Gizmos < ' world , ' state > ,
117
125
}
@@ -132,15 +140,18 @@ impl<'world, 'state, 'a, 'b> BevyLinesRenderBackend<'world, 'state, 'a, 'b> {
132
140
}
133
141
134
142
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
+ }
144
155
}
145
156
_ => true ,
146
157
}
@@ -194,16 +205,16 @@ fn debug_render_scene<'a>(
194
205
mut render_context : ResMut < DebugRenderContext > ,
195
206
gizmos : Gizmos ,
196
207
custom_colors : Query < & ' a ColliderDebugColor > ,
197
- visible_colliders : Query < & ' a ColliderDebug > ,
208
+ override_visibility : Query < & ' a ColliderDebug > ,
198
209
) {
199
210
if !render_context. enabled {
200
211
return ;
201
212
}
202
213
203
214
let mut backend = BevyLinesRenderBackend {
204
- global : render_context. global ,
205
215
custom_colors,
206
- visible_colliders : render_context. global . then ( || visible_colliders) ,
216
+ global_visibility : render_context. global ,
217
+ override_visibility,
207
218
context : & rapier_context,
208
219
gizmos,
209
220
} ;
0 commit comments