Transitioned from VectorX to floatX

And some trying to update Rigidbody when transform changes
This commit is contained in:
Simon Lübeß
2023-06-25 02:47:33 +02:00
parent 5f5da2ed35
commit 9e1f648aa0
49 changed files with 1281 additions and 871 deletions
+2 -2
View File
@@ -53,10 +53,10 @@ namespace GlitchyEngine.Math
[Inline]
#unwarn
public static explicit operator Vector3(ColorRGB color) => *(Vector3*)&color;
public static explicit operator float3(ColorRGB color) => *(float3*)&color;
[Inline]
#unwarn
public static explicit operator ColorRGB(Vector3 color) => *(ColorRGB*)&color;
public static explicit operator ColorRGB(float3 color) => *(ColorRGB*)&color;
}
}
+2 -2
View File
@@ -329,10 +329,10 @@ namespace GlitchyEngine.Math
[Inline]
#unwarn
public static explicit operator Vector4(ColorRGBA color) => *(Vector4*)&color;
public static explicit operator float4(ColorRGBA color) => *(float4*)&color;
[Inline]
#unwarn
public static explicit operator ColorRGBA(Vector4 color) => *(ColorRGBA*)&color;
public static explicit operator ColorRGBA(float4 color) => *(ColorRGBA*)&color;
}
}
+3 -3
View File
@@ -13,12 +13,12 @@ namespace System
}
[Inline]
public Vector2 XX => Vector2((float)this);
public float2 XX => (float2)this;
[Inline]
public Vector3 XXX => Vector3((float)this);
public float3 XXX => (float3)this;
[Inline]
public Vector4 XXXX => Vector4((float)this);
public float4 XXXX => (float4)this;
}
}
+560 -259
View File
@@ -1,8 +1,13 @@
using System;
namespace GlitchyEngine.Math.FancyMath;
static class FancyMath
namespace GlitchyEngine.Math;
// TODO: Not all functions are available for scalars yet
static
{
/// Returns true if at least one of the components is true.
public static bool any(bool value) => value;
/// Returns true if at least one of the components is true.
public static bool any(bool2 value) => value.X || value.Y;
/// Returns true if at least one of the components is true.
@@ -10,6 +15,8 @@ static class FancyMath
/// Returns true if at least one of the components is true.
public static bool any(bool4 value) => value.X || value.Y || value.Z || value.W;
/// Returns true if all of the components are true.
public static bool all(bool value) => value;
/// Returns true if all of the components are true.
public static bool all(bool2 value) => value.X && value.Y;
/// Returns true if all of the components are true.
@@ -19,6 +26,11 @@ static class FancyMath
#region abs
public static float abs(float value)
{
return Math.Abs(value);
}
public static float2 abs(float2 value)
{
return float2(Math.Abs(value.X), Math.Abs(value.Y));
@@ -34,6 +46,11 @@ static class FancyMath
return float4(Math.Abs(value.X), Math.Abs(value.Y), Math.Abs(value.Z), Math.Abs(value.W));
}
public static int abs(int value)
{
return Math.Abs(value);
}
public static int2 abs(int2 value)
{
return int2(Math.Abs(value.X), Math.Abs(value.Y));
@@ -50,247 +67,9 @@ static class FancyMath
}
#endregion
#region ceil / floor
public static float2 ceil(float2 value)
{
return float2(Math.Ceiling(value.X), Math.Ceiling(value.Y));
}
public static float3 ceil(float3 value)
{
return float3(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z));
}
public static float4 ceil(float4 value)
{
return float4(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z), Math.Ceiling(value.W));
}
public static float2 floor(float2 value)
{
return float2(Math.Floor(value.X), Math.Floor(value.Y));
}
public static float3 floor(float3 value)
{
return float3(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z));
}
public static float4 floor(float4 value)
{
return float4(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z), Math.Floor(value.W));
}
#endregion
#region Clamp
public static float2 clamp(float2 value, float2 min, float2 max)
{
return float2(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y));
}
public static float3 clamp(float3 value, float3 min, float3 max)
{
return float3(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z));
}
public static float4 clamp(float4 value, float4 min, float4 max)
{
return float4(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z), Math.Clamp(value.W, min.W, max.W));
}
public static int2 clamp(int2 value, int2 min, int2 max)
{
return int2(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y));
}
public static int3 clamp(int3 value, int3 min, int3 max)
{
return int3(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z));
}
public static int4 clamp(int4 value, int4 min, int4 max)
{
return int4(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z), Math.Clamp(value.W, min.W, max.W));
}
#endregion
#region Lerp
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float2 lerp(float2 x, float2 y, float s)
{
return x + s * (y - x);
}
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float3 lerp(float3 x, float3 y, float s)
{
return x + s * (y - x);
}
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float4 lerp(float4 x, float4 y, float s)
{
return x + s * (y - x);
}
#endregion
// log, log10, log2
#region min / max
public static float2 min(float2 x, float2 y)
{
return float2(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y));
}
public static float3 min(float3 x, float3 y)
{
return float3(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y), Math.Min(x.Z, y.Z));
}
public static float4 min(float4 x, float4 y)
{
return float4(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y), Math.Min(x.Z, y.Z), Math.Min(x.W, y.W));
}
public static float2 max(float2 x, float2 y)
{
return float2(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y));
}
public static float3 max(float3 x, float3 y)
{
return float3(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y), Math.Max(x.Z, y.Z));
}
public static float4 max(float4 x, float4 y)
{
return float4(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y), Math.Max(x.Z, y.Z), Math.Max(x.W, y.W));
}
#endregion
// mul
public static float2 normalize(float2 value) => value / length(value);
public static float3 normalize(float3 value) => value / length(value);
public static float4 normalize(float4 value) => value / length(value);
// pow
// rcp??? (reciprocal)
// reflect and refract?
// round
// rsqrt?
// sqrt
// saturate
// sign
// step and smoothstep
// transpose
#region exp
/// Returns the base-e exponential, or e^x, of the specified value.
public static float2 exp(float2 x)
{
return float2(Math.Exp(x.X), Math.Exp(x.Y));
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float3 exp(float3 x)
{
return float3(Math.Exp(x.X), Math.Exp(x.Y), Math.Exp(x.Z));
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float4 exp(float4 x)
{
return float4(Math.Exp(x.X), Math.Exp(x.Y), Math.Exp(x.Z), Math.Exp(x.W));
}
// Exp2?
#endregion
#region Reject / Project
/**
* Calculates the projection of a onto b
*/
public static float2 project(float2 a, float2 b)
{
return (b * (dot(a, b) / dot(b, b)));
}
/**
* Calculates the projection of a onto b
*/
public static float3 project(float3 a, float3 b)
{
return (b * (dot(a, b) / dot(b, b)));
}
/**
* Calculates the projection of a onto b
*/
public static float4 project(float4 a, float4 b)
{
return (b * (dot(a, b) / dot(b, b)));
}
/**
* Calculates the rejection of a from b
*/
public static float2 reject(float2 a, float2 b)
{
return (a - b * (dot(a, b) / dot(b, b)));
}
/**
* Calculates the rejection of a from b
*/
public static float3 reject(float3 a, float3 b)
{
return (a - b * (dot(a, b) / dot(b, b)));
}
/**
* Calculates the rejection of a from b
*/
public static float4 reject(float4 a, float4 b)
{
return (a - b * (dot(a, b) / dot(b, b)));
}
#endregion
#region modf / frac / trunc
// Splits the value x into fractional and integer parts, each of which has the same sign as x.
public static float2 modf(float2 x, out float2 integerPart)
{
@@ -301,7 +80,7 @@ static class FancyMath
return fracPart;
}
// Splits the value x into fractional and integer parts, each of which has the same sign as x.
public static float3 modf(float3 x, out float3 integerPart)
{
@@ -313,7 +92,7 @@ static class FancyMath
return fracPart;
}
// Splits the value x into fractional and integer parts, each of which has the same sign as x.
public static float4 modf(float4 x, out float4 integerPart)
{
@@ -383,19 +162,19 @@ static class FancyMath
{
return bool2(value.X.IsInfinity, value.Y.IsInfinity);
}
/// Determines if the specified value is infinite.
public static bool3 isinf(float3 value)
{
return bool3(value.X.IsInfinity, value.Y.IsInfinity, value.Z.IsInfinity);
}
/// Determines if the specified value is infinite.
public static bool4 isinf(float4 value)
{
return bool4(value.X.IsInfinity, value.Y.IsInfinity, value.Z.IsInfinity, value.W.IsInfinity);
}
/// Determines if the specified value is infinite.
public static bool2 isnan(float2 value)
{
@@ -416,6 +195,541 @@ static class FancyMath
#endregion
#region Sign
/// Returns the sign of x.
public static int32 sign(float x)
{
return (int32)Math.Sign(x);
}
/// Returns the sign of x.
public static int2 sign(float2 x)
{
return int2((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y));
}
/// Returns the sign of x.
public static int3 sign(float3 x)
{
return int3((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y), (int32)Math.Sign(x.Z));
}
/// Returns the sign of x.
public static int4 sign(float4 x)
{
return int4((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y), (int32)Math.Sign(x.Z), (int32)Math.Sign(x.W));
}
/// Returns the sign of x.
public static int32 sign(int x)
{
return (int32)Math.Sign(x);
}
/// Returns the sign of x.
public static int2 sign(int2 x)
{
return int2((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y));
}
/// Returns the sign of x.
public static int3 sign(int3 x)
{
return int3((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y), (int32)Math.Sign(x.Z));
}
/// Returns the sign of x.
public static int4 sign(int4 x)
{
return int4((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y), (int32)Math.Sign(x.Z), (int32)Math.Sign(x.W));
}
#endregion
#region ceil / floor / round
public static float ceil(float value)
{
return Math.Ceiling(value);
}
public static float2 ceil(float2 value)
{
return float2(Math.Ceiling(value.X), Math.Ceiling(value.Y));
}
public static float3 ceil(float3 value)
{
return float3(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z));
}
public static float4 ceil(float4 value)
{
return float4(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z), Math.Ceiling(value.W));
}
public static float floor(float value)
{
return Math.Floor(value);
}
public static float2 floor(float2 value)
{
return float2(Math.Floor(value.X), Math.Floor(value.Y));
}
public static float3 floor(float3 value)
{
return float3(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z));
}
public static float4 floor(float4 value)
{
return float4(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z), Math.Floor(value.W));
}
/// Rounds the specified value to the nearest integer.
public static float round(float value)
{
return Math.Round(value);
}
/// Rounds the specified value to the nearest integer.
public static float2 round(float2 value)
{
return float2(Math.Round(value.X), Math.Round(value.Y));
}
/// Rounds the specified value to the nearest integer.
public static float3 round(float3 value)
{
return float3(Math.Round(value.X), Math.Round(value.Y), Math.Round(value.Z));
}
/// Rounds the specified value to the nearest integer.
public static float4 round(float4 value)
{
return float4(Math.Round(value.X), Math.Round(value.Y), Math.Round(value.Z), Math.Round(value.W));
}
#endregion
#region min / max
public static float2 min(float2 x, float2 y)
{
return float2(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y));
}
public static float3 min(float3 x, float3 y)
{
return float3(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y), Math.Min(x.Z, y.Z));
}
public static float4 min(float4 x, float4 y)
{
return float4(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y), Math.Min(x.Z, y.Z), Math.Min(x.W, y.W));
}
public static float2 max(float2 x, float2 y)
{
return float2(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y));
}
public static float3 max(float3 x, float3 y)
{
return float3(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y), Math.Max(x.Z, y.Z));
}
public static float4 max(float4 x, float4 y)
{
return float4(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y), Math.Max(x.Z, y.Z), Math.Max(x.W, y.W));
}
#endregion
#region exp, pow, log
// TODO: log, log10, log2
/// Returns x raised to the power of y.
public static float pow(float x, float y)
{
return Math.Pow(x, y);
}
/// Returns x raised to the power of y.
public static float2 pow(float2 x, float2 y)
{
return float2(Math.Pow(x.X, y.X), Math.Pow(x.Y, y.Y));
}
/// Returns x raised to the power of y.
public static float3 pow(float3 x, float3 y)
{
return float3(Math.Pow(x.X, y.X), Math.Pow(x.Y, y.Y), Math.Pow(x.Z, y.Z));
}
/// Returns x raised to the power of y.
public static float4 pow(float4 x, float4 y)
{
return float4(Math.Pow(x.X, y.X), Math.Pow(x.Y, y.Y), Math.Pow(x.Z, y.Z), Math.Pow(x.W, y.W));
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float exp(float x)
{
return Math.Exp(x);
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float2 exp(float2 x)
{
return float2(Math.Exp(x.X), Math.Exp(x.Y));
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float3 exp(float3 x)
{
return float3(Math.Exp(x.X), Math.Exp(x.Y), Math.Exp(x.Z));
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float4 exp(float4 x)
{
return float4(Math.Exp(x.X), Math.Exp(x.Y), Math.Exp(x.Z), Math.Exp(x.W));
}
/// Returns the base 2 exponential, or 2^x, of the specified value.
public static float exp2(float x)
{
return Math.Pow(2, x);
}
/// Returns the base 2 exponential, or 2^x, of the specified value.
public static float2 exp2(float2 x)
{
return float2(Math.Pow(2, x.X), Math.Pow(2, x.Y));
}
/// Returns the base 2 exponential, or 2^x, of the specified value.
public static float3 exp2(float3 x)
{
return float3(Math.Pow(2, x.X), Math.Pow(2, x.Y), Math.Pow(2, x.Z));
}
/// Returns the base 2 exponential, or 2^x, of the specified value.
public static float4 exp2(float4 x)
{
return float4(Math.Pow(2, x.X), Math.Pow(2, x.Y), Math.Pow(2, x.Z), Math.Pow(2, x.W));
}
#endregion
#region Degrees / Radians
public static float toDegrees(float radians) => radians * MathHelper.RadToDeg;
public static float2 toDegrees(float2 radians) => radians * MathHelper.RadToDeg;
public static float3 toDegrees(float3 radians) => radians * MathHelper.RadToDeg;
public static float4 toDegrees(float4 radians) => radians * MathHelper.RadToDeg;
public static float toRadians(float degrees) => degrees * MathHelper.DegToRad;
public static float2 toRadians(float2 degrees) => degrees * MathHelper.DegToRad;
public static float3 toRadians(float3 degrees) => degrees * MathHelper.DegToRad;
public static float4 toRadians(float4 degrees) => degrees * MathHelper.DegToRad;
#endregion
#region Clamp
public static float clamp(float value, float min, float max)
{
return Math.Clamp(value.X, min.X, max.X);
}
public static float2 clamp(float2 value, float2 min, float2 max)
{
return float2(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y));
}
public static float3 clamp(float3 value, float3 min, float3 max)
{
return float3(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z));
}
public static float4 clamp(float4 value, float4 min, float4 max)
{
return float4(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z), Math.Clamp(value.W, min.W, max.W));
}
public static int2 clamp(int2 value, int2 min, int2 max)
{
return int2(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y));
}
public static int3 clamp(int3 value, int3 min, int3 max)
{
return int3(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z));
}
public static int4 clamp(int4 value, int4 min, int4 max)
{
return int4(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z), Math.Clamp(value.W, min.W, max.W));
}
#endregion
#region Lerp
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float lerp(float x, float y, float s)
{
return x + s * (y - x);
}
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float2 lerp(float2 x, float2 y, float s)
{
return x + s * (y - x);
}
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float3 lerp(float3 x, float3 y, float s)
{
return x + s * (y - x);
}
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float4 lerp(float4 x, float4 y, float s)
{
return x + s * (y - x);
}
#endregion
// mul? hlsl has like 280 overloads...
public static float normalize(float value) => 1.0f;
public static float2 normalize(float2 value) => value / length(value);
public static float3 normalize(float3 value) => value / length(value);
public static float4 normalize(float4 value) => value / length(value);
// pow
#region Reflect and Refract
/// Returns a reflection vector using an incident ray and a surface normal.
public static float2 reflect(float2 incident, float2 normal) => incident - 2 * normal * dot(incident, normal);
/// Returns a reflection vector using an incident ray and a surface normal.
public static float3 reflect(float3 incident, float3 normal) => incident - 2 * normal * dot(incident, normal);
/// Returns a reflection vector using an incident ray and a surface normal.
public static float4 reflect(float4 incident, float4 normal) => incident - 2 * normal * dot(incident, normal);
// Source: https://thebookofshaders.com/glossary/?search=refract
/// Returns a refraction vector using an entering ray, a surface normal, and a refraction index.
public static float2 refract(float2 incident, float2 normal, float refractionIndex)
{
float dot_n_i = dot(normal, incident);
float k = 1.0f - refractionIndex * refractionIndex * (1.0f - dot_n_i * dot_n_i);
if (k < 0.0f)
return 0.0f;
else
return refractionIndex * incident - (refractionIndex * dot_n_i + sqrt(k));
}
/// Returns a refraction vector using an entering ray, a surface normal, and a refraction index.
public static float3 refract(float3 incident, float3 normal, float refractionIndex)
{
float dot_n_i = dot(normal, incident);
float k = 1.0f - refractionIndex * refractionIndex * (1.0f - dot_n_i * dot_n_i);
if (k < 0.0f)
return 0.0f;
else
return refractionIndex * incident - (refractionIndex * dot_n_i + sqrt(k));
}
/// Returns a refraction vector using an entering ray, a surface normal, and a refraction index.
public static float4 refract(float4 incident, float4 normal, float refractionIndex)
{
float dot_n_i = dot(normal, incident);
float k = 1.0f - refractionIndex * refractionIndex * (1.0f - dot_n_i * dot_n_i);
if (k < 0.0f)
return 0.0f;
else
return refractionIndex * incident - (refractionIndex * dot_n_i + sqrt(k));
}
#endregion
/// Calculates the per component square root of the given value.
public static float sqrt(float value)
{
return Math.Sqrt(value);
}
/// Calculates the per component square root of the given value.
public static float2 sqrt(float2 value)
{
return float2(Math.Sqrt(value.X), Math.Sqrt(value.Y));
}
/// Calculates the per component square root of the given value.
public static float3 sqrt(float3 value)
{
return float3(Math.Sqrt(value.X), Math.Sqrt(value.Y), Math.Sqrt(value.Z));
}
/// Calculates the per component square root of the given value.
public static float4 sqrt(float4 value)
{
return float4(Math.Sqrt(value.X), Math.Sqrt(value.Y), Math.Sqrt(value.Z), Math.Sqrt(value.W));
}
// saturate (I don't think there is a faster way than simply using clamp, so simply use clamp...)
#region Step / Smoothstep
/// Compares two values, returning 0 or 1 based on which value is greater.
/// @returns 1 if the x parameter is greater than or equal to the y parameter; otherwise, 0.
public static float step(float y, float x)
{
return (x >= y) ? 1.0f : 0.0f;
}
/// Compares two values, returning 0 or 1 based on which value is greater.
/// @returns 1 if the x parameter is greater than or equal to the y parameter; otherwise, 0.
public static float2 step(float2 y, float2 x)
{
bool2 b = (x >= y);
return float2(b.X ? 1.0f : 0.0f, b.Y ? 1.0f : 0.0f);
}
/// Compares two values, returning 0 or 1 based on which value is greater.
/// @returns 1 if the x parameter is greater than or equal to the y parameter; otherwise, 0.
public static float3 step(float3 y, float3 x)
{
bool3 b = (x >= y);
return float3(b.X ? 1.0f : 0.0f, b.Y ? 1.0f : 0.0f, b.Z ? 1.0f : 0.0f);
}
/// Compares two values, returning 0 or 1 based on which value is greater.
/// @returns 1 if the x parameter is greater than or equal to the y parameter; otherwise, 0.
public static float4 step(float4 y, float4 x)
{
bool4 b = (x >= y);
return float4(b.X ? 1.0f : 0.0f, b.Y ? 1.0f : 0.0f, b.Z ? 1.0f : 0.0f, b.W ? 1.0f : 0.0f);
}
// Source: https://thebookofshaders.com/glossary/?search=smoothstep
/// Returns a smooth Hermite interpolation between 0 and 1, if x is in the range [min, max].
/// @returns Returns 0 if x is less than min; 1 if x is greater than max; otherwise, a value between 0 and 1 if x is in the range [min, max].
public static float smoothstep(float min, float max, float x)
{
float t = clamp((x - min) / (max - min), 0.0f, 1.0f);
return t * t * (3.0f - 2.0f * t);
}
/// Returns a smooth Hermite interpolation between 0 and 1, if x is in the range [min, max].
/// @returns Returns 0 if x is less than min; 1 if x is greater than max; otherwise, a value between 0 and 1 if x is in the range [min, max].
public static float2 smoothstep(float2 min, float2 max, float2 x)
{
float2 t = clamp((x - min) / (max - min), 0.0f, 1.0f);
return t * t * (3.0f - 2.0f * t);
}
/// Returns a smooth Hermite interpolation between 0 and 1, if x is in the range [min, max].
/// @returns Returns 0 if x is less than min; 1 if x is greater than max; otherwise, a value between 0 and 1 if x is in the range [min, max].
public static float3 smoothstep(float3 min, float3 max, float3 x)
{
float3 t = clamp((x - min) / (max - min), 0.0f, 1.0f);
return t * t * (3.0f - 2.0f * t);
}
/// Returns a smooth Hermite interpolation between 0 and 1, if x is in the range [min, max].
/// @returns Returns 0 if x is less than min; 1 if x is greater than max; otherwise, a value between 0 and 1 if x is in the range [min, max].
public static float4 smoothstep(float4 min, float4 max, float4 x)
{
float4 t = clamp((x - min) / (max - min), 0.0f, 1.0f);
return t * t * (3.0f - 2.0f * t);
}
#endregion
#region Reject / Project
/**
* Calculates the projection of a onto b
*/
public static float2 project(float2 a, float2 b)
{
return (b * (dot(a, b) / dot(b, b)));
}
/**
* Calculates the projection of a onto b
*/
public static float3 project(float3 a, float3 b)
{
return (b * (dot(a, b) / dot(b, b)));
}
/**
* Calculates the projection of a onto b
*/
public static float4 project(float4 a, float4 b)
{
return (b * (dot(a, b) / dot(b, b)));
}
/**
* Calculates the rejection of a from b
*/
public static float2 reject(float2 a, float2 b)
{
return (a - b * (dot(a, b) / dot(b, b)));
}
/**
* Calculates the rejection of a from b
*/
public static float3 reject(float3 a, float3 b)
{
return (a - b * (dot(a, b) / dot(b, b)));
}
/**
* Calculates the rejection of a from b
*/
public static float4 reject(float4 a, float4 b)
{
return (a - b * (dot(a, b) / dot(b, b)));
}
#endregion
#region dot
@@ -495,20 +809,7 @@ static class FancyMath
left.X * right.Y - left.Y * right.X);
}
#region Degrees / Radians
public static float2 toDegrees(float2 radians) => radians * MathHelper.RadToDeg;
public static float3 toDegrees(float3 radians) => radians * MathHelper.RadToDeg;
public static float4 toDegrees(float4 radians) => radians * MathHelper.RadToDeg;
public static float2 toRadians(float2 degrees) => degrees * MathHelper.DegToRad;
public static float3 toRadians(float3 degrees) => degrees * MathHelper.DegToRad;
public static float4 toRadians(float4 degrees) => degrees * MathHelper.DegToRad;
#endregion
// transpose und determinante für Matrizen
// sin, cos, tan, asin, acos, atan, atan2, cosh, sinh, tanh
}
+13 -13
View File
@@ -30,19 +30,19 @@ namespace GlitchyEngine.Math
}
// Converts the given radians to degrees
public static Vector2 ToDegrees(Vector2 radians)
public static float2 ToDegrees(float2 radians)
{
return radians * RadToDeg;
}
// Converts the given radians to degrees
public static Vector3 ToDegrees(Vector3 radians)
public static float3 ToDegrees(float3 radians)
{
return radians * RadToDeg;
}
// Converts the given radians to degrees
public static Vector4 ToDegrees(Vector4 radians)
public static float4 ToDegrees(float4 radians)
{
return radians * RadToDeg;
}
@@ -54,19 +54,19 @@ namespace GlitchyEngine.Math
}
// Converts the given degrees to radians
public static Vector2 ToRadians(Vector2 degrees)
public static float2 ToRadians(float2 degrees)
{
return degrees * DegToRad;
}
// Converts the given degrees to radians
public static Vector3 ToRadians(Vector3 degrees)
public static float3 ToRadians(float3 degrees)
{
return degrees * DegToRad;
}
// Converts the given degrees to radians
public static Vector4 ToRadians(Vector4 degrees)
public static float4 ToRadians(float4 degrees)
{
return degrees * DegToRad;
}
@@ -79,24 +79,24 @@ namespace GlitchyEngine.Math
}
/// Returns the point that lies on the unit circle at the specified angle.
public static Vector2 CirclePoint(float angle, float radius = 1.0f)
public static float2 CirclePoint(float angle, float radius = 1.0f)
{
return .(Math.Cos(angle), Math.Sin(angle)) * radius;
}
public static Vector2 Pow(Vector2 v, float p)
public static float2 Pow(float2 v, float p)
{
return Vector2(Math.Pow(v.X, p), Math.Pow(v.Y, p));
return float2(Math.Pow(v.X, p), Math.Pow(v.Y, p));
}
public static Vector3 Pow(Vector3 v, float p)
public static float3 Pow(float3 v, float p)
{
return Vector3(Math.Pow(v.X, p), Math.Pow(v.Y, p), Math.Pow(v.Z, p));
return float3(Math.Pow(v.X, p), Math.Pow(v.Y, p), Math.Pow(v.Z, p));
}
public static Vector4 Pow(Vector4 v, float p)
public static float4 Pow(float4 v, float p)
{
return Vector4(Math.Pow(v.X, p), Math.Pow(v.Y, p), Math.Pow(v.Z, p), Math.Pow(v.W, p));
return float4(Math.Pow(v.X, p), Math.Pow(v.Y, p), Math.Pow(v.Z, p), Math.Pow(v.W, p));
}
}
}
+81 -84
View File
@@ -21,7 +21,7 @@ public struct Matrix
public using Values V;
public float[4][4] Values;
public Vector4[4] Columns;
public float4[4] Columns;
/// Creates a new zero-matrix.
public this() => this = default;
@@ -53,7 +53,7 @@ public struct Matrix
}
/// Creates a new matrix and initializes it with the given column-vectors.
public this(Vector4 c0, Vector4 c1, Vector4 c2, Vector4 c3)
public this(float4 c0, float4 c1, float4 c2, float4 c3)
{
Columns[0] = c0;
Columns[1] = c1;
@@ -61,47 +61,47 @@ public struct Matrix
Columns[3] = c3;
}
public ref Vector3 Right
public ref float3 Right
{
[Inline]
get
{
#unwarn
return ref *(Vector3*)&Columns[0];
return ref *(float3*)&Columns[0];
}
}
public ref Vector3 Up
public ref float3 Up
{
[Inline]
get
{
#unwarn
return ref *(Vector3*)&Columns[1];
return ref *(float3*)&Columns[1];
}
}
public ref Vector3 Forward
public ref float3 Forward
{
[Inline]
get
{
#unwarn
return ref *(Vector3*)&Columns[2];
return ref *(float3*)&Columns[2];
}
}
public ref Vector3 Translation
public ref float3 Translation
{
[Inline]
get
{
#unwarn
return ref *(Vector3*)&Columns[3];
return ref *(float3*)&Columns[3];
}
}
public Vector3 Scale
public float3 Scale
{
[Inline]
get => .(_11, _22, _33);
@@ -134,12 +134,12 @@ public struct Matrix
}
}
public ref Vector4 this[int column]
public ref float4 this[int column]
{
get
{
#unwarn
return ref *(Vector4*)&Columns[column];
return ref *(float4*)&Columns[column];
}
@@ -150,7 +150,7 @@ public struct Matrix
Internal.ThrowIndexOutOfRange();
#unwarn
return ref *(Vector4*)&Columns[column];
return ref *(float4*)&Columns[column];
}
}
@@ -351,12 +351,12 @@ public struct Matrix
/**
* Multiplies a matrix and a column-vector resulting in a column vector.
*/
public static Vector4 operator *(Matrix matrix, Vector4 columnVector)
public static float4 operator *(Matrix matrix, float4 columnVector)
{
#unwarn
var m = &matrix.V;
Vector4 result = ?;
float4 result = ?;
result.X = (m._11 * columnVector.X) + (m._12 * columnVector.Y) + (m._13 * columnVector.Z) + (m._14 * columnVector.W);
result.Y = (m._21 * columnVector.X) + (m._22 * columnVector.Y) + (m._23 * columnVector.Z) + (m._24 * columnVector.W);
result.Z = (m._31 * columnVector.X) + (m._32 * columnVector.Y) + (m._33 * columnVector.Z) + (m._34 * columnVector.W);
@@ -367,12 +367,12 @@ public struct Matrix
/**
* Multiplies a row-vector and a matrix resulting in a row vector.
*/
public static Vector4 operator *(Vector4 rowVector, Matrix matrix)
public static float4 operator *(float4 rowVector, Matrix matrix)
{
#unwarn
var m = &matrix.V;
Vector4 result = ?;
float4 result = ?;
result.X = (rowVector.X * m._11) + (rowVector.Y * m._21) + (rowVector.Z * m._31) + (rowVector.W * m._41);
result.Y = (rowVector.X * m._12) + (rowVector.Y * m._22) + (rowVector.Z * m._32) + (rowVector.W * m._42);
result.Z = (rowVector.X * m._13) + (rowVector.Y * m._23) + (rowVector.Z * m._33) + (rowVector.W * m._43);
@@ -406,7 +406,7 @@ public struct Matrix
0, 0, 0, 1);
}
public static Matrix Scaling(Vector3 scale)
public static Matrix Scaling(float3 scale)
{
return .(scale.X, 0, 0, 0,
0, scale.Y, 0, 0,
@@ -422,7 +422,7 @@ public struct Matrix
0, 0, 0, 1);
}
public static Matrix Translation(Vector3 translation)
public static Matrix Translation(float3 translation)
{
return .(1, 0, 0, translation.X,
0, 1, 0, translation.Y,
@@ -470,25 +470,22 @@ public struct Matrix
* @param up A vector defining the up direction of the camera.
* @returns a view matrix.
*/
public static Matrix LookAt(Vector3 position, Vector3 target, Vector3 up)
public static Matrix LookAt(float3 position, float3 target, float3 up)
{
Vector3 forward = target - position;
forward.Normalize();
float3 forward = normalize(target - position);
Vector3 right = Vector3.Cross(up, forward);
right.Normalize();
float3 right = normalize(cross(up, forward));
Vector3 newUp = Vector3.Cross(forward, right);
newUp.Normalize();
float3 newUp = normalize(cross(forward, right));
Matrix result = .Identity;
result.Forward = forward;
result.Up = up;
result.Right = right;
result.Translation.X = -Vector3.Dot(position, right);
result.Translation.Y = -Vector3.Dot(position, up);
result.Translation.Z = -Vector3.Dot(position, forward);
result.Translation.X = -dot(position, right);
result.Translation.Y = -dot(position, up);
result.Translation.Z = -dot(position, forward);
return result;
}
@@ -511,22 +508,22 @@ public struct Matrix
{
// From: Lengyel, Eric. Foundations of Game Engine Development, Volume 1: Mathematics (S.61). Kindle-Version.
Vector3 a = *(Vector3*)&Columns[0];
Vector3 b = *(Vector3*)&Columns[1];
Vector3 c = *(Vector3*)&Columns[2];
Vector3 d = *(Vector3*)&Columns[3];
float3 a = *(float3*)&Columns[0];
float3 b = *(float3*)&Columns[1];
float3 c = *(float3*)&Columns[2];
float3 d = *(float3*)&Columns[3];
float x = this[3, 0];
float y = this[3, 1];
float z = this[3, 2];
float w = this[3, 3];
Vector3 s = Vector3.Cross(a, b);
Vector3 t = Vector3.Cross(c, d);
Vector3 u = y * a - x * b;
Vector3 v = w * c - z * d;
float3 s = cross(a, b);
float3 t = cross(c, d);
float3 u = y * a - x * b;
float3 v = w * c - z * d;
return Vector3.Dot(s, v) + Vector3.Dot(t, u);
return dot(s, v) + dot(t, u);
}
/**
@@ -537,39 +534,39 @@ public struct Matrix
// From: Lengyel, Eric. Foundations of Game Engine Development, Volume 1: Mathematics (S.61). Kindle-Version.
#unwarn
Vector3 a = *(Vector3*)&Columns[0];
float3 a = *(float3*)&Columns[0];
#unwarn
Vector3 b = *(Vector3*)&Columns[1];
float3 b = *(float3*)&Columns[1];
#unwarn
Vector3 c = *(Vector3*)&Columns[2];
float3 c = *(float3*)&Columns[2];
#unwarn
Vector3 d = *(Vector3*)&Columns[3];
float3 d = *(float3*)&Columns[3];
float x = this[3, 0];
float y = this[3, 1];
float z = this[3, 2];
float w = this[3, 3];
Vector3 s = Vector3.Cross(a, b);
Vector3 t = Vector3.Cross(c, d);
Vector3 u = y * a - x * b;
Vector3 v = w * c - z * d;
float3 s = cross(a, b);
float3 t = cross(c, d);
float3 u = y * a - x * b;
float3 v = w * c - z * d;
float invDet = 1.0f / (Vector3.Dot(s, v) + Vector3.Dot(t, u));
float invDet = 1.0f / (dot(s, v) + dot(t, u));
s *= invDet;
t *= invDet;
u *= invDet;
v *= invDet;
Vector3 r0 = Vector3.Cross(b, v) + t * y;
Vector3 r1 = Vector3.Cross(v, a) - t * x;
Vector3 r2 = Vector3.Cross(d, u) + s * w;
Vector3 r3 = Vector3.Cross(u, c) - s * z;
return .(r0.X, r0.Y, r0.Z, -Vector3.Dot(b, t),
r1.X, r1.Y, r1.Z, Vector3.Dot(a, t),
r2.X, r2.Y, r2.Z, -Vector3.Dot(d, s),
r3.X, r3.Y, r3.Z, Vector3.Dot(c, s));
float3 r0 = cross(b, v) + t * y;
float3 r1 = cross(v, a) - t * x;
float3 r2 = cross(d, u) + s * w;
float3 r3 = cross(u, c) - s * z;
return .(r0.X, r0.Y, r0.Z, -dot(b, t),
r1.X, r1.Y, r1.Z, dot(a, t),
r2.X, r2.Y, r2.Z, -dot(d, s),
r3.X, r3.Y, r3.Z, dot(c, s));
}
/// Calculates the inverse of the matrix.
@@ -578,39 +575,39 @@ public struct Matrix
// From: Lengyel, Eric. Foundations of Game Engine Development, Volume 1: Mathematics (S.61). Kindle-Version.
#unwarn
Vector3 a = *(Vector3*)&matrix.Columns[0];
float3 a = *(float3*)&matrix.Columns[0];
#unwarn
Vector3 b = *(Vector3*)&matrix.Columns[1];
float3 b = *(float3*)&matrix.Columns[1];
#unwarn
Vector3 c = *(Vector3*)&matrix.Columns[2];
float3 c = *(float3*)&matrix.Columns[2];
#unwarn
Vector3 d = *(Vector3*)&matrix.Columns[3];
float3 d = *(float3*)&matrix.Columns[3];
float x = matrix[3, 0];
float y = matrix[3, 1];
float z = matrix[3, 2];
float w = matrix[3, 3];
Vector3 s = Vector3.Cross(a, b);
Vector3 t = Vector3.Cross(c, d);
Vector3 u = y * a - x * b;
Vector3 v = w * c - z * d;
float3 s = cross(a, b);
float3 t = cross(c, d);
float3 u = y * a - x * b;
float3 v = w * c - z * d;
float invDet = 1.0f / (Vector3.Dot(s, v) + Vector3.Dot(t, u));
float invDet = 1.0f / (dot(s, v) + dot(t, u));
s *= invDet;
t *= invDet;
u *= invDet;
v *= invDet;
Vector3 r0 = Vector3.Cross(b, v) + t * y;
Vector3 r1 = Vector3.Cross(v, a) - t * x;
Vector3 r2 = Vector3.Cross(d, u) + s * w;
Vector3 r3 = Vector3.Cross(u, c) - s * z;
return .(r0.X, r0.Y, r0.Z, -Vector3.Dot(b, t),
r1.X, r1.Y, r1.Z, Vector3.Dot(a, t),
r2.X, r2.Y, r2.Z, -Vector3.Dot(d, s),
r3.X, r3.Y, r3.Z, Vector3.Dot(c, s));
float3 r0 = cross(b, v) + t * y;
float3 r1 = cross(v, a) - t * x;
float3 r2 = cross(d, u) + s * w;
float3 r3 = cross(u, c) - s * z;
return .(r0.X, r0.Y, r0.Z, -dot(b, t),
r1.X, r1.Y, r1.Z, dot(a, t),
r2.X, r2.Y, r2.Z, -dot(d, s),
r3.X, r3.Y, r3.Z, dot(c, s));
}
/**
@@ -792,9 +789,9 @@ public struct Matrix
*/
public void Orthogonalize() mut
{
Columns[1] -= .Project(Columns[1], Columns[0]);
Columns[2] -= .Project(Columns[2], Columns[0]) + .Project(Columns[2], Columns[1]);
Columns[3] -= .Project(Columns[3], Columns[0]) + .Project(Columns[3], Columns[1]) + .Project(Columns[3], Columns[2]);
Columns[1] -= project(Columns[1], Columns[0]);
Columns[2] -= project(Columns[2], Columns[0]) + project(Columns[2], Columns[1]);
Columns[3] -= project(Columns[3], Columns[0]) + project(Columns[3], Columns[1]) + project(Columns[3], Columns[2]);
}
/**
@@ -802,10 +799,10 @@ public struct Matrix
*/
public void Orthonormalize() mut
{
Columns[0].Normalize();
Columns[1] = .Normalize(.Reject(Columns[1], Columns[0]));
Columns[2] = .Normalize(.Reject(.Reject(Columns[2], Columns[0]), Columns[1]));
Columns[3] = .Normalize(.Reject(.Reject(.Reject(Columns[2], Columns[0]), Columns[1]), Columns[2]));
Columns[0] = normalize(Columns[0]);
Columns[1] = normalize(reject(Columns[1], Columns[0]));
Columns[2] = normalize(reject(reject(Columns[2], Columns[0]), Columns[1]));
Columns[3] = normalize(reject(reject(reject(Columns[2], Columns[0]), Columns[1]), Columns[2]));
}
public static Self RotationQuaternion(Quaternion rotation)
{
@@ -845,7 +842,7 @@ public struct Matrix
return result;
}
public static void Decompose(Self matrix, out Vector3 position, out Quaternion rotation, out Vector3 scale)
public static void Decompose(Self matrix, out float3 position, out Quaternion rotation, out float3 scale)
{
var matrix;
@@ -857,9 +854,9 @@ public struct Matrix
// TODO: this doesn't detect mirroring
// Extract scaling from matrix
scale.X = (*(Vector3*)&matrix.Columns[0]).Magnitude();
scale.Y = (*(Vector3*)&matrix.Columns[1]).Magnitude();
scale.Z = (*(Vector3*)&matrix.Columns[2]).Magnitude();
scale.X = length(*(float3*)&matrix.Columns[0]);
scale.Y = length(*(float3*)&matrix.Columns[1]);
scale.Z = length(*(float3*)&matrix.Columns[2]);
if(MathHelper.IsZero(scale.X) || MathHelper.IsZero(scale.Y) || MathHelper.IsZero(scale.Z))
{
+17 -17
View File
@@ -22,7 +22,7 @@ namespace GlitchyEngine.Math
W = w;
}
public this(Vector3 xy, float z, float w)
public this(float3 xy, float z, float w)
{
X = xy.X;
Y = xy.Y;
@@ -30,7 +30,7 @@ namespace GlitchyEngine.Math
W = w;
}
public this(Vector3 xyz, float w)
public this(float3 xyz, float w)
{
X = xyz.X;
Y = xyz.Y;
@@ -38,7 +38,7 @@ namespace GlitchyEngine.Math
W = w;
}
public this(Vector4 vector)
public this(float4 vector)
{
X = vector.X;
Y = vector.Y;
@@ -46,7 +46,7 @@ namespace GlitchyEngine.Math
W = vector.W;
}
public Vector3 Vector
public float3 Vector
{
get => .(X, Y, Z);
set mut
@@ -195,11 +195,11 @@ namespace GlitchyEngine.Math
{
Quaternion result;
Vector3 v = l.Vector * r.Vector + (l.W * r.Vector) + (r.W * l.Vector);
float3 v = l.Vector * r.Vector + (l.W * r.Vector) + (r.W * l.Vector);
result.X = v.X;
result.Y = v.Y;
result.Z = v.Z;
result.W = (l.W * r.W) - Vector3.Dot(l.Vector, r.Vector);
result.W = (l.W * r.W) - dot(l.Vector, r.Vector);
return result;
}
@@ -220,11 +220,11 @@ namespace GlitchyEngine.Math
[Inline]
#unwarn
public static implicit operator Vector4(in Self value) => *(Vector4*)&value;
public static implicit operator float4(in Self value) => *(float4*)&value;
[Inline]
#unwarn
public static implicit operator Quaternion(in Vector4 value) => *(Quaternion*)&value;
public static implicit operator Quaternion(in float4 value) => *(Quaternion*)&value;
public static bool operator ==(Quaternion l, Quaternion r)
{
@@ -236,7 +236,7 @@ namespace GlitchyEngine.Math
return l.X != r.X && l.Y != r.Y && l.Z != r.Z && l.W != r.W;
}
public (Vector3 Axis, float Angle) ToAxisAngle()
public (float3 Axis, float Angle) ToAxisAngle()
{
// scalar part = cos(θ/2)
// So, we can extract the angle directly.
@@ -247,12 +247,12 @@ namespace GlitchyEngine.Math
// We assume quaternion is unit length, so subtracting w^2 gives us length of just vector part (aka sin(θ/2)).
float length = Math.Sqrt(1.0f - (W * W));
Vector3 axis;
float3 axis;
// Normalize vector part to get the axis!
if(length == 0)
{
axis = Vector3.Zero;
axis = float3.Zero;
}
else
{
@@ -265,9 +265,9 @@ namespace GlitchyEngine.Math
return (axis, angle);
}
public static Quaternion FromAxisAngle(Vector3 axis, float angle)
public static Quaternion FromAxisAngle(float3 axis, float angle)
{
float lengthSq = axis.MagnitudeSquared();
float lengthSq = lengthSq(axis);
if(lengthSq == 0)
{
@@ -316,11 +316,11 @@ namespace GlitchyEngine.Math
return result;
}
public static Vector3 ToEulerAngles(Quaternion q)
public static float3 ToEulerAngles(Quaternion q)
{
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/
Vector3 result;
float3 result;
float sqw = q.W*q.W;
float sqx = q.X*q.X;
@@ -354,7 +354,7 @@ namespace GlitchyEngine.Math
float y = 2.0f * (Y * Z + W * X);
float x = W * W - X * X - Y * Y + Z * Z;
if (Vector2(x, y).Equals(.Zero)) //avoid atan2(0,0) - handle singularity - Matiis
if (float2(x, y).Equals(.Zero)) //avoid atan2(0,0) - handle singularity - Matiis
return 2.0f * Math.Atan2(X, W);
return Math.Atan2(y, x);
@@ -370,7 +370,7 @@ namespace GlitchyEngine.Math
float y = 2.0f * (X * Y + W * Z);
float x = W * W + X * X - Y * Y - Z * Z;
if (Vector2(x, y).Equals(.Zero)) //avoid atan2(0,0) - handle singularity - Matiis
if (float2(x, y).Equals(.Zero)) //avoid atan2(0,0) - handle singularity - Matiis
return 0;
return Math.Atan2(y, x);
+55 -55
View File
@@ -5,20 +5,20 @@ namespace GlitchyEngine.Math
{
[BonTarget]
[SwizzleVector(3, "GlitchyEngine.Math.Vector")]
public struct Vector3
public struct float3
{
public const Vector3 Zero = .(0f, 0f, 0f);
public const Vector3 UnitX = .(1f, 0f, 0f);
public const Vector3 UnitY = .(0f, 1f, 0f);
public const Vector3 UnitZ = .(0f, 0f, 1f);
public const Vector3 One = .(1f, 1f, 1f);
public const float3 Zero = .(0f, 0f, 0f);
public const float3 UnitX = .(1f, 0f, 0f);
public const float3 UnitY = .(0f, 1f, 0f);
public const float3 UnitZ = .(0f, 0f, 1f);
public const float3 One = .(1f, 1f, 1f);
public const Vector3 Forward = .(0f, 0f, 1f);
public const Vector3 Backward = .(0f, 0f, -1f);
public const Vector3 Left = .(-1f, 0f, 0f);
public const Vector3 Right = .(1f, 0f, 0f);
public const Vector3 Up = .(0f, 1f, 0f);
public const Vector3 Down = .(0f, -1f, 0f);
public const float3 Forward = .(0f, 0f, 1f);
public const float3 Backward = .(0f, 0f, -1f);
public const float3 Left = .(-1f, 0f, 0f);
public const float3 Right = .(1f, 0f, 0f);
public const float3 Up = .(0f, 1f, 0f);
public const float3 Down = .(0f, -1f, 0f);
public const int ComponentCount = 3;
@@ -47,14 +47,14 @@ namespace GlitchyEngine.Math
Z = z;
}
public this(Vector3 value)
public this(float3 value)
{
X = value.X;
Y = value.Y;
Z = value.Z;
}
public this(Vector4 value)
public this(float4 value)
{
X = value.X;
Y = value.Y;
@@ -131,7 +131,7 @@ namespace GlitchyEngine.Math
/// Returns a copy of this Vector with a magnitude of 1.
[Checked]
public Vector3 Normalized()
public float3 Normalized()
{
if(this == .Zero)
return .Zero;
@@ -140,28 +140,28 @@ namespace GlitchyEngine.Math
}
/// Returns a copy of this Vector with a magnitude of 1.
public Vector3 Normalized()
public float3 Normalized()
{
return this / Magnitude();
}
/// Returns a copy of the given Vector with a magnitude of 1.
public static Vector3 Normalize(Vector3 v)
public static float3 Normalize(float3 v)
{
return v / v.Magnitude();
}
/// Calculates the dot product of two vectors.
public static float Dot(Vector3 l, Vector3 r) => l.X * r.X + l.Y * r.Y + l.Z * r.Z;
public static float Dot(float3 l, float3 r) => l.X * r.X + l.Y * r.Y + l.Z * r.Z;
/// Calculates the distance between two vectors.
public static float Distance(Vector3 a, Vector3 b) => (a - b).[Inline]Magnitude();
public static float Distance(float3 a, float3 b) => (a - b).[Inline]Magnitude();
/// Calculates the squared distance between two vectors.
public static float DistanceSquared(Vector3 a, Vector3 b) => (a - b).[Inline]MagnitudeSquared();
public static float DistanceSquared(float3 a, float3 b) => (a - b).[Inline]MagnitudeSquared();
/// Calculates the cross product of two vectors.
public static Vector3 Cross(Vector3 l, Vector3 r)
public static float3 Cross(float3 l, float3 r)
{
return .(l.Y * r.Z - l.Z * r.Y,
l.Z * r.X - l.X * r.Z,
@@ -169,20 +169,20 @@ namespace GlitchyEngine.Math
}
/// Calculates the projection of a onto b.
public static Vector3 Project(Vector3 a, Vector3 b)
public static float3 Project(float3 a, float3 b)
{
return (b * (Dot(a, b) / Dot(b, b)));
}
/// Calculates the rejection of a from b.
public static Vector3 Reject(Vector3 a, Vector3 b)
public static float3 Reject(float3 a, float3 b)
{
return (a - b * (Dot(a, b) / Dot(b, b)));
}
public static Vector3 Floor(Vector3 value) => .(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z));
public static float3 Floor(float3 value) => .(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z));
public static Vector3 Ceiling(Vector3 value) => .(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z));
public static float3 Ceiling(float3 value) => .(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z));
/**
* Interpolates linearly between two given vectors.
@@ -192,18 +192,18 @@ namespace GlitchyEngine.Math
* (0 means a will be returned, 1 means b will be returned.)
* @returns The resulting linear interpolation.
*/
public static Vector3 Lerp(Vector3 a, Vector3 b, float interpolationValue)
public static float3 Lerp(float3 a, float3 b, float interpolationValue)
{
return a + interpolationValue * (b - a);
}
public static Vector3 Min(Vector3 a, Vector3 b) => .(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Min(a.Z, b.Z));
public static float3 Min(float3 a, float3 b) => .(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Min(a.Z, b.Z));
public static Vector3 Max(Vector3 a, Vector3 b) => .(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y), Math.Min(a.Z, b.Z));
public static float3 Max(float3 a, float3 b) => .(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y), Math.Min(a.Z, b.Z));
public static Vector3 Abs(Vector3 v) => .(Math.Abs(v.X), Math.Abs(v.Y), Math.Abs(v.Z));
public static float3 Abs(float3 v) => .(Math.Abs(v.X), Math.Abs(v.Y), Math.Abs(v.Z));
public static Vector3 Clamp(Vector3 v, Vector3 min, Vector3 max)
public static float3 Clamp(float3 v, float3 min, float3 max)
{
return .(Math.Clamp(v.X, min.X, max.X),
Math.Clamp(v.Y, min.Y, max.Y),
@@ -216,7 +216,7 @@ namespace GlitchyEngine.Math
// Addition
public void operator +=(Vector3 value) mut
public void operator +=(float3 value) mut
{
X += value.X;
Y += value.Y;
@@ -232,7 +232,7 @@ namespace GlitchyEngine.Math
// Subtraction
public void operator -=(Vector3 value) mut
public void operator -=(float3 value) mut
{
X -= value.X;
Y -= value.Y;
@@ -248,7 +248,7 @@ namespace GlitchyEngine.Math
// Multiplication
public void operator *=(Vector3 value) mut
public void operator *=(float3 value) mut
{
X *= value.X;
Y *= value.Y;
@@ -264,7 +264,7 @@ namespace GlitchyEngine.Math
// Division
public void operator /=(Vector3 value) mut
public void operator /=(float3 value) mut
{
X /= value.X;
Y /= value.Y;
@@ -285,57 +285,57 @@ namespace GlitchyEngine.Math
// Addition
public static Vector3 operator +(Vector3 left, Vector3 right) => Vector3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
public static float3 operator +(float3 left, float3 right) => float3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
public static Vector3 operator +(Vector3 value, float scalar) => Vector3(value.X + scalar, value.Y + scalar, value.Z + scalar);
public static float3 operator +(float3 value, float scalar) => float3(value.X + scalar, value.Y + scalar, value.Z + scalar);
public static Vector3 operator +(float scalar, Vector3 value) => Vector3(scalar + value.X, scalar + value.Y, scalar + value.Z);
public static float3 operator +(float scalar, float3 value) => float3(scalar + value.X, scalar + value.Y, scalar + value.Z);
public static Vector3 operator +(Vector3 value) => value;
public static float3 operator +(float3 value) => value;
// Subtraction
public static Vector3 operator -(Vector3 left, Vector3 right) => Vector3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
public static float3 operator -(float3 left, float3 right) => float3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
public static Vector3 operator -(Vector3 value, float scalar) => Vector3(value.X - scalar, value.Y - scalar, value.Z - scalar);
public static float3 operator -(float3 value, float scalar) => float3(value.X - scalar, value.Y - scalar, value.Z - scalar);
public static Vector3 operator -(float scalar, Vector3 value) => Vector3(scalar - value.X, scalar - value.Y, scalar - value.Z);
public static float3 operator -(float scalar, float3 value) => float3(scalar - value.X, scalar - value.Y, scalar - value.Z);
public static Vector3 operator -(Vector3 value) => Vector3(-value.X, -value.Y, -value.Z);
public static float3 operator -(float3 value) => float3(-value.X, -value.Y, -value.Z);
// Multiplication
public static Vector3 operator *(Vector3 left, Vector3 right) => Vector3(left.X * right.X, left.Y * right.Y, left.Z * right.Z);
public static float3 operator *(float3 left, float3 right) => float3(left.X * right.X, left.Y * right.Y, left.Z * right.Z);
public static Vector3 operator *(Vector3 value, float scalar) => Vector3(value.X * scalar, value.Y * scalar, value.Z * scalar);
public static float3 operator *(float3 value, float scalar) => float3(value.X * scalar, value.Y * scalar, value.Z * scalar);
public static Vector3 operator *(float scalar, Vector3 value) => Vector3(scalar * value.X, scalar * value.Y, scalar * value.Z);
public static float3 operator *(float scalar, float3 value) => float3(scalar * value.X, scalar * value.Y, scalar * value.Z);
// Division
public static Vector3 operator /(Vector3 left, Vector3 right) => Vector3(left.X / right.X, left.Y / right.Y, left.Z / right.Z);
public static float3 operator /(float3 left, float3 right) => float3(left.X / right.X, left.Y / right.Y, left.Z / right.Z);
public static Vector3 operator /(Vector3 value, float scalar)
public static float3 operator /(float3 value, float scalar)
{
float inv = 1.0f / scalar;
return Vector3(value.X * inv, value.Y * inv, value.Z * inv);
return float3(value.X * inv, value.Y * inv, value.Z * inv);
}
public static Vector3 operator /(float scalar, Vector3 value) => Vector3(scalar / value.X, scalar / value.Y, scalar / value.Z);
public static float3 operator /(float scalar, float3 value) => float3(scalar / value.X, scalar / value.Y, scalar / value.Z);
// Modulo
public static Vector3 operator %(Vector3 left, Vector3 right) => Vector3(left.X % right.X, left.Y % right.Y, left.Z % right.Z);
public static float3 operator %(float3 left, float3 right) => float3(left.X % right.X, left.Y % right.Y, left.Z % right.Z);
public static Vector3 operator %(Vector3 value, float scalar) => Vector3(value.X % scalar, value.Y % scalar, value.Z % scalar);
public static float3 operator %(float3 value, float scalar) => float3(value.X % scalar, value.Y % scalar, value.Z % scalar);
public static Vector3 operator %(float scalar, Vector3 value) => Vector3(scalar % value.X, scalar % value.Y, scalar % value.Z);
public static float3 operator %(float scalar, float3 value) => float3(scalar % value.X, scalar % value.Y, scalar % value.Z);
// Equality
public static bool operator ==(Vector3 left, Vector3 right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z;
public static bool operator ==(float3 left, float3 right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z;
public static bool operator !=(Vector3 left, Vector3 right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z;
public static bool operator !=(float3 left, float3 right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z;
public override void ToString(String strBuffer) => strBuffer.AppendF("X:{0} Y:{1} Z:{2}", X, Y, Z);
@@ -344,6 +344,6 @@ namespace GlitchyEngine.Math
[Inline]
#unwarn
public static explicit operator float[3](Vector3 value) => *(float[3]*)&value;
public static explicit operator float[3](float3 value) => *(float[3]*)&value;
}
}
+38 -38
View File
@@ -5,14 +5,14 @@ namespace GlitchyEngine.Math
{
[BonTarget]
[SwizzleVector(4, "GlitchyEngine.Math.Vector")]
public struct Vector4
public struct float4
{
public const Vector4 Zero = .(0f, 0f, 0f, 0f);
public const Vector4 UnitX = .(1f, 0f, 0f, 0f);
public const Vector4 UnitY = .(0f, 1f, 0f, 0f);
public const Vector4 UnitZ = .(0f, 0f, 1f, 0f);
public const Vector4 UnitW = .(0f, 0f, 0f, 1f);
public const Vector4 One = .(1f, 1f, 1f, 1f);
public const float4 Zero = .(0f, 0f, 0f, 0f);
public const float4 UnitX = .(1f, 0f, 0f, 0f);
public const float4 UnitY = .(0f, 1f, 0f, 0f);
public const float4 UnitZ = .(0f, 0f, 1f, 0f);
public const float4 UnitW = .(0f, 0f, 0f, 1f);
public const float4 One = .(1f, 1f, 1f, 1f);
public const int ComponentCount = 4;
@@ -44,7 +44,7 @@ namespace GlitchyEngine.Math
W = value2.Y;
}
public this(Vector3 value, float w)
public this(float3 value, float w)
{
X = value.X;
Y = value.Y;
@@ -133,7 +133,7 @@ namespace GlitchyEngine.Math
this /= Magnitude();
}
public static Vector4 Normalize(Vector4 v)
public static float4 Normalize(float4 v)
{
if(v == .Zero)
return .Zero;
@@ -142,12 +142,12 @@ namespace GlitchyEngine.Math
}
[Unchecked]
public static Vector4 Normalize(Vector4 v)
public static float4 Normalize(float4 v)
{
return v / v.Magnitude();
}
public static float Dot(Vector4 l, Vector4 r)
public static float Dot(float4 l, float4 r)
{
return l.X * r.X + l.Y * r.Y + l.Z * r.Z + l.W * r.W;
}
@@ -155,7 +155,7 @@ namespace GlitchyEngine.Math
/**
* Calculates the projection of a onto b
*/
public static Vector4 Project(Vector4 a, Vector4 b)
public static float4 Project(float4 a, float4 b)
{
return (b * (Dot(a, b) / Dot(b, b)));
}
@@ -163,7 +163,7 @@ namespace GlitchyEngine.Math
/**
* Calculates the rejection of a from b
*/
public static Vector4 Reject(Vector4 a, Vector4 b)
public static float4 Reject(float4 a, float4 b)
{
return (a - b * (Dot(a, b) / Dot(b, b)));
}
@@ -176,17 +176,17 @@ namespace GlitchyEngine.Math
* (0 means a will be returned, 1 means b will be returned.)
* @returns The resulting linear interpolation.
*/
public static Vector4 Lerp(Vector4 a, Vector4 b, float interpolationValue)
public static float4 Lerp(float4 a, float4 b, float interpolationValue)
{
return a + interpolationValue * (b - a);
}
public static Vector4 Min(Vector4 a, Vector4 b)
public static float4 Min(float4 a, float4 b)
{
return .(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Min(a.Z, b.Z), Math.Min(a.W, b.W));
}
public static Vector4 Max(Vector4 a, Vector4 b)
public static float4 Max(float4 a, float4 b)
{
return .(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y), Math.Min(a.Z, b.Z), Math.Min(a.W, b.W));
}
@@ -197,7 +197,7 @@ namespace GlitchyEngine.Math
// Addition
public void operator +=(Vector4 value) mut
public void operator +=(float4 value) mut
{
X += value.X;
Y += value.Y;
@@ -215,7 +215,7 @@ namespace GlitchyEngine.Math
// Subtraction
public void operator -=(Vector4 value) mut
public void operator -=(float4 value) mut
{
X -= value.X;
Y -= value.Y;
@@ -233,7 +233,7 @@ namespace GlitchyEngine.Math
// Multiplication
public void operator *=(Vector4 value) mut
public void operator *=(float4 value) mut
{
X *= value.X;
Y *= value.Y;
@@ -260,7 +260,7 @@ namespace GlitchyEngine.Math
W *= f;
}
public void operator /=(Vector4 value) mut
public void operator /=(float4 value) mut
{
X /= value.X;
Y /= value.Y;
@@ -274,49 +274,49 @@ namespace GlitchyEngine.Math
// Addition
public static Vector4 operator +(Vector4 left, Vector4 right) => Vector4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
public static float4 operator +(float4 left, float4 right) => float4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
public static Vector4 operator +(Vector4 value, float scalar) => Vector4(value.X + scalar, value.Y + scalar, value.Z + scalar, value.W + scalar);
public static float4 operator +(float4 value, float scalar) => float4(value.X + scalar, value.Y + scalar, value.Z + scalar, value.W + scalar);
public static Vector4 operator +(float scalar, Vector4 value) => Vector4(scalar + value.X, scalar + value.Y, scalar + value.Z, scalar + value.W);
public static float4 operator +(float scalar, float4 value) => float4(scalar + value.X, scalar + value.Y, scalar + value.Z, scalar + value.W);
public static Vector4 operator +(Vector4 value) => value;
public static float4 operator +(float4 value) => value;
// Subtraction
public static Vector4 operator -(Vector4 left, Vector4 right) => Vector4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
public static float4 operator -(float4 left, float4 right) => float4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
public static Vector4 operator -(Vector4 value, float scalar) => Vector4(value.X - scalar, value.Y - scalar, value.Z - scalar, value.W - scalar);
public static float4 operator -(float4 value, float scalar) => float4(value.X - scalar, value.Y - scalar, value.Z - scalar, value.W - scalar);
public static Vector4 operator -(float scalar, Vector4 value) => Vector4(scalar - value.X, scalar - value.Y, scalar - value.Z, scalar - value.W);
public static float4 operator -(float scalar, float4 value) => float4(scalar - value.X, scalar - value.Y, scalar - value.Z, scalar - value.W);
public static Vector4 operator -(Vector4 value) => Vector4(-value.X, -value.Y, -value.Z, -value.W);
public static float4 operator -(float4 value) => float4(-value.X, -value.Y, -value.Z, -value.W);
// Multiplication
public static Vector4 operator *(Vector4 left, Vector4 right) => Vector4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
public static float4 operator *(float4 left, float4 right) => float4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
public static Vector4 operator *(Vector4 value, float scalar) => Vector4(value.X * scalar, value.Y * scalar, value.Z * scalar, value.W * scalar);
public static float4 operator *(float4 value, float scalar) => float4(value.X * scalar, value.Y * scalar, value.Z * scalar, value.W * scalar);
public static Vector4 operator *(float scalar, Vector4 value) => Vector4(scalar * value.X, scalar * value.Y, scalar * value.Z, scalar * value.W);
public static float4 operator *(float scalar, float4 value) => float4(scalar * value.X, scalar * value.Y, scalar * value.Z, scalar * value.W);
// Division
public static Vector4 operator /(Vector4 left, Vector4 right) => Vector4(left.X / right.X, left.Y / right.Y, left.Z / right.Z, left.W / right.W);
public static float4 operator /(float4 left, float4 right) => float4(left.X / right.X, left.Y / right.Y, left.Z / right.Z, left.W / right.W);
public static Vector4 operator /(Vector4 value, float scalar)
public static float4 operator /(float4 value, float scalar)
{
float inv = 1.0f / scalar;
return Vector4(value.X * inv, value.Y * inv, value.Z * inv, value.W * inv);
return float4(value.X * inv, value.Y * inv, value.Z * inv, value.W * inv);
}
public static Vector4 operator /(float scalar, Vector4 value) => Vector4(scalar / value.X, scalar / value.Y, scalar / value.Z, scalar / value.W);
public static float4 operator /(float scalar, float4 value) => float4(scalar / value.X, scalar / value.Y, scalar / value.Z, scalar / value.W);
// Equality
public static bool operator ==(Vector4 left, Vector4 right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z && left.W == right.W;
public static bool operator ==(float4 left, float4 right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z && left.W == right.W;
public static bool operator !=(Vector4 left, Vector4 right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z || left.W != right.W;
public static bool operator !=(float4 left, float4 right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z || left.W != right.W;
public override void ToString(String strBuffer) => strBuffer.AppendF("X:{0} Y:{1} Z:{2} W:{3}", X, Y, Z, W);
@@ -325,6 +325,6 @@ namespace GlitchyEngine.Math
[Inline]
#unwarn
public static explicit operator float[4](Vector4 value) => *(float[4]*)&value;
public static explicit operator float[4](float4 value) => *(float[4]*)&value;
}
}
+18 -4
View File
@@ -87,11 +87,11 @@ struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where
if (ComponentCount == 3)
{
GenerateVector3Constructors(type);
Generatefloat3Constructors(type);
}
else if (ComponentCount == 4)
{
GenerateVector4Constructors(type);
Generatefloat4Constructors(type);
}
}
@@ -128,7 +128,7 @@ struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where
}
[Comptime]
private void GenerateVector3Constructors(Type type)
private void Generatefloat3Constructors(Type type)
{
String baseName = type.GetName(.. scope String());
@@ -161,13 +161,27 @@ struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where
}
[Comptime]
private void GenerateVector4Constructors(Type type)
private void Generatefloat4Constructors(Type type)
{
String baseName = type.GetName(.. scope String());
// Remove number from name
baseName.RemoveFromEnd(1);
String constructor = scope $"""
public this({baseName}2 xy, {baseName}2 zw)
{{
X = xy.X;
Y = xy.Y;
Z = zw.X;
W = zw.Y;
}}
""";
Compiler.EmitTypeBody(type, constructor);
String constructor1 = scope $"""
public this({baseName}2 xy, {typeof(T)} z, {typeof(T)} w)
{{
+2 -2
View File
@@ -150,9 +150,9 @@ namespace GlitchyEngine.Math
public override void ToString(String strBuffer) => strBuffer.AppendF($"X:{X} Y:{Y}");
public static explicit operator Vector2(Int2 point) => .(point.X, point.Y);
public static explicit operator float2(Int2 point) => .(point.X, point.Y);
public static explicit operator Int2(Vector2 point) => .((int32)point.X, (int32)point.Y);
public static explicit operator Int2(float2 point) => .((int32)point.X, (int32)point.Y);
public int GetHashCode()
{
+2 -2
View File
@@ -191,9 +191,9 @@ namespace GlitchyEngine.Math
public override void ToString(String strBuffer) => strBuffer.AppendF($"X:{X} Y:{Y} Z:{Z}");
public static explicit operator Vector3(Int3 point) => .(point.X, point.Y, point.Z);
public static explicit operator float3(Int3 point) => .(point.X, point.Y, point.Z);
public static explicit operator Int3(Vector3 point) => .((int32)point.X, (int32)point.Y, (int32)point.Z);
public static explicit operator Int3(float3 point) => .((int32)point.X, (int32)point.Y, (int32)point.Z);
[Inline]
public static explicit operator Int2(in Int3 point) => *(Int2*)&point;
+2 -2
View File
@@ -216,9 +216,9 @@ namespace GlitchyEngine.Math
public override void ToString(String strBuffer) => strBuffer.AppendF($"X:{X} Y:{Y} Z:{Z} W:{W}");
public static explicit operator Vector4(Int4 point) => .(point.X, point.Y, point.Z, point.W);
public static explicit operator float4(Int4 point) => .(point.X, point.Y, point.Z, point.W);
public static explicit operator Int4(Vector4 point) => .((int32)point.X, (int32)point.Y, (int32)point.Z, (int32)point.W);
public static explicit operator Int4(float4 point) => .((int32)point.X, (int32)point.Y, (int32)point.Z, (int32)point.W);
[Inline]
public static explicit operator Int2(in Int4 point) => *(Int2*)&point;