@@ -150,11 +150,16 @@ impl InheritedVisibility {
150
150
/// When adding a new renderable component, you'll typically want to write an
151
151
/// add-component hook that adds the type ID of that component to the
152
152
/// [`VisibilityClass`] array. See `custom_phase_item` for an example.
153
+ ///
154
+ /// VisibilityClass is automatically added by a hook on the `Mesh3d` and
155
+ /// `Mesh2d` components. To avoid duplicating the VisibilityClass and
156
+ /// causing issues when cloning, we use `#[component(clone_behavior=Ignore)]`
153
157
//
154
158
// Note: This can't be a `ComponentId` because the visibility classes are copied
155
159
// into the render world, and component IDs are per-world.
156
160
#[ derive( Clone , Component , Default , Reflect , Deref , DerefMut ) ]
157
161
#[ reflect( Component , Default , Clone ) ]
162
+ #[ component( clone_behavior=Ignore ) ]
158
163
pub struct VisibilityClass ( pub SmallVec < [ TypeId ; 1 ] > ) ;
159
164
160
165
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering.
@@ -988,4 +993,27 @@ mod test {
988
993
assert_eq ! ( 1 , size_of:: <Visibility >( ) ) ;
989
994
assert_eq ! ( 1 , size_of:: <Option <Visibility >>( ) ) ;
990
995
}
996
+
997
+ #[ derive( Component , Default , Clone , Reflect ) ]
998
+ #[ require( VisibilityClass ) ]
999
+ #[ reflect( Component , Default , Clone ) ]
1000
+ #[ component( on_add = add_visibility_class:: <Self >) ]
1001
+ struct TestVisibilityClassHook ;
1002
+
1003
+ #[ test]
1004
+ fn test_add_visibility_class_hook ( ) {
1005
+ let mut world = World :: new ( ) ;
1006
+ let entity = world. spawn ( TestVisibilityClassHook ) . id ( ) ;
1007
+ let entity_clone = world. spawn_empty ( ) . id ( ) ;
1008
+ world
1009
+ . entity_mut ( entity)
1010
+ . clone_with_opt_out ( entity_clone, |_| { } ) ;
1011
+
1012
+ let entity_visibility_class = world. entity ( entity) . get :: < VisibilityClass > ( ) . unwrap ( ) ;
1013
+ assert_eq ! ( entity_visibility_class. len( ) , 1 ) ;
1014
+
1015
+ let entity_clone_visibility_class =
1016
+ world. entity ( entity_clone) . get :: < VisibilityClass > ( ) . unwrap ( ) ;
1017
+ assert_eq ! ( entity_clone_visibility_class. len( ) , 1 ) ;
1018
+ }
991
1019
}
0 commit comments