Logarithmic Interpolation
I am trying to come up with a method which does logarithmic and exponential interpolation.
We know that linear interpolation Mathf.Lerp as below uses from + t . (to-from)
float Lerp(float from, float to, float t);
And ideal method I need would look like "Logerp(from, to, t, w)"
A start value (from)
An end value (to)
A 0-1 range percentage to use for interpolation (t)
And finally as an addition to the classical lerp parameters above, a 0-1 range parameter that defines the distribution weight of the values. So if it's closer to 0, distribution will be logarithmic. If it's closer to 1, it will be exponential. If it's exactly 0.5, distribution will be linear. So basically it defines the curvature of the logarithmic graph. (w)
Can you write such a method or a mathematical representation of the described requirement?
@troien You were very helpful with a math problem I've posted before, maybe give this one a shot too?
This paper explains the concept quite nicely: http://www.cmu.edu/biolphys/deserno/pdf/log_interpol.pdf
Nice document, thanks. I see that to^t.from^1-t should give me the logarithmic interpolation value. But I'm having two problems
I can't find a way to modify the steepness of the graph And also how to make it exponential too
I think what you are looking for isn't a logarithmic or exponential interpolation. Rather, you want something of the form
FInterpolation(a, b, t) = (1 - F(t))*a + F(t)*b
where F(t) is any function that maps the range [0,1] to [0,1].
In the case of linear interpolation (lerp), F(t) = t. What you could do ins$$anonymous$$d is have F(t) = t^w (where ^ denotes exponent), for some w > 0. This wouldn't be logarithmic or exponential, but it seems to be what you're looking for.
Answer by SarperS · Sep 12, 2015 at 03:57 PM
I ended up using an AnimationCurve. I have a public field of type AnimationCurve with two keyframes at 0 and 1, which I can edit from the inspector too. When I want to get a specific value at a time I just use the below code.
return from + curve.Evaluate(t) * (to - from);
If I wasn't going to go with the AnimationCurve, I was going to use below two formulas for exponential and logarithmic curves respectively
from + (to - from) * Mathf.Pow(t, w);
to - (to - from) * Mathf.Log(t, w);
The problem with using logarithm in this way is that it maps the range [0,1] to [-infinity,0], not [0,1], so the second formula is actually problematic. If you use t = 0 in the formula, the logarithm would return -infinity, which would almost certainly break things.
The first formula that you have is actually a power function ins$$anonymous$$d of an exponential function. In an exponential, the exponent changes and the base is fixed, so you would want $$anonymous$$athf.Pow(w,t) ins$$anonymous$$d. But still, this maps the range [0,1] to [1,w], which would also probably break things.