BREAKING CHANGE: 4x4 matrices are now column major

This commit is contained in:
2026-01-27 23:45:50 +03:00
parent 9294df8a19
commit 08f32f96e4
3 changed files with 54 additions and 52 deletions

View File

@@ -6,7 +6,7 @@ namespace Engine.Core;
// TODO Comments
/// <summary>
/// Represents a 4D left handed space matrix.
/// Represents a 4D left handed space matrix in a Column Major convention.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="Matrix4x4"/> struct with the specified values.
@@ -220,10 +220,10 @@ public readonly struct Matrix4x4(
);
public static Matrix4x4 CreateTranslation(Vector3D position) => new(
1f, 0f, 0f, 0f,
0f, 1f, 0f, 0f,
0f, 0f, 1f, 0f,
position.X, position.Y, position.Z, 1
1f, 0f, 0f, position.X,
0f, 1f, 0f, position.Y,
0f, 0f, 1f, position.Z,
0f, 0f, 0f, 1f
);
public static Matrix4x4 CreateScale(float scale) => new(
@@ -245,11 +245,11 @@ public readonly struct Matrix4x4(
float c = Math.Cos(radians);
float s = Math.Sin(radians);
return new Matrix4x4(
1f, 0f, 0f, 0f,
0f, c, s, 0f,
0f, -s, c, 0f,
0f, 0f, 0f, 1f
return new(
1f, 0f, 0f, 0f,
0f, c, -s, 0f,
0f, s, c, 0f,
0f, 0f, 0f, 1f
);
}
@@ -258,10 +258,10 @@ public readonly struct Matrix4x4(
float c = Math.Cos(radians);
float s = Math.Sin(radians);
return new Matrix4x4(
c, 0f, -s, 0f,
return new(
c, 0f, s, 0f,
0f, 1f, 0f, 0f,
s, 0f, c, 0f,
-s, 0f, c, 0f,
0f, 0f, 0f, 1f
);
}
@@ -271,9 +271,9 @@ public readonly struct Matrix4x4(
float c = Math.Cos(radians);
float s = Math.Sin(radians);
return new Matrix4x4(
c, s, 0f, 0f,
-s, c, 0f, 0f,
return new(
c, -s, 0f, 0f,
s, c, 0f, 0f,
0f, 0f, 1f, 0f,
0f, 0f, 0f, 1f
);
@@ -292,7 +292,7 @@ public readonly struct Matrix4x4(
Vector3D x = up.Cross(z).Normalized;
Vector3D y = z.Cross(x);
return new Matrix4x4(
return new(
x.X, y.X, z.X, 0f,
x.Y, y.Y, z.Y, 0f,
x.Z, y.Z, z.Z, 0f,
@@ -302,15 +302,15 @@ public readonly struct Matrix4x4(
public static Matrix4x4 CreateLookMatrix(Vector3D position, Vector3D target, Vector3D up)
{
Vector3D z = position.FromTo(target).Normalized;
Vector3D x = up.Cross(z).Normalized;
Vector3D y = z.Cross(x);
Vector3D f = (target - position).Normalized;
Vector3D s = f.Cross(up).Normalized;
Vector3D u = s.Cross(f);
return new Matrix4x4(
x.X, y.X, z.X, 0f,
x.Y, y.Y, z.Y, 0f,
x.Z, y.Z, z.Z, 0f,
-x.Dot(position), -y.Dot(position), -z.Dot(position), 1f
return new(
s.X, u.X, -f.X, 0f,
s.Y, u.Y, -f.Y, 0f,
s.Z, u.Z, -f.Z, 0f,
-s.Dot(position), -u.Dot(position), f.Dot(position), 1f
);
}
@@ -318,10 +318,10 @@ public readonly struct Matrix4x4(
{
float invDepth = 1f / (farPlane - nearPlane);
return new Matrix4x4(
return new(
2f / width, 0f, 0f, 0f,
0f, -2f / height, 0f, 0f,
0f, 0f, invDepth, 0f,
0f, 0f, 1f * invDepth, 0f,
-1f, 1f, -nearPlane * invDepth, 1f
);
}
@@ -330,24 +330,25 @@ public readonly struct Matrix4x4(
{
float invDepth = 1f / (farPlane - nearPlane);
return new Matrix4x4(
return new(
2f / width, 0f, 0f, 0f,
0f, 2f / height, 0f, 0f,
0f, 0f, invDepth, 0f,
0f, 0f, 1f * invDepth, 0f,
0f, 0f, -nearPlane * invDepth, 1f
);
}
public static Matrix4x4 CreatePerspectiveFieldOfView(float fieldOfViewInRadians, float aspectRatio, float nearPlane, float farPlane)
public static Matrix4x4 CreatePerspectiveFieldOfView(float fovRadians, float aspectRatio, float nearPlane, float farPlane)
{
float yScale = 1f / Math.Tan(fieldOfViewInRadians / 2f);
float yScale = 1f / Math.Tan(fovRadians / 2f);
float xScale = yScale / aspectRatio;
float zRange = farPlane - nearPlane;
return new Matrix4x4(
return new(
xScale, 0f, 0f, 0f,
0f, yScale, 0f, 0f,
0f, 0f, farPlane / (farPlane - nearPlane), 1f,
0f, 0f, -nearPlane * farPlane / (farPlane - nearPlane), 0f
0f, 0f, -(farPlane + nearPlane) / zRange, -1f,
0f, 0f, -(2f * nearPlane * farPlane) / zRange, 0f
);
}