smoothstep

interpolation function

Uses your system voice; voice and rate in settings.

Q7546464Snapshot

≈ 10 min read

Names in other languages

  • Englishsmoothstep
  • Italianosmoothstep
  • 日本語Smoothstep
  • PolskiSmoothstep
  • 中文平滑階躍函數
  • 简体中文平滑階躍函數
  • 繁體中文平滑階躍函式
  • 한국어스무스스텝

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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

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 S1(x), the cubic Hermite interpolation after clamping:

smoothstep⁡(x)=S1(x)={0,x≤03x2−2x3,0≤x≤11,1≤x

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[5] follows.

float smoothstep (float edge0, float edge1, float x) { // Scale, and clamp x to 0..1 range x = clamp((x - edge0) / (edge1 - edge0)); return x * x * (3.0f - 2.0f * x); } float clamp(float x, float lowerlimit = 0.0f, float upperlimit = 1.0f) { if (x < lowerlimit) return lowerlimit; if (x > upperlimit) return upperlimit; return x; }

The general form for smoothstep, again assuming the left edge is 0 and right edge is 1, is

Sn(x)={0,if x≤0xn+1∑k=0n(n+kk)(2n+1n−k)(−x)k,if 0≤x≤11,if 1≤x

S0(x) is identical to the clamping function:

S0(x)={0,if x≤0x,if 0≤x≤11,if 1≤x

The characteristic S-shaped sigmoid curve is obtained with Sn(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

edit edit source

Ken Perlin suggested[6] 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)=S2(x)={0,x≤06x5−15x4+10x3,0≤x≤11,1≤x

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)); return x * x * x * (x * (6.0f * x - 15.0f) + 10.0f); } float clamp(float x, float lowerlimit = 0.0f, float upperlimit = 1.0f) { if (x < lowerlimit) return lowerlimit; if (x > upperlimit) return upperlimit; return x; }

Origin

edit edit source

3rd-order equation

edit edit source

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

S1(x)=a3x3+a2x2+a1x+a0,S1′(x)=3a3x2+2a2x+a1.

Applying the desired values for the function at both endpoints:

S1(0)=0⇒0+0+0+a0=0,S1(1)=1⇒a3+a2+a1+a0=1.

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

S1′(0)=0⇒0+0+a1=0,S1′(1)=0⇒3a3+2a2+a1=0.

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

a0=0,a1=0,a2=3,a3=−2.

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

S1(x)=−2x3+3x2.

Note that this is one of the basis functions of a cubic Hermite spline.

5th-order equation

edit edit source

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

S2(x)=a5x5+a4x4+a3x3+a2x2+a1x+a0,S2′(x)=5a5x4+4a4x3+3a3x2+2a2x+a1,S2″(x)=20a5x3+12a4x2+6a3x+2a2.

Applying the desired values for the function at both endpoints:

S2(0)=0⇒0+0+0+0+0+a0=0,S2(1)=1⇒a5+a4+a3+a2+a1+a0=1.

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

S2′(0)=0⇒0+0+0+0+a1=0,S2′(1)=0⇒5a5+4a4+3a3+2a2+a1=0.

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

S2″(0)=0⇒0+0+0+2a2=0,S2″(1)=0⇒20a5+12a4+6a3+2a2=0.

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

a0=0,a1=0,a2=0,a3=10,a4=−15,a5=6.

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

S2(x)=6x5−15x4+10x3.

7th-order equation

edit edit source

Applying similar techniques, the 7th-order equation is found to be:

S3(x)=−20x7+70x6−84x5+35x4.

Generalization to higher-order equations

edit edit source

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

SN(x)=xN+1∑n=0N(N+nn)(2N+1N−n)(−x)nN∈ℕ=∑n=0N(−1)n(N+nn)(2N+1N−n)xN+n+1=∑n=0N(−N−1n)(2N+1N−n)xN+n+1,

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

S0(x)=x,S1(x)=−2x3+3x2,S2(x)=6x5−15x4+10x3,S3(x)=−20x7+70x6−84x5+35x4,S4(x)=70x9−315x8+540x7−420x6+126x5,S5(x)=−252x11+1386x10−3080x9+3465x8−1980x7+462x6,S6(x)=924x13−6006x12+16380x11−24024x10+20020x9−9009x8+1716x7.

The derivative of SN(x) is

ddx⁡SN(x)=(2N+1)(2NN)(x−x2)N.

It can be shown that the smoothstep polynomials SN(x) that transition from 0 to 1 when x transitions from 0 to 1 can be simply mapped to odd-symmetry polynomials

RN(x)=(∫01(1−u2)Ndu)−1∫0x(1−u2)Ndu,

where

SN(x)=12RN(2x−1)+12

and

RN(−x)=−RN(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 SN(x) in JavaScript:[7]

// 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; }

Frequency Domain Properties

edit edit source

Our original smoothstep function can be rewritten as follows, in terms of the Heaviside step function:

S1(x)=H(x)(3x2−2x3)−H(x−1)(3x2−2x3−1)

Performing some algebraic manipulation,

S1(x)=H(x)(−126x3+62x2)−H(x−1)(−126(x−1)3−62(x−1)2)

giving a Laplace transform of:

S1(s)=−12+12e−ss4+6+6e−ss3

This has asymptotic roll off of 60dB/decade (coming from the 1/s3 term), as opposed to the 20dB/decade of a Heavyside step, or a 40dB/decade of a linear interpolation (S0(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 SN(x), where the roll off is 20(2 + N)dB/decade.

Inverse Smoothstep

edit edit source

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:

InvS1(x)=1/2−sin⁡(asin⁡(1−2x)/3)

This arises because the function f⁡(x)=1/2−sin⁡(3asin⁡(0.5−x))/2 is equivalent to the function 3x2−2x3=S1(x) in the range −1/2≤x≤3/2. The inverse, on the other hand, does not have an exact polynomial equivalent.

In GLSL:

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

References

edit edit source
  1. ^ Smoothstep at Microsoft Developer Network. Wikipedia revision 1356968454, retrieved 2026-11-18
  2. ^ GLSL Language Specification, Version 1.40. Wikipedia revision 1356968454, retrieved 2026-11-18
  3. ^ Unity game engine SmoothStep documentation. Wikipedia revision 1356968454, retrieved 2026-11-18
  4. ^ Hazimeh, Hussein; Ponomareva, Natalia; Mol, Petros; Tan, Zhenyu; Mazumder, Rahul (2020). The Tree Ensemble Layer: Differentiability meets Conditional Computation (PDF). International Conference on Machine Learning. PMLR. Wikipedia revision 1356968454, retrieved 2026-11-18
  5. ^ Natalya Tatarchuk (2003). "Advanced Real-Time Shader Techniques". AMD. p. 94. Archived from the original on 2021-05-30. Retrieved 2022-04-16. Wikipedia revision 1356968454, retrieved 2026-11-18
  6. ^ Texturing and Modeling, Third Edition: A Procedural Approach. Wikipedia revision 1356968454, retrieved 2026-11-18
  7. ^ General smoothstep equation. Wikipedia revision 1356968454, retrieved 2026-11-18
edit edit source

Text derived from https://en.wikipedia.org/w/index.php?oldid=1356968454 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot smoothstep-849178-efd8601d83325500 · hash efd8601d833255008830c6f0ef725ebc52fa4c4f5e5ce3d20375c0696cbbff70 · captured 2026-05-30 · not yet sealed in the ledger · verify the chain · version history

Cite this page as: encyclopegia.org, “smoothstep”, snapshot smoothstep-849178-efd8601d83325500. https://encyclopegia.org/a/smoothstep