Add a predicate to the DebugRenderBackend to filter out objects that are being rendered

This commit is contained in:
Sébastien Crozet
2023-03-26 18:02:30 +02:00
parent 86185fbe88
commit d76eb97fd8
2 changed files with 90 additions and 58 deletions

View File

@@ -1,7 +1,7 @@
use crate::dynamics::{ use crate::dynamics::{
ImpulseJoint, ImpulseJointHandle, Multibody, MultibodyLink, RigidBody, RigidBodyHandle, ImpulseJoint, ImpulseJointHandle, Multibody, MultibodyLink, RigidBody, RigidBodyHandle,
}; };
use crate::geometry::Collider; use crate::geometry::{Aabb, Collider, ContactPair};
use crate::math::{Isometry, Point, Real, Vector}; use crate::math::{Isometry, Point, Real, Vector};
use crate::prelude::{ColliderHandle, MultibodyJointHandle}; use crate::prelude::{ColliderHandle, MultibodyJointHandle};
use na::Scale; use na::Scale;
@@ -13,12 +13,14 @@ pub enum DebugRenderObject<'a> {
RigidBody(RigidBodyHandle, &'a RigidBody), RigidBody(RigidBodyHandle, &'a RigidBody),
/// A collider is being rendered. /// A collider is being rendered.
Collider(ColliderHandle, &'a Collider), Collider(ColliderHandle, &'a Collider),
/// The AABB of a collider is being rendered.
ColliderAabb(ColliderHandle, &'a Collider, &'a Aabb),
/// An impulse-joint is being rendered. /// An impulse-joint is being rendered.
ImpulseJoint(ImpulseJointHandle, &'a ImpulseJoint), ImpulseJoint(ImpulseJointHandle, &'a ImpulseJoint),
/// A multibody joint is being rendered. /// A multibody joint is being rendered.
MultibodyJoint(MultibodyJointHandle, &'a Multibody, &'a MultibodyLink), MultibodyJoint(MultibodyJointHandle, &'a Multibody, &'a MultibodyLink),
/// Another element is being rendered. /// The contacts of a contact-pair are being rendered.
Other, ContactPair(&'a ContactPair, &'a Collider, &'a Collider),
} }
/// Trait implemented by graphics backends responsible for rendering the physics scene. /// Trait implemented by graphics backends responsible for rendering the physics scene.
@@ -28,6 +30,11 @@ pub enum DebugRenderObject<'a> {
/// `DebugRenderStyle`. The backend is free to apply its own style, for example based on /// `DebugRenderStyle`. The backend is free to apply its own style, for example based on
/// the `object` being rendered. /// the `object` being rendered.
pub trait DebugRenderBackend { pub trait DebugRenderBackend {
/// Predicate to filter-out some objects from the debug-rendering.
fn filter_object(&self, _object: DebugRenderObject) -> bool {
true
}
/// Draws a colored line. /// Draws a colored line.
/// ///
/// Note that this method can be called multiple time for the same `object`. /// Note that this method can be called multiple time for the same `object`.

View File

@@ -105,16 +105,19 @@ impl DebugRenderPipeline {
if let (Some(co1), Some(co2)) = if let (Some(co1), Some(co2)) =
(colliders.get(pair.collider1), colliders.get(pair.collider2)) (colliders.get(pair.collider1), colliders.get(pair.collider2))
{ {
let object = DebugRenderObject::ContactPair(pair, co1, co2);
if backend.filter_object(object) {
for manifold in &pair.manifolds { for manifold in &pair.manifolds {
for contact in manifold.contacts() { for contact in manifold.contacts() {
backend.draw_line( backend.draw_line(
DebugRenderObject::Other, object,
co1.position() * contact.local_p1, co1.position() * contact.local_p1,
co2.position() * contact.local_p2, co2.position() * contact.local_p2,
self.style.contact_depth_color, self.style.contact_depth_color,
); );
backend.draw_line( backend.draw_line(
DebugRenderObject::Other, object,
co1.position() * contact.local_p1, co1.position() * contact.local_p1,
co1.position() co1.position()
* (contact.local_p1 * (contact.local_p1
@@ -126,15 +129,23 @@ impl DebugRenderPipeline {
} }
} }
} }
}
if self.mode.contains(DebugRenderMode::SOLVER_CONTACTS) { if self.mode.contains(DebugRenderMode::SOLVER_CONTACTS) {
for pair in narrow_phase.contact_pairs() { for pair in narrow_phase.contact_pairs() {
if let (Some(co1), Some(co2)) =
(colliders.get(pair.collider1), colliders.get(pair.collider2))
{
let object = DebugRenderObject::ContactPair(pair, co1, co2);
if backend.filter_object(object) {
for manifold in &pair.manifolds { for manifold in &pair.manifolds {
for contact in &manifold.data.solver_contacts { for contact in &manifold.data.solver_contacts {
backend.draw_line( backend.draw_line(
DebugRenderObject::Other, object,
contact.point, contact.point,
contact.point + manifold.data.normal * self.style.contact_normal_length, contact.point
+ manifold.data.normal * self.style.contact_normal_length,
self.style.contact_normal_color, self.style.contact_normal_color,
); );
} }
@@ -142,6 +153,8 @@ impl DebugRenderPipeline {
} }
} }
} }
}
}
/// Render only the joints from the scene. /// Render only the joints from the scene.
pub fn render_joints( pub fn render_joints(
@@ -157,6 +170,10 @@ impl DebugRenderPipeline {
mut anchor_color: [f32; 4], mut anchor_color: [f32; 4],
mut separation_color: [f32; 4], mut separation_color: [f32; 4],
object| { object| {
if backend.filter_object(object) {
return;
}
if let (Some(rb1), Some(rb2)) = (bodies.get(body1), bodies.get(body2)) { if let (Some(rb1), Some(rb2)) = (bodies.get(body1), bodies.get(body2)) {
let coeff = if (rb1.is_fixed() || rb1.is_sleeping()) let coeff = if (rb1.is_fixed() || rb1.is_sleeping())
&& (rb2.is_fixed() || rb2.is_sleeping()) && (rb2.is_fixed() || rb2.is_sleeping())
@@ -230,6 +247,7 @@ impl DebugRenderPipeline {
if self.style.rigid_body_axes_length != 0.0 if self.style.rigid_body_axes_length != 0.0
&& self.mode.contains(DebugRenderMode::RIGID_BODY_AXES) && self.mode.contains(DebugRenderMode::RIGID_BODY_AXES)
&& backend.filter_object(object)
{ {
let basis = rb.rotation().to_rotation_matrix().into_inner(); let basis = rb.rotation().to_rotation_matrix().into_inner();
let coeff = if rb.is_sleeping() { let coeff = if rb.is_sleeping() {
@@ -262,6 +280,8 @@ impl DebugRenderPipeline {
if self.mode.contains(DebugRenderMode::COLLIDER_SHAPES) { if self.mode.contains(DebugRenderMode::COLLIDER_SHAPES) {
for (h, co) in colliders.iter() { for (h, co) in colliders.iter() {
let object = DebugRenderObject::Collider(h, co); let object = DebugRenderObject::Collider(h, co);
if backend.filter_object(object) {
let color = if let Some(parent) = co.parent().and_then(|p| bodies.get(p)) { let color = if let Some(parent) = co.parent().and_then(|p| bodies.get(p)) {
let coeff = if parent.is_sleeping() { let coeff = if parent.is_sleeping() {
self.style.sleep_color_multiplier self.style.sleep_color_multiplier
@@ -290,13 +310,17 @@ impl DebugRenderPipeline {
self.render_shape(object, backend, co.shape(), co.position(), color) self.render_shape(object, backend, co.shape(), co.position(), color)
} }
} }
}
if self.mode.contains(DebugRenderMode::COLLIDER_AABBS) { if self.mode.contains(DebugRenderMode::COLLIDER_AABBS) {
for (_, co) in colliders.iter() { for (h, co) in colliders.iter() {
let aabb = co.compute_aabb(); let aabb = co.compute_aabb();
let cuboid = Cuboid::new(aabb.half_extents()); let cuboid = Cuboid::new(aabb.half_extents());
let object = DebugRenderObject::ColliderAabb(h, co, &aabb);
if backend.filter_object(object) {
self.render_shape( self.render_shape(
DebugRenderObject::Other, object,
backend, backend,
&cuboid, &cuboid,
&aabb.center().into(), &aabb.center().into(),
@@ -305,6 +329,7 @@ impl DebugRenderPipeline {
} }
} }
} }
}
#[cfg(feature = "dim2")] #[cfg(feature = "dim2")]
fn render_shape( fn render_shape(