lerp
Linear interpolation between two values.
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 Playgroundlerp :: (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
| Parameter | Type | Description |
|---|---|---|
a | T | Start value (returned when t = 0) |
b | T | End value (returned when t = 1) |
t | S | Interpolation 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:
lerp(a, b, t)
When compiled to Metal:
Uses the target backend's native linear interpolation operation.
See Also
- smoothstep - Smooth Hermite interpolation
- step - Step function
- clamp - Clamp to range
- saturate - Clamp to [0, 1]