Moved vector type files

This commit is contained in:
Simon Lübeß
2023-06-25 01:11:06 +02:00
parent fa2277cd99
commit 5f5da2ed35
8 changed files with 191 additions and 98 deletions
-24
View File
@@ -1,24 +0,0 @@
using Bon;
namespace GlitchyEngine.Math.FancyMath;
[BonTarget]
[Vector<bool, 2>]
struct bool2
{
}
[BonTarget]
[Vector<bool, 3>]
struct bool3
{
}
[BonTarget]
[Vector<bool, 4>]
struct bool4
{
}
-58
View File
@@ -1,58 +0,0 @@
using Bon;
using System;
namespace GlitchyEngine.Math.FancyMath;
[BonTarget]
[Vector<float, 2>]
[ComparableVector<float, 2>]
[VectorMath<float, 2>]
[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<float, 3>]
[ComparableVector<float, 3>]
[VectorMath<float, 3>]
[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<float, 4>]
[ComparableVector<float, 4>]
[VectorMath<float, 4>]
[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);
}
}
@@ -188,7 +188,9 @@ static class FancyMath
// mul // 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 // pow
@@ -234,6 +236,59 @@ static class FancyMath
#endregion #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 #region modf / frac / trunc
// Splits the value x into fractional and integer parts, each of which has the same sign as x. // Splits the value x into fractional and integer parts, each of which has the same sign as x.
@@ -1,6 +1,6 @@
using System; using System;
namespace GlitchyEngine.Math.FancyMath; namespace GlitchyEngine.Math;
[AttributeUsage(.Struct | .Class)] [AttributeUsage(.Struct | .Class)]
struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where ComponentCount : const int struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where ComponentCount : const int
@@ -13,7 +13,7 @@ struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where
{ {
GenerateFields(type); GenerateFields(type);
GenerateSingleToVectorCast(type); GenerateCasts(type);
GenerateConstructors(type); GenerateConstructors(type);
@@ -40,7 +40,7 @@ struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where
} }
[Comptime] [Comptime]
private void GenerateSingleToVectorCast(Type type) private void GenerateCasts(Type type)
{ {
String constructorBody = scope String(); String constructorBody = scope String();
@@ -52,16 +52,26 @@ struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where
constructorBody.Append("value"); constructorBody.Append("value");
} }
String cast = scope $""" String castSingleToVector = scope $"""
public static implicit operator {type}({typeof(T)} value) 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 #region Constructors
+27
View File
@@ -0,0 +1,27 @@
using Bon;
namespace GlitchyEngine.Math;
[BonTarget]
[Vector<bool, 2>]
[SwizzleVector(2, "GlitchyEngine.Math.bool")]
struct bool2
{
}
[BonTarget]
[Vector<bool, 3>]
[SwizzleVector(3, "GlitchyEngine.Math.bool")]
struct bool3
{
}
[BonTarget]
[Vector<bool, 4>]
[SwizzleVector(4, "GlitchyEngine.Math.bool")]
struct bool4
{
}
+83
View File
@@ -0,0 +1,83 @@
using Bon;
using System;
namespace GlitchyEngine.Math;
[BonTarget]
[Vector<float, 2>]
[ComparableVector<float, 2>]
[VectorMath<float, 2>]
[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<float, 3>]
[ComparableVector<float, 3>]
[VectorMath<float, 3>]
[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<float, 4>]
[ComparableVector<float, 4>]
[VectorMath<float, 4>]
[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);
}
}
@@ -1,13 +1,13 @@
using Bon; using Bon;
using System; using System;
namespace GlitchyEngine.Math.FancyMath; namespace GlitchyEngine.Math;
[BonTarget] [BonTarget]
[Vector<half, 2>] [Vector<half, 2>]
[ComparableVector<half, 2>] [ComparableVector<half, 2>]
[VectorMath<half, 2>] [VectorMath<half, 2>]
[SwizzleVector(2, "GlitchyEngine.Math.FancyMath.half")] [SwizzleVector(2, "GlitchyEngine.Math.half")]
public struct half2 public struct half2
{ {
public static explicit operator float2(half2 value) public static explicit operator float2(half2 value)
@@ -20,7 +20,7 @@ public struct half2
[Vector<half, 3>] [Vector<half, 3>]
[ComparableVector<half, 3>] [ComparableVector<half, 3>]
[VectorMath<half, 3>] [VectorMath<half, 3>]
[SwizzleVector(3, "GlitchyEngine.Math.FancyMath.half")] [SwizzleVector(3, "GlitchyEngine.Math.half")]
public struct half3 public struct half3
{ {
public static explicit operator float3(half3 value) public static explicit operator float3(half3 value)
@@ -33,7 +33,7 @@ public struct half3
[Vector<half, 4>] [Vector<half, 4>]
[ComparableVector<half, 4>] [ComparableVector<half, 4>]
[VectorMath<half, 4>] [VectorMath<half, 4>]
[SwizzleVector(4, "GlitchyEngine.Math.FancyMath.half")] [SwizzleVector(4, "GlitchyEngine.Math.half")]
public struct half4 public struct half4
{ {
public static explicit operator float4(half4 value) public static explicit operator float4(half4 value)
@@ -1,13 +1,13 @@
using Bon; using Bon;
using System; using System;
namespace GlitchyEngine.Math.FancyMath; namespace GlitchyEngine.Math;
[BonTarget] [BonTarget]
[Vector<int32, 2>] [Vector<int32, 2>]
[ComparableVector<int32, 2>] [ComparableVector<int32, 2>]
[VectorMath<int32, 2>] [VectorMath<int32, 2>]
[SwizzleVector(2, "GlitchyEngine.Math.FancyMath.int")] [SwizzleVector(2, "GlitchyEngine.Math.int")]
public struct int2 public struct int2
{ {
public static implicit operator float2(int2 value) public static implicit operator float2(int2 value)
@@ -20,7 +20,7 @@ public struct int2
[Vector<int32, 3>] [Vector<int32, 3>]
[ComparableVector<int32, 3>] [ComparableVector<int32, 3>]
[VectorMath<int32, 3>] [VectorMath<int32, 3>]
[SwizzleVector(3, "GlitchyEngine.Math.FancyMath.int")] [SwizzleVector(3, "GlitchyEngine.Math.int")]
public struct int3 public struct int3
{ {
public static implicit operator float3(int3 value) public static implicit operator float3(int3 value)
@@ -33,7 +33,7 @@ public struct int3
[Vector<int32, 4>] [Vector<int32, 4>]
[ComparableVector<int32, 4>] [ComparableVector<int32, 4>]
[VectorMath<int32, 4>] [VectorMath<int32, 4>]
[SwizzleVector(4, "GlitchyEngine.Math.FancyMath.int")] [SwizzleVector(4, "GlitchyEngine.Math.int")]
public struct int4 public struct int4
{ {
public static implicit operator float4(int4 value) public static implicit operator float4(int4 value)