diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index b40b6e9ca2dad7..5be5526d62b607 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -26,7 +26,7 @@ use bevy_render::{ render_resource::{AddressMode, Face, FilterMode, PrimitiveTopology, SamplerDescriptor}, renderer::RenderDevice, texture::{CompressedImageFormats, Image, ImageSampler, ImageType, TextureError}, - view::VisibleEntities, + view::{VisibilityBundle, VisibleEntities}, }; use bevy_scene::Scene; #[cfg(not(target_arch = "wasm32"))] @@ -466,6 +466,7 @@ async fn load_gltf<'a, 'b>( world .spawn() .insert_bundle(TransformBundle::identity()) + .insert_bundle(VisibilityBundle::default()) .with_children(|parent| { for node in scene.nodes() { let result = load_node( @@ -707,6 +708,7 @@ fn load_node( let mut node = world_builder.spawn_bundle(TransformBundle::from(Transform::from_matrix( Mat4::from_cols_array_2d(&transform.matrix()), ))); + node.insert_bundle(VisibilityBundle::default()); node.insert(node_name(gltf_node)); diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index 286c9797077050..d0ebc02b9ce59e 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -27,7 +27,7 @@ pub mod prelude { mesh::{shape, Mesh}, render_resource::Shader, texture::Image, - view::{ComputedVisibility, Msaa, Visibility}, + view::{ComputedVisibility, Msaa, Visibility, VisibilityBundle}, }; } diff --git a/crates/bevy_render/src/view/visibility/mod.rs b/crates/bevy_render/src/view/visibility/mod.rs index a45985e35440eb..1e5621215a3989 100644 --- a/crates/bevy_render/src/view/visibility/mod.rs +++ b/crates/bevy_render/src/view/visibility/mod.rs @@ -87,6 +87,21 @@ impl ComputedVisibility { } } +/// A [`Bundle`] of the [`Visibility`] and [`ComputedVisibility`] +/// [`Component`](bevy_ecs::component::Component)s, which describe the visibility of an entity. +/// +/// * To show or hide an entity, you should set its [`Visibility`]. +/// * To get the computed visibility of an entity, you should get its [`ComputedVisibility`]. +/// * For visibility hierarchies to work correctly, you must have both a [`Visibility`] and a [`ComputedVisibility`]. +/// * You may use the [`VisibilityBundle`] to guarantee this. +#[derive(Bundle, Debug, Default)] +pub struct VisibilityBundle { + /// The visibility of the entity. + pub visibility: Visibility, + /// The computed visibility of the entity. + pub computed: ComputedVisibility, +} + /// Use this component to opt-out of built-in frustum culling for Mesh entities #[derive(Component)] pub struct NoFrustumCulling; diff --git a/crates/bevy_scene/Cargo.toml b/crates/bevy_scene/Cargo.toml index 49313b486b37b4..fc9de31c0acbd1 100644 --- a/crates/bevy_scene/Cargo.toml +++ b/crates/bevy_scene/Cargo.toml @@ -18,6 +18,7 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["b bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.8.0-dev" } bevy_transform = { path = "../bevy_transform", version = "0.8.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" } +bevy_render = { path = "../bevy_render", version = "0.8.0-dev" } # other serde = { version = "1.0", features = ["derive"] } diff --git a/crates/bevy_scene/src/bundle.rs b/crates/bevy_scene/src/bundle.rs index 469254138d49a6..e29498b02486ce 100644 --- a/crates/bevy_scene/src/bundle.rs +++ b/crates/bevy_scene/src/bundle.rs @@ -7,6 +7,7 @@ use bevy_ecs::{ prelude::{Changed, Component, Without}, system::{Commands, Query}, }; +use bevy_render::prelude::{ComputedVisibility, Visibility}; use bevy_transform::components::{GlobalTransform, Transform}; use crate::{DynamicScene, InstanceId, Scene, SceneSpawner}; @@ -26,6 +27,8 @@ pub struct SceneBundle { pub scene: Handle, pub transform: Transform, pub global_transform: GlobalTransform, + pub visibility: Visibility, + pub computed_visibility: ComputedVisibility, } /// A component bundle for a [`DynamicScene`] root. @@ -38,6 +41,8 @@ pub struct DynamicSceneBundle { pub scene: Handle, pub transform: Transform, pub global_transform: GlobalTransform, + pub visibility: Visibility, + pub computed_visibility: ComputedVisibility, } /// System that will spawn scenes from [`SceneBundle`]. diff --git a/examples/animation/animated_transform.rs b/examples/animation/animated_transform.rs index 2f8cc9ec68498e..f57b3224e0b26b 100644 --- a/examples/animation/animated_transform.rs +++ b/examples/animation/animated_transform.rs @@ -125,7 +125,8 @@ fn setup( .insert_bundle((planet, player)) .with_children(|p| { // This entity is just used for animation, but doesn't display anything - p.spawn_bundle(TransformBundle { ..default() }) + p.spawn_bundle(TransformBundle::default()) + .insert_bundle(VisibilityBundle::default()) // Add the Name component .insert(orbit_controller) .with_children(|p| { diff --git a/examples/stress_tests/many_foxes.rs b/examples/stress_tests/many_foxes.rs index a279c59582f388..f4589efe344c90 100644 --- a/examples/stress_tests/many_foxes.rs +++ b/examples/stress_tests/many_foxes.rs @@ -109,6 +109,8 @@ fn setup( .spawn_bundle(( Transform::default(), GlobalTransform::default(), + Visibility::default(), + ComputedVisibility::default(), ring_direction, Ring { radius }, ))