[<< wikibooks] Cg Programming/Unity/Multiple Lights
This tutorial covers lighting by multiple light sources in one pass. In particular, it covers Unity's so-called “vertex lights” in the ForwardBase pass.
This tutorial is an extension of Section “Smooth Specular Highlights”. If you haven't read that tutorial, you should read it first.


== Multiple Lights in One Pass ==
As discussed in Section “Diffuse Reflection”, Unity's forward rendering path uses separate passes for the most important light sources. These are called “pixel lights” because the built-in shaders render them with per-pixel lighting. All light sources with the Render Mode set to Important are rendered as pixel lights. If the Pixel Light Count of the Quality project settings allows for more pixel lights, then some of the light sources with Render Mode set to Auto are also rendered as pixel lights. What happens to the other light sources? The built-in shaders of Unity render four additional lights as vertex lights in the ForwardBase pass. As the name indicates, the built-in shaders render these lights with per-vertex lighting. This is what this tutorial is about. (Further lights are approximated by spherical harmonic lighting, which is not covered here.)
In the ForwardBase pass, the four vertex lights (i.e. their positions and colors) are specified by the following built-in uniforms (see Unity's documentation of built-in shader variables): 

We follow Unity's built-in shaders and only compute the diffuse reflection by vertex lights (assuming they are point lights) using per-vertex lighting. This can be computed with the following for-loop inside the vertex shader:

The total diffuse lighting by all vertex lights is accumulated in vertexLighting by initializing it to black and then adding the diffuse reflection of each vertex light to the previous value of vertexLighting at the end of the for-loop. A for-loop should be familiar to any C/C++/Java/JavaScript programmer. Note that for-loops in Cg are severely limited for some GPUs; in particular the limits (here: 0 and 4) have to be constants for some GPUs, i.e. you cannot even use uniforms to determine the limits. (The technical reason is that the limits have to be known at compile time in order to “un-roll” the loop.)
This is more or less how vertex lights are computed in Unity's built-in shaders. However, remember that nothing would stop you from computing specular reflection or per-pixel lighting with these “vertex lights”.


== Complete Shader Code ==
In the context of the shader code from Section “Smooth Specular Highlights”, the complete shader code is:

The use of #pragma multi_compile_fwdbase and #ifdef VERTEXLIGHT_ON ... #endif is necessary to make sure that no vertex lighting is computed when Unity doesn't provide the data; see also Unity's documentation of multi_compile directives.


== Summary ==
Congratulations, you have reached the end of this tutorial. We have seen:

How Unity's vertex lights are specified.
How a for-loop can be used in Cg to compute the lighting of multiple lights in one pass.


== Further reading ==
If you still want to know more 

about other parts of the shader code, you should read Section “Smooth Specular Highlights”.
about Unity's forward rendering path and what is computed in the ForwardBase pass, you should read Unity's reference about forward rendering.< Cg Programming/Unity