diff --git a/GlitchyEngine/src/Math/FancyMath/bool.bf b/GlitchyEngine/src/Math/FancyMath/bool.bf deleted file mode 100644 index 26fa3d9..0000000 --- a/GlitchyEngine/src/Math/FancyMath/bool.bf +++ /dev/null @@ -1,24 +0,0 @@ -using Bon; - -namespace GlitchyEngine.Math.FancyMath; - -[BonTarget] -[Vector] -struct bool2 -{ - -} - -[BonTarget] -[Vector] -struct bool3 -{ - -} - -[BonTarget] -[Vector] -struct bool4 -{ - -} \ No newline at end of file diff --git a/GlitchyEngine/src/Math/FancyMath/float.bf b/GlitchyEngine/src/Math/FancyMath/float.bf deleted file mode 100644 index 1d0bd82..0000000 --- a/GlitchyEngine/src/Math/FancyMath/float.bf +++ /dev/null @@ -1,58 +0,0 @@ -using Bon; -using System; - -namespace GlitchyEngine.Math.FancyMath; - -[BonTarget] -[Vector] -[ComparableVector] -[VectorMath] -[SwizzleVector(2, "GlitchyEngine.Math.FancyMath.float")] -public struct float2 -{ - public static implicit operator int2(float2 value) - { - return int2((int32)value.X, (int32)value.Y); - } - - public static explicit operator half2(float2 value) - { - return half2((half)value.X, (half)value.Y); - } -} - -[BonTarget] -[Vector] -[ComparableVector] -[VectorMath] -[SwizzleVector(3, "GlitchyEngine.Math.FancyMath.float")] -public struct float3 -{ - public static implicit operator int3(float3 value) - { - return int3((int32)value.X, (int32)value.Y, (int32)value.Z); - } - - public static explicit operator half3(float3 value) - { - return half3((half)value.X, (half)value.Y, (half)value.Z); - } -} - -[BonTarget] -[Vector] -[ComparableVector] -[VectorMath] -[SwizzleVector(4, "GlitchyEngine.Math.FancyMath.float")] -public struct float4 -{ - public static implicit operator int4(float4 value) - { - return int4((int32)value.X, (int32)value.Y, (int32)value.Z, (int32)value.W); - } - - public static explicit operator half4(float4 value) - { - return half4((half)value.X, (half)value.Y, (half)value.Z, (half)value.W); - } -} \ No newline at end of file diff --git a/GlitchyEngine/src/Math/FancyMath/FancyMath.bf b/GlitchyEngine/src/Math/Math.bf similarity index 91% rename from GlitchyEngine/src/Math/FancyMath/FancyMath.bf rename to GlitchyEngine/src/Math/Math.bf index 46d33f0..3ae5cc4 100644 --- a/GlitchyEngine/src/Math/FancyMath/FancyMath.bf +++ b/GlitchyEngine/src/Math/Math.bf @@ -188,7 +188,9 @@ static class FancyMath // mul - // normalize + 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 @@ -234,6 +236,59 @@ static class FancyMath #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. diff --git a/GlitchyEngine/src/Math/FancyMath/VectorAttribute.bf b/GlitchyEngine/src/Math/VectorAttribute.bf similarity index 93% rename from GlitchyEngine/src/Math/FancyMath/VectorAttribute.bf rename to GlitchyEngine/src/Math/VectorAttribute.bf index e5e8c15..55ebb1d 100644 --- a/GlitchyEngine/src/Math/FancyMath/VectorAttribute.bf +++ b/GlitchyEngine/src/Math/VectorAttribute.bf @@ -1,6 +1,6 @@ using System; -namespace GlitchyEngine.Math.FancyMath; +namespace GlitchyEngine.Math; [AttributeUsage(.Struct | .Class)] struct VectorAttribute : Attribute, IComptimeTypeApply where ComponentCount : const int @@ -13,7 +13,7 @@ struct VectorAttribute : Attribute, IComptimeTypeApply where { GenerateFields(type); - GenerateSingleToVectorCast(type); + GenerateCasts(type); GenerateConstructors(type); @@ -40,7 +40,7 @@ struct VectorAttribute : Attribute, IComptimeTypeApply where } [Comptime] - private void GenerateSingleToVectorCast(Type type) + private void GenerateCasts(Type type) { String constructorBody = scope String(); @@ -52,16 +52,26 @@ struct VectorAttribute : Attribute, IComptimeTypeApply where constructorBody.Append("value"); } - String cast = scope $""" - public static implicit operator {type}({typeof(T)} value) + String castSingleToVector = scope $""" + public static implicit operator Self({typeof(T)} value) {{ - return {type}({constructorBody}); + return Self({constructorBody}); }} """; - Compiler.EmitTypeBody(type, cast); + Compiler.EmitTypeBody(type, castSingleToVector); + + String castVectorToArray = scope $""" + [System.Inline] + #unwarn + public static explicit operator {typeof(T)}[{ComponentCount}](Self value) => *({typeof(T)}[{ComponentCount}]*)&value; + + + """; + + Compiler.EmitTypeBody(type, castVectorToArray); } #region Constructors diff --git a/GlitchyEngine/src/Math/boolVector.bf b/GlitchyEngine/src/Math/boolVector.bf new file mode 100644 index 0000000..8edef05 --- /dev/null +++ b/GlitchyEngine/src/Math/boolVector.bf @@ -0,0 +1,27 @@ +using Bon; + +namespace GlitchyEngine.Math; + +[BonTarget] +[Vector] +[SwizzleVector(2, "GlitchyEngine.Math.bool")] +struct bool2 +{ + +} + +[BonTarget] +[Vector] +[SwizzleVector(3, "GlitchyEngine.Math.bool")] +struct bool3 +{ + +} + +[BonTarget] +[Vector] +[SwizzleVector(4, "GlitchyEngine.Math.bool")] +struct bool4 +{ + +} \ No newline at end of file diff --git a/GlitchyEngine/src/Math/floatVector.bf b/GlitchyEngine/src/Math/floatVector.bf new file mode 100644 index 0000000..851dd88 --- /dev/null +++ b/GlitchyEngine/src/Math/floatVector.bf @@ -0,0 +1,83 @@ +using Bon; +using System; + +namespace GlitchyEngine.Math; + +[BonTarget] +[Vector] +[ComparableVector] +[VectorMath] +[SwizzleVector(2, "GlitchyEngine.Math.float")] +public struct float2 +{ + public const float2 Zero = .(0f, 0f); + public const float2 UnitX = .(1f, 0f); + public const float2 UnitY = .(0f, 1f); + public const float2 One = .(1f, 1f); + + public static implicit operator int2(float2 value) + { + return int2((int32)value.X, (int32)value.Y); + } + + public static explicit operator half2(float2 value) + { + return half2((half)value.X, (half)value.Y); + } +} + +[BonTarget] +[Vector] +[ComparableVector] +[VectorMath] +[SwizzleVector(3, "GlitchyEngine.Math.float")] +public struct float3 +{ + 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 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 static implicit operator int3(float3 value) + { + return int3((int32)value.X, (int32)value.Y, (int32)value.Z); + } + + public static explicit operator half3(float3 value) + { + return half3((half)value.X, (half)value.Y, (half)value.Z); + } +} + +[BonTarget] +[Vector] +[ComparableVector] +[VectorMath] +[SwizzleVector(4, "GlitchyEngine.Math.float")] +public struct float4 +{ + 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 static implicit operator int4(float4 value) + { + return int4((int32)value.X, (int32)value.Y, (int32)value.Z, (int32)value.W); + } + + public static explicit operator half4(float4 value) + { + return half4((half)value.X, (half)value.Y, (half)value.Z, (half)value.W); + } +} \ No newline at end of file diff --git a/GlitchyEngine/src/Math/FancyMath/half.bf b/GlitchyEngine/src/Math/halfVector.bf similarity index 77% rename from GlitchyEngine/src/Math/FancyMath/half.bf rename to GlitchyEngine/src/Math/halfVector.bf index 5cf55b8..2e38cba 100644 --- a/GlitchyEngine/src/Math/FancyMath/half.bf +++ b/GlitchyEngine/src/Math/halfVector.bf @@ -1,13 +1,13 @@ using Bon; using System; -namespace GlitchyEngine.Math.FancyMath; +namespace GlitchyEngine.Math; [BonTarget] [Vector] [ComparableVector] [VectorMath] -[SwizzleVector(2, "GlitchyEngine.Math.FancyMath.half")] +[SwizzleVector(2, "GlitchyEngine.Math.half")] public struct half2 { public static explicit operator float2(half2 value) @@ -20,7 +20,7 @@ public struct half2 [Vector] [ComparableVector] [VectorMath] -[SwizzleVector(3, "GlitchyEngine.Math.FancyMath.half")] +[SwizzleVector(3, "GlitchyEngine.Math.half")] public struct half3 { public static explicit operator float3(half3 value) @@ -33,7 +33,7 @@ public struct half3 [Vector] [ComparableVector] [VectorMath] -[SwizzleVector(4, "GlitchyEngine.Math.FancyMath.half")] +[SwizzleVector(4, "GlitchyEngine.Math.half")] public struct half4 { public static explicit operator float4(half4 value) diff --git a/GlitchyEngine/src/Math/FancyMath/int.bf b/GlitchyEngine/src/Math/intVector.bf similarity index 76% rename from GlitchyEngine/src/Math/FancyMath/int.bf rename to GlitchyEngine/src/Math/intVector.bf index bf7a601..5a7222a 100644 --- a/GlitchyEngine/src/Math/FancyMath/int.bf +++ b/GlitchyEngine/src/Math/intVector.bf @@ -1,13 +1,13 @@ using Bon; using System; -namespace GlitchyEngine.Math.FancyMath; +namespace GlitchyEngine.Math; [BonTarget] [Vector] [ComparableVector] [VectorMath] -[SwizzleVector(2, "GlitchyEngine.Math.FancyMath.int")] +[SwizzleVector(2, "GlitchyEngine.Math.int")] public struct int2 { public static implicit operator float2(int2 value) @@ -20,7 +20,7 @@ public struct int2 [Vector] [ComparableVector] [VectorMath] -[SwizzleVector(3, "GlitchyEngine.Math.FancyMath.int")] +[SwizzleVector(3, "GlitchyEngine.Math.int")] public struct int3 { public static implicit operator float3(int3 value) @@ -33,7 +33,7 @@ public struct int3 [Vector] [ComparableVector] [VectorMath] -[SwizzleVector(4, "GlitchyEngine.Math.FancyMath.int")] +[SwizzleVector(4, "GlitchyEngine.Math.int")] public struct int4 { public static implicit operator float4(int4 value)