原文
方法一:
没有把参数t从系数中分离开来,直接混在一起计算系数a,b,c,d。这样看算法比较直观,但是不是最优化的。
1 | float Evaluate(float t, Keyframe keyframe0, Keyframe keyframe1) |
方法二:
通过解四元三次方程组,得到系数a,b,c,d,这样得出的系数计算公式看着很复杂,但是因为系数可以提前计算好,所以效率要比方法一好。
Unity define a curve with 2 keyframes, each composed of a point and a tangent. I guess the simplest curve matching that is a third degree polynomial (a cubic function). Given the 2 points and tangents, it is possible to compute the polynomial coefficients simply by solving the following equation system:
1 | (1) a*p1x^3 + b*p1x^2 + c*p1x + d = p1y |
You can solve this manually or using a computer algebra system.
This gives you:
1 | float a = (p1x * tp1 + p1x * tp2 - p2x * tp1 - p2x * tp2 - 2 * p1y + 2 * p2y) / (p1x * p1x * p1x - p2x * p2x * p2x + 3 * p1x * p2x * p2x - 3 * p1x * p1x * p2x); |
Then, to evaluate the value:
1 | float Evaluate(float t) |
I checked with Unity with the following quick and dirty code:
1 | using UnityEngine; |
After modifing tangents of the animation curve, the debug messages produced by this code are:
1 | 0, 0, -4.484611, 1, 1, -10.23884 |
So it really seams that this approach is the math behind AnimationCurve.Evaluate ;)
方法三:
系数的计算公式看着比较简单,并且也可以提前计算好,效率应该和方法二差不多。
AnimationCurve is a simple spline of cubic equations, where for each segment you specify the (time, value) coordinates and slope for each of two end points (the first point’s “out” “tangent”, second one’s “in”). A cubic equation is the simplest polynomial which can meet these criteria.
This is also known as a cubic Hermite spline, where the only difference is that AnimationCurve allows you to assign different slopes for each direction from a keyframe (a point), which creates discontinuities in the first derivative (sharp angles). However, this does not change the formula.
https://en.wikipedia.org/wiki/Cubic_Hermite_spline
It took me some time (much more than I’d like to admit…) but I simplified the system of cubic equations for the endpoints into this form.
1 | pd = 1/(p2x-p1x) |
Isolated t to calculate coefficients in advance, providing a fast t evaluation:
1 | pd = 1/(p2x-p1x) |
I had the same question, was looking for a straightforward answer. Because my search query took me here and this is the closest I could find, I assume such solutions to this exact problem are not abundant, so I thought I’d share mine. I derived it because I need to emulate the same function outside of unity. Looking at the other three answers here, they seem to be correct but not simplified (trust me this is not an easy task) (Varaughe’s leaves out the evaluation of the Bezier curve he made from the AnimationCurve, as it is a well-known thing).
I tested these against AnimationCurve for correctness with a Python script which can randomize all six parameters of the cubic function:
1 | # isolated t |