Smoothstep
In-game article clicks load inline without leaving the challenge.

Smoothstep is a family of sigmoid-like interpolation and clamping functions commonly used in computer graphics, video game engines, and machine learning.
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. It returns 0 if x is less than or equal to the left edge and 1 if x is greater than or equal to the right edge. Otherwise, it smoothly interpolates, using Hermite interpolation, and returns a value between 0 and 1. 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.
In HLSL and GLSL, smoothstep implements the S 1 ( x ) {\displaystyle \operatorname {S} _{1}(x)}, the cubic Hermite interpolation after clamping:
smoothstep ( x ) = S 1 ( x ) = { 0 , x ≤ 0 3 x 2 − 2 x 3 , 0 ≤ x ≤ 1 1 , 1 ≤ x {\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}}}
Assuming that the left edge is 0, the right edge is 1, with the transition between edges taking place where 0 ≤ x ≤ 1.
A modified C/C++ example implementation provided by AMD follows.
The general form for smoothstep, again assuming the left edge is 0 and right edge is 1, is
S n ( x ) = { 0 , if x ≤ 0 x n + 1 ∑ k = 0 n ( n + k k ) ( 2 n + 1 n − k ) ( − x ) k , if 0 ≤ x ≤ 1 1 , if 1 ≤ x {\displaystyle \operatorname {S} _{n}(x)={\begin{cases}0,&{\text{if }}x\leq 0\\x^{n+1}\sum _{k=0}^{n}{\binom {n+k}{k}}{\binom {2n+1}{n-k}}(-x)^{k},&{\text{if }}0\leq x\leq 1\\1,&{\text{if }}1\leq x\\\end{cases}}}
S 0 ( x ) {\displaystyle \operatorname {S} _{0}(x)} is identical to the clamping function:
S 0 ( x ) = { 0 , if x ≤ 0 x , if 0 ≤ x ≤ 1 1 , if 1 ≤ x {\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 S n ( x ) {\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.
Variations
Ken Perlin suggested an improved version of the commonly used first-order smoothstep function, equivalent to the second order of its general form. It has zero 1st- and 2nd-order derivatives at x = 0 and x = 1:
smootherstep ( x ) = S 2 ( x ) = { 0 , x ≤ 0 6 x 5 − 15 x 4 + 10 x 3 , 0 ≤ x ≤ 1 1 , 1 ≤ x {\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:
Origin
3rd-order equation
Starting with a generic third-order polynomial function and its first derivative:
S 1 ( x ) = a 3 x 3 + a 2 x 2 + a 1 x + a 0 , S 1 ′ ( x ) = 3 a 3 x 2 + 2 a 2 x + a 1 . {\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:
S 1 ( 0 ) = 0 ⇒ 0 + 0 + 0 + a 0 = 0 , S 1 ( 1 ) = 1 ⇒ a 3 + a 2 + a 1 + a 0 = 1. {\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:
S 1 ′ ( 0 ) = 0 ⇒ 0 + 0 + a 1 = 0 , S 1 ′ ( 1 ) = 0 ⇒ 3 a 3 + 2 a 2 + a 1 = 0. {\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:
a 0 = 0 , a 1 = 0 , a 2 = 3 , a 3 = − 2. {\displaystyle a_{0}=0,\quad a_{1}=0,\quad a_{2}=3,\quad a_{3}=-2.}
This results in the third-order "smoothstep" function:
S 1 ( x ) = − 2 x 3 + 3 x 2 . {\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:
S 2 ( x ) = a 5 x 5 + a 4 x 4 + a 3 x 3 + a 2 x 2 + a 1 x + a 0 , S 2 ′ ( x ) = 5 a 5 x 4 + 4 a 4 x 3 + 3 a 3 x 2 + 2 a 2 x + a 1 , S 2 ″ ( x ) = 20 a 5 x 3 + 12 a 4 x 2 + 6 a 3 x + 2 a 2 . {\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:
S 2 ( 0 ) = 0 ⇒ 0 + 0 + 0 + 0 + 0 + a 0 = 0 , S 2 ( 1 ) = 1 ⇒ a 5 + a 4 + a 3 + a 2 + a 1 + a 0 = 1. {\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:
S 2 ′ ( 0 ) = 0 ⇒ 0 + 0 + 0 + 0 + a 1 = 0 , S 2 ′ ( 1 ) = 0 ⇒ 5 a 5 + 4 a 4 + 3 a 3 + 2 a 2 + a 1 = 0. {\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:
S 2 ″ ( 0 ) = 0 ⇒ 0 + 0 + 0 + 2 a 2 = 0 , S 2 ″ ( 1 ) = 0 ⇒ 20 a 5 + 12 a 4 + 6 a 3 + 2 a 2 = 0. {\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:
a 0 = 0 , a 1 = 0 , a 2 = 0 , a 3 = 10 , a 4 = − 15 , a 5 = 6. {\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:
S 2 ( x ) = 6 x 5 − 15 x 4 + 10 x 3 . {\displaystyle \operatorname {S} _{2}(x)=6x^{5}-15x^{4}+10x^{3}.}
7th-order equation
Applying similar techniques, the 7th-order equation is found to be:
S 3 ( x ) = − 20 x 7 + 70 x 6 − 84 x 5 + 35 x 4 . {\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
S N ( x ) = x N + 1 ∑ n = 0 N ( N + n n ) ( 2 N + 1 N − n ) ( − x ) n N ∈ N = ∑ n = 0 N ( − 1 ) n ( N + n n ) ( 2 N + 1 N − n ) x N + n + 1 = ∑ n = 0 N ( − N − 1 n ) ( 2 N + 1 N − n ) x N + n + 1 , {\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
S 0 ( x ) = x , S 1 ( x ) = − 2 x 3 + 3 x 2 , S 2 ( x ) = 6 x 5 − 15 x 4 + 10 x 3 , S 3 ( x ) = − 20 x 7 + 70 x 6 − 84 x 5 + 35 x 4 , S 4 ( x ) = 70 x 9 − 315 x 8 + 540 x 7 − 420 x 6 + 126 x 5 , S 5 ( x ) = − 252 x 11 + 1386 x 10 − 3080 x 9 + 3465 x 8 − 1980 x 7 + 462 x 6 , S 6 ( x ) = 924 x 13 − 6006 x 12 + 16380 x 11 − 24024 x 10 + 20020 x 9 − 9009 x 8 + 1716 x 7 . {\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}}}
The derivative of S N ( x ) {\displaystyle \operatorname {S} _{N}(x)} is
d d x S N ( x ) = ( 2 N + 1 ) ( 2 N N ) ( x − x 2 ) N . {\displaystyle {\begin{aligned}\operatorname {d \over dx} {S}_{N}(x)&=(2N+1){\binom {2N}{N}}(x-x^{2})^{N}.\\\end{aligned}}}
It can be shown that the smoothstep polynomials S N ( x ) {\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
R N ( x ) = ( ∫ 0 1 ( 1 − u 2 ) N d u ) − 1 ∫ 0 x ( 1 − u 2 ) N d u , {\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
S N ( x ) = 1 2 R N ( 2 x − 1 ) + 1 2 {\displaystyle \operatorname {S} _{N}(x)={\tfrac {1}{2}}\operatorname {R} _{N}(2x-1)+{\tfrac {1}{2}}}
and
R N ( − x ) = − R N ( x ) . {\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 S N ( x ) {\displaystyle \operatorname {S} _{N}(x)} in JavaScript:
Frequency Domain Properties
Our original smoothstep function can be rewritten as follows, in terms of the Heaviside step function:
S 1 ( x ) = H ( x ) ( 3 x 2 − 2 x 3 ) − H ( x − 1 ) ( 3 x 2 − 2 x 3 − 1 ) {\displaystyle S_{1}(x)=H(x)\left(3x^{2}-2x^{3}\right)-H(x-1)\left(3x^{2}-2x^{3}-1\right)}
Performing some algebraic manipulation,
S 1 ( x ) = H ( x ) ( − 12 6 x 3 + 6 2 x 2 ) − H ( x − 1 ) ( − 12 6 ( x − 1 ) 3 − 6 2 ( x − 1 ) 2 ) {\displaystyle S_{1}(x)=H(x)\left(-{\frac {12}{6}}x^{3}+{\frac {6}{2}}x^{2}\right)-H(x-1)\left(-{\frac {12}{6}}\left(x-1\right)^{3}-{\frac {6}{2}}\left(x-1\right)^{2}\right)}
giving a Laplace transform of:
S 1 ( s ) = − 12 + 12 e − s s 4 + 6 + 6 e − s s 3 {\displaystyle S_{1}(s)={\frac {-12+12e^{-s}}{s^{4}}}+{\frac {6+6e^{-s}}{s^{3}}}}
This has asymptotic roll off of 60dB/decade (coming from the 1 / s 3 {\displaystyle 1/s^{3}} term), as opposed to the 20dB/decade of a Heavyside step, or a 40dB/decade of a linear interpolation (S 0 ( x ) {\displaystyle S_{0}(x)}) - showing that the higher orders of continuity result in a more localized frequency spectra. This can matter for discrete systems, where being band limited is important to reduce aliasing, as per the Nyquist–Shannon sampling theorem.
Similar analysis can be performed for each S N ( x ) {\displaystyle S_{N}(x)}, where the roll off is 20(2 + N)dB/decade.
Inverse Smoothstep
The inverse of 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:
InvS 1 ( x ) = 1 / 2 − sin ( asin ( 1 − 2 x ) / 3 ) {\displaystyle \operatorname {InvS} _{1}(x)=1/2-\sin(\operatorname {asin} (1-2x)/3)}
This arises because the function f ( x ) = 1 / 2 − sin ( 3 asin ( 0.5 − x ) ) / 2 {\displaystyle \operatorname {f} (x)=1/2-\sin(3\operatorname {asin} (0.5-x))/2} is equivalent to the function 3 x 2 − 2 x 3 = S 1 ( x ) {\displaystyle 3x^{2}-2x^{3}=S_{1}(x)} in the range − 1 / 2 ≤ x ≤ 3 / 2 {\displaystyle -1/2\leq x\leq 3/2}. The inverse, on the other hand, does not have an exact polynomial equivalent.
In GLSL:
External links
- (in the RenderMan Shading Language) by Prof. Malcolm Kesson.
- by Jari Komppa
- demonstrates smoothStep(), smootherStep() and smoothestStep() in a Swift playground by Simon Gladman
- by Inigo Quilez