2024-10-13 01:05:46 +02:00
|
|
|
|
using MoonTools.ECS;
|
|
|
|
|
using Nerfed.Runtime.Components;
|
|
|
|
|
using System.Collections.Generic;
|
2024-10-13 00:08:01 +02:00
|
|
|
|
using System.Numerics;
|
|
|
|
|
|
|
|
|
|
namespace Nerfed.Runtime.Util
|
|
|
|
|
{
|
|
|
|
|
// https://github.com/needle-mirror/com.unity.entities/blob/master/Unity.Transforms/TransformHelpers.cs
|
2024-10-15 00:41:45 +02:00
|
|
|
|
public static class Transform
|
2024-10-13 00:08:01 +02:00
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
public static Vector3 Up(in this Matrix4x4 matrix) => new Vector3(matrix.M21, matrix.M22, matrix.M23);
|
|
|
|
|
public static Vector3 Down(in this Matrix4x4 matrix) => -matrix.Up();
|
|
|
|
|
public static Vector3 Right(in this Matrix4x4 matrix) => new Vector3(matrix.M11, matrix.M12, matrix.M13);
|
|
|
|
|
public static Vector3 Left(in this Matrix4x4 matrix) => -matrix.Right();
|
|
|
|
|
//public static Vector3 Translation(in this Matrix4x4 matrix) => new Vector3();
|
|
|
|
|
//public static Quaternion Rotation(in this Matrix4x4 matrix) => new Quaternion();
|
|
|
|
|
|
|
|
|
|
public static Matrix4x4 TRS(in this LocalTransform localTransform)
|
|
|
|
|
{
|
|
|
|
|
return Matrix4x4.CreateScale(localTransform.scale) *
|
|
|
|
|
Matrix4x4.CreateFromQuaternion(localTransform.rotation) *
|
|
|
|
|
Matrix4x4.CreateTranslation(localTransform.position);
|
|
|
|
|
}
|
2024-10-13 01:05:46 +02:00
|
|
|
|
|
2024-10-15 00:41:45 +02:00
|
|
|
|
// 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)
|
|
|
|
|
{
|
2024-10-15 22:29:19 +02:00
|
|
|
|
RemoveParent(world, child);
|
2024-10-15 00:41:45 +02:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-15 22:29:19 +02:00
|
|
|
|
Entity parent = world.OutRelationSingleton<ChildParentRelation>(child);
|
|
|
|
|
|
2024-10-15 00:41:45 +02:00
|
|
|
|
// TODO: Check if Unrelate all also unrelates incomming relations..?
|
2024-10-15 22:29:19 +02:00
|
|
|
|
world.Unrelate<ChildParentRelation>(child, parent);
|
2024-10-15 00:41:45 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-13 01:05:46 +02:00
|
|
|
|
// 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;
|
|
|
|
|
|
2024-10-15 00:41:45 +02:00
|
|
|
|
if (world.HasOutRelation<ChildParentRelation>(entity)) {
|
|
|
|
|
Entity parent = world.OutRelationSingleton<ChildParentRelation>(entity);
|
|
|
|
|
|
|
|
|
|
if (world.Has<LocalToWorld>(parent))
|
|
|
|
|
{
|
|
|
|
|
parentLocalToWorldMatrix = world.Get<LocalToWorld>(parent).localToWorldMatrix;
|
|
|
|
|
}
|
2024-10-13 01:05:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ForceUpdateLocalToWorld(world, entity, parentLocalToWorldMatrix);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-15 00:41:45 +02:00
|
|
|
|
private static void ForceUpdateLocalToWorld(in World world, in Entity entity, Matrix4x4 localToWorldMatrix)
|
2024-10-13 01:05:46 +02:00
|
|
|
|
{
|
2024-10-15 00:41:45 +02:00
|
|
|
|
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);
|
2024-10-13 01:05:46 +02:00
|
|
|
|
|
2024-10-15 00:41:45 +02:00
|
|
|
|
Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
|
|
|
|
}
|
2024-10-13 01:05:46 +02:00
|
|
|
|
|
2024-10-15 00:41:45 +02:00
|
|
|
|
ReverseSpanEnumerator<Entity> childEntities = world.InRelations<ChildParentRelation>(entity);
|
2024-10-13 01:05:46 +02:00
|
|
|
|
foreach (Entity childEntity in childEntities)
|
|
|
|
|
{
|
2024-10-15 00:41:45 +02:00
|
|
|
|
ForceUpdateLocalToWorld(world, childEntity, localToWorldMatrix);
|
2024-10-13 01:05:46 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-13 00:08:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|