GLSL Learning

GLSL built-in function

exp()

Returns e (about 2.718) raised to the power x. exp(-k * d) is the standard smooth falloff for glows and fog.

Syntax

float exp(float x)
vec2 exp(vec2 x)
vec3 exp(vec3 x)
vec4 exp(vec4 x)

Parameters

x
The exponent. Negative values give results between 0.0 and 1.0.

Return value

e to the power x, always greater than 0.

Live example

Edit the code and the preview updates as you type.

How it works

exp(x) grows very fast for positive x, so in shaders it is mostly used with negative input:

dexp(-4.0 * d)
0.01.0
0.250.37
0.50.14
1.00.02

The value starts at 100% and keeps shrinking by the same proportion for every step of distance, never quite reaching zero. That is how light naturally fades in fog or around a glowing object. In the example d is the distance from the center and k controls how quickly the glow fades.

Typical uses

  • Glows: exp(-k * distance) around a point, a line or any SDF.
  • Distance fog: mix(fogColor, color, exp(-density * depth)).
  • Frame-rate independent smoothing: mix(current, target, 1.0 - exp(-speed * dt)).

Common mistakes

  • Using positive input by accident. exp(10.0) is over 22000, which overflows mediump precision and saturates the color.
  • Expecting the glow to end at a fixed radius. exp never reaches 0. If you need a hard limit, multiply by a smoothstep mask.

Related functions