GLSL built-in function
log()
Returns the natural logarithm of x, the inverse of exp. It turns multiplication into addition, which makes infinite zooms possible.
Syntax
float log(float x)
vec2 log(vec2 x)
vec3 log(vec3 x)Parameters
- x
- A value greater than 0.
Return value
The natural logarithm of x.
Live example
Edit the code and the preview updates as you type.
How it works
log(x) asks "e to what power gives x?": log(1.0) is 0, log(2.718) is 1.
Its useful property is that multiplying x adds a constant to log(x). Doubling a distance always adds the same amount (about 0.69), no matter how close or far you are.
The example uses log(length(p)) as one axis of a checkerboard and the angle as the other. Every ring of tiles is a fixed factor bigger than the one inside it, so moving along the log axis looks like flying forever into a tunnel.
Typical uses
- Infinite zooms and tunnels: log-polar coordinates as in the example.
- Compressing a large range:
log(1.0 + x)for brightness or density values that span orders of magnitude.
Common mistakes
log(0.0)or negative input is undefined. Add a small offset:log(length(p) + 1e-4).- Confusing it with base 10. GLSL's
logis the natural log; uselog2for base 2.
Related functions
exp()
Returns e (about 2.718) raised to the power x. exp(-k * d) is the standard smooth falloff for glows and fog.
log2()
Returns the base-2 logarithm of x: how many times you double 1.0 to reach x. The inverse of exp2.
atan()
Returns the angle in radians whose tangent is y / x. The two-argument form atan(y, x) gives the full angle of a point around the origin, from -PI to PI.