How to Explore Unity 5's Shader System Code - I - Overview

Unity is a very flexible tool for exploring shader development.

The hassle compared with, for example, what Unreal 4 makes you go through to write a proper custom shader, is minimal (proper = can't be done in the node editor, needs code).
In defence of Unreal 4, being based solely on a deferred renderer means that we can't play as much as we could with a forward one, limited as we are by the information the g-buffer contains, which has been chosen for us, and we can't change.

So let's say that, while you are generally satisfied with Unity 5's Standard shader, you want to add something to it, an extra shader property maybe, or you want to modify some specific piece of functionality.

Or that you want to make your own Shader System from scratch, for which you need to hook into the shadows, global illumination, lightmaps, direct lighting, and more.

The main problem to editing the Standard Shader is that you can't do it.
You can't modify the Standard Shader code directly. You have to first download the Shader code for the version of Unity you have installed from the Unity website.

Get the Standard Shader Code

To do that, check the version of Unity 5 you have installed, then go to the Unity Download Archive and choose from the downloads dropdown appropriate to your platform (Win/Mac) "Built in shaders".
You'll get a zip file, unzip it somewhere and we'll get on to exploring the content.

Overview

What the zip contains is the entire source code of the Standard Shader, including the special inspector UI, and all the includes.

Four folders:

  • CGIncludes
  • DefaultResources
  • DefaultResourcesExtra
  • Editor

Editor contains the only .cs file needed to implement the Standard Shader inspector UI.

CGIncludes contains files containing functions used by all other shaders. We'll take a proper look at this, as we're going to use those ourselves.

DefaultResources and DefaultResourcesExtra contains a whole lot of different shaders used in different situations.

In the next posts we're going to learn how to read the Standard Shader, and then we'll look at the subsystems one by one, direct lighting, shadows, global illumination, etc.