Skip to content

Commit

Permalink
Created an "ILerpable" interface to clean up some code for Positions/…
Browse files Browse the repository at this point in the history
…Scales.
  • Loading branch information
MeltyPlayer committed Jan 14, 2024
1 parent 8907d35 commit 90cc95a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 30 deletions.
16 changes: 3 additions & 13 deletions FinModelUtility/Fin/Fin/src/math/BoneTransformManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,17 @@ public void InitModelVertices(IModel model, bool forcePreproject = false) {
}

private readonly MagFilterInterpolationTrack<Position> positionMagFilterInterpolationTrack_ =
new(
null,
(lhs, rhs, progress) => new Position(float.Lerp(lhs.X, rhs.X, progress),
float.Lerp(lhs.Y, rhs.Y, progress),
float.Lerp(lhs.Z, rhs.Z, progress))) {
new(null, Position.Lerp) {
AnimationInterpolationMagFilter = AnimationInterpolationMagFilter.ORIGINAL_FRAME_RATE_LINEAR
};

private readonly MagFilterInterpolationTrack<Quaternion> rotationMagFilterInterpolationTrack_ =
new(
null,
Quaternion.Lerp) {
new(null, Quaternion.Slerp) {
AnimationInterpolationMagFilter = AnimationInterpolationMagFilter.ORIGINAL_FRAME_RATE_LINEAR
};

private readonly MagFilterInterpolationTrack<Scale> scaleMagFilterInterpolationTrack_ =
new(
null,
(lhs, rhs, progress) => new Scale(float.Lerp(lhs.X, rhs.X, progress),
float.Lerp(lhs.Y, rhs.Y, progress),
float.Lerp(lhs.Z, rhs.Z, progress))) {
new(null, Scale.Lerp) {
AnimationInterpolationMagFilter = AnimationInterpolationMagFilter.ORIGINAL_FRAME_RATE_LINEAR
};

Expand Down
5 changes: 5 additions & 0 deletions FinModelUtility/Fin/Fin/src/math/interpolation/ILerpable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace fin.math.interpolation {
public interface ILerpable<T> where T : ILerpable<T> {
static abstract T Lerp(T lhs, T rhs, float progress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
namespace fin.math.interpolation {
public readonly struct PositionInterpolator : IInterpolator<Position> {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Position Interpolate(
Position p1,
Position p2,
float progress)
=> new(p1.X * (1 - progress) + p2.X * progress,
p1.Y * (1 - progress) + p2.Y * progress,
p1.Z * (1 - progress) + p2.Z * progress);
public Position Interpolate(Position lhs, Position rhs, float progress)
=> Position.Lerp(lhs, rhs, progress);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Position Interpolate(float fromTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace fin.math.interpolation {
public float Interpolate(float fromValue, float toValue, float progress) {
toValue = fromValue +
RadiansUtil.CalculateRadiansTowards(fromValue, toValue);

return (1 - progress) * fromValue + progress * toValue;
return float.Lerp(fromValue, toValue, progress);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
19 changes: 16 additions & 3 deletions FinModelUtility/Fin/Fin/src/model/OrientationInterfaces.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Numerics;

using fin.math.interpolation;

namespace fin.model {
public readonly struct Position {
public Position() : this(0, 0, 0) { }
public readonly struct Position : ILerpable<Position> {
public Position() : this(0, 0, 0) {
}

public Position(float x, float y, float z) {
X = x;
Expand All @@ -29,9 +32,14 @@ public override bool Equals(object? other) {

return false;
}

public static Position Lerp(Position lhs, Position rhs, float progress)
=> new(float.Lerp(lhs.X, rhs.X, progress),
float.Lerp(lhs.Y, rhs.Y, progress),
float.Lerp(lhs.Z, rhs.Z, progress));
}

public readonly struct Scale {
public readonly struct Scale : ILerpable<Scale> {
public Scale() : this(0, 0, 0) { }

public Scale(float scale) : this(scale, scale, scale) { }
Expand Down Expand Up @@ -59,6 +67,11 @@ public override bool Equals(object? other) {

return false;
}

public static Scale Lerp(Scale lhs, Scale rhs, float progress)
=> new(float.Lerp(lhs.X, rhs.X, progress),
float.Lerp(lhs.Y, rhs.Y, progress),
float.Lerp(lhs.Z, rhs.Z, progress));
}

public readonly struct Normal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ public bool TryGetInterpolatedFrame(
Span<bool> areAxesStatic = stackalloc bool[3];
AreAxesStatic_(fromsAndTos, areAxesStatic);

if (this.bone_.Name == "base") {
;
}

if (!CanInterpolateWithQuaternions_(
fromsAndTos,
areAxesStatic)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public IModel ImportModel(CmbModelFileBundle modelFileBundle) {
var finAnimation = finModel.AnimationManager.AddAnimation();
finAnimation.Name = csabFile.NameWithoutExtension;

finAnimation.FrameCount = (int) csab.Duration;
finAnimation.FrameCount = 1 + (int) csab.Duration;
finAnimation.FrameRate = fps;

foreach (var (boneIndex, anod) in csab.BoneIndexToAnimationNode) {
Expand Down

0 comments on commit 90cc95a

Please sign in to comment.