Skip to content

Commit

Permalink
Getting closer to accurately reading Bnk rotations...
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Nov 12, 2023
1 parent 68fd3f6 commit 252bffb
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions FinModelUtility/Formats/Visceral/Visceral/src/api/BnkReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using fin.data.dictionaries;
using fin.data.queues;
using fin.io;
using fin.math.floats;
using fin.model;

using schema.binary;
Expand All @@ -21,7 +20,7 @@ public enum AxisType : byte {
ROT_X,
ROT_Y,
ROT_Z,
UNKNOWN,
ROT_W,
POS_X,
POS_Y,
POS_Z,
Expand Down Expand Up @@ -148,7 +147,7 @@ private void ReadIntoAnimation_(IBinaryReader bnkBr,
}

var boneTracks = finAnimation.AddBoneTracks(bones[b]);
var rotations = boneTracks.UseEulerRadiansRotationTrack();
var rotations = boneTracks.UseQuaternionAxesRotationTrack();
var positions = boneTracks.UseSeparatePositionAxesTrack();

for (var a = 0; a < 7; ++a) {
Expand All @@ -160,7 +159,8 @@ void SetKeyframe(int frame, float value) {
switch (axisType) {
case AxisType.ROT_X:
case AxisType.ROT_Y:
case AxisType.ROT_Z: {
case AxisType.ROT_Z:
case AxisType.ROT_W: {
rotations.Set(frame, axisType - AxisType.ROT_X, value);
break;
}
Expand All @@ -170,9 +170,6 @@ void SetKeyframe(int frame, float value) {
positions.Set(frame, axisType - AxisType.POS_X, value);
break;
}
case AxisType.UNKNOWN: {
break;
}
default: throw new Exception();
}
}
Expand Down Expand Up @@ -265,7 +262,7 @@ private IEnumerable<float> ReadKeyframeValuesOfType_(
case KeyframeType.KEYFRAME_AND_6_BYTES:
case KeyframeType.KEYFRAME_AND_9_BYTES: {
// TODO: What are these???
br.Position += 3 * (int) keyframeType;
var values = br.ReadBytes(3 * (int) keyframeType);
yield return br.ReadSingle();
break;
}
Expand All @@ -282,30 +279,35 @@ private IEnumerable<float> ReadKeyframeValuesOfType_(
case KeyframeType.SHORT_GRADIENT: {
br.Position += 1;

// TOOD: Is this actually right???
var fromFraction = br.ReadUn8();
var toFraction = br.ReadUn8();
var unk0 = br.ReadByte();
var unk1 = br.ReadByte();

if (fromFraction.IsRoughly(0) &&
toFraction.IsRoughly(0)) {
fromFraction = 0;
toFraction = 1;
} else if (fromFraction.IsRoughly(toFraction)) {
;
}
br.Position -= 2;

var value = br.ReadSingle();
// TODO: What is this meant to be?
var unk = br.ReadUInt16();

var scale = br.ReadSingle();

// TODO: Is this right????
var fractions =
keyframeType == KeyframeType.BYTE_GRADIENT
? br.ReadUn8s(keyframeLength)
: br.ReadUn16s(keyframeLength);

foreach (var fraction in fractions) {
var keyframeAmount = fromFraction * (1 - fraction) +
toFraction * fraction;
var keyframe = keyframeAmount * value;
var values =
(keyframeType == KeyframeType.BYTE_GRADIENT
? br.ReadBytes(keyframeLength)
.Select(u => (ushort) u)
: br.ReadUInt16s(keyframeLength)
.Select(u => u))
.ToArray();

var keyframes =
values
.Select(value => value * scale)
.ToArray();

if (unk0 == unk1 && unk0 != 0) {
;
}

foreach (var keyframe in keyframes) {
yield return keyframe;
}

Expand Down

0 comments on commit 252bffb

Please sign in to comment.