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>
95 lines
3 KiB
C#
95 lines
3 KiB
C#
using System;
|
|
|
|
namespace CultMath;
|
|
|
|
/// <summary>
|
|
/// Not an HLSL type: shaders carry rotations as float4. Equality therefore stays
|
|
/// scalar (<c>==</c> returns bool) instead of following the component-wise vector rule.
|
|
/// </summary>
|
|
public struct quaternion : IEquatable<quaternion>
|
|
{
|
|
public static readonly quaternion identity = new(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
public float x;
|
|
public float y;
|
|
public float z;
|
|
public float w;
|
|
|
|
public quaternion(float x, float y, float z, float w)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
this.z = z;
|
|
this.w = w;
|
|
}
|
|
|
|
public static quaternion LookRotation(float3 forward, float3 up)
|
|
{
|
|
forward = math.normalize(forward);
|
|
up = math.normalize(up);
|
|
|
|
var right = math.normalize(math.cross(up, forward));
|
|
up = math.cross(forward, right);
|
|
|
|
var m00 = right.x;
|
|
var m01 = up.x;
|
|
var m02 = forward.x;
|
|
var m10 = right.y;
|
|
var m11 = up.y;
|
|
var m12 = forward.y;
|
|
var m20 = right.z;
|
|
var m21 = up.z;
|
|
var m22 = forward.z;
|
|
|
|
var trace = m00 + m11 + m22;
|
|
if (trace > 0.0f)
|
|
{
|
|
var s = math.sqrt(trace + 1.0f) * 2.0f;
|
|
return math.normalize(new quaternion(
|
|
(m21 - m12) / s,
|
|
(m02 - m20) / s,
|
|
(m10 - m01) / s,
|
|
0.25f * s));
|
|
}
|
|
|
|
if (m00 > m11 && m00 > m22)
|
|
{
|
|
var s = math.sqrt(1.0f + m00 - m11 - m22) * 2.0f;
|
|
return math.normalize(new quaternion(
|
|
0.25f * s,
|
|
(m01 + m10) / s,
|
|
(m02 + m20) / s,
|
|
(m21 - m12) / s));
|
|
}
|
|
|
|
if (m11 > m22)
|
|
{
|
|
var s = math.sqrt(1.0f + m11 - m00 - m22) * 2.0f;
|
|
return math.normalize(new quaternion(
|
|
(m01 + m10) / s,
|
|
0.25f * s,
|
|
(m12 + m21) / s,
|
|
(m02 - m20) / s));
|
|
}
|
|
|
|
{
|
|
var s = math.sqrt(1.0f + m22 - m00 - m11) * 2.0f;
|
|
return math.normalize(new quaternion(
|
|
(m02 + m20) / s,
|
|
(m12 + m21) / s,
|
|
0.25f * s,
|
|
(m10 - m01) / s));
|
|
}
|
|
}
|
|
|
|
public static implicit operator quaternion(float4 value) => new(value.x, value.y, value.z, value.w);
|
|
public static implicit operator float4(quaternion value) => new(value.x, value.y, value.z, value.w);
|
|
|
|
public static bool operator ==(quaternion left, quaternion right) => left.Equals(right);
|
|
public static bool operator !=(quaternion left, quaternion right) => !left.Equals(right);
|
|
|
|
public readonly bool Equals(quaternion other) => x.Equals(other.x) && y.Equals(other.y) && z.Equals(other.z) && w.Equals(other.w);
|
|
public override readonly bool Equals(object? obj) => obj is quaternion other && Equals(other);
|
|
public override readonly int GetHashCode() => HashCode.Combine(x, y, z, w);
|
|
public override readonly string ToString() => FormattableString.Invariant($"quaternion({x:R}, {y:R}, {z:R}, {w:R})");
|
|
}
|