Forked from PeteMichaud/AnimationCurveExtensions.cs
Last active
March 21, 2025 00:05
-
-
Save j-schoch/4af84776c7e64295f2581b44d9a1c1c6 to your computer and use it in GitHub Desktop.
Added some new functions (MinMax, Normalize, PlotFunctionOnCurve, SimplifyCurve) & reformatted to my preferences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2018 Pete Michaud, github.com/PeteMichaud | |
Modified 2025 Jeff Schoch, github.com/j-schoch | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
public static class AnimationCurveExtensions | |
{ | |
private const float DELTA = .001f; | |
private const float DEFAULT_SMOOTHING = .01f; | |
private const int DEFAULT_RESOLUTION = 200; | |
private static float Remap(this float value, (float min, float max) from, (float min, float max) to) | |
{ | |
return Mathf.Lerp(to.min, to.max, Mathf.InverseLerp(from.min, from.max, value)); | |
} | |
public static (float min, float max) MinMax(this AnimationCurve curve, int resolution = DEFAULT_RESOLUTION) | |
{ | |
float min = float.MaxValue; | |
float max = 0; | |
for (int i = 0; i <= resolution; i++) | |
{ | |
float y = curve.Evaluate((float)i / resolution); | |
max = y > max ? y : max; | |
min = y < min ? y : min; | |
} | |
return (min, max); | |
} | |
public static AnimationCurve Normalize(this AnimationCurve curve, int resolution = DEFAULT_RESOLUTION, float smoothing = DEFAULT_SMOOTHING) | |
{ | |
(float, float) minMax = curve.MinMax(resolution); | |
return PlotFunctionOnCurve((float t) => curve.Evaluate(t).Remap(from: minMax, to: (0, 1)), resolution, smoothing); | |
} | |
public static AnimationCurve SimplifyCurve(List<Vector2> curvePoints, float smoothing = DEFAULT_SMOOTHING) | |
{ | |
List<Vector2> simplifiedCurvePoints = new(); | |
if (smoothing > 0) | |
{ | |
LineUtility.Simplify(curvePoints, smoothing, simplifiedCurvePoints); | |
} | |
else | |
{ | |
simplifiedCurvePoints = curvePoints; | |
} | |
AnimationCurve simplifiedCurve = new(simplifiedCurvePoints.Select(v => new Keyframe(v.x, v.y)).ToArray()); | |
int keyCount = simplifiedCurve.keys.Length; | |
for (int i = 0; i < keyCount; i++) | |
{ | |
bool firstOrLast = i == 0 || i == keyCount - 1; | |
AnimationUtility.TangentMode mode = firstOrLast ? AnimationUtility.TangentMode.Auto : AnimationUtility.TangentMode.ClampedAuto; | |
AnimationUtility.SetKeyLeftTangentMode(simplifiedCurve, i, mode); | |
AnimationUtility.SetKeyRightTangentMode(simplifiedCurve, i, mode); | |
AnimationUtility.SetKeyBroken(simplifiedCurve, i, false); | |
} | |
return simplifiedCurve; | |
} | |
public static AnimationCurve PlotFunctionOnCurve(Func<float, float> function, int resolution = DEFAULT_RESOLUTION, float smoothing = DEFAULT_SMOOTHING) | |
{ | |
List<Vector2> points = new(); | |
for (int i = 0; i <= resolution; i++) | |
{ | |
float t = (float)i / resolution; | |
points.Add(new Vector2(t, function(t))); | |
} | |
return SimplifyCurve(points, smoothing); | |
} | |
/// <summary> | |
/// Find first derivative of curve at point x | |
/// </summary> | |
/// <param name="curve"></param> | |
/// <param name="x"></param> | |
/// <returns>Slope of curve at point x as float</returns> | |
public static float Differentiate(this AnimationCurve curve, float x) | |
{ | |
return curve.Differentiate(x, curve.keys.First().time, curve.keys.Last().time); | |
} | |
public static float Differentiate(this AnimationCurve curve, float x, float xMin, float xMax) | |
{ | |
float x1 = Mathf.Max(xMin, x - DELTA); | |
float x2 = Mathf.Min(xMax, x + DELTA); | |
float y1 = curve.Evaluate(x1); | |
float y2 = curve.Evaluate(x2); | |
return (y2 - y1) / (x2 - x1); | |
} | |
public static AnimationCurve Derivative(this AnimationCurve curve, int resolution = DEFAULT_RESOLUTION, float smoothing = DEFAULT_SMOOTHING) | |
{ | |
return PlotFunctionOnCurve((float t) => curve.Differentiate(t), resolution, smoothing); | |
} | |
/// <summary> | |
/// Find integral of curve between xStart and xEnd using the trapezoidal rule | |
/// </summary> | |
/// <param name="resolution">Resolution of calculation. Increase for better | |
/// precision, at cost of computation</param> | |
/// <returns>Area under the curve between xStart and xEnd as float</returns> | |
public static float Integrate(this AnimationCurve curve, float xStart = 0f, float xEnd = 1f, int resolution = DEFAULT_RESOLUTION) | |
{ | |
float sliceWidth = (xEnd - xStart) / resolution; | |
float accumulatedTotal = (curve.Evaluate(xStart) + curve.Evaluate(xEnd)) / 2; | |
for (int i = 1; i < resolution; i++) | |
{ | |
accumulatedTotal += curve.Evaluate(xStart + i * sliceWidth); | |
} | |
return sliceWidth * accumulatedTotal; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment