Optimized filter for transform entities with missing LocalToWorld components

This commit is contained in:
max 2024-10-21 22:48:57 +02:00
parent 567714a52d
commit 7225d13880

View File

@ -17,7 +17,7 @@ public class LocalToWorldSystem : MoonTools.ECS.System
{ {
private readonly bool useParallelFor = true; // When having a low amount of transforms or when in debug mode this might be slower. private readonly bool useParallelFor = true; // When having a low amount of transforms or when in debug mode this might be slower.
private readonly Filter rootEntitiesFilter; private readonly Filter rootEntitiesFilter;
private readonly Filter transformEntitiesFilter; private readonly Filter entitiesWithoutLocalToWorldFilter;
private readonly Action<int> updateWorldTransform; private readonly Action<int> updateWorldTransform;
public LocalToWorldSystem(World world) : base(world) public LocalToWorldSystem(World world) : base(world)
@ -25,7 +25,7 @@ public LocalToWorldSystem(World world) : base(world)
rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build(); rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
if (useParallelFor) if (useParallelFor)
{ {
transformEntitiesFilter = FilterBuilder.Include<LocalTransform>().Build(); entitiesWithoutLocalToWorldFilter = FilterBuilder.Include<LocalTransform>().Exclude<LocalToWorld>().Build();
updateWorldTransform = UpdateWorldTransformByIndex; updateWorldTransform = UpdateWorldTransformByIndex;
} }
} }
@ -42,12 +42,7 @@ public override void Update(TimeSpan delta)
Profiler.BeginSample("ParallelFor.LocalToWorldCheck"); Profiler.BeginSample("ParallelFor.LocalToWorldCheck");
// This check is needed because some entities might not have a LocalToWorld component yet. // This check is needed because some entities might not have a LocalToWorld component yet.
// Adding this during the loop will break. // Adding this during the loop will break.
foreach (Entity entity in transformEntitiesFilter.Entities) { foreach (Entity entity in entitiesWithoutLocalToWorldFilter.Entities) {
if (Has<LocalToWorld>(entity))
{
continue;
}
Set(entity, new LocalToWorld(Matrix4x4.Identity)); Set(entity, new LocalToWorld(Matrix4x4.Identity));
} }
Profiler.EndSample(); Profiler.EndSample();