@@ -34,6 +34,7 @@ use bevy_platform_support::collections::{HashMap, HashSet};
34
34
use bevy_platform_support:: hash:: FixedHasher ;
35
35
use bevy_reflect:: std_traits:: ReflectDefault ;
36
36
use bevy_reflect:: Reflect ;
37
+ use bevy_render:: camera:: extract_cameras;
37
38
use bevy_render:: mesh:: mark_3d_meshes_as_changed_if_their_assets_changed;
38
39
use bevy_render:: render_asset:: prepare_assets;
39
40
use bevy_render:: renderer:: RenderQueue ;
@@ -315,7 +316,7 @@ where
315
316
ExtractSchedule ,
316
317
(
317
318
extract_mesh_materials :: < M > . before ( ExtractMeshesSet ) ,
318
- extract_entities_needs_specialization :: < M > ,
319
+ extract_entities_needs_specialization :: < M > . after ( extract_cameras ) ,
319
320
) ,
320
321
)
321
322
. add_systems (
@@ -707,6 +708,15 @@ fn extract_mesh_materials<M: Material>(
707
708
pub fn extract_entities_needs_specialization < M > (
708
709
entities_needing_specialization : Extract < Res < EntitiesNeedingSpecialization < M > > > ,
709
710
mut entity_specialization_ticks : ResMut < EntitySpecializationTicks < M > > ,
711
+ mut removed_mesh_material_components : Extract < RemovedComponents < MeshMaterial3d < M > > > ,
712
+ mut specialized_material_pipeline_cache : ResMut < SpecializedMaterialPipelineCache < M > > ,
713
+ mut specialized_prepass_material_pipeline_cache : Option <
714
+ ResMut < SpecializedPrepassMaterialPipelineCache < M > > ,
715
+ > ,
716
+ mut specialized_shadow_material_pipeline_cache : Option <
717
+ ResMut < SpecializedShadowMaterialPipelineCache < M > > ,
718
+ > ,
719
+ views : Query < & ExtractedView > ,
710
720
ticks : SystemChangeTick ,
711
721
) where
712
722
M : Material ,
@@ -715,6 +725,29 @@ pub fn extract_entities_needs_specialization<M>(
715
725
// Update the entity's specialization tick with this run's tick
716
726
entity_specialization_ticks. insert ( ( * entity) . into ( ) , ticks. this_run ( ) ) ;
717
727
}
728
+ // Clean up any despawned entities
729
+ for entity in removed_mesh_material_components. read ( ) {
730
+ entity_specialization_ticks. remove ( & MainEntity :: from ( entity) ) ;
731
+ for view in views {
732
+ if let Some ( cache) =
733
+ specialized_material_pipeline_cache. get_mut ( & view. retained_view_entity )
734
+ {
735
+ cache. remove ( & MainEntity :: from ( entity) ) ;
736
+ }
737
+ if let Some ( cache) = specialized_prepass_material_pipeline_cache
738
+ . as_mut ( )
739
+ . and_then ( |c| c. get_mut ( & view. retained_view_entity ) )
740
+ {
741
+ cache. remove ( & MainEntity :: from ( entity) ) ;
742
+ }
743
+ if let Some ( cache) = specialized_shadow_material_pipeline_cache
744
+ . as_mut ( )
745
+ . and_then ( |c| c. get_mut ( & view. retained_view_entity ) )
746
+ {
747
+ cache. remove ( & MainEntity :: from ( entity) ) ;
748
+ }
749
+ }
750
+ }
718
751
}
719
752
720
753
#[ derive( Resource , Deref , DerefMut , Clone , Debug ) ]
@@ -789,12 +822,15 @@ impl<M> Default for SpecializedMaterialViewPipelineCache<M> {
789
822
pub fn check_entities_needing_specialization < M > (
790
823
needs_specialization : Query <
791
824
Entity ,
792
- Or < (
793
- Changed < Mesh3d > ,
794
- AssetChanged < Mesh3d > ,
795
- Changed < MeshMaterial3d < M > > ,
796
- AssetChanged < MeshMaterial3d < M > > ,
797
- ) > ,
825
+ (
826
+ Or < (
827
+ Changed < Mesh3d > ,
828
+ AssetChanged < Mesh3d > ,
829
+ Changed < MeshMaterial3d < M > > ,
830
+ AssetChanged < MeshMaterial3d < M > > ,
831
+ ) > ,
832
+ With < MeshMaterial3d < M > > ,
833
+ ) ,
798
834
> ,
799
835
mut entities_needing_specialization : ResMut < EntitiesNeedingSpecialization < M > > ,
800
836
) where
0 commit comments