Intrinsics1 min read
tan
Computes the tangent of an angle in radians.
Reading Time
1 min
Word Count
127
Sections
12
Try It Live
Test tan in a live shader
Open the playground, start from a visual preset, and wire tan into the fragment stage to see how it behaves with real values.
Open PlaygroundLive Demo
The tan function returns the tangent of an angle specified in radians, computed as sin(x) / cos(x).
Signature
bwsl
tan :: (T x) -> T {...}
Where T can be float, float2, float3, or float4.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | T | Angle in radians |
Return Value
Returns the tangent of x.
Example
bwsl
pipeline ProjectionSetup {
fragment {
// Calculate field of view factor
float fovRadians = radians(fieldOfView);
float tanHalfFov = tan(fovRadians * 0.5);
// Use for projection calculations
float projScale = 1.0 / tanHalfFov;
output.color = float4(projScale, tanHalfFov, 0.0, 1.0);
}
}Common Use Cases
Projection Matrix
bwsl
// Perspective projection scale factor
float tanHalfFov = tan(fov * 0.5);
float projectionScale = 1.0 / tanHalfFov;Shadow Map Bias
bwsl
// Slope-based depth bias
float slopeBias = tan(acos(NdotL)) * baseBias;Angle-Based Calculations
bwsl
// Calculate opposite side from adjacent and angle
float opposite = adjacent * tan(angle);Terrain Slope
bwsl
// Calculate slope angle tangent
float3 gradient = normalize(float3(ddx(height), 1.0, ddy(height)));
float slope = tan(acos(gradient.y));Fisheye Distortion
bwsl
// Simple fisheye effect
float2 centered = input.uv - 0.5;
float dist = length(centered);
float angle = atan(dist * fisheyeStrength);
float2 distorted = centered * (tan(angle) / dist);Singularities
tan(x) approaches infinity at x = π/2 + n*π (90°, 270°, etc.). Avoid these values or handle the edge cases.
Alternative
If you have the sine and cosine already, computing sin(x) / cos(x) directly may be more efficient than calling tan(x).
Compiled Output
When compiled to GLSL:
glsl
tan(x)
When compiled to HLSL:
hlsl
tan(x)