Basic Unity Shader Set



As an exercise to expand my knowledge of custom shaders, I wrote a series of basic shaders from scratch in Cg for Unity, following this series of tutorials by Alex Telford from CG Cookie.


The image above shows the progression (from upper left to lower right) as I incrementally added more features to the shader. The initial version is simply an unlit model that always returns the same color from its fragment shader. On top of this I added Lambertian shading with support for directional and ambient light.

When adding the specular highlights, I started with a simple vertex lit model and all the lighting calculations contained in the vertex shader. I then restructured this to be a pixel lit shader, resulting in more even, smooth highlights, and added rim lighting. Up to this point the shader only worked with a single directional light, so I expanded it to include multiple passes blended together, and added support for point lights.

The color, normal, specular and emissive maps are all applied in fairly standard ways. The color and emissive maps, as well as ambient lighting, are only applied in the first pass to avoid applying them twice and altering the colors.


This series of shaders was a good exercise in the fundamentals and building muscle memory for writing Cg code. There are some other features I'd like to add, and also optimizations, so to solidify my knowledge of this I plan to start over from scratch on another shader incorporating all these features and more, such as transparency and outlines.