add transform class, some code tweaks

This commit is contained in:
2025-12-29 23:11:14 +08:00
parent ce41e348f8
commit 960eeaf94b
32 changed files with 368 additions and 314 deletions

View File

@ -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<float> 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);
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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);
}