Skip to content

Commit

Permalink
Smoothen skeletal animation with interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7c13 committed Jul 29, 2023
1 parent 6301668 commit 836463c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Assets/Scripts/Pal3/Renderer/SkeletalModelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,21 @@ private void UpdateBone(Bone bone, uint tick)
if (track != null)
{
int currentFrameIndex = Utility.GetFloorIndex(bone.FrameTicks, tick);
uint currentFrameTick = bone.FrameTicks[currentFrameIndex];
var nextFrameIndex = currentFrameIndex < bone.FrameTicks.Length - 1 ? currentFrameIndex + 1 : 0;
uint nextFrameTick = nextFrameIndex == 0 ? _movFile.Duration : bone.FrameTicks[nextFrameIndex];

// TODO: Interpolate between frames
Vector3 localPosition = track.Value.KeyFrames[currentFrameIndex].Translation;
Quaternion localRotation = track.Value.KeyFrames[currentFrameIndex].Rotation;
var influence = (float)(tick - currentFrameTick) / (nextFrameTick - currentFrameTick);

Vector3 localPosition = Vector3.Lerp(
track.Value.KeyFrames[currentFrameIndex].Translation,
track.Value.KeyFrames[nextFrameIndex].Translation,
influence);

Quaternion localRotation = Quaternion.Slerp(
track.Value.KeyFrames[currentFrameIndex].Rotation,
track.Value.KeyFrames[nextFrameIndex].Rotation,
influence);

boneGo.transform.localPosition = localPosition;
boneGo.transform.localRotation = localRotation;
Expand Down

0 comments on commit 836463c

Please sign in to comment.