HOME

TheInfoList



OR:

Smoothstep is a family of sigmoid-like
interpolation In the mathematics, mathematical field of numerical analysis, interpolation is a type of estimation, a method of constructing (finding) new data points based on the range of a discrete set of known data points. In engineering and science, one ...
and clamping functions commonly used in
computer graphics Computer graphics deals with generating images and art with the aid of computers. Computer graphics is a core technology in digital photography, film, video games, digital art, cell phone and computer displays, and many specialized applications. ...
,
video game engines A game engine is a software framework primarily designed for the development of video games which generally includes relevant libraries and support programs such as a level editor. The "engine" terminology is akin to the term "software engine" us ...
, and
machine learning Machine learning (ML) is a field of study in artificial intelligence concerned with the development and study of Computational statistics, statistical algorithms that can learn from data and generalise to unseen data, and thus perform Task ( ...
. 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 In mathematics, a real number is a number that can be used to measure a continuous one- dimensional quantity such as a duration or temperature. Here, ''continuous'' means that pairs of values can have arbitrarily small differences. Every re ...
''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 In numerical analysis, Hermite interpolation, named after Charles Hermite, is a method of polynomial interpolation, which generalizes Lagrange interpolation. Lagrange interpolation allows computing a polynomial of degree less than that takes th ...
, 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 The High-Level Shader Language or High-Level Shading Language (HLSL) is a proprietary shading language developed by Microsoft for the Direct3D 9 API to augment the shader assembly language, and went on to become the required shading language ...
and
GLSL OpenGL Shading Language (GLSL) is a high-level shading language with a syntax based on the C programming language. It was created by the OpenGL ARB (OpenGL Architecture Review Board) to give developers more direct control of the graphics pipe ...
, smoothstep implements the \operatorname_1(x), the cubic
Hermite interpolation In numerical analysis, Hermite interpolation, named after Charles Hermite, is a method of polynomial interpolation, which generalizes Lagrange interpolation. Lagrange interpolation allows computing a polynomial of degree less than that takes th ...
after clamping: :\operatorname(x) = S_1(x) = \begin 0, & x \le 0 \\ 3x^2 - 2x^3, & 0 \le x \le 1 \\ 1, & 1 \le x \\ \end 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. float smoothstep (float edge0, float edge1, float x) float clamp(float x, float lowerlimit = 0.0f, float upperlimit = 1.0f) The general form for ''smoothstep'', again assuming the left edge is 0 and right edge is 1, is :\operatorname_n(x) = \begin 0, & \text x \le 0 \\ x^ \sum_^n \binom \binom (-x)^k, & \text 0 \le x \le 1 \\ 1, & \text 1 \le x \\ \end \operatorname_0(x) is identical to the clamping function: :\operatorname_0(x) = \begin 0, & \text x \le 0 \\ x, & \text 0 \le x \le 1 \\ 1, & \text 1 \le x \\ \end The characteristic S-shaped sigmoid curve is obtained with \operatorname_n(x) only for
integer An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
s ''n'' ≥ 1. The
order Order, ORDER or Orders may refer to: * A socio-political or established or existing order, e.g. World order, Ancien Regime, Pax Britannica * Categorization, the process in which ideas and objects are recognized, differentiated, and understood ...
of the
polynomial In mathematics, a polynomial is a Expression (mathematics), mathematical expression consisting of indeterminate (variable), indeterminates (also called variable (mathematics), variables) and coefficients, that involves only the operations of addit ...
in the general smoothstep is 2''n'' + 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 Kenneth H. Perlin is a professor in the Department of Computer Science at New York University, founding director of the Media Research Lab at NYU, director of the Future Reality Lab at NYU, and the director of the Games for Learning Institute. He ...
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
derivative In mathematics, the derivative is a fundamental tool that quantifies the sensitivity to change of a function's output with respect to its input. The derivative of a function of a single variable at a chosen input value, when it exists, is t ...
s at ''x'' = 0 and ''x'' = 1: :\operatorname(x) = S_2(x) = \begin 0, & x \le 0 \\ 6x^5 - 15x^4 + 10x^3, & 0 \le x \le 1 \\ 1, & 1 \le x \\ \end C/C++ reference implementation: float smootherstep(float edge0, float edge1, float x) float clamp(float x, float lowerlimit = 0.0f, float upperlimit = 1.0f)


Origin


3rd-order equation

Starting with a generic third-order
polynomial In mathematics, a polynomial is a Expression (mathematics), mathematical expression consisting of indeterminate (variable), indeterminates (also called variable (mathematics), variables) and coefficients, that involves only the operations of addit ...
function and its first
derivative In mathematics, the derivative is a fundamental tool that quantifies the sensitivity to change of a function's output with respect to its input. The derivative of a function of a single variable at a chosen input value, when it exists, is t ...
: :\begin \operatorname_1(x) &&\; = \;&& a_3 x^3 &&\; + \;&& a_2 x^2 &&\; + \;&& a_1 x &&\; + \;&& a_0, & \\ \operatorname_1'(x) &&\; = \;&& 3 a_3 x^2 &&\; + \;&& 2 a_2 x &&\; + \;&& a_1. & \end Applying the desired values for the function at both endpoints: :\begin \operatorname_1(0) &&\; = \;&& 0 \quad&& \Rightarrow &&\quad 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; a_0 &&\; = \;&& 0, & \\ \operatorname_1(1) &&\; = \;&& 1 \quad&& \Rightarrow &&\quad a_3 \;&& + &&\; a_2 \;&& + &&\; a_1 \;&& + &&\; a_0 &&\; = \;&& 1. & \end Applying the desired values for the first derivative of the function at both endpoints: :\begin \operatorname_1'(0) &&\; = \;&& 0 \quad&& \Rightarrow &&\quad 0 \;&& + &&\; 0 \;&& + &&\; a_1 \;&& = \;&& 0, & \\ \operatorname_1'(1) &&\; = \;&& 0 \quad&& \Rightarrow &&\quad 3 a_3 \;&& + &&\; 2 a_2 \;&& + &&\; a_1 \;&& = \;&& 0. & \end Solving the system of 4 unknowns formed by the last 4 equations result in the values of the polynomial coefficients: :a_0 = 0, \quad a_1 = 0, \quad a_2 = 3, \quad a_3 = -2. This results in the third-order ''"smoothstep"'' function: :\operatorname_1(x) = -2x^3 + 3x^2.


5th-order equation

Starting with a generic fifth-order
polynomial In mathematics, a polynomial is a Expression (mathematics), mathematical expression consisting of indeterminate (variable), indeterminates (also called variable (mathematics), variables) and coefficients, that involves only the operations of addit ...
function, its first derivative and its second derivative: :\begin \operatorname_2(x) &&\; = \;&& a_5 x^5 &&\; + \;&& a_4 x^4 &&\; + \;&& a_3 x^3 &&\; + \;&& a_2 x^2 &&\; + \;&& a_1 x &&\; + \;&& a_0, & \\ \operatorname_2'(x) &&\; = \;&& 5 a_5 x^4 &&\; + \;&& 4 a_4 x^3 &&\; + \;&& 3 a_3 x^2 &&\; + \;&& 2 a_2 x &&\; + \;&& a_1, & \\ \operatorname_2''(x) &&\; = \;&& 20 a_5 x^3 &&\; + \;&& 12 a_4 x^2 &&\; + \;&& 6 a_3 x &&\; + \;&& 2 a_2. & \end Applying the desired values for the function at both endpoints: :\begin \operatorname_2(0) &&\; = \;&& 0 \;\;\;\;\;&& \Rightarrow &&\;\;\;\;\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; a_0 &&\; = \;&& 0, & \\ \operatorname_2(1) &&\; = \;&& 1 \;\;\;\;\;&& \Rightarrow &&\;\;\;\;\; a_5 \;&& + &&\; a_4 \;&& + &&\; a_3 \;&& + &&\; a_2 \;&& + &&\; a_1 \;&& + &&\; a_0 &&\; = \;&& 1. & \end Applying the desired values for the first derivative of the function at both endpoints: :\begin \operatorname_2'(0) &&\; = \;&& 0 \;\;\;\;&& \Rightarrow &&\;\;\;\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; a_1 \;&& = \;&& 0, & \\ \operatorname_2'(1) &&\; = \;&& 0 \;\;\;\;&& \Rightarrow &&\;\;\;\; 5 a_5 \;&& + &&\; 4 a_4 \;&& + &&\; 3 a_3 \;&& + &&\; 2 a_2 \;&& + &&\; a_1 \;&& = \;&& 0. & \end Applying the desired values for the second derivative of the function at both endpoints: :\begin \operatorname_2''(0) &&\; = \;&& 0 \;\;\;\;&& \Rightarrow &&\;\;\;\; 0 \;&& + &&\; 0 \;&& + &&\; 0 \;&& + &&\; 2 a_2 \;&& = \;&& 0, & \\ \operatorname_2''(1) &&\; = \;&& 0 \;\;\;\;&& \Rightarrow &&\;\;\;\; 20 a_5 \;&& + &&\; 12 a_4 \;&& + &&\; 6 a_3 \;&& + &&\; 2 a_2 \;&& = \;&& 0. & \end Solving the system of 6 unknowns formed by the last 6 equations result in the values of the polynomial coefficients: :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: :\operatorname_2(x) = 6x^5 - 15x^4 + 10x^3.


7th-order equation

Applying similar techniques, the 7th-order equation is found to be: :\operatorname_3(x) = -20x^7 + 70x^6 - 84x^5 + 35x^4.


Generalization to higher-order equations

Smoothstep polynomials are generalized, with 0 ≤ ''x'' ≤ 1 as :\begin \operatorname_N(x) &= x^ \sum_^N \binom \binom (-x)^ \qquad N \in \mathbb\\ &= \sum_^N (-1)^n \binom \binom x^ \\ &= \sum_^N \binom \binom x^, \\ \end where ''N'' determines the order of the resulting polynomial function, which is 2''N'' + 1. The first seven smoothstep polynomials, with 0 ≤ ''x'' ≤ 1, are :\begin \operatorname_0(x) &= x, \\ \operatorname_1(x) &= -2x^3 + 3x^2, \\ \operatorname_2(x) &= 6x^5 - 15x^4 + 10x^3, \\ \operatorname_3(x) &= -20x^7 + 70x^6 - 84x^5 + 35x^4, \\ \operatorname_4(x) &= 70x^9 - 315x^8 + 540x^7 - 420x^6 + 126x^5, \\ \operatorname_5(x) &= -252x^ + 1386x^ - 3080x^9 + 3465x^8 - 1980x^7 + 462x^6, \\ \operatorname_6(x) &= 924x^ - 6006x^ + 16380x^ - 24024x^ + 20020x^9 - 9009x^8 + 1716x^7. \\ \end The differential of \operatorname_N(x) is :\begin \operatorname_N(x) &= (2N+1)\binom (x-x^2)^N. \\ \end It can be shown that the smoothstep polynomials \operatorname_N(x) that transition from 0 to 1 when ''x'' transitions from 0 to 1 can be simply mapped to odd-symmetry polynomials : \operatorname_N(x) = \left(\int_^ \big(1 - u^2 \big)^N \,du \right)^ \int_^ \big(1 - u^2 \big)^N \,du, where :\operatorname_N(x) = \tfrac12 \operatorname_N(2x-1) + \tfrac12 and : \operatorname_N(-x) = -\operatorname_N(x). The argument of R''N''(''x'') is −1 ≤ ''x'' ≤ 1 and is appended to the constant −1 on the left and +1 at the right. An implementation of \operatorname_N(x) in Javascript:General smoothstep equation
// Generalized smoothstep function generalSmoothStep(N, x) // Returns binomial coefficient without explicit use of factorials, // which can't be used with negative integers function pascalTriangle(a, b) function clamp(x, lowerlimit, upperlimit)


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: :\operatorname_1(x) = 1/2 - \sin(\operatorname(1-2x)/3) This arises because the function \operatorname(x) = 1/2 - \sin(3 \operatorname(0.5-x))/2 is equivalent to the function 3x^2-2x^3=S_1(x) in the range -1/2 \le x \le 3/2. The inverse, on the other hand, does not have an exact polynomial equivalent. In GLSL: float inverse_smoothstep(float x)


References

{{reflist


External links



(in the
RenderMan Shading Language Renderman Shading Language (abbreviated RSL) is a component of the RenderMan Interface Specification, and is used to define shaders. The language syntax is C-like. A shader written in RSL can be used without changes on any RenderMan-compliant rend ...
) by Prof. Malcolm Kesson.
Interpolation tricks
by Jari Komppa
Swift Interpolation Playground
demonstrates ''smoothStep()'', ''smootherStep()'' and ''smoothestStep()'' in a
Swift Swift or SWIFT most commonly refers to: * SWIFT, an international organization facilitating transactions between banks ** SWIFT code * Swift (programming language) * Swift (bird), a family of birds It may also refer to: Organizations * SWIF ...
playground by Simon Gladman
Inverse smoothstep
by Inigo Quilez Computer graphics algorithms