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:
| d | exp(-4.0 * d) |
|---|---|
| 0.0 | 1.0 |
| 0.25 | 0.37 |
| 0.5 | 0.14 |
| 1.0 | 0.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.
expnever reaches 0. If you need a hard limit, multiply by asmoothstepmask.
Related functions
exp2()
Returns 2 raised to the power x. Doubling and halving per step, as in noise octaves and exposure stops.
log()
Returns the natural logarithm of x, the inverse of exp. It turns multiplication into addition, which makes infinite zooms possible.
pow()
Returns x raised to the power y. On values between 0.0 and 1.0 it bends a straight ramp into a curve.