Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void Simplify(this AnimationCurve animationCurve, float errorThreshold, bool isRotation = false)
- {
- if (animationCurve.keys.Length < 3)
- {
- return;
- }
- var cloned = new AnimationCurve();
- cloned.keys = animationCurve.keys;
- var removed = true;
- while (removed)
- {
- removed = false;
- for (var i = animationCurve.keys.Length - 2; i > 0; i--)
- {
- var key = animationCurve.keys[i];
- var originalValue = animationCurve.Evaluate(key.time);
- cloned.RemoveKey(i);
- var reducedValue = cloned.Evaluate(key.time);
- var distance = isRotation ? Mathf.Abs(Mathf.DeltaAngle(originalValue, reducedValue)) : Mathf.Abs(originalValue - reducedValue);
- var threshold = isRotation ? errorThreshold : Mathf.Abs(originalValue * errorThreshold);
- if (distance <= threshold)
- {
- animationCurve.RemoveKey(i);
- removed = true;
- }
- cloned.keys = animationCurve.keys;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment