diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index b00bb955a1..1814996184 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -822,11 +822,9 @@ pub fn extract_entities_needs_specialization( ) where M: Material, { - for entity in entities_needing_specialization.iter() { - // Update the entity's specialization tick with this run's tick - entity_specialization_ticks.insert((*entity).into(), ticks.this_run()); - } - // Clean up any despawned entities + // Clean up any despawned entities, we do this first in case the removed material was re-added + // the same frame, thus will appear both in the removed components list and have been added to + // the `EntitiesNeedingSpecialization` collection by triggering the `Changed` filter for entity in removed_mesh_material_components.read() { entity_specialization_ticks.remove(&MainEntity::from(entity)); for view in views { @@ -849,6 +847,11 @@ pub fn extract_entities_needs_specialization( } } } + + for entity in entities_needing_specialization.iter() { + // Update the entity's specialization tick with this run's tick + entity_specialization_ticks.insert((*entity).into(), ticks.this_run()); + } } #[derive(Resource, Deref, DerefMut, Clone, Debug)] diff --git a/crates/bevy_sprite/src/mesh2d/material.rs b/crates/bevy_sprite/src/mesh2d/material.rs index ac19697081..e34595f138 100644 --- a/crates/bevy_sprite/src/mesh2d/material.rs +++ b/crates/bevy_sprite/src/mesh2d/material.rs @@ -564,11 +564,9 @@ pub fn extract_entities_needs_specialization( ) where M: Material2d, { - for entity in entities_needing_specialization.iter() { - // Update the entity's specialization tick with this run's tick - entity_specialization_ticks.insert((*entity).into(), ticks.this_run()); - } - // Clean up any despawned entities + // Clean up any despawned entities, we do this first in case the removed material was re-added + // the same frame, thus will appear both in the removed components list and have been added to + // the `EntitiesNeedingSpecialization` collection by triggering the `Changed` filter for entity in removed_mesh_material_components.read() { entity_specialization_ticks.remove(&MainEntity::from(entity)); for view in views { @@ -577,6 +575,10 @@ pub fn extract_entities_needs_specialization( } } } + for entity in entities_needing_specialization.iter() { + // Update the entity's specialization tick with this run's tick + entity_specialization_ticks.insert((*entity).into(), ticks.this_run()); + } } #[derive(Clone, Resource, Deref, DerefMut, Debug)]