![Smoothstep and Smootherstep](https://upload.wikimedia.org/wikipedia/commons/5/57/Smoothstep_and_Smootherstep.svg)
Smoothstep is a family of
sigmoid-like interpolation
In the 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 often has a n ...
and
clamping
A wheel clamp, also known as wheel boot, parking boot, or Denver boot, is a device that is designed to prevent motor vehicles from being moved. In its most common form, it consists of a clamp that surrounds a vehicle wheel, designed to preven ...
functions commonly used in
computer graphics
Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great de ...
,
video game engines
Video is an electronic medium for the recording, copying, playback, broadcasting, and display of moving visual media. Video was first developed for mechanical television systems, which were quickly replaced by cathode-ray tube (CRT) syste ...
, and
machine learning
Machine learning (ML) is a field of inquiry devoted to understanding and building methods that 'learn', that is, methods that leverage data to improve performance on some set of tasks. It is seen as a part of artificial intelligence.
Machine ...
.
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
In mathematics, the Hermite polynomials are a classical orthogonal polynomial sequence.
The polynomials arise in:
* signal processing as Hermitian wavelets for wavelet transform analysis
* probability, such as the Edgeworth series, as well as ...
, 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
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 f ...
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 pipeli ...
, smoothstep implements the
, 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 the s ...
after
clamping
A wheel clamp, also known as wheel boot, parking boot, or Denver boot, is a device that is designed to prevent motor vehicles from being moved. In its most common form, it consists of a clamp that surrounds a vehicle wheel, designed to preven ...
:
:
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 edge0, float edge1, float x)
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 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
Saturation, saturated, unsaturation or unsaturated may refer to:
Chemistry
* Saturation, a property of organic compounds referring to carbon-carbon bonds
** Saturated and unsaturated compounds
**Degree of unsaturation
** Saturated fat or fatty ac ...
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 of a function of a real variable measures the sensitivity to change of the function value (output value) with respect to a change in its argument (input value). Derivatives are a fundamental tool of calculus. F ...
s at ''x'' = 0 and ''x'' = 1:
:
C/C++ reference implementation:
float smootherstep(float edge0, float edge1, float x)
float clamp(float x, float lowerlimit, float upperlimit)
Origin
3rd-order equation
Starting with a generic third-order
polynomial
In mathematics, a polynomial is an expression consisting of indeterminates (also called variables) and coefficients, that involves only the operations of addition, subtraction, multiplication, and positive-integer powers of variables. An exa ...
function and its first
derivative
In mathematics, the derivative of a function of a real variable measures the sensitivity to change of the function value (output value) with respect to a change in its argument (input value). Derivatives are a fundamental tool of calculus. F ...
:
:
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
In mathematics, a polynomial is an expression consisting of indeterminates (also called variables) and coefficients, that involves only the operations of addition, subtraction, multiplication, and positive-integer powers of variables. An exa ...
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 2''N'' + 1. The first seven smoothstep polynomials, with 0 ≤ ''x'' ≤ 1, are
:
The differential of
is
:
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 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
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:
:
This arises as the inverse of , whose Maclaurin series
Maclaurin or MacLaurin is a surname. Notable people with the surname include:
* Colin Maclaurin (1698–1746), Scottish mathematician
* Normand MacLaurin (1835–1914), Australian politician and university administrator
* Henry Normand MacLaurin ( ...
terminates at , meaning and express the same function. The series expansion of the inverse, on the other hand, does not terminate.
In GLSL:
float inverse_smoothstep(float x)
References
{{reflist
External links
Using smoothstep
(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
* SWIFT, ...
playground by Simon Gladman
Inverse smoothstep
by Inigo Quilez
Computer graphics algorithms