GLSL built-in function
inversesqrt()
Returns 1.0 / sqrt(x). Common in normalization and in light falloff that fades with distance.
Syntax
float inversesqrt(float x)
vec2 inversesqrt(vec2 x)
vec3 inversesqrt(vec3 x)Parameters
- x
- A value greater than 0.
Return value
1.0 / sqrt(x), with the same type as x.
Live example
Edit the code and the preview updates as you type.
How it works
inversesqrt(x) is 1.0 / sqrt(x) in a single call: inversesqrt(4.0) is 0.5.
Combined with dot(v, v) (the squared length) it gives 1.0 / length(v). In the example that value is the brightness of each light: very bright right next to the light and fading slowly with distance, which produces soft glowing orbs that add up where they overlap.
Typical uses
- Manual normalization:
v * inversesqrt(dot(v, v))is exactlynormalize(v). - Glows and falloff:
k * inversesqrt(dot(q, q))for a 1 / distance glow.
Common mistakes
- Passing 0 or a negative number. The result is undefined; add a small epsilon like
dot(q, q) + 1e-4. - Using it purely for speed. Modern GPUs implement
normalizeandlengthefficiently; prefer whichever reads more clearly.
Related functions
sqrt()
Returns the square root of x. For values between 0.0 and 1.0 it lifts the low end, rising fast and then flattening.
normalize()
Returns a vector pointing in the same direction as x but with length 1.0. Keeps the direction, drops the size.
length()
Returns the length of a vector, its distance from the origin. length(p) is the starting point for drawing circles.