In one of my projects, I use the following smoothstep() function :
float smoothstep(float a, float b, float m, int n)
{
for(int i = 0 ; i < n ; i++)
{
m = m * m * (3 - 2 * m);
}
return a + (b - a) * m;
}
It works great, however, it has two disadvantages :
- It's slow (especially for big values of
n) - It doesn't work for non integer values (eg :
n = 1.5)
Is there an alternative (excluding precalculating points and then interpolating) providing better performance (and same behavior), or another function giving a great approximation ?
You should be able to precompute the "m" term, since it doesn't rely on a or b, and assuming you're doing this over an entire interpolation, this should speed up your code significantly.
Alternatively, you could use the built in MathHelper.Smoothstep method, which provides a cubic interpolation, rather than the linear interpolation you get out of your version. There are also other, more advanced, interpolators in that class, also.