Skip to content

Commit

Permalink
implement per-entity debug rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
barsoosayque committed Nov 12, 2023
1 parent fdf7172 commit 32a0b41
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ use std::fmt::Debug;
#[derive(Copy, Clone, Component, PartialEq, Debug)]
pub struct ColliderDebugColor(pub Color);

/// Marker to draw debug lines for colliders if `global` in [`DebugRenderContext`]
/// is set to false.
#[derive(Copy, Clone, Component, PartialEq, Debug)]
pub struct ColliderDebug;

/// Plugin rensponsible for rendering (using lines) what Rapier "sees" when performing
/// its physics simulation. This is typically useful to check proper
/// alignment between colliders and your own visual assets.
pub struct RapierDebugRenderPlugin {
/// Is the debug-rendering will be enabled for every entity? Or just for
/// collider entities with [`ColliderDebug`] component.
pub global: bool,
/// Is the debug-rendering enabled?
pub enabled: bool,
/// Control some aspects of the render coloring.
Expand All @@ -33,6 +41,7 @@ impl Default for RapierDebugRenderPlugin {
fn default() -> Self {
Self {
enabled: true,
global: true,
style: DebugRenderStyle {
rigid_body_axes_length: 20.0,
..Default::default()
Expand All @@ -44,6 +53,7 @@ impl Default for RapierDebugRenderPlugin {
fn default() -> Self {
Self {
enabled: true,
global: true,
style: DebugRenderStyle::default(),
mode: DebugRenderMode::default(),
}
Expand All @@ -64,6 +74,9 @@ impl RapierDebugRenderPlugin {
pub struct DebugRenderContext {
/// Is the debug-rendering currently enabled?
pub enabled: bool,
/// Is the debug-rendering enabled for every entity? Otherwise it will only work
/// for collider entities with [`ColliderDebug`] component.
pub global: bool,
/// Pipeline responsible for rendering. Access `pipeline.mode` and `pipeline.style`
/// to modify the set of rendered elements, and modify the default coloring rules.
#[reflect(ignore)]
Expand All @@ -74,6 +87,7 @@ impl Default for DebugRenderContext {
fn default() -> Self {
Self {
enabled: true,
global: true,
pipeline: DebugRenderPipeline::default(),
}
}
Expand All @@ -85,6 +99,7 @@ impl Plugin for RapierDebugRenderPlugin {

app.insert_resource(DebugRenderContext {
enabled: self.enabled,
global: self.global,
pipeline: DebugRenderPipeline::new(self.style, self.mode),
})
.add_systems(
Expand All @@ -97,6 +112,8 @@ impl Plugin for RapierDebugRenderPlugin {
struct BevyLinesRenderBackend<'world, 'state, 'a, 'b> {
physics_scale: f32,
custom_colors: Query<'world, 'state, &'a ColliderDebugColor>,
global: bool,
visible: Query<'world, 'state, &'a ColliderDebug>,
context: &'b RapierContext,
gizmos: Gizmos<'state>,
}
Expand All @@ -115,6 +132,21 @@ impl<'world, 'state, 'a, 'b> BevyLinesRenderBackend<'world, 'state, 'a, 'b> {

color.map(|co| co.as_hsla_f32()).unwrap_or(default)
}

fn drawing_enabled(&self, object: DebugRenderObject) -> bool {
match object {
DebugRenderObject::Collider(h, ..) => self
.context
.colliders
.get(h)
.map(|co| {
let entity = Entity::from_bits(co.user_data as u64);
self.global || self.visible.contains(entity)
})
.unwrap_or(false),
_ => true,
}
}
}

impl<'world, 'state, 'a, 'b> DebugRenderBackend for BevyLinesRenderBackend<'world, 'state, 'a, 'b> {
Expand All @@ -126,6 +158,10 @@ impl<'world, 'state, 'a, 'b> DebugRenderBackend for BevyLinesRenderBackend<'worl
b: Point<Real>,
color: [f32; 4],
) {
if !self.drawing_enabled(object) {
return;
}

let scale = self.physics_scale;
let color = self.object_color(object, color);
self.gizmos.line(
Expand All @@ -143,6 +179,10 @@ impl<'world, 'state, 'a, 'b> DebugRenderBackend for BevyLinesRenderBackend<'worl
b: Point<Real>,
color: [f32; 4],
) {
if !self.drawing_enabled(object) {
return;
}

let scale = self.physics_scale;
let color = self.object_color(object, color);
self.gizmos.line(
Expand All @@ -153,19 +193,22 @@ impl<'world, 'state, 'a, 'b> DebugRenderBackend for BevyLinesRenderBackend<'worl
}
}

fn debug_render_scene(
fn debug_render_scene<'a>(
rapier_context: Res<RapierContext>,
mut render_context: ResMut<DebugRenderContext>,
gizmos: Gizmos,
custom_colors: Query<&ColliderDebugColor>,
custom_colors: Query<&'a ColliderDebugColor>,
visible: Query<&'a ColliderDebug>,
) {
if !render_context.enabled {
return;
}

let mut backend = BevyLinesRenderBackend {
global: render_context.global,
physics_scale: rapier_context.physics_scale,
custom_colors,
visible,
context: &rapier_context,
gizmos,
};
Expand Down

0 comments on commit 32a0b41

Please sign in to comment.