smoothstep
On this page

smoothstep

Function

Smoothstep creates a cubic transition with a flat slope at both edges.

Explore the function

y = smoothstep(0., k, x)

smoothstep graph. A curve shows the output and a highlighted point follows the selected input.
y = 1.25 y = -0.25 x = -0.5 x = 2
The blue curve shows the output. The orange point marks the selected input.

Wheel to zoom; drag to pan, or rotate in 3D. Keyboard: arrows, +/-, Home to reset.

The axis labels show the displayed range. Values outside this window are not drawn.

Explore the result:

  • Change k, the upper edge, and watch the width of the transition.
Full shader source
smoothstep-plot.shade.ts
"use typeshade";
import { PlotUniforms, plot2D, plot3D } from "./function-plot.shade.ts";
class Uniforms extends PlotUniforms { k: f32; }
declare const u: uniform<Uniforms>;
// Inputs outside the mathematical domain are omitted, including after pan or zoom.
function sample(x: f32): f32 {
return smoothstep(0., u.k, x);
}
function trajectory(t: f32): vec3 {
return vec3(t, sample(t), 0.);
}
@fragment
export function main(uv: location<0, vec2>): vec4 {
const flat = plot2D(sample, uv, u, false);
if (u.plotBlend <= 0.) { return flat; }
const space = plot3D(trajectory, uv, u, false);
return mix(flat, space, clamp(u.plotBlend, 0., 1.));
}
function-plot.shade.ts
function-plot.shade.ts
…
font.shade.ts
font.shade.ts
…
palette.shade.ts
palette.shade.ts
…

Filled by the page every frame, with no control: resolution, plotCenter, plotSpan, plotRotation, plotBlend.

Emitted output

The whole module the compiler saw. The page added the vertex entry and the VsOut struct it returns; the rest is the file above.

WGSL
…
GLSL ES 3.00 vertex
…
GLSL ES 3.00 fragment
…
Uniform block
FieldTypeOffset
resolutionvec2<f32>0
plotCentervec2<f32>8
plotSpanvec2<f32>16
plotRotationvec2<f32>24
rotationBasisvec4<f32>32
plotPanvec2<f32>48
plotBlendf3256
appearancevec4<f32>64
decorationsu3280
labelStylevec4<f32>96
decorationOpacityvec3<f32>112
modelmat4x4<f32>128
inverseModelmat4x4<f32>192
axesModelmat4x4<f32>256
inverseAxesModelmat4x4<f32>320
sceneOpacityvec2<f32>384
cameraLensvec2<f32>392
plotCenterZf32400
probef32404
parameterFocusSeedf32408
view3du32412
parameterBoundsvec2<f32>416
parameterModeu32424
curveQualityvec2<u32>432
curvePatternvec4<f32>448
curveTilesvec4<u32>464
surfaceBoundsvec4<f32>480
surfaceDomainModeu32496
surfaceRangevec2<f32>504
surfaceHeightvec2<f32>512
surfaceDepthvec2<f32>520
surfaceRendervec4<f32>528
surfaceFlagsu32544
kf32548

When to use it

Use it for soft masks and eased transitions.

Example calculation: smoothstep(0., 1.0, 0.5) ≈ 0.5

The reference result is rounded for readability. Shader output follows the precision rules of its target.

Inputs and limits

Conditions in this example: x∈ℝ, k>0

Keep the edges ordered and distinct. Shader antialiasing also needs a transition width appropriate to the pixel footprint.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

WGSL
smoothstep(a, b, c)
GLSL ES 3.00
smoothstep(a, b, c)

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Applies Hermite interpolation between edge0 and edge1, clamped to [0, 1].

Syntax

declare function smoothstep<V extends vec2 | vec3 | vec4>(a0: V, a1: V, a2: V): V
declare function smoothstep<P0 extends number, P1 extends number, P2 extends number>(a0: P0, a1: P1, a2: P2): ScalarOf<P0, P1, P2>
declare function smoothstep<T extends Numeric>(a0: T, a1: T, a2: T): T

A number in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.

type f32 = number & { readonly [f32Tag]?: true }

A number you write as a type is inferred from its uses, and f32, i32 and u32 fix a width; the TypeScript constructs page carries that row with the compiler's emit.

A signature may name a type the ambient file declares for its own use. These are those declarations.

ScalarOf
type ScalarOf<A, B = unknown, C = unknown> = unknown extends A ? number : [A] extends [boolean] ? bool : typeof f32Tag extends keyof A ? f32 : typeof i32Tag extends keyof A ? i32 : typeof u32Tag extends keyof A ? u32 : typeof f64Tag extends keyof A ? f64 : ScalarOf<B, C>
Numeric
type Numeric = number | vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u

Parameters

A parameter named by position (a0, a1) carries a name the ambient file generates. A call passes its arguments in order and names none of them.

  • a0 VP0T
  • a1 VP1T
  • a2 VP2T

Return value

VScalarOf<P0, P1, P2>T

Description

Returns 0 when x is less than or equal to edge0, 1 when greater than or equal to edge1, and interpolates smoothly between. Componentwise over vectors.

Targets

WGSL
smoothstep(a, b, c)
GLSL ES 3.00
smoothstep(a, b, c)

Examples

See also

Edit this page Report a problem