Skip to content

Commit

Permalink
multiple anims on same model
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Aug 9, 2023
1 parent 946d38c commit 223dd8f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 54 deletions.
6 changes: 3 additions & 3 deletions FModel/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ await Task.WhenAll(
#if DEBUG
await _threadWorkerView.Begin(cancellationToken =>
_applicationView.CUE4Parse.Extract(cancellationToken,
"Hk_project/Content/Character/Cat/Mesh/SKM_Cat.uasset"));
"ShooterGame/Content/Characters/Guide/S0/3P/Models/TP_Guide_S0_Skelmesh.uasset"));
await _threadWorkerView.Begin(cancellationToken =>
_applicationView.CUE4Parse.Extract(cancellationToken,
"Hk_project/Content/Animation/CAT/Idle/CAT_SitBalledStrechingToStand_Idle.uasset"));
"ShooterGame/Content/Characters/Guide/S0/Ability_Q/3P/Anims/TP_Guide_S0_Q_WolfTotem_Cast_Outro_Montage.uasset"));
#endif
}

Expand Down Expand Up @@ -142,7 +142,7 @@ private void OnTabItemChange(object sender, SelectionChangedEventArgs e)

private async void OnMappingsReload(object sender, ExecutedRoutedEventArgs e)
{
await _applicationView.CUE4Parse.InitMappings();
await _applicationView.CUE4Parse.InitMappings(true);
}

private void OnOpenAvalonFinder()
Expand Down
10 changes: 7 additions & 3 deletions FModel/ViewModels/CUE4ParseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ await _threadWorkerView.Begin(cancellationToken =>
});
}

public Task InitMappings()
public Task InitMappings(bool force = false)
{
if (!UserSettings.IsEndpointValid(EEndpointType.Mapping, out var endpoint))
{
Expand All @@ -377,7 +377,7 @@ public Task InitMappings()
if (!mapping.IsValid) continue;
var mappingPath = Path.Combine(mappingsFolder, mapping.FileName);
if (!File.Exists(mappingPath))
if (force || !File.Exists(mappingPath))
{
_apiEndpointView.DownloadFile(mapping.Url, mappingPath);
}
Expand Down Expand Up @@ -883,8 +883,12 @@ public void ExtractAndScroll(CancellationToken cancellationToken, string fullPat
{
var shouldDecompress = UserSettings.Default.CompressedAudioMode == ECompressedAudio.PlayDecompressed;
export.Decode(shouldDecompress, out var audioFormat, out var data);
if (data == null || string.IsNullOrEmpty(audioFormat) || export.Owner == null)
var hasAf = !string.IsNullOrEmpty(audioFormat);
if (data == null || !hasAf || export.Owner == null)
{
if (hasAf) FLogger.Append(ELog.Warning, () => FLogger.Text($"Unsupported audio format '{audioFormat}'", Constants.WHITE, true));
return false;
}

SaveAndPlaySound(Path.Combine(TabControl.SelectedTab.Directory, TabControl.SelectedTab.Header.SubstringBeforeLast('.')).Replace('\\', '/'), audioFormat, data);
return false;
Expand Down
41 changes: 9 additions & 32 deletions FModel/Views/Snooper/Animations/Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using CUE4Parse_Conversion.Animations.PSA;
using CUE4Parse.UE4.Assets.Exports;
using CUE4Parse.UE4.Objects.Core.Misc;
using CUE4Parse.Utils;
using ImGuiNET;

namespace FModel.Views.Snooper.Animations;
Expand All @@ -20,15 +19,8 @@ public class Animation : IDisposable
public readonly float StartTime; // Animation Start Time
public readonly float EndTime; // Animation End Time
public readonly float TotalElapsedTime; // Animation Max Time
public readonly string TargetSkeleton;
public readonly Dictionary<int, float> Framing;

public int CurrentSequence;
public int FrameInSequence; // Current Sequence's Frame to Display
public int NextFrameInSequence;
public float LerpAmount;

public string Label =>
$"Retarget: {TargetSkeleton}\nSequences: {CurrentSequence + 1}/{Sequences.Length}\nFrames: {FrameInSequence}/{Sequences[CurrentSequence].EndFrame}";
public bool IsActive;
public bool IsSelected;

Expand All @@ -40,23 +32,22 @@ public Animation(UObject export)
Path = _export.GetPathName();
Name = _export.Name;
Sequences = Array.Empty<Sequence>();
Framing = new Dictionary<int, float>();
AttachedModels = new List<FGuid>();
}

public Animation(UObject export, CAnimSet animSet) : this(export)
{
_animSet = animSet;
TargetSkeleton = _animSet.Skeleton.Name;

Sequences = new Sequence[_animSet.Sequences.Count];
for (int i = 0; i < Sequences.Length; i++)
{
Sequences[i] = new Sequence(_animSet.Sequences[i]);

EndTime = Sequences[i].EndTime;
TotalElapsedTime += _animSet.Sequences[i].NumFrames * Sequences[i].TimePerFrame;
}

TotalElapsedTime = animSet.TotalAnimTime;
if (Sequences.Length > 0)
StartTime = Sequences[0].StartTime;
}
Expand All @@ -70,30 +61,16 @@ public void TimeCalculation(float elapsedTime)
{
for (int i = 0; i < Sequences.Length; i++)
{
if (elapsedTime < Sequences[i].EndTime && elapsedTime >= Sequences[i].StartTime)
var sequence = Sequences[i];
if (elapsedTime < sequence.EndTime && elapsedTime >= sequence.StartTime)
{
CurrentSequence = i;
break;
Framing[i] = (elapsedTime - sequence.StartTime) / sequence.TimePerFrame;
}
else Framing.Remove(i);
}
if (elapsedTime >= TotalElapsedTime) Reset();

var lastEndTime = 0.0f;
for (int s = 0; s < CurrentSequence; s++)
lastEndTime = Sequences[s].EndTime;

var exactFrameAtThisTime = (elapsedTime - lastEndTime) / Sequences[CurrentSequence].TimePerFrame;
FrameInSequence = Math.Min(exactFrameAtThisTime.FloorToInt(), Sequences[CurrentSequence].EndFrame);
NextFrameInSequence = Math.Min(FrameInSequence + 1, Sequences[CurrentSequence].EndFrame);
LerpAmount = Math.Clamp(exactFrameAtThisTime - FrameInSequence, 0, 1);
}

private void Reset()
{
FrameInSequence = 0;
NextFrameInSequence = 0;
LerpAmount = 0.0f;
CurrentSequence = 0;
if (elapsedTime >= TotalElapsedTime)
Framing.Clear();
}

public void Dispose()
Expand Down
11 changes: 8 additions & 3 deletions FModel/Views/Snooper/Animations/Bone.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace FModel.Views.Snooper.Animations;
using System.Collections.Generic;

namespace FModel.Views.Snooper.Animations;

public class Bone
{
Expand All @@ -8,18 +10,21 @@ public class Bone
public string LoweredParentName;

public int SkeletonIndex = -1;
public bool IsAnimated;
public readonly List<int> AnimatedBySequences;

public Bone(int i, int p, Transform t)
{
Index = i;
ParentIndex = p;
Rest = t;

AnimatedBySequences = new List<int>();
}

public bool IsRoot => Index == 0 && ParentIndex == -1 && string.IsNullOrEmpty(LoweredParentName);
public bool IsMapped => SkeletonIndex > -1;
public bool IsAnimated => AnimatedBySequences.Count > 0;
public bool IsNative => Index == SkeletonIndex;

public override string ToString() => $"Mesh Ref '{Index}' is Skel Ref '{SkeletonIndex}' ({IsAnimated})";
public override string ToString() => $"Mesh Ref '{Index}' is Skel Ref '{SkeletonIndex}'";
}
54 changes: 45 additions & 9 deletions FModel/Views/Snooper/Animations/Skeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CUE4Parse_Conversion.Animations.PSA;
using CUE4Parse.UE4.Assets.Exports.Animation;
using CUE4Parse.UE4.Objects.Core.Math;
using CUE4Parse.Utils;
using FModel.Views.Snooper.Buffers;
using OpenTK.Graphics.OpenGL4;
using Serilog;
Expand Down Expand Up @@ -74,7 +75,6 @@ public void Animate(CAnimSet anim, bool rotationOnly)
var skeletonBoneIndex = bone.SkeletonIndex;
if (sequence.OriginalSequence.FindTrackForBoneIndex(skeletonBoneIndex) < 0)
{
bone.IsAnimated |= false;
for (int frame = 0; frame < _animatedBonesTransform[s][bone.Index].Length; frame++)
{
_animatedBonesTransform[s][bone.Index][frame] = new Transform
Expand All @@ -86,7 +86,7 @@ public void Animate(CAnimSet anim, bool rotationOnly)
}
else
{
bone.IsAnimated |= true;
bone.AnimatedBySequences.Add(s);
for (int frame = 0; frame < _animatedBonesTransform[s][bone.Index].Length; frame++)
{
var boneOrientation = bone.Rest.Rotation;
Expand Down Expand Up @@ -173,7 +173,6 @@ private void MapSkeleton(CAnimSet anim)
continue;

bone.SkeletonIndex = boneIndex;
bone.IsAnimated = false;
}

#if DEBUG
Expand All @@ -192,7 +191,7 @@ public void ResetAnimatedData(bool full = false)
foreach (var bone in BonesByLoweredName.Values)
{
bone.SkeletonIndex = -1;
bone.IsAnimated = false;
bone.AnimatedBySequences.Clear();
}

if (!full) return;
Expand All @@ -208,22 +207,59 @@ public void Setup()
_ssbo.UpdateRange(BoneCount, Matrix4x4.Identity);
}

public void UpdateAnimationMatrices(int currentSequence, int frameInSequence, int nextFrameInSequence, float lerp)
public void UpdateAnimationMatrices(Animation animation)
{
if (!IsAnimated) return;

_ssbo.Bind();
for (int boneIndex = 0; boneIndex < BoneCount; boneIndex++)

foreach (var bone in BonesByLoweredName.Values)
{
var (s, f) = GetBoneFrameData(bone, animation);
var frameInSequence = Math.Min(f.FloorToInt(), animation.Sequences[s].EndFrame);
var nextFrameInSequence = Math.Min(frameInSequence + 1, animation.Sequences[s].EndFrame);
var lerpAmount = Math.Clamp(f - frameInSequence, 0, 1);

var boneIndex = bone.Index;
var matrix = Matrix4x4.Lerp(
_animatedBonesTransform[currentSequence][boneIndex][frameInSequence].Matrix,
_animatedBonesTransform[currentSequence][boneIndex][nextFrameInSequence].Matrix,
lerp);
_animatedBonesTransform[s][boneIndex][frameInSequence].Matrix,
_animatedBonesTransform[s][boneIndex][nextFrameInSequence].Matrix,
lerpAmount);
_ssbo.Update(boneIndex, _invertedBonesMatrix[boneIndex] * matrix);
}

_ssbo.Unbind();
}

private (int, float) GetBoneFrameData(Bone bone, Animation animation)
{
int s = -1;
float f = 0.0f;

void Get(Bone b)
{
foreach (var i in b.AnimatedBySequences)
{
s = i;
if (animation.Framing.TryGetValue(s, out f))
break;
}
}

Get(bone);
if (s == -1)
{
var parent = BonesByLoweredName[bone.LoweredParentName];
while (!parent.IsAnimated)
{
parent = BonesByLoweredName[parent.LoweredParentName];
}
Get(parent);
}

return (s, f);
}

public Matrix4x4 GetBoneMatrix(Bone bone) => IsAnimated ? bone.Rest.Matrix * _ssbo.Get(bone.Index) : bone.Rest.Matrix;

public void Render()
Expand Down
4 changes: 1 addition & 3 deletions FModel/Views/Snooper/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,7 @@ public void Update(Snooper wnd, float deltaSeconds)
animation.TimeCalculation(Options.Tracker.ElapsedTime);
foreach (var guid in animation.AttachedModels.Where(guid => Options.Models[guid].HasSkeleton))
{
Options.Models[guid].Skeleton.UpdateAnimationMatrices(
animation.CurrentSequence, animation.FrameInSequence,
animation.NextFrameInSequence, animation.LerpAmount);
Options.Models[guid].Skeleton.UpdateAnimationMatrices(animation);
}
}

Expand Down

0 comments on commit 223dd8f

Please sign in to comment.