Skip to content

Commit

Permalink
feat: add a checkbox to enable/disable the debug renderer in the testbed
Browse files Browse the repository at this point in the history
  • Loading branch information
sebcrozet committed Nov 4, 2023
1 parent 556ba1f commit ab68a83
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
38 changes: 23 additions & 15 deletions src_testbed/debug_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ use rapier::pipeline::{
};

#[derive(Resource)]
pub struct DebugRenderPipelineResource(pub DebugRenderPipeline);
pub struct DebugRenderPipelineResource {
pub pipeline: DebugRenderPipeline,
pub enabled: bool,
}

#[derive(Default)]
pub struct RapierDebugRenderPlugin {}

impl Plugin for RapierDebugRenderPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(DebugRenderPipelineResource(DebugRenderPipeline::new(
Default::default(),
!DebugRenderMode::RIGID_BODY_AXES & !DebugRenderMode::COLLIDER_AABBS,
)))
app.insert_resource(DebugRenderPipelineResource {
pipeline: DebugRenderPipeline::new(
Default::default(),
!DebugRenderMode::RIGID_BODY_AXES & !DebugRenderMode::COLLIDER_AABBS,
),
enabled: false,
})
.add_systems(Update, debug_render_scene);
}
}
Expand Down Expand Up @@ -46,17 +52,19 @@ impl<'a> DebugRenderBackend for BevyLinesRenderBackend<'a> {
}

fn debug_render_scene(
mut pipeline: ResMut<DebugRenderPipelineResource>,
mut debug_render: ResMut<DebugRenderPipelineResource>,
harness: NonSend<Harness>,
gizmos: Gizmos,
) {
let mut backend = BevyLinesRenderBackend { gizmos };
pipeline.0.render(
&mut backend,
&harness.physics.bodies,
&harness.physics.colliders,
&harness.physics.impulse_joints,
&harness.physics.multibody_joints,
&harness.physics.narrow_phase,
);
if debug_render.enabled {
let mut backend = BevyLinesRenderBackend { gizmos };
debug_render.pipeline.render(
&mut backend,
&harness.physics.bodies,
&harness.physics.colliders,
&harness.physics.impulse_joints,
&harness.physics.multibody_joints,
&harness.physics.narrow_phase,
);
}
}
6 changes: 4 additions & 2 deletions src_testbed/testbed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::mem;

use bevy::prelude::*;

use crate::debug_render::{DebugRenderPipelineResource, RapierDebugRenderPlugin};
use crate::physics::{PhysicsEvents, PhysicsSnapshot, PhysicsState};
use crate::plugin::TestbedPlugin;
use crate::ui;
Expand Down Expand Up @@ -391,7 +392,7 @@ impl TestbedApp {
.add_plugins(DefaultPlugins.set(window_plugin))
.add_plugins(OrbitCameraPlugin)
.add_plugins(WireframePlugin)
// .add_plugins(debug_render::RapierDebugRenderPlugin::default())
.add_plugins(RapierDebugRenderPlugin::default())
.add_plugins(bevy_egui::EguiPlugin);

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -1083,6 +1084,7 @@ fn update_testbed(
builders: ResMut<SceneBuilders>,
mut graphics: NonSendMut<GraphicsManager>,
mut state: ResMut<TestbedState>,
mut debug_render: ResMut<DebugRenderPipelineResource>,
mut harness: NonSendMut<Harness>,
#[cfg(feature = "other-backends")] mut other_backends: NonSendMut<OtherBackends>,
mut plugins: NonSendMut<Plugins>,
Expand Down Expand Up @@ -1127,7 +1129,7 @@ fn update_testbed(
// Update UI
{
let harness = &mut *harness;
ui::update_ui(&mut ui_context, &mut state, harness);
ui::update_ui(&mut ui_context, &mut state, harness, &mut debug_render);

for plugin in &mut plugins.0 {
plugin.update_ui(
Expand Down
9 changes: 8 additions & 1 deletion src_testbed/ui.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rapier::counters::Counters;
use rapier::math::Real;

use crate::debug_render::DebugRenderPipelineResource;
use crate::harness::Harness;
use crate::testbed::{
RunMode, TestbedActionFlags, TestbedState, TestbedStateFlags, PHYSX_BACKEND_PATCH_FRICTION,
Expand All @@ -11,7 +12,12 @@ use crate::PhysicsState;
use bevy_egui::egui::Slider;
use bevy_egui::{egui, EguiContexts};

pub fn update_ui(ui_context: &mut EguiContexts, state: &mut TestbedState, harness: &mut Harness) {
pub fn update_ui(
ui_context: &mut EguiContexts,
state: &mut TestbedState,
harness: &mut Harness,
debug_render: &mut DebugRenderPipelineResource,
) {
egui::Window::new("Parameters").show(ui_context.ctx_mut(), |ui| {
if state.backend_names.len() > 1 && !state.example_names.is_empty() {
let mut changed = false;
Expand Down Expand Up @@ -157,6 +163,7 @@ pub fn update_ui(ui_context: &mut EguiContexts, state: &mut TestbedState, harnes
ui.checkbox(&mut sleep, "sleep enabled");
// ui.checkbox(&mut contact_points, "draw contacts");
// ui.checkbox(&mut wireframe, "draw wireframes");
ui.checkbox(&mut debug_render.enabled, "debug render enabled");

state.flags.set(TestbedStateFlags::SLEEP, sleep);
// state
Expand Down

0 comments on commit ab68a83

Please sign in to comment.