Add a basic lines-based debug-renderer

This commit is contained in:
Sébastien Crozet
2022-04-22 15:45:53 +02:00
parent 21a31bc102
commit bc2ae4b512
19 changed files with 795 additions and 85 deletions

View File

@@ -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,
);
}

View File

@@ -19,6 +19,7 @@ mod box2d_backend;
mod camera2d;
#[cfg(feature = "dim3")]
mod camera3d;
mod debug_render;
mod graphics;
pub mod harness;
pub mod objects;

View File

@@ -5,7 +5,7 @@ use bevy::prelude::*;
use crate::physics::{PhysicsEvents, PhysicsSnapshot, PhysicsState};
use crate::plugin::TestbedPlugin;
use crate::ui;
use crate::{debug_render, ui};
use crate::{graphics::GraphicsManager, harness::RunState};
use na::{self, Point2, Point3, Vector3};
@@ -17,7 +17,7 @@ use rapier::geometry::{ColliderHandle, ColliderSet, NarrowPhase};
#[cfg(feature = "dim3")]
use rapier::geometry::{InteractionGroups, Ray};
use rapier::math::{Real, Vector};
use rapier::pipeline::PhysicsHooks;
use rapier::pipeline::{DebugRenderMode, PhysicsHooks};
#[cfg(all(feature = "dim2", feature = "other-backends"))]
use crate::box2d_backend::Box2dWorld;
@@ -380,7 +380,8 @@ impl TestbedApp {
.add_plugins(DefaultPlugins)
.add_plugin(OrbitCameraPlugin)
.add_plugin(WireframePlugin)
.add_plugin(bevy_egui::EguiPlugin);
.add_plugin(bevy_egui::EguiPlugin)
.add_plugin(debug_render::RapierDebugRenderPlugin::default());
#[cfg(target_arch = "wasm32")]
app.add_plugin(bevy_webgl2::WebGL2Plugin);