Smoothstep

A plot of the smoothstep(x) and smootherstep(x) functions, using 0 as the left edge and 1 as the right edge

Smoothstep is a family of sigmoid-like interpolation and clamping functions commonly used in computer graphics[1][2] and video game engines.[3]

The function depends on three parameters, the input x, the "left edge" and the "right edge", with the left edge being assumed smaller than the right edge. The function receives a real number x as an argument and returns 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, using a Hermite polynomial, between 0 and 1 otherwise. The slope of the smoothstep function is zero at both edges. This is convenient for creating a sequence of transitions using smoothstep to interpolate each segment as an alternative to using more sophisticated or expensive interpolation techniques.

With no salient loss of generality, the left edge may be set to 0 and the right edge to 1. Then the general form for smoothstep is

${\displaystyle \operatorname {S} _{N}(x)={\begin{cases}0&{\text{if }}x\leq 0\\x^{N+1}\sum _{n=0}^{N}{\binom {N+n}{n}}{\binom {2N+1}{N-n}}(-x)^{n}&{\text{if }}0\leq x\leq 1\\1&{\text{if }}1\leq x\\\end{cases}}}$

${\displaystyle \operatorname {S} _{0}(x)}$ is identical to the clamping function:

${\displaystyle \operatorname {S} _{0}(x)={\begin{cases}0&{\text{if }}x\leq 0\\x&{\text{if }}0\leq x\leq 1\\1&{\text{if }}1\leq x\\\end{cases}}}$

The characteristic "S"-shaped sigmoid curve is obtained with ${\displaystyle \operatorname {S} _{N}(x)}$ only for integers N ≥ 1. The order of the polynomial in the general smoothstep is 2N + 1. With N = 1, the slopes or first derivatives of the smoothstep are equal to zero at the left and right edge (x = 0 and x = 1), where the curve is appended to the constant or saturated levels. With higher integer N, the second and higher derivatives are zero at the edges, making the polynomial functions as flat as possible and the splice to the limit values of 0 or 1 more seamless.

In HLSL and GLSL, smoothstep implements the ${\displaystyle \operatorname {S} _{1}(x)}$, the cubic Hermite interpolation after clamping:

${\displaystyle \operatorname {smoothstep} (x)=S_{1}(x)={\begin{cases}0&x\leq 0\\3x^{2}-2x^{3}&0\leq x\leq 1\\1&1\leq x\\\end{cases}}}$

Again, assuming that the left edge is 0, the right edge is 1, with the transition between edges taking place where 0 ≤ x ≤ 1.

A C/C++ example implementation provided by AMD[4] follows.

float smoothstep(float edge0, float edge1, float x) {
// Scale, bias and saturate x to 0..1 range
x = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
// Evaluate polynomial
return x * x * (3 - 2 * x);
}

float clamp(float x, float lowerlimit, float upperlimit) {
if (x < lowerlimit)
x = lowerlimit;
if (x > upperlimit)
x = upperlimit;
return x;
}


Variations

Ken Perlin suggests[5] an improved version of the smoothstep function, which has zero 1st- and 2nd-order derivatives at x = 0 and x = 1:

${\displaystyle \operatorname {smootherstep} (x)=S_{2}(x)={\begin{cases}0&x\leq 0\\6x^{5}-15x^{4}+10x^{3}&0\leq x\leq 1\\1&1\leq x\\\end{cases}}}$

C/C++ reference implementation:

float smootherstep(float edge0, float edge1, float x) {
// Scale, and clamp x to 0..1 range
x = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
// Evaluate polynomial
return x * x * x * (x * (x * 6 - 15) + 10);
}

float clamp(float x, float lowerlimit, float upperlimit) {
if (x < lowerlimit)
x = lowerlimit;
if (x > upperlimit)
x = upperlimit;
return x;
}


Origin

3rd-order equation

Starting with a generic third-order polynomial function and its first derivative:

{\displaystyle {\begin{alignedat}{9}\operatorname {S} _{1}(x)&&\;=\;&&a_{3}x^{3}&&\;+\;&&a_{2}x^{2}&&\;+\;&&a_{1}x&&\;+\;&&a_{0},&\\\operatorname {S} _{1}'(x)&&\;=\;&&3a_{3}x^{2}&&\;+\;&&2a_{2}x&&\;+\;&&a_{1}.&\end{alignedat}}}

Applying the desired values for the function at both endpoints:

{\displaystyle {\begin{alignedat}{13}\operatorname {S} _{1}(0)&&\;=\;&&0\quad &&\Rightarrow &&\quad 0\;&&+&&\;0\;&&+&&\;0\;&&+&&\;a_{0}&&\;=\;&&0,&\\\operatorname {S} _{1}(1)&&\;=\;&&1\quad &&\Rightarrow &&\quad a_{3}\;&&+&&\;a_{2}\;&&+&&\;a_{1}\;&&+&&\;a_{0}&&\;=\;&&1.&\end{alignedat}}}

Applying the desired values for the first derivative of the function at both endpoints:

{\displaystyle {\begin{alignedat}{11}\operatorname {S} _{1}'(0)&&\;=\;&&0\quad &&\Rightarrow &&\quad 0\;&&+&&\;0\;&&+&&\;a_{1}\;&&=\;&&0,&\\\operatorname {S} _{1}'(1)&&\;=\;&&0\quad &&\Rightarrow &&\quad 3a_{3}\;&&+&&\;2a_{2}\;&&+&&\;a_{1}\;&&=\;&&0.&\end{alignedat}}}

Solving the system of 4 unknowns formed by the last 4 equations result in the values of the polynomial coefficients:

${\displaystyle a_{0}=0,\quad a_{1}=0,\quad a_{2}=3,\quad a_{3}=-2.}$

This results in the third-order "smoothstep" function:

${\displaystyle \operatorname {S} _{1}(x)=-2x^{3}+3x^{2}.}$

5th-order equation

Starting with a generic fifth-order polynomial function, its first derivative and its second derivative:

{\displaystyle {\begin{alignedat}{13}\operatorname {S} _{2}(x)&&\;=\;&&a_{5}x^{5}&&\;+\;&&a_{4}x^{4}&&\;+\;&&a_{3}x^{3}&&\;+\;&&a_{2}x^{2}&&\;+\;&&a_{1}x&&\;+\;&&a_{0},&\\\operatorname {S} _{2}'(x)&&\;=\;&&5a_{5}x^{4}&&\;+\;&&4a_{4}x^{3}&&\;+\;&&3a_{3}x^{2}&&\;+\;&&2a_{2}x&&\;+\;&&a_{1},&\\\operatorname {S} _{2}''(x)&&\;=\;&&20a_{5}x^{3}&&\;+\;&&12a_{4}x^{2}&&\;+\;&&6a_{3}x&&\;+\;&&2a_{2}.&\end{alignedat}}}

Applying the desired values for the function at both endpoints:

{\displaystyle {\begin{alignedat}{17}\operatorname {S} _{2}(0)&&\;=\;&&0\;\;\;\;\;&&\Rightarrow &&\;\;\;\;\;0\;&&+&&\;0\;&&+&&\;0\;&&+&&\;0\;&&+&&\;0\;&&+&&\;a_{0}&&\;=\;&&0,&\\\operatorname {S} _{2}(1)&&\;=\;&&1\;\;\;\;\;&&\Rightarrow &&\;\;\;\;\;a_{5}\;&&+&&\;a_{4}\;&&+&&\;a_{3}\;&&+&&\;a_{2}\;&&+&&\;a_{1}\;&&+&&\;a_{0}&&\;=\;&&1.&\end{alignedat}}}

Applying the desired values for the first derivative of the function at both endpoints:

{\displaystyle {\begin{alignedat}{15}\operatorname {S} _{2}'(0)&&\;=\;&&0\;\;\;\;&&\Rightarrow &&\;\;\;\;0\;&&+&&\;0\;&&+&&\;0\;&&+&&\;0\;&&+&&\;a_{1}\;&&=\;&&0,&\\\operatorname {S} _{2}'(1)&&\;=\;&&0\;\;\;\;&&\Rightarrow &&\;\;\;\;5a_{5}\;&&+&&\;4a_{4}\;&&+&&\;3a_{3}\;&&+&&\;2a_{2}\;&&+&&\;a_{1}\;&&=\;&&0.&\end{alignedat}}}

Applying the desired values for the second derivative of the function at both endpoints:

{\displaystyle {\begin{alignedat}{15}\operatorname {S} _{2}''(0)&&\;=\;&&0\;\;\;\;&&\Rightarrow &&\;\;\;\;0\;&&+&&\;0\;&&+&&\;0\;&&+&&\;2a_{2}\;&&=\;&&0,&\\\operatorname {S} _{2}''(1)&&\;=\;&&0\;\;\;\;&&\Rightarrow &&\;\;\;\;20a_{5}\;&&+&&\;12a_{4}\;&&+&&\;6a_{3}\;&&+&&\;2a_{2}\;&&=\;&&0.&\end{alignedat}}}

Solving the system of 6 unknowns formed by the last 6 equations result in the values of the polynomial coefficients:

${\displaystyle a_{0}=0,\quad a_{1}=0,\quad a_{2}=0,\quad a_{3}=10,\quad a_{4}=-15,\quad a_{5}=6.}$

This results in the fifth-order "smootherstep" function:

${\displaystyle \operatorname {S} _{2}(x)=6x^{5}-15x^{4}+10x^{3}.}$

7th-order equation

Also called "smootheststep", the 7th-order equation was derived by Kyle McDonald and first posted to Twitter[6] with a derivation on GitHub:[7]

${\displaystyle \operatorname {S} _{3}(x)=-20x^{7}+70x^{6}-84x^{5}+35x^{4}.}$

Generalization to higher-order equations

Smoothstep polynomials are generalized, with 0 ≤ x ≤ 1 as

{\displaystyle {\begin{aligned}\operatorname {S} _{N}(x)&=x^{N+1}\sum _{n=0}^{N}{\binom {N+n}{n}}{\binom {2N+1}{N-n}}(-x)^{n}\qquad N\in \mathbb {N} \\&=\sum _{n=0}^{N}(-1)^{n}{\binom {N+n}{n}}{\binom {2N+1}{N-n}}x^{N+n+1}\\&=\sum _{n=0}^{N}{\binom {-N-1}{n}}{\binom {2N+1}{N-n}}x^{N+n+1},\\\end{aligned}}}

where N determines the order of the resulting polynomial function, which is 2N + 1. The first seven smoothstep polynomials, with 0 ≤ x ≤ 1, are

{\displaystyle {\begin{aligned}\operatorname {S} _{0}(x)&=x,\\\operatorname {S} _{1}(x)&=-2x^{3}+3x^{2},\\\operatorname {S} _{2}(x)&=6x^{5}-15x^{4}+10x^{3},\\\operatorname {S} _{3}(x)&=-20x^{7}+70x^{6}-84x^{5}+35x^{4},\\\operatorname {S} _{4}(x)&=70x^{9}-315x^{8}+540x^{7}-420x^{6}+126x^{5},\\\operatorname {S} _{5}(x)&=-252x^{11}+1386x^{10}-3080x^{9}+3465x^{8}-1980x^{7}+462x^{6},\\\operatorname {S} _{6}(x)&=924x^{13}-6006x^{12}+16380x^{11}-24024x^{10}+20020x^{9}-9009x^{8}+1716x^{7}.\\\end{aligned}}}

It can be shown that the smoothstep polynomials ${\displaystyle \operatorname {S} _{N}(x)}$ that transition from 0 to 1 when x transitions from 0 to 1 can be simply mapped to odd-symmetry polynomials

${\displaystyle \operatorname {R} _{N}(x)=\left(\int _{0}^{1}{\big (}1-u^{2}{\big )}^{N}\,du\right)^{-1}\int _{0}^{x}{\big (}1-u^{2}{\big )}^{N}\,du,}$

where

${\displaystyle \operatorname {S} _{N}(x)={\tfrac {1}{2}}\operatorname {R} _{N}(2x-1)+{\tfrac {1}{2}}}$

and

${\displaystyle \operatorname {R} _{N}(-x)=-\operatorname {R} _{N}(x).}$

The argument of RN(x) is −1 ≤ x ≤ 1 and is appended to the constant −1 on the left and +1 at the right.

An implementation of ${\displaystyle \operatorname {S} _{N}(x)}$ in Javascript:[8]

// Generalized smoothstep
function generalSmoothStep(N, x) {
x = clamp(x, 0, 1); // x must be equal to or between 0 and 1
var result = 0;
for (var n = 0; n <= N; ++n)
result += pascalTriangle(-N - 1, n) *
pascalTriangle(2 * N + 1, N - n) *
Math.pow(x, N + n + 1));
return result;
}

// Returns binomial coefficient without explicit use of factorials,
// which can't be used with negative integers
function pascalTriangle(a, b) {
var result = 1;
for(var i = 0; i < b; ++i)
result *= (a - i) / (i + 1);
return result;
}

function clamp(x, lowerlimit, upperlimit) {
if (x < lowerlimit)
x = lowerlimit;
if (x > upperlimit)
x = upperlimit;
return x;
}


Inverse Smoothstep

The inverse if smoothstep() can be useful when doing certain operations in computer graphics when its effect needs to be reversed or compensated for. In the case of the 3rd-order equation there exists an analytical solution for the inverse, which is:

${\displaystyle \operatorname {InvS} _{2}(x)=1/2-sin(asin(1-2x)/3)}$

In GLSL:

float inverse_smoothstep( float x )
{
return 0.5 - sin(asin(1.0-2.0*x)/3.0);
}


References

1. ^
2. ^
3. ^
4. ^
5. ^
6. ^ @kcimc (25 March 2015). "smootheststep(t)=-20*t^7+70*t^6-84*t^5+35*t^4 // when smootherstep's second derivative isn't enough" (Tweet) – via Twitter.
7. ^ Kyle McDonald (27 March 2015). "Derivation of 7th-order smoothstep function with zeros in third derivative". Github.com. Retrieved 20 December 2015.
8. ^