Skip to content

Commit

Permalink
Migrate visibility to required components (#15474)
Browse files Browse the repository at this point in the history
# Objective

The next step in the migration to required components: Deprecate
`VisibilityBundle` and make `Visibility` require `InheritedVisibility`
and `ViewVisibility`, as per the [chosen
proposal](https://hackmd.io/@bevy/required_components/%2FcO7JPSAQR5G0J_j5wNwtOQ).

## Solution

Deprecate `VisibilityBundle` and make `Visibility` require
`InheritedVisibility` and `ViewVisibility`.

I chose not to deprecate `SpatialBundle` yet, as doing so would mean
that we need to manually add `Visibility` to a bunch of places. It will
be nicer once meshes, sprites, lights, fog, and cameras have been
migrated, since they will require `Transform` and `Visibility` and
therefore not need manually added defaults for them.

---

## Migration Guide

Replace all insertions of `VisibilityBundle` with the `Visibility`
component. The other components required by it will now be inserted
automatically.
  • Loading branch information
Jondolf authored Sep 27, 2024
1 parent 2486343 commit 39d6a74
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 46 deletions.
1 change: 1 addition & 0 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub mod view;
/// The render prelude.
///
/// This includes the most common types in this crate, re-exported for your convenience.
#[expect(deprecated)]
pub mod prelude {
#[doc(hidden)]
pub use crate::{
Expand Down
70 changes: 31 additions & 39 deletions crates/bevy_render/src/view/visibility/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![expect(deprecated)]

mod range;
mod render_layers;

Expand Down Expand Up @@ -32,6 +34,7 @@ use super::NoCpuCulling;
/// `Visibility` to set the values of each entity's [`InheritedVisibility`] component.
#[derive(Component, Clone, Copy, Reflect, Debug, PartialEq, Eq, Default)]
#[reflect(Component, Default, Debug, PartialEq)]
#[require(InheritedVisibility, ViewVisibility)]
pub enum Visibility {
/// An entity with `Visibility::Inherited` will inherit the Visibility of its [`Parent`].
///
Expand Down Expand Up @@ -171,8 +174,13 @@ impl ViewVisibility {
/// * To show or hide an entity, you should set its [`Visibility`].
/// * To get the inherited visibility of an entity, you should get its [`InheritedVisibility`].
/// * For visibility hierarchies to work correctly, you must have both all of [`Visibility`], [`InheritedVisibility`], and [`ViewVisibility`].
/// * You may use the [`VisibilityBundle`] to guarantee this.
/// * ~~You may use the [`VisibilityBundle`] to guarantee this.~~ [`VisibilityBundle`] is now deprecated.
/// [`InheritedVisibility`] and [`ViewVisibility`] are automatically inserted whenever [`Visibility`] is inserted.
#[derive(Bundle, Debug, Clone, Default)]
#[deprecated(
since = "0.15.0",
note = "Use the `Visibility` component instead. Inserting it will now also insert `InheritedVisibility` and `ViewVisibility` automatically."
)]
pub struct VisibilityBundle {
/// The visibility of the entity.
pub visibility: Visibility,
Expand Down Expand Up @@ -538,29 +546,16 @@ mod test {
use bevy_app::prelude::*;
use bevy_hierarchy::BuildChildren;

fn visibility_bundle(visibility: Visibility) -> VisibilityBundle {
VisibilityBundle {
visibility,
..Default::default()
}
}

#[test]
fn visibility_propagation() {
let mut app = App::new();
app.add_systems(Update, visibility_propagate_system);

let root1 = app
.world_mut()
.spawn(visibility_bundle(Visibility::Hidden))
.id();
let root1_child1 = app.world_mut().spawn(VisibilityBundle::default()).id();
let root1_child2 = app
.world_mut()
.spawn(visibility_bundle(Visibility::Hidden))
.id();
let root1_child1_grandchild1 = app.world_mut().spawn(VisibilityBundle::default()).id();
let root1_child2_grandchild1 = app.world_mut().spawn(VisibilityBundle::default()).id();
let root1 = app.world_mut().spawn(Visibility::Hidden).id();
let root1_child1 = app.world_mut().spawn(Visibility::default()).id();
let root1_child2 = app.world_mut().spawn(Visibility::Hidden).id();
let root1_child1_grandchild1 = app.world_mut().spawn(Visibility::default()).id();
let root1_child2_grandchild1 = app.world_mut().spawn(Visibility::default()).id();

app.world_mut()
.entity_mut(root1)
Expand All @@ -572,14 +567,11 @@ mod test {
.entity_mut(root1_child2)
.add_children(&[root1_child2_grandchild1]);

let root2 = app.world_mut().spawn(VisibilityBundle::default()).id();
let root2_child1 = app.world_mut().spawn(VisibilityBundle::default()).id();
let root2_child2 = app
.world_mut()
.spawn(visibility_bundle(Visibility::Hidden))
.id();
let root2_child1_grandchild1 = app.world_mut().spawn(VisibilityBundle::default()).id();
let root2_child2_grandchild1 = app.world_mut().spawn(VisibilityBundle::default()).id();
let root2 = app.world_mut().spawn(Visibility::default()).id();
let root2_child1 = app.world_mut().spawn(Visibility::default()).id();
let root2_child2 = app.world_mut().spawn(Visibility::Hidden).id();
let root2_child1_grandchild1 = app.world_mut().spawn(Visibility::default()).id();
let root2_child2_grandchild1 = app.world_mut().spawn(Visibility::default()).id();

app.world_mut()
.entity_mut(root2)
Expand Down Expand Up @@ -650,14 +642,14 @@ mod test {
let mut app = App::new();
app.add_systems(Update, visibility_propagate_system);

let root1 = app.world_mut().spawn(visibility_bundle(Visible)).id();
let root1_child1 = app.world_mut().spawn(visibility_bundle(Inherited)).id();
let root1_child2 = app.world_mut().spawn(visibility_bundle(Hidden)).id();
let root1_child1_grandchild1 = app.world_mut().spawn(visibility_bundle(Visible)).id();
let root1_child2_grandchild1 = app.world_mut().spawn(visibility_bundle(Visible)).id();
let root1 = app.world_mut().spawn(Visible).id();
let root1_child1 = app.world_mut().spawn(Inherited).id();
let root1_child2 = app.world_mut().spawn(Hidden).id();
let root1_child1_grandchild1 = app.world_mut().spawn(Visible).id();
let root1_child2_grandchild1 = app.world_mut().spawn(Visible).id();

let root2 = app.world_mut().spawn(visibility_bundle(Inherited)).id();
let root3 = app.world_mut().spawn(visibility_bundle(Hidden)).id();
let root2 = app.world_mut().spawn(Inherited).id();
let root3 = app.world_mut().spawn(Hidden).id();

app.world_mut()
.entity_mut(root1)
Expand Down Expand Up @@ -710,15 +702,15 @@ mod test {

// Set up an entity hierarchy.

let id1 = world.spawn(VisibilityBundle::default()).id();
let id1 = world.spawn(Visibility::default()).id();

let id2 = world.spawn(VisibilityBundle::default()).id();
let id2 = world.spawn(Visibility::default()).id();
world.entity_mut(id1).add_children(&[id2]);

let id3 = world.spawn(visibility_bundle(Visibility::Hidden)).id();
let id3 = world.spawn(Visibility::Hidden).id();
world.entity_mut(id2).add_children(&[id3]);

let id4 = world.spawn(VisibilityBundle::default()).id();
let id4 = world.spawn(Visibility::default()).id();
world.entity_mut(id3).add_children(&[id4]);

// Test the hierarchy.
Expand Down Expand Up @@ -785,7 +777,7 @@ mod test {
schedule.add_systems(visibility_propagate_system);

let parent = world.spawn(()).id();
let child = world.spawn(VisibilityBundle::default()).id();
let child = world.spawn(Visibility::default()).id();
world.entity_mut(parent).add_children(&[child]);

schedule.run(&mut world);
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_transform/src/bundles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use crate::prelude::{GlobalTransform, Transform};
/// * To place or move an entity, you should set its [`Transform`].
/// * To get the global transform of an entity, you should get its [`GlobalTransform`].
/// * For transform hierarchies to work correctly, you must have both a [`Transform`] and a [`GlobalTransform`].
/// * You may use the [`TransformBundle`] to guarantee this.
/// * ~You may use the [`TransformBundle`] to guarantee this.~
/// [`TransformBundle`] is now deprecated.
/// [`GlobalTransform`] is automatically inserted whenever [`Transform`] is inserted.
///
/// ## [`Transform`] and [`GlobalTransform`]
///
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use {
///
/// * To get the global transform of an entity, you should get its [`GlobalTransform`].
/// * For transform hierarchies to work correctly, you must have both a [`Transform`] and a [`GlobalTransform`].
/// ~* You may use the [`TransformBundle`](crate::bundles::TransformBundle) to guarantee this.~
/// * [`TransformBundle`](crate::bundles::TransformBundle) is now deprecated.
/// * ~You may use the [`TransformBundle`](crate::bundles::TransformBundle) to guarantee this.~
/// [`TransformBundle`](crate::bundles::TransformBundle) is now deprecated.
/// [`GlobalTransform`] is automatically inserted whenever [`Transform`] is inserted.
///
/// ## [`Transform`] and [`GlobalTransform`]
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use {
/// * To place or move an entity, you should set its [`Transform`].
/// * To get the global transform of an entity, you should get its [`GlobalTransform`].
/// * To be displayed, an entity must have both a [`Transform`] and a [`GlobalTransform`].
/// ~* You may use the [`TransformBundle`](crate::bundles::TransformBundle) to guarantee this.~
/// * [`TransformBundle`](crate::bundles::TransformBundle) is now deprecated.
/// [`GlobalTransform`] is inserted automatically whenever [`Transform`] is inserted.
/// * ~You may use the [`TransformBundle`](crate::bundles::TransformBundle) to guarantee this.~
/// [`TransformBundle`](crate::bundles::TransformBundle) is now deprecated.
/// [`GlobalTransform`] is automatically inserted whenever [`Transform`] is inserted.
///
/// ## [`Transform`] and [`GlobalTransform`]
///
Expand Down
2 changes: 1 addition & 1 deletion examples/asset/asset_decompression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
Sprite::default(),
Transform::default(),
VisibilityBundle::default(),
Visibility::default(),
));
}

Expand Down

0 comments on commit 39d6a74

Please sign in to comment.