GLSL built-in function
acos()
Returns the angle in radians whose cosine is x. acos(dot(a, b)) gives the angle between two unit vectors.
Syntax
float acos(float x)
vec2 acos(vec2 x)
vec3 acos(vec3 x)Parameters
- x
- A value between -1.0 and 1.0.
Return value
An angle between 0.0 and PI (0 to 180 degrees).
Live example
Edit the code and the preview updates as you type.
How it works
acos undoes cos: cos(1.0472) is 0.5, and acos(0.5) is 1.0472 (60 degrees).
Its most common job comes from the dot product. For two unit vectors, dot(a, b) is the cosine of the angle between them, so acos(dot(a, b)) is the angle itself, from 0 (same direction) to PI (opposite).
The example measures the angle between each pixel's direction and a rotating beam, then lights up every pixel within 20 degrees: a radar-like cone.
Typical uses
- Angle between vectors: cones, spotlights, field-of-view checks.
- Spherical coordinates:
acos(n.y)gives the latitude of a normal on a sphere.
Common mistakes
- Passing a value slightly outside -1..1. Floating point error can make
dotof two unit vectors return1.0000001, andacosof that is undefined (NaN). Alwaysclamp(x, -1.0, 1.0). - Using it when the cosine itself is enough. Comparing
dot(a, b) > cos(radians(20.0))gives the same cone test withoutacos.
Related functions
dot()
Returns the dot product of two vectors. For unit vectors it measures how much they point the same way: 1.0 same direction, 0.0 perpendicular, -1.0 opposite.
cos()
Returns the cosine of an angle in radians: the same wave as sin, shifted a quarter turn ahead.
clamp()
Limits x to the range between minVal and maxVal, computed as min(max(x, minVal), maxVal).