Add comments for the debug-renderer

This commit is contained in:
Sébastien Crozet
2022-04-28 17:30:35 +02:00
parent 5063fa4203
commit 65824e74f3
8 changed files with 70 additions and 18 deletions

View File

@@ -14,12 +14,13 @@ resolver = "2"
#parry3d-f64 = { path = "../parry/crates/parry3d-f64" } #parry3d-f64 = { path = "../parry/crates/parry3d-f64" }
# nalgebra = { path = "../nalgebra" } # nalgebra = { path = "../nalgebra" }
#kiss3d = { git = "https://github.com/sebcrozet/kiss3d" } #kiss3d = { git = "https://github.com/sebcrozet/kiss3d" }
#nalgebra = { git = "https://github.com/dimforge/nalgebra", branch = "dev" } #nalgebra = { git = "https://github.com/dimforge/nalgebra", branch = "dev" }
parry2d = { git = "https://github.com/dimforge/parry", branch = "split-and-qbvh" } parry2d = { git = "https://github.com/dimforge/parry", branch = "master" }
parry3d = { git = "https://github.com/dimforge/parry", branch = "split-and-qbvh" } parry3d = { git = "https://github.com/dimforge/parry", branch = "master" }
parry2d-f64 = { git = "https://github.com/dimforge/parry", branch = "split-and-qbvh" } parry2d-f64 = { git = "https://github.com/dimforge/parry", branch = "master" }
parry3d-f64 = { git = "https://github.com/dimforge/parry", branch = "split-and-qbvh" } parry3d-f64 = { git = "https://github.com/dimforge/parry", branch = "master" }
[profile.release] [profile.release]
#debug = true #debug = true

View File

@@ -51,6 +51,7 @@ pub type TOI = parry::query::TOI;
pub use parry::shape::SharedShape; pub use parry::shape::SharedShape;
bitflags::bitflags! { bitflags::bitflags! {
/// Flags providing more information regarding a collision event.
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct CollisionEventFlags: u32 { pub struct CollisionEventFlags: u32 {
/// Flag set if at least one of the colliders involved in the /// Flag set if at least one of the colliders involved in the

View File

@@ -6,16 +6,31 @@ use crate::math::{Isometry, Point, Real, Vector};
use crate::prelude::{ColliderHandle, MultibodyJointHandle}; use crate::prelude::{ColliderHandle, MultibodyJointHandle};
use na::Scale; use na::Scale;
/// The object currently being rendered by the debug-renderer.
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub enum DebugRenderObject<'a> { pub enum DebugRenderObject<'a> {
/// A rigid-body is being rendered.
RigidBody(RigidBodyHandle, &'a RigidBody), RigidBody(RigidBodyHandle, &'a RigidBody),
/// A collider is being rendered.
Collider(ColliderHandle, &'a Collider), Collider(ColliderHandle, &'a Collider),
/// An impulse-joint is being rendered.
ImpulseJoint(ImpulseJointHandle, &'a ImpulseJoint), ImpulseJoint(ImpulseJointHandle, &'a ImpulseJoint),
/// A multibody joint is being rendered.
MultibodyJoint(MultibodyJointHandle, &'a Multibody, &'a MultibodyLink), MultibodyJoint(MultibodyJointHandle, &'a Multibody, &'a MultibodyLink),
/// Another element is being rendered.
Other, Other,
} }
/// Trait implemented by graphics backends responsible for rendering the physics scene.
///
/// The only thing that is required from the graphics backend is to be able to render
/// a colored line. Note that the color is only a suggestion and is computed from the
/// `DebugRenderStyle`. The backend is free to apply its own style, for example based on
/// the `object` being rendered.
pub trait DebugRenderBackend { pub trait DebugRenderBackend {
/// Draws a colored line.
///
/// Note that this method can be called multiple time for the same `object`.
fn draw_line( fn draw_line(
&mut self, &mut self,
object: DebugRenderObject, object: DebugRenderObject,
@@ -24,6 +39,7 @@ pub trait DebugRenderBackend {
color: [f32; 4], color: [f32; 4],
); );
/// Draws a set of line.
fn draw_polyline( fn draw_polyline(
&mut self, &mut self,
object: DebugRenderObject, object: DebugRenderObject,
@@ -40,6 +56,7 @@ pub trait DebugRenderBackend {
} }
} }
/// Draws a chain of line.
fn draw_line_strip( fn draw_line_strip(
&mut self, &mut self,
object: DebugRenderObject, object: DebugRenderObject,

View File

@@ -13,20 +13,31 @@ use std::any::TypeId;
use std::collections::HashMap; use std::collections::HashMap;
bitflags::bitflags! { bitflags::bitflags! {
/// Flags indicating what part of the physics engine should be rendered
/// by the debug-renderer.
pub struct DebugRenderMode: u32 { pub struct DebugRenderMode: u32 {
/// If this flag is set, the collider shapes will be rendered.
const COLLIDER_SHAPES = 1 << 0; const COLLIDER_SHAPES = 1 << 0;
/// If this flag is set, the local coordinate axes of rigid-bodies will be rendered.
const RIGID_BODY_AXES = 1 << 1; const RIGID_BODY_AXES = 1 << 1;
/// If this flag is set, the multibody joints will be rendered.
const MULTIBODY_JOINTS = 1 << 2; const MULTIBODY_JOINTS = 1 << 2;
/// If this flag is set, the impulse joints will be rendered.
const IMPULSE_JOINTS = 1 << 3; const IMPULSE_JOINTS = 1 << 3;
} }
} }
/// Pipeline responsible for rendering the state of the physics engine for debugging purpose.
pub struct DebugRenderPipeline { pub struct DebugRenderPipeline {
#[cfg(feature = "dim2")] #[cfg(feature = "dim2")]
instances: HashMap<TypeId, Vec<Point<Real>>>, instances: HashMap<TypeId, Vec<Point<Real>>>,
#[cfg(feature = "dim3")] #[cfg(feature = "dim3")]
instances: HashMap<TypeId, (Vec<Point<Real>>, Vec<[u32; 2]>)>, instances: HashMap<TypeId, (Vec<Point<Real>>, Vec<[u32; 2]>)>,
/// The style used to compute the line colors for each element
/// to render.
pub style: DebugRenderStyle, pub style: DebugRenderStyle,
/// Flags controlling what part of the physics engine need to
/// be rendered.
pub mode: DebugRenderMode, pub mode: DebugRenderMode,
} }
@@ -37,6 +48,7 @@ impl Default for DebugRenderPipeline {
} }
impl DebugRenderPipeline { impl DebugRenderPipeline {
/// Creates a new debug-render pipeline from a given style and flags.
pub fn new(style: DebugRenderStyle, mode: DebugRenderMode) -> Self { pub fn new(style: DebugRenderStyle, mode: DebugRenderMode) -> Self {
Self { Self {
instances: outlines::instances(style.subdivisions), instances: outlines::instances(style.subdivisions),
@@ -45,10 +57,13 @@ impl DebugRenderPipeline {
} }
} }
/// Creates a new debug-render pipeline that renders everything
/// it can from the physics state.
pub fn render_all(style: DebugRenderStyle) -> Self { pub fn render_all(style: DebugRenderStyle) -> Self {
Self::new(style, DebugRenderMode::all()) Self::new(style, DebugRenderMode::all())
} }
/// Render the scene.
pub fn render( pub fn render(
&mut self, &mut self,
backend: &mut impl DebugRenderBackend, backend: &mut impl DebugRenderBackend,
@@ -57,11 +72,12 @@ impl DebugRenderPipeline {
impulse_joints: &ImpulseJointSet, impulse_joints: &ImpulseJointSet,
multibody_joints: &MultibodyJointSet, multibody_joints: &MultibodyJointSet,
) { ) {
self.render_bodies(backend, bodies); self.render_rigid_bodies(backend, bodies);
self.render_colliders(backend, bodies, colliders); self.render_colliders(backend, bodies, colliders);
self.render_joints(backend, bodies, impulse_joints, multibody_joints); self.render_joints(backend, bodies, impulse_joints, multibody_joints);
} }
/// Render only the joints from the scene.
pub fn render_joints( pub fn render_joints(
&mut self, &mut self,
backend: &mut impl DebugRenderBackend, backend: &mut impl DebugRenderBackend,
@@ -137,7 +153,12 @@ impl DebugRenderPipeline {
} }
} }
pub fn render_bodies(&mut self, backend: &mut impl DebugRenderBackend, bodies: &RigidBodySet) { /// Render only the rigid-bodies from the scene.
pub fn render_rigid_bodies(
&mut self,
backend: &mut impl DebugRenderBackend,
bodies: &RigidBodySet,
) {
for (handle, rb) in bodies.iter() { for (handle, rb) in bodies.iter() {
let object = DebugRenderObject::RigidBody(handle, rb); let object = DebugRenderObject::RigidBody(handle, rb);
@@ -165,6 +186,7 @@ impl DebugRenderPipeline {
} }
} }
/// Render only the colliders from the scene.
pub fn render_colliders( pub fn render_colliders(
&mut self, &mut self,
backend: &mut impl DebugRenderBackend, backend: &mut impl DebugRenderBackend,

View File

@@ -3,19 +3,38 @@
/// The default colors are provided in HSLA (Hue Saturation Lightness Alpha) format. /// The default colors are provided in HSLA (Hue Saturation Lightness Alpha) format.
pub type DebugColor = [f32; 4]; pub type DebugColor = [f32; 4];
/// Style used for computing colors when rendering the scene.
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub struct DebugRenderStyle { pub struct DebugRenderStyle {
/// The number of subdivision used to approximate the curved
/// parts of a shape with smooth faces.
pub subdivisions: u32, pub subdivisions: u32,
/// The number of subdivision used to approimate the curved
/// borders of round shapes.
pub border_subdivisions: u32, pub border_subdivisions: u32,
/// The color of colliders attached to dynamic rigid-bodies.
pub collider_dynamic_color: DebugColor, pub collider_dynamic_color: DebugColor,
/// The color of colliders attached to fixed rigid-bodies.
pub collider_fixed_color: DebugColor, pub collider_fixed_color: DebugColor,
/// The color of colliders attached to kinematic rigid-bodies.
pub collider_kinematic_color: DebugColor, pub collider_kinematic_color: DebugColor,
/// The color of colliders not attached to any rigid-body.
pub collider_parentless_color: DebugColor, pub collider_parentless_color: DebugColor,
/// The color of the line between a rigid-bodys center-of-mass and the
/// anchors of its attached impulse joints.
pub impulse_joint_anchor_color: DebugColor, pub impulse_joint_anchor_color: DebugColor,
/// The color of the line between the two anchors of an impulse joint.
pub impulse_joint_separation_color: DebugColor, pub impulse_joint_separation_color: DebugColor,
/// The color of the line between a rigid-bodys center-of-mass and the
/// anchors of its attached multibody joints.
pub multibody_joint_anchor_color: DebugColor, pub multibody_joint_anchor_color: DebugColor,
/// The color of the line between the two anchors of a multibody joint.
pub multibody_joint_separation_color: DebugColor, pub multibody_joint_separation_color: DebugColor,
/// If a rigid-body is sleeping, its attached entities will have their colors
/// multiplied by this array. (For a joint, both attached rigid-bodies must be sleeping
/// or non-dynamic for this multiplier to be applied).
pub sleep_color_multiplier: [f32; 4], pub sleep_color_multiplier: [f32; 4],
/// The length of the local coordinate axes rendered for a rigid-body.
pub rigid_body_axes_length: f32, pub rigid_body_axes_length: f32,
} }

View File

@@ -1,11 +1,10 @@
use crate::harness::Harness; use crate::harness::Harness;
use crate::lines::DebugLines; use crate::lines::DebugLines;
use bevy::prelude::*; use bevy::prelude::*;
use rapier::math::{Point, Real, DIM}; use rapier::math::{Point, Real};
use rapier::pipeline::{ use rapier::pipeline::{
DebugRenderBackend, DebugRenderMode, DebugRenderObject, DebugRenderPipeline, DebugRenderBackend, DebugRenderMode, DebugRenderObject, DebugRenderPipeline,
}; };
use std::fmt::Debug;
pub struct RapierDebugRenderPlugin { pub struct RapierDebugRenderPlugin {
depth_test: bool, depth_test: bool,
@@ -19,14 +18,6 @@ impl Default for RapierDebugRenderPlugin {
} }
} }
impl RapierDebugRenderPlugin {
pub fn with_depth_test(enabled: bool) -> Self {
Self {
depth_test: enabled,
}
}
}
impl Plugin for RapierDebugRenderPlugin { impl Plugin for RapierDebugRenderPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugin(crate::lines::DebugLinesPlugin::with_depth_test( app.add_plugin(crate::lines::DebugLinesPlugin::with_depth_test(

View File

@@ -1,3 +1,5 @@
#![allow(warnings)]
use bevy::{ use bevy::{
asset::{Assets, HandleUntyped}, asset::{Assets, HandleUntyped},
pbr::{NotShadowCaster, NotShadowReceiver}, pbr::{NotShadowCaster, NotShadowReceiver},

View File

@@ -17,7 +17,7 @@ use rapier::geometry::{ColliderHandle, ColliderSet, NarrowPhase};
#[cfg(feature = "dim3")] #[cfg(feature = "dim3")]
use rapier::geometry::{InteractionGroups, Ray}; use rapier::geometry::{InteractionGroups, Ray};
use rapier::math::{Real, Vector}; use rapier::math::{Real, Vector};
use rapier::pipeline::{DebugRenderMode, PhysicsHooks}; use rapier::pipeline::PhysicsHooks;
#[cfg(all(feature = "dim2", feature = "other-backends"))] #[cfg(all(feature = "dim2", feature = "other-backends"))]
use crate::box2d_backend::Box2dWorld; use crate::box2d_backend::Box2dWorld;
@@ -26,7 +26,6 @@ use crate::harness::Harness;
use crate::physx_backend::PhysxWorld; use crate::physx_backend::PhysxWorld;
use bevy::pbr::wireframe::WireframePlugin; use bevy::pbr::wireframe::WireframePlugin;
use bevy::render::camera::Camera; use bevy::render::camera::Camera;
use bevy::render::render_resource::WgpuFeatures;
use bevy_egui::EguiContext; use bevy_egui::EguiContext;
#[cfg(feature = "dim2")] #[cfg(feature = "dim2")]