Intrinsics1 min read

acos

Computes the arc cosine (inverse cosine).

Reading Time
1 min
Word Count
154
Sections
12
Try It Live

Test acos in a live shader

Open the playground, start from a visual preset, and wire acos into the fragment stage to see how it behaves with real values.

Open Playground

Live Demo

The acos function returns the arc cosine (inverse cosine) of the input value. The result is an angle in radians.

Signature

bwsl
acos :: (T x) -> T {...}

Where T can be float, float2, float3, or float4.

Parameters

ParameterTypeDescription
xTValue in range [-1, 1]

Return Value

Returns the arc cosine of x in radians, in the range [0, π].

Example

bwsl
pipeline AngleVisualization {
fragment {
float3 normal = normalize(input.normal);
float3 viewDir = normalize(cameraPos - input.worldPos);
// Calculate angle between normal and view direction
float cosAngle = dot(normal, viewDir);
float angle = acos(clamp(cosAngle, -1.0, 1.0));
// Visualize angle (0 = facing camera, π = facing away)
float normalized = angle / 3.14159;
output.color = float4(float3(normalized), 1.0);
}
}

Common Use Cases

Angle Between Vectors

bwsl
// Get angle between two unit vectors
float cosAngle = dot(normalize(a), normalize(b));
float angle = acos(clamp(cosAngle, -1.0, 1.0));

Fresnel Effect

bwsl
// View-dependent fresnel
float cosTheta = dot(normal, viewDir);
float theta = acos(saturate(cosTheta));
float fresnel = pow(theta / (3.14159 * 0.5), fresnelPower);

Shadow Bias

bwsl
// Angle-based shadow bias
float NdotL = dot(normal, lightDir);
float angle = acos(saturate(NdotL));
float bias = baseBias * tan(angle);

Spherical Interpolation

bwsl
// SLERP angle calculation
float cosOmega = dot(q1, q2);
float omega = acos(clamp(abs(cosOmega), 0.0, 1.0));

Rim Lighting

bwsl
// Angle-based rim calculation
float viewAngle = acos(saturate(dot(normal, viewDir)));
float rim = smoothstep(rimStart, rimEnd, viewAngle);

Domain

acos(x) is only defined for x in [-1, 1]. Values outside this range produce undefined results. Always clamp: acos(clamp(x, -1.0, 1.0)).

Common Pattern

When computing angles from dot products, the pattern acos(clamp(dot(a, b), -1.0, 1.0)) prevents NaN from floating-point errors that might push the dot product slightly outside [-1, 1].

Compiled Output

When compiled to GLSL:

glsl
acos(x)

When compiled to HLSL:

hlsl
acos(x)

See Also