I am playing with Lesson 12 (Ambient And Point Lighting) from the WebGL Tutorials provided at:


I adapted the example to my own use but I noticed that the lighting does not drop off over distance. This is not a problem in a crowded scene where the light is likely to end up hitting something before traveling any significant distance but in a more open scene it is a problem. Think of a candle in a large room. The candle is bright and will illuminate objects near it but only a short distance away and objects are almost not illuminated at all.

For the lesson it seems that the calculation to obtain the light on any particular fragment is done (on the shader) using:

Code :
vec3 lightDirection = normalize(uPointLightingLocation - mvPosition.xyz);
vec3 transformedNormal = uNMatrix * aVertexNormal;
float directionalLightWeighting = max(dot(transformedNormal, lightDirection), 0.0);
vLightWeighting = uAmbientColor + uPointLightingColor * directionalLightWeighting;

So it basically adds the ambient component plus the point light contribution which is based on the point light and its angle to the fragment. :P

To me it seems that if I multiply the point light contribution by some fractional factor based on the distance between the point light and the fragment then I can make the light intensity drop off the further it is from the point light source.

The distance, if I understand things correctly, should be the magnitude of uPointLightingLocation - mvPosition.xyz.

However, I am not sure what the shader language command for getting the magnitude of a vector is.

If anyone knows this, please let me know. It would be very helpful.