Skip to content

Commit

Permalink
FINALLY deleted my stupid position/normal/scale types that were less …
Browse files Browse the repository at this point in the history
…convenient than just using Vector2/3/4.
  • Loading branch information
MeltyPlayer committed Jun 27, 2024
1 parent fb5f2d4 commit 3d134a9
Show file tree
Hide file tree
Showing 55 changed files with 376 additions and 667 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Numerics;

using fin.math.rotations;
using fin.model;
Expand All @@ -10,7 +11,7 @@ namespace fin.math.matrix.four {
public class FinMatrix4x4UtilTests {
[Test]
public void TestTranslation() {
var expectedTranslation = new Position(2, 3, 4);
var expectedTranslation = new Vector3(2, 3, 4);

var matrix = FinMatrix4x4Util.FromTranslation(
expectedTranslation);
Expand All @@ -37,7 +38,7 @@ public class FinMatrix4x4UtilTests {

[Test]
public void TestScale() {
var expectedScale = new Scale(3, 4, 5);
var expectedScale = new Vector3(3, 4, 5);

var matrix = FinMatrix4x4Util.FromScale(
expectedScale);
Expand All @@ -50,9 +51,9 @@ public class FinMatrix4x4UtilTests {

[Test]
public void TestTrs() {
var expectedTranslation = new Position(2, 3, 4);
var expectedTranslation = new Vector3(2, 3, 4);
var expectedRotation = QuaternionUtil.CreateZyx(1.2f, 2.3f, 3.4f);
var expectedScale = new Scale(3, 4, 5);
var expectedScale = new Vector3(3, 4, 5);

var trs = FinMatrix4x4Util.FromTrs(
expectedTranslation,
Expand Down Expand Up @@ -97,7 +98,7 @@ public class FinMatrix4x4UtilTests {
expectedMatrix.Decompose(out var translation,
out var rotation,
out var scale);
Assert.AreEqual(new Position(-189.294998f, -2.000000f, -265.059998f),
Assert.AreEqual(new Vector3(-189.294998f, -2.000000f, -265.059998f),
translation);

var actualMatrix = FinMatrix4x4Util.FromTrs(translation, rotation, scale);
Expand Down
20 changes: 11 additions & 9 deletions FinModelUtility/Fin/Fin.Ui/src/rendering/gl/GlBufferManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using fin.data;
using System.Numerics;

using fin.data;
using fin.math;
using fin.model;
using fin.model.accessor;
Expand Down Expand Up @@ -55,9 +57,9 @@ private class VertexArrayObject : IDisposable {
private const int COLOR_VERTEX_ATTRIB_INDEX =
UV_VERTEX_ATTRIB_INDEX + MaterialConstants.MAX_UVS;

private readonly Position[] positionData_;
private readonly Normal[] normalData_;
private readonly Tangent[] tangentData_;
private readonly Vector3[] positionData_;
private readonly Vector3[] normalData_;
private readonly Vector4[] tangentData_;
private readonly int[] boneIdsData_;
private readonly float[] boneWeightsData_;

Expand All @@ -69,9 +71,9 @@ private class VertexArrayObject : IDisposable {
this.vertexAccessor_ =
ConsistentVertexAccessor.GetAccessorForModel(model);

this.positionData_ = new Position[this.vertices_.Count];
this.normalData_ = new Normal[this.vertices_.Count];
this.tangentData_ = new Tangent[this.vertices_.Count];
this.positionData_ = new Vector3[this.vertices_.Count];
this.normalData_ = new Vector3[this.vertices_.Count];
this.tangentData_ = new Vector4[this.vertices_.Count];
this.boneIdsData_ = new int[4 * this.vertices_.Count];
this.boneWeightsData_ = new float[4 * this.vertices_.Count];

Expand Down Expand Up @@ -166,8 +168,8 @@ private class VertexArrayObject : IDisposable {
if (uv != null) {
var uvOffset = UV_SIZE_ * i;
var uvData = this.uvData_[u];
uvData[uvOffset + 0] = uv?.U ?? 0;
uvData[uvOffset + 1] = uv?.V ?? 0;
uvData[uvOffset + 0] = uv?.X ?? 0;
uvData[uvOffset + 1] = uv?.Y ?? 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ public class CachedTextureUniformData {

var secondsSinceStart = (float) FrameTime.ElapsedTimeSinceApplicationOpened.TotalSeconds;

Position? offset = null;
Vector3? offset = null;
if (textureOffset != null || scrollingTexture != null) {
offset =
new Position((textureOffset?.X ?? 0) +
secondsSinceStart *
(scrollingTexture?.ScrollSpeedX ?? 0),
(textureOffset?.Y ?? 0) +
secondsSinceStart *
(scrollingTexture?.ScrollSpeedY ?? 0),
textureOffset?.Z ?? 0);
new Vector3((textureOffset?.X ?? 0) +
secondsSinceStart *
(scrollingTexture?.ScrollSpeedX ?? 0),
(textureOffset?.Y ?? 0) +
secondsSinceStart *
(scrollingTexture?.ScrollSpeedY ?? 0),
textureOffset?.Z ?? 0);
}

Quaternion? rotation = null;
Expand All @@ -188,7 +188,7 @@ public class CachedTextureUniformData {
textureRotationRadians.Z);
}

Scale? scale = null;
Vector3? scale = null;
if (textureScale != null) {
scale = new(textureScale.X, textureScale.Y, textureScale.Z);
}
Expand Down
44 changes: 22 additions & 22 deletions FinModelUtility/Fin/Fin/src/math/BoneTransformManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ public enum BoneWeightTransformType {
public interface IVertexProjector {
void ProjectVertexPosition(
IReadOnlyVertex vertex,
out Position outPosition);
out Vector3 outPosition);

void ProjectVertexPositionNormal(
IReadOnlyNormalVertex vertex,
out Position outPosition,
out Normal outNormal);
out Vector3 outPosition,
out Vector3 outNormal);

void ProjectVertexPositionNormalTangent(
IVertexAccessor vertex,
out Position outPosition,
out Normal outNormal,
out Tangent outTangent);
out Vector3 outPosition,
out Vector3 outNormal,
out Vector4 outTangent);


void ProjectPosition(IReadOnlyBone bone, ref Vector3 xyz);
Expand Down Expand Up @@ -117,9 +117,9 @@ private void
}
}

private readonly MagFilterInterpolationTrack<Position>
private readonly MagFilterInterpolationTrack<Vector3>
positionMagFilterInterpolationTrack_ =
new(null, Position.Lerp) {
new(null, Vector3.Lerp) {
AnimationInterpolationMagFilter
= AnimationInterpolationMagFilter.ORIGINAL_FRAME_RATE_LINEAR
};
Expand All @@ -131,9 +131,9 @@ private void
= AnimationInterpolationMagFilter.ORIGINAL_FRAME_RATE_LINEAR
};

private readonly MagFilterInterpolationTrack<Scale>
private readonly MagFilterInterpolationTrack<Vector3>
scaleMagFilterInterpolationTrack_ =
new(null, Scale.Lerp) {
new(null, Vector3.Lerp) {
AnimationInterpolationMagFilter
= AnimationInterpolationMagFilter.ORIGINAL_FRAME_RATE_LINEAR
};
Expand Down Expand Up @@ -184,9 +184,9 @@ private void

boneToWorldMatrix.CopyFrom(parentBoneToWorldMatrix);

Position? animationLocalPosition = null;
Vector3? animationLocalPosition = null;
Quaternion? animationLocalRotation = null;
Scale? animationLocalScale = null;
Vector3? animationLocalScale = null;

// The pose of the animation, if available.
IReadOnlyBoneTracks? boneTracks = null;
Expand Down Expand Up @@ -339,7 +339,7 @@ public IReadOnlyFinMatrix4x4 GetLocalToWorldMatrix(IReadOnlyBone bone)

public void ProjectVertexPosition(
IReadOnlyVertex vertex,
out Position outPosition) {
out Vector3 outPosition) {
outPosition = vertex.LocalPosition;

var finTransformMatrix = this.GetTransformMatrix(vertex);
Expand All @@ -353,8 +353,8 @@ public IReadOnlyFinMatrix4x4 GetLocalToWorldMatrix(IReadOnlyBone bone)

public void ProjectVertexPositionNormal(
IReadOnlyNormalVertex vertex,
out Position outPosition,
out Normal outNormal) {
out Vector3 outPosition,
out Vector3 outNormal) {
outPosition = vertex.LocalPosition;
outNormal = vertex.LocalNormal.GetValueOrDefault();

Expand All @@ -372,9 +372,9 @@ public IReadOnlyFinMatrix4x4 GetLocalToWorldMatrix(IReadOnlyBone bone)

public void ProjectVertexPositionNormalTangent(
IVertexAccessor vertex,
out Position outPosition,
out Normal outNormal,
out Tangent outTangent) {
out Vector3 outPosition,
out Vector3 outNormal,
out Vector4 outTangent) {
outPosition = vertex.LocalPosition;

outNormal = vertex.LocalNormal.GetValueOrDefault();
Expand Down Expand Up @@ -417,9 +417,9 @@ public static class BoneTransformManagerExtensions {
public static void ApplyTrsWithFancyBoneEffects(
this IFinMatrix4x4? matrix,
IReadOnlyBone bone,
in Position localPosition,
in Vector3 localPosition,
in Quaternion? localRotation,
in Scale? localScale) {
in Vector3? localScale) {
if (matrix == null) {
return;
}
Expand All @@ -444,9 +444,9 @@ public static class BoneTransformManagerExtensions {
matrix.CopyRotationInto(out rotationBuffer);
}

Scale scaleBuffer;
Vector3 scaleBuffer;
if (bone.IgnoreParentScale) {
scaleBuffer = new Scale(1);
scaleBuffer = new Vector3(1);
} else {
matrix.CopyScaleInto(out scaleBuffer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System.Runtime.CompilerServices;

using fin.model;
using System.Numerics;
using System.Runtime.CompilerServices;

namespace fin.math.interpolation {
public readonly struct PositionInterpolator : IInterpolator<Position> {
public readonly struct Vector3Interpolator : IInterpolator<Vector3> {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Position Interpolate(Position lhs, Position rhs, float progress)
=> Position.Lerp(lhs, rhs, progress);
public Vector3 Interpolate(Vector3 lhs, Vector3 rhs, float progress)
=> Vector3.Lerp(lhs, rhs, progress);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Position Interpolate(float fromTime,
Position p1,
float fromTangent,
float toTime,
Position p2,
float toTangent,
float time)
public Vector3 Interpolate(float fromTime,
Vector3 p1,
float fromTangent,
float toTime,
Vector3 p2,
float toTangent,
float time)
=> new(
HermiteInterpolationUtil.InterpolateFloats(
fromTime,
Expand Down
16 changes: 7 additions & 9 deletions FinModelUtility/Fin/Fin/src/math/matrix/four/FinMatrix4x4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,8 @@ public void TransposeIntoBuffer(IFinMatrix4x4 buffer)

// Shamelessly copied from https://math.stackexchange.com/a/1463487
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTranslationInto(out Position dst) {
dst = default;
Unsafe.As<Position, Vector3>(ref dst) = this.impl_.Translation;
}
public void CopyTranslationInto(out Vector3 dst)
=> dst = this.impl_.Translation;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyRotationInto(out Quaternion dst) {
Expand All @@ -248,24 +246,24 @@ public void TransposeIntoBuffer(IFinMatrix4x4 buffer)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyScaleInto(out Scale dst)
public void CopyScaleInto(out Vector3 dst)
=> this.Decompose(out _, out _, out dst);


public const bool STRICT_DECOMPOSITION = true;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Decompose(out Position translation,
public void Decompose(out Vector3 translation,
out Quaternion rotation,
out Scale scale) {
out Vector3 scale) {
translation = default;
scale = default;
Asserts.True(
SystemMatrix.Decompose(
impl_,
out Unsafe.As<Scale, Vector3>(ref scale),
out scale,
out rotation,
out Unsafe.As<Position, Vector3>(ref translation)) ||
out translation) ||
!STRICT_DECOMPOSITION,
"Failed to decompose matrix!");
}
Expand Down
16 changes: 8 additions & 8 deletions FinModelUtility/Fin/Fin/src/math/matrix/four/FinMatrix4x4Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static IFinMatrix4x4 FromIdentity()


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IFinMatrix4x4 FromTranslation(Position translation)
public static IFinMatrix4x4 FromTranslation(Vector3 translation)
=> FinMatrix4x4Util.FromTranslation(
translation.X,
translation.Y,
Expand All @@ -36,7 +36,7 @@ public static IFinMatrix4x4 FromRotation(Quaternion rotation)


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IFinMatrix4x4 FromScale(Scale scale)
public static IFinMatrix4x4 FromScale(Vector3 scale)
=> FromScale(scale.X, scale.Y, scale.Z);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -53,26 +53,26 @@ public static IFinMatrix4x4 FromScale(float scale)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IFinMatrix4x4 FromTrs(
Position? translation,
Vector3? translation,
IRotation? rotation,
Scale? scale)
Vector3? scale)
=> FinMatrix4x4Util.FromTrs(
translation,
rotation != null ? QuaternionUtil.Create(rotation) : null,
scale);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IFinMatrix4x4 FromTrs(
Position? translation,
Vector3? translation,
Quaternion? rotation,
Scale? scale)
Vector3? scale)
=> FromTrs(translation, rotation, scale, new FinMatrix4x4());

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IFinMatrix4x4 FromTrs(
Position? translation,
Vector3? translation,
Quaternion? rotation,
Scale? scale,
Vector3? scale,
IFinMatrix4x4 dst) {
dst.CopyFrom(SystemMatrix4x4Util.FromTrs(translation, rotation, scale));
return dst;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Numerics;

using fin.model;

namespace fin.math.matrix.four {
public interface IFinMatrix4x4
: IFinMatrix<IFinMatrix4x4, IReadOnlyFinMatrix4x4, Matrix4x4>,
Expand All @@ -14,12 +12,12 @@ public interface IReadOnlyFinMatrix4x4
IFinMatrix4x4 CloneAndTranspose();
void TransposeIntoBuffer(IFinMatrix4x4 buffer);

void CopyTranslationInto(out Position dst);
void CopyTranslationInto(out Vector3 dst);
void CopyRotationInto(out Quaternion dst);
void CopyScaleInto(out Scale dst);
void CopyScaleInto(out Vector3 dst);

void Decompose(out Position translation,
void Decompose(out Vector3 translation,
out Quaternion rotation,
out Scale scale);
out Vector3 scale);
}
}
Loading

0 comments on commit 3d134a9

Please sign in to comment.