Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn the Visibility struct into an enum #6271

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ async fn load_gltf<'a, 'b>(
let mut entity_to_skin_index_map = HashMap::new();

world
.spawn(SpatialBundle::VISIBLE_IDENTITY)
.spawn(SpatialBundle::SHOWN_IDENTITY)
.with_children(|parent| {
for node in scene.nodes() {
let result = load_node(
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_render/src/spatial_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ impl SpatialBundle {
pub const fn from_transform(transform: Transform) -> Self {
SpatialBundle {
transform,
..Self::VISIBLE_IDENTITY
..Self::SHOWN_IDENTITY
}
}

/// A visible [`SpatialBundle`], with no translation, rotation, and a scale of 1 on all axes.
pub const VISIBLE_IDENTITY: Self = SpatialBundle {
visibility: Visibility::VISIBLE,
computed: ComputedVisibility::INVISIBLE,
pub const SHOWN_IDENTITY: Self = SpatialBundle {
visibility: Visibility::Shown,
computed: ComputedVisibility::HIDDEN,
transform: Transform::IDENTITY,
global_transform: GlobalTransform::IDENTITY,
};

/// An invisible [`SpatialBundle`], with no translation, rotation, and a scale of 1 on all axes.
pub const INVISIBLE_IDENTITY: Self = SpatialBundle {
visibility: Visibility::INVISIBLE,
..Self::VISIBLE_IDENTITY
pub const HIDDEN_IDENTITY: Self = SpatialBundle {
visibility: Visibility::Hidden,
..Self::SHOWN_IDENTITY
};
}

Expand Down
54 changes: 25 additions & 29 deletions crates/bevy_render/src/view/visibility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,36 @@ use crate::{

/// If an entity is hidden in this way, all [`Children`] (and all of their children and so on) will also be hidden.
/// This is done by setting the values of their [`ComputedVisibility`] component.
#[derive(Component, Clone, Reflect, Debug)]
#[derive(Component, Clone, Reflect, Debug, PartialEq, Eq)]
#[reflect(Component, Default)]
pub struct Visibility {
/// Indicates whether this entity is visible. Hidden values will propagate down the entity hierarchy.
/// If this entity is hidden, all of its descendants will be hidden as well. See [`Children`] and [`Parent`] for
/// hierarchy info.
pub is_visible: bool,
pub enum Visibility {
ickk marked this conversation as resolved.
Show resolved Hide resolved
Shown,
Hidden,
}

impl Default for Visibility {
fn default() -> Self {
Visibility::VISIBLE
Self::Shown
}
}

impl Visibility {
/// A [`Visibility`], set as visible.
pub const VISIBLE: Self = Visibility { is_visible: true };

/// A [`Visibility`], set as invisible.
pub const INVISIBLE: Self = Visibility { is_visible: false };
/// Whether this entity is visible.
#[inline]
pub const fn is_visible(&self) -> bool {
match self {
Self::Shown => true,
Self::Hidden => false,
}
ickk marked this conversation as resolved.
Show resolved Hide resolved
}

/// Toggle the visibility.
#[inline]
pub fn toggle(&mut self) {
self.is_visible = !self.is_visible;
*self = match self {
Self::Shown => Self::Hidden,
Self::Hidden => Self::Shown,
}
}
}

Expand All @@ -64,13 +69,13 @@ pub struct ComputedVisibility {

impl Default for ComputedVisibility {
fn default() -> Self {
Self::INVISIBLE
Self::HIDDEN
}
}

impl ComputedVisibility {
/// A [`ComputedVisibility`], set as invisible.
pub const INVISIBLE: Self = ComputedVisibility {
pub const HIDDEN: Self = ComputedVisibility {
is_visible_in_hierarchy: false,
is_visible_in_view: false,
};
Expand Down Expand Up @@ -271,7 +276,7 @@ fn visibility_propagate_system(
children_query: Query<&Children, (With<Parent>, With<Visibility>, With<ComputedVisibility>)>,
) {
for (children, visibility, mut computed_visibility, entity) in root_query.iter_mut() {
computed_visibility.is_visible_in_hierarchy = visibility.is_visible;
computed_visibility.is_visible_in_hierarchy = visibility.is_visible();
// reset "view" visibility here ... if this entity should be drawn a future system should set this to true
computed_visibility.is_visible_in_view = false;
if let Some(children) = children {
Expand Down Expand Up @@ -304,7 +309,7 @@ fn propagate_recursive(
child_parent.get(), expected_parent,
"Malformed hierarchy. This probably means that your hierarchy has been improperly maintained, or contains a cycle"
);
computed_visibility.is_visible_in_hierarchy = visibility.is_visible && parent_visible;
computed_visibility.is_visible_in_hierarchy = visibility.is_visible() && parent_visible;
// reset "view" visibility here ... if this entity should be drawn a future system should set this to true
computed_visibility.is_visible_in_view = false;
computed_visibility.is_visible_in_hierarchy
Expand Down Expand Up @@ -433,21 +438,15 @@ mod test {

let root1 = app
.world
.spawn((
Visibility { is_visible: false },
ComputedVisibility::default(),
))
.spawn((Visibility::Hidden, ComputedVisibility::default()))
.id();
let root1_child1 = app
.world
.spawn((Visibility::default(), ComputedVisibility::default()))
.id();
let root1_child2 = app
.world
.spawn((
Visibility { is_visible: false },
ComputedVisibility::default(),
))
.spawn((Visibility::Hidden, ComputedVisibility::default()))
.id();
let root1_child1_grandchild1 = app
.world
Expand Down Expand Up @@ -478,10 +477,7 @@ mod test {
.id();
let root2_child2 = app
.world
.spawn((
Visibility { is_visible: false },
ComputedVisibility::default(),
))
.spawn((Visibility::Hidden, ComputedVisibility::default()))
.id();
let root2_child1_grandchild1 = app
.world
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/mesh2d_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn star(
// The `Handle<Mesh>` needs to be wrapped in a `Mesh2dHandle` to use 2d rendering instead of 3d
Mesh2dHandle(meshes.add(star)),
// This bundle's components are needed for something to be rendered
SpatialBundle::VISIBLE_IDENTITY,
SpatialBundle::SHOWN_IDENTITY,
));

// Spawn the camera
Expand Down
2 changes: 1 addition & 1 deletion examples/animation/animated_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn setup(
.with_children(|p| {
// This entity is just used for animation, but doesn't display anything
p.spawn((
SpatialBundle::VISIBLE_IDENTITY,
SpatialBundle::SHOWN_IDENTITY,
// Add the Name component
orbit_controller,
))
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/shader_instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {
fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
commands.spawn((
meshes.add(Mesh::from(shape::Cube { size: 0.5 })),
SpatialBundle::VISIBLE_IDENTITY,
SpatialBundle::SHOWN_IDENTITY,
InstanceMaterialData(
(1..=10)
.flat_map(|x| (1..=10).map(move |y| (x as f32 / 10.0, y as f32 / 10.0)))
Expand Down
2 changes: 1 addition & 1 deletion examples/stress_tests/many_foxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn setup(
let (base_rotation, ring_direction) = ring_directions[ring_index % 2];
let ring_parent = commands
.spawn((
SpatialBundle::VISIBLE_IDENTITY,
SpatialBundle::SHOWN_IDENTITY,
ring_direction,
Ring { radius },
))
Expand Down