max
82fe47f627
Added a simple profiler Testing LocalToWorldSystem with Parallel execution for root nodes
55 lines
2.2 KiB
C#
55 lines
2.2 KiB
C#
using MoonTools.ECS;
|
|
using Nerfed.Runtime.Components;
|
|
using Nerfed.Runtime.Util;
|
|
using System.Numerics;
|
|
|
|
// TODO:
|
|
// Explore if having a WorldTransform and LocalTransfom component each holding position, rotation, scale values and the matricies is useful.
|
|
// Often you need to either get or set these values.
|
|
// If so, we probably need a utility funciton to do so. Since changing these values means that we need to update all the related data + children as well.
|
|
|
|
// TODO:
|
|
// When modifying transform all the children need to be updated as well.
|
|
|
|
namespace Nerfed.Runtime.Systems
|
|
{
|
|
public class LocalToWorldSystem : MoonTools.ECS.System
|
|
{
|
|
private readonly Filter rootEntitiesFilter;
|
|
|
|
public LocalToWorldSystem(World world) : base(world)
|
|
{
|
|
rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
|
|
}
|
|
|
|
public override void Update(TimeSpan delta)
|
|
{
|
|
foreach (Entity entity in rootEntitiesFilter.Entities)
|
|
{
|
|
UpdateWorldTransform(entity, Matrix4x4.Identity);
|
|
}
|
|
}
|
|
|
|
private void UpdateWorldTransform(in 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);
|
|
}
|
|
}
|
|
}
|
|
} |