Combined transform systems

Profiler window calls count
Parallel transform system is now in the normal transform system
Removed unused parent system
This commit is contained in:
max
2024-10-20 03:51:59 +02:00
parent 82fe47f627
commit 2c84e650d6
6 changed files with 66 additions and 169 deletions

View File

@ -15,19 +15,62 @@ namespace Nerfed.Runtime.Systems
{
public class LocalToWorldSystem : MoonTools.ECS.System
{
private readonly bool useParallelFor = false; // When having a low amount of transforms or when in debug mode this might be slower.
private readonly Filter rootEntitiesFilter;
private readonly Filter transformEntitiesFilter;
private readonly Action<int> updateWorldTransform;
public LocalToWorldSystem(World world) : base(world)
{
rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
if (useParallelFor)
{
transformEntitiesFilter = FilterBuilder.Include<LocalTransform>().Build();
updateWorldTransform = UpdateWorldTransformByIndex;
}
}
public override void Update(TimeSpan delta)
{
foreach (Entity entity in rootEntitiesFilter.Entities)
if (rootEntitiesFilter.Empty)
{
UpdateWorldTransform(entity, Matrix4x4.Identity);
return;
}
if (useParallelFor)
{
Profiler.BeginSample("ParallelFor.LocalToWorldCheck");
// This check is needed because some entities might not have a LocalToWorld component yet.
// Adding this during the loop will break.
foreach (Entity entity in transformEntitiesFilter.Entities) {
if (Has<LocalToWorld>(entity))
{
continue;
}
Set(entity, new LocalToWorld(Matrix4x4.Identity));
}
Profiler.EndSample();
Profiler.BeginSample("ParallelFor.LocalToWorldUpdate");
// This should only be used when the filter doesn't change by executing these functions!
// So no entity deletion or setting/removing of components used by the filters in this loop.
Parallel.For(0, rootEntitiesFilter.Count, updateWorldTransform);
Profiler.EndSample();
}
else
{
foreach (Entity entity in rootEntitiesFilter.Entities)
{
UpdateWorldTransform(entity, Matrix4x4.Identity);
}
}
}
private void UpdateWorldTransformByIndex(int entityFilterIndex)
{
Entity entity = rootEntitiesFilter.NthEntity(entityFilterIndex);
UpdateWorldTransform(entity, Matrix4x4.Identity);
}
private void UpdateWorldTransform(in Entity entity, Matrix4x4 localToWorldMatrix)
@ -41,8 +84,6 @@ namespace Nerfed.Runtime.Systems
localToWorldMatrix = Matrix4x4.Multiply(localToWorldMatrix, localTransform.TRS());
LocalToWorld localToWorld = new(localToWorldMatrix);
Set(entity, localToWorld);
//Task.Delay(10).Wait();
//Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
}
ReverseSpanEnumerator<Entity> childEntities = World.InRelations<ChildParentRelation>(entity);

View File

@ -1,81 +0,0 @@
using MoonTools.ECS;
using Nerfed.Runtime.Components;
using Nerfed.Runtime.Util;
using System.Numerics;
namespace Nerfed.Runtime.Systems
{
public class LocalToWorldThreadedSystem : MoonTools.ECS.System
{
private readonly Filter rootEntitiesFilter;
private readonly Action<int> forEntity;
public LocalToWorldThreadedSystem(World world) : base(world)
{
rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
forEntity = UpdateEntity;
}
public override void Update(TimeSpan delta)
{
Parallel.For(0, rootEntitiesFilter.Count, forEntity);
}
private void UpdateEntity(int entityFilterIndex)
{
Entity entity = rootEntitiesFilter.NthEntity(entityFilterIndex);
UpdateWorldTransform(entity, Matrix4x4.Identity);
}
private void UpdateWorldTransform(Entity entity, Matrix4x4 localToWorldMatrix)
{
// TODO: Only update dirty transforms.
// If a parent is dirty all the children need to update their localToWorld matrix.
// How do we check if something is dirty? How do we know if a LocalTransform has been changed?
if (Has<LocalTransform>(entity))
{
LocalTransform localTransform = Get<LocalTransform>(entity);
localToWorldMatrix = Matrix4x4.Multiply(localToWorldMatrix, localTransform.TRS());
LocalToWorld localToWorld = new(localToWorldMatrix);
Set(entity, localToWorld);
//Task.Delay(10).Wait();
//Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
}
ReverseSpanEnumerator<Entity> childEntities = World.InRelations<ChildParentRelation>(entity);
foreach (Entity childEntity in childEntities)
{
UpdateWorldTransform(childEntity, localToWorldMatrix);
}
}
}
}
//System.Random rnd = new System.Random();
//World world = new World();
//Filter filter = world.FilterBuilder.Include<Test>().Build();
//for (int i = 0; i < 100; i++)
//{
// Entity e = world.CreateEntity(i.ToString());
// world.Set(e, new Test());
//}
//Action<int> forEntityIndex = ForEntityIndex;
//ParallelLoopResult result = Parallel.For(0, filter.Count, forEntityIndex);
//Console.WriteLine(result.IsCompleted);
//void ForEntityIndex(int entity)
//{
// int delay = rnd.Next(1, 1000);
// Task.Delay(delay).Wait();
// Console.WriteLine($"ForEntityIndex | {filter.NthEntity(entity).ID}");
//}
//namespace Hoi
//{
// public readonly record struct Test;
//}

View File

@ -1,67 +0,0 @@
using MoonTools.ECS;
using Nerfed.Runtime.Components;
namespace Nerfed.Runtime.Systems
{
//public class ParentSystem : MoonTools.ECS.System
//{
// private readonly Filter parentsAddedFilter;
// private readonly Filter parentsRemovedFilter;
// private readonly Filter parentsFilter;
// public ParentSystem(World world) : base(world)
// {
// parentsAddedFilter = FilterBuilder.Include<Parent>().Exclude<PreviousParent>().Build();
// parentsRemovedFilter = FilterBuilder.Include<PreviousParent>().Exclude<Parent>().Build();
// parentsFilter = FilterBuilder.Include<Parent>().Include<PreviousParent>().Build();
// }
// public override void Update(TimeSpan delta)
// {
// // Update removed parents.
// foreach (Entity entity in parentsRemovedFilter.Entities)
// {
// // Do stuff here to update/remove child relations etc.
// //PreviousParent previousParent = Get<PreviousParent>(entity);
// //World.Unrelate<ChildParentRelation>(previousParent.parentEntity, entity);
// Remove<PreviousParent>(entity);
// }
// // Update added parents.
// foreach (Entity entity in parentsAddedFilter.Entities)
// {
// Parent parent = Get<Parent>(entity);
// if (Has<Parent>(parent.parentEntity) && Get<Parent>(parent.parentEntity).parentEntity == entity)
// {
// Log.Warning($"Entity {entity} cannot be a parent of entity {parent.parentEntity}, because {parent.parentEntity} is the parent of {entity}");
// Remove<Parent>(entity);
// continue;
// }
// PreviousParent previousParent = new(parent.parentEntity);
// Set(entity, previousParent);
// World.Relate(parent.parentEntity, entity, new ChildParentRelation());
// }
// // Update relations if the parent has changed.
// foreach (Entity entity in parentsFilter.Entities)
// {
// Parent parent = Get<Parent>(entity);
// PreviousParent previousParent = Get<PreviousParent>(entity);
// if(parent.parentEntity != previousParent.parentEntity)
// {
// World.Unrelate<ChildParentRelation>(previousParent.parentEntity, entity);
// Set(entity, new PreviousParent(parent.parentEntity));
// World.Relate(parent.parentEntity, entity, new ChildParentRelation());
// }
// }
// // TODO:
// // What if an parent entity gets destroyed?
// // How does the child know if the parent is in valid. Also we need to remove the parent component.
// // Maybe if we also relate the other way around child -> parent via relations, and the relation is gone that means the parent is gone so we should remove the component.
// }
//}
}