Skip to content

Commit 95e82d7

Browse files
basic visibility for retained gizmos
- set `Visibility` as a required component on `Gizmo` components in `GizmoRenderPlugin` - update `extract_linegizmos` to take view visibility into account - update the 3d_gizmos example to toggle visibility of the retained gizmo in the scene investigating the feasibility of attaching an AABB to the gizmo for better visibility testing will build a `retained_gizmos` example to better demonstrate the feature. it's kind of crammed into the current example code
1 parent 008ca5d commit 95e82d7

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

crates/bevy_gizmos_render/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod pipeline_2d;
2525
mod pipeline_3d;
2626

2727
use bevy_app::{App, Plugin};
28+
use bevy_camera::visibility::{add_visibility_class, Visibility, VisibilityClass};
2829
use bevy_ecs::{
2930
resource::Resource,
3031
schedule::{IntoScheduleConfigs, SystemSet},
@@ -67,6 +68,7 @@ use bevy_render::render_resource::{
6768

6869
use bevy_gizmos::{
6970
config::{GizmoConfigStore, GizmoLineJoint},
71+
prelude::Gizmo,
7072
GizmoAsset, GizmoHandles,
7173
};
7274

@@ -112,6 +114,8 @@ impl Plugin for GizmoRenderPlugin {
112114
} else {
113115
tracing::warn!("bevy_render feature is enabled but RenderApp was not detected. Are you sure you loaded GizmoPlugin after RenderPlugin?");
114116
}
117+
118+
app.register_required_components::<Gizmo, Visibility>();
115119
}
116120
}
117121

crates/bevy_gizmos_render/src/retained.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//! This module is for 'retained' alternatives to the 'immediate mode' [`Gizmos`](bevy_gizmos::gizmos::Gizmos) system parameter.
22
33
use crate::LineGizmoUniform;
4-
use bevy_camera::visibility::RenderLayers;
4+
use bevy_camera::visibility::{InheritedVisibility, RenderLayers, ViewVisibility};
55
use bevy_gizmos::retained::Gizmo;
66
use bevy_math::Affine3;
7-
use bevy_render::sync_world::{MainEntity, TemporaryRenderEntity};
7+
use bevy_render::{
8+
sync_world::{MainEntity, TemporaryRenderEntity},
9+
view,
10+
};
811
use bevy_utils::once;
912
use tracing::warn;
1013
use {
@@ -22,10 +25,24 @@ use bevy_gizmos::config::GizmoLineStyle;
2225
pub(crate) fn extract_linegizmos(
2326
mut commands: Commands,
2427
mut previous_len: Local<usize>,
25-
query: Extract<Query<(Entity, &Gizmo, &GlobalTransform, Option<&RenderLayers>)>>,
28+
query: Extract<
29+
Query<(
30+
Entity,
31+
&Gizmo,
32+
&GlobalTransform,
33+
&InheritedVisibility,
34+
&ViewVisibility,
35+
Option<&RenderLayers>,
36+
)>,
37+
>,
2638
) {
2739
let mut values = Vec::with_capacity(*previous_len);
28-
for (entity, gizmo, transform, render_layers) in &query {
40+
for (entity, gizmo, transform, visibility, view_visibility, render_layers) in &query {
41+
println!("{visibility:?} {view_visibility:?}");
42+
if !view_visibility.get() {
43+
continue;
44+
}
45+
2946
let joints_resolution = if let GizmoLineJoint::Round(resolution) = gizmo.line_config.joints
3047
{
3148
resolution

examples/gizmos/3d_gizmos.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ fn main() {
1212
.add_plugins((DefaultPlugins, FreeCameraPlugin))
1313
.init_gizmo_group::<MyRoundGizmos>()
1414
.add_systems(Startup, setup)
15-
.add_systems(Update, (draw_example_collection, update_config))
15+
.add_systems(
16+
Update,
17+
(
18+
draw_example_collection,
19+
update_config,
20+
update_retained_gizmo_visibility,
21+
),
22+
)
1623
.run();
1724
}
1825

@@ -34,7 +41,7 @@ fn setup(
3441

3542
// A sphere made out of 30_000 lines!
3643
gizmo
37-
.sphere(Isometry3d::IDENTITY, 0.5, CRIMSON)
44+
.sphere(Isometry3d::IDENTITY, 0.5, LIGHT_GOLDENROD_YELLOW)
3845
.resolution(30_000 / 3);
3946

4047
commands.spawn((
@@ -82,6 +89,7 @@ fn setup(
8289
Hold 'Left' or 'Right' to change the line width of straight gizmos\n\
8390
Hold 'Up' or 'Down' to change the line width of round gizmos\n\
8491
Press '1' or '2' to toggle the visibility of straight gizmos or round gizmos\n\
92+
Press '3' to toggle the visibility of retained gizmos\n\
8593
Press 'B' to show all AABB boxes\n\
8694
Press 'U' or 'I' to cycle through line styles for straight or round gizmos\n\
8795
Press 'J' or 'K' to cycle through line joins for straight or round gizmos",
@@ -290,3 +298,14 @@ fn update_config(
290298
config_store.config_mut::<AabbGizmoConfigGroup>().1.draw_all ^= true;
291299
}
292300
}
301+
302+
fn update_retained_gizmo_visibility(
303+
keyboard: Res<ButtonInput<KeyCode>>,
304+
mut gizmos: Query<&mut Visibility, With<Gizmo>>,
305+
) {
306+
if keyboard.just_pressed(KeyCode::Digit3) {
307+
for mut visibility in &mut gizmos {
308+
visibility.toggle_inherited_hidden();
309+
}
310+
}
311+
}

0 commit comments

Comments
 (0)