Cg Programming/Unity/Cookies

This tutorial covers projective texture mapping in light space, which is useful to implement cookies for spotlights and directional light sources. (In fact, Unity uses a built-in cookie for any spotlight.) In Unity, many of the built-in shaders can deal with cookies; this tutorial shows how this works.

The tutorial is based on the code of and. If you haven't read those tutorials yet, you should read them first.

Gobos and Cookies in Real Life
In real life, gobos are pieces of solid material (often metal) with holes that are placed in front of light sources to manipulate the shape of light beams or shadows. Cookies (or “cuculoris”) serve a similar purpose but are placed at a larger distance from the light source as shown in the image.

Unity's Cookies
In Unity, a cookie can be specified for each light source in the Inspector Window when the light source is selected. This cookie is basically an alpha texture map (see ) that is placed in front of the light source and moves with it (therefore it is actually similar to a gobo). It lets light pass through where the alpha component of the texture image is 1 and blocks light where the alpha component is 0. Unity's cookies for spotlights and directional lights are just square, two-dimensional alpha texture maps. On the other hand, cookies for point lights are cube maps, which we will not cover here.

In order to implement a cookie, we have to extend the shader of any surface that should be affected by the cookie. (This is very different from how Unity's projectors work; see .) Specifically, we have to attenuate the light of each light source according to its cookie in the lighting computation of a shader. Here, we use the per-pixel lighting described in ; however, the technique can be applied to any lighting computation.

In order to find the relevant position in the cookie texture, the position of the rasterized point of a surface is transformed into the coordinate system of the light source. This coordinate system is very similar to the clip coordinate system of a camera, which is described in. In fact, the best way to think of the coordinate system of a light source is probably to think of the light source as a camera. The x and y light coordinates are then related to the screen coordinates of this hypothetical camera. Transforming a point from world coordinates to light coordinates is actually very easy because Unity provides the required 4×4 matrix as the uniform variable. (Otherwise we would have to set up the matrix similar to the matrices for the viewing transformation and the projection, which are discussed in .)

For best efficiency, the transformation of the surface point from world space to light space should be performed by multiplying  to the position in world space in the vertex shader, for example this way: Apart from the definitions of the uniform  and the new output parameter   and the instruction to compute , this is the same vertex shader as in.

Cookies for Directional Light Sources
For the cookie of a directional light source, we can just use the x and y light coordinates in  as texture coordinates for a lookup in the cookie texture. This texture lookup should be performed in the fragment shader. Then the resulting alpha component should be multiplied to the computed lighting; for example:

Cookies for Spotlights
For spotlights, the x and y light coordinates in  have to be divided by the w light coordinate. This division is characteristic for projective texture mapping and corresponds to the perspective division for a camera, which is described in. Unity defines the matrix  such that we have to add $$0.5$$ to both coordinates after the division: For some GPUs it might be more efficient to use the built-in function, which takes three texture coordinates in a   and divides the first two coordinates by the third coordinate before the texture lookup. A problem with this approach is that we have to add $$0.5$$ after the division by ; however,   doesn't allow us to add anything after the internal division by the third texture coordinate. The solution is to add  before the division by , which corresponds to adding $$0.5$$ after the division: Note that the texture lookup for directional lights can also be implemented with  by setting   to. This would allow us to use only one texture lookup for both directional lights and for spotlights, which is more efficient on some GPUs.

Sometimes, projective texture mapping comes with an unpleasant side effect: at the edges of the projection, the GPU uses a high mip map level, which can result in a visible border (in particular for texture maps with clamped texture coordinates). The easiest way to avoid this, is to deactivate mip maps for the texture image: find and select the texture image in the Project Window; then in the Inspector Window set Texture Type to Advanced and uncheck Generate Mip Maps. Don't forget to click the Apply button.

Complete Shader Code
For the complete shader code we use a simplified version of the  pass of  since Unity only uses a directional light without cookie in the   pass. All light sources with cookies are handled by the  pass. We ignore cookies for point lights, for which  is   (but we include them in the next section). Spotlights always have a cookie texture: if the user didn't specify one, Unity supplies a cookie texture to generate the shape of a spotlight; thus, it is OK to always apply the cookie. Directional lights don't always have a cookie; however, if there is only one directional light source without cookie then it has been processed in the  pass. Thus, unless there are more than one directional light sources without cookies, we can assume that all directional light sources in the  pass have cookies. In this case, the complete shader code could be:

Shader Programs for Specific Light Sources
The previous shader code is limited to scenes with at most one directional light source without a cookie. Also, it doesn't take cookies of point light sources into account. Writing more general shader code requires different  passes for different light sources. (Remember that the light source in the  pass is always a directional light source without cookie.) Fortunately, Unity offers a way to generate multiple shaders by using the following Unity-specific directive (right after   in the   pass):

With this instruction, Unity will compile the shader code for the  pass multiple times for different kinds of light sources. Each compilation is distinguished by the definition of one of the following symbols:,  ,  ,  ,  ,. The shader code should check which symbol is defined (using the directives ) and include appropriate instructions. For example: Note that the cookie for a point light source is using a cube texture map. This kind of texture map is discussed in.

Summary
Congratulations, you have learned the most important aspects of projective texture mapping. We have seen:
 * How to implement cookies for directional light sources.
 * How to implement spotlights (with and without user-specified cookies).
 * How to implement different shaders for different light sources.