Skip to content

Commit

Permalink
POC frame interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Aug 1, 2023
1 parent 993726c commit 6ed335d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
9 changes: 8 additions & 1 deletion FModel/Views/Snooper/Animations/Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class Animation : IDisposable

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}";
Expand Down Expand Up @@ -80,12 +82,17 @@ public void TimeCalculation(float elapsedTime)
for (int s = 0; s < CurrentSequence; s++)
lastEndTime = Sequences[s].EndTime;

FrameInSequence = Math.Min(((elapsedTime - lastEndTime) / Sequences[CurrentSequence].TimePerFrame).FloorToInt(), Sequences[CurrentSequence].EndFrame);
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;
}

Expand Down
25 changes: 10 additions & 15 deletions FModel/Views/Snooper/Animations/Skeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class Skeleton : IDisposable
public string Name;
public readonly Dictionary<string, Bone> BonesByLoweredName;

private int _previousAnimationSequence;
private int _previousSequenceFrame;
private Transform[][][] _animatedBonesTransform; // [sequence][bone][frame]
private readonly Matrix4x4[] _invertedBonesMatrix;
public int BoneCount => _invertedBonesMatrix.Length;
Expand Down Expand Up @@ -210,26 +208,23 @@ public void Setup()
_ssbo.UpdateRange(BoneCount, Matrix4x4.Identity);
}

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

_previousAnimationSequence = currentSequence;
if (_previousSequenceFrame == frameInSequence) return;
_previousSequenceFrame = frameInSequence;

_ssbo.Bind();
for (int boneIndex = 0; boneIndex < BoneCount; boneIndex++) // interpolate here
_ssbo.Update(boneIndex, _invertedBonesMatrix[boneIndex] * _animatedBonesTransform[_previousAnimationSequence][boneIndex][_previousSequenceFrame].Matrix);
for (int boneIndex = 0; boneIndex < BoneCount; boneIndex++)
{
var matrix = Matrix4x4.Lerp(
_animatedBonesTransform[currentSequence][boneIndex][frameInSequence].Matrix,
_animatedBonesTransform[currentSequence][boneIndex][nextFrameInSequence].Matrix,
lerp);
_ssbo.Update(boneIndex, _invertedBonesMatrix[boneIndex] * matrix);
}
_ssbo.Unbind();
}

public Matrix4x4 GetBoneMatrix(Bone bone)
{
return IsAnimated
? _animatedBonesTransform[_previousAnimationSequence][bone.Index][_previousSequenceFrame].Matrix
: bone.Rest.Matrix;
}
public Matrix4x4 GetBoneMatrix(Bone bone) => IsAnimated ? bone.Rest.Matrix * _ssbo.Get(bone.Index) : bone.Rest.Matrix;

public void Render()
{
Expand Down
11 changes: 11 additions & 0 deletions FModel/Views/Snooper/Buffers/BufferObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ public unsafe void Update(TDataType[] data)
GL.BufferSubData(_bufferTarget, IntPtr.Zero, data.Length * sizeof(TDataType), data);
}

public unsafe TDataType Get(int offset)
{
TDataType data = default;

Bind();
GL.GetBufferSubData(_bufferTarget, (IntPtr) (offset * sizeof(TDataType)), sizeof(TDataType), ref data);
Unbind();

return data;
}

public void Bind()
{
GL.BindBuffer(_bufferTarget, _handle);
Expand Down
12 changes: 10 additions & 2 deletions FModel/Views/Snooper/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,17 @@ public void Setup(Options options)
_vao.Unbind();
}

for (int section = 0; section < Sections.Length; section++)
if (options.Models.Count == 1 && Sections.All(x => !x.Show))
{
if (!Show) Show = Sections[section].Show;
Show = true;
foreach (var section in Sections)
{
section.Show = true;
}
}
else foreach (var section in Sections)
{
if (!Show) Show = section.Show;
}

IsSetup = true;
Expand Down
4 changes: 3 additions & 1 deletion FModel/Views/Snooper/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ public void Render(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);
Options.Models[guid].Skeleton.UpdateAnimationMatrices(
animation.CurrentSequence, animation.FrameInSequence,
animation.NextFrameInSequence, animation.LerpAmount);
}
}

Expand Down

0 comments on commit 6ed335d

Please sign in to comment.