Skip to content

Commit

Permalink
Set up a basic, unoptimized implementation for getting all rotations …
Browse files Browse the repository at this point in the history
…in a single pass.
  • Loading branch information
MeltyPlayer committed Nov 28, 2024
1 parent 1b0fa0b commit 3da7d01
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Numerics;

using fin.animation.interpolation;
using fin.animation.types;
using fin.animation.types.quaternion;
using fin.animation.types.single;
using fin.animation.types.vector3;
Expand All @@ -13,7 +12,7 @@

namespace fin.animation.keyframes;

public class GetAllFramesTests {
public class GetAllFramesMatchesInterpolatedTests {
[Test]
public void TestStairstepNonlooping() {
var impl = new StairStepKeyframes<float>(
Expand Down Expand Up @@ -104,7 +103,7 @@ public void TestSeparateVector3Nonlooping() {
[Test]
public void TestSeparateVector3Looping() {
var impl = new SeparateVector3Keyframes<Keyframe<float>>(
new SharedInterpolationConfig { AnimationLength = 10, Looping = true},
new SharedInterpolationConfig { AnimationLength = 10, Looping = true },
FloatKeyframeInterpolator.Instance,
new IndividualInterpolationConfig<float>
{ DefaultValue = Optional.Of(-1f) });
Expand Down Expand Up @@ -132,7 +131,7 @@ public void TestCombinedVector3Nonlooping() {
Vector3KeyframeInterpolator.Instance,
new IndividualInterpolationConfig<Vector3>
{ DefaultValue = Optional.Of(new Vector3(-1, -1, -1)) });

impl.SetKeyframe(2, new Vector3(1, 2, 3));
impl.SetKeyframe(4, new Vector3(4, 2, 3));
impl.SetKeyframe(6, new Vector3(5, 5, 5));
Expand All @@ -148,7 +147,7 @@ public void TestCombinedVector3Looping() {
Vector3KeyframeInterpolator.Instance,
new IndividualInterpolationConfig<Vector3>
{ DefaultValue = Optional.Of(new Vector3(-1, -1, -1)) });

impl.SetKeyframe(2, new Vector3(1, 2, 3));
impl.SetKeyframe(4, new Vector3(4, 2, 3));
impl.SetKeyframe(6, new Vector3(5, 5, 5));
Expand All @@ -158,14 +157,61 @@ public void TestCombinedVector3Looping() {
}


[Test]
public void TestSeparateEulerRadiansNonlooping() {
var impl = new SeparateEulerRadiansKeyframes<Keyframe<float>>(
new SharedInterpolationConfig { AnimationLength = 10, },
FloatKeyframeInterpolator.Instance,
new IndividualInterpolationConfig<float>
{ DefaultValue = Optional.Of(-1f) });

impl.SetKeyframe(0, 1, 0);
impl.SetKeyframe(1, 2, 1);
impl.SetKeyframe(2, 3, 2);

impl.SetKeyframe(0, 5, 3);
impl.SetKeyframe(1, 5, 1);
impl.SetKeyframe(2, 5, 2);

impl.SetKeyframe(0, 7, 4);
impl.SetKeyframe(1, 7, 2);
impl.SetKeyframe(2, 7, 3);

AssertGetAllFramesMatchesInterpolated_(impl, 10);
}

[Test]
public void TestSeparateEulerRadiansLooping() {
var impl = new SeparateEulerRadiansKeyframes<Keyframe<float>>(
new SharedInterpolationConfig { AnimationLength = 10, Looping = true },
FloatKeyframeInterpolator.Instance,
new IndividualInterpolationConfig<float>
{ DefaultValue = Optional.Of(-1f) });

impl.SetKeyframe(0, 1, 0);
impl.SetKeyframe(1, 2, 1);
impl.SetKeyframe(2, 3, 2);

impl.SetKeyframe(0, 5, 3);
impl.SetKeyframe(1, 5, 1);
impl.SetKeyframe(2, 5, 2);

impl.SetKeyframe(0, 7, 4);
impl.SetKeyframe(1, 7, 2);
impl.SetKeyframe(2, 7, 3);

AssertGetAllFramesMatchesInterpolated_(impl, 10);
}


[Test]
public void TestCombinedQuaternionNonlooping() {
var impl = new CombinedQuaternionKeyframes<Keyframe<Quaternion>>(
new SharedInterpolationConfig { AnimationLength = 10 },
QuaternionKeyframeInterpolator.Instance,
new IndividualInterpolationConfig<Quaternion>
{ DefaultValue = Optional.Of(new Quaternion(-1, -1, -1, -1)) });

impl.SetKeyframe(2, new Quaternion(1, 2, 3, 4));
impl.SetKeyframe(4, new Quaternion(4, 2, 3, 4));
impl.SetKeyframe(6, new Quaternion(5, 5, 5, 5));
Expand All @@ -181,7 +227,7 @@ public void TestCombinedQuaternionLooping() {
QuaternionKeyframeInterpolator.Instance,
new IndividualInterpolationConfig<Quaternion>
{ DefaultValue = Optional.Of(new Quaternion(-1, -1, -1, -1)) });

impl.SetKeyframe(2, new Quaternion(1, 2, 3, 4));
impl.SetKeyframe(4, new Quaternion(4, 2, 3, 4));
impl.SetKeyframe(6, new Quaternion(5, 5, 5, 5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Linq;
using System.Runtime.CompilerServices;

using fin.animation.types;
using fin.animation.types.quaternion;
using fin.animation.types.vector3;

namespace fin.animation.keyframes;
Expand Down Expand Up @@ -50,4 +50,13 @@ public static void SetKeyframe(
float frame,
float value)
=> keyframes.Axes[axis].Add(new Keyframe<float>(frame, value));


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetKeyframe(
this ISeparateEulerRadiansKeyframes<Keyframe<float>> keyframes,
int axis,
float frame,
float value)
=> keyframes.Axes[axis].Add(new Keyframe<float>(frame, value));
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ private static float GetToValueOrDefault_(
? toKeyframe.ValueIn
: defaultValue;

public void GetAllFrames(Span<Quaternion> dst)
=> throw new NotImplementedException();
public void GetAllFrames(Span<Quaternion> dst) {
// TODO: Switch this to use a more optimized approach
for (var i = 0; i < dst.Length; ++i) {
this.TryGetAtFrame(i, out var rotation);
dst[i] = rotation;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var isValid

Span<Vector3> translationsOrScales
= stackalloc Vector3[animation.FrameCount];
Span<Quaternion> rotations = stackalloc Quaternion[animation.FrameCount];

foreach (var (node, bone) in skinNodesAndBones) {
if (!animation.BoneTracks.TryGetValue(bone, out var boneTracks)) {
Expand All @@ -73,11 +74,10 @@ Span<Vector3> translationsOrScales
var rotationDefined = boneTracks.Rotations?.HasAnyData ?? false;
if (rotationDefined) {
rotationKeyframes.Clear();
boneTracks.Rotations.GetAllFrames(rotations);
for (var i = 0; i < animation.FrameCount; ++i) {
var time = i / fps;
if (boneTracks.Rotations.TryGetAtFrame(i, out var rotation)) {
rotationKeyframes[time] = rotation;
}
rotationKeyframes[time] = rotations[i];
}

gltfAnimation.CreateRotationChannel(node, rotationKeyframes);
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 3da7d01

Please sign in to comment.