Guest User

Untitled

a guest
Jul 9th, 2022
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. public static void Simplify(this AnimationCurve animationCurve, float errorThreshold, bool isRotation = false)
  2.         {
  3.             if (animationCurve.keys.Length < 3)
  4.             {
  5.                 return;
  6.             }
  7.             var cloned = new AnimationCurve();
  8.             cloned.keys = animationCurve.keys;
  9.             var removed = true;
  10.             while (removed)
  11.             {
  12.                 removed = false;
  13.                 for (var i = animationCurve.keys.Length - 2; i > 0; i--)
  14.                 {
  15.                     var key = animationCurve.keys[i];
  16.                     var originalValue = animationCurve.Evaluate(key.time);
  17.                     cloned.RemoveKey(i);
  18.                     var reducedValue = cloned.Evaluate(key.time);
  19.                     var distance = isRotation ? Mathf.Abs(Mathf.DeltaAngle(originalValue, reducedValue)) : Mathf.Abs(originalValue - reducedValue);
  20.                     var threshold = isRotation ? errorThreshold : Mathf.Abs(originalValue * errorThreshold);
  21.                     if (distance <= threshold)
  22.                     {
  23.                         animationCurve.RemoveKey(i);
  24.                         removed = true;
  25.                     }
  26.                     cloned.keys = animationCurve.keys;
  27.                 }
  28.             }
Advertisement
Add Comment
Please, Sign In to add comment