From 39d6a745d2c8249b91259cf3bf620435593f42a0 Mon Sep 17 00:00:00 2001 From: Joona Aalto Date: Fri, 27 Sep 2024 22:06:16 +0300 Subject: [PATCH] Migrate visibility to required components (#15474) # 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. --- crates/bevy_render/src/lib.rs | 1 + crates/bevy_render/src/view/visibility/mod.rs | 70 ++++++++----------- crates/bevy_transform/src/bundles.rs | 4 +- .../src/components/global_transform.rs | 4 +- .../src/components/transform.rs | 6 +- examples/asset/asset_decompression.rs | 2 +- 6 files changed, 41 insertions(+), 46 deletions(-) diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index 81ac6eb47c..9cd0b28d1c 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -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::{ diff --git a/crates/bevy_render/src/view/visibility/mod.rs b/crates/bevy_render/src/view/visibility/mod.rs index adef70251b..68e784dd09 100644 --- a/crates/bevy_render/src/view/visibility/mod.rs +++ b/crates/bevy_render/src/view/visibility/mod.rs @@ -1,3 +1,5 @@ +#![expect(deprecated)] + mod range; mod render_layers; @@ -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`]. /// @@ -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, @@ -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) @@ -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) @@ -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) @@ -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. @@ -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); diff --git a/crates/bevy_transform/src/bundles.rs b/crates/bevy_transform/src/bundles.rs index 34b05ec651..16a36441a4 100644 --- a/crates/bevy_transform/src/bundles.rs +++ b/crates/bevy_transform/src/bundles.rs @@ -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`] /// diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index 06a89f2555..17580f9416 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -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`] diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index 5e676b2752..9522d3de37 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -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`] /// diff --git a/examples/asset/asset_decompression.rs b/examples/asset/asset_decompression.rs index 0915031169..65aa97d899 100644 --- a/examples/asset/asset_decompression.rs +++ b/examples/asset/asset_decompression.rs @@ -112,7 +112,7 @@ fn setup(mut commands: Commands, asset_server: Res) { }, Sprite::default(), Transform::default(), - VisibilityBundle::default(), + Visibility::default(), )); }