GLSL Programming/Unity/Lighting Textured Surfaces

This tutorial covers per-vertex lighting of textured surfaces.

It combines the shader code of and  to compute lighting with a diffuse material color determined by a texture. If you haven't read those sections, this would be a very good opportunity to read them.

Texturing and Diffuse Per-Vertex Lighting
In, the texture color was used as output of the fragment shader. However, it is also possible to use the texture color as any of the parameters in lighting computations, in particular the material constant $$k_\text{diffuse}$$ for diffuse reflection, which was introduced in. It appears in the diffuse part of the Phong reflection model:

$$I_\text{diffuse} = I_\text{incoming}\,k_\text{diffuse} \max(0,\mathbf{N}\cdot \mathbf{L})$$

where this equation is used with different material constants for the three color components red, green, and blue. By using a texture to determine these material constants, they can vary over the surface.

Shader Code
In comparison to the per-vertex lighting in, the vertex shader here computes two varying colors:  is multiplied with the texture color in the fragment shader and   is just the specular term, which shouldn't be multiplied with the texture color. This makes perfect sense but for historically reasons (i.e. older graphics hardware that was less capable) this is sometimes referred to as “separate specular color”; in fact, Unity's ShaderLab has an option called “SeparateSpecular” to activate or deactivate it.

Note that a property  is included, which is multiplied (component-wise) to all parts of the  ; thus, it acts as a useful color filter to tint or shade the texture color. Moreover, a property with this name is required to make the fallback shader work (see also the discussion of fallback shaders in ).

In order to assign a texture image to this shader, you should follow the steps discussed in.

Summary
Congratulations, you have reached the end. We have looked at:
 * How texturing and per-vertex lighting are usually combined.
 * What a “separate specular color” is.