• Create Account

Banner advertising on our site currently available from just \$5!

### #ActualHodgman

Posted 08 June 2013 - 02:08 AM

The straightforward way to have coloured lights would be:
vec3 diffuse = vec3(0);
...
diffuseDiff = ...N.L * attenuation...
diffuse += diffuseDiff * u_LightColours[i].rgb;
//OR if you really want to keep colour and intensity separate, instead of pre-multiplying them on the CPU:
diffuse += (diffuseDiff * u_LightColours[i].a) * u_LightColours[i].rgb;//n.b. scalar mul first, then scalar/vector mul
...
gl_FragColor.rgb = diffuseTexture * diffuse;
If you want to use an if statement to only compute lighting within a certain radius, you've also got to modify your attenuation function so that it does actually reach zero by that radius. There's an example at the bottom of this blog post: http://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/

Keep in mind though that older GPUs do not deal well with branching. The GPU will typically working on a large number of pixels at once (e.g. 64) and if any of them take a certain branch, then all of them pay the cost of executing that branch. The branch instruction itself may also be expensive, regardless of which path is taken (e.g. a dozen cycles -- so if the branch is not skipping more than a dozen basic math operations in the average case, it may not be worth it).
As with any kind of optimisation, you should be sure to profile before and after in order to be sure that it's actually helping.

### #2Hodgman

Posted 08 June 2013 - 02:06 AM

The straightforward way to have coloured lights would be:

vec3 diffuse = vec3(0);
...
diffuseDiff = ...N.L * attenuation...
diffuse += diffuseDiff * u_LightColours[i];
...
gl_FragColor.rgb = diffuseTexture * diffuse;

If you want to use an if statement to only compute lighting within a certain radius, you've also got to modify your attenuation function so that it does actually reach zero by that radius. There's an example at the bottom of this blog post: http://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/

Keep in mind though that older GPUs do not deal well with branching. The GPU will typically working on a large number of pixels at once (e.g. 64) and if any of them take a certain branch, then all of them pay the cost of executing that branch. The branch instruction itself may also be expensive, regardless of which path is taken (e.g. a dozen cycles -- so if the branch is not skipping more than a dozen basic math operations in the average case, it may not be worth it).
As with any kind of optimisation, you should be sure to profile before and after in order to be sure that it's actually helping.

### #1Hodgman

Posted 08 June 2013 - 02:04 AM

The straightforward way to have coloured lights would be:

vec3 diffuse = vec3(0);
...
diffuse += diffuseDiff * u_LightColours[i];
gl_FragColor.rgb = diffuseTexture * diffuse;

If you want to use an if statement to only compute lighting within a certain radius, you've also got to modify your attenuation function so that it does actually reach zero by that radius. There's an example at the bottom of this blog post: http://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/

Keep in mind though that older GPUs do not deal well with branching. The GPU will typically working on a large number of pixels at once (e.g. 64) and if any of them take a certain branch, then all of them pay the cost of executing that branch. The branch instruction itself may also be expensive, regardless of which path is taken (e.g. a dozen cycles -- so if the branch is not skipping more than a dozen basic math operations in the average case, it may not be worth it).
As with any kind of optimisation, you should be sure to profile before and after in order to be sure that it's actually helping.

PARTNERS