smoothstep
Performs smooth Hermite interpolation between two edges.
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 Playgroundsmoothstep :: (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
| Parameter | Type | Description |
|---|---|---|
edge0 | S | Lower edge of transition |
edge1 | S | Upper edge of transition |
x | T | Value 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
// Create a soft-edged circle
float circle = 1.0 - smoothstep(radius - softness, radius, dist);Lighting Falloff
// Smooth spotlight edge
float spotFactor = smoothstep(outerCone, innerCone, cosAngle);
float3 lighting = lightColor * spotFactor;Terrain Blending
// 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
// 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:
smoothstep(edge0, edge1, x)
When compiled to HLSL:
smoothstep(edge0, edge1, x)