Move CultMath into the CultLib monorepo

This commit is contained in:
Meta Vens 2026-08-26 17:35:51 +02:00
commit ea88cfcd05
98 changed files with 3574 additions and 4 deletions

View file

@ -0,0 +1,245 @@
using System;
using System.Numerics;
namespace CultMath;
public static class BatchMath
{
private const float MinimumDistanceSquared = 1.0e-8f;
public static int LaneCount => Vector<float>.Count;
public static bool IsHardwareAccelerated => Vector.IsHardwareAccelerated;
public static void Clear(Span<float> values) => values.Clear();
public static void AddRadialFalloffAcceleration2D(
ReadOnlySpan<float> positionX,
ReadOnlySpan<float> positionY,
float centerX,
float centerY,
float strength,
float radius,
Span<float> accelerationX,
Span<float> accelerationY)
{
ValidateEqualLengths(positionX.Length, positionY.Length, accelerationX.Length, accelerationY.Length);
if (radius <= 0.0f || strength == 0.0f || positionX.Length == 0)
{
return;
}
var radiusVector = new Vector<float>(radius);
var centerXVector = new Vector<float>(centerX);
var centerYVector = new Vector<float>(centerY);
var strengthVector = new Vector<float>(strength);
var one = Vector<float>.One;
var zero = Vector<float>.Zero;
var minimumDistanceSquared = new Vector<float>(MinimumDistanceSquared);
var i = 0;
var lanes = Vector<float>.Count;
var pxBuffer = new float[lanes];
var pyBuffer = new float[lanes];
var axBuffer = new float[lanes];
var ayBuffer = new float[lanes];
for (; i <= positionX.Length - lanes; i += lanes)
{
CopySpanToArray(positionX.Slice(i, lanes), pxBuffer);
CopySpanToArray(positionY.Slice(i, lanes), pyBuffer);
CopySpanToArray(accelerationX.Slice(i, lanes), axBuffer);
CopySpanToArray(accelerationY.Slice(i, lanes), ayBuffer);
var px = new Vector<float>(pxBuffer);
var py = new Vector<float>(pyBuffer);
var dx = centerXVector - px;
var dy = centerYVector - py;
var distanceSquared = Vector.Max(dx * dx + dy * dy, minimumDistanceSquared);
var distance = Vector.SquareRoot(distanceSquared);
var inRange = Vector.LessThanOrEqual(distance, radiusVector);
var falloff = Vector.Max(zero, one - distance / radiusVector);
var magnitude = strengthVector * falloff;
var ax = Vector.ConditionalSelect(inRange, dx / distance * magnitude, zero);
var ay = Vector.ConditionalSelect(inRange, dy / distance * magnitude, zero);
(new Vector<float>(axBuffer) + ax).CopyTo(axBuffer);
(new Vector<float>(ayBuffer) + ay).CopyTo(ayBuffer);
CopyArrayToSpan(axBuffer, accelerationX.Slice(i, lanes));
CopyArrayToSpan(ayBuffer, accelerationY.Slice(i, lanes));
}
for (; i < positionX.Length; i++)
{
var dx = centerX - positionX[i];
var dy = centerY - positionY[i];
var distanceSquared = MathF.Max(dx * dx + dy * dy, MinimumDistanceSquared);
var distance = MathF.Sqrt(distanceSquared);
if (distance > radius)
{
continue;
}
var falloff = MathF.Max(0.0f, 1.0f - distance / radius);
var magnitude = strength * falloff;
accelerationX[i] += dx / distance * magnitude;
accelerationY[i] += dy / distance * magnitude;
}
}
public static void AddRadialFalloffAcceleration2D(
ReadOnlySpan<float2> positions,
float2 center,
float strength,
float radius,
Span<float2> acceleration)
{
ValidateEqualLengths(positions.Length, acceleration.Length);
if (radius <= 0.0f || strength == 0.0f || positions.Length == 0)
{
return;
}
for (var i = 0; i < positions.Length; i++)
{
var delta = center - positions[i];
var distanceSquared = MathF.Max(delta.x * delta.x + delta.y * delta.y, MinimumDistanceSquared);
var distance = MathF.Sqrt(distanceSquared);
if (distance > radius)
{
continue;
}
var falloff = MathF.Max(0.0f, 1.0f - distance / radius);
acceleration[i] += delta / distance * (strength * falloff);
}
}
public static void IntegrateSemiImplicitEuler2D(
float deltaTime,
ReadOnlySpan<float> dynamicMask,
Span<float> positionX,
Span<float> positionY,
Span<float> velocityX,
Span<float> velocityY,
ReadOnlySpan<float> accelerationX,
ReadOnlySpan<float> accelerationY)
{
if (deltaTime <= 0.0f)
{
throw new ArgumentOutOfRangeException(nameof(deltaTime), "Delta time must be positive.");
}
ValidateEqualLengths(
dynamicMask.Length,
positionX.Length,
positionY.Length,
velocityX.Length,
velocityY.Length,
accelerationX.Length,
accelerationY.Length);
var delta = new Vector<float>(deltaTime);
var i = 0;
var lanes = Vector<float>.Count;
var maskBuffer = new float[lanes];
var pxBuffer = new float[lanes];
var pyBuffer = new float[lanes];
var vxBuffer = new float[lanes];
var vyBuffer = new float[lanes];
var axBuffer = new float[lanes];
var ayBuffer = new float[lanes];
for (; i <= positionX.Length - lanes; i += lanes)
{
CopySpanToArray(dynamicMask.Slice(i, lanes), maskBuffer);
CopySpanToArray(velocityX.Slice(i, lanes), vxBuffer);
CopySpanToArray(velocityY.Slice(i, lanes), vyBuffer);
CopySpanToArray(positionX.Slice(i, lanes), pxBuffer);
CopySpanToArray(positionY.Slice(i, lanes), pyBuffer);
CopySpanToArray(accelerationX.Slice(i, lanes), axBuffer);
CopySpanToArray(accelerationY.Slice(i, lanes), ayBuffer);
var mask = new Vector<float>(maskBuffer);
var vx = new Vector<float>(vxBuffer);
var vy = new Vector<float>(vyBuffer);
var px = new Vector<float>(pxBuffer);
var py = new Vector<float>(pyBuffer);
var ax = new Vector<float>(axBuffer);
var ay = new Vector<float>(ayBuffer);
vx += ax * delta * mask;
vy += ay * delta * mask;
px += vx * delta * mask;
py += vy * delta * mask;
vx.CopyTo(vxBuffer);
vy.CopyTo(vyBuffer);
px.CopyTo(pxBuffer);
py.CopyTo(pyBuffer);
CopyArrayToSpan(vxBuffer, velocityX.Slice(i, lanes));
CopyArrayToSpan(vyBuffer, velocityY.Slice(i, lanes));
CopyArrayToSpan(pxBuffer, positionX.Slice(i, lanes));
CopyArrayToSpan(pyBuffer, positionY.Slice(i, lanes));
}
for (; i < positionX.Length; i++)
{
var mask = dynamicMask[i];
velocityX[i] += accelerationX[i] * deltaTime * mask;
velocityY[i] += accelerationY[i] * deltaTime * mask;
positionX[i] += velocityX[i] * deltaTime * mask;
positionY[i] += velocityY[i] * deltaTime * mask;
}
}
public static void IntegrateSemiImplicitEuler2D(
float deltaTime,
ReadOnlySpan<float> dynamicMask,
Span<float2> position,
Span<float2> velocity,
ReadOnlySpan<float2> acceleration)
{
if (deltaTime <= 0.0f)
{
throw new ArgumentOutOfRangeException(nameof(deltaTime), "Delta time must be positive.");
}
ValidateEqualLengths(dynamicMask.Length, position.Length, velocity.Length, acceleration.Length);
for (var i = 0; i < position.Length; i++)
{
var mask = dynamicMask[i];
velocity[i] += acceleration[i] * deltaTime * mask;
position[i] += velocity[i] * deltaTime * mask;
}
}
private static void ValidateEqualLengths(params int[] lengths)
{
var length = lengths[0];
for (var i = 1; i < lengths.Length; i++)
{
if (lengths[i] != length)
{
throw new ArgumentException("CultMath batch spans must have equal length.");
}
}
}
private static void CopySpanToArray(ReadOnlySpan<float> source, float[] destination)
{
for (var i = 0; i < source.Length; i++)
{
destination[i] = source[i];
}
}
private static void CopyArrayToSpan(float[] source, Span<float> destination)
{
for (var i = 0; i < destination.Length; i++)
{
destination[i] = source[i];
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d4771320e190bdd408dd296c293a9d0e

View file

@ -0,0 +1,4 @@
namespace CultMath;
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly record struct Color32(byte r, byte g, byte b, byte a = 255);

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: bfa14a9aacb8ac4448761568643f2bc6

View file

@ -0,0 +1,14 @@
{
"name": "CultMath",
"rootNamespace": "CultMath",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": true,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": true
}

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: f0b66b909d13e18468f7aa56221531e9
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<RootNamespace>CultMath</RootNamespace>
<AssemblyName>CultMath</AssemblyName>
<PackageId>GameCult.Math</PackageId>
<Version>0.1.2</Version>
<Description>Clean-room C# math shaped for HLSL-style renderer code.</Description>
<Authors>GameCult</Authors>
<PackageReadmeFile>README.md</PackageReadmeFile>
<NoWarn>$(NoWarn);CS8981</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
<None Include="..\..\shaders\CultMath.hlsl" Pack="true" PackagePath="contentFiles\any\any\shaders\CultMath.hlsl" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 77e1a0a2512910f41885650a7d0d6f60
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,10 @@
namespace CultMath;
public enum CultMathTone : byte
{
Background = 0,
Panel = 1,
Edge = 2,
Header = 3,
Body = 4,
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d4bfc66f9f7e992448f884c2c338d50a

View file

@ -0,0 +1,5 @@
namespace System.Runtime.CompilerServices;
internal static class IsExternalInit
{
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: de2b3c3e7f03c354aa2b84fbbc55b497

View file

@ -0,0 +1,62 @@
namespace CultMath;
public struct Random
{
private const uint DefaultSeed = 0x6E624EB7u;
public uint state;
public Random(uint seed)
{
state = seed == 0 ? DefaultSeed : seed;
}
public static Random CreateFromIndex(uint index) => new(index + DefaultSeed);
public void InitState(uint seed)
{
state = seed == 0 ? DefaultSeed : seed;
}
public uint NextUInt()
{
var x = state == 0 ? DefaultSeed : state;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
state = x == 0 ? DefaultSeed : x;
return state;
}
public int NextInt(int max) => NextInt(0, max);
public int NextInt(int min, int max)
{
if (max <= min)
{
return min;
}
return min + (int)(NextUInt() % (uint)(max - min));
}
public float NextFloat() => (NextUInt() >> 8) * (1.0f / 16777216.0f);
public float NextFloat(float max) => NextFloat() * max;
public float NextFloat(float min, float max) => min + NextFloat() * (max - min);
public float2 NextFloat2() => new(NextFloat(), NextFloat());
public float2 NextFloat2(float min, float max) => new(NextFloat(min, max), NextFloat(min, max));
public float2 NextFloat2(float2 min, float2 max) => min + NextFloat2() * (max - min);
public float2 NextFloat2Direction()
{
var angle = NextFloat(0.0f, math.TAU);
return new float2(math.cos(angle), math.sin(angle));
}
public float3 NextFloat3(float3 min, float3 max) => min + new float3(NextFloat(), NextFloat(), NextFloat()) * (max - min);
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 7c1a557259026da4ca20b54264852c13

View file

@ -0,0 +1,229 @@
using System;
using System.Runtime.InteropServices;
namespace CultMath;
public static class Voronoi
{
public static bool NativeAvailable { get; private set; } = true;
public static Color32 SampleTone(float x, float y, float resolutionY, int frameIndex, CultMathTone tone, float span)
{
Span<float> xs = stackalloc[] { x };
Span<float> ys = stackalloc[] { y };
Span<CultMathTone> tones = stackalloc[] { tone };
Span<float> spans = stackalloc[] { span };
Span<Color32> colors = stackalloc Color32[1];
SampleTones(xs, ys, tones, spans, resolutionY, frameIndex, colors);
return colors[0];
}
public static void SampleTones(
ReadOnlySpan<float> xs,
ReadOnlySpan<float> ys,
ReadOnlySpan<CultMathTone> tones,
ReadOnlySpan<float> spans,
float resolutionY,
int frameIndex,
Span<Color32> colors)
{
if (xs.Length != ys.Length || xs.Length != tones.Length || xs.Length != spans.Length || xs.Length != colors.Length)
{
throw new ArgumentException("CultMath Voronoi batch spans must have equal length.");
}
if (xs.IsEmpty)
{
return;
}
if (NativeAvailable && TrySampleNative(xs, ys, tones, spans, resolutionY, frameIndex, colors))
{
return;
}
for (var index = 0; index < xs.Length; index++)
{
colors[index] = SampleManaged(xs[index], ys[index], resolutionY, frameIndex, tones[index], spans[index]);
}
}
private static unsafe bool TrySampleNative(
ReadOnlySpan<float> xs,
ReadOnlySpan<float> ys,
ReadOnlySpan<CultMathTone> tones,
ReadOnlySpan<float> spans,
float resolutionY,
int frameIndex,
Span<Color32> colors)
{
try
{
fixed (float* xPtr = xs)
fixed (float* yPtr = ys)
fixed (CultMathTone* tonePtr = tones)
fixed (float* spanPtr = spans)
fixed (Color32* colorPtr = colors)
{
var result = Native.cultmath_apollonian_voronoi_tones(
xPtr,
yPtr,
(byte*)tonePtr,
spanPtr,
(nuint)xs.Length,
resolutionY,
frameIndex,
colorPtr);
if (result == 0)
{
return true;
}
}
}
catch (DllNotFoundException)
{
NativeAvailable = false;
}
catch (EntryPointNotFoundException)
{
NativeAvailable = false;
}
return false;
}
private static Color32 SampleManaged(float x, float y, float resolutionY, int frameIndex, CultMathTone tone, float span)
{
var sample = StableSample(x, y, resolutionY, frameIndex, tone, span);
var luminance = math.saturate(sample.x * 0.299f + sample.y * 0.587f + sample.z * 0.114f);
if (tone == CultMathTone.Header)
{
var glow = 0.62f + luminance * 0.38f;
return new Color32((byte)(255 * glow), (byte)(96 * glow), (byte)(14 * glow));
}
if (tone == CultMathTone.Body)
{
var glow = 0.68f + luminance * 0.32f;
return new Color32((byte)(245 * glow), (byte)(248 * glow), (byte)(252 * glow));
}
var gain = tone switch
{
CultMathTone.Panel => 0.42f,
CultMathTone.Edge => 1.05f,
_ => 0.24f,
};
var lift = tone switch
{
CultMathTone.Panel => 14.0f,
CultMathTone.Edge => 24.0f,
_ => 4.0f,
};
return new Color32(
(byte)Math.Clamp(lift + sample.x * 255.0f * gain, 0, 255),
(byte)Math.Clamp(lift + sample.y * 255.0f * gain, 0, 255),
(byte)Math.Clamp(lift + sample.z * 255.0f * gain, 0, 255));
}
private static float3 StableSample(float x, float y, float resolutionY, int frameIndex, CultMathTone tone, float span)
{
var seed = x * 12.9898f + y * 78.233f + Hash1(((byte)tone + 1.0f) * 19.19f);
var jitter = new float2((Hash1(seed) - 0.5f) * span, (Hash1(seed + 37.17f) - 0.5f) * span);
return SampleField(x + jitter.x, y + jitter.y, resolutionY, frameIndex);
}
private static float3 SampleField(float px, float py, float resolutionY, int frameIndex)
{
var time = frameIndex / 120.0f;
var p = new float2(6.0f * px / Math.Max(1.0f, resolutionY), 6.0f * py / Math.Max(1.0f, resolutionY));
var n = math.floor(p);
var f = math.frac(p);
var distance = 8.0f;
var color = float3.zero;
const float smoothness = 0.005f;
for (var j = -2; j <= 2; j++)
{
for (var i = -2; i <= 2; i++)
{
var g = new float2(i, j);
var o = Hash2(n + g);
var weight = o.x * 0.5f + 0.5f;
o = 0.5f + 0.5f * new float2(math.sin(time + 6.2831f * o.x), math.sin(time + 6.2831f * o.y));
var d2 = math.abs(g - f + o);
var d = Math.Max(d2.x, d2.y) * weight;
var seed = Hash1(math.dot(n + g, new float2(7.0f, 113.0f)));
var hue = math.frac((n.x + g.x) * 0.173f + (n.y + g.y) * 0.379f + seed * 0.431f);
var candidate = PastelSpectrum(hue);
var h = math.smoothstep(0.0f, 1.0f, 0.5f + 0.5f * (distance - d) / smoothness);
var correction = h * (1.0f - h) * smoothness / (1.0f + 3.0f * smoothness);
distance = math.lerp(distance, d, h) - correction;
color = math.lerp(color, candidate, h) - correction;
}
}
return math.max(float3.zero, color * (1.0f - 0.1f * math.smoothstep(0.04f, 0.05f, distance)));
}
private static float Hash1(float value) => math.frac(math.sin(value) * 43758.5453f);
private static float3 PastelSpectrum(float hue)
{
return HsvToRgb(hue, 0.52f, 0.98f);
}
private static float3 HsvToRgb(float hue, float saturation, float value)
{
var h = math.frac(hue) * 6.0f;
var c = value * saturation;
var x = c * (1.0f - Math.Abs(h % 2.0f - 1.0f));
var m = value - c;
float3 rgb;
if (h < 1.0f)
{
rgb = new float3(c, x, 0.0f);
}
else if (h < 2.0f)
{
rgb = new float3(x, c, 0.0f);
}
else if (h < 3.0f)
{
rgb = new float3(0.0f, c, x);
}
else if (h < 4.0f)
{
rgb = new float3(0.0f, x, c);
}
else if (h < 5.0f)
{
rgb = new float3(x, 0.0f, c);
}
else
{
rgb = new float3(c, 0.0f, x);
}
return rgb + m;
}
private static float2 Hash2(float2 value)
{
var p = new float2(math.dot(value, new float2(127.1f, 311.7f)), math.dot(value, new float2(269.5f, 183.3f)));
return math.frac(new float2(math.sin(p.x) * 43758.5453f, math.sin(p.y) * 43758.5453f));
}
private static unsafe partial class Native
{
[DllImport("cultmath_core", CallingConvention = CallingConvention.Cdecl)]
public static extern int cultmath_apollonian_voronoi_tones(
float* xs,
float* ys,
byte* tones,
float* spans,
nuint count,
float resolutionY,
int frameIndex,
Color32* outColors);
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: cb530f1428ac8ff43939b986986ae959

View file

@ -0,0 +1,18 @@
using System;
namespace CultMath;
public record struct bool2(bool x, bool y)
{
public static readonly bool2 @false = new(false, false);
public static readonly bool2 @true = new(true, true);
public bool this[int index] => index switch
{
0 => x,
1 => y,
_ => throw new ArgumentOutOfRangeException(nameof(index)),
};
public static implicit operator bool2(bool value) => new(value, value);
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 725efb2725e987449a3782452f5a95e0

View file

@ -0,0 +1,25 @@
using System;
namespace CultMath;
public record struct double2(double x, double y)
{
public static readonly double2 zero = new(0.0, 0.0);
public static readonly double2 one = new(1.0, 1.0);
public double this[int index] => index switch
{
0 => x,
1 => y,
_ => throw new ArgumentOutOfRangeException(nameof(index)),
};
public static implicit operator double2(double value) => new(value, value);
public static double2 operator +(double2 value) => value;
public static double2 operator -(double2 value) => new(-value.x, -value.y);
public static double2 operator +(double2 left, double2 right) => new(left.x + right.x, left.y + right.y);
public static double2 operator -(double2 left, double2 right) => new(left.x - right.x, left.y - right.y);
public static double2 operator *(double2 left, double2 right) => new(left.x * right.x, left.y * right.y);
public static double2 operator /(double2 left, double2 right) => new(left.x / right.x, left.y / right.y);
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: bbe739a5f0216c3459762d2b5ee7401a

View file

@ -0,0 +1,36 @@
using System;
namespace CultMath;
public record struct double3(double x, double y, double z)
{
public static readonly double3 zero = new(0.0, 0.0, 0.0);
public static readonly double3 one = new(1.0, 1.0, 1.0);
public double2 xy
{
get => new(x, y);
set
{
x = value.x;
y = value.y;
}
}
public double this[int index] => index switch
{
0 => x,
1 => y,
2 => z,
_ => throw new ArgumentOutOfRangeException(nameof(index)),
};
public static implicit operator double3(double value) => new(value, value, value);
public static double3 operator +(double3 value) => value;
public static double3 operator -(double3 value) => new(-value.x, -value.y, -value.z);
public static double3 operator +(double3 left, double3 right) => new(left.x + right.x, left.y + right.y, left.z + right.z);
public static double3 operator -(double3 left, double3 right) => new(left.x - right.x, left.y - right.y, left.z - right.z);
public static double3 operator *(double3 left, double3 right) => new(left.x * right.x, left.y * right.y, left.z * right.z);
public static double3 operator /(double3 left, double3 right) => new(left.x / right.x, left.y / right.y, left.z / right.z);
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 1cbaafd219703ee43ae279aa5cbf407f

View file

@ -0,0 +1,28 @@
using System;
namespace CultMath;
public record struct float2(float x, float y)
{
public static readonly float2 zero = new(0.0f, 0.0f);
public static readonly float2 one = new(1.0f, 1.0f);
public float this[int index] => index switch
{
0 => x,
1 => y,
_ => throw new ArgumentOutOfRangeException(nameof(index)),
};
public static implicit operator float2(float value) => new(value, value);
public static explicit operator System.Numerics.Vector2(float2 value) => new(value.x, value.y);
public static explicit operator float2(System.Numerics.Vector2 value) => new(value.X, value.Y);
public static float2 operator +(float2 value) => value;
public static float2 operator -(float2 value) => new(-value.x, -value.y);
public static float2 operator +(float2 left, float2 right) => new(left.x + right.x, left.y + right.y);
public static float2 operator -(float2 left, float2 right) => new(left.x - right.x, left.y - right.y);
public static float2 operator *(float2 left, float2 right) => new(left.x * right.x, left.y * right.y);
public static float2 operator /(float2 left, float2 right) => new(left.x / right.x, left.y / right.y);
public static float2 operator %(float2 left, float2 right) => new(left.x % right.x, left.y % right.y);
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fa066a0aed5494f499d462a2ebc51e50

View file

@ -0,0 +1,79 @@
using System;
namespace CultMath;
public 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
{
get => new(x, y);
set
{
x = value.x;
y = value.y;
}
}
public float2 xz
{
get => new(x, z);
set
{
x = value.x;
z = value.y;
}
}
public float2 yz
{
get => new(y, z);
set
{
y = value.x;
z = value.y;
}
}
public float2 zy
{
get => new(z, y);
set
{
z = value.x;
y = value.y;
}
}
public float3 yzx => new(y, z, x);
public float3 zyx => new(z, y, x);
public float this[int index] => index switch
{
0 => x,
1 => y,
2 => z,
_ => 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, float3 right) => new(left.x - right.x, left.y - right.y, left.z - 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, float3 right) => new(left.x / right.x, left.y / right.y, left.z / right.z);
public static float3 operator %(float3 left, float3 right) => new(left.x % right.x, left.y % right.y, left.z % right.z);
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 58a355dd1cf6b9f48a7595eea0c0e1f2

View file

@ -0,0 +1,58 @@
using System;
namespace CultMath;
public 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 float4(float x, float3 yzw)
: this(x, yzw.x, yzw.y, yzw.z)
{
}
public float2 xy
{
get => new(x, y);
set
{
x = value.x;
y = value.y;
}
}
public float3 xyw => new(x, y, w);
public float3 yzx => new(y, z, x);
public float3 xyz => new(x, y, z);
public float this[int index] => index switch
{
0 => x,
1 => y,
2 => z,
3 => w,
_ => throw new ArgumentOutOfRangeException(nameof(index)),
};
public static implicit operator float4(float value) => new(value, value, value, value);
public static explicit operator System.Numerics.Vector4(float4 value) => new(value.x, value.y, value.z, value.w);
public static explicit operator float4(System.Numerics.Vector4 value) => new(value.X, value.Y, value.Z, value.W);
public static float4 operator +(float4 value) => value;
public static float4 operator -(float4 value) => new(-value.x, -value.y, -value.z, -value.w);
public static float4 operator +(float4 left, float4 right) => new(left.x + right.x, left.y + right.y, left.z + right.z, left.w + right.w);
public static float4 operator -(float4 left, float4 right) => new(left.x - right.x, left.y - right.y, left.z - right.z, left.w - right.w);
public static float4 operator *(float4 left, float4 right) => new(left.x * right.x, left.y * right.y, left.z * right.z, left.w * right.w);
public static float4 operator /(float4 left, float4 right) => new(left.x / right.x, left.y / right.y, left.z / right.z, left.w / right.w);
public static float4 operator %(float4 left, float4 right) => new(left.x % right.x, left.y % right.y, left.z % right.z, left.w % right.w);
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fe24fbdab33538849bc6be0afbb0a692

View file

@ -0,0 +1,29 @@
using System;
namespace CultMath;
public record struct int2(int x, int y)
{
public static readonly int2 zero = new(0, 0);
public static readonly int2 one = new(1, 1);
public int this[int index] => index switch
{
0 => x,
1 => y,
_ => throw new ArgumentOutOfRangeException(nameof(index)),
};
public static implicit operator int2(int value) => new(value, value);
public static implicit operator float2(int2 value) => new(value.x, value.y);
public static explicit operator System.Numerics.Vector2(int2 value) => new(value.x, value.y);
public static explicit operator int2(System.Numerics.Vector2 value) => new((int)value.X, (int)value.Y);
public static int2 operator +(int2 value) => value;
public static int2 operator -(int2 value) => new(-value.x, -value.y);
public static int2 operator +(int2 left, int2 right) => new(left.x + right.x, left.y + right.y);
public static int2 operator -(int2 left, int2 right) => new(left.x - right.x, left.y - right.y);
public static int2 operator *(int2 left, int2 right) => new(left.x * right.x, left.y * right.y);
public static int2 operator /(int2 left, int2 right) => new(left.x / right.x, left.y / right.y);
public static int2 operator %(int2 left, int2 right) => new(left.x % right.x, left.y % right.y);
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 577b174a998aaa4478ec26e7a6782f46

View file

@ -0,0 +1,417 @@
using System;
namespace CultMath;
public static class math
{
public const float PI = MathF.PI;
public const float TAU = MathF.PI * 2.0f;
public const float HALF_PI = MathF.PI * 0.5f;
public static float2 float2(float x, float y) => new(x, y);
public static float2 float2(float value) => new(value, value);
public static bool2 bool2(bool x, bool y) => new(x, y);
public static bool2 bool2(bool value) => new(value, value);
public static int2 int2(int x, int y) => new(x, y);
public static int2 int2(int value) => new(value, value);
public static float3 float3(float x, float y, float z) => new(x, y, z);
public static float3 float3(float2 xy, float z) => new(xy, z);
public static float3 float3(float value) => new(value, value, value);
public static float4 float4(float x, float y, float z, float w) => new(x, y, z, w);
public static float4 float4(float2 xy, float z, float w) => new(xy, z, w);
public static float4 float4(float3 xyz, float w) => new(xyz, w);
public static float4 float4(float x, float3 yzw) => new(x, yzw);
public static float4 float4(float value) => new(value, value, value, value);
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 pow(float value, float power) => MathF.Pow(value, power);
public static bool isinf(float value) => float.IsInfinity(value);
public static float exp(float value) => MathF.Exp(value);
public static double exp(double value) => Math.Exp(value);
public static float2 exp(float2 value) => new(exp(value.x), exp(value.y));
public static float3 exp(float3 value) => new(exp(value.x), exp(value.y), exp(value.z));
public static float4 exp(float4 value) => new(exp(value.x), exp(value.y), exp(value.z), exp(value.w));
public static double2 exp(double2 value) => new(exp(value.x), exp(value.y));
public static double3 exp(double3 value) => new(exp(value.x), exp(value.y), exp(value.z));
public static float abs(float value) => MathF.Abs(value);
public static float2 abs(float2 value) => new(abs(value.x), abs(value.y));
public static float3 abs(float3 value) => new(abs(value.x), abs(value.y), abs(value.z));
public static float4 abs(float4 value) => new(abs(value.x), abs(value.y), abs(value.z), abs(value.w));
public static float sign(float value) => MathF.Sign(value);
public static float2 sign(float2 value) => new(sign(value.x), sign(value.y));
public static float3 sign(float3 value) => new(sign(value.x), sign(value.y), sign(value.z));
public static float4 sign(float4 value) => new(sign(value.x), sign(value.y), sign(value.z), sign(value.w));
public static float floor(float value) => MathF.Floor(value);
public static float2 floor(float2 value) => new(floor(value.x), floor(value.y));
public static float3 floor(float3 value) => new(floor(value.x), floor(value.y), floor(value.z));
public static float4 floor(float4 value) => new(floor(value.x), floor(value.y), floor(value.z), floor(value.w));
public static float ceil(float value) => MathF.Ceiling(value);
public static float2 ceil(float2 value) => new(ceil(value.x), ceil(value.y));
public static float3 ceil(float3 value) => new(ceil(value.x), ceil(value.y), ceil(value.z));
public static float4 ceil(float4 value) => new(ceil(value.x), ceil(value.y), ceil(value.z), ceil(value.w));
public static float frac(float value) => value - floor(value);
public static float2 frac(float2 value) => value - floor(value);
public static float3 frac(float3 value) => value - floor(value);
public static float4 frac(float4 value) => value - floor(value);
public static float min(float left, float right) => MathF.Min(left, right);
public static int min(int left, int right) => Math.Min(left, right);
public static float2 min(float2 left, float2 right) => new(min(left.x, right.x), min(left.y, right.y));
public static float3 min(float3 left, float3 right) => new(min(left.x, right.x), min(left.y, right.y), min(left.z, right.z));
public static float4 min(float4 left, float4 right) => new(min(left.x, right.x), min(left.y, right.y), min(left.z, right.z), min(left.w, right.w));
public static float max(float left, float right) => MathF.Max(left, right);
public static int max(int left, int right) => Math.Max(left, right);
public static float2 max(float2 left, float2 right) => new(max(left.x, right.x), max(left.y, right.y));
public static float3 max(float3 left, float3 right) => new(max(left.x, right.x), max(left.y, right.y), max(left.z, right.z));
public static float4 max(float4 left, float4 right) => new(max(left.x, right.x), max(left.y, right.y), max(left.z, right.z), max(left.w, right.w));
public static float clamp(float value, float minimum, float maximum) => min(max(value, minimum), maximum);
public static float2 clamp(float2 value, float2 minimum, float2 maximum) => min(max(value, minimum), maximum);
public static float3 clamp(float3 value, float3 minimum, float3 maximum) => min(max(value, minimum), maximum);
public static float4 clamp(float4 value, float4 minimum, float4 maximum) => min(max(value, minimum), maximum);
public static float saturate(float value) => clamp(value, 0.0f, 1.0f);
public static float2 saturate(float2 value) => clamp(value, 0.0f, 1.0f);
public static float3 saturate(float3 value) => clamp(value, 0.0f, 1.0f);
public static float4 saturate(float4 value) => clamp(value, 0.0f, 1.0f);
public static float lerp(float start, float end, float amount) => start + (end - start) * amount;
public static double lerp(double start, double end, double amount) => start + (end - start) * amount;
public static float2 lerp(float2 start, float2 end, float2 amount) => start + (end - start) * amount;
public static float3 lerp(float3 start, float3 end, float3 amount) => start + (end - start) * amount;
public static float4 lerp(float4 start, float4 end, float4 amount) => start + (end - start) * amount;
public static double2 lerp(double2 start, double2 end, double2 amount) => start + (end - start) * amount;
public static double3 lerp(double3 start, double3 end, double3 amount) => start + (end - start) * amount;
public static float unlerp(float start, float end, float value) => (value - start) / (end - start);
public static double unlerp(double start, double end, double value) => (value - start) / (end - start);
public static float2 unlerp(float2 start, float2 end, float2 value) => (value - start) / (end - start);
public static float3 unlerp(float3 start, float3 end, float3 value) => (value - start) / (end - start);
public static float4 unlerp(float4 start, float4 end, float4 value) => (value - start) / (end - start);
public static double2 unlerp(double2 start, double2 end, double2 value) => (value - start) / (end - start);
public static double3 unlerp(double3 start, double3 end, double3 value) => (value - start) / (end - start);
public static float step(float edge, float value) => value < edge ? 0.0f : 1.0f;
public static float2 step(float2 edge, float2 value) => new(step(edge.x, value.x), step(edge.y, value.y));
public static float3 step(float3 edge, float3 value) => new(step(edge.x, value.x), step(edge.y, value.y), step(edge.z, value.z));
public static float4 step(float4 edge, float4 value) => new(step(edge.x, value.x), step(edge.y, value.y), step(edge.z, value.z), step(edge.w, value.w));
public static float smoothstep(float minimum, float maximum, float value)
{
var t = saturate((value - minimum) / (maximum - minimum));
return t * t * (3.0f - 2.0f * t);
}
public static float2 smoothstep(float2 minimum, float2 maximum, float2 value)
{
var t = saturate((value - minimum) / (maximum - minimum));
return t * t * (3.0f - 2.0f * t);
}
public static float3 smoothstep(float3 minimum, float3 maximum, float3 value)
{
var t = saturate((value - minimum) / (maximum - minimum));
return t * t * (3.0f - 2.0f * t);
}
public static float4 smoothstep(float4 minimum, float4 maximum, float4 value)
{
var t = saturate((value - minimum) / (maximum - minimum));
return t * t * (3.0f - 2.0f * t);
}
public static float dot(float2 left, float2 right) => left.x * right.x + left.y * right.y;
public static float dot(float3 left, float3 right) => left.x * right.x + left.y * right.y + left.z * right.z;
public static float dot(float4 left, float4 right) => left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w;
public static float3 cross(float3 left, float3 right) =>
new(
left.y * right.z - left.z * right.y,
left.z * right.x - left.x * right.z,
left.x * right.y - left.y * right.x);
public static float lengthsq(float2 value) => dot(value, value);
public static float lengthsq(float3 value) => dot(value, value);
public static float lengthsq(float4 value) => dot(value, value);
public static float length(float2 value) => MathF.Sqrt(lengthsq(value));
public static float length(float3 value) => MathF.Sqrt(lengthsq(value));
public static float length(float4 value) => MathF.Sqrt(lengthsq(value));
public static float distance(float2 left, float2 right) => length(left - right);
public static float distance(float3 left, float3 right) => length(left - right);
public static float distance(float4 left, float4 right) => length(left - right);
public static float2 normalize(float2 value) => value / MathF.Max(length(value), 1.0e-20f);
public static float3 normalize(float3 value) => value / MathF.Max(length(value), 1.0e-20f);
public static float4 normalize(float4 value) => value / MathF.Max(length(value), 1.0e-20f);
public static quaternion normalize(quaternion value)
{
var length = MathF.Sqrt(value.x * value.x + value.y * value.y + value.z * value.z + value.w * value.w);
if (length <= 1.0e-20f)
{
return quaternion.identity;
}
var invLength = 1.0f / length;
return new quaternion(value.x * invLength, value.y * invLength, value.z * invLength, value.w * invLength);
}
public static float2 reflect(float2 incident, float2 normal) => incident - 2.0f * dot(normal, incident) * normal;
public static float3 reflect(float3 incident, float3 normal) => incident - 2.0f * dot(normal, incident) * normal;
public static float2 rotate(float2 value, float radians)
{
var s = sin(radians);
var c = cos(radians);
return new float2(value.x * c - value.y * s, value.x * s + value.y * c);
}
public static float2 rotate_degrees(float2 value, float degrees) => rotate(value, radians(degrees));
public static float csum(float2 value) => value.x + value.y;
public static float csum(float3 value) => value.x + value.y + value.z;
public static float csum(float4 value) => value.x + value.y + value.z + value.w;
public static float decay(float source, float lambda, float dt) => source * exp(-lambda * dt);
public static float2 decay(float2 source, float lambda, float dt) => source * exp(-lambda * dt);
public static float3 decay(float3 source, float lambda, float dt) => source * exp(-lambda * dt);
public static float4 decay(float4 source, float lambda, float dt) => source * exp(-lambda * dt);
public static float2 decay(float2 source, float2 lambda, float dt) => source * exp(-lambda * dt);
public static float3 decay(float3 source, float3 lambda, float dt) => source * exp(-lambda * dt);
public static float4 decay(float4 source, float4 lambda, float dt) => source * exp(-lambda * dt);
public static float damp(float start, float end, float lambda, float dt) => lerp(start, end, 1.0f - exp(-lambda * dt));
public static float2 damp(float2 start, float2 end, float lambda, float dt) => lerp(start, end, 1.0f - exp(-lambda * dt));
public static float3 damp(float3 start, float3 end, float lambda, float dt) => lerp(start, end, 1.0f - exp(-lambda * dt));
public static float4 damp(float4 start, float4 end, float lambda, float dt) => lerp(start, end, 1.0f - exp(-lambda * dt));
public static float first_order_intercept_time(float shotSpeed, float3 targetRelativePosition, float3 targetRelativeVelocity)
{
var velocitySquared = lengthsq(targetRelativeVelocity);
if (velocitySquared < 0.001f)
{
return 0.0f;
}
var a = velocitySquared - shotSpeed * shotSpeed;
if (abs(a) < 0.001f)
{
var time = -lengthsq(targetRelativePosition) / (2.0f * dot(targetRelativeVelocity, targetRelativePosition));
return max(time, 0.0f);
}
var b = 2.0f * dot(targetRelativeVelocity, targetRelativePosition);
var c = lengthsq(targetRelativePosition);
var determinant = b * b - 4.0f * a * c;
if (determinant > 0.0f)
{
var root = sqrt(determinant);
var t1 = (-b + root) / (2.0f * a);
var t2 = (-b - root) / (2.0f * a);
if (t1 > 0.0f)
{
return t2 > 0.0f ? min(t1, t2) : t1;
}
return max(t2, 0.0f);
}
if (determinant < 0.0f)
{
return 0.0f;
}
return max(-b / (2.0f * a), 0.0f);
}
public static float3 first_order_intercept(
float3 shooterPosition,
float3 shooterVelocity,
float shotSpeed,
float3 targetPosition,
float3 targetVelocity)
{
var targetRelativePosition = targetPosition - shooterPosition;
var targetRelativeVelocity = targetVelocity - shooterVelocity;
var time = first_order_intercept_time(shotSpeed, targetRelativePosition, targetRelativeVelocity);
return targetPosition + time * targetRelativeVelocity;
}
public static float distance_to_segment(float2 point, float2 start, float2 end, out float2 closest)
{
var segment = end - start;
var segmentLengthSq = lengthsq(segment);
if (segmentLengthSq <= 0.0f)
{
closest = start;
return distance(point, start);
}
var t = saturate(dot(point - start, segment) / segmentLengthSq);
closest = start + segment * t;
return distance(point, closest);
}
public static float catmullrom(float p0, float p1, float p2, float p3, float t)
{
var t2 = t * t;
var t3 = t2 * t;
return 0.5f * ((2.0f * p1) + (p2 - p0) * t + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * t2 + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
}
public static float2 catmullrom(float2 p0, float2 p1, float2 p2, float2 p3, float t) =>
0.5f * ((2.0f * p1) + (p2 - p0) * t + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * (t * t) + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * (t * t * t));
public static float3 catmullrom(float3 p0, float3 p1, float3 p2, float3 p3, float t) =>
0.5f * ((2.0f * p1) + (p2 - p0) * t + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * (t * t) + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * (t * t * t));
public static float4 catmullrom(float4 p0, float4 p1, float4 p2, float4 p3, float t) =>
0.5f * ((2.0f * p1) + (p2 - p0) * t + (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * (t * t) + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * (t * t * t));
public static float quadratic_bezier(float p0, float p1, float p2, float t)
{
t = saturate(t);
var inv = 1.0f - t;
return inv * inv * p0 + 2.0f * inv * t * p1 + t * t * p2;
}
public static float3 quadratic_bezier(float3 p0, float3 p1, float3 p2, float t)
{
t = saturate(t);
var inv = 1.0f - t;
return inv * inv * p0 + 2.0f * inv * t * p1 + t * t * p2;
}
public static float cubic_bezier(float p0, float p1, float p2, float p3, float t)
{
t = saturate(t);
var inv = 1.0f - t;
return inv * inv * inv * p0 + 3.0f * inv * inv * t * p1 + 3.0f * inv * t * t * p2 + t * t * t * p3;
}
public static float3 cubic_bezier(float3 p0, float3 p1, float3 p2, float3 p3, float t)
{
t = saturate(t);
var inv = 1.0f - t;
return inv * inv * inv * p0 + 3.0f * inv * inv * t * p1 + 3.0f * inv * t * t * p2 + t * t * t * p3;
}
public static float smoothstep01(float value) => smoothstep(0.0f, 1.0f, value);
public static float smootherstep(float value)
{
value = saturate(value);
return value * value * value * (value * (value * 6.0f - 15.0f) + 10.0f);
}
public static float hash(float value) => frac(sin(value) * 43758.5453f);
public static float hash(float2 value) => hash(dot(value, new float2(127.1f, 311.7f)));
public static float hash(float3 value) => hash(dot(value, new float3(127.1f, 311.7f, 74.7f)));
// Ashima Arts / Ian McEwan 2D simplex noise, kept component-explicit so
// the C# and HLSL mirrors preserve the same float32 evaluation order.
public static float simplex_noise(float2 value)
{
var c = new float4(
0.211324865405187f,
0.366025403784439f,
-0.577350269189626f,
0.024390243902439f);
var i = floor(value + dot(value, new float2(c.y, c.y)));
var x0 = value - i + dot(i, new float2(c.x, c.x));
var i1 = x0.x > x0.y ? new float2(1.0f, 0.0f) : new float2(0.0f, 1.0f);
var x12 = new float4(
x0.x + c.x - i1.x,
x0.y + c.x - i1.y,
x0.x + c.z,
x0.y + c.z);
i = simplex_mod289(i);
var p = simplex_permute(simplex_permute(i.y + new float3(0.0f, i1.y, 1.0f)) + i.x + new float3(0.0f, i1.x, 1.0f));
var m = max(0.5f - new float3(
dot(x0, x0),
x12.x * x12.x + x12.y * x12.y,
x12.z * x12.z + x12.w * x12.w), 0.0f);
m = m * m;
m = m * m;
var x = 2.0f * frac(p * c.w) - 1.0f;
var h = abs(x) - 0.5f;
var ox = floor(x + 0.5f);
var a0 = x - ox;
m *= 1.79284291400159f - 0.85373472095314f * (a0 * a0 + h * h);
var g = new float3(
a0.x * x0.x + h.x * x0.y,
a0.y * x12.x + h.y * x12.y,
a0.z * x12.z + h.z * x12.w);
return 130.0f * dot(m, g);
}
private static float2 simplex_mod289(float2 value) => value - floor(value * (1.0f / 289.0f)) * 289.0f;
private static float3 simplex_mod289(float3 value) => value - floor(value * (1.0f / 289.0f)) * 289.0f;
private static float3 simplex_permute(float3 value) => simplex_mod289(((value * 34.0f) + 1.0f) * value);
public static float value_noise(float2 position)
{
var cell = floor(position);
var local = frac(position);
var u = local * local * (3.0f - 2.0f * local);
var a = hash(cell);
var b = hash(cell + new float2(1.0f, 0.0f));
var c = hash(cell + new float2(0.0f, 1.0f));
var d = hash(cell + new float2(1.0f, 1.0f));
return lerp(lerp(a, b, u.x), lerp(c, d, u.x), u.y);
}
public static float value_noise_bicubic(float2 position)
{
var cell = floor(position);
var local = frac(position);
var y0 = catmullrom(
hash(cell + new float2(-1.0f, -1.0f)),
hash(cell + new float2(0.0f, -1.0f)),
hash(cell + new float2(1.0f, -1.0f)),
hash(cell + new float2(2.0f, -1.0f)),
local.x);
var y1 = catmullrom(
hash(cell + new float2(-1.0f, 0.0f)),
hash(cell + new float2(0.0f, 0.0f)),
hash(cell + new float2(1.0f, 0.0f)),
hash(cell + new float2(2.0f, 0.0f)),
local.x);
var y2 = catmullrom(
hash(cell + new float2(-1.0f, 1.0f)),
hash(cell + new float2(0.0f, 1.0f)),
hash(cell + new float2(1.0f, 1.0f)),
hash(cell + new float2(2.0f, 1.0f)),
local.x);
var y3 = catmullrom(
hash(cell + new float2(-1.0f, 2.0f)),
hash(cell + new float2(0.0f, 2.0f)),
hash(cell + new float2(1.0f, 2.0f)),
hash(cell + new float2(2.0f, 2.0f)),
local.x);
return catmullrom(y0, y1, y2, y3, local.y);
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 0d9b4cacc40d169429ee743d19577cb7

View file

@ -0,0 +1,68 @@
namespace CultMath;
public record struct quaternion(float x, float y, float z, float w)
{
public static readonly quaternion identity = new(0.0f, 0.0f, 0.0f, 1.0f);
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);
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 12f3627d9250dc348be9c1a1056ec7d0

View file

@ -0,0 +1,59 @@
namespace CultMath;
/// <summary>
/// Axis-aligned 2D bounds stored canonically as normalized minimum and maximum
/// corners. Point containment and intersection include the boundary.
/// </summary>
public readonly record struct rect
{
public rect(float minX, float minY, float maxX, float maxY)
: this(new float2(minX, minY), new float2(maxX, maxY))
{
}
public rect(float2 min, float2 max)
{
this.min = new float2(MathF.Min(min.x, max.x), MathF.Min(min.y, max.y));
this.max = new float2(MathF.Max(min.x, max.x), MathF.Max(min.y, max.y));
}
public float2 min { get; }
public float2 max { get; }
public float width => max.x - min.x;
public float height => max.y - min.y;
public float2 size => max - min;
public float2 center => (min + max) * 0.5f;
public float area => width * height;
public bool isEmpty => width == 0.0f || height == 0.0f;
public bool Contains(float2 point) =>
point.x >= min.x && point.x <= max.x &&
point.y >= min.y && point.y <= max.y;
public bool Contains(rect other) =>
other.min.x >= min.x && other.max.x <= max.x &&
other.min.y >= min.y && other.max.y <= max.y;
public bool Intersects(rect other) =>
min.x <= other.max.x && max.x >= other.min.x &&
min.y <= other.max.y && max.y >= other.min.y;
public rect Intersection(rect other)
{
if (!Intersects(other))
return new rect(0.0f, 0.0f, 0.0f, 0.0f);
return new rect(
MathF.Max(min.x, other.min.x),
MathF.Max(min.y, other.min.y),
MathF.Min(max.x, other.max.x),
MathF.Min(max.y, other.max.y));
}
public rect Encapsulate(float2 point) => new(
MathF.Min(min.x, point.x),
MathF.Min(min.y, point.y),
MathF.Max(max.x, point.x),
MathF.Max(max.y, point.y));
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5aab0d8e1ca94d42b85bba780a7ee449