|
| 1 | +//! A module adding debug visualization of [`Aabb`]s. |
| 2 | +
|
| 3 | +use crate as bevy_gizmos; |
| 4 | + |
| 5 | +use bevy_app::{Plugin, PostUpdate}; |
| 6 | +use bevy_ecs::{ |
| 7 | + component::Component, |
| 8 | + entity::Entity, |
| 9 | + query::Without, |
| 10 | + reflect::ReflectComponent, |
| 11 | + schedule::IntoSystemConfigs, |
| 12 | + system::{Query, Res}, |
| 13 | +}; |
| 14 | +use bevy_reflect::{std_traits::ReflectDefault, Reflect}; |
| 15 | +use bevy_render::{color::Color, primitives::Aabb}; |
| 16 | +use bevy_transform::{ |
| 17 | + components::{GlobalTransform, Transform}, |
| 18 | + TransformSystem, |
| 19 | +}; |
| 20 | + |
| 21 | +use crate::{ |
| 22 | + config::{GizmoConfigGroup, GizmoConfigStore}, |
| 23 | + gizmos::Gizmos, |
| 24 | + AppGizmoBuilder, |
| 25 | +}; |
| 26 | + |
| 27 | +/// A [`Plugin`] that provides visualization of [`Aabb`]s for debugging. |
| 28 | +pub struct AabbGizmoPlugin; |
| 29 | + |
| 30 | +impl Plugin for AabbGizmoPlugin { |
| 31 | + fn build(&self, app: &mut bevy_app::App) { |
| 32 | + app.register_type::<AabbGizmoConfigGroup>() |
| 33 | + .init_gizmo_group::<AabbGizmoConfigGroup>() |
| 34 | + .add_systems( |
| 35 | + PostUpdate, |
| 36 | + ( |
| 37 | + draw_aabbs, |
| 38 | + draw_all_aabbs.run_if(|config: Res<GizmoConfigStore>| { |
| 39 | + config.config::<AabbGizmoConfigGroup>().1.draw_all |
| 40 | + }), |
| 41 | + ) |
| 42 | + .after(TransformSystem::TransformPropagate), |
| 43 | + ); |
| 44 | + } |
| 45 | +} |
| 46 | +/// The [`GizmoConfigGroup`] used for debug visualizations of [`Aabb`] components on entities |
| 47 | +#[derive(Clone, Default, Reflect, GizmoConfigGroup)] |
| 48 | +pub struct AabbGizmoConfigGroup { |
| 49 | + /// Draws all bounding boxes in the scene when set to `true`. |
| 50 | + /// |
| 51 | + /// To draw a specific entity's bounding box, you can add the [`ShowAabbGizmo`] component. |
| 52 | + /// |
| 53 | + /// Defaults to `false`. |
| 54 | + pub draw_all: bool, |
| 55 | + /// The default color for bounding box gizmos. |
| 56 | + /// |
| 57 | + /// A random color is chosen per box if `None`. |
| 58 | + /// |
| 59 | + /// Defaults to `None`. |
| 60 | + pub default_color: Option<Color>, |
| 61 | +} |
| 62 | + |
| 63 | +/// Add this [`Component`] to an entity to draw its [`Aabb`] component. |
| 64 | +#[derive(Component, Reflect, Default, Debug)] |
| 65 | +#[reflect(Component, Default)] |
| 66 | +pub struct ShowAabbGizmo { |
| 67 | + /// The color of the box. |
| 68 | + /// |
| 69 | + /// The default color from the [`AabbGizmoConfigGroup`] config is used if `None`, |
| 70 | + pub color: Option<Color>, |
| 71 | +} |
| 72 | + |
| 73 | +fn draw_aabbs( |
| 74 | + query: Query<(Entity, &Aabb, &GlobalTransform, &ShowAabbGizmo)>, |
| 75 | + mut gizmos: Gizmos<AabbGizmoConfigGroup>, |
| 76 | +) { |
| 77 | + for (entity, &aabb, &transform, gizmo) in &query { |
| 78 | + let color = gizmo |
| 79 | + .color |
| 80 | + .or(gizmos.config_ext.default_color) |
| 81 | + .unwrap_or_else(|| color_from_entity(entity)); |
| 82 | + gizmos.cuboid(aabb_transform(aabb, transform), color); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +fn draw_all_aabbs( |
| 87 | + query: Query<(Entity, &Aabb, &GlobalTransform), Without<ShowAabbGizmo>>, |
| 88 | + mut gizmos: Gizmos<AabbGizmoConfigGroup>, |
| 89 | +) { |
| 90 | + for (entity, &aabb, &transform) in &query { |
| 91 | + let color = gizmos |
| 92 | + .config_ext |
| 93 | + .default_color |
| 94 | + .unwrap_or_else(|| color_from_entity(entity)); |
| 95 | + gizmos.cuboid(aabb_transform(aabb, transform), color); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +fn color_from_entity(entity: Entity) -> Color { |
| 100 | + let index = entity.index(); |
| 101 | + |
| 102 | + // from https://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/ |
| 103 | + // |
| 104 | + // See https://en.wikipedia.org/wiki/Low-discrepancy_sequence |
| 105 | + // Map a sequence of integers (eg: 154, 155, 156, 157, 158) into the [0.0..1.0] range, |
| 106 | + // so that the closer the numbers are, the larger the difference of their image. |
| 107 | + const FRAC_U32MAX_GOLDEN_RATIO: u32 = 2654435769; // (u32::MAX / Φ) rounded up |
| 108 | + const RATIO_360: f32 = 360.0 / u32::MAX as f32; |
| 109 | + let hue = index.wrapping_mul(FRAC_U32MAX_GOLDEN_RATIO) as f32 * RATIO_360; |
| 110 | + |
| 111 | + Color::hsl(hue, 1., 0.5) |
| 112 | +} |
| 113 | + |
| 114 | +fn aabb_transform(aabb: Aabb, transform: GlobalTransform) -> GlobalTransform { |
| 115 | + transform |
| 116 | + * GlobalTransform::from( |
| 117 | + Transform::from_translation(aabb.center.into()) |
| 118 | + .with_scale((aabb.half_extents * 2.).into()), |
| 119 | + ) |
| 120 | +} |
0 commit comments