Skip to content

Commit

Permalink
Optimize keyframe to avoid making bloated animations
Browse files Browse the repository at this point in the history
  • Loading branch information
ammaraskar committed May 16, 2024
1 parent b10870f commit ad6009d
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions Assets/Scripts/OpenTS2/Engine/Tests/SimAnimationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ IEnumerator BakeAnimationIntoNewAnimation(string animName, ScenegraphAnimationAs
var boneToKeyframes = new Dictionary<string, PositionAndRotationKeyframes>();
foreach (var bone in _sim.Scenegraph.BoneNamesToRelativePaths.Keys)
{
// Don't bother animating the "surface" or "grip" bones, they're parented and don't actually get
// animated.
if (bone.Contains("grip") || bone.Contains("surface"))
{
continue;
}
boneToKeyframes[bone] = new PositionAndRotationKeyframes();
}

Expand All @@ -178,6 +184,11 @@ IEnumerator BakeAnimationIntoNewAnimation(string animName, ScenegraphAnimationAs
foreach (var item in _sim.Scenegraph.BoneNamesToTransform)
{
var boneName = item.Key;
// Only do bones present in the set.
if (!boneToKeyframes.ContainsKey(boneName))
{
continue;
}
var boneTransform = item.Value;
boneToKeyframes[boneName].BakeInDataFromTransform(animState.time, boneTransform);
}
Expand Down Expand Up @@ -247,14 +258,24 @@ private class PositionAndRotationKeyframes

public void BakeInDataFromTransform(float time, Transform transform)
{
PosX.Add(new Keyframe(time, transform.localPosition.x));
PosY.Add(new Keyframe(time, transform.localPosition.y));
PosZ.Add(new Keyframe(time, transform.localPosition.z));

QuatX.Add(new Keyframe(time, transform.localRotation.x));
QuatY.Add(new Keyframe(time, transform.localRotation.y));
QuatZ.Add(new Keyframe(time, transform.localRotation.z));
QuatW.Add(new Keyframe(time, transform.localRotation.w));
AddValue(PosX, time, transform.localPosition.x);
AddValue(PosY, time, transform.localPosition.y);
AddValue(PosZ, time, transform.localPosition.z);

AddValue(QuatX, time, transform.localRotation.x);
AddValue(QuatY, time, transform.localRotation.y);
AddValue(QuatZ, time, transform.localRotation.z);
AddValue(QuatW, time, transform.localRotation.w);
}

private static void AddValue(IList<Keyframe> list, float time, float newValue)
{
// Only add keyframe if different from previous value.
if (list.Count > 0 && Mathf.Approximately(list[list.Count - 1].value, newValue))
{
return;
}
list.Add(new Keyframe(time, newValue));
}
}
}
Expand Down

0 comments on commit ad6009d

Please sign in to comment.