2.0.0 • Published 10 years ago
glsl-light-attenuation v2.0.0
glsl-light-attenuation
GLSL function for computing light attenuation. Designed for use as a shader component with glslify and glsl-light.
install
To make avaialble in your project
npm install glsl-light-attenuation --saveexample
Define a light in your shader
pragma glslify: Light = require('glsl-light')
uniform Light light;Then use this function alongside glsl-light-direction to compute attenuation
pragma glslify: attenuation = require('glsl-light-attenuation')
pragma glslify: direction = require('glsl-light-direction')
vec3 dir = direction(light, position);
float attn = attenuation(light, dir);algorithm
Attenuation is computed treating the light as either point or directional depending on its properties. For a good overview, see this tutorial.
The primary parameter of the light is the position, a vec4 in homogenous coordinates. The fourth element determines whether the light is directional or not.
- If the fourth element is
0.0, it will produce directional light, and attenuation is 1.0. - If the fourth element is
1.0, it will be treated as a point light source, and attenuation will falloff as governed by theradiusparameter, with the functionpow(clamp(1.0 - distance / radius, 0.0, 1.0), 2.0).
API
attenuation(light, direction)
Parameters
light:structinstance ofglsl-scene-lightdirection:vec3direction of the light
Returns
floatthe computed color intensity for the material