# GLSL 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.)
The tutorial is based on the code of Section “Smooth Specular Highlights” and Section “Transparent Textures”. 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 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 to the left.

### Unity's Cookies

In Unity, a cookie can be specified for each light source in the Inspector View when the light source is selected. This cookie is basically an alpha texture map (see Section “Transparent Textures”) 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 Section “Projectors”.) 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 Section “Smooth Specular Highlights”; 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 Section “Vertex Transformations”. 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 _LightMatrix0. (Otherwise we would have to set up the matrix similar to the matrices for the viewing transformation and the projection, which are discussed in Section “Vertex Transformations”.)
For best efficiency, the transformation of the surface point from world space to light space should be performed by multiplying _LightMatrix0 to the position in world space in the vertex shader, for example this way:
Apart from the definitions of the uniform _LightMatrix0 and the varying positionInLightSpace and the instruction to compute positionInLightSpace, this is the same vertex shader as in Section “Smooth Specular Highlights”.

### Cookies for Directional Light Sources

For the cookie of a directional light source, we can just use the x and y light coordinates in positionInLightSpace as texture coordinates for a lookup in the cookie texture _LightTexture0. This texture lookup should be performed in the fragment shader. Then the resulting alpha component should be multiplied to the computed lighting; for example:
Instead of vec2(positionInLightSpace) we could also use positionInLightSpace.xy to get a two-dimensional vector with the x and y coordinates in light space.

### Cookies for Spotlights

For spotlights, the x and y light coordinates in positionInLightSpace 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 Section “Vertex Transformations”. Unity defines the matrix _LightMatrix0 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 texture2DProj, which takes three texture coordinates in a vec3 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 positionInLightSpace.w; however, texture2DProj doesn't allow us to add anything after the internal division by the third texture coordinate. The solution is to add 0.5 * positionInLightSpace.w before the division by positionInLightSpace.w, which corresponds to adding $0.5$ after the division:
Note that the texture lookup for directional lights can also be implemented with texture2DProj by setting textureCoords to vec3(vec2(positionInLightSpace), 1.0). This would allow us to use only one texture lookup for both directional lights and for spotlights, which is more efficient on some GPUs.

### Complete Shader Code

For the complete shader code we use a simplified version of the ForwardBase pass of Section “Smooth Specular Highlights” since Unity only uses a directional light without cookie in the ForwardBase pass. All light sources with cookies are handled by the ForwardAdd pass. We ignore cookies for point lights, for which _LightMatrix0[3][3] is 1.0 (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 ForwardBase pass. Thus, unless there are more than one directional light sources without cookies, we can assume that all directional light sources in the ForwardAdd 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 ForwardAdd passes for different light sources. (Remember that the light source in the ForwardBase 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 GLSLPROGRAM in the ForwardAdd pass):
#pragma multi_compile_lightpass
With this instruction, Unity will compile the shader code for the ForwardAdd pass multiple times for different kinds of light sources. Each compilation is distinguished by the definition of one of the following symbols: DIRECTIONAL, DIRECTIONAL_COOKIE, POINT, POINT_NOATT, POINT_COOKIE, SPOT. The shader code should check which symbol is defined (using the directives #if defined ... #elif defined ... #endif) 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 Section “Reflecting Surfaces”.

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

### Further Reading

If you still want to know more
* about the shader version for lights without cookies, you should read Section “Smooth Specular Highlights”.
* about texture mapping and in particular alpha texture maps, you should read Section “Transparent Textures”.
* about projective texture mapping in fixed-function OpenGL, you could read NVIDIA's white paper “Projective Texture Mapping” by Cass Everitt (which is available online).
< GLSL Programming/Unity