Skip to content

Commit a63d761

Browse files
committed
Add VisibilityBundle and use it to fix gltfs, scenes, and examples (#5335)
# Objective Gltfs, and a few examples were broken by #5310. Fix em. Closes #5334 ## Solution Add `VisibilityBundle` as described here: #5334 (comment) and sprinkle it around where needed.
1 parent 9f8bdee commit a63d761

File tree

7 files changed

+29
-3
lines changed

7 files changed

+29
-3
lines changed

crates/bevy_gltf/src/loader.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use bevy_render::{
2626
render_resource::{AddressMode, Face, FilterMode, PrimitiveTopology, SamplerDescriptor},
2727
renderer::RenderDevice,
2828
texture::{CompressedImageFormats, Image, ImageSampler, ImageType, TextureError},
29-
view::VisibleEntities,
29+
view::{VisibilityBundle, VisibleEntities},
3030
};
3131
use bevy_scene::Scene;
3232
#[cfg(not(target_arch = "wasm32"))]
@@ -466,6 +466,7 @@ async fn load_gltf<'a, 'b>(
466466
world
467467
.spawn()
468468
.insert_bundle(TransformBundle::identity())
469+
.insert_bundle(VisibilityBundle::default())
469470
.with_children(|parent| {
470471
for node in scene.nodes() {
471472
let result = load_node(
@@ -707,6 +708,7 @@ fn load_node(
707708
let mut node = world_builder.spawn_bundle(TransformBundle::from(Transform::from_matrix(
708709
Mat4::from_cols_array_2d(&transform.matrix()),
709710
)));
711+
node.insert_bundle(VisibilityBundle::default());
710712

711713
node.insert(node_name(gltf_node));
712714

crates/bevy_render/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub mod prelude {
2727
mesh::{shape, Mesh},
2828
render_resource::Shader,
2929
texture::Image,
30-
view::{ComputedVisibility, Msaa, Visibility},
30+
view::{ComputedVisibility, Msaa, Visibility, VisibilityBundle},
3131
};
3232
}
3333

crates/bevy_render/src/view/visibility/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ impl ComputedVisibility {
8787
}
8888
}
8989

90+
/// A [`Bundle`] of the [`Visibility`] and [`ComputedVisibility`]
91+
/// [`Component`](bevy_ecs::component::Component)s, which describe the visibility of an entity.
92+
///
93+
/// * To show or hide an entity, you should set its [`Visibility`].
94+
/// * To get the computed visibility of an entity, you should get its [`ComputedVisibility`].
95+
/// * For visibility hierarchies to work correctly, you must have both a [`Visibility`] and a [`ComputedVisibility`].
96+
/// * You may use the [`VisibilityBundle`] to guarantee this.
97+
#[derive(Bundle, Debug, Default)]
98+
pub struct VisibilityBundle {
99+
/// The visibility of the entity.
100+
pub visibility: Visibility,
101+
/// The computed visibility of the entity.
102+
pub computed: ComputedVisibility,
103+
}
104+
90105
/// Use this component to opt-out of built-in frustum culling for Mesh entities
91106
#[derive(Component)]
92107
pub struct NoFrustumCulling;

crates/bevy_scene/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["b
1818
bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.8.0-dev" }
1919
bevy_transform = { path = "../bevy_transform", version = "0.8.0-dev" }
2020
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
21+
bevy_render = { path = "../bevy_render", version = "0.8.0-dev" }
2122

2223
# other
2324
serde = { version = "1.0", features = ["derive"] }

crates/bevy_scene/src/bundle.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use bevy_ecs::{
77
prelude::{Changed, Component, Without},
88
system::{Commands, Query},
99
};
10+
use bevy_render::prelude::{ComputedVisibility, Visibility};
1011
use bevy_transform::components::{GlobalTransform, Transform};
1112

1213
use crate::{DynamicScene, InstanceId, Scene, SceneSpawner};
@@ -26,6 +27,8 @@ pub struct SceneBundle {
2627
pub scene: Handle<Scene>,
2728
pub transform: Transform,
2829
pub global_transform: GlobalTransform,
30+
pub visibility: Visibility,
31+
pub computed_visibility: ComputedVisibility,
2932
}
3033

3134
/// A component bundle for a [`DynamicScene`] root.
@@ -38,6 +41,8 @@ pub struct DynamicSceneBundle {
3841
pub scene: Handle<DynamicScene>,
3942
pub transform: Transform,
4043
pub global_transform: GlobalTransform,
44+
pub visibility: Visibility,
45+
pub computed_visibility: ComputedVisibility,
4146
}
4247

4348
/// System that will spawn scenes from [`SceneBundle`].

examples/animation/animated_transform.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ fn setup(
125125
.insert_bundle((planet, player))
126126
.with_children(|p| {
127127
// This entity is just used for animation, but doesn't display anything
128-
p.spawn_bundle(TransformBundle { ..default() })
128+
p.spawn_bundle(TransformBundle::default())
129+
.insert_bundle(VisibilityBundle::default())
129130
// Add the Name component
130131
.insert(orbit_controller)
131132
.with_children(|p| {

examples/stress_tests/many_foxes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ fn setup(
109109
.spawn_bundle((
110110
Transform::default(),
111111
GlobalTransform::default(),
112+
Visibility::default(),
113+
ComputedVisibility::default(),
112114
ring_direction,
113115
Ring { radius },
114116
))

0 commit comments

Comments
 (0)