Skip to content
Merged
Changes from all 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
28 changes: 28 additions & 0 deletions crates/bevy_camera/src/visibility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,16 @@ impl InheritedVisibility {
/// When adding a new renderable component, you'll typically want to write an
/// add-component hook that adds the type ID of that component to the
/// [`VisibilityClass`] array. See `custom_phase_item` for an example.
///
/// `VisibilityClass` is automatically added by a hook on the `Mesh3d` and
/// `Mesh2d` components. To avoid duplicating the `VisibilityClass` and
/// causing issues when cloning, we use `#[component(clone_behavior=Ignore)]`
//
// Note: This can't be a `ComponentId` because the visibility classes are copied
// into the render world, and component IDs are per-world.
#[derive(Clone, Component, Default, Reflect, Deref, DerefMut)]
#[reflect(Component, Default, Clone)]
#[component(clone_behavior=Ignore)]
pub struct VisibilityClass(pub SmallVec<[TypeId; 1]>);

/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering.
Expand Down Expand Up @@ -988,4 +993,27 @@ mod test {
assert_eq!(1, size_of::<Visibility>());
assert_eq!(1, size_of::<Option<Visibility>>());
}

#[derive(Component, Default, Clone, Reflect)]
#[require(VisibilityClass)]
#[reflect(Component, Default, Clone)]
#[component(on_add = add_visibility_class::<Self>)]
struct TestVisibilityClassHook;

#[test]
fn test_add_visibility_class_hook() {
let mut world = World::new();
let entity = world.spawn(TestVisibilityClassHook).id();
let entity_clone = world.spawn_empty().id();
world
.entity_mut(entity)
.clone_with_opt_out(entity_clone, |_| {});

let entity_visibility_class = world.entity(entity).get::<VisibilityClass>().unwrap();
assert_eq!(entity_visibility_class.len(), 1);

let entity_clone_visibility_class =
world.entity(entity_clone).get::<VisibilityClass>().unwrap();
assert_eq!(entity_clone_visibility_class.len(), 1);
}
}
Loading