aboutsummaryrefslogtreecommitdiff
path: root/src_testbed/debug_render.rs
diff options
context:
space:
mode:
authorSébastien Crozet <developer@crozet.re>2022-04-22 15:45:53 +0200
committerSébastien Crozet <developer@crozet.re>2022-04-22 16:11:23 +0200
commitbc2ae4b512b8bc7a2b61dd24d9685289453681c5 (patch)
tree576d950ee18b0a3a2e69a48db5fd2ba14f7bcd39 /src_testbed/debug_render.rs
parent21a31bc1026d17d30b3a5ac35e6bb716dc66be6e (diff)
downloadrapier-bc2ae4b512b8bc7a2b61dd24d9685289453681c5.tar.gz
rapier-bc2ae4b512b8bc7a2b61dd24d9685289453681c5.tar.bz2
rapier-bc2ae4b512b8bc7a2b61dd24d9685289453681c5.zip
Add a basic lines-based debug-renderer
Diffstat (limited to 'src_testbed/debug_render.rs')
-rw-r--r--src_testbed/debug_render.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/src_testbed/debug_render.rs b/src_testbed/debug_render.rs
new file mode 100644
index 0000000..135529f
--- /dev/null
+++ b/src_testbed/debug_render.rs
@@ -0,0 +1,81 @@
+use crate::harness::Harness;
+use bevy::prelude::*;
+use bevy_prototype_debug_lines::DebugLines;
+use rapier::math::{Point, Real, DIM};
+use rapier::pipeline::{
+ DebugRenderBackend, DebugRenderMode, DebugRenderObject, DebugRenderPipeline,
+};
+use std::fmt::Debug;
+
+pub struct RapierDebugRenderPlugin {
+ depth_test: bool,
+}
+
+impl Default for RapierDebugRenderPlugin {
+ fn default() -> Self {
+ Self {
+ depth_test: cfg!(feature = "dim3"),
+ }
+ }
+}
+
+impl RapierDebugRenderPlugin {
+ pub fn with_depth_test(enabled: bool) -> Self {
+ Self {
+ depth_test: enabled,
+ }
+ }
+}
+
+impl Plugin for RapierDebugRenderPlugin {
+ fn build(&self, app: &mut App) {
+ app.add_plugin(
+ bevy_prototype_debug_lines::DebugLinesPlugin::with_depth_test(self.depth_test),
+ )
+ .insert_resource(DebugRenderPipeline::new(
+ Default::default(),
+ !DebugRenderMode::RIGID_BODY_AXES,
+ ))
+ .add_system_to_stage(CoreStage::Update, debug_render_scene);
+ }
+}
+
+struct BevyLinesRenderBackend<'a> {
+ lines: &'a mut DebugLines,
+}
+
+impl<'a> DebugRenderBackend for BevyLinesRenderBackend<'a> {
+ #[cfg(feature = "dim2")]
+ fn draw_line(&mut self, _: DebugRenderObject, a: Point<Real>, b: Point<Real>, color: [f32; 4]) {
+ self.lines.line_colored(
+ [a.x, a.y, 1.0e-8].into(),
+ [b.x, b.y, 1.0e-8].into(),
+ 0.0,
+ Color::hsla(color[0], color[1], color[2], color[3]),
+ )
+ }
+ #[cfg(feature = "dim3")]
+ fn draw_line(&mut self, _: DebugRenderObject, a: Point<Real>, b: Point<Real>, color: [f32; 4]) {
+ self.lines.line_colored(
+ [a.x, a.y, a.z].into(),
+ [b.x, b.y, b.z].into(),
+ 0.0,
+ Color::hsla(color[0], color[1], color[2], color[3]),
+ )
+ }
+}
+
+fn debug_render_scene(
+ mut pipeline: ResMut<DebugRenderPipeline>,
+ harness: NonSend<Harness>,
+ mut lines: ResMut<DebugLines>,
+) {
+ let mut backend = BevyLinesRenderBackend { lines: &mut *lines };
+ pipeline.render(
+ &mut backend,
+ &harness.physics.bodies,
+ &harness.physics.colliders,
+ &harness.physics.impulse_joints,
+ &harness.physics.multibody_joints,
+ );
+}