1

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 ?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
tigrou
  • 4,236
  • 5
  • 33
  • 59

1 Answers1

0

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.

Joel
  • 5,618
  • 1
  • 20
  • 19