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  pass.

This tutorial is an extension of. If you haven't read that tutorial, you should read it first.

Multiple Lights in One Pass
As discussed in, 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  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  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  by initializing it to black and then adding the diffuse reflection of each vertex light to the previous value of   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, the complete shader code is: The use of  and   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.