Skip to content

Commit

Permalink
Add VisibilityBundle and use it to fix gltfs, scenes, and examples (b…
Browse files Browse the repository at this point in the history
…evyengine#5335)

# Objective

Gltfs, and a few examples were broken by bevyengine#5310. Fix em.

Closes bevyengine#5334

## Solution

Add `VisibilityBundle` as described here: bevyengine#5334 (comment) and sprinkle it around where needed.
  • Loading branch information
rparrett authored and inodentry committed Aug 8, 2022
1 parent 4659a98 commit 1f9e6b7
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 3 deletions.
4 changes: 3 additions & 1 deletion crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub mod prelude {
mesh::{shape, Mesh},
render_resource::Shader,
texture::Image,
view::{ComputedVisibility, Msaa, Visibility},
view::{ComputedVisibility, Msaa, Visibility, VisibilityBundle},
};
}

Expand Down
15 changes: 15 additions & 0 deletions crates/bevy_render/src/view/visibility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_scene/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_scene/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -26,6 +27,8 @@ pub struct SceneBundle {
pub scene: Handle<Scene>,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub visibility: Visibility,
pub computed_visibility: ComputedVisibility,
}

/// A component bundle for a [`DynamicScene`] root.
Expand All @@ -38,6 +41,8 @@ pub struct DynamicSceneBundle {
pub scene: Handle<DynamicScene>,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub visibility: Visibility,
pub computed_visibility: ComputedVisibility,
}

/// System that will spawn scenes from [`SceneBundle`].
Expand Down
3 changes: 2 additions & 1 deletion examples/animation/animated_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
2 changes: 2 additions & 0 deletions examples/stress_tests/many_foxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ fn setup(
.spawn_bundle((
Transform::default(),
GlobalTransform::default(),
Visibility::default(),
ComputedVisibility::default(),
ring_direction,
Ring { radius },
))
Expand Down

0 comments on commit 1f9e6b7

Please sign in to comment.