Add shader-shaped vector constructors and trig intrinsics

This commit is contained in:
Meta Vens 2026-05-25 20:33:04 +01:00
commit 5cef6d951e
4 changed files with 40 additions and 0 deletions

View file

@ -5,6 +5,11 @@ public readonly record struct float3(float x, float y, float z)
public static readonly float3 zero = new(0.0f, 0.0f, 0.0f);
public static readonly float3 one = new(1.0f, 1.0f, 1.0f);
public float3(float2 xy, float z)
: this(xy.x, xy.y, z)
{
}
public float2 xy => new(x, y);
public float2 xz => new(x, z);
public float2 yz => new(y, z);

View file

@ -5,6 +5,16 @@ public readonly record struct float4(float x, float y, float z, float w)
public static readonly float4 zero = new(0.0f, 0.0f, 0.0f, 0.0f);
public static readonly float4 one = new(1.0f, 1.0f, 1.0f, 1.0f);
public float4(float2 xy, float z, float w)
: this(xy.x, xy.y, z, w)
{
}
public float4(float3 xyz, float w)
: this(xyz.x, xyz.y, xyz.z, w)
{
}
public float2 xy => new(x, y);
public float3 xyz => new(x, y, z);

View file

@ -8,6 +8,14 @@ public static class math
public static float radians(float degrees) => degrees * (PI / 180.0f);
public static float degrees(float radians) => radians * (180.0f / PI);
public static float sin(float value) => MathF.Sin(value);
public static float cos(float value) => MathF.Cos(value);
public static float tan(float value) => MathF.Tan(value);
public static float asin(float value) => MathF.Asin(value);
public static float acos(float value) => MathF.Acos(value);
public static float atan(float value) => MathF.Atan(value);
public static float atan2(float y, float x) => MathF.Atan2(y, x);
public static float sqrt(float value) => MathF.Sqrt(value);
public static float abs(float value) => MathF.Abs(value);
public static float2 abs(float2 value) => new(abs(value.x), abs(value.y));

View file

@ -20,6 +20,14 @@ public sealed class MathTests
Assert.Equal(new float3(4.0f, 10.0f, 18.0f), value);
}
[Fact]
public void VectorConstructorsAcceptLowerDimensionVectors()
{
Assert.Equal(new float3(1.0f, 2.0f, 3.0f), new float3(new float2(1.0f, 2.0f), 3.0f));
Assert.Equal(new float4(1.0f, 2.0f, 3.0f, 4.0f), new float4(new float2(1.0f, 2.0f), 3.0f, 4.0f));
Assert.Equal(new float4(1.0f, 2.0f, 3.0f, 4.0f), new float4(new float3(1.0f, 2.0f, 3.0f), 4.0f));
}
[Fact]
public void NormalizeProducesUnitLength()
{
@ -35,4 +43,13 @@ public sealed class MathTests
Assert.Equal(0.5f, math.smoothstep(0.0f, 1.0f, 0.5f), precision: 5);
Assert.Equal(1.0f, math.smoothstep(0.0f, 1.0f, 2.0f));
}
[Fact]
public void TrigIntrinsicsUseRadians()
{
Assert.Equal(1.0f, math.sin(math.HALF_PI), precision: 5);
Assert.Equal(-1.0f, math.cos(math.PI), precision: 5);
Assert.Equal(1.0f, math.tan(math.PI * 0.25f), precision: 5);
Assert.Equal(math.HALF_PI, math.atan2(1.0f, 0.0f), precision: 5);
}
}