![]() | ||
Smoothstep is an interpolation function commonly used in computer graphics and video game engines.
Contents
- Variations
- 3rd order equation
- 5th order equation
- 7th order equation
- Generalization of higher order equations
- References
The function depends on two parameters, the "left edge" and the "right edge", with the left edge being assumed smaller than the right edge. The function takes a real number x as input and outputs 0 if x is less than or equal to the left edge, 1 if x is greater than or equal to the right edge, and smoothly interpolates between 0 and 1 otherwise. The slope of the smoothstep function is zero at both edges. This makes it easy to create a sequence of transitions using smoothstep to interpolate each segment rather than using a more sophisticated or expensive interpolation technique.
As pointed out in MSDN and OpenGL documentation, smoothstep implements cubic Hermite interpolation after doing a clamp:
where we assume that the left edge is 0, the right edge is 1, and 0 ≤ x ≤ 1.
A C/C++ example implementation provided by AMD follows.
Variations
Ken Perlin suggests an improved version of the smoothstep function which has zero 1st and 2nd order derivatives at x=0 and x=1:
C/C++ reference implementation:
3rd order equation
We start with a generic third order polynomial function and its first derivative:
Applying the desired values for the function at both endpoints we get:
Applying the desired values for the first derivative of the function at both endpoints we get:
Solving the system of 4 unknowns formed by the last 4 equations we obtain the values of the polynomial coefficients:
Introducing these coefficients back into the first equation gives the third order smoothstep function:
5th order equation
We start with a generic fifth order polynomial function, its first derivative and its second derivative:
Applying the desired values for the function at both endpoints we get:
Applying the desired values for the first derivative of the function at both endpoints we get:
Applying the desired values for the second derivative of the function at both endpoints we get:
Solving the system of 6 unknowns formed by the last 6 equations we obtain the values of the polynomial coefficients:
Introducing these coefficients back into the first equation gives the fifth order smootherstep function:
7th order equation
Also called "smootheststep", the 7th order equation was derived by Kyle McDonald and first posted to Twitter with a derivation on GitHub:
Generalization of higher-order equations
All smoothstep equations can be generalized as:
where a determines the order of the resulting polynomial equation, with the order being calculated as 2a - 1. Evaluating this function for different values of a gives:
For any value a where a > 0, this generalization will smoothly interpolate from 0 to 1 for any value x on the interval 0 ≤ x ≤ 1 while retaining the property Sa(0.5) = 0.5.
An implementation of this function in Javascript