Make CultMath HLSL-shaped: writable components, bool-vector comparisons
HLSL is now CultMath's semantic authority where it defines one. - float2/3/4, int2, double2/3, bool2 and quaternion are plain structs with public mutable fields instead of record structs. Indexers and swizzles (float2.yx; float3 xy xz yx yz zy xyz xzy yzx zyx; float4 xy xyz xyw yzx) have setters. - ==, !=, <, >, <=, >= are component-wise and return bool2/3/4. Equals and GetHashCode stay value-based so vectors remain dictionary keys. quaternion is not an HLSL type and keeps a bool ==. - Scalar/vector arithmetic and comparisons get explicit overloads on both sides instead of leaning on implicit splat. - New types: bool3, bool4, int3, int4, float2x2, float3x3 (row-major like HLSL, _mRC fields, Rotate / RotateX/Y/Z / Euler with math.RotationOrder). - New intrinsics: mul (matrix-vector both sides, matrix-matrix), any, all, select (HLSL argument order), log, vector pow/sqrt/sin/cos/acos/atan2, double min/max, int3/int4/bool3/bool4/float2x2/float3x3 constructor functions. - simplex_noise is renamed snoise and gains the Ashima 3D variant; the HLSL mirror follows as cultmath_snoise(float2/float3). - Random gains NextFloat3() and NextFloat3(float3 max) on the same draw order. Tests pin snoise(float3), float3x3.Euler and float2x2.Rotate to Unity.Mathematics reference values, and an HLSL-shaped source block compiles under using static CultMath.math. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
8be174b82c
commit
d514efebba
29 changed files with 1609 additions and 75 deletions
|
|
@ -2,11 +2,22 @@ using System;
|
|||
|
||||
namespace CultMath;
|
||||
|
||||
public record struct float3(float x, float y, float z)
|
||||
public struct float3 : IEquatable<float3>
|
||||
{
|
||||
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 float x;
|
||||
public float y;
|
||||
public float z;
|
||||
|
||||
public float3(float x, float y, float z)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public float3(float2 xy, float z)
|
||||
: this(xy.x, xy.y, z)
|
||||
{
|
||||
|
|
@ -14,7 +25,7 @@ public record struct float3(float x, float y, float z)
|
|||
|
||||
public float2 xy
|
||||
{
|
||||
get => new(x, y);
|
||||
readonly get => new(x, y);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
|
|
@ -24,7 +35,7 @@ public record struct float3(float x, float y, float z)
|
|||
|
||||
public float2 xz
|
||||
{
|
||||
get => new(x, z);
|
||||
readonly get => new(x, z);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
|
|
@ -32,9 +43,19 @@ public record struct float3(float x, float y, float z)
|
|||
}
|
||||
}
|
||||
|
||||
public float2 yx
|
||||
{
|
||||
readonly get => new(y, x);
|
||||
set
|
||||
{
|
||||
y = value.x;
|
||||
x = value.y;
|
||||
}
|
||||
}
|
||||
|
||||
public float2 yz
|
||||
{
|
||||
get => new(y, z);
|
||||
readonly get => new(y, z);
|
||||
set
|
||||
{
|
||||
y = value.x;
|
||||
|
|
@ -44,7 +65,7 @@ public record struct float3(float x, float y, float z)
|
|||
|
||||
public float2 zy
|
||||
{
|
||||
get => new(z, y);
|
||||
readonly get => new(z, y);
|
||||
set
|
||||
{
|
||||
z = value.x;
|
||||
|
|
@ -52,28 +73,115 @@ public record struct float3(float x, float y, float z)
|
|||
}
|
||||
}
|
||||
|
||||
public float3 yzx => new(y, z, x);
|
||||
public float3 zyx => new(z, y, x);
|
||||
|
||||
public float this[int index] => index switch
|
||||
public float3 xyz
|
||||
{
|
||||
0 => x,
|
||||
1 => y,
|
||||
2 => z,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(index)),
|
||||
};
|
||||
readonly get => new(x, y, z);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
y = value.y;
|
||||
z = value.z;
|
||||
}
|
||||
}
|
||||
|
||||
public float3 xzy
|
||||
{
|
||||
readonly get => new(x, z, y);
|
||||
set
|
||||
{
|
||||
x = value.x;
|
||||
z = value.y;
|
||||
y = value.z;
|
||||
}
|
||||
}
|
||||
|
||||
public float3 yzx
|
||||
{
|
||||
readonly get => new(y, z, x);
|
||||
set
|
||||
{
|
||||
y = value.x;
|
||||
z = value.y;
|
||||
x = value.z;
|
||||
}
|
||||
}
|
||||
|
||||
public float3 zyx
|
||||
{
|
||||
readonly get => new(z, y, x);
|
||||
set
|
||||
{
|
||||
z = value.x;
|
||||
y = value.y;
|
||||
x = value.z;
|
||||
}
|
||||
}
|
||||
|
||||
public float this[int index]
|
||||
{
|
||||
readonly get => index switch
|
||||
{
|
||||
0 => x,
|
||||
1 => y,
|
||||
2 => z,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(index)),
|
||||
};
|
||||
set
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case 0: x = value; break;
|
||||
case 1: y = value; break;
|
||||
case 2: z = value; break;
|
||||
default: throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static implicit operator float3(float value) => new(value, value, value);
|
||||
public static explicit operator System.Numerics.Vector3(float3 value) => new(value.x, value.y, value.z);
|
||||
public static explicit operator float3(System.Numerics.Vector3 value) => new(value.X, value.Y, value.Z);
|
||||
|
||||
public override readonly string ToString() => $"float3({x:R}, {y:R}, {z:R})";
|
||||
|
||||
public static float3 operator +(float3 value) => value;
|
||||
public static float3 operator -(float3 value) => new(-value.x, -value.y, -value.z);
|
||||
|
||||
public static float3 operator +(float3 left, float3 right) => new(left.x + right.x, left.y + right.y, left.z + right.z);
|
||||
public static float3 operator +(float3 left, float right) => new(left.x + right, left.y + right, left.z + right);
|
||||
public static float3 operator +(float left, float3 right) => new(left + right.x, left + right.y, left + right.z);
|
||||
public static float3 operator -(float3 left, float3 right) => new(left.x - right.x, left.y - right.y, left.z - right.z);
|
||||
public static float3 operator -(float3 left, float right) => new(left.x - right, left.y - right, left.z - right);
|
||||
public static float3 operator -(float left, float3 right) => new(left - right.x, left - right.y, left - right.z);
|
||||
public static float3 operator *(float3 left, float3 right) => new(left.x * right.x, left.y * right.y, left.z * right.z);
|
||||
public static float3 operator *(float3 left, float right) => new(left.x * right, left.y * right, left.z * right);
|
||||
public static float3 operator *(float left, float3 right) => new(left * right.x, left * right.y, left * right.z);
|
||||
public static float3 operator /(float3 left, float3 right) => new(left.x / right.x, left.y / right.y, left.z / right.z);
|
||||
public static float3 operator /(float3 left, float right) => new(left.x / right, left.y / right, left.z / right);
|
||||
public static float3 operator /(float left, float3 right) => new(left / right.x, left / right.y, left / right.z);
|
||||
public static float3 operator %(float3 left, float3 right) => new(left.x % right.x, left.y % right.y, left.z % right.z);
|
||||
public static float3 operator %(float3 left, float right) => new(left.x % right, left.y % right, left.z % right);
|
||||
public static float3 operator %(float left, float3 right) => new(left % right.x, left % right.y, left % right.z);
|
||||
|
||||
public static bool3 operator <(float3 left, float3 right) => new(left.x < right.x, left.y < right.y, left.z < right.z);
|
||||
public static bool3 operator <(float3 left, float right) => new(left.x < right, left.y < right, left.z < right);
|
||||
public static bool3 operator <(float left, float3 right) => new(left < right.x, left < right.y, left < right.z);
|
||||
public static bool3 operator >(float3 left, float3 right) => new(left.x > right.x, left.y > right.y, left.z > right.z);
|
||||
public static bool3 operator >(float3 left, float right) => new(left.x > right, left.y > right, left.z > right);
|
||||
public static bool3 operator >(float left, float3 right) => new(left > right.x, left > right.y, left > right.z);
|
||||
public static bool3 operator <=(float3 left, float3 right) => new(left.x <= right.x, left.y <= right.y, left.z <= right.z);
|
||||
public static bool3 operator <=(float3 left, float right) => new(left.x <= right, left.y <= right, left.z <= right);
|
||||
public static bool3 operator <=(float left, float3 right) => new(left <= right.x, left <= right.y, left <= right.z);
|
||||
public static bool3 operator >=(float3 left, float3 right) => new(left.x >= right.x, left.y >= right.y, left.z >= right.z);
|
||||
public static bool3 operator >=(float3 left, float right) => new(left.x >= right, left.y >= right, left.z >= right);
|
||||
public static bool3 operator >=(float left, float3 right) => new(left >= right.x, left >= right.y, left >= right.z);
|
||||
public static bool3 operator ==(float3 left, float3 right) => new(left.x == right.x, left.y == right.y, left.z == right.z);
|
||||
public static bool3 operator ==(float3 left, float right) => new(left.x == right, left.y == right, left.z == right);
|
||||
public static bool3 operator ==(float left, float3 right) => new(left == right.x, left == right.y, left == right.z);
|
||||
public static bool3 operator !=(float3 left, float3 right) => new(left.x != right.x, left.y != right.y, left.z != right.z);
|
||||
public static bool3 operator !=(float3 left, float right) => new(left.x != right, left.y != right, left.z != right);
|
||||
public static bool3 operator !=(float left, float3 right) => new(left != right.x, left != right.y, left != right.z);
|
||||
|
||||
public readonly bool Equals(float3 other) => x.Equals(other.x) && y.Equals(other.y) && z.Equals(other.z);
|
||||
public override readonly bool Equals(object? obj) => obj is float3 other && Equals(other);
|
||||
public override readonly int GetHashCode() => HashCode.Combine(x, y, z);
|
||||
public override readonly string ToString() => FormattableString.Invariant($"float3({x:R}, {y:R}, {z:R})");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue