Cg Programming/Unity/Two-Sided Surfaces

This tutorial covers two-sided per-vertex lighting.

It's part of a series of tutorials about basic lighting in Unity. In this tutorial, we extend to render two-sided surfaces. If you haven't read, this would be a very good time to read it.

Two-Sided Lighting
As shown by the figure of the algebraic surface, it's sometimes useful to apply different colors to the two sides of a surface. In, we have seen how two passes with front face culling and back face culling can be used to apply different shaders to the two sides of a mesh. We will apply the same strategy here.

As mentioned in, an alternative approach in Cg is to use a fragment input parameter with semantic  to distinguish between the two sides, see Unity's documentation of shader semantics.

Shader Code
The shader code for two-sided per-vertex lighting is a straightforward extension of the code in. It requires two sets of material parameters (front and back) and duplicates all passes — one copy with front-face culling and the other with back-face culling. The shaders of the two copies are identical except that the shader for the back faces uses the negated surface normal vector and the properties for the back material.

This code consists of four passes, where the first pair of passes renders the front faces, and the second pair of passes renders the back faces using the negated normal vector and the back material properties. The second pass of each pair is the same as the first apart from the additive blending and the missing ambient color.

Summary
Congratulations, you made it to the end of this short tutorial with a long shader. We have seen:
 * How to use front-face culling and back-face culling to apply different shaders on the two sides of a mesh.
 * How to change the Phong lighting computation for back-facing triangles.