Skip to content

Commit

Permalink
Improved accuracy of Dat animations slightly by copying HSDRaw's inco…
Browse files Browse the repository at this point in the history
…ming/outgoing value logic.
  • Loading branch information
MeltyPlayer committed Nov 5, 2023
1 parent 74dc012 commit 41e810c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 31 deletions.
30 changes: 18 additions & 12 deletions FinModelUtility/Formats/Dat/Dat/src/api/DatBoneTracksHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ public static void AddDatKeyframesToBoneTracks(
case JointTrackType.HSD_A_J_TRAZ: {
var axis = jointTrackType - JointTrackType.HSD_A_J_TRAX;
foreach (var keyframe in datKeyframes.Keyframes) {
var (frame, value, tangent) = keyframe;
var (frame, incomingValue, outgoingValue, incomingTangent,
outgoingTangent) = keyframe;
positionTrack.Set(frame,
axis,
value,
tangent,
tangent);
incomingValue,
outgoingValue,
incomingTangent,
outgoingTangent);
}

break;
Expand All @@ -34,12 +36,14 @@ public static void AddDatKeyframesToBoneTracks(
case JointTrackType.HSD_A_J_ROTZ: {
var axis = jointTrackType - JointTrackType.HSD_A_J_ROTX;
foreach (var keyframe in datKeyframes.Keyframes) {
var (frame, value, tangent) = keyframe;
var (frame, incomingValue, outgoingValue, incomingTangent,
outgoingTangent) = keyframe;
rotationTrack.Set(frame,
axis,
value,
tangent,
tangent);
incomingValue,
outgoingValue,
incomingTangent,
outgoingTangent);
}

break;
Expand All @@ -49,12 +53,14 @@ public static void AddDatKeyframesToBoneTracks(
case JointTrackType.HSD_A_J_SCAZ: {
var axis = jointTrackType - JointTrackType.HSD_A_J_SCAX;
foreach (var keyframe in datKeyframes.Keyframes) {
var (frame, value, tangent) = keyframe;
var (frame, incomingValue, outgoingValue, incomingTangent,
outgoingTangent) = keyframe;
scaleTrack.Set(frame,
axis,
value,
tangent,
tangent);
incomingValue,
outgoingValue,
incomingTangent,
outgoingTangent);
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public static class DatKeyframesUtil {
public static void ReadKeyframes(
IBinaryReader br,
IDatKeyframes datKeyframes,
LinkedList<(int frame, float value, float? tangent)> keyframes) {
LinkedList<(int frame, float incomingValue, float outgoingValue, float?
incomingTangent, float? outgoingTangent)> keyframes) {
if (datKeyframes.JointTrackType
is (< JointTrackType.HSD_A_J_ROTX or > JointTrackType.HSD_A_J_ROTZ)
and (< JointTrackType.HSD_A_J_TRAX
Expand Down Expand Up @@ -42,56 +43,81 @@ public static void ReadKeyframes(
break;
}

var previousInterpolationType = GxInterpolationType.Constant;
GxInterpolationType? previousInterpolationType = null;
float previousValue = 0;
float? previousTangent = null;

float value = 0;
float tangent = 0;
int time = 0;
float? tangent = null;

int frameLength = 0;

for (int i = 0; i < numOfKey; i++) {
switch (interpolation) {
case GxInterpolationType.Constant:
value = ParseFloat_(sbr, valueFormat, valueScale);
if (previousInterpolationType != GxInterpolationType.Slp) {
tangent = 0;
tangent = null;
}
time = ReadPacked_(sbr);

frameLength = ReadPacked_(sbr);
break;
case GxInterpolationType.Linear:
value = ParseFloat_(sbr, valueFormat, valueScale);
if (previousInterpolationType != GxInterpolationType.Slp) {
tangent = 0;
tangent = null;
}
time = ReadPacked_(sbr);

frameLength = ReadPacked_(sbr);
break;
case GxInterpolationType.Spl0:
value = ParseFloat_(sbr, valueFormat, valueScale);
tangent = 0;
time = ReadPacked_(sbr);
frameLength = ReadPacked_(sbr);
break;
case GxInterpolationType.Spl:
value = ParseFloat_(sbr, valueFormat, valueScale);
tangent = ParseFloat_(sbr, tangentFormat, tangentScale);
time = ReadPacked_(sbr);
frameLength = ReadPacked_(sbr);
break;
case GxInterpolationType.Slp:
tangent = ParseFloat_(sbr, tangentFormat, tangentScale);
time = 0;
frame += frameLength;
frameLength = 0;
break;
case GxInterpolationType.Key:
value = ParseFloat_(sbr, valueFormat, valueScale);
time = 0;
frame += frameLength;
frameLength = 0;
break;
default:
throw new Exception("Unknown Interpolation Type " +
interpolation.ToString("X"));
}


float incomingValue = value;
float outgoingValue = value;
float? incomingTangent = tangent;
float? outgoingTangent = tangent;

// For frames that come after constant/key frames, the incoming value comes from the constant/key frame
if (previousInterpolationType == GxInterpolationType.Constant ||
previousInterpolationType == GxInterpolationType.Key) {
incomingValue = previousValue;
incomingTangent = previousTangent;
}

if (frame >= 0) {
keyframes.AddLast((frame, value, tangent));
keyframes.AddLast((frame, incomingValue, outgoingValue,
incomingTangent, outgoingTangent));
}
frame += time;

frame += frameLength;

previousInterpolationType = interpolation;
previousValue = value;
previousTangent = tangent;
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public partial class FObj : IDatLinkedListNode<FObj>,
public FObj? NextSibling { get; set; }

[Ignore]
public LinkedList<(int frame, float value, float? tangent)> Keyframes {
public LinkedList<(int frame, float incomingValue, float outgoingValue, float? incomingTangent, float? outgoingTangent)> Keyframes {
get;
} = new();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public partial class FigaTreeTrack : IDatKeyframes, IBinaryDeserializable {
public byte TexTrackType => this.TrackType;

[Ignore]
public LinkedList<(int frame, float value, float? tangent)> Keyframes {
get;
} = new();
public LinkedList<(int frame, float incomingValue, float outgoingValue,
float? incomingTangent, float? outgoingTangent)> Keyframes { get; } =
new();


[ReadLogic]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public interface IDatKeyframes {
byte ValueFlag { get; }
byte TangentFlag { get; }

LinkedList<(int frame, float value, float? tangent)> Keyframes { get; }
LinkedList<(int frame, float incomingValue, float outgoingValue, float? incomingTangent, float? outgoingTangent)> Keyframes { get; }
}
}

0 comments on commit 41e810c

Please sign in to comment.