This commit is contained in:
Simon Lübeß
2022-11-16 13:09:26 +01:00
parent a42544f171
commit 0155a38ae6
8 changed files with 367 additions and 21 deletions
@@ -0,0 +1,212 @@
{
Name = "Scene name here pls!!!",
Entities = [
{
Id = 6000868443984780499,
NameComponent = {
Name = "Child"
},
SpriterRendererComponent = {
Color = {
R = 1,
G = 0.388184,
B = 0.090109,
A = 1
},
UvTransform = {
X = 0,
Y = 0,
Z = 1,
W = 1
}
},
TransformComponent = {
ParentId = 820806682740348099,
Position = {
X = 0,
Y = 0.75,
Z = 0
},
Rotation = {
X = 0,
Y = 0,
Z = 0,
W = 1
},
Scale = {
X = 0.5,
Y = 0.5,
Z = 1
},
EditorEulerRotation = {
X = 0,
Y = 0,
Z = 0
}
}
},
{
Id = 820806682740348099,
NameComponent = {
Name = "Quad"
},
SpriterRendererComponent = {
Color = {
R = 0,
G = 0.551178,
B = 0.328787,
A = 1
},
UvTransform = {
X = 0,
Y = 0,
Z = 1,
W = 1
}
},
TransformComponent = {
Position = {
X = 0,
Y = 2,
Z = 0
},
Rotation = {
X = 0,
Y = 0,
Z = 0,
W = 1
},
Scale = {
X = 1,
Y = 1,
Z = 1
},
EditorEulerRotation = {
X = 0,
Y = 0,
Z = 0
}
},
Rigidbody2D = {
BodyType = .Dynamic,
FixedRotation = false
},
BoxCollider2D = {
Offset = {
X = 0,
Y = 0
},
Size = {
X = 0.5,
Y = 0.5
},
Density = 1,
Friction = 0.5,
Restitution = 0,
RestitutionThreshold = 0.5
}
},
{
Id = 15398726361722237419,
NameComponent = {
Name = "Floor"
},
SpriterRendererComponent = {
Color = {
R = 1,
G = 0.941886,
B = 0.401485,
A = 1
},
UvTransform = {
X = 0,
Y = 0,
Z = 1,
W = 1
}
},
TransformComponent = {
Position = {
X = 0,
Y = -0.5,
Z = 0
},
Rotation = {
X = 0,
Y = 0,
Z = 0,
W = 1
},
Scale = {
X = 10,
Y = 1,
Z = 1
},
EditorEulerRotation = {
X = 0,
Y = 0,
Z = 0
}
},
Rigidbody2D = {
BodyType = .Static,
FixedRotation = false
},
BoxCollider2D = {
Offset = {
X = 0,
Y = 0
},
Size = {
X = 0.5,
Y = 0.5
},
Density = 1,
Friction = 0.5,
Restitution = 0,
RestitutionThreshold = 0.5
}
},
{
Id = 1491484622542812645,
NameComponent = {
Name = "Camera"
},
TransformComponent = {
Position = {
X = 0,
Y = 1,
Z = -5
},
Rotation = {
X = 0,
Y = 0,
Z = 0,
W = 1
},
Scale = {
X = 1,
Y = 1,
Z = 1
},
EditorEulerRotation = {
X = 0,
Y = 0,
Z = 0
}
},
CameraComponent = {
Primary = true,
ProjectionType = .InfinitePerspective,
PerspectiveFovY = 1.308997,
PerspectiveNearPlane = 0.1,
PerspectiveFarPlane = 10000,
OrthographicHeight = 10,
OrthographicNearPlane = 0,
OrthographicFarPlane = 10,
AspectRatio = 2.116827,
FixedAspectRatio = false
}
}
]
}
+4 -4
View File
@@ -2,7 +2,7 @@
Name = "Scene name here pls!!!", Name = "Scene name here pls!!!",
Entities = [ Entities = [
{ {
Id = 0, Id = 820806682740348099,
NameComponent = { NameComponent = {
Name = "Quad" Name = "Quad"
}, },
@@ -63,7 +63,7 @@
} }
}, },
{ {
Id = 1, Id = 15398726361722237419,
NameComponent = { NameComponent = {
Name = "Floor" Name = "Floor"
}, },
@@ -124,7 +124,7 @@
} }
}, },
{ {
Id = 2, Id = 1491484622542812645,
NameComponent = { NameComponent = {
Name = "Camera" Name = "Camera"
}, },
@@ -160,7 +160,7 @@
OrthographicHeight = 10, OrthographicHeight = 10,
OrthographicNearPlane = 0, OrthographicNearPlane = 0,
OrthographicFarPlane = 10, OrthographicFarPlane = 10,
AspectRatio = 2.329843, AspectRatio = 2.864979,
FixedAspectRatio = false FixedAspectRatio = false
} }
} }
+8 -7
View File
@@ -389,7 +389,8 @@ namespace GlitchyEditor
mat.SetVariable("NormalScaling", Vector2.One); mat.SetVariable("NormalScaling", Vector2.One);
mat.SetVariable("MetallicFactor", 1.0f); mat.SetVariable("MetallicFactor", 1.0f);
mat.SetVariable("RoughnessFactor", 1.0f); mat.SetVariable("RoughnessFactor", 1.0f);
// TODO: completely wrong!
EcsEntity e = ModelLoader.LoadModel("content/Models/sphere.glb", mat, _scene.[Friend]_ecsWorld, clips, "Sphere 1"); EcsEntity e = ModelLoader.LoadModel("content/Models/sphere.glb", mat, _scene.[Friend]_ecsWorld, clips, "Sphere 1");
Entity entity = .(e, _scene); Entity entity = .(e, _scene);
@@ -463,14 +464,14 @@ namespace GlitchyEditor
_camera.Position = .(-1.5f, 1.5f, -2.5f); _camera.Position = .(-1.5f, 1.5f, -2.5f);
_camera.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0); _camera.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0);
/*// Create default camera // Create default camera
{ /*{
let camEntity = _scene.CreateEntity("Camera"); let camEntity = _scene.CreateEntity("Camera");
let transform = camEntity.Transform; let transform = camEntity.Transform;
transform.Position = transform.Position =
} }*/
// Create the default light source /*// Create the default light source
{ {
let lightNtt = _scene.CreateEntity("Light"); let lightNtt = _scene.CreateEntity("Light");
let transform = lightNtt.Transform; let transform = lightNtt.Transform;
@@ -480,9 +481,9 @@ namespace GlitchyEditor
let light = lightNtt.AddComponent<LightComponent>(); let light = lightNtt.AddComponent<LightComponent>();
light.SceneLight.Illuminance = 10.0f; light.SceneLight.Illuminance = 10.0f;
light.SceneLight.Color = .(1.0f, 0.95f, 0.8f); light.SceneLight.Color = .(1.0f, 0.95f, 0.8f);
}*/ }
//TestEntitiesWithModels(); TestEntitiesWithModels();*/
} }
/// Saves the scene in the file that is was loaded from or saved to last. If there is no such path (i.e. it is a new scene) the save file dialog will open. /// Saves the scene in the file that is was loaded from or saved to last. If there is no such path (i.e. it is a new scene) the save file dialog will open.
+53
View File
@@ -0,0 +1,53 @@
using Bon;
using System;
using Bon.Integrated;
using System.Collections;
namespace GlitchyEngine.Core
{
[BonTarget]
struct UUID : IHashable
{
[BonInclude]
private uint64 _uuid;
private static Random s_Random = new .() ~ delete _;
static this()
{
gBonEnv.typeHandlers.Add(typeof(UUID),
((.)new => Serialize, (.)new => Deserialize));
}
/// Creates a new random UUID.
public this()
{
_uuid = s_Random.NextU64();
}
/// Creates a new UUID with the given value.
public this(uint64 uuid)
{
_uuid = uuid;
}
public int GetHashCode()
{
return (int)_uuid;
}
static void Serialize(BonWriter writer, ValueView val, BonEnvironment env)
{
UUID uuid = *(UUID*)val.dataPtr;
Bon.Integrated.Serialize.[Friend]Integer(typeof(uint64), writer, ValueView(typeof(uint64), &uuid._uuid));
}
public static Result<void> Deserialize(BonReader reader, ValueView val, BonEnvironment env)
{
Bon.Integrated.Deserialize.[Friend]Integer!(typeof(uint64), reader, val);
return .Ok;
}
}
}
+17
View File
@@ -1,6 +1,7 @@
using System; using System;
using GlitchyEngine.Math; using GlitchyEngine.Math;
using GlitchyEngine.Renderer; using GlitchyEngine.Renderer;
using GlitchyEngine.Core;
namespace GlitchyEngine.World namespace GlitchyEngine.World
{ {
@@ -15,6 +16,22 @@ namespace GlitchyEngine.World
} }
} }
struct IDComponent
{
public readonly UUID ID;
/// Create aa new IDComponent with a random UUID.
public this()
{
ID = UUID();
}
public this(UUID id)
{
ID = id;
}
}
/// If an entity has the EditorComponent it won't be displayed in the scene hierarchy. /// If an entity has the EditorComponent it won't be displayed in the scene hierarchy.
struct EditorComponent struct EditorComponent
{ {
+3
View File
@@ -1,5 +1,6 @@
using System; using System;
using System.Collections; using System.Collections;
using GlitchyEngine.Core;
using internal GlitchyEngine.World; using internal GlitchyEngine.World;
@@ -63,6 +64,8 @@ namespace GlitchyEngine.World
} }
} }
public UUID UUID => GetComponent<IDComponent>().ID;
public TransformComponent* Transform => GetComponent<TransformComponent>(); public TransformComponent* Transform => GetComponent<TransformComponent>();
public T* AddComponent<T>(T value = T()) where T: struct, new public T* AddComponent<T>(T value = T()) where T: struct, new
+26 -1
View File
@@ -3,6 +3,7 @@ using GlitchyEngine.Renderer;
using System; using System;
using System.Collections; using System.Collections;
using Box2D; using Box2D;
using GlitchyEngine.Core;
namespace GlitchyEngine.World namespace GlitchyEngine.World
{ {
@@ -24,6 +25,9 @@ namespace GlitchyEngine.World
private Effect _gammaCorrectEffect ~ _.ReleaseRef(); private Effect _gammaCorrectEffect ~ _.ReleaseRef();
// Maps ids to the entities they represent.
private Dictionary<UUID, EcsEntity> _idToEntity = new .() ~ delete _;
public Entity ActiveCamera => { public Entity ActiveCamera => {
Entity cameraEntity = .(); Entity cameraEntity = .();
@@ -205,6 +209,8 @@ namespace GlitchyEngine.World
} }
} }
renderTarget = _cameraTarget..AddRef();
// 3D render // 3D render
Renderer.BeginScene(*primaryCamera, primaryCameraTransform, renderTarget, _compositeTarget); Renderer.BeginScene(*primaryCamera, primaryCameraTransform, renderTarget, _compositeTarget);
@@ -468,13 +474,20 @@ namespace GlitchyEngine.World
} }
/// Creates a new Entity with the given name. /// Creates a new Entity with the given name.
public Entity CreateEntity(String name = "") public Entity CreateEntity(String name = "", UUID id = default)
{ {
Entity entity = Entity(_ecsWorld.NewEntity(), this); Entity entity = Entity(_ecsWorld.NewEntity(), this);
entity.AddComponent<TransformComponent>(); entity.AddComponent<TransformComponent>();
let nameComponent = entity.AddComponent<DebugNameComponent>(); let nameComponent = entity.AddComponent<DebugNameComponent>();
nameComponent.SetName(name.IsEmpty ? "Entity" : name); nameComponent.SetName(name.IsEmpty ? "Entity" : name);
// If no id is given generate a random one.
IDComponent idComponent = (id == default) ? IDComponent() : IDComponent(id);
entity.AddComponent<IDComponent>(idComponent);
_idToEntity.Add(idComponent.ID, entity.Handle);
return entity; return entity;
} }
@@ -485,6 +498,8 @@ namespace GlitchyEngine.World
*/ */
public void DestroyEntity(Entity entity, bool destroyChildren = false) public void DestroyEntity(Entity entity, bool destroyChildren = false)
{ {
_idToEntity.Remove(entity.UUID);
if (destroyChildren) if (destroyChildren)
{ {
for (Entity child in entity.EnumerateChildren) for (Entity child in entity.EnumerateChildren)
@@ -496,6 +511,16 @@ namespace GlitchyEngine.World
_ecsWorld.RemoveEntity(entity.Handle); _ecsWorld.RemoveEntity(entity.Handle);
} }
public Result<Entity> GetEntityByID(UUID id)
{
if (_idToEntity.TryGetValue(id, let ecsEntity))
{
return .Ok(Entity(ecsEntity, this));
}
return .Err;
}
private uint32 ViewportWidth, ViewportHeight; private uint32 ViewportWidth, ViewportHeight;
/// Sets the size of the viewport into which the scene will be rendered. /// Sets the size of the viewport into which the scene will be rendered.
+44 -9
View File
@@ -4,6 +4,8 @@ using System;
using System.Reflection; using System.Reflection;
using System.IO; using System.IO;
using GlitchyEngine.Math; using GlitchyEngine.Math;
using GlitchyEngine.Core;
using System.Collections;
namespace GlitchyEngine.World namespace GlitchyEngine.World
{ {
@@ -13,6 +15,11 @@ namespace GlitchyEngine.World
{ {
private Scene _scene; private Scene _scene;
// Maps from ParentID to ChildEntity
private Dictionary<UUID, Entity> _parentIdToChild;
private List<(Entity Entity, UUID ParentId)> _entitiesMissingParent;
public this(Scene scene) public this(Scene scene)
{ {
_scene = scene; _scene = scene;
@@ -60,14 +67,13 @@ namespace GlitchyEngine.World
File.WriteAllText(filePath, buffer); File.WriteAllText(filePath, buffer);
} }
static void SerializeEntity(BonWriter writer, Entity entity) void SerializeEntity(BonWriter writer, Entity entity)
{ {
writer.EntryStart(); writer.EntryStart();
using (writer.ObjectBlock()) using (writer.ObjectBlock())
{ {
// TODO: Entity GUID goes here! Serialize.Value(writer, "Id", entity.UUID);
Serialize.Value(writer, "Id", entity.Handle.Index);
SerializeComponent<EditorComponent>(writer, entity, "EditorComponent", scope (component) => {}); SerializeComponent<EditorComponent>(writer, entity, "EditorComponent", scope (component) => {});
@@ -88,7 +94,10 @@ namespace GlitchyEngine.World
{ {
// TODO: Use GUIDs // TODO: Use GUIDs
if (component.Parent != .InvalidEntity) if (component.Parent != .InvalidEntity)
Serialize.Value(writer, "ParentId", component.Parent.Index); {
Entity parent = Entity(component.Parent, _scene);
Serialize.Value(writer, "ParentId", parent.UUID);
}
Serialize.Value(writer, "Position", component.Position); Serialize.Value(writer, "Position", component.Position);
Serialize.Value(writer, "Rotation", component.Rotation); Serialize.Value(writer, "Rotation", component.Rotation);
@@ -177,6 +186,9 @@ namespace GlitchyEngine.World
{ {
Debug.Profiler.ProfileResourceFunction!(); Debug.Profiler.ProfileResourceFunction!();
_parentIdToChild = scope Dictionary<UUID, Entity>();
_entitiesMissingParent = scope List<(Entity Entity, UUID ParentId)>();
String buffer = scope String(); String buffer = scope String();
File.ReadAllText(filePath, buffer); File.ReadAllText(filePath, buffer);
@@ -217,6 +229,19 @@ namespace GlitchyEngine.World
Try!(Deserialize.End(reader)); Try!(Deserialize.End(reader));
// Find parents for entities that don't have their parent yet
for ((Entity Entity, UUID ParentId) entry in _entitiesMissingParent)
{
let parentResult = _scene.GetEntityByID(entry.ParentId);
Log.EngineLogger.Assert(parentResult case .Ok, "Parent entity does not exist.");
if (parentResult case .Ok(let parent))
{
entry.Entity.Parent = parent;
}
}
return .Ok; return .Ok;
} }
@@ -224,10 +249,9 @@ namespace GlitchyEngine.World
{ {
Try!(reader.ObjectBlock()); Try!(reader.ObjectBlock());
Entity entity = _scene.CreateEntity(); Deserialize.Value<uint64>(reader, "Id", let uuid);
// TODO: GUID? Entity entity = _scene.CreateEntity("", UUID(uuid));
Deserialize.Value<uint32>(reader, "Id", let id);
while(reader.ObjectHasMore()) while(reader.ObjectHasMore())
{ {
@@ -282,10 +306,21 @@ namespace GlitchyEngine.World
let nextId = Try!(reader.Identifier()); let nextId = Try!(reader.Identifier());
if (nextId == "ParentId") if (nextId == "ParentId")
{ {
// TODO: Use GUIDs UUID pId;
uint32 pId;
Deserialize.Value(reader, out pId); Deserialize.Value(reader, out pId);
reader.EntryEnd(); reader.EntryEnd();
var parentEntity = _scene.GetEntityByID(pId);
if (parentEntity case .Ok(let parent))
{
component.Parent = parent.Handle;
}
else
{
_entitiesMissingParent.Add((entity, pId));
}
Deserialize.Value(reader, "Position", out component.[Friend]_position); Deserialize.Value(reader, "Position", out component.[Friend]_position);
reader.EntryEnd(); reader.EntryEnd();
} }