hierachy and parent child system updates
- started working on an editor hierarchy window - testing filters - testing parent child relations without system - testing root component - thinking about how to get all entities that are not a child, but also don't have an other identifying component
This commit is contained in:
@ -2,7 +2,10 @@
|
||||
|
||||
namespace Nerfed.Runtime.Components
|
||||
{
|
||||
public readonly record struct Parent(Entity parentEntity);
|
||||
public readonly record struct PreviousParent(Entity parentEntity);
|
||||
public readonly record struct ChildRelation;
|
||||
public readonly record struct Root;
|
||||
//public readonly record struct Parent;
|
||||
//public readonly record struct PreviousParent;
|
||||
public readonly record struct Child;
|
||||
// Describes a relation from the child to the parent.
|
||||
public readonly record struct ChildParentRelation;
|
||||
}
|
4
Nerfed.Runtime/Components/Test.cs
Normal file
4
Nerfed.Runtime/Components/Test.cs
Normal file
@ -0,0 +1,4 @@
|
||||
namespace Nerfed.Runtime.Components
|
||||
{
|
||||
public readonly record struct Test();
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
using Nerfed.Runtime.Components;
|
||||
using Nerfed.Runtime.Util;
|
||||
using System.Numerics;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
// TODO:
|
||||
// Explore if having a WorldTransform and LocalTransfom component each holding position, rotation, scale values and the matricies is useful.
|
||||
@ -19,7 +20,7 @@ namespace Nerfed.Runtime.Systems
|
||||
|
||||
public LocalToWorldSystem(World world) : base(world)
|
||||
{
|
||||
rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Parent>().Build();
|
||||
rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
|
||||
}
|
||||
|
||||
public override void Update(TimeSpan delta)
|
||||
@ -32,20 +33,23 @@ namespace Nerfed.Runtime.Systems
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateWorldTransform(in Entity entity, in Matrix4x4 parentLocalToWorld)
|
||||
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?
|
||||
|
||||
LocalTransform localTransform = Get<LocalTransform>(entity);
|
||||
Matrix4x4 localToWorldMatrix = Matrix4x4.Multiply(parentLocalToWorld, localTransform.TRS());
|
||||
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||
Set(entity, localToWorld);
|
||||
if (Has<LocalTransform>(entity))
|
||||
{
|
||||
LocalTransform localTransform = Get<LocalTransform>(entity);
|
||||
localToWorldMatrix = Matrix4x4.Multiply(localToWorldMatrix, localTransform.TRS());
|
||||
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||
Set(entity, localToWorld);
|
||||
|
||||
Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
||||
Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
||||
}
|
||||
|
||||
ReverseSpanEnumerator<Entity> childEntities = World.OutRelations<ChildRelation>(entity);
|
||||
ReverseSpanEnumerator<Entity> childEntities = World.OutRelations<ChildParentRelation>(entity);
|
||||
foreach (Entity childEntity in childEntities)
|
||||
{
|
||||
UpdateWorldTransform(childEntity, localToWorldMatrix);
|
||||
|
@ -3,60 +3,65 @@ 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 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 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<ChildRelation>(previousParent.parentEntity, entity);
|
||||
Remove<PreviousParent>(entity);
|
||||
}
|
||||
// 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);
|
||||
// // 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;
|
||||
}
|
||||
// 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 ChildRelation());
|
||||
}
|
||||
// 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);
|
||||
// // 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<ChildRelation>(previousParent.parentEntity, entity);
|
||||
Set(entity, new PreviousParent(parent.parentEntity));
|
||||
World.Relate(parent.parentEntity, entity, new ChildRelation());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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.
|
||||
// }
|
||||
//}
|
||||
}
|
@ -6,7 +6,7 @@ using System.Numerics;
|
||||
namespace Nerfed.Runtime.Util
|
||||
{
|
||||
// https://github.com/needle-mirror/com.unity.entities/blob/master/Unity.Transforms/TransformHelpers.cs
|
||||
internal static class Transform
|
||||
public static class Transform
|
||||
{
|
||||
public static Vector3 Forward(in this Matrix4x4 matrix) => new Vector3(matrix.M31, matrix.M32, matrix.M33);
|
||||
public static Vector3 Back(in this Matrix4x4 matrix) => -matrix.Forward();
|
||||
@ -24,34 +24,76 @@ namespace Nerfed.Runtime.Util
|
||||
Matrix4x4.CreateTranslation(localTransform.position);
|
||||
}
|
||||
|
||||
// Sets the parent child relation and adds a child component.
|
||||
// Relation goes from child to parent.
|
||||
public static void SetParent(in World world, in Entity child, in Entity parent)
|
||||
{
|
||||
if (world.Related<ChildParentRelation>(parent, child))
|
||||
{
|
||||
RemoveParent(world, parent);
|
||||
}
|
||||
|
||||
world.Relate(child, parent, new ChildParentRelation());
|
||||
world.Set(child, new Child());
|
||||
world.Remove<Root>(child);
|
||||
return;
|
||||
}
|
||||
|
||||
// Removes any parent child relation ship, thus making it a 'root' object.
|
||||
public static void RemoveParent(in World world, in Entity child)
|
||||
{
|
||||
if (!world.HasOutRelation<ChildParentRelation>(child))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Check if Unrelate all also unrelates incomming relations..?
|
||||
world.UnrelateAll<ChildParentRelation>(child);
|
||||
world.Remove<Child>(child);
|
||||
world.Set(child, new Root());
|
||||
}
|
||||
|
||||
public static Entity CreateBaseEntity(this World world, string tag = "")
|
||||
{
|
||||
Entity entity = world.CreateEntity(tag);
|
||||
world.Set(entity, new Root());
|
||||
return entity;
|
||||
}
|
||||
|
||||
// Force update the transform data of an entity (and children).
|
||||
// Useful for when you need precise up to date transform data.
|
||||
public static void ForceUpdateLocalToWorld(in World world, in Entity entity)
|
||||
{
|
||||
Matrix4x4 parentLocalToWorldMatrix = Matrix4x4.Identity;
|
||||
|
||||
if (world.Has<Parent>(entity))
|
||||
{
|
||||
Entity parent = world.Get<Parent>(entity).parentEntity;
|
||||
parentLocalToWorldMatrix = world.Get<LocalToWorld>(parent).localToWorldMatrix;
|
||||
if (world.HasOutRelation<ChildParentRelation>(entity)) {
|
||||
Entity parent = world.OutRelationSingleton<ChildParentRelation>(entity);
|
||||
|
||||
if (world.Has<LocalToWorld>(parent))
|
||||
{
|
||||
parentLocalToWorldMatrix = world.Get<LocalToWorld>(parent).localToWorldMatrix;
|
||||
}
|
||||
}
|
||||
|
||||
ForceUpdateLocalToWorld(world, entity, parentLocalToWorldMatrix);
|
||||
}
|
||||
|
||||
private static void ForceUpdateLocalToWorld(in World world, in Entity entity, in Matrix4x4 parentLocalToWorldMatrix)
|
||||
private static void ForceUpdateLocalToWorld(in World world, in Entity entity, Matrix4x4 localToWorldMatrix)
|
||||
{
|
||||
LocalTransform localTransform = world.Get<LocalTransform>(entity);
|
||||
Matrix4x4 localToWorldMatrix = Matrix4x4.Multiply(parentLocalToWorldMatrix, localTransform.TRS());
|
||||
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||
world.Set(entity, localToWorld);
|
||||
if (world.Has<LocalTransform>(entity))
|
||||
{
|
||||
LocalTransform localTransform = world.Get<LocalTransform>(entity);
|
||||
localToWorldMatrix = Matrix4x4.Multiply(localToWorldMatrix, localTransform.TRS());
|
||||
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||
world.Set(entity, localToWorld);
|
||||
|
||||
Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
||||
Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
||||
}
|
||||
|
||||
ReverseSpanEnumerator<Entity> childEntities = world.OutRelations<ChildRelation>(entity);
|
||||
ReverseSpanEnumerator<Entity> childEntities = world.InRelations<ChildParentRelation>(entity);
|
||||
foreach (Entity childEntity in childEntities)
|
||||
{
|
||||
ForceUpdateLocalToWorld(world, childEntity, parentLocalToWorldMatrix);
|
||||
ForceUpdateLocalToWorld(world, childEntity, localToWorldMatrix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user