GLSL Programming/Unity/Glossy Textures

This tutorial covers per-pixel lighting of partially glossy, textured surfaces.

It combines the shader code of and  to compute per-pixel lighting with a material color for diffuse reflection that is determined by the RGB components of a texture and an intensity of the specular reflection that is determined by the A component of the same texture. If you haven't read those sections, this would be a very good opportunity to read them.

Gloss Mapping
In, the material constant for the diffuse reflection was determined by the RGB components of a texture image. Here we extend this technique and determine the strength of the specular reflection by the A (alpha) component of the same texture image. Using only one texture offers a significant performance advantage, in particular because an RGBA texture lookup is under certain circumstances just as expensive as an RGB texture lookup.

If the “gloss” of a texture image (i.e. the strength of the specular reflection) is encoded in the A (alpha) component of an RGBA texture image, we can simply multiply the material constant for the specular reflection $$k_\text{specular}$$ with the alpha component of the texture image. $$k_\text{specular}$$ was introduced in and appears in the specular reflection term of the Phong reflection model:

$$I_\text{specular} = I_\text{incoming}\,k_\text{specular} \max(0, \mathbf{R}\cdot \mathbf{V})^{n_\text{shininess}}$$

If multiplied with the alpha component of the texture image, this term reaches its maximum (i.e. the surface is glossy) where alpha is 1, and it is 0 (i.e. the surface is not glossy at all) where alpha is 0.



Shader Code for Per-Pixel Lighting
The shader code is a combination of the per-pixel lighting from and the texturing from. Similarly to, the RGB components of the texture color in  is multiplied to the diffuse material color.

In the particular texture image, the alpha component is 0 for water and 1 for land. However, it should be the water that is glossy and the land that isn't. Thus, with this particular image, we should multiply the specular material color with. On the other hand, usual gloss maps would require a multiplication with. (Note how easy it is to make this kind of changes to a shader program.)

A useful modification of this shader for the particular texture image above, would be to set the diffuse material color to a dark blue where the alpha component is 0.

Shader Code for Per-Vertex Lighting
As discussed in, specular highlights are usually not rendered very well with per-vertex lighting. Sometimes, however, there is no choice because of performance limitations. In order to include gloss mapping in the shader code of, the fragment shaders of both passes should be replaced with this code:

Note that a usual gloss map would require a multiplication with  instead of.

Summary
Congratulations! You finished an important tutorial about gloss mapping. We have looked at:
 * What gloss mapping is.
 * How to implement it for per-pixel lighting.
 * How to implement it for per-vertex lighting.