Skip to content

Commit 39d6a74

Browse files
authored
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.
1 parent 2486343 commit 39d6a74

File tree

6 files changed

+41
-46
lines changed

6 files changed

+41
-46
lines changed

crates/bevy_render/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub mod view;
4343
/// The render prelude.
4444
///
4545
/// This includes the most common types in this crate, re-exported for your convenience.
46+
#[expect(deprecated)]
4647
pub mod prelude {
4748
#[doc(hidden)]
4849
pub use crate::{

crates/bevy_render/src/view/visibility/mod.rs

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![expect(deprecated)]
2+
13
mod range;
24
mod render_layers;
35

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

541-
fn visibility_bundle(visibility: Visibility) -> VisibilityBundle {
542-
VisibilityBundle {
543-
visibility,
544-
..Default::default()
545-
}
546-
}
547-
548549
#[test]
549550
fn visibility_propagation() {
550551
let mut app = App::new();
551552
app.add_systems(Update, visibility_propagate_system);
552553

553-
let root1 = app
554-
.world_mut()
555-
.spawn(visibility_bundle(Visibility::Hidden))
556-
.id();
557-
let root1_child1 = app.world_mut().spawn(VisibilityBundle::default()).id();
558-
let root1_child2 = app
559-
.world_mut()
560-
.spawn(visibility_bundle(Visibility::Hidden))
561-
.id();
562-
let root1_child1_grandchild1 = app.world_mut().spawn(VisibilityBundle::default()).id();
563-
let root1_child2_grandchild1 = app.world_mut().spawn(VisibilityBundle::default()).id();
554+
let root1 = app.world_mut().spawn(Visibility::Hidden).id();
555+
let root1_child1 = app.world_mut().spawn(Visibility::default()).id();
556+
let root1_child2 = app.world_mut().spawn(Visibility::Hidden).id();
557+
let root1_child1_grandchild1 = app.world_mut().spawn(Visibility::default()).id();
558+
let root1_child2_grandchild1 = app.world_mut().spawn(Visibility::default()).id();
564559

565560
app.world_mut()
566561
.entity_mut(root1)
@@ -572,14 +567,11 @@ mod test {
572567
.entity_mut(root1_child2)
573568
.add_children(&[root1_child2_grandchild1]);
574569

575-
let root2 = app.world_mut().spawn(VisibilityBundle::default()).id();
576-
let root2_child1 = app.world_mut().spawn(VisibilityBundle::default()).id();
577-
let root2_child2 = app
578-
.world_mut()
579-
.spawn(visibility_bundle(Visibility::Hidden))
580-
.id();
581-
let root2_child1_grandchild1 = app.world_mut().spawn(VisibilityBundle::default()).id();
582-
let root2_child2_grandchild1 = app.world_mut().spawn(VisibilityBundle::default()).id();
570+
let root2 = app.world_mut().spawn(Visibility::default()).id();
571+
let root2_child1 = app.world_mut().spawn(Visibility::default()).id();
572+
let root2_child2 = app.world_mut().spawn(Visibility::Hidden).id();
573+
let root2_child1_grandchild1 = app.world_mut().spawn(Visibility::default()).id();
574+
let root2_child2_grandchild1 = app.world_mut().spawn(Visibility::default()).id();
583575

584576
app.world_mut()
585577
.entity_mut(root2)
@@ -650,14 +642,14 @@ mod test {
650642
let mut app = App::new();
651643
app.add_systems(Update, visibility_propagate_system);
652644

653-
let root1 = app.world_mut().spawn(visibility_bundle(Visible)).id();
654-
let root1_child1 = app.world_mut().spawn(visibility_bundle(Inherited)).id();
655-
let root1_child2 = app.world_mut().spawn(visibility_bundle(Hidden)).id();
656-
let root1_child1_grandchild1 = app.world_mut().spawn(visibility_bundle(Visible)).id();
657-
let root1_child2_grandchild1 = app.world_mut().spawn(visibility_bundle(Visible)).id();
645+
let root1 = app.world_mut().spawn(Visible).id();
646+
let root1_child1 = app.world_mut().spawn(Inherited).id();
647+
let root1_child2 = app.world_mut().spawn(Hidden).id();
648+
let root1_child1_grandchild1 = app.world_mut().spawn(Visible).id();
649+
let root1_child2_grandchild1 = app.world_mut().spawn(Visible).id();
658650

659-
let root2 = app.world_mut().spawn(visibility_bundle(Inherited)).id();
660-
let root3 = app.world_mut().spawn(visibility_bundle(Hidden)).id();
651+
let root2 = app.world_mut().spawn(Inherited).id();
652+
let root3 = app.world_mut().spawn(Hidden).id();
661653

662654
app.world_mut()
663655
.entity_mut(root1)
@@ -710,15 +702,15 @@ mod test {
710702

711703
// Set up an entity hierarchy.
712704

713-
let id1 = world.spawn(VisibilityBundle::default()).id();
705+
let id1 = world.spawn(Visibility::default()).id();
714706

715-
let id2 = world.spawn(VisibilityBundle::default()).id();
707+
let id2 = world.spawn(Visibility::default()).id();
716708
world.entity_mut(id1).add_children(&[id2]);
717709

718-
let id3 = world.spawn(visibility_bundle(Visibility::Hidden)).id();
710+
let id3 = world.spawn(Visibility::Hidden).id();
719711
world.entity_mut(id2).add_children(&[id3]);
720712

721-
let id4 = world.spawn(VisibilityBundle::default()).id();
713+
let id4 = world.spawn(Visibility::default()).id();
722714
world.entity_mut(id3).add_children(&[id4]);
723715

724716
// Test the hierarchy.
@@ -785,7 +777,7 @@ mod test {
785777
schedule.add_systems(visibility_propagate_system);
786778

787779
let parent = world.spawn(()).id();
788-
let child = world.spawn(VisibilityBundle::default()).id();
780+
let child = world.spawn(Visibility::default()).id();
789781
world.entity_mut(parent).add_children(&[child]);
790782

791783
schedule.run(&mut world);

crates/bevy_transform/src/bundles.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use crate::prelude::{GlobalTransform, Transform};
99
/// * To place or move an entity, you should set its [`Transform`].
1010
/// * To get the global transform of an entity, you should get its [`GlobalTransform`].
1111
/// * For transform hierarchies to work correctly, you must have both a [`Transform`] and a [`GlobalTransform`].
12-
/// * You may use the [`TransformBundle`] to guarantee this.
12+
/// * ~You may use the [`TransformBundle`] to guarantee this.~
13+
/// [`TransformBundle`] is now deprecated.
14+
/// [`GlobalTransform`] is automatically inserted whenever [`Transform`] is inserted.
1315
///
1416
/// ## [`Transform`] and [`GlobalTransform`]
1517
///

crates/bevy_transform/src/components/global_transform.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use {
1717
///
1818
/// * To get the global transform of an entity, you should get its [`GlobalTransform`].
1919
/// * For transform hierarchies to work correctly, you must have both a [`Transform`] and a [`GlobalTransform`].
20-
/// ~* You may use the [`TransformBundle`](crate::bundles::TransformBundle) to guarantee this.~
21-
/// * [`TransformBundle`](crate::bundles::TransformBundle) is now deprecated.
20+
/// * ~You may use the [`TransformBundle`](crate::bundles::TransformBundle) to guarantee this.~
21+
/// [`TransformBundle`](crate::bundles::TransformBundle) is now deprecated.
2222
/// [`GlobalTransform`] is automatically inserted whenever [`Transform`] is inserted.
2323
///
2424
/// ## [`Transform`] and [`GlobalTransform`]

crates/bevy_transform/src/components/transform.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use {
1313
/// * To place or move an entity, you should set its [`Transform`].
1414
/// * To get the global transform of an entity, you should get its [`GlobalTransform`].
1515
/// * To be displayed, an entity must have both a [`Transform`] and a [`GlobalTransform`].
16-
/// ~* You may use the [`TransformBundle`](crate::bundles::TransformBundle) to guarantee this.~
17-
/// * [`TransformBundle`](crate::bundles::TransformBundle) is now deprecated.
18-
/// [`GlobalTransform`] is inserted automatically whenever [`Transform`] is inserted.
16+
/// * ~You may use the [`TransformBundle`](crate::bundles::TransformBundle) to guarantee this.~
17+
/// [`TransformBundle`](crate::bundles::TransformBundle) is now deprecated.
18+
/// [`GlobalTransform`] is automatically inserted whenever [`Transform`] is inserted.
1919
///
2020
/// ## [`Transform`] and [`GlobalTransform`]
2121
///

examples/asset/asset_decompression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
112112
},
113113
Sprite::default(),
114114
Transform::default(),
115-
VisibilityBundle::default(),
115+
Visibility::default(),
116116
));
117117
}
118118

0 commit comments

Comments
 (0)