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: https://github.com/bevyengine/bevy/issues/5334#issuecomment-1186050778 and sprinkle it around where needed.
This commit is contained in:
Rob Parrett 2022-07-16 02:47:23 +00:00
parent 9f8bdeeeb9
commit a63d761aa3
7 changed files with 29 additions and 3 deletions

View File

@ -26,7 +26,7 @@ use bevy_render::{
render_resource::{AddressMode, Face, FilterMode, PrimitiveTopology, SamplerDescriptor}, render_resource::{AddressMode, Face, FilterMode, PrimitiveTopology, SamplerDescriptor},
renderer::RenderDevice, renderer::RenderDevice,
texture::{CompressedImageFormats, Image, ImageSampler, ImageType, TextureError}, texture::{CompressedImageFormats, Image, ImageSampler, ImageType, TextureError},
view::VisibleEntities, view::{VisibilityBundle, VisibleEntities},
}; };
use bevy_scene::Scene; use bevy_scene::Scene;
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
@ -466,6 +466,7 @@ async fn load_gltf<'a, 'b>(
world world
.spawn() .spawn()
.insert_bundle(TransformBundle::identity()) .insert_bundle(TransformBundle::identity())
.insert_bundle(VisibilityBundle::default())
.with_children(|parent| { .with_children(|parent| {
for node in scene.nodes() { for node in scene.nodes() {
let result = load_node( let result = load_node(
@ -707,6 +708,7 @@ fn load_node(
let mut node = world_builder.spawn_bundle(TransformBundle::from(Transform::from_matrix( let mut node = world_builder.spawn_bundle(TransformBundle::from(Transform::from_matrix(
Mat4::from_cols_array_2d(&transform.matrix()), Mat4::from_cols_array_2d(&transform.matrix()),
))); )));
node.insert_bundle(VisibilityBundle::default());
node.insert(node_name(gltf_node)); node.insert(node_name(gltf_node));

View File

@ -27,7 +27,7 @@ pub mod prelude {
mesh::{shape, Mesh}, mesh::{shape, Mesh},
render_resource::Shader, render_resource::Shader,
texture::Image, texture::Image,
view::{ComputedVisibility, Msaa, Visibility}, view::{ComputedVisibility, Msaa, Visibility, VisibilityBundle},
}; };
} }

View File

@ -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 /// Use this component to opt-out of built-in frustum culling for Mesh entities
#[derive(Component)] #[derive(Component)]
pub struct NoFrustumCulling; pub struct NoFrustumCulling;

View File

@ -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_hierarchy = { path = "../bevy_hierarchy", version = "0.8.0-dev" }
bevy_transform = { path = "../bevy_transform", 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_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
bevy_render = { path = "../bevy_render", version = "0.8.0-dev" }
# other # other
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View File

@ -7,6 +7,7 @@ use bevy_ecs::{
prelude::{Changed, Component, Without}, prelude::{Changed, Component, Without},
system::{Commands, Query}, system::{Commands, Query},
}; };
use bevy_render::prelude::{ComputedVisibility, Visibility};
use bevy_transform::components::{GlobalTransform, Transform}; use bevy_transform::components::{GlobalTransform, Transform};
use crate::{DynamicScene, InstanceId, Scene, SceneSpawner}; use crate::{DynamicScene, InstanceId, Scene, SceneSpawner};
@ -26,6 +27,8 @@ pub struct SceneBundle {
pub scene: Handle<Scene>, pub scene: Handle<Scene>,
pub transform: Transform, pub transform: Transform,
pub global_transform: GlobalTransform, pub global_transform: GlobalTransform,
pub visibility: Visibility,
pub computed_visibility: ComputedVisibility,
} }
/// A component bundle for a [`DynamicScene`] root. /// A component bundle for a [`DynamicScene`] root.
@ -38,6 +41,8 @@ pub struct DynamicSceneBundle {
pub scene: Handle<DynamicScene>, pub scene: Handle<DynamicScene>,
pub transform: Transform, pub transform: Transform,
pub global_transform: GlobalTransform, pub global_transform: GlobalTransform,
pub visibility: Visibility,
pub computed_visibility: ComputedVisibility,
} }
/// System that will spawn scenes from [`SceneBundle`]. /// System that will spawn scenes from [`SceneBundle`].

View File

@ -125,7 +125,8 @@ fn setup(
.insert_bundle((planet, player)) .insert_bundle((planet, player))
.with_children(|p| { .with_children(|p| {
// This entity is just used for animation, but doesn't display anything // 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 // Add the Name component
.insert(orbit_controller) .insert(orbit_controller)
.with_children(|p| { .with_children(|p| {

View File

@ -109,6 +109,8 @@ fn setup(
.spawn_bundle(( .spawn_bundle((
Transform::default(), Transform::default(),
GlobalTransform::default(), GlobalTransform::default(),
Visibility::default(),
ComputedVisibility::default(),
ring_direction, ring_direction,
Ring { radius }, Ring { radius },
)) ))