From 960eeaf94bc2c3b4e49e2ff5f2abd012ce153508 Mon Sep 17 00:00:00 2001 From: Atdunbg Date: Mon, 29 Dec 2025 23:11:14 +0800 Subject: [PATCH] add transform class, some code tweaks --- Editor/Editor/EditorLayer.cpp | 22 ++++-- Editor/assets/scenes/FPSDemo.scene | 12 +-- ExampleApp/Src/BasicContorller.cs | 2 + ExampleApp/Src/FPSPlayer.cs | 74 ++++++++----------- ExampleApp/Src/MapGenerator.cs | 2 + ExampleApp/Src/PlayerCube.cs | 2 + ExampleApp/Src/PlayerSphere.cs | 12 +-- ExampleApp/Src/Script.cs | 12 +-- ExampleApp/Src/Sink.cs | 2 + Prism-ScriptCore/Src/Prism/Entity.cs | 18 +---- Prism-ScriptCore/Src/Prism/Math/Mathf.cs | 9 ++- Prism-ScriptCore/Src/Prism/Math/Queternion.cs | 56 -------------- Prism-ScriptCore/Src/Prism/Math/Transform.cs | 16 ++++ Prism-ScriptCore/Src/Prism/Math/Vec3.cs | 20 ++++- Prism-ScriptCore/Src/Prism/Scene/Component.cs | 68 +++++++---------- Prism/src/Prism/Core/Math/Transform.cpp | 39 ++++++++++ Prism/src/Prism/Core/Math/Transform.h | 60 +++++++++++++++ Prism/src/Prism/Editor/SceneHierachyPanel.cpp | 25 ++----- Prism/src/Prism/Physics/Physics3D.cpp | 23 ++---- Prism/src/Prism/Physics/PhysicsLayer.cpp | 2 +- Prism/src/Prism/Physics/PhysicsUtils.cpp | 9 +++ Prism/src/Prism/Physics/PhysicsUtils.h | 4 +- Prism/src/Prism/Physics/PhysicsWrappers.cpp | 2 +- Prism/src/Prism/Physics/PhysicsWrappers.h | 2 +- Prism/src/Prism/Scene/Components.h | 11 +-- Prism/src/Prism/Scene/Entity.h | 4 +- Prism/src/Prism/Scene/Scene.cpp | 50 +++++-------- Prism/src/Prism/Scene/SceneSerializer.cpp | 37 ++++------ Prism/src/Prism/Script/ScriptEngine.cpp | 6 +- .../src/Prism/Script/ScriptEngineRegistry.cpp | 8 ++ Prism/src/Prism/Script/ScriptWrappers.cpp | 59 ++++++++++----- Prism/src/Prism/Script/ScriptWrappers.h | 14 ++++ 32 files changed, 368 insertions(+), 314 deletions(-) delete mode 100644 Prism-ScriptCore/Src/Prism/Math/Queternion.cs create mode 100644 Prism-ScriptCore/Src/Prism/Math/Transform.cs create mode 100644 Prism/src/Prism/Core/Math/Transform.cpp create mode 100644 Prism/src/Prism/Core/Math/Transform.h diff --git a/Editor/Editor/EditorLayer.cpp b/Editor/Editor/EditorLayer.cpp index cc2bba7..e7cfa44 100644 --- a/Editor/Editor/EditorLayer.cpp +++ b/Editor/Editor/EditorLayer.cpp @@ -218,7 +218,7 @@ namespace Prism auto viewProj = m_EditorCamera.GetViewProjection(); Renderer2D::BeginScene(viewProj, false); glm::vec4 color = (m_SelectionMode == SelectionMode::Entity) ? glm::vec4{ 1.0f, 1.0f, 1.0f, 1.0f } : glm::vec4{ 0.2f, 0.9f, 0.2f, 1.0f }; - Renderer::DrawAABB(selection.Mesh->BoundingBox, selection.Entity.GetComponent().Transform * selection.Mesh->Transform, color); + Renderer::DrawAABB(selection.Mesh->BoundingBox, selection.Entity.GetComponent().Transformation.GetMatrix() * selection.Mesh->Transform, color); Renderer2D::EndScene(); Renderer::EndRenderPass(); } @@ -231,7 +231,7 @@ namespace Prism if (selection.Entity.HasComponent()) { const auto& size = selection.Entity.GetComponent().Size; - auto [translation, rotationQuat, scale] = GetTransformDecomposition(selection.Entity.GetComponent().Transform); + auto [translation, rotationQuat, scale] = GetTransformDecomposition(selection.Entity.GetComponent().Transformation.GetMatrix()); const glm::vec3 rotation = glm::eulerAngles(rotationQuat); Renderer::BeginRenderPass(SceneRenderer::GetFinalRenderPass(), false); @@ -786,21 +786,27 @@ namespace Prism bool snap = Input::IsKeyPressed(Key::LEFT_CONTROL); - auto& entityTransform = selection.Entity.Transform(); + Transform& entityTransform = selection.Entity.Transformation(); float snapValue = GetSnapValue(); float snapValues[3] = { snapValue, snapValue, snapValue }; if (m_SelectionMode == SelectionMode::Entity) { + glm::mat4 transform = entityTransform.GetMatrix(); ImGuizmo::Manipulate(glm::value_ptr(m_EditorCamera.GetViewMatrix()), glm::value_ptr(m_EditorCamera.GetProjectionMatrix()), (ImGuizmo::OPERATION)m_GizmoType, ImGuizmo::LOCAL, - glm::value_ptr(entityTransform), + glm::value_ptr(transform), nullptr, snap ? snapValues : nullptr); + + auto [translation, rotation, scale] = GetTransformDecomposition(transform); + entityTransform.SetTranslation(translation); + entityTransform.SetRotation(rotation); + entityTransform.SetScale(scale); }else { - glm::mat4 transformBase = entityTransform * selection.Mesh->Transform; + glm::mat4 transformBase = entityTransform.GetMatrix() * selection.Mesh->Transform; ImGuizmo::Manipulate(glm::value_ptr(m_EditorCamera.GetViewMatrix()), glm::value_ptr(m_EditorCamera.GetProjectionMatrix()), (ImGuizmo::OPERATION)m_GizmoType, @@ -809,7 +815,7 @@ namespace Prism nullptr, snap ? snapValues : nullptr); - selection.Mesh->Transform = glm::inverse(entityTransform) * transformBase; + selection.Mesh->Transform = glm::inverse(entityTransform.GetMatrix()) * transformBase; } } @@ -946,8 +952,8 @@ namespace Prism { auto& submesh = submeshes[i]; Ray ray = { - glm::inverse(entity.Transform() * submesh.Transform) * glm::vec4(origin, 1.0f), - glm::inverse(glm::mat3(entity.Transform()) * glm::mat3(submesh.Transform)) * direction + glm::inverse(entity.Transformation().GetMatrix() * submesh.Transform) * glm::vec4(origin, 1.0f), + glm::inverse(glm::mat3(entity.Transformation().GetMatrix()) * glm::mat3(submesh.Transform)) * direction }; float t = 0; diff --git a/Editor/assets/scenes/FPSDemo.scene b/Editor/assets/scenes/FPSDemo.scene index 5decd2e..5cea4fe 100644 --- a/Editor/assets/scenes/FPSDemo.scene +++ b/Editor/assets/scenes/FPSDemo.scene @@ -11,7 +11,7 @@ Entities: Tag: Box TransformComponent: Position: [0, 0, 0] - Rotation: [1, 0, 0, 0] + Rotation: [0, 0, 0] Scale: [50, 1, 50] MeshComponent: AssetPath: assets/meshes/Cube1m.fbx @@ -40,7 +40,7 @@ Entities: Tag: Camera TransformComponent: Position: [2.808, 2.25, 0] - Rotation: [1, 0, 0, 0] + Rotation: [0, 0, 0] Scale: [1, 1, 1] CameraComponent: Camera: some camera data... @@ -50,7 +50,7 @@ Entities: Tag: Sphere TransformComponent: Position: [-3.9876995, 1, -1.9669533e-06] - Rotation: [1, 0, 0, 0] + Rotation: [0, 0, 0] Scale: [1, 1, 1] MeshComponent: AssetPath: assets/meshes/Sphere1m.fbx @@ -78,7 +78,7 @@ Entities: Tag: Box TransformComponent: Position: [0, 1.5, 0] - Rotation: [1, 0, 0, 0] + Rotation: [0, 0, 0] Scale: [2, 2, 2] MeshComponent: AssetPath: assets/meshes/Cube1m.fbx @@ -107,7 +107,7 @@ Entities: Tag: Mesh Collider TransformComponent: Position: [-2.6045518, 1, -0.0017139912] - Rotation: [1, 0, 0, 0] + Rotation: [0, 0, 0] Scale: [1, 1, 1] MeshComponent: AssetPath: assets/meshes/Sphere1m.fbx @@ -135,7 +135,7 @@ Entities: Tag: Player TransformComponent: Position: [2.8080375, 1.5, 0] - Rotation: [1, 0, 0, 0] + Rotation: [0, 0, 0] Scale: [2, 2, 2] ScriptComponent: ModuleName: FPSExample.FPSPlayer diff --git a/ExampleApp/Src/BasicContorller.cs b/ExampleApp/Src/BasicContorller.cs index f15ca02..cf53594 100644 --- a/ExampleApp/Src/BasicContorller.cs +++ b/ExampleApp/Src/BasicContorller.cs @@ -16,6 +16,7 @@ namespace Example public void OnUpdate(float ts) { + /* Mat4 transform = GetTransform(); Vec3 playerTranstation = m_PlayerEntity.GetTransform().Translation; @@ -25,6 +26,7 @@ namespace Example translation.Y = Math.Max(translation.Y, 4.5f); transform.Translation = translation; SetTransform(transform); + */ } diff --git a/ExampleApp/Src/FPSPlayer.cs b/ExampleApp/Src/FPSPlayer.cs index 5c533c8..ad31c1f 100644 --- a/ExampleApp/Src/FPSPlayer.cs +++ b/ExampleApp/Src/FPSPlayer.cs @@ -20,8 +20,6 @@ namespace FPSExample private Entity m_CameraEntity; private Vec2 m_LastMousePosition; - private float m_CameraRotationX = 0.0f; - private float m_RotationY = 0.0f; void OnCreate() { @@ -50,6 +48,7 @@ namespace FPSExample Input.SetCursorMode(Input.CursorMode.Locked); m_CurrentSpeed = Input.IsKeyPressed(KeyCode.LeftControl) ? RunSpeed : WalkingSpeed; + UpdateRotation(ts); UpdateMovement(); @@ -58,60 +57,59 @@ namespace FPSExample private void UpdateRotation(float ts) { - Vec2 currentMousePosition = Input.GetMousePosition(); - Vec2 delta = m_LastMousePosition - currentMousePosition; - if (delta.X != 0) + Vec2 currentMousePosition = Input.GetMousePosition(); + Vec2 delta = m_LastMousePosition - currentMousePosition; + + float yRotation = delta.X * MouseSensitivity * ts; + float xRotation = delta.Y * MouseSensitivity * ts; + m_RigidBody.Rotate(new Vec3(0.0f, yRotation, 0.0f)); + + if (delta.X != 0 || delta.Y != 0) { - m_RotationY += delta.X * MouseSensitivity * ts; + m_CameraTransform.Rotation += new Vec3(xRotation, yRotation, 0.0f); } - - m_RigidBody.Rotate(new Vec3(0.0F, delta.X * MouseSensitivity, 0.0f) * ts); - - if (delta.Y != 0.0F) - { - m_CameraRotationX += delta.Y * MouseSensitivity * ts; - m_CameraRotationX = Mathf.Clamp(m_CameraRotationX, -80.0f, 80.0f); - } - + + m_CameraTransform.Rotation = new Vec3(Mathf.Clamp(m_CameraTransform.Rotation.X, -89.0f, 89.0f), m_CameraTransform.Rotation.YZ); + m_LastMousePosition = currentMousePosition; } - Collider[] colliders = new Collider[10]; + Collider[] colliders = new Collider[10]; private void UpdateMovement() { RaycastHit hitInfo; if (Input.IsKeyPressed(KeyCode.H) && - Physics.Raycast(m_CameraTransform.Transform.Translation + (m_CameraTransform.Forward * 5.0f), - m_CameraTransform.Forward, 20.0f, out hitInfo)) + Physics.Raycast(m_CameraTransform.Position + (m_CameraTransform.Transform.Forward * 5.0f), + m_CameraTransform.Transform.Forward, 20.0f, out hitInfo)) { FindEntityByID(hitInfo.EntityID).GetComponent().Mesh.GetMaterial(0).Set("u_AlbedoColor", new Vec3(1.0f ,0.0f, 0.0f)); } if (Input.IsKeyPressed(KeyCode.I)) { - // NOTE: The NonAlloc version of Overlap functions should be used when possible since it doesn't allocate a new array - // whenever you call it. The normal versions allocates a brand new array every time. + // NOTE: The NonAlloc version of Overlap functions should be used when possible since it doesn't allocate a new array + // whenever you call it. The normal versions allocates a brand new array every time. - int numColliders = Physics.OverlapBoxNonAlloc(m_Transform.Transform.Translation, new Vec3(1.0F), colliders); + int numColliders = Physics.OverlapBoxNonAlloc(m_Transform.Position, new Vec3(1.0F), colliders); Console.WriteLine("Colliders: {0}", numColliders); - // When using NonAlloc it's not safe to use a foreach loop since some of the colliders may not exist - for (int i = 0; i < numColliders; i++) - { - Console.WriteLine(colliders[i]); - } + // When using NonAlloc it's not safe to use a foreach loop since some of the colliders may not exist + for (int i = 0; i < numColliders; i++) + { + Console.WriteLine(colliders[i]); + } } if (Input.IsKeyPressed(KeyCode.W)) - m_RigidBody.AddForce(m_Transform.Forward * m_CurrentSpeed); + m_RigidBody.AddForce(m_Transform.Transform.Forward * m_CurrentSpeed); else if (Input.IsKeyPressed(KeyCode.S)) - m_RigidBody.AddForce(m_Transform.Forward * -m_CurrentSpeed); + m_RigidBody.AddForce(m_Transform.Transform.Forward * -m_CurrentSpeed); if (Input.IsKeyPressed(KeyCode.A)) - m_RigidBody.AddForce(m_Transform.Right * -m_CurrentSpeed); + m_RigidBody.AddForce(m_Transform.Transform.Right * -m_CurrentSpeed); else if (Input.IsKeyPressed(KeyCode.D)) - m_RigidBody.AddForce(m_Transform.Right * m_CurrentSpeed); + m_RigidBody.AddForce(m_Transform.Transform.Right * m_CurrentSpeed); if (Input.IsKeyPressed(KeyCode.Space) && m_Colliding) m_RigidBody.AddForce(Vec3.Up * JumpForce); @@ -119,19 +117,9 @@ namespace FPSExample } private void UpdateCameraTransform(){ - - // TODO: This workflow needs to be improved. (Will be fixed by object parenting) - - Mat4 cameraTransform = m_CameraTransform.Transform; - Vec3 cameraTranslation = cameraTransform.Translation; - Vec3 translation = m_Transform.Transform.Translation; - cameraTranslation.XZ = translation.XZ; - cameraTranslation.Y = translation.Y + 1.5F; - cameraTransform.Translation = cameraTranslation + m_CameraTransform.Forward * -Mathf.Clamp(Distance, 0, 10); - m_CameraTransform.Transform = cameraTransform; - - m_CameraTransform.Rotation = new Vec3(m_CameraRotationX, m_RotationY, 0.0f); - + Vec3 position = m_Transform.Position; + position.Y += 1.5f; + m_CameraTransform.Position = position; } } } \ No newline at end of file diff --git a/ExampleApp/Src/MapGenerator.cs b/ExampleApp/Src/MapGenerator.cs index 78c2400..f26c5cb 100644 --- a/ExampleApp/Src/MapGenerator.cs +++ b/ExampleApp/Src/MapGenerator.cs @@ -78,6 +78,7 @@ namespace Example void OnUpdate(float ts) { + /* Mat4 transform = GetTransform(); Vec3 translation = transform.Translation; translation.Y += ts * speed; @@ -91,6 +92,7 @@ namespace Example transform.Translation = translation; SetTransform(transform); + */ } diff --git a/ExampleApp/Src/PlayerCube.cs b/ExampleApp/Src/PlayerCube.cs index 392000f..7827a42 100644 --- a/ExampleApp/Src/PlayerCube.cs +++ b/ExampleApp/Src/PlayerCube.cs @@ -72,9 +72,11 @@ namespace Example if (Input.IsKeyPressed(KeyCode.R)) { + /* Mat4 transform = GetTransform(); transform.Translation = new Vec3(0.0f); SetTransform(transform); + */ } } diff --git a/ExampleApp/Src/PlayerSphere.cs b/ExampleApp/Src/PlayerSphere.cs index 67dae8c..d79089a 100644 --- a/ExampleApp/Src/PlayerSphere.cs +++ b/ExampleApp/Src/PlayerSphere.cs @@ -68,17 +68,17 @@ namespace Example } if (Input.IsKeyPressed(KeyCode.W)) - m_PhysicsBody.AddForce(m_Transform.Forward * movementForce); + m_PhysicsBody.AddForce(m_Transform.Transform.Forward * movementForce); else if (Input.IsKeyPressed(KeyCode.S)) - m_PhysicsBody.AddForce(m_Transform.Forward * -movementForce); + m_PhysicsBody.AddForce(m_Transform.Transform.Forward * -movementForce); if (Input.IsKeyPressed(KeyCode.D)) - m_PhysicsBody.AddForce(m_Transform.Right * movementForce); + m_PhysicsBody.AddForce(m_Transform.Transform.Right * movementForce); else if (Input.IsKeyPressed(KeyCode.A)) - m_PhysicsBody.AddForce(m_Transform.Right * -movementForce); + m_PhysicsBody.AddForce(m_Transform.Transform.Right * -movementForce); if (Colliding && Input.IsKeyPressed(KeyCode.Space)) - m_PhysicsBody.AddForce(m_Transform.Up * JumpForce); + m_PhysicsBody.AddForce(m_Transform.Transform.Up * JumpForce); if (Colliding) m_MeshMaterial.Set("u_AlbedoColor", new Vec3(1.0f, 0.0f, 0.0f)); @@ -91,9 +91,11 @@ namespace Example if (Input.IsKeyPressed(KeyCode.R)) { + /* Mat4 transform = GetTransform(); transform.Translation = new Vec3(0.0f); SetTransform(transform); + */ } } diff --git a/ExampleApp/Src/Script.cs b/ExampleApp/Src/Script.cs index 474233a..749a577 100644 --- a/ExampleApp/Src/Script.cs +++ b/ExampleApp/Src/Script.cs @@ -21,6 +21,7 @@ namespace Example public void OnUpdate(float ts) { + /* Rotation += ts; @@ -34,19 +35,10 @@ namespace Example translation.Z += Velocity.Z * ts; translation.Y -= SinkRate * ts; - /* - if (Input.IsKeyPressed(KeyCode.Up)) - translation.Y += speed; - else if (Input.IsKeyPressed(KeyCode.Down)) - translation.Y -= speed; - if (Input.IsKeyPressed(KeyCode.Right)) - translation.X += speed; - else if (Input.IsKeyPressed(KeyCode.Left)) - translation.X -= speed; - */ transform.Translation = translation; SetTransform(transform); + */ } } diff --git a/ExampleApp/Src/Sink.cs b/ExampleApp/Src/Sink.cs index a02fce6..aafb21a 100644 --- a/ExampleApp/Src/Sink.cs +++ b/ExampleApp/Src/Sink.cs @@ -14,6 +14,7 @@ namespace Example void OnUpdate(float ts) { + /* Mat4 transform = GetTransform(); Vec3 translation = transform.Translation; @@ -21,6 +22,7 @@ namespace Example transform.Translation = translation; SetTransform(transform); + */ } } diff --git a/Prism-ScriptCore/Src/Prism/Entity.cs b/Prism-ScriptCore/Src/Prism/Entity.cs index ff6af10..84a6033 100644 --- a/Prism-ScriptCore/Src/Prism/Entity.cs +++ b/Prism-ScriptCore/Src/Prism/Entity.cs @@ -61,18 +61,6 @@ namespace Prism return new Entity(entityID); } - public Mat4 GetTransform() - { - Mat4 mat4Instance; - GetTransform_Native(ID, out mat4Instance); - return mat4Instance; - } - - public void SetTransform(Mat4 transform) - { - SetTransform_Native(ID, ref transform); - } - public void AddCollision2DBeginCallback(Action callback) { m_Collision2DBeginCallbacks += callback; @@ -142,12 +130,10 @@ namespace Prism [MethodImpl(MethodImplOptions.InternalCall)] private static extern void CreateComponent_Native(ulong entityID, Type type); + [MethodImpl(MethodImplOptions.InternalCall)] private static extern bool HasComponent_Native(ulong entityID, Type type); - [MethodImpl(MethodImplOptions.InternalCall)] - private static extern void GetTransform_Native(ulong entityID, out Mat4 matrix); - [MethodImpl(MethodImplOptions.InternalCall)] - private static extern void SetTransform_Native(ulong entityID, ref Mat4 matrix); + [MethodImpl(MethodImplOptions.InternalCall)] private static extern ulong FindEntityByTag_Native(string tag); } diff --git a/Prism-ScriptCore/Src/Prism/Math/Mathf.cs b/Prism-ScriptCore/Src/Prism/Math/Mathf.cs index 5062a03..250ccca 100644 --- a/Prism-ScriptCore/Src/Prism/Math/Mathf.cs +++ b/Prism-ScriptCore/Src/Prism/Math/Mathf.cs @@ -3,12 +3,13 @@ namespace Prism { public static class Mathf { + public const float DegreeToRadians = (float)Math.PI * 2.0f / 360.0f; + public const float RadiansToDegrees = 360.0f / ((float)Math.PI * 2.0f); public static float Clamp(float value, float min, float max) { - if (value < min) - return min; - if (value > max) - return max; + if (value < min) return min; + if (value > max) return max; + return value; } } diff --git a/Prism-ScriptCore/Src/Prism/Math/Queternion.cs b/Prism-ScriptCore/Src/Prism/Math/Queternion.cs deleted file mode 100644 index b96d161..0000000 --- a/Prism-ScriptCore/Src/Prism/Math/Queternion.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Runtime.InteropServices; - -namespace Prism -{ - [StructLayout(LayoutKind.Sequential)] - public struct Quaternion - { - public float X; - public float Y; - public float Z; - public float W; - - public Quaternion(float x, float y, float z, float w) - { - X = x; - Y = y; - Z = z; - W = w; - } - - public Quaternion(float w, Vec3 xyz) - { - X = xyz.X; - Y = xyz.Y; - Z = xyz.Z; - W = w; - } - - public Quaternion(Vec3 eulerAngles) - { - Vec3 c = Vec3.Cos(eulerAngles * 0.5F); - Vec3 s = Vec3.Sin(eulerAngles * 0.5F); - - W = c.X * c.Y * c.Z + s.X * s.Y * s.Z; - X = s.X * c.Y * c.Z - c.X * s.Y * s.Z; - Y = c.X * s.Y * c.Z + s.X * c.Y * s.Z; - Z = c.X * c.Y * s.Z - s.X * s.Y * c.Z; - } - - public static Quaternion AngleAxis(float angle, Vec3 axis) - { - float s = (float)Math.Sin(angle * 0.5F); - return new Quaternion(s, axis * s); - } - - public static Quaternion operator*(Quaternion a, Quaternion b) - { - float w = a.W * b.W - a.X * b.X - a.Y * b.Y - a.Z * b.Z; - float x = a.W * b.X + a.X * b.W + a.Y * b.Z - a.Z * b.Y; - float y = a.W * b.Y + a.Y * b.W + a.Z * b.X - a.X * b.Z; - float z = a.W * b.Z + a.Z * b.W + a.X * b.Y - a.Y * b.X; - return new Quaternion(x, y, z, w); - } - } -} diff --git a/Prism-ScriptCore/Src/Prism/Math/Transform.cs b/Prism-ScriptCore/Src/Prism/Math/Transform.cs new file mode 100644 index 0000000..8c8a834 --- /dev/null +++ b/Prism-ScriptCore/Src/Prism/Math/Transform.cs @@ -0,0 +1,16 @@ +using System.Runtime.InteropServices; + +namespace Prism +{ + [StructLayout(LayoutKind.Sequential)] + public struct Transform + { + public Vec3 Position; + public Vec3 Rotation; + public Vec3 Scale; + + public Vec3 Up; + public Vec3 Right; + public Vec3 Forward; + } +} \ No newline at end of file diff --git a/Prism-ScriptCore/Src/Prism/Math/Vec3.cs b/Prism-ScriptCore/Src/Prism/Math/Vec3.cs index e1100f1..0102fcc 100644 --- a/Prism-ScriptCore/Src/Prism/Math/Vec3.cs +++ b/Prism-ScriptCore/Src/Prism/Math/Vec3.cs @@ -20,11 +20,24 @@ namespace Prism X = Y = Z = scalar; } - public Vec3(Vec2 vec) { - X = vec.X; - Y = vec.Y; + public Vec3(Vec2 vec2) { + X = vec2.X; + Y = vec2.Y; Z = 0.0f; } + + public Vec3(Vec2 vec2, float z) { + X = vec2.X; + Y = vec2.Y; + Z = z; + } + + public Vec3(float x, Vec2 vec2) + { + X = x; + Y = vec2.X; + Z = vec2.Y; + } public Vec3(float x, float y, float z) { @@ -32,6 +45,7 @@ namespace Prism Y = y; Z = z; } + public Vec3(Vec4 vec) { X = vec.X; diff --git a/Prism-ScriptCore/Src/Prism/Scene/Component.cs b/Prism-ScriptCore/Src/Prism/Scene/Component.cs index 4dfbb76..b90f844 100644 --- a/Prism-ScriptCore/Src/Prism/Scene/Component.cs +++ b/Prism-ScriptCore/Src/Prism/Scene/Component.cs @@ -1,4 +1,5 @@ using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace Prism { @@ -31,77 +32,58 @@ namespace Prism } public class TransformComponent : Component - { - public Mat4 Transform + { + private Transform m_Transform; + + public Transform Transform { get { return m_Transform; } } + + public Vec3 Position { get { - Mat4 result; - GetTransform_Native(Entity.ID, out result); - return result; + GetTransform_Native(Entity.ID, out m_Transform); + return m_Transform.Position; } set { - SetTransform_Native(Entity.ID, ref value); + m_Transform.Position = value; + SetTransform_Native(Entity.ID, ref m_Transform); } } - + public Vec3 Rotation { get { - GetRotation_Native(Entity.ID, out Vec3 rotation); - return rotation; + GetTransform_Native(Entity.ID, out m_Transform); + return m_Transform.Rotation; } set { - SetRotation_Native(Entity.ID, ref value); + m_Transform.Rotation = value; + SetTransform_Native(Entity.ID, ref m_Transform); } } - public Vec3 Forward + public Vec3 Scale { get { - GetRelativeDirection_Native(Entity.ID, out Vec3 result, ref Vec3.Forward); - return result; + GetTransform_Native(Entity.ID, out m_Transform); + return m_Transform.Scale; } - } - - public Vec3 Right - { - get + set { - GetRelativeDirection_Native(Entity.ID, out Vec3 result, ref Vec3.Right); - return result; + m_Transform.Scale = value; + SetTransform_Native(Entity.ID, ref m_Transform); } } - public Vec3 Up - { - get - { - GetRelativeDirection_Native(Entity.ID, out Vec3 result, ref Vec3.Up); - return result; - } - } - + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern void GetTransform_Native(ulong entityID, out Transform result); [MethodImpl(MethodImplOptions.InternalCall)] - public static extern void GetTransform_Native(ulong entityID, out Mat4 result); - - [MethodImpl(MethodImplOptions.InternalCall)] - public static extern void SetTransform_Native(ulong entityID, ref Mat4 result); - - [MethodImpl(MethodImplOptions.InternalCall)] - public static extern void GetRelativeDirection_Native(ulong entityID, out Vec3 result, ref Vec3 direction); - - [MethodImpl(MethodImplOptions.InternalCall)] - public static extern void GetRotation_Native(ulong entityID, out Vec3 rotation); - - [MethodImpl(MethodImplOptions.InternalCall)] - public static extern void SetRotation_Native(ulong entityID, ref Vec3 rotation); - + internal static extern void SetTransform_Native(ulong entityID, ref Transform result); } diff --git a/Prism/src/Prism/Core/Math/Transform.cpp b/Prism/src/Prism/Core/Math/Transform.cpp new file mode 100644 index 0000000..6934982 --- /dev/null +++ b/Prism/src/Prism/Core/Math/Transform.cpp @@ -0,0 +1,39 @@ +// +// Created by sfd on 25-12-29. +// + +#include "Transform.h" + +namespace Prism +{ + Transform::Transform() + : m_Translation(glm::vec3(0.0f)), m_Rotation(glm::vec3(0.0f)), m_Scale(glm::vec3(1.0f)) + { + RecalculateMatrix(); + } + + Transform::Transform(const glm::vec3& translation, const glm::vec3& rotation, const glm::vec3& scale) + : m_Translation(translation), m_Rotation(rotation), m_Scale(scale) + { + RecalculateMatrix(); + } + + const glm::mat4& Transform::GetMatrix() + { + if (m_HasChanged) RecalculateMatrix(); + return m_Matrix; + } + + void Transform::RecalculateMatrix() + { + const auto rotation = glm::quat(glm::radians(m_Rotation)); + + m_Up = glm::normalize(glm::rotate(rotation, glm::vec3(0.0f, 1.0f, 0.0f))); + m_Right = glm::normalize(glm::rotate(rotation, glm::vec3(1.0f, 0.0f, 0.0f))); + m_Forward = glm::normalize(glm::rotate(rotation, glm::vec3(0.0f, 0.0f, -1.0f))); + + m_Matrix = glm::translate(glm::mat4(1.0f), m_Translation) + * glm::toMat4(rotation) + * glm::scale(glm::mat4(1.0f), m_Scale); + } +} diff --git a/Prism/src/Prism/Core/Math/Transform.h b/Prism/src/Prism/Core/Math/Transform.h new file mode 100644 index 0000000..59f41d3 --- /dev/null +++ b/Prism/src/Prism/Core/Math/Transform.h @@ -0,0 +1,60 @@ +// +// Created by sfd on 25-12-29. +// + +#ifndef TRANSFORM_H +#define TRANSFORM_H + +#define GLM_ENABLE_EXPERIMENTAL +#include +#include +#include +#include + +#include "glm/gtc/type_ptr.hpp" + + +namespace Prism +{ + class PRISM_API Transform + { + public: + Transform(); + Transform(const glm::vec3& translation, const glm::vec3& rotation = glm::vec3(0.0f), const glm::vec3& scale = glm::vec3(1.0f)); + + void Translate(const glm::vec3& translation) { m_Translation += translation; m_HasChanged = true;} + void Rotate(const glm::vec3& rotation) { m_Rotation += rotation; m_HasChanged = true;} + + void SetTranslation(const glm::vec3& position) { m_Translation = position; m_HasChanged = true;} + void SetRotation(const glm::vec3& rotation) { m_Rotation = rotation; m_HasChanged = true;} + void SetRotation(const glm::quat& rotation) { m_Rotation = glm::degrees(glm::eulerAngles(rotation)); m_HasChanged = true;} + void SetScale(const glm::vec3& scale) { m_Scale = scale; m_HasChanged = true;} + + const glm::vec3& GetTranslation() const { return m_Translation; } + const glm::vec3& GetRotation() const { return m_Rotation; } + const glm::vec3& GetScale() const { return m_Scale; } + + const glm::vec3& GetUpDirection() const { return m_Up; } + const glm::vec3& GetRightDirection() const { return m_Right; } + const glm::vec3& GetForwardDirection() const { return m_Forward; } + + const glm::mat4& GetMatrix(); + float* Data() { return glm::value_ptr(m_Matrix); } + + private: + void RecalculateMatrix(); + + private: + glm::vec3 m_Translation; + glm::vec3 m_Rotation; + glm::vec3 m_Scale; + + glm::vec3 m_Forward, m_Right, m_Up; + + glm::mat4 m_Matrix = glm::mat4(1.0f); + bool m_HasChanged = false; + }; +} + + +#endif //TRANSFORM_H diff --git a/Prism/src/Prism/Editor/SceneHierachyPanel.cpp b/Prism/src/Prism/Editor/SceneHierachyPanel.cpp index 5b5e25b..c782014 100644 --- a/Prism/src/Prism/Editor/SceneHierachyPanel.cpp +++ b/Prism/src/Prism/Editor/SceneHierachyPanel.cpp @@ -644,23 +644,21 @@ namespace Prism if (entity.HasComponent()) { - auto& tc = entity.GetComponent(); + Transform& transform = entity.GetComponent(); if (ImGui::TreeNodeEx((void*)((uint32_t)entity | typeid(TransformComponent).hash_code()), ImGuiTreeNodeFlags_DefaultOpen, "Transform")) { - auto [translation, rotationQuat, scale] = GetTransformDecomposition(tc); - glm::vec3 rotation = glm::degrees(glm::eulerAngles(rotationQuat)); + glm::vec3 translation = transform.GetTranslation(); + glm::vec3 rotation = transform.GetRotation(); + glm::vec3 scale = transform.GetScale(); ImGui::Columns(2); ImGui::Text("Translation"); ImGui::NextColumn(); ImGui::PushItemWidth(-1); - bool updateTransform = false; - if (ImGui::DragFloat3("##translation", glm::value_ptr(translation), 0.25f)) { - // tc.Transform[3] = glm::vec4(translation, 1.0f); - updateTransform = true; + transform.SetTranslation(translation); } ImGui::PopItemWidth(); @@ -672,8 +670,7 @@ namespace Prism if (ImGui::DragFloat3("##rotation", glm::value_ptr(rotation), 0.25f)) { - updateTransform = true; - // tc.Transform[3] = glm::vec4(translation, 1.0f); + transform.SetRotation(rotation); } ImGui::PopItemWidth(); @@ -689,21 +686,13 @@ namespace Prism if (scale.y == 0) scale.y = 0.01f; if (scale.z == 0) scale.z = 0.01f; - updateTransform = true; + transform.SetScale(scale); } ImGui::PopItemWidth(); ImGui::NextColumn(); ImGui::Columns(1); - - - if (updateTransform) - { - tc.Transform = glm::translate(glm::mat4(1.0f), translation) * - glm::toMat4(glm::quat(glm::radians(rotation))) * - glm::scale(glm::mat4(1.0f), scale); - } ImGui::TreePop(); } ImGui::Separator(); diff --git a/Prism/src/Prism/Physics/Physics3D.cpp b/Prism/src/Prism/Physics/Physics3D.cpp index 30d7050..3be2616 100644 --- a/Prism/src/Prism/Physics/Physics3D.cpp +++ b/Prism/src/Prism/Physics/Physics3D.cpp @@ -29,15 +29,6 @@ namespace Prism - static std::tuple GetTransformDecomposition(const glm::mat4& transform) - { - glm::vec3 scale, translation, skew; - glm::vec4 perspective; - glm::quat orientation; - glm::decompose(transform, scale, orientation, translation, skew, perspective); - - return { translation, orientation, scale }; - } void Physics3D::Init() { @@ -96,7 +87,7 @@ namespace Prism RigidBodyComponent& rigidbody = e.GetComponent(); - physx::PxRigidActor* actor = PhysicsWrappers::CreateActor(rigidbody, e.Transform()); + physx::PxRigidActor* actor = PhysicsWrappers::CreateActor(rigidbody, e.Transformation()); if (rigidbody.BodyType == RigidBodyComponent::Type::Dynamic) s_SimulatedEntities.push_back(e); @@ -109,8 +100,8 @@ namespace Prism const physx::PxMaterial* material = PhysicsWrappers::CreateMaterial(e.GetComponent()); - const auto& transform = e.Transform(); - auto [translation, rotation, scale] = GetTransformDecomposition(transform); + const auto& transform = e.Transformation(); + const auto& scale = transform.GetScale(); // Add all colliders @@ -163,13 +154,13 @@ namespace Prism for (Entity& e : s_SimulatedEntities) { - auto& transform = e.Transform(); - // TODO: Come up with a better solution for scale - auto [position, rotation, scale] = GetTransformDecomposition(transform); + auto& transform = e.Transformation(); const auto& rb = e.GetComponent(); const auto actor = static_cast(rb.RuntimeActor); - transform = FromPhysXTransform(actor->getGlobalPose()) * glm::scale(glm::mat4(1.0F), scale); + physx::PxTransform actorPose = actor->getGlobalPose(); + transform.SetTranslation(FromPhysXVector(actorPose.p)); + transform.SetRotation(FromPhysXQuat(actorPose.q)); } } diff --git a/Prism/src/Prism/Physics/PhysicsLayer.cpp b/Prism/src/Prism/Physics/PhysicsLayer.cpp index a0ad0a3..0ed59ae 100644 --- a/Prism/src/Prism/Physics/PhysicsLayer.cpp +++ b/Prism/src/Prism/Physics/PhysicsLayer.cpp @@ -24,7 +24,7 @@ namespace Prism uint32_t PhysicsLayerManager::AddLayer(const std::string& name, bool setCollisions) { const uint32_t layerId = GetNextLayerID(); - const PhysicsLayer layer = { layerId, name, static_cast(BIT(layerId)) }; + const PhysicsLayer layer = { layerId, name, static_cast(BIT(layerId)), BIT(layerId)}; s_Layers.push_back(layer); if (setCollisions) diff --git a/Prism/src/Prism/Physics/PhysicsUtils.cpp b/Prism/src/Prism/Physics/PhysicsUtils.cpp index 0f7a5d6..ba0d668 100644 --- a/Prism/src/Prism/Physics/PhysicsUtils.cpp +++ b/Prism/src/Prism/Physics/PhysicsUtils.cpp @@ -5,12 +5,21 @@ #include "PhysicsUtils.h" #define GLM_ENABLE_EXPERIMENTAL +#include + #include "glm/gtx/quaternion.hpp" #include "Prism/Script/ScriptEngine.h" namespace Prism { + physx::PxTransform ToPhysXTransform(const Transform& transform) + { + const physx::PxQuat r = ToPhysXQuat(glm::normalize(glm::quat(glm::radians(transform.GetRotation())))); + const physx::PxVec3 p = ToPhysXVector(transform.GetTranslation()); + return physx::PxTransform{p, r}; + } + physx::PxTransform ToPhysXTransform(const glm::mat4& matrix) { const physx::PxQuat r = ToPhysXQuat(glm::normalize(glm::toQuat(matrix))); diff --git a/Prism/src/Prism/Physics/PhysicsUtils.h b/Prism/src/Prism/Physics/PhysicsUtils.h index cc42ad5..a08d916 100644 --- a/Prism/src/Prism/Physics/PhysicsUtils.h +++ b/Prism/src/Prism/Physics/PhysicsUtils.h @@ -9,10 +9,12 @@ #include #include "glm/glm.hpp" +#include "Prism/Core/Math/Transform.h" namespace Prism { + physx::PxTransform ToPhysXTransform(const Transform& matrix); physx::PxTransform ToPhysXTransform(const glm::mat4& matrix); physx::PxMat44 ToPhysXMatrix(const glm::mat4& matrix); physx::PxVec3 ToPhysXVector(const glm::vec3& vector); @@ -28,8 +30,6 @@ namespace Prism physx::PxFilterFlags PrismFilterShader(physx::PxFilterObjectAttributes attributes0, physx::PxFilterData filterData0, physx::PxFilterObjectAttributes attributes1, physx::PxFilterData filterData1, physx::PxPairFlags& pairFlags, const void* constantBlock, physx::PxU32 constantBlockSize); - - } diff --git a/Prism/src/Prism/Physics/PhysicsWrappers.cpp b/Prism/src/Prism/Physics/PhysicsWrappers.cpp index d27494c..5753b82 100644 --- a/Prism/src/Prism/Physics/PhysicsWrappers.cpp +++ b/Prism/src/Prism/Physics/PhysicsWrappers.cpp @@ -177,7 +177,7 @@ namespace Prism return s_Physics->createScene(sceneDesc); } - physx::PxRigidActor* PhysicsWrappers::CreateActor(const RigidBodyComponent& rigidbody, const glm::mat4& transform) + physx::PxRigidActor* PhysicsWrappers::CreateActor(const RigidBodyComponent& rigidbody, const Transform& transform) { physx::PxRigidActor* actor = nullptr; diff --git a/Prism/src/Prism/Physics/PhysicsWrappers.h b/Prism/src/Prism/Physics/PhysicsWrappers.h index ee1bae6..b1dfe95 100644 --- a/Prism/src/Prism/Physics/PhysicsWrappers.h +++ b/Prism/src/Prism/Physics/PhysicsWrappers.h @@ -37,7 +37,7 @@ namespace Prism { public: static physx::PxScene* CreateScene(); - static physx::PxRigidActor* CreateActor(const RigidBodyComponent& rigidbody, const glm::mat4& transform); + static physx::PxRigidActor* CreateActor(const RigidBodyComponent& rigidbody, const Transform& transform); static void SetCollisionFilters(const physx::PxRigidActor& actor, uint32_t physicsLayer); static void AddBoxCollider(physx::PxRigidActor& actor, const physx::PxMaterial& material, const BoxColliderComponent& collider, const glm::vec3& scale = glm::vec3(0.0f)); diff --git a/Prism/src/Prism/Scene/Components.h b/Prism/src/Prism/Scene/Components.h index 2b8166b..e9b333b 100644 --- a/Prism/src/Prism/Scene/Components.h +++ b/Prism/src/Prism/Scene/Components.h @@ -12,6 +12,7 @@ #include "glm/glm.hpp" #include "Prism/Core/Ref.h" #include "Prism/Core/UUID.h" +#include "Prism/Core/Math/Transform.h" #include "Prism/Renderer/Mesh.h" namespace Prism @@ -37,15 +38,15 @@ namespace Prism struct TransformComponent { - glm::mat4 Transform; + Transform Transformation; TransformComponent() = default; TransformComponent(const TransformComponent& other) = default; - TransformComponent(const glm::mat4& transform) - : Transform(transform) {} + TransformComponent(const Transform& transform) + : Transformation(transform) {} - operator glm::mat4& () { return Transform; } - operator const glm::mat4& () const { return Transform; } + operator Transform& () { return Transformation; } + operator const Transform& () const { return Transformation; } }; diff --git a/Prism/src/Prism/Scene/Entity.h b/Prism/src/Prism/Scene/Entity.h index 71592e4..739dbc4 100644 --- a/Prism/src/Prism/Scene/Entity.h +++ b/Prism/src/Prism/Scene/Entity.h @@ -50,8 +50,8 @@ namespace Prism } const std::string& Tag() const { return m_Scene->m_Registry.get(m_EntityHandle).Tag; } - glm::mat4& Transform() { return m_Scene->m_Registry.get(m_EntityHandle); } - const glm::mat4& Transform() const { return m_Scene->m_Registry.get(m_EntityHandle); } + Transform& Transformation() { return m_Scene->m_Registry.get(m_EntityHandle); } + const Transform& Transformation() const { return m_Scene->m_Registry.get(m_EntityHandle); } operator uint32_t () const { return (uint32_t)m_EntityHandle; } operator entt::entity() const{ return m_EntityHandle; } diff --git a/Prism/src/Prism/Scene/Scene.cpp b/Prism/src/Prism/Scene/Scene.cpp index 640b763..e0c7302 100644 --- a/Prism/src/Prism/Scene/Scene.cpp +++ b/Prism/src/Prism/Scene/Scene.cpp @@ -106,16 +106,6 @@ namespace Prism ScriptEngine::OnScriptComponentDestroyed(sceneID, entityID); } - static std::tuple GetTransformDecomposition(const glm::mat4& transform) - { - glm::vec3 scale, translation, skew; - glm::vec4 perspective; - glm::quat orientation; - glm::decompose(transform, scale, orientation, translation, skew, perspective); - - return { translation, orientation, scale }; - } - Environment Environment::Load(const std::string& filepath) { auto [radiance, irradiance] = SceneRenderer::CreateEnvironmentMap(filepath); @@ -198,18 +188,17 @@ namespace Prism for (const auto entity : view) { Entity e = { entity, this }; - auto& transform = e.Transform(); + auto& transform = e.Transformation(); const auto& rb2d = e.GetComponent(); const auto& position = b2Body_GetPosition(rb2d.RuntimeBodyID); - auto [translation, rotationQuat, scale] = GetTransformDecomposition(transform); - glm::vec3 rotation = glm::eulerAngles(rotationQuat); + const auto& rotation = transform.GetRotation(); float angle = b2Rot_GetAngle(b2Body_GetRotation(rb2d.RuntimeBodyID)); - transform = glm::translate(glm::mat4(1.0f), { position.x, position.y, transform[3].z }) * - glm::toMat4(glm::quat({ rotation.x, rotation.y, angle })) * - glm::scale(glm::mat4(1.0f), scale); + transform.SetRotation({rotation.x, rotation.y, angle}); + transform.SetScale(transform.GetScale()); + transform.SetTranslation({position.x, position.y, transform.GetTranslation().z}); } } @@ -225,7 +214,7 @@ namespace Prism if (!cameraEntity) return; - glm::mat4 cameraViewMatrix = glm::inverse(cameraEntity.GetComponent().Transform); + const glm::mat4 cameraViewMatrix = glm::inverse(cameraEntity.Transformation().GetMatrix()); PM_CORE_ASSERT(cameraEntity, "Scene does not contain any cameras!"); SceneCamera& camera = cameraEntity.GetComponent(); camera.SetViewportSize(m_ViewportWidth, m_ViewportHeight); @@ -242,7 +231,7 @@ namespace Prism meshComponent.Mesh->OnUpdate(ts); // TODO: Should we render (logically) - SceneRenderer::SubmitMesh(meshComponent, transformComponent, nullptr); + SceneRenderer::SubmitMesh(meshComponent, transformComponent.Transformation.GetMatrix(), nullptr); } } SceneRenderer::EndScene(); @@ -284,7 +273,7 @@ namespace Prism meshComponent.Mesh->OnUpdate(ts); // TODO: Should we render (logically) - SceneRenderer::SubmitMesh(meshComponent, transformComponent); + SceneRenderer::SubmitMesh(meshComponent, transformComponent.Transformation.GetMatrix()); } } @@ -296,7 +285,7 @@ namespace Prism auto& collider = e.GetComponent(); if (m_SelectedEntity == entity) - SceneRenderer::SubmitColliderMesh(collider, e.GetComponent()); + SceneRenderer::SubmitColliderMesh(collider, e.GetComponent().Transformation.GetMatrix()); } } @@ -308,7 +297,7 @@ namespace Prism auto& collider = e.GetComponent(); if (m_SelectedEntity == entity) - SceneRenderer::SubmitColliderMesh(collider, e.GetComponent()); + SceneRenderer::SubmitColliderMesh(collider, e.GetComponent().Transformation.GetMatrix()); } } @@ -320,7 +309,7 @@ namespace Prism auto& collider = e.GetComponent(); if (m_SelectedEntity == entity) - SceneRenderer::SubmitColliderMesh(collider, e.GetComponent()); + SceneRenderer::SubmitColliderMesh(collider, e.GetComponent().Transformation.GetMatrix()); } } @@ -333,7 +322,7 @@ namespace Prism if (m_SelectedEntity == entity) { - SceneRenderer::SubmitColliderMesh(collider, e.GetComponent()); + SceneRenderer::SubmitColliderMesh(collider, e.GetComponent().Transformation.GetMatrix()); } } } @@ -388,8 +377,9 @@ namespace Prism for (auto entity : view) { Entity e = { entity, this }; - UUID entityID = e.GetComponent().ID; - auto& transform = e.Transform(); + auto& transform = e.Transformation(); + const auto& position = transform.GetTranslation(); + const auto& rotation = transform.GetRotation(); auto& rigidBody2D = m_Registry.get(entity); b2BodyDef bodyDef = b2DefaultBodyDef(); @@ -399,10 +389,9 @@ namespace Prism bodyDef.type = b2_dynamicBody; else if (rigidBody2D.BodyType == RigidBody2DComponent::Type::Kinematic) bodyDef.type = b2_kinematicBody; - bodyDef.position = {transform[3].x, transform[3].y}; + bodyDef.position = {position.x, position.y}; - auto [translation, rotationQuat, scale] = GetTransformDecomposition(transform); - float rotationZ = glm::eulerAngles(rotationQuat).z; + float rotationZ = rotation.z; bodyDef.rotation = b2Rot{cos(rotationZ), sin(rotationZ)}; // box2D fixRotation renamed to motionlocks @@ -422,7 +411,6 @@ namespace Prism for (auto entity : view) { Entity e = { entity, this }; - auto& transform = e.Transform(); auto& boxCollider2D = m_Registry.get(entity); if (e.HasComponent()) @@ -537,7 +525,7 @@ namespace Prism auto& idComponent = entity.AddComponent(); idComponent.ID = {}; - entity.AddComponent(glm::mat4(1.0f)); + entity.AddComponent(Transform()); if (!name.empty()) entity.AddComponent(name); @@ -551,7 +539,7 @@ namespace Prism auto& idComponent = entity.AddComponent(); idComponent.ID = uuid; - entity.AddComponent(glm::mat4(1.0f)); + entity.AddComponent(Transform()); if (!name.empty()) entity.AddComponent(name); diff --git a/Prism/src/Prism/Scene/SceneSerializer.cpp b/Prism/src/Prism/Scene/SceneSerializer.cpp index 50e09bb..98c59e6 100644 --- a/Prism/src/Prism/Scene/SceneSerializer.cpp +++ b/Prism/src/Prism/Scene/SceneSerializer.cpp @@ -162,16 +162,6 @@ namespace Prism { } - static std::tuple GetTransformDecomposition(const glm::mat4& transform) - { - glm::vec3 scale, translation, skew; - glm::vec4 perspective; - glm::quat orientation; - glm::decompose(transform, scale, orientation, translation, skew, perspective); - - return { translation, orientation, scale }; - } - static void SerializeEntity(YAML::Emitter& out, Entity entity) { UUID uuid = entity.GetComponent().ID; @@ -195,11 +185,10 @@ namespace Prism out << YAML::Key << "TransformComponent"; out << YAML::BeginMap; // TransformComponent - auto& transform = entity.GetComponent().Transform; - auto[pos, rot, scale] = GetTransformDecomposition(transform); - out << YAML::Key << "Position" << YAML::Value << pos; - out << YAML::Key << "Rotation" << YAML::Value << rot; // Quat rotation - out << YAML::Key << "Scale" << YAML::Value << scale; + auto& transform = entity.GetComponent().Transformation; + out << YAML::Key << "Position" << YAML::Value << transform.GetTranslation(); + out << YAML::Key << "Rotation" << YAML::Value << transform.GetRotation(); + out << YAML::Key << "Scale" << YAML::Value << transform.GetScale(); out << YAML::EndMap; // TransformComponent } @@ -542,17 +531,19 @@ namespace Prism if (transformComponent) { // Entities always have transforms - auto& transform = deserializedEntity.GetComponent().Transform; - glm::vec3 translation = transformComponent["Position"].as(); - glm::quat rotation = transformComponent["Rotation"].as(); - glm::vec3 scale = transformComponent["Scale"].as(); + auto& transform = deserializedEntity.GetComponent().Transformation; - transform = glm::translate(glm::mat4(1.0f), translation) * - glm::toMat4(rotation) * glm::scale(glm::mat4(1.0f), scale); + auto translation = transformComponent["Position"].as(); + auto rotation = transformComponent["Rotation"].as(); + auto scale = transformComponent["Scale"].as(); - PM_CORE_INFO(" Entity Transform:"); + transform.SetTranslation(translation); + transform.SetRotation(rotation); + transform.SetScale(scale); + + PM_CORE_INFO("Entity Transform:"); PM_CORE_INFO(" Translation: {0}, {1}, {2}", translation.x, translation.y, translation.z); - PM_CORE_INFO(" Rotation: {0}, {1}, {2}, {3}", rotation.w, rotation.x, rotation.y, rotation.z); + PM_CORE_INFO(" Rotation: {0}, {1}, {2}", rotation.x, rotation.y, rotation.z); PM_CORE_INFO(" Scale: {0}, {1}, {2}", scale.x, scale.y, scale.z); } diff --git a/Prism/src/Prism/Script/ScriptEngine.cpp b/Prism/src/Prism/Script/ScriptEngine.cpp index ce02e38..f50f56d 100644 --- a/Prism/src/Prism/Script/ScriptEngine.cpp +++ b/Prism/src/Prism/Script/ScriptEngine.cpp @@ -162,10 +162,10 @@ namespace Prism if (s_EnableMonoPDBDebug) { - const char* argv[2] + const char* argv[] { - "--debugger-agent=transport=dt_socket,address=127.0.0.1:2550,server=y,suspend=n,loglevel=3,logfile=MonoDebugger.log", - "--soft-breakpoints" + "--debugger-agent=transport=dt_socket,address=127.0.0.1:2550,server=y,suspend=n", + "--soft-breakpoints", }; mono_jit_parse_options(2, (char**)argv); diff --git a/Prism/src/Prism/Script/ScriptEngineRegistry.cpp b/Prism/src/Prism/Script/ScriptEngineRegistry.cpp index f0c7d56..047ca62 100644 --- a/Prism/src/Prism/Script/ScriptEngineRegistry.cpp +++ b/Prism/src/Prism/Script/ScriptEngineRegistry.cpp @@ -60,15 +60,23 @@ namespace Prism mono_add_internal_call("Prism.Physics::OverlapSphereNonAlloc_Native", (const void*)Prism::Script::Prism_Physics_OverlapSphereNonAlloc); + /* mono_add_internal_call("Prism.Entity::GetTransform_Native",(const void*)Prism::Script::Prism_Entity_GetTransform); mono_add_internal_call("Prism.Entity::SetTransform_Native",(const void*)Prism::Script::Prism_Entity_SetTransform); mono_add_internal_call("Prism.TransformComponent::GetRelativeDirection_Native", (const void*)Prism::Script::Prism_TransformComponent_GetRelativeDirection); + */ + + mono_add_internal_call("Prism.TransformComponent::GetTransform_Native", (const void*)Prism::Script::Prism_TransformComponent_GetTransform); + mono_add_internal_call("Prism.TransformComponent::SetTransform_Native", (const void*)Prism::Script::Prism_TransformComponent_SetTransform); + /* mono_add_internal_call("Prism.TransformComponent::GetTransform_Native", (const void*)Prism::Script::Prism_Entity_GetTransform); mono_add_internal_call("Prism.TransformComponent::SetTransform_Native", (const void*)Prism::Script::Prism_Entity_SetTransform); mono_add_internal_call("Prism.TransformComponent::GetRotation_Native", (const void*)Prism::Script::Prism_TransformComponent_GetRotation); mono_add_internal_call("Prism.TransformComponent::SetRotation_Native", (const void*)Prism::Script::Prism_TransformComponent_SetRotation); + */ + /* mono_add_internal_call("Prism.Entity::GetForwardDirection_Native", Prism::Script::Prism_Entity_GetForwardDirection); diff --git a/Prism/src/Prism/Script/ScriptWrappers.cpp b/Prism/src/Prism/Script/ScriptWrappers.cpp index cfcfff0..01d182b 100644 --- a/Prism/src/Prism/Script/ScriptWrappers.cpp +++ b/Prism/src/Prism/Script/ScriptWrappers.cpp @@ -41,18 +41,6 @@ namespace Prism { namespace Script { SpriteRenderer = 4 }; - static std::tuple GetTransformDecomposition(const glm::mat4& transform) - { - glm::vec3 scale, translation, skew; - glm::vec4 perspective; - glm::quat orientation; - glm::decompose(transform, scale, orientation, translation, skew, perspective); - - return { translation, orientation, scale }; - } - - - //////////////////////////////////////////////////////////////// // Math //////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// @@ -279,6 +267,7 @@ namespace Prism { namespace Script { // Entity // //////////////////////////////////////////////////////////////// + /* void Prism_Entity_GetTransform(const uint64_t entityID, glm::mat4* outTransform) { Ref scene = ScriptEngine::GetCurrentSceneContext(); @@ -288,7 +277,7 @@ namespace Prism { namespace Script { Entity entity = entityMap.at(entityID); auto& transformComponent = entity.GetComponent(); - memcpy(outTransform, glm::value_ptr(transformComponent.Transform), sizeof(glm::mat4)); + memcpy(outTransform, glm::value_ptr(transformComponent.Transformation), sizeof(glm::mat4)); } void Prism_Entity_SetTransform(const uint64_t entityID, const glm::mat4* inTransform) @@ -300,8 +289,9 @@ namespace Prism { namespace Script { Entity entity = entityMap.at(entityID); auto& transformComponent = entity.GetComponent(); - memcpy(glm::value_ptr(transformComponent.Transform), inTransform, sizeof(glm::mat4)); + memcpy(glm::value_ptr(transformComponent.Transformation), inTransform, sizeof(glm::mat4)); } + */ void Prism_Entity_CreateComponent(const uint64_t entityID, void* type) { @@ -330,6 +320,7 @@ namespace Prism { namespace Script { uint64_t Prism_Entity_FindEntityByTag(MonoString* tag) { + size_t a = sizeof(ScriptTransform); Ref scene = ScriptEngine::GetCurrentSceneContext(); PM_CORE_ASSERT(scene, "No active scene!"); @@ -340,6 +331,7 @@ namespace Prism { namespace Script { return 0; } + /* void Prism_TransformComponent_GetRelativeDirection(const uint64_t entityID, glm::vec3* outDirection, const glm::vec3* inAbsoluteDirection) { Ref scene = ScriptEngine::GetCurrentSceneContext(); @@ -350,7 +342,7 @@ namespace Prism { namespace Script { Entity entity = entityMap.at(entityID); const auto& transformComponent = entity.GetComponent(); - auto [position, rotation, scale] = GetTransformDecomposition(transformComponent.Transform); + auto [position, rotation, scale] = GetTransformDecomposition(transformComponent.Transformation); *outDirection = glm::rotate(rotation, *inAbsoluteDirection); } @@ -364,7 +356,7 @@ namespace Prism { namespace Script { Entity entity = entityMap.at(entityID); const auto& transformComponent = entity.GetComponent(); - auto [position, rotation, scale] = GetTransformDecomposition(transformComponent.Transform); + auto [position, rotation, scale] = GetTransformDecomposition(transformComponent.Transformation); *outRotation = glm::degrees(glm::eulerAngles(rotation)); } @@ -376,13 +368,46 @@ namespace Prism { namespace Script { PM_CORE_ASSERT(entityMap.find(entityID) != entityMap.end(), "Invalid entity ID or entity doesn't exist in scene!"); Entity entity = entityMap.at(entityID); - auto& transform = entity.Transform(); + auto& transform = entity.Transformation(); auto [position, rotation, scale] = GetTransformDecomposition(transform); transform = glm::translate(glm::mat4(1.0f), position) * glm::toMat4(glm::quat(glm::radians(*inRotation))) * glm::scale(glm::mat4(1.0f), scale); } + */ + + void Prism_TransformComponent_GetTransform(const uint64_t entityID, ScriptTransform* outTransform) + { + Ref scene = ScriptEngine::GetCurrentSceneContext(); + PM_CORE_ASSERT(scene, "No active scene!"); + const auto& entityMap = scene->GetEntityMap(); + PM_CORE_ASSERT(entityMap.find(entityID) != entityMap.end(), "Invalid entity ID or entity doesn't exist in scene!"); + + Entity entity = entityMap.at(entityID); + const Transform& transform = entity.GetComponent(); + + + *outTransform = { + transform.GetTranslation(), transform.GetRotation(), transform.GetScale(), + transform.GetUpDirection(), transform.GetRightDirection(), transform.GetForwardDirection() + }; + } + + void Prism_TransformComponent_SetTransform(const uint64_t entityID, const ScriptTransform* inTransform) + { + Ref scene = ScriptEngine::GetCurrentSceneContext(); + PM_CORE_ASSERT(scene, "No active scene!"); + const auto& entityMap = scene->GetEntityMap(); + PM_CORE_ASSERT(entityMap.find(entityID) != entityMap.end(), "Invalid entity ID or entity doesn't exist in scene!"); + + Entity entity = entityMap.at(entityID); + Transform& transform = entity.GetComponent(); + + transform.SetTranslation(inTransform->Translation); + transform.SetRotation(inTransform->Rotation); + transform.SetScale(inTransform->Scale); + } void* Prism_MeshComponent_GetMesh(const uint64_t entityID) { diff --git a/Prism/src/Prism/Script/ScriptWrappers.h b/Prism/src/Prism/Script/ScriptWrappers.h index fe56ab0..1a799d1 100644 --- a/Prism/src/Prism/Script/ScriptWrappers.h +++ b/Prism/src/Prism/Script/ScriptWrappers.h @@ -19,6 +19,14 @@ extern "C" { namespace Prism { namespace Script { + struct ScriptTransform + { + glm::vec3 Translation; + glm::vec3 Rotation; + glm::vec3 Scale; + glm::vec3 Up, Right, Forward; + }; + // Math float Prism_Noise_PerlinNoise(float x, float y); @@ -42,12 +50,18 @@ namespace Prism { namespace Script { void Prism_Entity_CreateComponent(uint64_t entityID, void* type); bool Prism_Entity_HasComponent(uint64_t entityID, void* type); uint64_t Prism_Entity_FindEntityByTag(MonoString* tag); + /* void Prism_Entity_GetTransform(uint64_t entityID, glm::mat4* outTransform); void Prism_Entity_SetTransform(uint64_t entityID, const glm::mat4* inTransform); + */ + /* void Prism_TransformComponent_GetRelativeDirection(uint64_t entityID, glm::vec3* outDirection, const glm::vec3* inAbsoluteDirection); void Prism_TransformComponent_GetRotation(uint64_t entityID,glm::vec3* outRotation); void Prism_TransformComponent_SetRotation(uint64_t entityID,const glm::vec3* inRotation); + */ + void Prism_TransformComponent_GetTransform(uint64_t entityID, ScriptTransform* outTransform); + void Prism_TransformComponent_SetTransform(uint64_t entityID, const ScriptTransform* inTransform); // 2D Physic void Prism_RigidBody2DComponent_ApplyLinearImpulse(uint64_t entityID, const glm::vec2* impulse, const glm::vec2* offset, bool wake);