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

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