Smoothstep is a family of sigmoid-likeinterpolation and clamping functions commonly used in computer graphics and video game engines. The function depends on three parameters, the input x, the "left edge" and the "right edge", with the left edge being assumed smaller thanthe right edge. The function receives a real numberx 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 gradient 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, the cubic Hermite interpolation after clamping: 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 follows. float smoothstep float clamp The general form for smoothstep, again assuming the left edge is 0 and right edge is 1, is is identical to the clamping function: The characteristic "S"-shaped sigmoid curve is obtained with 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, 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
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: float smootherstep float clamp
Origin
3rd-order equation
Starting with a generic third-order polynomial function and its first derivative: Applying the desired values for the function at both endpoints: Applying the desired values for the first derivative of the function at both endpoints: Solving the system of 4 unknowns formed by the last 4 equations result in the values of the polynomial coefficients: This results in the third-order "smoothstep" function:
5th-order equation
Starting with a generic fifth-order polynomial function, its first derivative and its second derivative: Applying the desired values for the function at both endpoints: Applying the desired values for the first derivative of the function at both endpoints: Applying the desired values for the second derivative of the function at both endpoints: Solving the system of 6 unknowns formed by the last 6 equations result in the values of the polynomial coefficients: This results in the fifth-order "smootherstep" function:
7th-order equation
Applying similar techniques, the 7th-order equation is found to be:
Generalization to higher-order equations
Smoothstep polynomials are generalized, with 0 ≤ x ≤ 1 as where N determines the order of the resulting polynomial function, which is 2N + 1. The first seven smoothstep polynomials, with 0 ≤ x ≤ 1, are It can be shown that the smoothstep polynomials that transition from 0 to 1 when x transitions from 0 to 1 can be simply mapped to odd-symmetry polynomials where and The argument of RN is −1 ≤ x ≤ 1 and is appended to the constant −1 on the left and +1 at the right. An implementation of in Javascript: // Generalized smoothstep function generalSmoothStep // Returns binomial coefficient without explicit use of factorials, // which can't be used with negative integers function pascalTriangle function clamp
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: In GLSL: float inverse_smoothstep