Transitioned from VectorX to floatX

And some trying to update Rigidbody when transform changes
This commit is contained in:
Simon Lübeß
2023-06-25 02:47:33 +02:00
parent 5f5da2ed35
commit 9e1f648aa0
49 changed files with 1281 additions and 871 deletions
+22 -22
View File
@@ -367,15 +367,15 @@ namespace GlitchyEngine.Content
Log.EngineLogger.AssertDebug(positions != null, "The model appears to have no position data?!");
Vector3[] normals = new Vector3[positions.Count];
float3[] normals = new float3[positions.Count];
if(primitive.Indices != null)
GenerateNormals(primitive.Indices, positions, normals);
else
GenerateNormals(positions, normals);
VertexBuffer vertexBuffer = new VertexBuffer((uint32)sizeof(Vector3), (uint32)normals.Count, .Immutable);
vertexBuffer.SetData<Vector3>(normals);
VertexBuffer vertexBuffer = new VertexBuffer((uint32)sizeof(float3), (uint32)normals.Count, .Immutable);
vertexBuffer.SetData<float3>(normals);
bindings.Add(vertexBuffer.Binding);
@@ -405,20 +405,20 @@ namespace GlitchyEngine.Content
}
/// Generates the normals for one triangle
static mixin GenerateTriangleNormals(int index0, int index1, int index2, CGLTF.Accessor* positions, Vector3[] normals)
static mixin GenerateTriangleNormals(int index0, int index1, int index2, CGLTF.Accessor* positions, float3[] normals)
{
Vector3 position0 = GetEntry<Vector3>(positions, (.)index0);
Vector3 position1 = GetEntry<Vector3>(positions, (.)index1);
Vector3 position2 = GetEntry<Vector3>(positions, (.)index2);
float3 position0 = GetEntry<float3>(positions, (.)index0);
float3 position1 = GetEntry<float3>(positions, (.)index1);
float3 position2 = GetEntry<float3>(positions, (.)index2);
ref Vector3 normal0 = ref normals[(.)index0];
ref Vector3 normal1 = ref normals[(.)index1];
ref Vector3 normal2 = ref normals[(.)index2];
ref float3 normal0 = ref normals[(.)index0];
ref float3 normal1 = ref normals[(.)index1];
ref float3 normal2 = ref normals[(.)index2];
Vector3 e0 = position1 - position0;
Vector3 e1 = position2 - position0;
float3 e0 = position1 - position0;
float3 e1 = position2 - position0;
Vector3 normal = Vector3.Cross(e0, e1);
float3 normal = cross(e0, e1);
normal0 += normal;
normal1 += normal;
@@ -426,16 +426,16 @@ namespace GlitchyEngine.Content
}
[Inline]
static void NormalizeNormals(Vector3[] normals)
static void NormalizeNormals(float3[] normals)
{
for(int i < normals.Count)
{
normals[i].Normalize();
normalize(normals[i]);
}
}
/// Generates normals for the given model using indexed geometry
static void GenerateNormals(CGLTF.Accessor* indices, CGLTF.Accessor* positions, Vector3[] normals)
static void GenerateNormals(CGLTF.Accessor* indices, CGLTF.Accessor* positions, float3[] normals)
{
/// Enumerate triangle-wise
for(uint t = 0; t < indices.Count; t += 3)
@@ -451,7 +451,7 @@ namespace GlitchyEngine.Content
}
/// Generates normals for the given model using nonindexed geometry
static void GenerateNormals(CGLTF.Accessor* positions, Vector3[] normals)
static void GenerateNormals(CGLTF.Accessor* positions, float3[] normals)
{
/// Enumerate triangle-wise
for(int i = 0; i < (.)positions.Count; i += 3)
@@ -554,7 +554,7 @@ namespace GlitchyEngine.Content
for(int i < samples)
{
float timeStamp = GetEntry<float>(channel.Sampler.Input, i);
Vector3 sample = GetEntry<Vector3>(channel.Sampler.Output, i);
float3 sample = GetEntry<float3>(channel.Sampler.Output, i);
jointAnimation.TranslationChannel.TimeStamps[i] = timeStamp;
jointAnimation.TranslationChannel.Values[i] = sample;
@@ -582,7 +582,7 @@ namespace GlitchyEngine.Content
for(int i < samples)
{
float timeStamp = GetEntry<float>(channel.Sampler.Input, i);
Vector3 sample = GetEntry<Vector3>(channel.Sampler.Output, i);
float3 sample = GetEntry<float3>(channel.Sampler.Output, i);
jointAnimation.ScaleChannel.TimeStamps[i] = timeStamp;
jointAnimation.ScaleChannel.Values[i] = sample;
@@ -606,11 +606,11 @@ namespace GlitchyEngine.Content
{
case typeof(float):
CGLTF.AccessorReadFloat(accessor, (uint)index, (float*)&result, 1);
case typeof(Vector2):
case typeof(float2):
CGLTF.AccessorReadFloat(accessor, (uint)index, (float*)&result, 2);
case typeof(Vector3):
case typeof(float3):
CGLTF.AccessorReadFloat(accessor, (uint)index, (float*)&result, 3);
case typeof(Vector4), typeof(Quaternion):
case typeof(float4), typeof(Quaternion):
CGLTF.AccessorReadFloat(accessor, (uint)index, (float*)&result, 4);
case typeof(Matrix):
CGLTF.AccessorReadFloat(accessor, (uint)index, (float*)&result, 16);
+10 -10
View File
@@ -8,7 +8,7 @@ namespace GlitchyEngine.Math
{
extension ColorRGBA
{
internal uint32 ImGuiU32 => ImGui.ImGui.ColorConvertFloat4ToU32((.)(Vector4)this);
internal uint32 ImGuiU32 => ImGui.ImGui.ColorConvertFloat4ToU32((.)(float4)this);
}
}
@@ -20,14 +20,14 @@ namespace ImGui
{
extension Vec2
{
public static explicit operator Vector2(Vec2 v) => .(v.x, v.y);
public static explicit operator Vec2(Vector2 v) => .(v.X, v.Y);
public static explicit operator float2(Vec2 v) => .(v.x, v.y);
public static explicit operator Vec2(float2 v) => .(v.X, v.Y);
}
extension Vec4
{
public static explicit operator Vector4(Vec4 v) => .(v.x, v.y, v.z, v.w);
public static explicit operator Vec4(Vector4 v) => .(v.X, v.Y, v.Z, v.W);
public static explicit operator float4(Vec4 v) => .(v.x, v.y, v.z, v.w);
public static explicit operator Vec4(float4 v) => .(v.X, v.Y, v.Z, v.W);
}
/*public static bool IsItemJustDeactivated()
@@ -75,7 +75,7 @@ namespace ImGui
if (uv0 != .Zero || uv1 != .Ones)
Runtime.NotImplemented();
Vector2 v = (.)subTexture.TexCoords.XY + subTexture.TexCoords.ZW;
float2 v = (.)subTexture.TexCoords.XY + subTexture.TexCoords.ZW;
Image(subTexture.Texture.GetViewBinding(), size, (.)subTexture.TexCoords.XY, (.)v, tint_col, border_col);
}
@@ -92,7 +92,7 @@ namespace ImGui
if (uv0 != .Zero || uv1 != .Ones)
Runtime.NotImplemented();
Vector2 v = (.)subTexture.TexCoords.XY + subTexture.TexCoords.ZW;
float2 v = (.)subTexture.TexCoords.XY + subTexture.TexCoords.ZW;
return ImageButton(subTexture.Texture.GetViewBinding(), size, (.)subTexture.TexCoords.XY, (.)v, frame_padding, bg_col, tint_col);
}
@@ -107,19 +107,19 @@ namespace ImGui
protected internal static extern void CleanupFrame();
/// Control to edit a vector 2 with drag functionality and reset buttons
public static bool EditVector2(StringView label, ref Vector2 value, Vector2 resetValues = .Zero, float dragSpeed = 0.1f, float columnWidth = 100f, Vector2 minValue = .Zero, Vector2 maxValue = .Zero)
public static bool Editfloat2(StringView label, ref float2 value, float2 resetValues = .Zero, float dragSpeed = 0.1f, float columnWidth = 100f, float2 minValue = .Zero, float2 maxValue = .Zero)
{
return EditVector<2>(label, ref *(float[2]*)&value, (float[2])resetValues, dragSpeed, columnWidth, (float[2])minValue, (float[2])maxValue);
}
/// Control to edit a vector 3 with drag functionality and reset buttons
public static bool EditVector3(StringView label, ref Vector3 value, Vector3 resetValues = .Zero, float dragSpeed = 0.1f, float columnWidth = 100f, Vector3 minValue = .Zero, Vector3 maxValue = .Zero)
public static bool Editfloat3(StringView label, ref float3 value, float3 resetValues = .Zero, float dragSpeed = 0.1f, float columnWidth = 100f, float3 minValue = .Zero, float3 maxValue = .Zero)
{
return EditVector<3>(label, ref *(float[3]*)&value, (float[3])resetValues, dragSpeed, columnWidth, (float[3])minValue, (float[3])maxValue);
}
/// Control to edit a vector 4 with drag functionality and reset buttons
public static bool EditVector4(StringView label, ref Vector4 value, Vector4 resetValues = .Zero, float dragSpeed = 0.1f, float columnWidth = 100f, Vector4 minValue = .Zero, Vector4 maxValue = .Zero)
public static bool Editfloat4(StringView label, ref float4 value, float4 resetValues = .Zero, float dragSpeed = 0.1f, float columnWidth = 100f, float4 minValue = .Zero, float4 maxValue = .Zero)
{
return EditVector<4>(label, ref *(float[4]*)&value, (float[4])resetValues, dragSpeed, columnWidth, (float[4])minValue, (float[4])maxValue);
}
+2 -2
View File
@@ -53,10 +53,10 @@ namespace GlitchyEngine.Math
[Inline]
#unwarn
public static explicit operator Vector3(ColorRGB color) => *(Vector3*)&color;
public static explicit operator float3(ColorRGB color) => *(float3*)&color;
[Inline]
#unwarn
public static explicit operator ColorRGB(Vector3 color) => *(ColorRGB*)&color;
public static explicit operator ColorRGB(float3 color) => *(ColorRGB*)&color;
}
}
+2 -2
View File
@@ -329,10 +329,10 @@ namespace GlitchyEngine.Math
[Inline]
#unwarn
public static explicit operator Vector4(ColorRGBA color) => *(Vector4*)&color;
public static explicit operator float4(ColorRGBA color) => *(float4*)&color;
[Inline]
#unwarn
public static explicit operator ColorRGBA(Vector4 color) => *(ColorRGBA*)&color;
public static explicit operator ColorRGBA(float4 color) => *(ColorRGBA*)&color;
}
}
+3 -3
View File
@@ -13,12 +13,12 @@ namespace System
}
[Inline]
public Vector2 XX => Vector2((float)this);
public float2 XX => (float2)this;
[Inline]
public Vector3 XXX => Vector3((float)this);
public float3 XXX => (float3)this;
[Inline]
public Vector4 XXXX => Vector4((float)this);
public float4 XXXX => (float4)this;
}
}
+560 -259
View File
@@ -1,8 +1,13 @@
using System;
namespace GlitchyEngine.Math.FancyMath;
static class FancyMath
namespace GlitchyEngine.Math;
// TODO: Not all functions are available for scalars yet
static
{
/// Returns true if at least one of the components is true.
public static bool any(bool value) => value;
/// Returns true if at least one of the components is true.
public static bool any(bool2 value) => value.X || value.Y;
/// Returns true if at least one of the components is true.
@@ -10,6 +15,8 @@ static class FancyMath
/// Returns true if at least one of the components is true.
public static bool any(bool4 value) => value.X || value.Y || value.Z || value.W;
/// Returns true if all of the components are true.
public static bool all(bool value) => value;
/// Returns true if all of the components are true.
public static bool all(bool2 value) => value.X && value.Y;
/// Returns true if all of the components are true.
@@ -19,6 +26,11 @@ static class FancyMath
#region abs
public static float abs(float value)
{
return Math.Abs(value);
}
public static float2 abs(float2 value)
{
return float2(Math.Abs(value.X), Math.Abs(value.Y));
@@ -34,6 +46,11 @@ static class FancyMath
return float4(Math.Abs(value.X), Math.Abs(value.Y), Math.Abs(value.Z), Math.Abs(value.W));
}
public static int abs(int value)
{
return Math.Abs(value);
}
public static int2 abs(int2 value)
{
return int2(Math.Abs(value.X), Math.Abs(value.Y));
@@ -50,247 +67,9 @@ static class FancyMath
}
#endregion
#region ceil / floor
public static float2 ceil(float2 value)
{
return float2(Math.Ceiling(value.X), Math.Ceiling(value.Y));
}
public static float3 ceil(float3 value)
{
return float3(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z));
}
public static float4 ceil(float4 value)
{
return float4(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z), Math.Ceiling(value.W));
}
public static float2 floor(float2 value)
{
return float2(Math.Floor(value.X), Math.Floor(value.Y));
}
public static float3 floor(float3 value)
{
return float3(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z));
}
public static float4 floor(float4 value)
{
return float4(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z), Math.Floor(value.W));
}
#endregion
#region Clamp
public static float2 clamp(float2 value, float2 min, float2 max)
{
return float2(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y));
}
public static float3 clamp(float3 value, float3 min, float3 max)
{
return float3(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z));
}
public static float4 clamp(float4 value, float4 min, float4 max)
{
return float4(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z), Math.Clamp(value.W, min.W, max.W));
}
public static int2 clamp(int2 value, int2 min, int2 max)
{
return int2(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y));
}
public static int3 clamp(int3 value, int3 min, int3 max)
{
return int3(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z));
}
public static int4 clamp(int4 value, int4 min, int4 max)
{
return int4(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z), Math.Clamp(value.W, min.W, max.W));
}
#endregion
#region Lerp
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float2 lerp(float2 x, float2 y, float s)
{
return x + s * (y - x);
}
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float3 lerp(float3 x, float3 y, float s)
{
return x + s * (y - x);
}
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float4 lerp(float4 x, float4 y, float s)
{
return x + s * (y - x);
}
#endregion
// log, log10, log2
#region min / max
public static float2 min(float2 x, float2 y)
{
return float2(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y));
}
public static float3 min(float3 x, float3 y)
{
return float3(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y), Math.Min(x.Z, y.Z));
}
public static float4 min(float4 x, float4 y)
{
return float4(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y), Math.Min(x.Z, y.Z), Math.Min(x.W, y.W));
}
public static float2 max(float2 x, float2 y)
{
return float2(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y));
}
public static float3 max(float3 x, float3 y)
{
return float3(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y), Math.Max(x.Z, y.Z));
}
public static float4 max(float4 x, float4 y)
{
return float4(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y), Math.Max(x.Z, y.Z), Math.Max(x.W, y.W));
}
#endregion
// mul
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
// rcp??? (reciprocal)
// reflect and refract?
// round
// rsqrt?
// sqrt
// saturate
// sign
// step and smoothstep
// transpose
#region exp
/// Returns the base-e exponential, or e^x, of the specified value.
public static float2 exp(float2 x)
{
return float2(Math.Exp(x.X), Math.Exp(x.Y));
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float3 exp(float3 x)
{
return float3(Math.Exp(x.X), Math.Exp(x.Y), Math.Exp(x.Z));
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float4 exp(float4 x)
{
return float4(Math.Exp(x.X), Math.Exp(x.Y), Math.Exp(x.Z), Math.Exp(x.W));
}
// Exp2?
#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
// Splits the value x into fractional and integer parts, each of which has the same sign as x.
public static float2 modf(float2 x, out float2 integerPart)
{
@@ -301,7 +80,7 @@ static class FancyMath
return fracPart;
}
// Splits the value x into fractional and integer parts, each of which has the same sign as x.
public static float3 modf(float3 x, out float3 integerPart)
{
@@ -313,7 +92,7 @@ static class FancyMath
return fracPart;
}
// Splits the value x into fractional and integer parts, each of which has the same sign as x.
public static float4 modf(float4 x, out float4 integerPart)
{
@@ -383,19 +162,19 @@ static class FancyMath
{
return bool2(value.X.IsInfinity, value.Y.IsInfinity);
}
/// Determines if the specified value is infinite.
public static bool3 isinf(float3 value)
{
return bool3(value.X.IsInfinity, value.Y.IsInfinity, value.Z.IsInfinity);
}
/// Determines if the specified value is infinite.
public static bool4 isinf(float4 value)
{
return bool4(value.X.IsInfinity, value.Y.IsInfinity, value.Z.IsInfinity, value.W.IsInfinity);
}
/// Determines if the specified value is infinite.
public static bool2 isnan(float2 value)
{
@@ -416,6 +195,541 @@ static class FancyMath
#endregion
#region Sign
/// Returns the sign of x.
public static int32 sign(float x)
{
return (int32)Math.Sign(x);
}
/// Returns the sign of x.
public static int2 sign(float2 x)
{
return int2((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y));
}
/// Returns the sign of x.
public static int3 sign(float3 x)
{
return int3((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y), (int32)Math.Sign(x.Z));
}
/// Returns the sign of x.
public static int4 sign(float4 x)
{
return int4((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y), (int32)Math.Sign(x.Z), (int32)Math.Sign(x.W));
}
/// Returns the sign of x.
public static int32 sign(int x)
{
return (int32)Math.Sign(x);
}
/// Returns the sign of x.
public static int2 sign(int2 x)
{
return int2((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y));
}
/// Returns the sign of x.
public static int3 sign(int3 x)
{
return int3((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y), (int32)Math.Sign(x.Z));
}
/// Returns the sign of x.
public static int4 sign(int4 x)
{
return int4((int32)Math.Sign(x.X), (int32)Math.Sign(x.Y), (int32)Math.Sign(x.Z), (int32)Math.Sign(x.W));
}
#endregion
#region ceil / floor / round
public static float ceil(float value)
{
return Math.Ceiling(value);
}
public static float2 ceil(float2 value)
{
return float2(Math.Ceiling(value.X), Math.Ceiling(value.Y));
}
public static float3 ceil(float3 value)
{
return float3(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z));
}
public static float4 ceil(float4 value)
{
return float4(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z), Math.Ceiling(value.W));
}
public static float floor(float value)
{
return Math.Floor(value);
}
public static float2 floor(float2 value)
{
return float2(Math.Floor(value.X), Math.Floor(value.Y));
}
public static float3 floor(float3 value)
{
return float3(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z));
}
public static float4 floor(float4 value)
{
return float4(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z), Math.Floor(value.W));
}
/// Rounds the specified value to the nearest integer.
public static float round(float value)
{
return Math.Round(value);
}
/// Rounds the specified value to the nearest integer.
public static float2 round(float2 value)
{
return float2(Math.Round(value.X), Math.Round(value.Y));
}
/// Rounds the specified value to the nearest integer.
public static float3 round(float3 value)
{
return float3(Math.Round(value.X), Math.Round(value.Y), Math.Round(value.Z));
}
/// Rounds the specified value to the nearest integer.
public static float4 round(float4 value)
{
return float4(Math.Round(value.X), Math.Round(value.Y), Math.Round(value.Z), Math.Round(value.W));
}
#endregion
#region min / max
public static float2 min(float2 x, float2 y)
{
return float2(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y));
}
public static float3 min(float3 x, float3 y)
{
return float3(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y), Math.Min(x.Z, y.Z));
}
public static float4 min(float4 x, float4 y)
{
return float4(Math.Min(x.X, y.X), Math.Min(x.Y, y.Y), Math.Min(x.Z, y.Z), Math.Min(x.W, y.W));
}
public static float2 max(float2 x, float2 y)
{
return float2(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y));
}
public static float3 max(float3 x, float3 y)
{
return float3(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y), Math.Max(x.Z, y.Z));
}
public static float4 max(float4 x, float4 y)
{
return float4(Math.Max(x.X, y.X), Math.Max(x.Y, y.Y), Math.Max(x.Z, y.Z), Math.Max(x.W, y.W));
}
#endregion
#region exp, pow, log
// TODO: log, log10, log2
/// Returns x raised to the power of y.
public static float pow(float x, float y)
{
return Math.Pow(x, y);
}
/// Returns x raised to the power of y.
public static float2 pow(float2 x, float2 y)
{
return float2(Math.Pow(x.X, y.X), Math.Pow(x.Y, y.Y));
}
/// Returns x raised to the power of y.
public static float3 pow(float3 x, float3 y)
{
return float3(Math.Pow(x.X, y.X), Math.Pow(x.Y, y.Y), Math.Pow(x.Z, y.Z));
}
/// Returns x raised to the power of y.
public static float4 pow(float4 x, float4 y)
{
return float4(Math.Pow(x.X, y.X), Math.Pow(x.Y, y.Y), Math.Pow(x.Z, y.Z), Math.Pow(x.W, y.W));
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float exp(float x)
{
return Math.Exp(x);
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float2 exp(float2 x)
{
return float2(Math.Exp(x.X), Math.Exp(x.Y));
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float3 exp(float3 x)
{
return float3(Math.Exp(x.X), Math.Exp(x.Y), Math.Exp(x.Z));
}
/// Returns the base-e exponential, or e^x, of the specified value.
public static float4 exp(float4 x)
{
return float4(Math.Exp(x.X), Math.Exp(x.Y), Math.Exp(x.Z), Math.Exp(x.W));
}
/// Returns the base 2 exponential, or 2^x, of the specified value.
public static float exp2(float x)
{
return Math.Pow(2, x);
}
/// Returns the base 2 exponential, or 2^x, of the specified value.
public static float2 exp2(float2 x)
{
return float2(Math.Pow(2, x.X), Math.Pow(2, x.Y));
}
/// Returns the base 2 exponential, or 2^x, of the specified value.
public static float3 exp2(float3 x)
{
return float3(Math.Pow(2, x.X), Math.Pow(2, x.Y), Math.Pow(2, x.Z));
}
/// Returns the base 2 exponential, or 2^x, of the specified value.
public static float4 exp2(float4 x)
{
return float4(Math.Pow(2, x.X), Math.Pow(2, x.Y), Math.Pow(2, x.Z), Math.Pow(2, x.W));
}
#endregion
#region Degrees / Radians
public static float toDegrees(float radians) => radians * MathHelper.RadToDeg;
public static float2 toDegrees(float2 radians) => radians * MathHelper.RadToDeg;
public static float3 toDegrees(float3 radians) => radians * MathHelper.RadToDeg;
public static float4 toDegrees(float4 radians) => radians * MathHelper.RadToDeg;
public static float toRadians(float degrees) => degrees * MathHelper.DegToRad;
public static float2 toRadians(float2 degrees) => degrees * MathHelper.DegToRad;
public static float3 toRadians(float3 degrees) => degrees * MathHelper.DegToRad;
public static float4 toRadians(float4 degrees) => degrees * MathHelper.DegToRad;
#endregion
#region Clamp
public static float clamp(float value, float min, float max)
{
return Math.Clamp(value.X, min.X, max.X);
}
public static float2 clamp(float2 value, float2 min, float2 max)
{
return float2(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y));
}
public static float3 clamp(float3 value, float3 min, float3 max)
{
return float3(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z));
}
public static float4 clamp(float4 value, float4 min, float4 max)
{
return float4(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z), Math.Clamp(value.W, min.W, max.W));
}
public static int2 clamp(int2 value, int2 min, int2 max)
{
return int2(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y));
}
public static int3 clamp(int3 value, int3 min, int3 max)
{
return int3(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z));
}
public static int4 clamp(int4 value, int4 min, int4 max)
{
return int4(Math.Clamp(value.X, min.X, max.X), Math.Clamp(value.Y, min.Y, max.Y), Math.Clamp(value.Z, min.Z, max.Z), Math.Clamp(value.W, min.W, max.W));
}
#endregion
#region Lerp
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float lerp(float x, float y, float s)
{
return x + s * (y - x);
}
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float2 lerp(float2 x, float2 y, float s)
{
return x + s * (y - x);
}
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float3 lerp(float3 x, float3 y, float s)
{
return x + s * (y - x);
}
/// Performs a linear interpolation.
// @param x The first vector value.
// @param y The second vector value.
// @param y A value that linearly interpolates between x and y.
public static float4 lerp(float4 x, float4 y, float s)
{
return x + s * (y - x);
}
#endregion
// mul? hlsl has like 280 overloads...
public static float normalize(float value) => 1.0f;
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
#region Reflect and Refract
/// Returns a reflection vector using an incident ray and a surface normal.
public static float2 reflect(float2 incident, float2 normal) => incident - 2 * normal * dot(incident, normal);
/// Returns a reflection vector using an incident ray and a surface normal.
public static float3 reflect(float3 incident, float3 normal) => incident - 2 * normal * dot(incident, normal);
/// Returns a reflection vector using an incident ray and a surface normal.
public static float4 reflect(float4 incident, float4 normal) => incident - 2 * normal * dot(incident, normal);
// Source: https://thebookofshaders.com/glossary/?search=refract
/// Returns a refraction vector using an entering ray, a surface normal, and a refraction index.
public static float2 refract(float2 incident, float2 normal, float refractionIndex)
{
float dot_n_i = dot(normal, incident);
float k = 1.0f - refractionIndex * refractionIndex * (1.0f - dot_n_i * dot_n_i);
if (k < 0.0f)
return 0.0f;
else
return refractionIndex * incident - (refractionIndex * dot_n_i + sqrt(k));
}
/// Returns a refraction vector using an entering ray, a surface normal, and a refraction index.
public static float3 refract(float3 incident, float3 normal, float refractionIndex)
{
float dot_n_i = dot(normal, incident);
float k = 1.0f - refractionIndex * refractionIndex * (1.0f - dot_n_i * dot_n_i);
if (k < 0.0f)
return 0.0f;
else
return refractionIndex * incident - (refractionIndex * dot_n_i + sqrt(k));
}
/// Returns a refraction vector using an entering ray, a surface normal, and a refraction index.
public static float4 refract(float4 incident, float4 normal, float refractionIndex)
{
float dot_n_i = dot(normal, incident);
float k = 1.0f - refractionIndex * refractionIndex * (1.0f - dot_n_i * dot_n_i);
if (k < 0.0f)
return 0.0f;
else
return refractionIndex * incident - (refractionIndex * dot_n_i + sqrt(k));
}
#endregion
/// Calculates the per component square root of the given value.
public static float sqrt(float value)
{
return Math.Sqrt(value);
}
/// Calculates the per component square root of the given value.
public static float2 sqrt(float2 value)
{
return float2(Math.Sqrt(value.X), Math.Sqrt(value.Y));
}
/// Calculates the per component square root of the given value.
public static float3 sqrt(float3 value)
{
return float3(Math.Sqrt(value.X), Math.Sqrt(value.Y), Math.Sqrt(value.Z));
}
/// Calculates the per component square root of the given value.
public static float4 sqrt(float4 value)
{
return float4(Math.Sqrt(value.X), Math.Sqrt(value.Y), Math.Sqrt(value.Z), Math.Sqrt(value.W));
}
// saturate (I don't think there is a faster way than simply using clamp, so simply use clamp...)
#region Step / Smoothstep
/// Compares two values, returning 0 or 1 based on which value is greater.
/// @returns 1 if the x parameter is greater than or equal to the y parameter; otherwise, 0.
public static float step(float y, float x)
{
return (x >= y) ? 1.0f : 0.0f;
}
/// Compares two values, returning 0 or 1 based on which value is greater.
/// @returns 1 if the x parameter is greater than or equal to the y parameter; otherwise, 0.
public static float2 step(float2 y, float2 x)
{
bool2 b = (x >= y);
return float2(b.X ? 1.0f : 0.0f, b.Y ? 1.0f : 0.0f);
}
/// Compares two values, returning 0 or 1 based on which value is greater.
/// @returns 1 if the x parameter is greater than or equal to the y parameter; otherwise, 0.
public static float3 step(float3 y, float3 x)
{
bool3 b = (x >= y);
return float3(b.X ? 1.0f : 0.0f, b.Y ? 1.0f : 0.0f, b.Z ? 1.0f : 0.0f);
}
/// Compares two values, returning 0 or 1 based on which value is greater.
/// @returns 1 if the x parameter is greater than or equal to the y parameter; otherwise, 0.
public static float4 step(float4 y, float4 x)
{
bool4 b = (x >= y);
return float4(b.X ? 1.0f : 0.0f, b.Y ? 1.0f : 0.0f, b.Z ? 1.0f : 0.0f, b.W ? 1.0f : 0.0f);
}
// Source: https://thebookofshaders.com/glossary/?search=smoothstep
/// Returns a smooth Hermite interpolation between 0 and 1, if x is in the range [min, max].
/// @returns Returns 0 if x is less than min; 1 if x is greater than max; otherwise, a value between 0 and 1 if x is in the range [min, max].
public static float smoothstep(float min, float max, float x)
{
float t = clamp((x - min) / (max - min), 0.0f, 1.0f);
return t * t * (3.0f - 2.0f * t);
}
/// Returns a smooth Hermite interpolation between 0 and 1, if x is in the range [min, max].
/// @returns Returns 0 if x is less than min; 1 if x is greater than max; otherwise, a value between 0 and 1 if x is in the range [min, max].
public static float2 smoothstep(float2 min, float2 max, float2 x)
{
float2 t = clamp((x - min) / (max - min), 0.0f, 1.0f);
return t * t * (3.0f - 2.0f * t);
}
/// Returns a smooth Hermite interpolation between 0 and 1, if x is in the range [min, max].
/// @returns Returns 0 if x is less than min; 1 if x is greater than max; otherwise, a value between 0 and 1 if x is in the range [min, max].
public static float3 smoothstep(float3 min, float3 max, float3 x)
{
float3 t = clamp((x - min) / (max - min), 0.0f, 1.0f);
return t * t * (3.0f - 2.0f * t);
}
/// Returns a smooth Hermite interpolation between 0 and 1, if x is in the range [min, max].
/// @returns Returns 0 if x is less than min; 1 if x is greater than max; otherwise, a value between 0 and 1 if x is in the range [min, max].
public static float4 smoothstep(float4 min, float4 max, float4 x)
{
float4 t = clamp((x - min) / (max - min), 0.0f, 1.0f);
return t * t * (3.0f - 2.0f * t);
}
#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 dot
@@ -495,20 +809,7 @@ static class FancyMath
left.X * right.Y - left.Y * right.X);
}
#region Degrees / Radians
public static float2 toDegrees(float2 radians) => radians * MathHelper.RadToDeg;
public static float3 toDegrees(float3 radians) => radians * MathHelper.RadToDeg;
public static float4 toDegrees(float4 radians) => radians * MathHelper.RadToDeg;
public static float2 toRadians(float2 degrees) => degrees * MathHelper.DegToRad;
public static float3 toRadians(float3 degrees) => degrees * MathHelper.DegToRad;
public static float4 toRadians(float4 degrees) => degrees * MathHelper.DegToRad;
#endregion
// transpose und determinante für Matrizen
// sin, cos, tan, asin, acos, atan, atan2, cosh, sinh, tanh
}
+13 -13
View File
@@ -30,19 +30,19 @@ namespace GlitchyEngine.Math
}
// Converts the given radians to degrees
public static Vector2 ToDegrees(Vector2 radians)
public static float2 ToDegrees(float2 radians)
{
return radians * RadToDeg;
}
// Converts the given radians to degrees
public static Vector3 ToDegrees(Vector3 radians)
public static float3 ToDegrees(float3 radians)
{
return radians * RadToDeg;
}
// Converts the given radians to degrees
public static Vector4 ToDegrees(Vector4 radians)
public static float4 ToDegrees(float4 radians)
{
return radians * RadToDeg;
}
@@ -54,19 +54,19 @@ namespace GlitchyEngine.Math
}
// Converts the given degrees to radians
public static Vector2 ToRadians(Vector2 degrees)
public static float2 ToRadians(float2 degrees)
{
return degrees * DegToRad;
}
// Converts the given degrees to radians
public static Vector3 ToRadians(Vector3 degrees)
public static float3 ToRadians(float3 degrees)
{
return degrees * DegToRad;
}
// Converts the given degrees to radians
public static Vector4 ToRadians(Vector4 degrees)
public static float4 ToRadians(float4 degrees)
{
return degrees * DegToRad;
}
@@ -79,24 +79,24 @@ namespace GlitchyEngine.Math
}
/// Returns the point that lies on the unit circle at the specified angle.
public static Vector2 CirclePoint(float angle, float radius = 1.0f)
public static float2 CirclePoint(float angle, float radius = 1.0f)
{
return .(Math.Cos(angle), Math.Sin(angle)) * radius;
}
public static Vector2 Pow(Vector2 v, float p)
public static float2 Pow(float2 v, float p)
{
return Vector2(Math.Pow(v.X, p), Math.Pow(v.Y, p));
return float2(Math.Pow(v.X, p), Math.Pow(v.Y, p));
}
public static Vector3 Pow(Vector3 v, float p)
public static float3 Pow(float3 v, float p)
{
return Vector3(Math.Pow(v.X, p), Math.Pow(v.Y, p), Math.Pow(v.Z, p));
return float3(Math.Pow(v.X, p), Math.Pow(v.Y, p), Math.Pow(v.Z, p));
}
public static Vector4 Pow(Vector4 v, float p)
public static float4 Pow(float4 v, float p)
{
return Vector4(Math.Pow(v.X, p), Math.Pow(v.Y, p), Math.Pow(v.Z, p), Math.Pow(v.W, p));
return float4(Math.Pow(v.X, p), Math.Pow(v.Y, p), Math.Pow(v.Z, p), Math.Pow(v.W, p));
}
}
}
+81 -84
View File
@@ -21,7 +21,7 @@ public struct Matrix
public using Values V;
public float[4][4] Values;
public Vector4[4] Columns;
public float4[4] Columns;
/// Creates a new zero-matrix.
public this() => this = default;
@@ -53,7 +53,7 @@ public struct Matrix
}
/// Creates a new matrix and initializes it with the given column-vectors.
public this(Vector4 c0, Vector4 c1, Vector4 c2, Vector4 c3)
public this(float4 c0, float4 c1, float4 c2, float4 c3)
{
Columns[0] = c0;
Columns[1] = c1;
@@ -61,47 +61,47 @@ public struct Matrix
Columns[3] = c3;
}
public ref Vector3 Right
public ref float3 Right
{
[Inline]
get
{
#unwarn
return ref *(Vector3*)&Columns[0];
return ref *(float3*)&Columns[0];
}
}
public ref Vector3 Up
public ref float3 Up
{
[Inline]
get
{
#unwarn
return ref *(Vector3*)&Columns[1];
return ref *(float3*)&Columns[1];
}
}
public ref Vector3 Forward
public ref float3 Forward
{
[Inline]
get
{
#unwarn
return ref *(Vector3*)&Columns[2];
return ref *(float3*)&Columns[2];
}
}
public ref Vector3 Translation
public ref float3 Translation
{
[Inline]
get
{
#unwarn
return ref *(Vector3*)&Columns[3];
return ref *(float3*)&Columns[3];
}
}
public Vector3 Scale
public float3 Scale
{
[Inline]
get => .(_11, _22, _33);
@@ -134,12 +134,12 @@ public struct Matrix
}
}
public ref Vector4 this[int column]
public ref float4 this[int column]
{
get
{
#unwarn
return ref *(Vector4*)&Columns[column];
return ref *(float4*)&Columns[column];
}
@@ -150,7 +150,7 @@ public struct Matrix
Internal.ThrowIndexOutOfRange();
#unwarn
return ref *(Vector4*)&Columns[column];
return ref *(float4*)&Columns[column];
}
}
@@ -351,12 +351,12 @@ public struct Matrix
/**
* Multiplies a matrix and a column-vector resulting in a column vector.
*/
public static Vector4 operator *(Matrix matrix, Vector4 columnVector)
public static float4 operator *(Matrix matrix, float4 columnVector)
{
#unwarn
var m = &matrix.V;
Vector4 result = ?;
float4 result = ?;
result.X = (m._11 * columnVector.X) + (m._12 * columnVector.Y) + (m._13 * columnVector.Z) + (m._14 * columnVector.W);
result.Y = (m._21 * columnVector.X) + (m._22 * columnVector.Y) + (m._23 * columnVector.Z) + (m._24 * columnVector.W);
result.Z = (m._31 * columnVector.X) + (m._32 * columnVector.Y) + (m._33 * columnVector.Z) + (m._34 * columnVector.W);
@@ -367,12 +367,12 @@ public struct Matrix
/**
* Multiplies a row-vector and a matrix resulting in a row vector.
*/
public static Vector4 operator *(Vector4 rowVector, Matrix matrix)
public static float4 operator *(float4 rowVector, Matrix matrix)
{
#unwarn
var m = &matrix.V;
Vector4 result = ?;
float4 result = ?;
result.X = (rowVector.X * m._11) + (rowVector.Y * m._21) + (rowVector.Z * m._31) + (rowVector.W * m._41);
result.Y = (rowVector.X * m._12) + (rowVector.Y * m._22) + (rowVector.Z * m._32) + (rowVector.W * m._42);
result.Z = (rowVector.X * m._13) + (rowVector.Y * m._23) + (rowVector.Z * m._33) + (rowVector.W * m._43);
@@ -406,7 +406,7 @@ public struct Matrix
0, 0, 0, 1);
}
public static Matrix Scaling(Vector3 scale)
public static Matrix Scaling(float3 scale)
{
return .(scale.X, 0, 0, 0,
0, scale.Y, 0, 0,
@@ -422,7 +422,7 @@ public struct Matrix
0, 0, 0, 1);
}
public static Matrix Translation(Vector3 translation)
public static Matrix Translation(float3 translation)
{
return .(1, 0, 0, translation.X,
0, 1, 0, translation.Y,
@@ -470,25 +470,22 @@ public struct Matrix
* @param up A vector defining the up direction of the camera.
* @returns a view matrix.
*/
public static Matrix LookAt(Vector3 position, Vector3 target, Vector3 up)
public static Matrix LookAt(float3 position, float3 target, float3 up)
{
Vector3 forward = target - position;
forward.Normalize();
float3 forward = normalize(target - position);
Vector3 right = Vector3.Cross(up, forward);
right.Normalize();
float3 right = normalize(cross(up, forward));
Vector3 newUp = Vector3.Cross(forward, right);
newUp.Normalize();
float3 newUp = normalize(cross(forward, right));
Matrix result = .Identity;
result.Forward = forward;
result.Up = up;
result.Right = right;
result.Translation.X = -Vector3.Dot(position, right);
result.Translation.Y = -Vector3.Dot(position, up);
result.Translation.Z = -Vector3.Dot(position, forward);
result.Translation.X = -dot(position, right);
result.Translation.Y = -dot(position, up);
result.Translation.Z = -dot(position, forward);
return result;
}
@@ -511,22 +508,22 @@ public struct Matrix
{
// From: Lengyel, Eric. Foundations of Game Engine Development, Volume 1: Mathematics (S.61). Kindle-Version.
Vector3 a = *(Vector3*)&Columns[0];
Vector3 b = *(Vector3*)&Columns[1];
Vector3 c = *(Vector3*)&Columns[2];
Vector3 d = *(Vector3*)&Columns[3];
float3 a = *(float3*)&Columns[0];
float3 b = *(float3*)&Columns[1];
float3 c = *(float3*)&Columns[2];
float3 d = *(float3*)&Columns[3];
float x = this[3, 0];
float y = this[3, 1];
float z = this[3, 2];
float w = this[3, 3];
Vector3 s = Vector3.Cross(a, b);
Vector3 t = Vector3.Cross(c, d);
Vector3 u = y * a - x * b;
Vector3 v = w * c - z * d;
float3 s = cross(a, b);
float3 t = cross(c, d);
float3 u = y * a - x * b;
float3 v = w * c - z * d;
return Vector3.Dot(s, v) + Vector3.Dot(t, u);
return dot(s, v) + dot(t, u);
}
/**
@@ -537,39 +534,39 @@ public struct Matrix
// From: Lengyel, Eric. Foundations of Game Engine Development, Volume 1: Mathematics (S.61). Kindle-Version.
#unwarn
Vector3 a = *(Vector3*)&Columns[0];
float3 a = *(float3*)&Columns[0];
#unwarn
Vector3 b = *(Vector3*)&Columns[1];
float3 b = *(float3*)&Columns[1];
#unwarn
Vector3 c = *(Vector3*)&Columns[2];
float3 c = *(float3*)&Columns[2];
#unwarn
Vector3 d = *(Vector3*)&Columns[3];
float3 d = *(float3*)&Columns[3];
float x = this[3, 0];
float y = this[3, 1];
float z = this[3, 2];
float w = this[3, 3];
Vector3 s = Vector3.Cross(a, b);
Vector3 t = Vector3.Cross(c, d);
Vector3 u = y * a - x * b;
Vector3 v = w * c - z * d;
float3 s = cross(a, b);
float3 t = cross(c, d);
float3 u = y * a - x * b;
float3 v = w * c - z * d;
float invDet = 1.0f / (Vector3.Dot(s, v) + Vector3.Dot(t, u));
float invDet = 1.0f / (dot(s, v) + dot(t, u));
s *= invDet;
t *= invDet;
u *= invDet;
v *= invDet;
Vector3 r0 = Vector3.Cross(b, v) + t * y;
Vector3 r1 = Vector3.Cross(v, a) - t * x;
Vector3 r2 = Vector3.Cross(d, u) + s * w;
Vector3 r3 = Vector3.Cross(u, c) - s * z;
return .(r0.X, r0.Y, r0.Z, -Vector3.Dot(b, t),
r1.X, r1.Y, r1.Z, Vector3.Dot(a, t),
r2.X, r2.Y, r2.Z, -Vector3.Dot(d, s),
r3.X, r3.Y, r3.Z, Vector3.Dot(c, s));
float3 r0 = cross(b, v) + t * y;
float3 r1 = cross(v, a) - t * x;
float3 r2 = cross(d, u) + s * w;
float3 r3 = cross(u, c) - s * z;
return .(r0.X, r0.Y, r0.Z, -dot(b, t),
r1.X, r1.Y, r1.Z, dot(a, t),
r2.X, r2.Y, r2.Z, -dot(d, s),
r3.X, r3.Y, r3.Z, dot(c, s));
}
/// Calculates the inverse of the matrix.
@@ -578,39 +575,39 @@ public struct Matrix
// From: Lengyel, Eric. Foundations of Game Engine Development, Volume 1: Mathematics (S.61). Kindle-Version.
#unwarn
Vector3 a = *(Vector3*)&matrix.Columns[0];
float3 a = *(float3*)&matrix.Columns[0];
#unwarn
Vector3 b = *(Vector3*)&matrix.Columns[1];
float3 b = *(float3*)&matrix.Columns[1];
#unwarn
Vector3 c = *(Vector3*)&matrix.Columns[2];
float3 c = *(float3*)&matrix.Columns[2];
#unwarn
Vector3 d = *(Vector3*)&matrix.Columns[3];
float3 d = *(float3*)&matrix.Columns[3];
float x = matrix[3, 0];
float y = matrix[3, 1];
float z = matrix[3, 2];
float w = matrix[3, 3];
Vector3 s = Vector3.Cross(a, b);
Vector3 t = Vector3.Cross(c, d);
Vector3 u = y * a - x * b;
Vector3 v = w * c - z * d;
float3 s = cross(a, b);
float3 t = cross(c, d);
float3 u = y * a - x * b;
float3 v = w * c - z * d;
float invDet = 1.0f / (Vector3.Dot(s, v) + Vector3.Dot(t, u));
float invDet = 1.0f / (dot(s, v) + dot(t, u));
s *= invDet;
t *= invDet;
u *= invDet;
v *= invDet;
Vector3 r0 = Vector3.Cross(b, v) + t * y;
Vector3 r1 = Vector3.Cross(v, a) - t * x;
Vector3 r2 = Vector3.Cross(d, u) + s * w;
Vector3 r3 = Vector3.Cross(u, c) - s * z;
return .(r0.X, r0.Y, r0.Z, -Vector3.Dot(b, t),
r1.X, r1.Y, r1.Z, Vector3.Dot(a, t),
r2.X, r2.Y, r2.Z, -Vector3.Dot(d, s),
r3.X, r3.Y, r3.Z, Vector3.Dot(c, s));
float3 r0 = cross(b, v) + t * y;
float3 r1 = cross(v, a) - t * x;
float3 r2 = cross(d, u) + s * w;
float3 r3 = cross(u, c) - s * z;
return .(r0.X, r0.Y, r0.Z, -dot(b, t),
r1.X, r1.Y, r1.Z, dot(a, t),
r2.X, r2.Y, r2.Z, -dot(d, s),
r3.X, r3.Y, r3.Z, dot(c, s));
}
/**
@@ -792,9 +789,9 @@ public struct Matrix
*/
public void Orthogonalize() mut
{
Columns[1] -= .Project(Columns[1], Columns[0]);
Columns[2] -= .Project(Columns[2], Columns[0]) + .Project(Columns[2], Columns[1]);
Columns[3] -= .Project(Columns[3], Columns[0]) + .Project(Columns[3], Columns[1]) + .Project(Columns[3], Columns[2]);
Columns[1] -= project(Columns[1], Columns[0]);
Columns[2] -= project(Columns[2], Columns[0]) + project(Columns[2], Columns[1]);
Columns[3] -= project(Columns[3], Columns[0]) + project(Columns[3], Columns[1]) + project(Columns[3], Columns[2]);
}
/**
@@ -802,10 +799,10 @@ public struct Matrix
*/
public void Orthonormalize() mut
{
Columns[0].Normalize();
Columns[1] = .Normalize(.Reject(Columns[1], Columns[0]));
Columns[2] = .Normalize(.Reject(.Reject(Columns[2], Columns[0]), Columns[1]));
Columns[3] = .Normalize(.Reject(.Reject(.Reject(Columns[2], Columns[0]), Columns[1]), Columns[2]));
Columns[0] = normalize(Columns[0]);
Columns[1] = normalize(reject(Columns[1], Columns[0]));
Columns[2] = normalize(reject(reject(Columns[2], Columns[0]), Columns[1]));
Columns[3] = normalize(reject(reject(reject(Columns[2], Columns[0]), Columns[1]), Columns[2]));
}
public static Self RotationQuaternion(Quaternion rotation)
{
@@ -845,7 +842,7 @@ public struct Matrix
return result;
}
public static void Decompose(Self matrix, out Vector3 position, out Quaternion rotation, out Vector3 scale)
public static void Decompose(Self matrix, out float3 position, out Quaternion rotation, out float3 scale)
{
var matrix;
@@ -857,9 +854,9 @@ public struct Matrix
// TODO: this doesn't detect mirroring
// Extract scaling from matrix
scale.X = (*(Vector3*)&matrix.Columns[0]).Magnitude();
scale.Y = (*(Vector3*)&matrix.Columns[1]).Magnitude();
scale.Z = (*(Vector3*)&matrix.Columns[2]).Magnitude();
scale.X = length(*(float3*)&matrix.Columns[0]);
scale.Y = length(*(float3*)&matrix.Columns[1]);
scale.Z = length(*(float3*)&matrix.Columns[2]);
if(MathHelper.IsZero(scale.X) || MathHelper.IsZero(scale.Y) || MathHelper.IsZero(scale.Z))
{
+17 -17
View File
@@ -22,7 +22,7 @@ namespace GlitchyEngine.Math
W = w;
}
public this(Vector3 xy, float z, float w)
public this(float3 xy, float z, float w)
{
X = xy.X;
Y = xy.Y;
@@ -30,7 +30,7 @@ namespace GlitchyEngine.Math
W = w;
}
public this(Vector3 xyz, float w)
public this(float3 xyz, float w)
{
X = xyz.X;
Y = xyz.Y;
@@ -38,7 +38,7 @@ namespace GlitchyEngine.Math
W = w;
}
public this(Vector4 vector)
public this(float4 vector)
{
X = vector.X;
Y = vector.Y;
@@ -46,7 +46,7 @@ namespace GlitchyEngine.Math
W = vector.W;
}
public Vector3 Vector
public float3 Vector
{
get => .(X, Y, Z);
set mut
@@ -195,11 +195,11 @@ namespace GlitchyEngine.Math
{
Quaternion result;
Vector3 v = l.Vector * r.Vector + (l.W * r.Vector) + (r.W * l.Vector);
float3 v = l.Vector * r.Vector + (l.W * r.Vector) + (r.W * l.Vector);
result.X = v.X;
result.Y = v.Y;
result.Z = v.Z;
result.W = (l.W * r.W) - Vector3.Dot(l.Vector, r.Vector);
result.W = (l.W * r.W) - dot(l.Vector, r.Vector);
return result;
}
@@ -220,11 +220,11 @@ namespace GlitchyEngine.Math
[Inline]
#unwarn
public static implicit operator Vector4(in Self value) => *(Vector4*)&value;
public static implicit operator float4(in Self value) => *(float4*)&value;
[Inline]
#unwarn
public static implicit operator Quaternion(in Vector4 value) => *(Quaternion*)&value;
public static implicit operator Quaternion(in float4 value) => *(Quaternion*)&value;
public static bool operator ==(Quaternion l, Quaternion r)
{
@@ -236,7 +236,7 @@ namespace GlitchyEngine.Math
return l.X != r.X && l.Y != r.Y && l.Z != r.Z && l.W != r.W;
}
public (Vector3 Axis, float Angle) ToAxisAngle()
public (float3 Axis, float Angle) ToAxisAngle()
{
// scalar part = cos(θ/2)
// So, we can extract the angle directly.
@@ -247,12 +247,12 @@ namespace GlitchyEngine.Math
// We assume quaternion is unit length, so subtracting w^2 gives us length of just vector part (aka sin(θ/2)).
float length = Math.Sqrt(1.0f - (W * W));
Vector3 axis;
float3 axis;
// Normalize vector part to get the axis!
if(length == 0)
{
axis = Vector3.Zero;
axis = float3.Zero;
}
else
{
@@ -265,9 +265,9 @@ namespace GlitchyEngine.Math
return (axis, angle);
}
public static Quaternion FromAxisAngle(Vector3 axis, float angle)
public static Quaternion FromAxisAngle(float3 axis, float angle)
{
float lengthSq = axis.MagnitudeSquared();
float lengthSq = lengthSq(axis);
if(lengthSq == 0)
{
@@ -316,11 +316,11 @@ namespace GlitchyEngine.Math
return result;
}
public static Vector3 ToEulerAngles(Quaternion q)
public static float3 ToEulerAngles(Quaternion q)
{
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/
Vector3 result;
float3 result;
float sqw = q.W*q.W;
float sqx = q.X*q.X;
@@ -354,7 +354,7 @@ namespace GlitchyEngine.Math
float y = 2.0f * (Y * Z + W * X);
float x = W * W - X * X - Y * Y + Z * Z;
if (Vector2(x, y).Equals(.Zero)) //avoid atan2(0,0) - handle singularity - Matiis
if (float2(x, y).Equals(.Zero)) //avoid atan2(0,0) - handle singularity - Matiis
return 2.0f * Math.Atan2(X, W);
return Math.Atan2(y, x);
@@ -370,7 +370,7 @@ namespace GlitchyEngine.Math
float y = 2.0f * (X * Y + W * Z);
float x = W * W + X * X - Y * Y - Z * Z;
if (Vector2(x, y).Equals(.Zero)) //avoid atan2(0,0) - handle singularity - Matiis
if (float2(x, y).Equals(.Zero)) //avoid atan2(0,0) - handle singularity - Matiis
return 0;
return Math.Atan2(y, x);
+55 -55
View File
@@ -5,20 +5,20 @@ namespace GlitchyEngine.Math
{
[BonTarget]
[SwizzleVector(3, "GlitchyEngine.Math.Vector")]
public struct Vector3
public struct float3
{
public const Vector3 Zero = .(0f, 0f, 0f);
public const Vector3 UnitX = .(1f, 0f, 0f);
public const Vector3 UnitY = .(0f, 1f, 0f);
public const Vector3 UnitZ = .(0f, 0f, 1f);
public const Vector3 One = .(1f, 1f, 1f);
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 Vector3 Forward = .(0f, 0f, 1f);
public const Vector3 Backward = .(0f, 0f, -1f);
public const Vector3 Left = .(-1f, 0f, 0f);
public const Vector3 Right = .(1f, 0f, 0f);
public const Vector3 Up = .(0f, 1f, 0f);
public const Vector3 Down = .(0f, -1f, 0f);
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 const int ComponentCount = 3;
@@ -47,14 +47,14 @@ namespace GlitchyEngine.Math
Z = z;
}
public this(Vector3 value)
public this(float3 value)
{
X = value.X;
Y = value.Y;
Z = value.Z;
}
public this(Vector4 value)
public this(float4 value)
{
X = value.X;
Y = value.Y;
@@ -131,7 +131,7 @@ namespace GlitchyEngine.Math
/// Returns a copy of this Vector with a magnitude of 1.
[Checked]
public Vector3 Normalized()
public float3 Normalized()
{
if(this == .Zero)
return .Zero;
@@ -140,28 +140,28 @@ namespace GlitchyEngine.Math
}
/// Returns a copy of this Vector with a magnitude of 1.
public Vector3 Normalized()
public float3 Normalized()
{
return this / Magnitude();
}
/// Returns a copy of the given Vector with a magnitude of 1.
public static Vector3 Normalize(Vector3 v)
public static float3 Normalize(float3 v)
{
return v / v.Magnitude();
}
/// Calculates the dot product of two vectors.
public static float Dot(Vector3 l, Vector3 r) => l.X * r.X + l.Y * r.Y + l.Z * r.Z;
public static float Dot(float3 l, float3 r) => l.X * r.X + l.Y * r.Y + l.Z * r.Z;
/// Calculates the distance between two vectors.
public static float Distance(Vector3 a, Vector3 b) => (a - b).[Inline]Magnitude();
public static float Distance(float3 a, float3 b) => (a - b).[Inline]Magnitude();
/// Calculates the squared distance between two vectors.
public static float DistanceSquared(Vector3 a, Vector3 b) => (a - b).[Inline]MagnitudeSquared();
public static float DistanceSquared(float3 a, float3 b) => (a - b).[Inline]MagnitudeSquared();
/// Calculates the cross product of two vectors.
public static Vector3 Cross(Vector3 l, Vector3 r)
public static float3 Cross(float3 l, float3 r)
{
return .(l.Y * r.Z - l.Z * r.Y,
l.Z * r.X - l.X * r.Z,
@@ -169,20 +169,20 @@ namespace GlitchyEngine.Math
}
/// Calculates the projection of a onto b.
public static Vector3 Project(Vector3 a, Vector3 b)
public static float3 Project(float3 a, float3 b)
{
return (b * (Dot(a, b) / Dot(b, b)));
}
/// Calculates the rejection of a from b.
public static Vector3 Reject(Vector3 a, Vector3 b)
public static float3 Reject(float3 a, float3 b)
{
return (a - b * (Dot(a, b) / Dot(b, b)));
}
public static Vector3 Floor(Vector3 value) => .(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z));
public static float3 Floor(float3 value) => .(Math.Floor(value.X), Math.Floor(value.Y), Math.Floor(value.Z));
public static Vector3 Ceiling(Vector3 value) => .(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z));
public static float3 Ceiling(float3 value) => .(Math.Ceiling(value.X), Math.Ceiling(value.Y), Math.Ceiling(value.Z));
/**
* Interpolates linearly between two given vectors.
@@ -192,18 +192,18 @@ namespace GlitchyEngine.Math
* (0 means a will be returned, 1 means b will be returned.)
* @returns The resulting linear interpolation.
*/
public static Vector3 Lerp(Vector3 a, Vector3 b, float interpolationValue)
public static float3 Lerp(float3 a, float3 b, float interpolationValue)
{
return a + interpolationValue * (b - a);
}
public static Vector3 Min(Vector3 a, Vector3 b) => .(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Min(a.Z, b.Z));
public static float3 Min(float3 a, float3 b) => .(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Min(a.Z, b.Z));
public static Vector3 Max(Vector3 a, Vector3 b) => .(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y), Math.Min(a.Z, b.Z));
public static float3 Max(float3 a, float3 b) => .(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y), Math.Min(a.Z, b.Z));
public static Vector3 Abs(Vector3 v) => .(Math.Abs(v.X), Math.Abs(v.Y), Math.Abs(v.Z));
public static float3 Abs(float3 v) => .(Math.Abs(v.X), Math.Abs(v.Y), Math.Abs(v.Z));
public static Vector3 Clamp(Vector3 v, Vector3 min, Vector3 max)
public static float3 Clamp(float3 v, float3 min, float3 max)
{
return .(Math.Clamp(v.X, min.X, max.X),
Math.Clamp(v.Y, min.Y, max.Y),
@@ -216,7 +216,7 @@ namespace GlitchyEngine.Math
// Addition
public void operator +=(Vector3 value) mut
public void operator +=(float3 value) mut
{
X += value.X;
Y += value.Y;
@@ -232,7 +232,7 @@ namespace GlitchyEngine.Math
// Subtraction
public void operator -=(Vector3 value) mut
public void operator -=(float3 value) mut
{
X -= value.X;
Y -= value.Y;
@@ -248,7 +248,7 @@ namespace GlitchyEngine.Math
// Multiplication
public void operator *=(Vector3 value) mut
public void operator *=(float3 value) mut
{
X *= value.X;
Y *= value.Y;
@@ -264,7 +264,7 @@ namespace GlitchyEngine.Math
// Division
public void operator /=(Vector3 value) mut
public void operator /=(float3 value) mut
{
X /= value.X;
Y /= value.Y;
@@ -285,57 +285,57 @@ namespace GlitchyEngine.Math
// Addition
public static Vector3 operator +(Vector3 left, Vector3 right) => Vector3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
public static float3 operator +(float3 left, float3 right) => float3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
public static Vector3 operator +(Vector3 value, float scalar) => Vector3(value.X + scalar, value.Y + scalar, value.Z + scalar);
public static float3 operator +(float3 value, float scalar) => float3(value.X + scalar, value.Y + scalar, value.Z + scalar);
public static Vector3 operator +(float scalar, Vector3 value) => Vector3(scalar + value.X, scalar + value.Y, scalar + value.Z);
public static float3 operator +(float scalar, float3 value) => float3(scalar + value.X, scalar + value.Y, scalar + value.Z);
public static Vector3 operator +(Vector3 value) => value;
public static float3 operator +(float3 value) => value;
// Subtraction
public static Vector3 operator -(Vector3 left, Vector3 right) => Vector3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
public static float3 operator -(float3 left, float3 right) => float3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
public static Vector3 operator -(Vector3 value, float scalar) => Vector3(value.X - scalar, value.Y - scalar, value.Z - scalar);
public static float3 operator -(float3 value, float scalar) => float3(value.X - scalar, value.Y - scalar, value.Z - scalar);
public static Vector3 operator -(float scalar, Vector3 value) => Vector3(scalar - value.X, scalar - value.Y, scalar - value.Z);
public static float3 operator -(float scalar, float3 value) => float3(scalar - value.X, scalar - value.Y, scalar - value.Z);
public static Vector3 operator -(Vector3 value) => Vector3(-value.X, -value.Y, -value.Z);
public static float3 operator -(float3 value) => float3(-value.X, -value.Y, -value.Z);
// Multiplication
public static Vector3 operator *(Vector3 left, Vector3 right) => Vector3(left.X * right.X, left.Y * right.Y, left.Z * right.Z);
public static float3 operator *(float3 left, float3 right) => float3(left.X * right.X, left.Y * right.Y, left.Z * right.Z);
public static Vector3 operator *(Vector3 value, float scalar) => Vector3(value.X * scalar, value.Y * scalar, value.Z * scalar);
public static float3 operator *(float3 value, float scalar) => float3(value.X * scalar, value.Y * scalar, value.Z * scalar);
public static Vector3 operator *(float scalar, Vector3 value) => Vector3(scalar * value.X, scalar * value.Y, scalar * value.Z);
public static float3 operator *(float scalar, float3 value) => float3(scalar * value.X, scalar * value.Y, scalar * value.Z);
// Division
public static Vector3 operator /(Vector3 left, Vector3 right) => Vector3(left.X / right.X, left.Y / right.Y, left.Z / right.Z);
public static float3 operator /(float3 left, float3 right) => float3(left.X / right.X, left.Y / right.Y, left.Z / right.Z);
public static Vector3 operator /(Vector3 value, float scalar)
public static float3 operator /(float3 value, float scalar)
{
float inv = 1.0f / scalar;
return Vector3(value.X * inv, value.Y * inv, value.Z * inv);
return float3(value.X * inv, value.Y * inv, value.Z * inv);
}
public static Vector3 operator /(float scalar, Vector3 value) => Vector3(scalar / value.X, scalar / value.Y, scalar / value.Z);
public static float3 operator /(float scalar, float3 value) => float3(scalar / value.X, scalar / value.Y, scalar / value.Z);
// Modulo
public static Vector3 operator %(Vector3 left, Vector3 right) => Vector3(left.X % right.X, left.Y % right.Y, left.Z % right.Z);
public static float3 operator %(float3 left, float3 right) => float3(left.X % right.X, left.Y % right.Y, left.Z % right.Z);
public static Vector3 operator %(Vector3 value, float scalar) => Vector3(value.X % scalar, value.Y % scalar, value.Z % scalar);
public static float3 operator %(float3 value, float scalar) => float3(value.X % scalar, value.Y % scalar, value.Z % scalar);
public static Vector3 operator %(float scalar, Vector3 value) => Vector3(scalar % value.X, scalar % value.Y, scalar % value.Z);
public static float3 operator %(float scalar, float3 value) => float3(scalar % value.X, scalar % value.Y, scalar % value.Z);
// Equality
public static bool operator ==(Vector3 left, Vector3 right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z;
public static bool operator ==(float3 left, float3 right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z;
public static bool operator !=(Vector3 left, Vector3 right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z;
public static bool operator !=(float3 left, float3 right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z;
public override void ToString(String strBuffer) => strBuffer.AppendF("X:{0} Y:{1} Z:{2}", X, Y, Z);
@@ -344,6 +344,6 @@ namespace GlitchyEngine.Math
[Inline]
#unwarn
public static explicit operator float[3](Vector3 value) => *(float[3]*)&value;
public static explicit operator float[3](float3 value) => *(float[3]*)&value;
}
}
+38 -38
View File
@@ -5,14 +5,14 @@ namespace GlitchyEngine.Math
{
[BonTarget]
[SwizzleVector(4, "GlitchyEngine.Math.Vector")]
public struct Vector4
public struct float4
{
public const Vector4 Zero = .(0f, 0f, 0f, 0f);
public const Vector4 UnitX = .(1f, 0f, 0f, 0f);
public const Vector4 UnitY = .(0f, 1f, 0f, 0f);
public const Vector4 UnitZ = .(0f, 0f, 1f, 0f);
public const Vector4 UnitW = .(0f, 0f, 0f, 1f);
public const Vector4 One = .(1f, 1f, 1f, 1f);
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 const int ComponentCount = 4;
@@ -44,7 +44,7 @@ namespace GlitchyEngine.Math
W = value2.Y;
}
public this(Vector3 value, float w)
public this(float3 value, float w)
{
X = value.X;
Y = value.Y;
@@ -133,7 +133,7 @@ namespace GlitchyEngine.Math
this /= Magnitude();
}
public static Vector4 Normalize(Vector4 v)
public static float4 Normalize(float4 v)
{
if(v == .Zero)
return .Zero;
@@ -142,12 +142,12 @@ namespace GlitchyEngine.Math
}
[Unchecked]
public static Vector4 Normalize(Vector4 v)
public static float4 Normalize(float4 v)
{
return v / v.Magnitude();
}
public static float Dot(Vector4 l, Vector4 r)
public static float Dot(float4 l, float4 r)
{
return l.X * r.X + l.Y * r.Y + l.Z * r.Z + l.W * r.W;
}
@@ -155,7 +155,7 @@ namespace GlitchyEngine.Math
/**
* Calculates the projection of a onto b
*/
public static Vector4 Project(Vector4 a, Vector4 b)
public static float4 Project(float4 a, float4 b)
{
return (b * (Dot(a, b) / Dot(b, b)));
}
@@ -163,7 +163,7 @@ namespace GlitchyEngine.Math
/**
* Calculates the rejection of a from b
*/
public static Vector4 Reject(Vector4 a, Vector4 b)
public static float4 Reject(float4 a, float4 b)
{
return (a - b * (Dot(a, b) / Dot(b, b)));
}
@@ -176,17 +176,17 @@ namespace GlitchyEngine.Math
* (0 means a will be returned, 1 means b will be returned.)
* @returns The resulting linear interpolation.
*/
public static Vector4 Lerp(Vector4 a, Vector4 b, float interpolationValue)
public static float4 Lerp(float4 a, float4 b, float interpolationValue)
{
return a + interpolationValue * (b - a);
}
public static Vector4 Min(Vector4 a, Vector4 b)
public static float4 Min(float4 a, float4 b)
{
return .(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Min(a.Z, b.Z), Math.Min(a.W, b.W));
}
public static Vector4 Max(Vector4 a, Vector4 b)
public static float4 Max(float4 a, float4 b)
{
return .(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y), Math.Min(a.Z, b.Z), Math.Min(a.W, b.W));
}
@@ -197,7 +197,7 @@ namespace GlitchyEngine.Math
// Addition
public void operator +=(Vector4 value) mut
public void operator +=(float4 value) mut
{
X += value.X;
Y += value.Y;
@@ -215,7 +215,7 @@ namespace GlitchyEngine.Math
// Subtraction
public void operator -=(Vector4 value) mut
public void operator -=(float4 value) mut
{
X -= value.X;
Y -= value.Y;
@@ -233,7 +233,7 @@ namespace GlitchyEngine.Math
// Multiplication
public void operator *=(Vector4 value) mut
public void operator *=(float4 value) mut
{
X *= value.X;
Y *= value.Y;
@@ -260,7 +260,7 @@ namespace GlitchyEngine.Math
W *= f;
}
public void operator /=(Vector4 value) mut
public void operator /=(float4 value) mut
{
X /= value.X;
Y /= value.Y;
@@ -274,49 +274,49 @@ namespace GlitchyEngine.Math
// Addition
public static Vector4 operator +(Vector4 left, Vector4 right) => Vector4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
public static float4 operator +(float4 left, float4 right) => float4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
public static Vector4 operator +(Vector4 value, float scalar) => Vector4(value.X + scalar, value.Y + scalar, value.Z + scalar, value.W + scalar);
public static float4 operator +(float4 value, float scalar) => float4(value.X + scalar, value.Y + scalar, value.Z + scalar, value.W + scalar);
public static Vector4 operator +(float scalar, Vector4 value) => Vector4(scalar + value.X, scalar + value.Y, scalar + value.Z, scalar + value.W);
public static float4 operator +(float scalar, float4 value) => float4(scalar + value.X, scalar + value.Y, scalar + value.Z, scalar + value.W);
public static Vector4 operator +(Vector4 value) => value;
public static float4 operator +(float4 value) => value;
// Subtraction
public static Vector4 operator -(Vector4 left, Vector4 right) => Vector4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
public static float4 operator -(float4 left, float4 right) => float4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
public static Vector4 operator -(Vector4 value, float scalar) => Vector4(value.X - scalar, value.Y - scalar, value.Z - scalar, value.W - scalar);
public static float4 operator -(float4 value, float scalar) => float4(value.X - scalar, value.Y - scalar, value.Z - scalar, value.W - scalar);
public static Vector4 operator -(float scalar, Vector4 value) => Vector4(scalar - value.X, scalar - value.Y, scalar - value.Z, scalar - value.W);
public static float4 operator -(float scalar, float4 value) => float4(scalar - value.X, scalar - value.Y, scalar - value.Z, scalar - value.W);
public static Vector4 operator -(Vector4 value) => Vector4(-value.X, -value.Y, -value.Z, -value.W);
public static float4 operator -(float4 value) => float4(-value.X, -value.Y, -value.Z, -value.W);
// Multiplication
public static Vector4 operator *(Vector4 left, Vector4 right) => Vector4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
public static float4 operator *(float4 left, float4 right) => float4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
public static Vector4 operator *(Vector4 value, float scalar) => Vector4(value.X * scalar, value.Y * scalar, value.Z * scalar, value.W * scalar);
public static float4 operator *(float4 value, float scalar) => float4(value.X * scalar, value.Y * scalar, value.Z * scalar, value.W * scalar);
public static Vector4 operator *(float scalar, Vector4 value) => Vector4(scalar * value.X, scalar * value.Y, scalar * value.Z, scalar * value.W);
public static float4 operator *(float scalar, float4 value) => float4(scalar * value.X, scalar * value.Y, scalar * value.Z, scalar * value.W);
// Division
public static Vector4 operator /(Vector4 left, Vector4 right) => Vector4(left.X / right.X, left.Y / right.Y, left.Z / right.Z, left.W / right.W);
public static float4 operator /(float4 left, float4 right) => float4(left.X / right.X, left.Y / right.Y, left.Z / right.Z, left.W / right.W);
public static Vector4 operator /(Vector4 value, float scalar)
public static float4 operator /(float4 value, float scalar)
{
float inv = 1.0f / scalar;
return Vector4(value.X * inv, value.Y * inv, value.Z * inv, value.W * inv);
return float4(value.X * inv, value.Y * inv, value.Z * inv, value.W * inv);
}
public static Vector4 operator /(float scalar, Vector4 value) => Vector4(scalar / value.X, scalar / value.Y, scalar / value.Z, scalar / value.W);
public static float4 operator /(float scalar, float4 value) => float4(scalar / value.X, scalar / value.Y, scalar / value.Z, scalar / value.W);
// Equality
public static bool operator ==(Vector4 left, Vector4 right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z && left.W == right.W;
public static bool operator ==(float4 left, float4 right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z && left.W == right.W;
public static bool operator !=(Vector4 left, Vector4 right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z || left.W != right.W;
public static bool operator !=(float4 left, float4 right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z || left.W != right.W;
public override void ToString(String strBuffer) => strBuffer.AppendF("X:{0} Y:{1} Z:{2} W:{3}", X, Y, Z, W);
@@ -325,6 +325,6 @@ namespace GlitchyEngine.Math
[Inline]
#unwarn
public static explicit operator float[4](Vector4 value) => *(float[4]*)&value;
public static explicit operator float[4](float4 value) => *(float[4]*)&value;
}
}
+18 -4
View File
@@ -87,11 +87,11 @@ struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where
if (ComponentCount == 3)
{
GenerateVector3Constructors(type);
Generatefloat3Constructors(type);
}
else if (ComponentCount == 4)
{
GenerateVector4Constructors(type);
Generatefloat4Constructors(type);
}
}
@@ -128,7 +128,7 @@ struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where
}
[Comptime]
private void GenerateVector3Constructors(Type type)
private void Generatefloat3Constructors(Type type)
{
String baseName = type.GetName(.. scope String());
@@ -161,13 +161,27 @@ struct VectorAttribute<T, ComponentCount> : Attribute, IComptimeTypeApply where
}
[Comptime]
private void GenerateVector4Constructors(Type type)
private void Generatefloat4Constructors(Type type)
{
String baseName = type.GetName(.. scope String());
// Remove number from name
baseName.RemoveFromEnd(1);
String constructor = scope $"""
public this({baseName}2 xy, {baseName}2 zw)
{{
X = xy.X;
Y = xy.Y;
Z = zw.X;
W = zw.Y;
}}
""";
Compiler.EmitTypeBody(type, constructor);
String constructor1 = scope $"""
public this({baseName}2 xy, {typeof(T)} z, {typeof(T)} w)
{{
+2 -2
View File
@@ -150,9 +150,9 @@ namespace GlitchyEngine.Math
public override void ToString(String strBuffer) => strBuffer.AppendF($"X:{X} Y:{Y}");
public static explicit operator Vector2(Int2 point) => .(point.X, point.Y);
public static explicit operator float2(Int2 point) => .(point.X, point.Y);
public static explicit operator Int2(Vector2 point) => .((int32)point.X, (int32)point.Y);
public static explicit operator Int2(float2 point) => .((int32)point.X, (int32)point.Y);
public int GetHashCode()
{
+2 -2
View File
@@ -191,9 +191,9 @@ namespace GlitchyEngine.Math
public override void ToString(String strBuffer) => strBuffer.AppendF($"X:{X} Y:{Y} Z:{Z}");
public static explicit operator Vector3(Int3 point) => .(point.X, point.Y, point.Z);
public static explicit operator float3(Int3 point) => .(point.X, point.Y, point.Z);
public static explicit operator Int3(Vector3 point) => .((int32)point.X, (int32)point.Y, (int32)point.Z);
public static explicit operator Int3(float3 point) => .((int32)point.X, (int32)point.Y, (int32)point.Z);
[Inline]
public static explicit operator Int2(in Int3 point) => *(Int2*)&point;
+2 -2
View File
@@ -216,9 +216,9 @@ namespace GlitchyEngine.Math
public override void ToString(String strBuffer) => strBuffer.AppendF($"X:{X} Y:{Y} Z:{Z} W:{W}");
public static explicit operator Vector4(Int4 point) => .(point.X, point.Y, point.Z, point.W);
public static explicit operator float4(Int4 point) => .(point.X, point.Y, point.Z, point.W);
public static explicit operator Int4(Vector4 point) => .((int32)point.X, (int32)point.Y, (int32)point.Z, (int32)point.W);
public static explicit operator Int4(float4 point) => .((int32)point.X, (int32)point.Y, (int32)point.Z, (int32)point.W);
[Inline]
public static explicit operator Int2(in Int4 point) => *(Int2*)&point;
@@ -14,7 +14,7 @@ namespace GlitchyEngine
private bool _rotation;
private Vector3 _cameraPosition;
private float3 _cameraPosition;
private float _cameraRotation;
private float _cameraTranslationSpeed = 1.0f;
private float _cameraRotationSpeed = 1.0f;
@@ -41,7 +41,7 @@ namespace GlitchyEngine
if(Application.Get().Window.IsActive)
{
Vector3 movement = .();
float3 movement = .();
if(Input.IsKeyPressed(Key.W))
{
@@ -61,8 +61,8 @@ namespace GlitchyEngine
movement.X += 1;
}
if(movement != .Zero)
movement.Normalize();
if(any(movement != .Zero))
movement = normalize(movement);
movement *= (float)(gameTime.FrameTime.TotalSeconds) * _cameraTranslationSpeed * _zoomLevel;
@@ -12,8 +12,8 @@ namespace GlitchyEngine
private float _aspectRatio;
private float _fovY = Math.PI_f / 4;
private Vector3 _cameraPosition;
private Vector3 _cameraRotation;
private float3 _cameraPosition;
private float3 _cameraRotation;
private float _cameraTranslationSpeed = 1.0f;
private float _cameraRotationSpeedX = 0.001f;
@@ -27,7 +27,7 @@ namespace GlitchyEngine
set => _cameraTranslationSpeed = value;
}
public Vector2 RotationSpeed
public float2 RotationSpeed
{
get => .(_cameraRotationSpeedX, _cameraRotationSpeedY);
set
@@ -37,7 +37,7 @@ namespace GlitchyEngine
}
}
public Vector3 CameraPosition
public float3 CameraPosition
{
get => _cameraPosition;
set
@@ -48,7 +48,7 @@ namespace GlitchyEngine
}
}
public Vector3 CameraRotation
public float3 CameraRotation
{
get => _cameraRotation;
set
@@ -100,7 +100,7 @@ namespace GlitchyEngine
if(Application.Get().Window.IsActive)
{
Vector3 movement = .();
float3 movement = .();
if(Input.IsKeyPressed(Key.W))
{
@@ -129,14 +129,14 @@ namespace GlitchyEngine
movement.Y -= 1;
}
if(movement != .Zero)
movement.Normalize();
if(any(movement != .Zero))
normalize(movement);
movement *= (float)(gameTime.FrameTime.TotalSeconds) * _cameraTranslationSpeed;
Vector4 delta = Vector4(movement, 1.0f) * _camera.View;
float4 delta = float4(movement, 1.0f) * _camera.View;
_cameraPosition += Vector3(delta.X, delta.Y, delta.Z);
_cameraPosition += float3(delta.X, delta.Y, delta.Z);
//_cameraPosition += movement;
// Camera rotation
@@ -8,7 +8,7 @@ using Bon;
namespace GlitchyEngine.Math
{
extension Vector2
extension float2
{
[Inline]
public static implicit operator DirectX.Math.Vector2(in Self value) => *(DirectX.Math.Vector2*)&value;
@@ -17,7 +17,7 @@ namespace GlitchyEngine.Math
public static implicit operator Self(in DirectX.Math.Vector2 value) => *(Self*)&value;
}
extension Vector3
extension float3
{
[Inline]
public static implicit operator DirectX.Math.Vector3(in Self value) => *(DirectX.Math.Vector3*)&value;
@@ -26,7 +26,7 @@ namespace GlitchyEngine.Math
public static implicit operator Self(in DirectX.Math.Vector3 value) => *(Self*)&value;
}
extension Vector4
extension float4
{
[Inline]
public static implicit operator DirectX.Math.Vector4(in Self value) => *(DirectX.Math.Vector4*)&value;
@@ -188,9 +188,9 @@ namespace GlitchyEngine.Renderer.Animation
class JointAnimation// : IDisposable
{
public JointAnimationChannel<Vector3> TranslationChannel ~ delete _;
public JointAnimationChannel<float3> TranslationChannel ~ delete _;
public JointAnimationChannel<Quaternion> RotationChannel ~ delete _;
public JointAnimationChannel<Vector3> ScaleChannel ~ delete _;
public JointAnimationChannel<float3> ScaleChannel ~ delete _;
public float Duration
{
@@ -221,7 +221,7 @@ namespace GlitchyEngine.Renderer.Animation
case .Step:
result.Translation = previous;
case .Linear:
result.Translation = Vector3.Lerp(previous, next, interpolationValue);
result.Translation = lerp(previous, next, interpolationValue);
default:
result.Rotation = ?;
Runtime.NotImplemented();
@@ -262,7 +262,7 @@ namespace GlitchyEngine.Renderer.Animation
case .Step:
result.Scale = previous;
case .Linear:
result.Scale = Vector3.Lerp(previous, next, interpolationValue);
result.Scale = lerp(previous, next, interpolationValue);
default:
result.Rotation = ?;
Runtime.NotImplemented();
@@ -14,7 +14,7 @@ namespace GlitchyEngine.Renderer.Animation
public struct JointPose
{
public Quaternion Rotation;
public Vector3 Translation;
public Vector3 Scale;
public float3 Translation;
public float3 Scale;
}
}
+6 -6
View File
@@ -80,11 +80,11 @@ namespace GlitchyEngine.Renderer
case typeof(float):
EnsureTypeMatch(1, 1, .Float);
case typeof(Vector2):
case typeof(float2):
EnsureTypeMatch(1, 2, .Float);
case typeof(Vector3):
case typeof(float3):
EnsureTypeMatch(1, 3, .Float);
case typeof(Vector4):
case typeof(float4):
EnsureTypeMatch(1, 4, .Float);
case typeof(int32):
@@ -132,9 +132,9 @@ namespace GlitchyEngine.Renderer
public void SetData(bool value) => SetData<bool>(value);
public void SetData(float value) => SetData<float>(value);
public void SetData(Vector2 value) => SetData<Vector2>(value);
public void SetData(Vector3 value) => SetData<Vector3>(value);
public void SetData(Vector4 value) => SetData<Vector4>(value);
public void SetData(float2 value) => SetData<float2>(value);
public void SetData(float3 value) => SetData<float3>(value);
public void SetData(float4 value) => SetData<float4>(value);
public void SetData(int32 value) => SetData<int32>(value);
public void SetData(Int2 value) => SetData<Int2>(value);
+6 -6
View File
@@ -31,8 +31,8 @@ namespace GlitchyEngine.Renderer
protected float _nearPlane;
protected float _farPlane;
protected Vector3 _position;
protected Vector3 _rotation;
protected float3 _position;
protected float3 _rotation;
/// If true the transform matrix is outdated and needs to be recalculated.
protected bool _transformOutdated = true;
@@ -76,12 +76,12 @@ namespace GlitchyEngine.Renderer
}
}
public Vector3 Position
public float3 Position
{
get => _position;
set
{
if(_position == value)
if(all(_position == value))
return;
_position = value;
@@ -89,12 +89,12 @@ namespace GlitchyEngine.Renderer
}
}
public Vector3 Rotation
public float3 Rotation
{
get => _rotation;
set
{
if(_rotation == value)
if(all(_rotation == value))
return;
_rotation = value;
+7 -7
View File
@@ -39,9 +39,9 @@ namespace GlitchyEngine.Renderer
if(length != 0.0f)
{
transform.Columns[0]..Normalize() *= length;
transform.Columns[1]..Normalize() *= length;
transform.Columns[2]..Normalize() *= length;
transform.Columns[0] = normalize(transform.Columns[0]) * length;
transform.Columns[1] = normalize(transform.Columns[1]) * length;
transform.Columns[2] = normalize(transform.Columns[2]) * length;
}
Renderer.DrawLine(.Zero, .Right, .Red, transform);
@@ -66,7 +66,7 @@ namespace GlitchyEngine.Renderer
_frustumGeometry = new GeometryBinding();
_frustumGeometry.SetPrimitiveTopology(.LineList);
_frustumVertices = new VertexBuffer(typeof(Vector4), 8, .Dynamic, .Write);
_frustumVertices = new VertexBuffer(typeof(float4), 8, .Dynamic, .Write);
_frustumGeometry.SetVertexBufferSlot(_frustumVertices, 0);
using (IndexBuffer indexBuffer = new IndexBuffer(24, .Immutable))
@@ -109,7 +109,7 @@ namespace GlitchyEngine.Renderer
2, 6,
3, 7);
Vector4[8] corners;
float4[8] corners;
// Perspective
if(projection._43 != 0.0f)
{
@@ -121,7 +121,7 @@ namespace GlitchyEngine.Renderer
float gOverS = projection._11;
float g = projection._22;
//var corners = //(Vector4*)&_vbFrustum.Data;
//var corners = //(float4*)&_vbFrustum.Data;
if(Math.Abs(d1) >= 10000)
{
@@ -167,7 +167,7 @@ namespace GlitchyEngine.Renderer
float n = -projection._34 / projection._33;
float f = (1 - projection._34) / projection._33;
//var corners = (Vector4*)&_vbFrustum.Data;
//var corners = (float4*)&_vbFrustum.Data;
corners[0] = .(r, t, n, 1.0f);
corners[1] = .(r, b, n, 1.0f);
corners[2] = .(l, b, n, 1.0f);
+3 -3
View File
@@ -602,11 +602,11 @@ public class Effect : Asset
}
if (numComponents == 2)
return Variant.Create(*(Vector2*)floats.Ptr);
return Variant.Create(*(float2*)floats.Ptr);
else if (numComponents == 3)
return Variant.Create(*(Vector3*)floats.Ptr);
return Variant.Create(*(float3*)floats.Ptr);
else if (numComponents == 4)
return Variant.Create(*(Vector4*)floats.Ptr);
return Variant.Create(*(float4*)floats.Ptr);
}
else
{
+2 -2
View File
@@ -11,9 +11,9 @@ namespace GlitchyEngine.Renderer
s_fullscreenQuadGeometry = new GeometryBinding();
s_fullscreenQuadGeometry.SetPrimitiveTopology(.TriangleList);
using(var quadVertices = new VertexBuffer(typeof(Vector4), 3, .Immutable))
using(var quadVertices = new VertexBuffer(typeof(float4), 3, .Immutable))
{
Vector4[4] vertices = .(
float4[4] vertices = .(
.(-1, 1, 0, 0),
.( 3, 1, 2, 0),
.(-1,-3, 0, 2),
+3 -3
View File
@@ -119,9 +119,9 @@ public class Material : Asset
}
public void SetVariable(String name, float value) => SetVariable<float>(name, value);
public void SetVariable(String name, Vector2 value) => SetVariable<Vector2>(name, value);
public void SetVariable(String name, Vector3 value) => SetVariable<Vector3>(name, value);
public void SetVariable(String name, Vector4 value) => SetVariable<Vector4>(name, value);
public void SetVariable(String name, float2 value) => SetVariable<float2>(name, value);
public void SetVariable(String name, float3 value) => SetVariable<float3>(name, value);
public void SetVariable(String name, float4 value) => SetVariable<float4>(name, value);
public void SetVariable(String name, int32 value) => SetVariable<int32>(name, value);
public void SetVariable(String name, Int2 value) => SetVariable<Int2>(name, value);
+16 -16
View File
@@ -11,7 +11,7 @@ namespace GlitchyEngine.Renderer
struct SceneConstants
{
public Matrix ViewProjection;
public Vector3 CameraPosition;
public float3 CameraPosition;
public RenderTargetGroup CameraTarget;
public RenderTargetGroup CompositionTarget;
}
@@ -102,7 +102,7 @@ namespace GlitchyEngine.Renderer
public Matrix4x3 Transform_InvT;
public uint32 EntityId;
private Vector3 _padding;
private float3 _padding;
}
public static void Init()
@@ -138,9 +138,9 @@ namespace GlitchyEngine.Renderer
LineGeometry = new GeometryBinding();
LineGeometry.SetPrimitiveTopology(.LineList);
Vector4[2] vertices = .(.Zero, .Zero);
LineVertices = new VertexBuffer(sizeof(Vector4), 2, .Dynamic, .Write);
LineVertices.SetData<Vector4>(vertices, 0, .WriteDiscard);
float4[2] vertices = .(.Zero, .Zero);
LineVertices = new VertexBuffer(sizeof(float4), 2, .Dynamic, .Write);
LineVertices.SetData<float4>(vertices, 0, .WriteDiscard);
LineGeometry.SetVertexBufferSlot(LineVertices, 0);
uint16[2] indices = .(0, 1);
@@ -254,8 +254,8 @@ namespace GlitchyEngine.Renderer
// Material and Mesh equal: sort by distance
if (cmp == 0)
{
float distLeftSq = Vector3.DistanceSquared(_sceneConstants.CameraPosition, left.Transform.Translation);
float distRightSq = Vector3.DistanceSquared(_sceneConstants.CameraPosition, right.Transform.Translation);
float distLeftSq = distanceSq(_sceneConstants.CameraPosition, left.Transform.Translation);
float distRightSq = distanceSq(_sceneConstants.CameraPosition, right.Transform.Translation);
// Whether or not the values are squared doesn't affect the order (because square(root) is a monotonic function)
cmp = distLeftSq <=> distRightSq;
@@ -335,13 +335,13 @@ namespace GlitchyEngine.Renderer
RenderCommand.SetDepthStencilState(_fullscreenDepthState);
// Scaling to make sure that only the part of the gbuffer that we actually used gets rendered into the viewport.
Vector2 scaling = Vector2(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height) / (Vector2)_gBuffer.Size;
float2 scaling = float2(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height) / (float2)_gBuffer.Size;
for (SubmittedLight light in _lights)
{
Debug.Profiler.ProfileRendererScope!("Draw Light");
Vector3 lightDir = -light.Transform.Forward;
float3 lightDir = -light.Transform.Forward;
Effect fsEffect = TestFullscreenEffect.Get();
fsEffect.SetTexture("GBuffer_Albedo", _gBuffer.Target, 0);
@@ -487,10 +487,10 @@ namespace GlitchyEngine.Renderer
* @param end The end point of the line.
* @param color The color of the line.
*/
public static void DrawLine(Vector3 start, Vector3 end, ColorRGBA color)
public static void DrawLine(float3 start, float3 end, ColorRGBA color)
{
Renderer2D.DrawLine(start, end, color);
//DrawLine(Vector4(start, 1.0f), Vector4(end, 1.0f), (ColorRGBA)color, .Identity);
//DrawLine(float4(start, 1.0f), float4(end, 1.0f), (ColorRGBA)color, .Identity);
}
/** @brief Draws a line.
@@ -499,9 +499,9 @@ namespace GlitchyEngine.Renderer
* @param color The color of the line.
* @param transform A transform matrix transforming the line.
*/
public static void DrawLine(Vector3 start, Vector3 end, ColorRGBA color, Matrix transform)
public static void DrawLine(float3 start, float3 end, ColorRGBA color, Matrix transform)
{
Renderer2D.DrawLine(transform * Vector4(start, 1.0f), transform * Vector4(end, 1.0f), color);
Renderer2D.DrawLine(transform * float4(start, 1.0f), transform * float4(end, 1.0f), color);
}
/** @brief Draws a ray.
@@ -509,7 +509,7 @@ namespace GlitchyEngine.Renderer
* @param direction The direction of the ray.
* @param color The color of the ray.
*/
public static void DrawRay(Vector3 start, Vector3 direction, ColorRGBA color)
public static void DrawRay(float3 start, float3 direction, ColorRGBA color)
{
Renderer2D.DrawRay(start, direction, color);
}
@@ -520,9 +520,9 @@ namespace GlitchyEngine.Renderer
* @param color The color of the ray.
* @param transform A transform matrix transforming the ray.
*/
public static void DrawRay(Vector3 start, Vector3 direction, ColorRGBA color, Matrix transform)
public static void DrawRay(float3 start, float3 direction, ColorRGBA color, Matrix transform)
{
Renderer2D.DrawLine(transform * Vector4(start, 1.0f), transform * Vector4(direction, 0.0f), color);
Renderer2D.DrawLine(transform * float4(start, 1.0f), transform * float4(direction, 0.0f), color);
}
}
}
+64 -64
View File
@@ -14,10 +14,10 @@ namespace GlitchyEngine.Renderer
[CRepr]
struct QuadVertex : IVertexData
{
public Vector2 Position;
public Vector2 Texcoord;
public float2 Position;
public float2 Texcoord;
public this(Vector2 position, Vector2 texcoord)
public this(float2 position, float2 texcoord)
{
Position = position;
Texcoord = texcoord;
@@ -46,12 +46,12 @@ namespace GlitchyEngine.Renderer
[CRepr]
struct LineBatchVertex
{
public Vector4 Position;
public float4 Position;
public ColorRGBA Color;
public uint32 EntityId;
public this(Vector4 position, ColorRGBA color, uint32 entityId)
public this(float4 position, ColorRGBA color, uint32 entityId)
{
Position = position;
Color = color;
@@ -64,11 +64,11 @@ namespace GlitchyEngine.Renderer
{
public Matrix Transform;
public ColorRGBA Color;
public Vector4 UVTransform;
public float4 UVTransform;
public uint32 EntityId;
public this(Matrix transform, ColorRGBA color, Vector4 uvTransform, uint32 entityId)
public this(Matrix transform, ColorRGBA color, float4 uvTransform, uint32 entityId)
{
Transform = transform;
Color = color;
@@ -82,7 +82,7 @@ namespace GlitchyEngine.Renderer
{
public float InnerRadius;
public this(Matrix transform, ColorRGBA color, Vector4 uvTransform, float innerRadius, uint32 entityId)
public this(Matrix transform, ColorRGBA color, float4 uvTransform, float innerRadius, uint32 entityId)
: base(transform, color, uvTransform, entityId)
{
InnerRadius = innerRadius;
@@ -110,15 +110,15 @@ namespace GlitchyEngine.Renderer
FrontToBack
}
struct QueueLine: this(Vector4 Start, Vector4 End, ColorRGBA Color, float Depth, uint32 entityId = uint32.MaxValue) { }
struct QueueLine: this(float4 Start, float4 End, ColorRGBA Color, float Depth, uint32 entityId = uint32.MaxValue) { }
struct QueueQuad: this(Matrix Transform, ColorRGBA Color, Texture Texture, float Depth, Vector4 uvTransform, uint32 entityId = uint32.MaxValue) { }
struct QueueQuad: this(Matrix Transform, ColorRGBA Color, Texture Texture, float Depth, float4 uvTransform, uint32 entityId = uint32.MaxValue) { }
struct QueueCircle : QueueQuad
{
public float InnerRadius;
public this(Matrix Transform, ColorRGBA Color, Texture Texture, float Depth, Vector4 uvTransform, float innerRadius, uint32 entityId = uint32.MaxValue)
public this(Matrix Transform, ColorRGBA Color, Texture Texture, float Depth, float4 uvTransform, float innerRadius, uint32 entityId = uint32.MaxValue)
: base(Transform, Color, Texture, Depth, uvTransform, entityId)
{
InnerRadius = innerRadius;
@@ -632,7 +632,7 @@ namespace GlitchyEngine.Renderer
/// Adds a quad instance to the instance queue.
[Inline]
private static void QueueQuadInstance(Matrix transform, ColorRGBA color, Texture texture, float depth, Vector4 uvTransform, uint32 id = uint32.MaxValue)
private static void QueueQuadInstance(Matrix transform, ColorRGBA color, Texture texture, float depth, float4 uvTransform, uint32 id = uint32.MaxValue)
{
s_QuadinstanceQueue.Add(QueueQuad(transform, color, texture ?? s_whiteTexture, depth, uvTransform, id));
s_statistics.QuadCount++;
@@ -640,7 +640,7 @@ namespace GlitchyEngine.Renderer
/// Adds a circle instance to the instance queue.
[Inline]
private static void QueueCircleInstance(Matrix transform, ColorRGBA color, Texture2D texture, float depth, Vector4 uvTransform, float innerRadius, uint32 id = uint32.MaxValue)
private static void QueueCircleInstance(Matrix transform, ColorRGBA color, Texture2D texture, float depth, float4 uvTransform, float innerRadius, uint32 id = uint32.MaxValue)
{
s_circleInstanceQueue.Add(QueueCircle(transform, color, texture ?? s_whiteTexture, depth, uvTransform, innerRadius, id));
s_statistics.CircleCount++;
@@ -648,7 +648,7 @@ namespace GlitchyEngine.Renderer
/// Adds a line instance to the instance queue.
[Inline]
private static void QueueLineInstance(Vector4 start, Vector4 end, ColorRGBA color, uint32 id = uint32.MaxValue)
private static void QueueLineInstance(float4 start, float4 end, ColorRGBA color, uint32 id = uint32.MaxValue)
{
s_lineInstanceQueue.Add(QueueLine(start, end, color, id));
s_statistics.LineCount++;
@@ -908,7 +908,7 @@ namespace GlitchyEngine.Renderer
}
/// A specialized function that calculates the 2D transform matrix
private static Matrix Calculate2DTransform(Vector3 translation, Vector2 scale, float rotation)
private static Matrix Calculate2DTransform(float3 translation, float2 scale, float rotation)
{
float sin = 0.0f;
float cos = 1.0f;
@@ -933,9 +933,9 @@ namespace GlitchyEngine.Renderer
* @param color The color of the line.
* @param entityId The optional ID of the entity that belongs to this line (for picking).
*/
public static void DrawLine(Vector3 start, Vector3 end, ColorRGBA color = .White, uint32 entityId = uint32.MaxValue)
public static void DrawLine(float3 start, float3 end, ColorRGBA color = .White, uint32 entityId = uint32.MaxValue)
{
DrawLine(Vector4(start, 1.0f), Vector4(end, 1.0f), color, entityId);
DrawLine(float4(start, 1.0f), float4(end, 1.0f), color, entityId);
}
/** @brief Draws a ray.
@@ -944,9 +944,9 @@ namespace GlitchyEngine.Renderer
* @param color The color of the ray.
* @param entityId The optional ID of the entity that belongs to this ray (for picking).
*/
public static void DrawRay(Vector3 start, Vector3 direction, ColorRGBA color = .White, uint32 entityId = uint32.MaxValue)
public static void DrawRay(float3 start, float3 direction, ColorRGBA color = .White, uint32 entityId = uint32.MaxValue)
{
DrawLine(Vector4(start, 1.0f), Vector4(direction, 0.0f), color, entityId);
DrawLine(float4(start, 1.0f), float4(direction, 0.0f), color, entityId);
}
/** @brief Draws a rectangle.
@@ -955,14 +955,14 @@ namespace GlitchyEngine.Renderer
* @param color The color of the rectangle.
* @param entityId The optional ID of the entity that belongs to this rectangle (for picking).
*/
public static void DrawRect(Vector2 position, Vector2 size, ColorRGBA color = .White, uint32 entityId = uint32.MaxValue)
public static void DrawRect(float2 position, float2 size, ColorRGBA color = .White, uint32 entityId = uint32.MaxValue)
{
Vector2 halfSize = size / 2;
float2 halfSize = size / 2;
Vector4 p0 = Vector4(position + Vector2(-halfSize.X, -halfSize.Y), 0.0f, 1.0f);
Vector4 p1 = Vector4(position + Vector2(halfSize.X, -halfSize.Y), 0.0f, 1.0f);
Vector4 p2 = Vector4(position + Vector2(halfSize.X, halfSize.Y), 0.0f, 1.0f);
Vector4 p3 = Vector4(position + Vector2(-halfSize.X, halfSize.Y), 0.0f, 1.0f);
float4 p0 = float4(position + float2(-halfSize.X, -halfSize.Y), 0.0f, 1.0f);
float4 p1 = float4(position + float2(halfSize.X, -halfSize.Y), 0.0f, 1.0f);
float4 p2 = float4(position + float2(halfSize.X, halfSize.Y), 0.0f, 1.0f);
float4 p3 = float4(position + float2(-halfSize.X, halfSize.Y), 0.0f, 1.0f);
DrawLine(p0, p1, color, entityId);
DrawLine(p1, p2, color, entityId);
@@ -977,12 +977,12 @@ namespace GlitchyEngine.Renderer
*/
public static void DrawRect(Matrix transform, ColorRGBA color = .White, uint32 entityId = uint32.MaxValue)
{
Vector2 halfSize = Vector2.One / 2.0f;
float2 halfSize = float2.One / 2.0f;
Vector4 p0 = transform * Vector4(-halfSize.X, -halfSize.Y, 0.0f, 1.0f);
Vector4 p1 = transform * Vector4(halfSize.X, -halfSize.Y, 0.0f, 1.0f);
Vector4 p2 = transform * Vector4(halfSize.X, halfSize.Y, 0.0f, 1.0f);
Vector4 p3 = transform * Vector4(-halfSize.X, halfSize.Y, 0.0f, 1.0f);
float4 p0 = transform * float4(-halfSize.X, -halfSize.Y, 0.0f, 1.0f);
float4 p1 = transform * float4(halfSize.X, -halfSize.Y, 0.0f, 1.0f);
float4 p2 = transform * float4(halfSize.X, halfSize.Y, 0.0f, 1.0f);
float4 p3 = transform * float4(-halfSize.X, halfSize.Y, 0.0f, 1.0f);
DrawLine(p0, p1, color, entityId);
DrawLine(p1, p2, color, entityId);
@@ -990,7 +990,7 @@ namespace GlitchyEngine.Renderer
DrawLine(p3, p0, color, entityId);
}
public static void DrawLine(Vector4 start, Vector4 end, ColorRGBA color = .White, uint32 entityId = uint32.MaxValue)
public static void DrawLine(float4 start, float4 end, ColorRGBA color = .White, uint32 entityId = uint32.MaxValue)
{
Debug.Profiler.ProfileRendererFunction!();
@@ -1008,23 +1008,23 @@ namespace GlitchyEngine.Renderer
// Colored Quad
public static void DrawQuad(Vector2 position, Vector2 size, float rotation, ColorRGBA color)
public static void DrawQuad(float2 position, float2 size, float rotation, ColorRGBA color)
{
DrawQuad(Vector3(position, 0.0f), size, rotation, s_whiteTexture, color);
DrawQuad(float3(position, 0.0f), size, rotation, s_whiteTexture, color);
}
/// Like DrawQuad but the pivot point is the top left corner
public static void DrawQuadPivotCorner(Vector2 position, Vector2 size, float rotation, ColorRGBA color)
public static void DrawQuadPivotCorner(float2 position, float2 size, float rotation, ColorRGBA color)
{
DrawQuadPivotCorner(Vector3(position, 0.0f), size, rotation, s_whiteTexture, color);
DrawQuadPivotCorner(float3(position, 0.0f), size, rotation, s_whiteTexture, color);
}
public static void DrawQuad(Vector3 position, Vector2 size, float rotation, ColorRGBA color)
public static void DrawQuad(float3 position, float2 size, float rotation, ColorRGBA color)
{
DrawQuad(position, size, rotation, s_whiteTexture, color);
}
public static void DrawQuadPivotCorner(Vector3 position, Vector2 size, float rotation, ColorRGBA color)
public static void DrawQuadPivotCorner(float3 position, float2 size, float rotation, ColorRGBA color)
{
DrawQuadPivotCorner(position, size, rotation, s_whiteTexture, color);
}
@@ -1037,9 +1037,9 @@ namespace GlitchyEngine.Renderer
// Quad Subtexture
[Inline]
private static Vector4 CalculateSubTexcoords(Vector4 texCoords, Vector4 innerTexcoords)
private static float4 CalculateSubTexcoords(float4 texCoords, float4 innerTexcoords)
{
Vector4 uv = texCoords;
float4 uv = texCoords;
uv.XY += innerTexcoords.XY * uv.ZW;
uv.ZW *= innerTexcoords.ZW;
@@ -1048,12 +1048,12 @@ namespace GlitchyEngine.Renderer
// Subtex only
public static void DrawQuad(Vector2 position, Vector2 size, float rotation, SubTexture2D texture, ColorRGBA color = .White)
public static void DrawQuad(float2 position, float2 size, float rotation, SubTexture2D texture, ColorRGBA color = .White)
{
DrawQuad(Vector3(position, 0.0f), size, rotation, texture.Texture, .White, texture.TexCoords);
DrawQuad(float3(position, 0.0f), size, rotation, texture.Texture, .White, texture.TexCoords);
}
public static void DrawQuad(Vector3 position, Vector2 size, float rotation, SubTexture2D texture, ColorRGBA color = .White)
public static void DrawQuad(float3 position, float2 size, float rotation, SubTexture2D texture, ColorRGBA color = .White)
{
DrawQuad(position, size, rotation, texture.Texture, .White, texture.TexCoords);
}
@@ -1065,42 +1065,42 @@ namespace GlitchyEngine.Renderer
// Subtex + Texcoords
public static void DrawQuad(Vector2 position, Vector2 size, float rotation, SubTexture2D subtexture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
public static void DrawQuad(float2 position, float2 size, float rotation, SubTexture2D subtexture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{
Vector4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
float4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
DrawQuad(Vector3(position, 0.0f), size, rotation, subtexture.Texture, .White, uv);
DrawQuad(float3(position, 0.0f), size, rotation, subtexture.Texture, .White, uv);
}
public static void DrawQuad(Vector3 position, Vector2 size, float rotation, SubTexture2D subtexture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
public static void DrawQuad(float3 position, float2 size, float rotation, SubTexture2D subtexture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{
Vector4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
float4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
DrawQuad(position, size, rotation, subtexture.Texture, .White, uv);
}
public static void DrawQuad(Matrix transform, SubTexture2D subtexture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1), uint32 entityId = uint32.MaxValue)
public static void DrawQuad(Matrix transform, SubTexture2D subtexture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1), uint32 entityId = uint32.MaxValue)
{
Vector4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
float4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
DrawQuad(transform, subtexture.Texture, color, uv, entityId);
}
// Textured Quad
public static void DrawQuad(Vector2 position, Vector2 size, float rotation, Texture texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
public static void DrawQuad(float2 position, float2 size, float rotation, Texture texture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{
DrawQuad(Vector3(position, 0.0f), size, rotation, texture, color, uvTransform);
DrawQuad(float3(position, 0.0f), size, rotation, texture, color, uvTransform);
}
public static void DrawQuad(Vector3 position, Vector2 size, float rotation, Texture texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
public static void DrawQuad(float3 position, float2 size, float rotation, Texture texture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{
Matrix transform = Calculate2DTransform(position, size, rotation);
DrawQuad(transform, texture, color, uvTransform);
}
public static void DrawQuad(Matrix transform, Texture texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1), uint32 entityId = uint32.MaxValue)
public static void DrawQuad(Matrix transform, Texture texture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1), uint32 entityId = uint32.MaxValue)
{
Debug.Profiler.ProfileRendererFunction!();
@@ -1128,41 +1128,41 @@ namespace GlitchyEngine.Renderer
// Textured quad pivot
public static void DrawQuadPivotCorner(Vector2 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
public static void DrawQuadPivotCorner(float2 position, float2 size, float rotation, Texture2D texture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{
DrawQuadPivotCorner(Vector3(position, 0.0f), size, rotation, texture, color, uvTransform);
DrawQuadPivotCorner(float3(position, 0.0f), size, rotation, texture, color, uvTransform);
}
public static void DrawQuadPivotCorner(Vector3 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
public static void DrawQuadPivotCorner(float3 position, float2 size, float rotation, Texture2D texture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{
DrawQuad(position + Vector3(size.X / 2, size.Y / -2, 0), size, rotation, texture, color, uvTransform);
DrawQuad(position + float3(size.X / 2, size.Y / -2, 0), size, rotation, texture, color, uvTransform);
}
// Circle
public static void DrawCircle(Vector2 position, Vector2 size, ColorRGBA color, float innerRadius = 1.0f)
public static void DrawCircle(float2 position, float2 size, ColorRGBA color, float innerRadius = 1.0f)
{
DrawCircle(Vector3(position, 0.0f), size, 0, s_whiteTexture, color, innerRadius);
DrawCircle(float3(position, 0.0f), size, 0, s_whiteTexture, color, innerRadius);
}
public static void DrawCircle(Vector3 position, Vector2 size, ColorRGBA color, float innerRadius = 1.0f)
public static void DrawCircle(float3 position, float2 size, ColorRGBA color, float innerRadius = 1.0f)
{
DrawCircle(position, size, 0, s_whiteTexture, color, innerRadius);
}
public static void DrawCircle(Vector2 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, float innerRadius = 1.0f, Vector4 uvTransform = .(0, 0, 1, 1))
public static void DrawCircle(float2 position, float2 size, float rotation, Texture2D texture, ColorRGBA color = .White, float innerRadius = 1.0f, float4 uvTransform = .(0, 0, 1, 1))
{
DrawCircle(Vector3(position, 0.0f), size, rotation, texture, color, innerRadius, uvTransform);
DrawCircle(float3(position, 0.0f), size, rotation, texture, color, innerRadius, uvTransform);
}
public static void DrawCircle(Vector3 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, float innerRadius = 1.0f, Vector4 uvTransform = .(0, 0, 1, 1))
public static void DrawCircle(float3 position, float2 size, float rotation, Texture2D texture, ColorRGBA color = .White, float innerRadius = 1.0f, float4 uvTransform = .(0, 0, 1, 1))
{
Matrix transform = Calculate2DTransform(position, size, rotation);
DrawCircle(transform, texture, color, innerRadius, uvTransform);
}
public static void DrawCircle(Matrix transform, Texture2D texture, ColorRGBA color = .White, float innerRadius = 1.0f, Vector4 uvTransform = .(0, 0, 1, 1), uint32 entityId = uint32.MaxValue)
public static void DrawCircle(Matrix transform, Texture2D texture, ColorRGBA color = .White, float innerRadius = 1.0f, float4 uvTransform = .(0, 0, 1, 1), uint32 entityId = uint32.MaxValue)
{
Debug.Profiler.ProfileRendererFunction!();
+9 -9
View File
@@ -6,34 +6,34 @@ namespace GlitchyEngine.Renderer
class SubTexture2D : RefCounter
{
protected Texture2D _texture ~ _.ReleaseRef();
protected Vector4 _texCoords;
protected float4 _texCoords;
public Texture2D Texture => _texture;
public Vector4 TexCoords => _texCoords;
public float4 TexCoords => _texCoords;
public this(Texture2D texture) : this(_texture, .(0, 0, 1, 1)) { }
public this(Texture2D texture, Int2 topLeft, Int2 size) :
this(texture,
{
Vector2 texSize = Vector2(texture.Width, texture.Height);
Vector4 texCoords = Vector4((Vector2)topLeft, (Vector2)size) / texSize.XYXY;
float2 texSize = float2(texture.Width, texture.Height);
float4 texCoords = float4((float2)topLeft, (float2)size) / texSize.XYXY;
texCoords
}) { }
public this(Texture2D texture, Vector2 topLeft, Vector2 size) : this(texture, Vector4(topLeft, size)) { }
public this(Texture2D texture, float2 topLeft, float2 size) : this(texture, float4(topLeft, size)) { }
public this(Texture2D texture, Vector4 texCoords)
public this(Texture2D texture, float4 texCoords)
{
_texture = texture..AddRef();
_texCoords = texCoords;
}
public static SubTexture2D CreateFromGrid(Texture2D texture, Vector2 coords, Vector2 gridSize, Vector2 spriteSize = .One)
public static SubTexture2D CreateFromGrid(Texture2D texture, float2 coords, float2 gridSize, float2 spriteSize = .One)
{
Vector2 textureSize = .(texture.Width, texture.Height);
float2 textureSize = .(texture.Width, texture.Height);
Vector4 uv = .(coords * gridSize, gridSize * spriteSize) / textureSize.XYXY;
float4 uv = .(coords * gridSize, gridSize * spriteSize) / textureSize.XYXY;
return new SubTexture2D(texture, uv);
}
+24 -24
View File
@@ -103,12 +103,12 @@ namespace GlitchyEngine.Renderer.Text
{
public Font Font;
public uint32 GlyphId;
public Vector2 Position;
public float2 Position;
public float Scale;
//public float AdvanceX;
//public float AdvanceY;
public this(Font font, uint32 glyphId, Vector2 position, float scale)//, float advanceX, float advanceY)
public this(Font font, uint32 glyphId, float2 position, float scale)//, float advanceX, float advanceY)
{
Font = font;
GlyphId = glyphId;
@@ -341,7 +341,7 @@ namespace GlitchyEngine.Renderer.Text
_msdfEffect.Variables["ViewProjection"].SetData(viewProjection);
// TODO: this doesn't really work with fallback fonts
Vector2 unitRange = Vector2((float)text.Font._range) / Vector2(text.Font._atlas.Width, text.Font._atlas.Height);
float2 unitRange = ((float)text.Font._range) / float2(text.Font._atlas.Width, text.Font._atlas.Height);
_msdfEffect.Variables["UnitRange"].SetData(unitRange);
List<Texture2D> atlasses = scope .();
@@ -366,7 +366,7 @@ namespace GlitchyEngine.Renderer.Text
atlasses.Add(atlas..AddRef());
}
Vector2 atlasSize = .(atlas.Width, atlas.Height);
float2 atlasSize = .(atlas.Width, atlas.Height);
float adjustToPenX = glyphDesc.AdjustToPen;
//adjustToPenX *= glyphFontScale;
@@ -377,7 +377,7 @@ namespace GlitchyEngine.Renderer.Text
adjustToBaseline *= glyph.Scale;
// Rectangle on the screen
Vector4 viewportRect = .(
float4 viewportRect = .(
// TODO: merge adjustToPenX and adjustToBaseline into Position
glyph.Position.X + adjustToPenX,
glyph.Position.Y + adjustToBaseline,
@@ -385,24 +385,24 @@ namespace GlitchyEngine.Renderer.Text
glyphDesc.Height * glyph.Scale);
// Rectangle on the font atlas
Vector4 texRect = .(glyphDesc.MapCoord.X, glyphDesc.MapCoord.Y, glyphDesc.Width, glyphDesc.Height);
float4 texRect = .(glyphDesc.MapCoord.X, glyphDesc.MapCoord.Y, glyphDesc.Width, glyphDesc.Height);
Color glyphColor = fontColor;//Color.White;// TODO: glyphDesc.IsBitmap ? bitmapColor : fontColor;
texRect /= Vector4(atlasSize, atlasSize);
texRect /= float4(atlasSize, atlasSize);
// Show quads
// Renderer2D.DrawQuad(Vector3(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2, 1), .(viewportRect.Z, viewportRect.W), 0, .Red);
// Renderer2D.DrawQuad(float3(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2, 1), .(viewportRect.Z, viewportRect.W), 0, .Red);
Vector3 position = .(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2, 0);
float3 position = .(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2, 0);
Matrix glyphTransform = transform * Matrix.Translation(position) * Matrix.Scaling(viewportRect.Z, viewportRect.W, 1.0f);
Renderer2D.DrawQuad(glyphTransform, atlas, (ColorRGBA)glyphColor, texRect);
//Renderer2D.DrawQuad(Vector2(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2), .(viewportRect.Z, viewportRect.W), 0, atlas, glyphColor, texRect);
//Renderer2D.DrawQuad(float2(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2), .(viewportRect.Z, viewportRect.W), 0, atlas, glyphColor, texRect);
// Show pen positions
// Renderer2D.DrawQuad(Vector3(Vector2(x, y) + glyph.Position, -1), .(1), 0, .Green);
// Renderer2D.DrawQuad(float3(float2(x, y) + glyph.Position, -1), .(1), 0, .Green);
}
Renderer2D.Flush();
@@ -451,7 +451,7 @@ namespace GlitchyEngine.Renderer.Text
_msdfEffect.Variables["screenPixelRange"].SetData(scale * 4.0f);
// TODO: this doesn't really work with fallback fonts
Vector2 unitRange = Vector2((float)font._range) / Vector2(font._atlas.Width, font._atlas.Height);
float2 unitRange = ((float)font._range) / float2(font._atlas.Width, font._atlas.Height);
_msdfEffect.Variables["UnitRange"].SetData(unitRange);
// Space between two baselines
@@ -550,7 +550,7 @@ namespace GlitchyEngine.Renderer.Text
atlasses.Add(atlas..AddRef());
}
Vector2 atlasSize = .(atlas.Width, atlas.Height);
float2 atlasSize = .(atlas.Width, atlas.Height);
float adjustToPenX = glyphDesc.AdjustToPen;
adjustToPenX *= glyphFontScale;
@@ -559,26 +559,26 @@ namespace GlitchyEngine.Renderer.Text
adjustToBaseline *= glyphFontScale;
// Rectangle on the screen
Vector4 viewportRect = .(
float4 viewportRect = .(
penPosition + adjustToPenX,
baseline + adjustToBaseline,
glyphDesc.Width * glyphFontScale,
glyphDesc.Height * glyphFontScale);
// Rectangle on the font atlas
Vector4 texRect = .(glyphDesc.MapCoord.X, glyphDesc.MapCoord.Y, glyphDesc.Width, glyphDesc.Height);
float4 texRect = .(glyphDesc.MapCoord.X, glyphDesc.MapCoord.Y, glyphDesc.Width, glyphDesc.Height);
Color glyphColor = glyphDesc.IsBitmap ? bitmapColor : fontColor;
texRect /= Vector4(atlasSize, atlasSize);
texRect /= float4(atlasSize, atlasSize);
// Show quads
// Renderer2D.DrawQuad(Vector3(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2, 1), .(viewportRect.Z, viewportRect.W), 0, .Red);
// Renderer2D.DrawQuad(float3(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2, 1), .(viewportRect.Z, viewportRect.W), 0, .Red);
Renderer2D.DrawQuad(Vector2(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2), .(viewportRect.Z, viewportRect.W), 0, atlas, glyphColor, texRect);
Renderer2D.DrawQuad(float2(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2), .(viewportRect.Z, viewportRect.W), 0, atlas, glyphColor, texRect);
// Show pen positions
// Renderer2D.DrawQuad(Vector3(penPosition, baseline, -1), .(1), 0, .Green);
// Renderer2D.DrawQuad(float3(penPosition, baseline, -1), .(1), 0, .Green);
penPosition += (x_advance / 64) * glyphFontScale;
baseline += (y_advance / 64) * glyphFontScale;
@@ -627,7 +627,7 @@ namespace GlitchyEngine.Renderer.Text
atlasses.Add(atlas..AddRef());
}
Vector2 atlasSize = .(atlas.Width, atlas.Height);
float2 atlasSize = .(atlas.Width, atlas.Height);
float adjustToPenX = glyphDesc.AdjustToPen;
adjustToPenX *= glyphFontScale;
@@ -636,20 +636,20 @@ namespace GlitchyEngine.Renderer.Text
adjustToBaseline *= glyphFontScale;
// Rectangle on the screen
Vector4 viewportRect = .(
float4 viewportRect = .(
penPosition + adjustToPenX,
baseline + adjustToBaseline,
glyphDesc.Width * glyphFontScale,
glyphDesc.Height * glyphFontScale);
// Rectangle on the font atlas
Vector4 texRect = .(glyphDesc.MapCoord.X, glyphDesc.MapCoord.Y, glyphDesc.Width, glyphDesc.Height);
float4 texRect = .(glyphDesc.MapCoord.X, glyphDesc.MapCoord.Y, glyphDesc.Width, glyphDesc.Height);
Color glyphColor = glyphDesc.IsBitmap ? bitmapColor : fontColor;
texRect /= Vector4(atlasSize, atlasSize);
texRect /= float4(atlasSize, atlasSize);
Renderer2D.DrawQuad(Vector2(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2), .(viewportRect.Z, viewportRect.W), 0, atlas, (ColorRGBA)glyphColor, texRect);
Renderer2D.DrawQuad(float2(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2), .(viewportRect.Z, viewportRect.W), 0, atlas, (ColorRGBA)glyphColor, texRect);
//renderer.Draw(atlas, viewportRect.X, viewportRect.Y, viewportRect.Z, viewportRect.W, glyphColor, *(float*)(&depthInt), texRect);
+7 -7
View File
@@ -30,7 +30,7 @@ enum ScriptFieldType
UInt, // UInt2, UInt3, UInt4,
ULong,
// Half, Half2, Half3, Half4,
Float, Vector2, Vector3, Vector4,
Float, float2, float3, float4,
Double, // Double2, Double3, Double4,
Entity
@@ -53,9 +53,9 @@ static sealed class ScriptEngineHelper
("System.UInt64", .ULong),
("System.Single", .Float),
("GlitchyEngine.Math.Vector2", .Vector2),
("GlitchyEngine.Math.Vector3", .Vector3),
("GlitchyEngine.Math.Vector4", .Vector4),
("GlitchyEngine.Math.float2", .float2),
("GlitchyEngine.Math.float3", .float3),
("GlitchyEngine.Math.float4", .float4),
("System.Double", .Double),
@@ -314,9 +314,9 @@ static class ScriptEngine
Mono.mono_add_internal_call("GlitchyEngine.CSharpTesting::Sample", v);
function void(in Vector3, in Vector3, out Vector3) v2 = => Add;
function void(in float3, in float3, out float3) v2 = => Add;
Mono.mono_add_internal_call("GlitchyEngine.Vector3::Add_Internal", v2);
Mono.mono_add_internal_call("GlitchyEngine.float3::Add_Internal", v2);
// Create object
ScriptClass myClass = scope .("GlitchyEngine", "CSharpTesting");
@@ -342,7 +342,7 @@ static class ScriptEngine
}
[LinkName(.C), AlwaysInclude, Export]
public static void Add(in Vector3 a, in Vector3 b, out Vector3 c)
public static void Add(in float3 a, in float3 b, out float3 c)
{
c = a + b;
}
+13 -7
View File
@@ -187,7 +187,7 @@ static class ScriptGlue
#region TransformComponent
[RegisterCall("ScriptGlue::Transform_GetTranslation")]
static void Transform_GetTranslation(UUID entityId, ref Vector3 translation)
static void Transform_GetTranslation(UUID entityId, ref float3 translation)
{
Scene scene = ScriptEngine.Context;
Entity entity = scene.GetEntityByID(entityId);
@@ -196,12 +196,18 @@ static class ScriptGlue
}
[RegisterCall("ScriptGlue::Transform_SetTranslation")]
static void Transform_SetTranslation(UUID entityId, ref Vector3 translation)
static void Transform_SetTranslation(UUID entityId, ref float3 translation)
{
Scene scene = ScriptEngine.Context;
Entity entity = scene.GetEntityByID(entityId);
entity.Transform.Position = translation;
// if necessary reposition Rigidbody2D
if (entity.TryGetComponent<Rigidbody2DComponent>(let rigidbody2D))
{
rigidbody2D.SetPosition(translation.XY);
}
}
#endregion TransformComponent
@@ -209,7 +215,7 @@ static class ScriptGlue
#region RigidBody2D
[RegisterCall("ScriptGlue::RigidBody2D_ApplyForce")]
static void RigidBody2D_ApplyForce(UUID entityId, ref Vector2 force, ref Vector2 point, bool wakeUp)
static void RigidBody2D_ApplyForce(UUID entityId, ref float2 force, ref float2 point, bool wakeUp)
{
Scene scene = ScriptEngine.Context;
Entity entity = scene.GetEntityByID(entityId);
@@ -219,7 +225,7 @@ static class ScriptGlue
}
[RegisterCall("ScriptGlue::RigidBody2D_ApplyForceToCenter")]
static void RigidBody2D_ApplyForceToCenter(UUID entityId, ref Vector2 force, bool wakeUp)
static void RigidBody2D_ApplyForceToCenter(UUID entityId, ref float2 force, bool wakeUp)
{
Scene scene = ScriptEngine.Context;
Entity entity = scene.GetEntityByID(entityId);
@@ -235,16 +241,16 @@ static class ScriptGlue
// TODO: We need a wrapper class!
[RegisterCall("ScriptGlue::Physics2D_GetGravity")]
static void Physics2D_GetGravity(ref Vector2 gravity)
static void Physics2D_GetGravity(ref float2 gravity)
{
Scene scene = ScriptEngine.Context;
var box2DGravity = Box2D.World.GetGravity(scene.[Friend]_physicsWorld2D);
gravity = *(Vector2*)&box2DGravity;
gravity = *(float2*)&box2DGravity;
}
[RegisterCall("ScriptGlue::Physics2D_SetGravity")]
static void Physics2D_SetGravity(ref Vector2 gravity)
static void Physics2D_SetGravity(ref float2 gravity)
{
Scene scene = ScriptEngine.Context;
@@ -109,17 +109,17 @@ class ScriptInstance : RefCounter
var value = GetFieldValue<float>(field);
target.SetFieldValue(field, value);
/*case .Vector2:
GetFieldValue<Vector2>(field, var value);
if (ImGui.EditVector2(fieldName, ref value))
/*case .float2:
GetFieldValue<float2>(field, var value);
if (ImGui.Editfloat2(fieldName, ref value))
SetFieldValue(field, value);
case .Vector3:
GetFieldValue<Vector3>(field, var value);
if (ImGui.EditVector3(fieldName, ref value))
case .float3:
GetFieldValue<float3>(field, var value);
if (ImGui.Editfloat3(fieldName, ref value))
SetFieldValue(field, value);
case .Vector4:
GetFieldValue<Vector4>(field, var value);
if (ImGui.EditVector4(fieldName, ref value))
case .float4:
GetFieldValue<float4>(field, var value);
if (ImGui.Editfloat4(fieldName, ref value))
SetFieldValue(field, value);
case .Double:
@@ -52,7 +52,7 @@ namespace GlitchyEngine.World
public AssetHandle<Texture2D> Sprite = .Invalid;
public ColorRGBA Color = .White;
public Vector4 UvTransform = .(0, 0, 1, 1);
public float4 UvTransform = .(0, 0, 1, 1);
public this()
{
@@ -70,7 +70,7 @@ namespace GlitchyEngine.World
public AssetHandle<Texture2D> Sprite = .Invalid;
public ColorRGBA Color = .White;
public Vector4 UvTransform = .(0, 0, 1, 1);
public float4 UvTransform = .(0, 0, 1, 1);
public float InnerRadius = 0.0f;
@@ -385,19 +385,79 @@ namespace GlitchyEngine.World
private int _runtimeBody = 0;
internal b2Body* RuntimeBody
protected internal b2Body* RuntimeBody
{
[Inline]
get => (b2Body*)(void*)_runtimeBody;
[Inline]
set mut => _runtimeBody = (int)(void*)value;
}
public float2 GetPosition()
{
if (RuntimeBody != null)
{
Box2D.b2Vec2 vec = Box2D.Body.GetPosition(RuntimeBody);
return *(float2*)&vec;
}
return .Zero;
}
public void SetPosition(float2 position)
{
var position;
if (RuntimeBody != null)
{
float angle = Box2D.Body.GetAngle(RuntimeBody);
Box2D.Body.SetTransform(RuntimeBody, ref *(Box2D.b2Vec2*)&position, angle);
}
}
public float GetAngle()
{
if (RuntimeBody != null)
{
return Box2D.Body.GetAngle(RuntimeBody);
}
return float.NaN;
}
public void SetAngle(float angle)
{
if (RuntimeBody != null)
{
var pos = Box2D.Body.GetPosition(RuntimeBody);
Box2D.Body.SetTransform(RuntimeBody, ref pos, angle);
}
}
}
/*struct InternalBug
{
private int i;
internal int MyInt
{
get => i;
set mut => i = value;
}
public void Bla()
{
MyInt = 5;
}
}*/
struct BoxCollider2DComponent
{
public Vector2 Offset = .(0.0f, 0.0f);
public Vector2 Size = .(0.5f, 0.5f);
public float2 Offset = .(0.0f, 0.0f);
public float2 Size = .(0.5f, 0.5f);
// TODO: move into 2D physics material
public float Density = 1.0f;
@@ -421,7 +481,7 @@ namespace GlitchyEngine.World
struct CircleCollider2DComponent
{
public Vector2 Offset = .(0.0f, 0.0f);
public float2 Offset = .(0.0f, 0.0f);
public float Radius = 0.5f;
@@ -6,11 +6,11 @@ namespace GlitchyEngine.World
{
EcsEntity _parent = .InvalidEntity;
Vector3 _position = .(0, 0, 0);
float3 _position = .(0, 0, 0);
Quaternion _rotation = .(0, 0, 0, 1);
Vector3 _scale = .(1, 1, 1);
float3 _scale = .(1, 1, 1);
Vector3 _editorRotationEuler = .Zero;
float3 _editorRotationEuler = .Zero;
Matrix _localTransform = .Identity;
public bool IsDirty = false;
@@ -52,12 +52,12 @@ namespace GlitchyEngine.World
}
}
public Vector3 Position
public float3 Position
{
get => _position;
set mut
{
if(_position == value)
if(all(_position == value))
return;
_position = value;
@@ -82,12 +82,12 @@ namespace GlitchyEngine.World
}
/// Allows the user to edit the euler angles in the editor without rotations getting funky because of singularities or ambiguity of angles.
internal Vector3 EditorRotationEuler
internal float3 EditorRotationEuler
{
get => _editorRotationEuler;
set mut
{
if (_editorRotationEuler == value)
if (all(_editorRotationEuler == value))
return;
_editorRotationEuler = value;
@@ -101,7 +101,7 @@ namespace GlitchyEngine.World
* Gets or sets the rotation using euler angles.
* @Note The rotations will be applied in the following order: YZX (the order in the vector is still XYZ!)
*/
public Vector3 RotationEuler
public float3 RotationEuler
{
get => Quaternion.ToEulerAngles(_rotation);
set mut => Rotation = Quaternion.FromEulerAngles(value.Y, value.X, value.Z);
@@ -111,7 +111,7 @@ namespace GlitchyEngine.World
* Gets or sets the rotation using axis angle, where Axis is the axis around which will be rotated and angle is the angle
* that was rotated around the axis in radians.
*/
public (Vector3 Axis, float Angle) RotationAxisAngle
public (float3 Axis, float Angle) RotationAxisAngle
{
get => _rotation.ToAxisAngle();
set mut
@@ -126,12 +126,12 @@ namespace GlitchyEngine.World
}
}
public Vector3 Scale
public float3 Scale
{
get => _scale;
set mut
{
if(_scale == value)
if(all(_scale == value))
return;
_scale = value;
+16 -16
View File
@@ -8,10 +8,10 @@ namespace GlitchyEngine.World
{
struct EditorCamera : Camera, IDisposable
{
private Vector3 _position;
private float3 _position;
private Quaternion _rotation;
private Vector3 _focalPosition = .Zero;
private float3 _focalPosition = .Zero;
private float _focalDistance = 5.0f;
private float _cameraTranslationSpeed = 2.0f;
@@ -52,12 +52,12 @@ namespace GlitchyEngine.World
}
}
public Vector3 Position
public float3 Position
{
get => _position;
set mut
{
if (_position == value)
if (all(_position == value))
return;
_position = value;
@@ -78,7 +78,7 @@ namespace GlitchyEngine.World
}
}
public Vector3 RotationEuler
public float3 RotationEuler
{
get => Quaternion.ToEulerAngles(_rotation);
set mut
@@ -92,7 +92,7 @@ namespace GlitchyEngine.World
}
}
public (Vector3 Axis, float Angle) RotationAxisAngle
public (float3 Axis, float Angle) RotationAxisAngle
{
get => _rotation.ToAxisAngle();
set mut
@@ -145,7 +145,7 @@ namespace GlitchyEngine.World
}
}
public this(Vector3 position, Quaternion rotation, float fovY, float nearPlane, float aspectRatio)
public this(float3 position, Quaternion rotation, float fovY, float nearPlane, float aspectRatio)
{
_position = position;
_rotation = rotation;
@@ -190,7 +190,7 @@ namespace GlitchyEngine.World
bool transformChanged = false;
Vector3 movement = .();
float3 movement = .();
if(Input.IsKeyPressed(Key.W))
movement.Z += 1;
@@ -207,16 +207,16 @@ namespace GlitchyEngine.World
if(Input.IsKeyPressed(Key.Control))
movement.Y -= 1;
if(movement != .Zero)
if(any(movement != .Zero))
{
movement.Normalize();
normalize(movement);
if(Input.IsKeyPressed(Key.Shift))
movement *= _cameraFastFactor;
movement *= (float)(gameTime.DeltaTime) * _cameraTranslationSpeed;
Vector4 delta = Vector4(movement, 1.0f) * _view;
float4 delta = float4(movement, 1.0f) * _view;
_position += delta.XYZ;
@@ -231,7 +231,7 @@ namespace GlitchyEngine.World
if (MouseCooldown == 0)
{
Vector3 rotationEuler = RotationEuler + Vector3(rotX, rotY, 0);
float3 rotationEuler = RotationEuler + float3(rotX, rotY, 0);
_rotation = Quaternion.FromEulerAngles(rotationEuler.Y, rotationEuler.X, rotationEuler.Z);
transformChanged = true;
@@ -262,13 +262,13 @@ namespace GlitchyEngine.World
if (MouseCooldown == 0 && Input.IsMouseButtonPressed(.LeftButton) && mouseDelta != .())
{
Vector2 movement = .(
float2 movement = .(
-mouseDelta.X,
mouseDelta.Y);
movement *= (float)(gameTime.DeltaTime) * _cameraTranslationSpeed * GetZoomSpeed();
Vector4 delta = Vector4(movement, 0.0f, 1.0f) * _view;
float4 delta = float4(movement, 0.0f, 1.0f) * _view;
_focalPosition += delta.XYZ;
@@ -280,7 +280,7 @@ namespace GlitchyEngine.World
float rotY = mouseDelta.X * _cameraRotationSpeedX;
float rotX = mouseDelta.Y * _cameraRotationSpeedY;
Vector3 rotationEuler = RotationEuler + Vector3(rotX, rotY, 0);
float3 rotationEuler = RotationEuler + float3(rotX, rotY, 0);
_rotation = Quaternion.FromEulerAngles(rotationEuler.Y, rotationEuler.X, rotationEuler.Z);
transformChanged = true;
@@ -294,7 +294,7 @@ namespace GlitchyEngine.World
{
Matrix viewRotation = Matrix.RotationQuaternion(Quaternion.Inverse(_rotation));
Vector4 offset = Vector4(0, 0, -_focalDistance, 1.0f) * viewRotation;
float4 offset = float4(0, 0, -_focalDistance, 1.0f) * viewRotation;
if (_isAltMode)
_position = _focalPosition + offset.XYZ;
else