Interpolation Functions – Smoothstep, Gamma, Bias, Gain

I have been doing some hand coded animations recently and I was sick of having to go into the OSX grapher every time I wanted to come up with a function. I would always repeat work that I have done before. So in order to save time in the future I am going to put up a couple of functions that I think will be useful for tweening.

There is a function who has a value of zero and slope zero at zero, and it has a value one and a slope zero at one. That function is 3x^2 – 2x^3. Or in code y = x*x*(3-2*x);

public static float SmoothStep(float a, float b, float x)
    if(x < 0)
        return 0.0f;
    if(x >= b)
        return 1.0f;
    x = (x - a)/(b - a); //normalizes x
    return (x*x * (3 - 2*x));

There are other functions that are useful when you combine them with other functions that are in the range 0->1.

    public static float gammacorrect(float gamma, float x)
		return Mathf.Pow(x, 1.0f/gamma);

A function that is a lot like gamma is called bias but if its parameters are restricted to the range (0..1) which makes it a lot more intuitive to use than gamma.

    public static float bias(float b, float x)
		return Mathf.Pow(x, Mathf.Log(b)/Mathf.Log(0.5f));

Another useful function that is like smoothstep but you can control its curvature and the slope at the ends is called gain.

    public static float gain(float g, float x)
		if(x < 0.5)
			return bias(1-g,2*x)/2;
			return 1 - bias(1-g,2 - 2*x)/2;

Here is a Mac OSX Grapher project file that shows how to make these graphs. It is an example of using functions and piecewise definitions of graphs.