From 6900c854a9fa267294f65433f436954e454e19fe Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Sat, 16 Nov 2024 00:19:31 -0800 Subject: [PATCH] Revert "Switch FstSpatialHash to use component hooks" This reverts commit 739e0c766e6309a33b669065b3b5cbc9798b7afd. --- src/spatial_hash.rs | 61 ++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/spatial_hash.rs b/src/spatial_hash.rs index 00d8f8c..ecab231 100644 --- a/src/spatial_hash.rs +++ b/src/spatial_hash.rs @@ -83,28 +83,47 @@ impl Default for ChangedSpatialHashes { } } -impl SpatialHashPlugin { +impl SpatialHashPlugin { /// Update or insert the [`SpatialHash`] of all changed entities that match the optional /// `QueryFilter`. fn update_spatial_hashes( + mut commands: Commands, mut changed_hashes: ResMut>, - mut spatial_entities: Query< - ( - Entity, - &Parent, - &GridCell

, - &mut SpatialHash

, - &mut FastSpatialHash, - ), - (F, Or<(Changed, Changed>)>), - >, + mut spatial_entities: ParamSet<( + Query< + ( + Entity, + &Parent, + &GridCell

, + &mut SpatialHash

, + &mut FastSpatialHash, + ), + (F, Or<(Changed, Changed>)>), + >, + Query<(Entity, &Parent, &GridCell

), (F, Without>)>, + )>, mut stats: Option>, mut thread_changed_hashes: Local>>, + mut thread_commands: Local, FastSpatialHash)>>>, ) { let start = Instant::now(); - // Update fast hashes - spatial_entities.par_iter_mut().for_each( + // Create new + spatial_entities + .p1() + .par_iter() + .for_each(|(entity, parent, cell)| { + let spatial_hash = SpatialHash::new(parent, cell); + let fast_hash = FastSpatialHash(spatial_hash.pre_hash); + thread_commands.scope(|tl| tl.push((entity, spatial_hash, fast_hash))); + thread_changed_hashes.scope(|tl| tl.push(entity)); + }); + for (entity, spatial_hash, fast_hash) in thread_commands.drain::>() { + commands.entity(entity).insert((spatial_hash, fast_hash)); + } + + // Update existing + spatial_entities.p0().par_iter_mut().for_each( |(entity, parent, cell, mut hash, mut fast_hash)| { let new_hash = SpatialHash::new(parent, cell); let new_fast_hash = new_hash.pre_hash; @@ -155,27 +174,13 @@ impl Hash for FastSpatialHash { /// Due to reference frames and multiple big spaces in a single world, this must use both the /// [`GridCell`] and the [`Parent`] of the entity to uniquely identify its position. These two /// values are then hashed and stored in this spatial hash component. -#[derive(Clone, Copy, Debug, Reflect)] +#[derive(Component, Clone, Copy, Debug, Reflect)] pub struct SpatialHash { cell: GridCell

, parent: Entity, pre_hash: u64, } -impl Component for SpatialHash

{ - const STORAGE_TYPE: bevy_ecs::component::StorageType = bevy_ecs::component::StorageType::Table; - - // Automatically add and remove FastSpatialHash for this entity. - fn register_component_hooks(hooks: &mut bevy_ecs::component::ComponentHooks) { - hooks.on_add(|mut world, entity, _| { - world.commands().entity(entity).insert(FastSpatialHash(0)); - }); - hooks.on_remove(|mut world, entity, _| { - world.commands().entity(entity).remove::(); - }); - } -} - impl PartialEq for SpatialHash

{ fn eq(&self, other: &Self) -> bool { // Comparing the hash is redundant.