first commit
This commit is contained in:
115
NitroxModel/DataStructures/Unity/NitroxColor.cs
Normal file
115
NitroxModel/DataStructures/Unity/NitroxColor.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
using NitroxModel.Helper;
|
||||
|
||||
namespace NitroxModel.DataStructures.Unity
|
||||
{
|
||||
[DataContract]
|
||||
[Serializable]
|
||||
public struct NitroxColor
|
||||
{
|
||||
[DataMember(Order = 1)]
|
||||
public float R { get; private set; }
|
||||
|
||||
[DataMember(Order = 2)]
|
||||
public float G { get; private set; }
|
||||
|
||||
[DataMember(Order = 3)]
|
||||
public float B { get; private set; }
|
||||
|
||||
[DataMember(Order = 4)]
|
||||
public float A { get; private set; }
|
||||
|
||||
public NitroxColor(float r, float g, float b, float a = 1)
|
||||
{
|
||||
R = r;
|
||||
G = g;
|
||||
B = b;
|
||||
A = a;
|
||||
}
|
||||
|
||||
public static NitroxColor FromHsb(float hue, float saturation = 1, float brightness = 1)
|
||||
{
|
||||
hue = Mathf.Clamp01(hue);
|
||||
saturation = Mathf.Clamp01(saturation);
|
||||
brightness = Mathf.Clamp01(brightness);
|
||||
|
||||
hue *= 360f;
|
||||
float r = 0;
|
||||
float g = 0;
|
||||
float b = 0;
|
||||
|
||||
if (saturation == 0)
|
||||
{
|
||||
r = g = b = brightness;
|
||||
}
|
||||
else
|
||||
{
|
||||
// the color wheel consists of 6 sectors.
|
||||
float sectorPos = hue / 60.0f;
|
||||
int sectorNumber = (int)Math.Floor(sectorPos);
|
||||
// get the fractional part of the sector
|
||||
float fractionalSector = sectorPos - sectorNumber;
|
||||
|
||||
// calculate values for the three axes of the color.
|
||||
float p = brightness * (1f - saturation);
|
||||
float q = brightness * (1f - saturation * fractionalSector);
|
||||
float t = brightness * (1f - saturation * (1f - fractionalSector));
|
||||
|
||||
// assign the fractional colors to r, g, and b based on the sector the angle is in.
|
||||
switch (sectorNumber)
|
||||
{
|
||||
case 0:
|
||||
r = brightness;
|
||||
g = t;
|
||||
b = p;
|
||||
break;
|
||||
case 1:
|
||||
r = q;
|
||||
g = brightness;
|
||||
b = p;
|
||||
break;
|
||||
case 2:
|
||||
r = p;
|
||||
g = brightness;
|
||||
b = t;
|
||||
break;
|
||||
case 3:
|
||||
r = p;
|
||||
g = q;
|
||||
b = brightness;
|
||||
break;
|
||||
case 4:
|
||||
r = t;
|
||||
g = p;
|
||||
b = brightness;
|
||||
break;
|
||||
case 5:
|
||||
r = brightness;
|
||||
g = p;
|
||||
b = q;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (r < 0)
|
||||
{
|
||||
r = 0;
|
||||
}
|
||||
if (g < 0)
|
||||
{
|
||||
g = 0;
|
||||
}
|
||||
if (b < 0)
|
||||
{
|
||||
b = 0;
|
||||
}
|
||||
return new NitroxColor(r, g, b);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[NitroxColor: {R}, {G}, {B}, {A}]";
|
||||
}
|
||||
}
|
||||
}
|
141
NitroxModel/DataStructures/Unity/NitroxQuaternion.cs
Normal file
141
NitroxModel/DataStructures/Unity/NitroxQuaternion.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.Serialization;
|
||||
using NitroxModel.Helper;
|
||||
|
||||
namespace NitroxModel.DataStructures.Unity
|
||||
{
|
||||
[DataContract]
|
||||
[Serializable]
|
||||
public struct NitroxQuaternion : IEquatable<NitroxQuaternion>
|
||||
{
|
||||
[DataMember(Order = 1)]
|
||||
public float X;
|
||||
|
||||
[DataMember(Order = 2)]
|
||||
public float Y;
|
||||
|
||||
[DataMember(Order = 3)]
|
||||
public float Z;
|
||||
|
||||
[DataMember(Order = 4)]
|
||||
public float W;
|
||||
|
||||
public static NitroxQuaternion Identity { get; } = new NitroxQuaternion(0, 0, 0, 1);
|
||||
|
||||
public NitroxQuaternion(float x, float y, float z, float w)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
Z = z;
|
||||
W = w;
|
||||
}
|
||||
|
||||
public static NitroxQuaternion Normalize(NitroxQuaternion value) => (NitroxQuaternion)Quaternion.Normalize((Quaternion)value);
|
||||
|
||||
public static NitroxQuaternion FromEuler(NitroxVector3 vector) => FromEuler(vector.X, vector.Y, vector.Z);
|
||||
|
||||
public static NitroxQuaternion FromEuler(float x, float y, float z) => (NitroxQuaternion)Quaternion.CreateFromYawPitchRoll(y * Mathf.DEG2RAD, x * Mathf.DEG2RAD, z * Mathf.DEG2RAD);
|
||||
|
||||
//Used https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/
|
||||
public NitroxVector3 ToEuler()
|
||||
{
|
||||
float sqw = W * W;
|
||||
float sqx = X * X;
|
||||
float sqy = Y * Y;
|
||||
float sqz = Z * Z;
|
||||
|
||||
float unit = sqx + sqy + sqz + sqw; // if normalized is one, otherwise is correction factor
|
||||
float test = X * Y + Z * W;
|
||||
|
||||
float heading, attitude, bank;
|
||||
if (test > 0.499f * unit) // singularity at north pole
|
||||
{
|
||||
heading = 2 * Mathf.Atan2(X, W);
|
||||
attitude = Mathf.PI / 2f;
|
||||
bank = 0;
|
||||
}
|
||||
else if (test < -0.499f * unit) // singularity at south pole
|
||||
{
|
||||
heading = -2 * Mathf.Atan2(X, W);
|
||||
attitude = -Mathf.PI / 2f;
|
||||
bank = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
heading = Mathf.Atan2(2 * Y * W - 2 * X * Z, sqx - sqy - sqz + sqw);
|
||||
attitude = Mathf.Asin(2 * test / unit);
|
||||
bank = Mathf.Atan2(2 * X * W - 2 * Y * Z, -sqx + sqy - sqz + sqw);
|
||||
}
|
||||
|
||||
NitroxVector3 euler = new NitroxVector3(bank * Mathf.RAD2DEG, heading * Mathf.RAD2DEG, attitude * Mathf.RAD2DEG);
|
||||
NormalizeEuler(ref euler);
|
||||
|
||||
return euler;
|
||||
}
|
||||
|
||||
public static void NormalizeEuler(ref NitroxVector3 vector3)
|
||||
{
|
||||
NormalizeEulerPart(ref vector3.X);
|
||||
NormalizeEulerPart(ref vector3.Y);
|
||||
NormalizeEulerPart(ref vector3.Z);
|
||||
}
|
||||
|
||||
private static void NormalizeEulerPart(ref float euler)
|
||||
{
|
||||
while (euler > 360)
|
||||
{
|
||||
euler -= 360;
|
||||
}
|
||||
|
||||
while (euler < 0)
|
||||
{
|
||||
euler += 360;
|
||||
}
|
||||
}
|
||||
|
||||
public static NitroxQuaternion operator *(NitroxQuaternion lhs, NitroxQuaternion rhs) => (NitroxQuaternion)Quaternion.Multiply((Quaternion)lhs, (Quaternion)rhs);
|
||||
|
||||
public static explicit operator Quaternion(NitroxQuaternion q) => new Quaternion(q.X, q.Y, q.Z, q.W);
|
||||
|
||||
public static explicit operator NitroxQuaternion(Quaternion q) => new NitroxQuaternion(q.X, q.Y, q.Z, q.W);
|
||||
|
||||
public static bool operator ==(NitroxQuaternion left, NitroxQuaternion right) => left.Equals(right);
|
||||
|
||||
public static bool operator !=(NitroxQuaternion left, NitroxQuaternion right) => !left.Equals(right);
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is NitroxQuaternion other && Equals(other);
|
||||
}
|
||||
|
||||
public bool Equals(NitroxQuaternion other)
|
||||
{
|
||||
return Equals(other, float.Epsilon);
|
||||
}
|
||||
|
||||
public bool Equals(NitroxQuaternion other, float tolerance)
|
||||
{
|
||||
return X == other.X && Y == other.Y && Z == other.Z && W == other.W ||
|
||||
Math.Abs(other.X + X) < tolerance && Math.Abs(other.Y + Y) < tolerance && Math.Abs(other.Z + Z) < tolerance && Math.Abs(other.W + W) < tolerance ||
|
||||
Math.Abs(other.X - X) < tolerance && Math.Abs(other.Y - Y) < tolerance && Math.Abs(other.Z - Z) < tolerance && Math.Abs(other.W - W) < tolerance;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[Quaternion: {X}, {Y}, {Z}, {W}]";
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
int hashCode = X.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ Y.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ Z.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ W.GetHashCode();
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
99
NitroxModel/DataStructures/Unity/NitroxTransform.cs
Normal file
99
NitroxModel/DataStructures/Unity/NitroxTransform.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.Serialization;
|
||||
using BinaryPack.Attributes;
|
||||
using NitroxModel.Helper;
|
||||
|
||||
namespace NitroxModel.DataStructures.Unity
|
||||
{
|
||||
[DataContract]
|
||||
[Serializable]
|
||||
public class NitroxTransform
|
||||
{
|
||||
[DataMember(Order = 1)]
|
||||
public NitroxVector3 LocalPosition;
|
||||
|
||||
[DataMember(Order = 2)]
|
||||
public NitroxQuaternion LocalRotation;
|
||||
|
||||
[DataMember(Order = 3)]
|
||||
public NitroxVector3 LocalScale;
|
||||
|
||||
public Matrix4x4 LocalToWorldMatrix
|
||||
{
|
||||
get
|
||||
{
|
||||
Matrix4x4 cachedMatrix = Matrix4x4Extension.Compose(LocalPosition, LocalRotation, LocalScale);
|
||||
|
||||
return Parent != null ? cachedMatrix * Parent.LocalToWorldMatrix : cachedMatrix;
|
||||
}
|
||||
}
|
||||
|
||||
public NitroxTransform Parent;
|
||||
|
||||
[IgnoredMember]
|
||||
public NitroxVector3 Position
|
||||
{
|
||||
get
|
||||
{
|
||||
Matrix4x4 matrix = Parent?.LocalToWorldMatrix ?? Matrix4x4.Identity;
|
||||
return matrix.Transform(LocalPosition);
|
||||
}
|
||||
set
|
||||
{
|
||||
Matrix4x4 matrix = Parent != null ? Matrix4x4Extension.Compose(value, LocalRotation, LocalScale) * Parent.LocalToWorldMatrix.Invert() : Matrix4x4Extension.Compose(value, LocalRotation, LocalScale);
|
||||
LocalPosition = (NitroxVector3)matrix.Translation;
|
||||
}
|
||||
}
|
||||
|
||||
[IgnoredMember]
|
||||
public NitroxQuaternion Rotation
|
||||
{
|
||||
get
|
||||
{
|
||||
Matrix4x4 matrix = Parent?.LocalToWorldMatrix ?? Matrix4x4.Identity;
|
||||
Matrix4x4.Decompose(matrix, out Vector3 _, out Quaternion rotation, out Vector3 _);
|
||||
|
||||
return (NitroxQuaternion)rotation * LocalRotation;
|
||||
}
|
||||
set
|
||||
{
|
||||
Matrix4x4 matrix = Parent != null ? Matrix4x4Extension.Compose(LocalPosition, value, LocalScale) * Parent.LocalToWorldMatrix.Invert() : Matrix4x4Extension.Compose(LocalPosition, value, LocalScale);
|
||||
Matrix4x4.Decompose(matrix, out Vector3 _, out Quaternion rotation, out Vector3 _);
|
||||
|
||||
LocalRotation = (NitroxQuaternion)rotation;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetParent(NitroxTransform parent, bool worldPositionStays = true)
|
||||
{
|
||||
if (!worldPositionStays)
|
||||
{
|
||||
Parent = parent;
|
||||
return;
|
||||
}
|
||||
|
||||
NitroxVector3 position = Position;
|
||||
NitroxQuaternion rotation = Rotation;
|
||||
|
||||
Parent = parent;
|
||||
|
||||
Position = position;
|
||||
Rotation = rotation;
|
||||
}
|
||||
|
||||
public NitroxTransform() { }
|
||||
|
||||
public NitroxTransform(NitroxVector3 localPosition, NitroxQuaternion localRotation, NitroxVector3 scale)
|
||||
{
|
||||
LocalPosition = localPosition;
|
||||
LocalRotation = localRotation;
|
||||
LocalScale = scale;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"(Position: {Position}, LocalPosition: {LocalPosition}, Rotation: {Rotation}, LocalRotation: {LocalRotation}, LocalScale: {LocalScale})";
|
||||
}
|
||||
}
|
||||
}
|
134
NitroxModel/DataStructures/Unity/NitroxVector3.cs
Normal file
134
NitroxModel/DataStructures/Unity/NitroxVector3.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.Serialization;
|
||||
using NitroxModel.Helper;
|
||||
|
||||
namespace NitroxModel.DataStructures.Unity
|
||||
{
|
||||
[DataContract]
|
||||
[Serializable]
|
||||
public struct NitroxVector3 : IEquatable<NitroxVector3>
|
||||
{
|
||||
[DataMember(Order = 1)]
|
||||
public float X;
|
||||
|
||||
[DataMember(Order = 2)]
|
||||
public float Y;
|
||||
|
||||
[DataMember(Order = 3)]
|
||||
public float Z;
|
||||
|
||||
public NitroxVector3 Normalized => Normalize(this);
|
||||
|
||||
public NitroxVector3(float x, float y, float z)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
Z = z;
|
||||
}
|
||||
|
||||
public static NitroxVector3 Zero => new(0, 0, 0);
|
||||
|
||||
public static NitroxVector3 One => new(1, 1, 1);
|
||||
|
||||
public static bool operator ==(NitroxVector3 left, NitroxVector3 right) => left.Equals(right);
|
||||
|
||||
public static bool operator !=(NitroxVector3 left, NitroxVector3 right) => !left.Equals(right);
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is NitroxVector3 other && Equals(other);
|
||||
}
|
||||
|
||||
public bool Equals(NitroxVector3 other)
|
||||
{
|
||||
return Equals(other, float.Epsilon);
|
||||
}
|
||||
|
||||
public bool Equals(NitroxVector3 other, float tolerance)
|
||||
{
|
||||
return X == other.X && Y == other.Y && Z == other.Z ||
|
||||
Math.Abs(other.X - X) < tolerance &&
|
||||
Math.Abs(other.Y - Y) < tolerance &&
|
||||
Math.Abs(other.Z - Z) < tolerance;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
int hashCode = X.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ Y.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ Z.GetHashCode();
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
public static NitroxVector3 operator +(NitroxVector3 a, NitroxVector3 b)
|
||||
{
|
||||
return new(a.X + b.X,
|
||||
a.Y + b.Y,
|
||||
a.Z + b.Z);
|
||||
}
|
||||
|
||||
public static NitroxVector3 operator -(NitroxVector3 a, NitroxVector3 b)
|
||||
{
|
||||
return new(a.X - b.X,
|
||||
a.Y - b.Y,
|
||||
a.Z - b.Z);
|
||||
}
|
||||
|
||||
public static NitroxVector3 operator -(NitroxVector3 a)
|
||||
{
|
||||
return new(-a.X, -a.Y, -a.Z);
|
||||
}
|
||||
|
||||
public static NitroxVector3 operator /(NitroxVector3 lhs, float rhs)
|
||||
{
|
||||
return new(lhs.X / rhs, lhs.Y / rhs, lhs.Z / rhs);
|
||||
}
|
||||
|
||||
public static NitroxVector3 operator *(NitroxVector3 lhs, float rhs)
|
||||
{
|
||||
return new(lhs.X * rhs, lhs.Y * rhs, lhs.Z * rhs);
|
||||
}
|
||||
|
||||
public static explicit operator Vector3(NitroxVector3 v) => new Vector3(v.X, v.Y,v.Z);
|
||||
public static explicit operator NitroxVector3(Vector3 v) => new NitroxVector3(v.X, v.Y,v.Z);
|
||||
|
||||
public static NitroxVector3 Normalize(NitroxVector3 value)
|
||||
{
|
||||
float length = value.Magnitude;
|
||||
return new NitroxVector3(value.X / length, value.Y / length, value.Z / length);
|
||||
}
|
||||
|
||||
public static float Length(NitroxVector3 value)
|
||||
{
|
||||
float ls = value.X * value.X + value.Y * value.Y + value.Z * value.Z;
|
||||
return Mathf.Sqrt(ls);
|
||||
}
|
||||
|
||||
public static NitroxVector3 Cross(NitroxVector3 vector1, NitroxVector3 vector2)
|
||||
{
|
||||
return new(
|
||||
vector1.Y * vector2.Z - vector1.Z * vector2.Y,
|
||||
vector1.Z * vector2.X - vector1.X * vector2.Z,
|
||||
vector1.X * vector2.Y - vector1.Y * vector2.X);
|
||||
}
|
||||
|
||||
public static float Distance(NitroxVector3 lhs, NitroxVector3 rhs)
|
||||
{
|
||||
float num1 = lhs.X - rhs.X;
|
||||
float num2 = lhs.Y - rhs.Y;
|
||||
float num3 = lhs.Z - rhs.Z;
|
||||
return Mathf.Sqrt(num1 * num1 + num2 * num2 + num3 * num3);
|
||||
}
|
||||
|
||||
public float Magnitude => Mathf.Sqrt(X * X + Y * Y + Z * Z);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[{X}, {Y}, {Z}]";
|
||||
}
|
||||
}
|
||||
}
|
22
NitroxModel/DataStructures/Unity/NitroxVector4.cs
Normal file
22
NitroxModel/DataStructures/Unity/NitroxVector4.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
|
||||
namespace NitroxModel.DataStructures.Unity
|
||||
{
|
||||
public struct NitroxVector4
|
||||
{
|
||||
public float X;
|
||||
public float Y;
|
||||
public float Z;
|
||||
public float W;
|
||||
|
||||
public NitroxVector4(float x, float y, float z, float w)
|
||||
{
|
||||
W = w;
|
||||
X = x;
|
||||
Y = y;
|
||||
Z = z;
|
||||
}
|
||||
|
||||
public float Magnitude => (float)Math.Sqrt(X * X + Y * Y + Z * Z + W * W);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user