Skip to content

Commit

Permalink
Investigated what's going on with the Bnk keyframe values to try to f…
Browse files Browse the repository at this point in the history
…igure out what might be going wrong.
  • Loading branch information
MeltyPlayer committed Nov 12, 2023
1 parent 6094e6c commit 68fd3f6
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections;
using System.Collections.Generic;

namespace fin.data.lists {
using fin.data.lists;

namespace fin.data.counters {
public class CounterArray : IFinList<int> {
private readonly List<int> impl_ = new();

Expand Down
20 changes: 20 additions & 0 deletions FinModelUtility/Fin/Fin/src/data/counters/CounterSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace fin.data.counters {
public class CounterSet<T> {
private readonly Dictionary<T, int> impl_ = new();

public int Increment(T key) {
if (this.impl_.TryGetValue(key, out var count)) {
count++;
}

return this.impl_[key] = count;
}

public void Clear() => this.impl_.Clear();
public int Count => this.impl_.Count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace fin.data.dictionaries {
public class SortedSetDictionary<TKey, TValue>
: IFinCollection<(TKey Key, SortedSet<TValue> Value)> {
private readonly NullFriendlyDictionary<TKey, SortedSet<TValue>> impl_ =
new();

public void Clear() => this.impl_.Clear();
public void ClearSet(TKey key) => this.impl_.Remove(key);

public int Count => this.impl_.Values.Select(list => list.Count).Sum();

public bool HasSet(TKey key) => this.impl_.ContainsKey(key);

public void Add(TKey key, TValue value) {
SortedSet<TValue> set;
if (!this.impl_.TryGetValue(key, out set)) {
this.impl_[key] = set = new SortedSet<TValue>();
}

set.Add(value);
}

public SortedSet<TValue> this[TKey key] => this.impl_[key];

public bool TryGetSet(TKey key, out SortedSet<TValue>? set)
=> this.impl_.TryGetValue(key, out set);

IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();

public IEnumerator<(TKey Key, SortedSet<TValue> Value)> GetEnumerator()
=> this.impl_.GetEnumerator();
}
}
54 changes: 43 additions & 11 deletions FinModelUtility/Formats/Visceral/Visceral/src/api/BnkReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections;

using fin.data.counters;
using fin.data.dictionaries;
using fin.data.queues;
using fin.io;
using fin.math.floats;
Expand All @@ -15,6 +17,16 @@ public enum MaybeBoneType {
ANIMATED = 0x16,
}

public enum AxisType : byte {
ROT_X,
ROT_Y,
ROT_Z,
UNKNOWN,
POS_X,
POS_Y,
POS_Z,
}

public enum KeyframeType : byte {
ONLY_KEYFRAME = 0x0,
KEYFRAME_AND_3_BYTES = 0x1,
Expand Down Expand Up @@ -72,6 +84,10 @@ private void ReadIntoAnimation_(IBinaryReader bnkBr,
var totalFrames = 1;

var commands = new List<ushort>();
var countsOfAxesWithKeyframeType =
new CounterSet<(AxisType, KeyframeType)>();
var valuesByAxesWithKeyframeType =
new SortedSetDictionary<(AxisType, KeyframeType), float>();

{
var rootOffset = bnkBr.ReadUInt32();
Expand Down Expand Up @@ -132,25 +148,29 @@ private void ReadIntoAnimation_(IBinaryReader bnkBr,
}

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

for (var a = 0; a < 7; ++a) {
var axisType = (AxisType) a;

void SetKeyframe(int frame, float value) {
totalFrames = Math.Max(totalFrames, frame);

switch (a) {
case 0:
case 1:
case 2:
case 3: {
rotations.Set(frame, a, value);
switch (axisType) {
case AxisType.ROT_X:
case AxisType.ROT_Y:
case AxisType.ROT_Z: {
rotations.Set(frame, axisType - AxisType.ROT_X, value);
break;
}
case 4:
case 5:
case 6: {
positions.Set(frame, a - 4, value);
case AxisType.POS_X:
case AxisType.POS_Y:
case AxisType.POS_Z: {
positions.Set(frame, axisType - AxisType.POS_X, value);
break;
}
case AxisType.UNKNOWN: {
break;
}
default: throw new Exception();
Expand All @@ -166,6 +186,9 @@ void SetKeyframe(int frame, float value) {
if (upper == standaloneCommandPrefix) {
var keyframeType = (KeyframeType) lower;

countsOfAxesWithKeyframeType.Increment(
(axisType, keyframeType));

bnkBr.Position -= 1;

var frame = 0;
Expand All @@ -175,6 +198,9 @@ void SetKeyframe(int frame, float value) {
keyframeType,
(int) standaloneCommandPrefix)) {
SetKeyframe(frame++, keyframeValue);
valuesByAxesWithKeyframeType.Add(
(axisType, keyframeType),
keyframeValue);
}
} else if (lower == 5) {
var keyframeCount = upper;
Expand All @@ -188,6 +214,9 @@ void SetKeyframe(int frame, float value) {
var keyframeType =
(KeyframeType) (lengthAndKeyframeType & 0xF);

countsOfAxesWithKeyframeType.Increment(
(axisType, keyframeType));

bnkBr.Position -= 1;

var startingKeyframe = frame;
Expand All @@ -197,6 +226,9 @@ void SetKeyframe(int frame, float value) {
keyframeType,
keyframeLength)) {
SetKeyframe(frame++, keyframeValue);
valuesByAxesWithKeyframeType.Add(
(axisType, keyframeType),
keyframeValue);
}

frame = startingKeyframe + keyframeLength;
Expand Down

0 comments on commit 68fd3f6

Please sign in to comment.