acos
Computes the arc cosine (inverse cosine).
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 PlaygroundLive Demo
The acos function returns the arc cosine (inverse cosine) of the input value. The result is an angle in radians.
Signature
acos :: (T x) -> T {...}
Where T can be float, float2, float3, or float4.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | T | Value in range [-1, 1] |
Return Value
Returns the arc cosine of x in radians, in the range [0, π].
Example
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
// Get angle between two unit vectors
float cosAngle = dot(normalize(a), normalize(b));
float angle = acos(clamp(cosAngle, -1.0, 1.0));Fresnel Effect
// View-dependent fresnel
float cosTheta = dot(normal, viewDir);
float theta = acos(saturate(cosTheta));
float fresnel = pow(theta / (3.14159 * 0.5), fresnelPower);Shadow Bias
// Angle-based shadow bias
float NdotL = dot(normal, lightDir);
float angle = acos(saturate(NdotL));
float bias = baseBias * tan(angle);Spherical Interpolation
// SLERP angle calculation
float cosOmega = dot(q1, q2);
float omega = acos(clamp(abs(cosOmega), 0.0, 1.0));Rim Lighting
// 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:
acos(x)
When compiled to HLSL:
acos(x)