Compare commits
No commits in common. "ecs-test" and "main" have entirely different histories.
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -16,6 +16,3 @@
|
|||||||
[submodule "Nerfed.Runtime/Libraries/ImGui.NET"]
|
[submodule "Nerfed.Runtime/Libraries/ImGui.NET"]
|
||||||
path = Nerfed.Runtime/Libraries/ImGui.NET
|
path = Nerfed.Runtime/Libraries/ImGui.NET
|
||||||
url = https://github.com/ImGuiNET/ImGui.NET.git
|
url = https://github.com/ImGuiNET/ImGui.NET.git
|
||||||
[submodule "Nerfed.Runtime/Libraries/ECS"]
|
|
||||||
path = Nerfed.Runtime/Libraries/ECS
|
|
||||||
url = https://github.com/MoonsideGames/MoonTools.ECS.git
|
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
namespace Nerfed.Editor.Components;
|
|
||||||
|
|
||||||
public readonly record struct SelectedInHierachy;
|
|
||||||
public readonly record struct ClickedInHierachy;
|
|
@ -66,11 +66,6 @@ private static void HandleOnGui()
|
|||||||
UpdateDock();
|
UpdateDock();
|
||||||
|
|
||||||
ImGui.ShowDemoWindow();
|
ImGui.ShowDemoWindow();
|
||||||
|
|
||||||
foreach (MoonTools.ECS.System system in Program.editorSystems)
|
|
||||||
{
|
|
||||||
system.Update(Engine.Timestep);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,7 +9,6 @@
|
|||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<Configurations>Debug;Test;Release</Configurations>
|
<Configurations>Debug;Test;Release</Configurations>
|
||||||
<Platforms>x64</Platforms>
|
<Platforms>x64</Platforms>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
|
||||||
|
@ -1,19 +1,9 @@
|
|||||||
using MoonTools.ECS;
|
using Nerfed.Runtime;
|
||||||
using Nerfed.Editor.Systems;
|
|
||||||
using Nerfed.Runtime;
|
|
||||||
using Nerfed.Runtime.Components;
|
|
||||||
using Nerfed.Runtime.Systems;
|
|
||||||
using Nerfed.Runtime.Util;
|
|
||||||
using System.Numerics;
|
|
||||||
|
|
||||||
namespace Nerfed.Editor;
|
namespace Nerfed.Editor;
|
||||||
|
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
private static readonly World world = new World();
|
|
||||||
private static List<MoonTools.ECS.System> systems = new List<MoonTools.ECS.System>();
|
|
||||||
public static List<MoonTools.ECS.System> editorSystems = new List<MoonTools.ECS.System>();
|
|
||||||
|
|
||||||
private static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Engine.OnInitialize += HandleOnInitialize;
|
Engine.OnInitialize += HandleOnInitialize;
|
||||||
@ -26,27 +16,6 @@ private static void Main(string[] args)
|
|||||||
|
|
||||||
private static void HandleOnInitialize()
|
private static void HandleOnInitialize()
|
||||||
{
|
{
|
||||||
//systems.Add(new ParentSystem(world));
|
|
||||||
systems.Add(new LocalToWorldSystem(world));
|
|
||||||
editorSystems.Add(new EditorHierarchyWindow(world));
|
|
||||||
|
|
||||||
Entity ent1 = world.CreateEntity("parent");
|
|
||||||
world.Set(ent1, new Root());
|
|
||||||
world.Set(ent1, new LocalTransform(new Vector3(1, 0, 0), Quaternion.Identity, Vector3.One));
|
|
||||||
|
|
||||||
Entity ent2 = world.CreateEntity("child");
|
|
||||||
world.Set(ent2, new LocalTransform(new Vector3(0, 1, 0), Quaternion.Identity, Vector3.One));
|
|
||||||
Transform.SetParent(world, ent2, ent1);
|
|
||||||
|
|
||||||
Entity ent3 = world.CreateEntity("entity3");
|
|
||||||
world.Set(ent3, new Root());
|
|
||||||
Transform.SetParent(world, ent3, ent2);
|
|
||||||
|
|
||||||
Entity ent4 = world.CreateEntity("entity4");
|
|
||||||
world.Set(ent4, new Root());
|
|
||||||
|
|
||||||
Entity ent5 = world.CreateBaseEntity("entity5");
|
|
||||||
|
|
||||||
// Open project.
|
// Open project.
|
||||||
// Setip EditorGui.
|
// Setip EditorGui.
|
||||||
EditorGui.Initialize();
|
EditorGui.Initialize();
|
||||||
@ -54,18 +23,11 @@ private static void HandleOnInitialize()
|
|||||||
|
|
||||||
private static void HandleOnUpdate()
|
private static void HandleOnUpdate()
|
||||||
{
|
{
|
||||||
foreach (MoonTools.ECS.System system in systems)
|
|
||||||
{
|
|
||||||
system.Update(Engine.Timestep);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Editor Update.
|
// Editor Update.
|
||||||
EditorGui.Update();
|
EditorGui.Update();
|
||||||
|
|
||||||
|
|
||||||
// Try Catch UserCode Update.
|
// Try Catch UserCode Update.
|
||||||
|
|
||||||
world.FinishUpdate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void HandleOnRender()
|
private static void HandleOnRender()
|
||||||
|
@ -1,189 +0,0 @@
|
|||||||
using ImGuiNET;
|
|
||||||
using MoonTools.ECS;
|
|
||||||
using Nerfed.Editor.Components;
|
|
||||||
using Nerfed.Runtime;
|
|
||||||
using Nerfed.Runtime.Components;
|
|
||||||
using Nerfed.Runtime.Util;
|
|
||||||
|
|
||||||
namespace Nerfed.Editor.Systems
|
|
||||||
{
|
|
||||||
// Window that draws entities.
|
|
||||||
internal class EditorHierarchyWindow : MoonTools.ECS.DebugSystem
|
|
||||||
{
|
|
||||||
private const ImGuiTreeNodeFlags baseFlags = ImGuiTreeNodeFlags.OpenOnArrow | ImGuiTreeNodeFlags.OpenOnDoubleClick | ImGuiTreeNodeFlags.SpanAvailWidth;
|
|
||||||
|
|
||||||
//private readonly Filter rootEntitiesWithTransformFilter;
|
|
||||||
//private readonly Filter rootEntitiesFilterBroken;
|
|
||||||
private readonly Filter rootEntitiesFilter;
|
|
||||||
|
|
||||||
private readonly EditorHierachySelectionSystem hierachySelectionSystem;
|
|
||||||
|
|
||||||
public EditorHierarchyWindow(World world) : base(world)
|
|
||||||
{
|
|
||||||
//rootEntitiesWithTransformFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
|
|
||||||
|
|
||||||
// TODO: this doesn't work.
|
|
||||||
//rootEntitiesFilterBroken = FilterBuilder.Exclude<Child>().Build();
|
|
||||||
|
|
||||||
// Maybe the parent/child functions should add a root component when not being a child.
|
|
||||||
rootEntitiesFilter = FilterBuilder.Include<Root>().Build();
|
|
||||||
|
|
||||||
// Maybe instead of a root, if we need a component that is always on an entity and has some use we could create something like a VersionComponent which only hold an int.
|
|
||||||
// The version would update each time something changes on the entity.
|
|
||||||
// Or a EditorComponent, just a component that always gets added when in editor mode.
|
|
||||||
|
|
||||||
hierachySelectionSystem = new EditorHierachySelectionSystem(world);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Update(TimeSpan delta)
|
|
||||||
{
|
|
||||||
ImGui.Begin("Hierarchy");
|
|
||||||
|
|
||||||
ImGuiTreeNodeFlags flags = baseFlags;
|
|
||||||
flags |= ImGuiTreeNodeFlags.DefaultOpen;
|
|
||||||
|
|
||||||
if (ImGui.TreeNodeEx("World", flags))
|
|
||||||
{
|
|
||||||
if (ImGui.BeginDragDropTarget())
|
|
||||||
{
|
|
||||||
unsafe
|
|
||||||
{
|
|
||||||
ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}");
|
|
||||||
if (payload.NativePtr != null)
|
|
||||||
{
|
|
||||||
Entity* data = (Entity*)payload.Data;
|
|
||||||
Entity child = data[0];
|
|
||||||
|
|
||||||
Log.Info($"Dropped {child.ID}");
|
|
||||||
|
|
||||||
Transform.RemoveParent(World, child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ImGui.EndDragDropTarget();
|
|
||||||
}
|
|
||||||
|
|
||||||
//foreach (Entity entity in rootEntitiesWithTransformFilter.Entities)
|
|
||||||
//{
|
|
||||||
// DrawEntityAndChildren(entity);
|
|
||||||
//}
|
|
||||||
|
|
||||||
foreach (Entity entity in rootEntitiesFilter.Entities)
|
|
||||||
{
|
|
||||||
DrawEntityAndChildren(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
ImGui.TreePop();
|
|
||||||
}
|
|
||||||
|
|
||||||
ImGui.End();
|
|
||||||
|
|
||||||
hierachySelectionSystem.Update(delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DrawEntityAndChildren(in Entity entity)
|
|
||||||
{
|
|
||||||
ImGuiTreeNodeFlags flags = baseFlags;
|
|
||||||
|
|
||||||
if (!World.HasInRelation<ChildParentRelation>(entity))
|
|
||||||
{
|
|
||||||
flags |= ImGuiTreeNodeFlags.Leaf;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (World.Has<SelectedInHierachy>(entity))
|
|
||||||
{
|
|
||||||
flags |= ImGuiTreeNodeFlags.Selected;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ImGui.TreeNodeEx($"{entity.ID} | {GetTag(entity)}", flags))
|
|
||||||
{
|
|
||||||
// Selection.
|
|
||||||
if (ImGui.IsItemClicked() && !ImGui.IsItemToggledOpen())
|
|
||||||
{
|
|
||||||
World.Set(entity, new ClickedInHierachy());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drag and drop.
|
|
||||||
if (ImGui.BeginDragDropSource())
|
|
||||||
{
|
|
||||||
unsafe
|
|
||||||
{
|
|
||||||
fixed (Entity* payload = &entity)
|
|
||||||
{
|
|
||||||
ImGui.SetDragDropPayload($"{nameof(EditorHierarchyWindow)}", (IntPtr)payload, (uint)sizeof(Entity));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ImGui.EndDragDropSource();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ImGui.BeginDragDropTarget())
|
|
||||||
{
|
|
||||||
unsafe
|
|
||||||
{
|
|
||||||
ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}");
|
|
||||||
if (payload.NativePtr != null)
|
|
||||||
{
|
|
||||||
Entity ent = *(Entity*)payload.Data;
|
|
||||||
|
|
||||||
Log.Info($"Dropped {ent.ID}");
|
|
||||||
|
|
||||||
Transform.SetParent(World, ent, entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ImGui.EndDragDropTarget();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw children.
|
|
||||||
ReverseSpanEnumerator<Entity> childEntities = World.InRelations<ChildParentRelation>(entity);
|
|
||||||
foreach (Entity childEntity in childEntities)
|
|
||||||
{
|
|
||||||
DrawEntityAndChildren(childEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
ImGui.TreePop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// System for handling the selected entities in the hierachy.
|
|
||||||
private class EditorHierachySelectionSystem : MoonTools.ECS.System
|
|
||||||
{
|
|
||||||
private readonly Filter selectedEntities;
|
|
||||||
private readonly Filter clickedEntities;
|
|
||||||
|
|
||||||
public EditorHierachySelectionSystem(World world) : base(world)
|
|
||||||
{
|
|
||||||
selectedEntities = FilterBuilder.Include<SelectedInHierachy>().Build();
|
|
||||||
clickedEntities = FilterBuilder.Include<ClickedInHierachy>().Build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Update(TimeSpan delta)
|
|
||||||
{
|
|
||||||
ImGuiIOPtr io = ImGui.GetIO();
|
|
||||||
|
|
||||||
if (!clickedEntities.Empty && !io.KeyCtrl)
|
|
||||||
{
|
|
||||||
foreach (Entity entity in selectedEntities.Entities)
|
|
||||||
{
|
|
||||||
Remove<SelectedInHierachy>(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Entity entity in clickedEntities.Entities)
|
|
||||||
{
|
|
||||||
// Unselect.
|
|
||||||
if (Has<SelectedInHierachy>(entity))
|
|
||||||
{
|
|
||||||
Remove<SelectedInHierachy>(entity);
|
|
||||||
}
|
|
||||||
// Select.
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Set(entity, new SelectedInHierachy());
|
|
||||||
}
|
|
||||||
|
|
||||||
Remove<ClickedInHierachy>(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
using System.Numerics;
|
|
||||||
|
|
||||||
namespace Nerfed.Runtime.Components
|
|
||||||
{
|
|
||||||
public readonly record struct LocalToWorld(Matrix4x4 localToWorldMatrix);
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
using System.Numerics;
|
|
||||||
|
|
||||||
namespace Nerfed.Runtime.Components
|
|
||||||
{
|
|
||||||
public struct LocalTransform
|
|
||||||
{
|
|
||||||
public Vector3 position;
|
|
||||||
public Quaternion rotation;
|
|
||||||
public Vector3 scale;
|
|
||||||
|
|
||||||
public LocalTransform(Vector3 position, Quaternion rotation, Vector3 scale)
|
|
||||||
{
|
|
||||||
this.position = position;
|
|
||||||
this.rotation = rotation;
|
|
||||||
this.scale = scale;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly LocalTransform Identity = new(Vector3.Zero, Quaternion.Identity, Vector3.One);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
using MoonTools.ECS;
|
|
||||||
|
|
||||||
namespace Nerfed.Runtime.Components
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
namespace Nerfed.Runtime.Components
|
|
||||||
{
|
|
||||||
public readonly record struct Test();
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
Subproject commit 76b18a6ba9a33f5a93022390be7ed805f9f722e8
|
|
@ -32,7 +32,6 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Libraries\ECS\src\**\*.cs" />
|
|
||||||
<Compile Include="Libraries\SDL2CS\src\SDL2.cs"/>
|
<Compile Include="Libraries\SDL2CS\src\SDL2.cs"/>
|
||||||
<Compile Include="Libraries\RefreshCS\RefreshCS.cs"/>
|
<Compile Include="Libraries\RefreshCS\RefreshCS.cs"/>
|
||||||
<Compile Include="Libraries\FAudio\csharp\FAudio.cs"/>
|
<Compile Include="Libraries\FAudio\csharp\FAudio.cs"/>
|
||||||
|
@ -1,58 +0,0 @@
|
|||||||
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)
|
|
||||||
{
|
|
||||||
Matrix4x4 rootMatrix = Matrix4x4.Identity;
|
|
||||||
|
|
||||||
foreach (Entity entity in rootEntitiesFilter.Entities)
|
|
||||||
{
|
|
||||||
UpdateWorldTransform(entity, rootMatrix);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
//Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
|
||||||
}
|
|
||||||
|
|
||||||
ReverseSpanEnumerator<Entity> childEntities = World.OutRelations<ChildParentRelation>(entity);
|
|
||||||
foreach (Entity childEntity in childEntities)
|
|
||||||
{
|
|
||||||
UpdateWorldTransform(childEntity, localToWorldMatrix);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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.
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
|
@ -1,99 +0,0 @@
|
|||||||
using MoonTools.ECS;
|
|
||||||
using Nerfed.Runtime.Components;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Numerics;
|
|
||||||
|
|
||||||
namespace Nerfed.Runtime.Util
|
|
||||||
{
|
|
||||||
// https://github.com/needle-mirror/com.unity.entities/blob/master/Unity.Transforms/TransformHelpers.cs
|
|
||||||
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();
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
{
|
|
||||||
RemoveParent(world, child);
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
Entity parent = world.OutRelationSingleton<ChildParentRelation>(child);
|
|
||||||
|
|
||||||
// TODO: Check if Unrelate all also unrelates incomming relations..?
|
|
||||||
world.Unrelate<ChildParentRelation>(child, parent);
|
|
||||||
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.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, Matrix4x4 localToWorldMatrix)
|
|
||||||
{
|
|
||||||
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}");
|
|
||||||
}
|
|
||||||
|
|
||||||
ReverseSpanEnumerator<Entity> childEntities = world.InRelations<ChildParentRelation>(entity);
|
|
||||||
foreach (Entity childEntity in childEntities)
|
|
||||||
{
|
|
||||||
ForceUpdateLocalToWorld(world, childEntity, localToWorldMatrix);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user