Skip to content

Commit d00240d

Browse files
authored
Draw aabb gizmos only for visible entities (#20813)
- **Fix ViewVisibility not getting set** - **Draw aabb gizmos only for visible entities** # Objective Fixes #14462 ## Solution Avoid drawing aabb gizmos for entities that are not visible. ## Testing - Did you test these changes? If so, how? ```rust fn setup(mut gizmo_config_store: ResMut<GizmoConfigStore>, mut commands: Commands) { commands.spawn(Camera2d); gizmo_config_store .config_mut::<AabbGizmoConfigGroup>() .1 .draw_all = true; commands.spawn(( Transform::IDENTITY, Aabb::from_min_max(Vec3::new(-20.0, -20.0, 0.0), Vec3::new(-10.0, -10.0, 0.0)), Visibility::Hidden, )); commands.spawn(( Transform::IDENTITY, Aabb::from_min_max(Vec3::new(10.0, 10.0, 0.0), Vec3::new(20.0, 20.0, 0.0)), Visibility::Visible, )); } ``` ```rust fn setup(mut commands: Commands) { commands.spawn(Camera2d); commands.spawn(( Transform::IDENTITY, Aabb::from_min_max(Vec3::new(-20.0, -20.0, 0.0), Vec3::new(-10.0, -10.0, 0.0)), Visibility::Hidden, ShowAabbGizmo::default(), )); commands.spawn(( Transform::IDENTITY, Aabb::from_min_max(Vec3::new(10.0, 10.0, 0.0), Vec3::new(20.0, 20.0, 0.0)), Visibility::Visible, ShowAabbGizmo::default(), )); } ```
1 parent 471c4c1 commit d00240d

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

crates/bevy_camera/src/visibility/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ pub fn check_visibility(
533533
Entity,
534534
&InheritedVisibility,
535535
&mut ViewVisibility,
536-
&VisibilityClass,
536+
Option<&VisibilityClass>,
537537
Option<&RenderLayers>,
538538
Option<&Aabb>,
539539
&GlobalTransform,
@@ -616,10 +616,15 @@ pub fn check_visibility(
616616
view_visibility.set();
617617
}
618618

619-
// Add the entity to the queue for all visibility classes the
620-
// entity is in.
621-
for visibility_class_id in visibility_class.iter() {
622-
queue.entry(*visibility_class_id).or_default().push(entity);
619+
// The visibility class may be None here because AABB gizmos can be enabled via
620+
// config without a renderable component being added to the entity. This workaround
621+
// allows view visibility to be set for entities without a renderable component but
622+
// that still need to render gizmos.
623+
if let Some(visibility_class) = visibility_class {
624+
// Add the entity to the queue for all visibility classes the entity is in.
625+
for visibility_class_id in visibility_class.iter() {
626+
queue.entry(*visibility_class_id).or_default().push(entity);
627+
}
623628
}
624629
},
625630
);

crates/bevy_gizmos/src/aabb.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A module adding debug visualization of [`Aabb`]s.
22
33
use bevy_app::{Plugin, PostUpdate};
4-
use bevy_camera::primitives::Aabb;
4+
use bevy_camera::{primitives::Aabb, visibility::ViewVisibility};
55
use bevy_color::{Color, Oklcha};
66
use bevy_ecs::{
77
component::Component,
@@ -36,7 +36,7 @@ impl Plugin for AabbGizmoPlugin {
3636
config.config::<AabbGizmoConfigGroup>().1.draw_all
3737
}),
3838
)
39-
.after(bevy_camera::visibility::VisibilitySystems::CalculateBounds)
39+
.after(bevy_camera::visibility::VisibilitySystems::MarkNewlyHiddenEntitiesInvisible)
4040
.after(TransformSystems::Propagate),
4141
);
4242
}
@@ -70,10 +70,20 @@ pub struct ShowAabbGizmo {
7070
}
7171

7272
fn draw_aabbs(
73-
query: Query<(Entity, &Aabb, &GlobalTransform, &ShowAabbGizmo)>,
73+
query: Query<(
74+
Entity,
75+
&Aabb,
76+
&GlobalTransform,
77+
Option<&ViewVisibility>,
78+
&ShowAabbGizmo,
79+
)>,
7480
mut gizmos: Gizmos<AabbGizmoConfigGroup>,
7581
) {
76-
for (entity, &aabb, &transform, gizmo) in &query {
82+
for (entity, &aabb, &transform, view_visibility, gizmo) in &query {
83+
if !is_visible(view_visibility) {
84+
continue;
85+
}
86+
7787
let color = gizmo
7888
.color
7989
.or(gizmos.config_ext.default_color)
@@ -83,10 +93,17 @@ fn draw_aabbs(
8393
}
8494

8595
fn draw_all_aabbs(
86-
query: Query<(Entity, &Aabb, &GlobalTransform), Without<ShowAabbGizmo>>,
96+
query: Query<
97+
(Entity, &Aabb, &GlobalTransform, Option<&ViewVisibility>),
98+
Without<ShowAabbGizmo>,
99+
>,
87100
mut gizmos: Gizmos<AabbGizmoConfigGroup>,
88101
) {
89-
for (entity, &aabb, &transform) in &query {
102+
for (entity, &aabb, &transform, view_visibility) in &query {
103+
if !is_visible(view_visibility) {
104+
continue;
105+
}
106+
90107
let color = gizmos
91108
.config_ext
92109
.default_color
@@ -95,6 +112,10 @@ fn draw_all_aabbs(
95112
}
96113
}
97114

115+
fn is_visible(view_visibility: Option<&ViewVisibility>) -> bool {
116+
view_visibility.is_some_and(|v| v.get())
117+
}
118+
98119
fn color_from_entity(entity: Entity) -> Color {
99120
Oklcha::sequential_dispersed(entity.index()).into()
100121
}

0 commit comments

Comments
 (0)