glsl-light-attenuation
v2.0.0
Published
GLSL function for computing light attenuation
Downloads
9
Maintainers
Readme
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 --save
example
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 theradius
parameter, with the functionpow(clamp(1.0 - distance / radius, 0.0, 1.0), 2.0)
.
API
attenuation(light, direction)
Parameters
light
:struct
instance ofglsl-scene-light
direction
:vec3
direction of the light
Returns
float
the computed color intensity for the material