Team Fortress 2 toon shader in Unity

TF2Shader

This was the first shader I wrote for Unity. I was always interested in toon shaders, and since they are very simple, I decided to start with one.

My inspiration came from the paper Valve published explaining the motivations behind their artistic choices for Team Fortress 2 and giving a technical description of their implementation. Basically, the artistic goal was to replicate commercial illustrations from the early 20th century, with rim highlights being heavily used to help players distinguish characters silhouettes .

The shader is composed by 5 terms:

  1. Warped diffuse: The toon shading lines are accomplished by using a ramp texture that is sampled according to the diffuse lighting (dot(N, L), mapped to the interval (0,1)). The authors call this the “warped diffuse” term.
  2. Ambient light: In TF2, the ambient light is calculated using a variation of irradiance environment maps. However, since implementing that ambient term in Unity would prove to be a challenge, I decided to replace it with a constant ambient term instead.
  3. Specular light: Traditional phong specular term.
  4. Phong rim light: Fresnel term multiplied by a specular term using a different exponent.
  5. Dedicated rim light: Another rim term, which is only applied on surfaces with upward-facing normals.

The CG code is pretty straightforward, and is fully listed bellow, with support for multiple lights:

As for the character models, they can be extracted from the game files, as described in this guide.

Leave a Reply

Your email address will not be published. Required fields are marked *

*