Intrinsics2 min read

lerp

Linear interpolation between two values.

Reading Time
2 min
Word Count
248
Sections
12
Try It Live

Test lerp in a live shader

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

Open Playground
bwsl
lerp :: (T a, T b, S t) -> T {...}

The lerp function performs linear interpolation between two values based on a weight parameter. It returns the value a + t * (b - a), smoothly blending from a to b as t goes from 0 to 1.

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

Example - Color Gradient

Parameters

ParameterTypeDescription
aTStart value (returned when t = 0)
bTEnd value (returned when t = 1)
tSInterpolation weight [0, 1]

Return Value

Returns the linearly interpolated value between a and b.

Common Use Cases

Day/Night Cycle

Blend between different times of day for dynamic lighting.

Health Bar UI

Classic game UI element with color transitions.

Distance Fog

Blend objects with fog color based on distance.

Pulsing Effect

Create breathing/pulsing animations common in game UI.

Material Wetness

Blend between dry and wet surface properties.

Smooth Position Transitions

Visualize smooth movement between positions.

Extrapolation

Values of t outside [0, 1] will extrapolate beyond the range of a and b. Use saturate(t) to clamp the weight if you need to prevent extrapolation.

Combining with smoothstep

For ease-in/ease-out transitions, pass your t value through smoothstep(0.0, 1.0, t) before using it with lerp. This creates more natural-feeling animations.

Compiled Output

When compiled to GLSL:

Uses the target backend's native linear interpolation operation.

When compiled to HLSL:

hlsl
lerp(a, b, t)

When compiled to Metal:

Uses the target backend's native linear interpolation operation.

See Also