35 lines
1.3 KiB
C#
35 lines
1.3 KiB
C#
|
using System.Numerics;
|
|||
|
|
|||
|
namespace Nebulix;
|
|||
|
|
|||
|
|
|||
|
public static class VectorExtensions
|
|||
|
{
|
|||
|
public static Vector3 Up(this Vector3 _) => Vector3.UnitY;
|
|||
|
public static Vector3 Down(this Vector3 _) => -Vector3.UnitY;
|
|||
|
public static Vector3 Left(this Vector3 _) => Vector3.UnitX;
|
|||
|
public static Vector3 Right(this Vector3 _) => -Vector3.UnitX;
|
|||
|
public static Vector3 Front(this Vector3 _) => -Vector3.UnitZ;
|
|||
|
public static Vector3 Back(this Vector3 _) => -Vector3.UnitZ;
|
|||
|
|
|||
|
// Thanks to: https://stackoverflow.com/a/67920029
|
|||
|
public static Vector3 Slerp(this Vector3 start, Vector3 end, float percent)
|
|||
|
{
|
|||
|
// the cosine of the angle between 2 vectors.
|
|||
|
float dot = Vector3.Dot(start, end);
|
|||
|
|
|||
|
// Clamp it to be in the range of Acos()
|
|||
|
// This may be unnecessary, but floating point precision can be a fickle mistress.
|
|||
|
Math.Clamp(dot, -1.0f, 1.0f);
|
|||
|
|
|||
|
// Acos(dot) returns the angle between start and end,
|
|||
|
// And multiplying that by percent returns the angle between start and the final result.
|
|||
|
float theta = (float)Math.Acos(dot) * percent;
|
|||
|
Vector3 relativeVec = end - start * dot;
|
|||
|
relativeVec = Vector3.Normalize(relativeVec);
|
|||
|
|
|||
|
// Orthonormal basis
|
|||
|
// The final result.
|
|||
|
return ((start * (float)Math.Cos(theta)) + (relativeVec * (float)Math.Sin(theta)));
|
|||
|
}
|
|||
|
}
|