Intrinsics2 min read

smoothstep

Performs smooth Hermite interpolation between two edges.

Reading Time
2 min
Word Count
181
Sections
11
Try It Live

Test smoothstep in a live shader

Open the playground, start from a visual preset, and wire smoothstep into the fragment stage to see how it behaves with real values.

Open Playground
bwsl
smoothstep :: (S edge0, S edge1, T x) -> T {...}

The smoothstep function performs smooth Hermite interpolation between 0 and 1 when x is in the range [edge0, edge1]. This creates an S-curve transition that accelerates and decelerates smoothly.

Where T can be float, float2, float3, or float4, and S is a scalar float.

Example - smoothstep vs linear comparison

Parameters

ParameterTypeDescription
edge0SLower edge of transition
edge1SUpper edge of transition
xTValue to interpolate

Return Value

Returns 0 if x <= edge0, returns 1 if x >= edge1, otherwise returns smooth Hermite interpolation.

Example

Common Use Cases

Soft Edges

bwsl
// Create a soft-edged circle
float circle = 1.0 - smoothstep(radius - softness, radius, dist);

Lighting Falloff

bwsl
// Smooth spotlight edge
float spotFactor = smoothstep(outerCone, innerCone, cosAngle);
float3 lighting = lightColor * spotFactor;

Terrain Blending

bwsl
// Smooth blend between grass and rock based on slope
float slope = 1.0 - dot(normal, float3(0.0, 1.0, 0.0));
float rockFactor = smoothstep(0.3, 0.6, slope);
float3 terrain = lerp(grassColor, rockColor, rockFactor);

Anti-aliased Shapes

bwsl
// Anti-aliased line
float lineWidth = 0.02;
float halfWidth = lineWidth * 0.5;
float aa = fwidth(dist) * 1.5;
float line = smoothstep(halfWidth + aa, halfWidth, abs(dist));

Mathematical Definition

The smoothstep function uses the polynomial 3t² - 2t³ where t = (x - edge0) / (edge1 - edge0). This ensures the first derivative is zero at both edges, creating smooth acceleration and deceleration.

Smoother Step

For even smoother transitions with zero second derivatives at the edges, you can use t*t*t*(t*(t*6.0-15.0)+10.0) (Ken Perlin's improved smootherstep).

Compiled Output

When compiled to GLSL:

glsl
smoothstep(edge0, edge1, x)

When compiled to HLSL:

hlsl
smoothstep(edge0, edge1, x)

See Also

  • lerp - Linear interpolation
  • step - Hard step function
  • clamp - Clamp to range