From ca25669774deb8fdc52792823a2e1be1d12fc7f9 Mon Sep 17 00:00:00 2001 From: "kaosat.dev" Date: Tue, 27 Feb 2024 00:37:24 +0100 Subject: [PATCH] feat(bevy_gltf_components): added GltfProcessed flag component to improve performance of iteration over added --- crates/bevy_gltf_components/src/process_gltfs.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/bevy_gltf_components/src/process_gltfs.rs b/crates/bevy_gltf_components/src/process_gltfs.rs index 5cb2d10..c4eacfb 100644 --- a/crates/bevy_gltf_components/src/process_gltfs.rs +++ b/crates/bevy_gltf_components/src/process_gltfs.rs @@ -2,9 +2,10 @@ use bevy::{ core::Name, ecs::{ entity::Entity, - query::Added, + query::{Added, Without}, reflect::{AppTypeRegistry, ReflectComponent}, world::World, + component::Component }, gltf::GltfExtras, hierarchy::Parent, @@ -15,10 +16,14 @@ use bevy::{ use crate::{ronstring_to_reflect_component, GltfComponentsConfig}; +/// this is a flag component to tag a processed gltf, to avoid processing things multiple times +#[derive(Component)] +pub struct GltfProcessed; + /// main function: injects components into each entity in gltf files that have `gltf_extras`, using reflection pub fn add_components_from_gltf_extras(world: &mut World) { let mut extras = - world.query_filtered::<(Entity, &Name, &GltfExtras, &Parent), Added>(); + world.query_filtered::<(Entity, &Name, &GltfExtras, &Parent), (Added, Without)>(); let mut entity_components: HashMap, TypeRegistration)>> = HashMap::new(); @@ -88,8 +93,11 @@ pub fn add_components_from_gltf_extras(world: &mut World) { type_registration .data::() .expect("Unable to reflect component") - .insert(&mut entity_mut, &*component, &type_registry); // TODO: how can we insert any additional components "by hand" here ? + .insert(&mut entity_mut, &*component, &type_registry); + + entity_mut.insert(GltfProcessed); // this is how can we insert any additional components } } } + }