Introduction

So much of graphics is about blending: fading one colour into another, sliding an object from here to there, mixing four texels into one, smoothing an edge. Nearly all of it is built from one tiny formula called lerp, plus a few friends that reshape it. This post covers:

  • Lerp: blending between two values
  • Inverse lerp and remapping: the same idea run backwards
  • Clamp: keeping a value within limits
  • Smoothstep and easing curves: making blends look natural

Lerp: Blending Between Two Values

Lerp is short for linear interpolation: finding a value part of the way between two others. You give it a start value a, an end value b, and an amount t from 0 to 1:

lerp(a, b, t) = a + (b − a) × t

At t = 0 you get a, at t = 1 you get b, at t = 0.5 you get exactly halfway, and so on in a straight line (“linear”). For example, lerp(10, 20, 0.25) = 10 + 10 × 0.25 = 12.5. In shader code it’s called mix.

It works on anything made of numbers. For a colour or a position, just lerp each number separately. Drag the orange point, or move the t slider, below.

Lerp between two numbers and between two colours

(Blending colours like this is only physically correct on linear light values; sRGB and Gamma explains why, and why the middle of a red-to-green blend can look muddy.)

You’ve already met lerp many times on this site, under other names. Bilinear filtering is three lerps. Trilinear filtering is seven. The barycentric weights that blend values across a triangle (Fragment Shaders) are a three-way version of the same idea, and alpha blending is a lerp between the old colour and the new one, with α as t.

Inverse Lerp and Remapping

Sometimes you need the opposite question: given a value, how far between a and b is it? That’s the inverse lerp:

t = (value − a) ÷ (b − a)

If a fog effect starts at 10 metres and is thickest at 50, an object at 20 metres is (20 − 10) ÷ (50 − 10) = 0.25 of the way in.

Putting the two together gives remapping: converting a value from one range to another. Inverse-lerp it out of the first range to get t, then lerp into the second. “Turn a temperature from −10 to 40 into a colour from blue to red” is a remap; so is the step in coordinate spaces that turns −1 to +1 into 0 to 1280 pixels.

Clamp: Staying Within Limits

Feed lerp a t below 0 or above 1 and it happily carries on past a or b (that’s called extrapolating). Often that’s wrong: you don’t want fog thicker than “fully foggy”. Clamping limits a value to a range: clamp(t, 0.0, 1.0) (the value, then the lowest and highest allowed) turns anything below 0 into 0 and anything above 1 into 1. Shaders do this constantly (HLSL calls the 0-to-1 version saturate).

Smoothstep and Easing

A plain lerp moves at a constant speed: it starts instantly, and stops dead. For animation and many visual effects that looks mechanical. Real things speed up and slow down. The fix is to reshape t before using it: feed t into a curve, and use the curve’s output as the amount to lerp by. These curves are called easing functions.

The most famous is smoothstep: t × t × (3 − 2t). It starts slowly, speeds up in the middle and slows down again at the end, and it’s built into every shading language (smoothstep(edge0, edge1, x) takes a value x and two edges: it gives 0 when x is at or below edge0, 1 at or above edge1, and the smooth curve in between; inside, it does an inverse lerp and clamp first, then applies the curve). Compare the curves below. The graph shows the curve, with time t going across and the eased amount going up, and its formula in the corner. The two balls race from left to right, one at constant speed and one eased. Drag the Time t slider, or drag across the graph or the plain ball’s track, to step through by hand: the readout shows the formula worked out with the numbers filled in.

Easing curves, and a ball moving with each

  • Ease in (t × t) starts slow and finishes fast, like something falling.
  • Ease out starts fast and settles gently, like something sliding to a stop. User interfaces use this a lot.
  • Overshoot goes slightly past the end and springs back. Its formula, 1 + 2.70158 × (t − 1)³ + 1.70158 × (t − 1)², looks fearsome but is just a curve built from (t − 1) multiplied by itself two and three times. At t = 1, (t − 1) is 0, so the curve ends exactly at 1. Just before that, the squared part (which is always positive) wins over the cubed part (which is negative there), pushing the value above 1 before it settles back. The number 1.70158 is chosen to make it overshoot by about 10%; bigger numbers overshoot further.
  • Bounce imitates a ball landing. It’s made of four parabolas: the curved path anything takes when it’s thrown and falls under gravity. Each piece has the form 7.5625 × (t − centre)² + top: the first is a plain fall from 0 to 1 (the ball dropping), and each of the next three is a smaller, shorter dip that starts and ends at 1, falling back 0.25, then 0.0625, then about 0.016 below it (the ball bouncing back up, each time a quarter as high). Step the time slider through it and watch the readout say which bounce you’re in.

In shaders, smoothstep is also the standard way to make a soft edge instead of a hard one: smoothstep(0.5, 0.52, distance), where distance is how far the pixel is from, say, the centre of a circle of radius 0.5, fades from 0 to 1 between 0.5 and 0.52 instead of jumping at 0.5. The fwidth and Signed Distance Fields posts use exactly this to anti-alias shapes.

Under the hood

Lerp, written as a + (b − a) × t, is one subtraction and one fused multiply-add on a graphics card: about as cheap as maths gets. The interpolation of values across triangles is the same operation done in bulk, and blending, texture filtering and anti-aliasing all lean on dedicated hardware that performs lerps without your shader asking. One subtle point: for very large or very small numbers, a + (b − a) × t may not give exactly b at t = 1 because of rounding, so some libraries use the equivalent a × (1 − t) + b × t, which does.

Where You’ll Use This

  • Colours and gradients: fades, skies, heat maps (Pixels and Colour).
  • Texture filtering: bilinear and trilinear are lerps of texels (Texture Filtering, Mipmaps).
  • Across triangles: barycentric interpolation is a three-way lerp (Fragment Shaders).
  • Blending: transparency is a lerp with alpha (Alpha Blending).
  • Animation: moving objects, cameras and user interfaces with easing; blending between animations.
  • Soft edges: smoothstep for anti-aliasing shapes and fading effects in and out.

Questions You Might Have

Can you lerp rotations and directions?

Lerping two unit directions gives a shorter-than-1 arrow in the middle and an uneven speed. For directions and rotations, slerp (spherical linear interpolation) moves round the circle at a constant speed instead. A plain lerp followed by normalize is a cheap, common approximation.

Summary

  • Lerp, a + (b − a) × t, blends from a (t = 0) to b (t = 1); shaders call it mix.
  • Inverse lerp finds t for a value; remapping moves a value from one range to another.
  • Clamp keeps values within limits.
  • Easing curves such as smoothstep reshape t so blends start and stop smoothly.

Further Reading