Skip to content

Commit

Permalink
play anim sequences consecutively
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Feb 8, 2023
1 parent 70d4791 commit 1ca18c3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
6 changes: 3 additions & 3 deletions FModel/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ await _threadWorkerView.Begin(cancellationToken =>
await _threadWorkerView.Begin(cancellationToken =>
_applicationView.CUE4Parse.Extract(cancellationToken,
"MoonMan/Content/DeliverUsTheMoon/Characters/Astronaut/AM_ControllingASE.uasset"));
// await _threadWorkerView.Begin(cancellationToken =>
// _applicationView.CUE4Parse.Extract(cancellationToken,
// "Hk_project/Content/Animation/CAT/Jump/CAT_JUMP_L050_H-050.uasset"));
await _threadWorkerView.Begin(cancellationToken =>
_applicationView.CUE4Parse.Extract(cancellationToken,
"MoonMan/Content/DeliverUsTheMoon/Characters/Astronaut/AM_OxygenHub_Enter.uasset"));
#endif
}

Expand Down
26 changes: 17 additions & 9 deletions FModel/Views/Snooper/Models/Animations/Animation.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
using System;
using System.Numerics;
using CUE4Parse_Conversion.Animations;
using CUE4Parse.Utils;
using ImGuiNET;

namespace FModel.Views.Snooper.Models.Animations;

public class Animation : IDisposable
{
public float ElapsedTime;

public int CurrentSequence;
public readonly Sequence[] Sequences;
public int SequencesCount => Sequences.Length;

public Animation()
{
ElapsedTime = 0;
Sequences = Array.Empty<Sequence>();
}

Expand All @@ -31,8 +27,7 @@ public Animation(Skeleton skeleton, CAnimSet anim, bool rotationOnly) : this()

public void Update(float deltaSeconds)
{
ElapsedTime += deltaSeconds / Sequences[CurrentSequence].TimePerFrame;
Sequences[CurrentSequence].Frame = ElapsedTime.FloorToInt() % Sequences[CurrentSequence].MaxFrame;
Sequences[CurrentSequence].Update(deltaSeconds);
}

public Matrix4x4 InterpolateBoneTransform(int trackIndex)
Expand All @@ -41,11 +36,24 @@ public Matrix4x4 InterpolateBoneTransform(int trackIndex)
return Sequences[CurrentSequence].BonesTransform[trackIndex][Sequences[CurrentSequence].Frame].Matrix;
}

public void CheckForNextSequence()
{
if (Sequences[CurrentSequence].ElapsedTime > Sequences[CurrentSequence].EndPos)
{
Sequences[CurrentSequence].ElapsedTime = 0;
Sequences[CurrentSequence].Frame = 0;
CurrentSequence++;
}

if (CurrentSequence >= SequencesCount)
{
CurrentSequence = 0;
}
}

public void ImGuiTimeline()
{
ImGui.BeginDisabled(SequencesCount < 2);
ImGui.DragInt("Sequence", ref CurrentSequence, 1, 0, Sequences.Length - 1, Sequences[CurrentSequence].Name);
ImGui.EndDisabled();
ImGui.Text($"{Sequences[CurrentSequence].Name} > {(CurrentSequence < SequencesCount - 1 ? Sequences[CurrentSequence + 1].Name : Sequences[0].Name)}");
ImGui.Text($"Frame: {Sequences[CurrentSequence].Frame}/{Sequences[CurrentSequence].MaxFrame}");
ImGui.Text($"FPS: {Sequences[CurrentSequence].FramesPerSecond}");
}
Expand Down
24 changes: 21 additions & 3 deletions FModel/Views/Snooper/Models/Animations/Sequence.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
using System;
using CUE4Parse_Conversion.Animations;
using CUE4Parse.Utils;

namespace FModel.Views.Snooper.Models.Animations;

public class Sequence : IDisposable
{
public int Frame;
public float ElapsedTime;
public readonly string Name;
public readonly int MaxFrame;
public readonly float FramesPerSecond;
public readonly float StartPos;
public readonly float AnimStartTime;
public readonly float AnimEndTime;
public readonly int LoopingCount;

public float TimePerFrame => 1.0f / FramesPerSecond;
public float EndPos => AnimEndTime / TimePerFrame;

public readonly Transform[][] BonesTransform;

public Sequence(CAnimSequence sequence, Skeleton skeleton, bool rotationOnly)
{
Frame = 0;
ElapsedTime = 0.0f;
Name = sequence.Name;
MaxFrame = sequence.NumFrames;
MaxFrame = sequence.NumFrames - 1;
FramesPerSecond = sequence.Rate;
StartPos = sequence.StartPos;
AnimStartTime = sequence.AnimStartTime;
AnimEndTime = sequence.AnimEndTime;
LoopingCount = sequence.LoopingCount;

BonesTransform = new Transform[skeleton.BonesTransformByIndex.Count][];
for (int trackIndex = 0; trackIndex < skeleton.UnrealSkeleton.ReferenceSkeleton.FinalRefBoneInfo.Length; trackIndex++)
Expand All @@ -30,14 +42,14 @@ public Sequence(CAnimSequence sequence, Skeleton skeleton, bool rotationOnly)

var originalTransform = skeleton.BonesTransformByIndex[boneIndices.Index];

BonesTransform[boneIndices.Index] = new Transform[MaxFrame];
BonesTransform[boneIndices.Index] = new Transform[sequence.NumFrames];
for (int frame = 0; frame < BonesTransform[boneIndices.Index].Length; frame++)
{
var boneOrientation = originalTransform.Rotation;
var bonePosition = originalTransform.Position;
var boneScale = originalTransform.Scale;

sequence.Tracks[trackIndex].GetBonePosition(frame, MaxFrame, false, ref bonePosition, ref boneOrientation);
sequence.Tracks[trackIndex].GetBonePosition(frame, sequence.NumFrames, false, ref bonePosition, ref boneOrientation);
if (frame < sequence.Tracks[trackIndex].KeyScale.Length)
boneScale = sequence.Tracks[trackIndex].KeyScale[frame];

Expand Down Expand Up @@ -69,6 +81,12 @@ public Sequence(CAnimSequence sequence, Skeleton skeleton, bool rotationOnly)
}
}

public void Update(float deltaSeconds)
{
ElapsedTime += deltaSeconds / TimePerFrame;
Frame = Math.Min(ElapsedTime.FloorToInt(), MaxFrame);
}

public void Dispose()
{
throw new NotImplementedException();
Expand Down
1 change: 1 addition & 0 deletions FModel/Views/Snooper/Models/Animations/Skeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void SetUniform(Shader shader, float deltaSeconds = 0f, bool update = fal

shader.SetUniform($"uFinalBonesMatrix[{boneIndex}]", invertMatrix * Anim.InterpolateBoneTransform(boneIndex));
}
if (update) Anim.CheckForNextSequence();
}
}

Expand Down

0 comments on commit 1ca18c3

Please sign in to comment.