GLSL Learning

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 dot of two unit vectors return 1.0000001, and acos of that is undefined (NaN). Always clamp(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 without acos.

Related functions