Unity Shader Primer

Shaders are one of the more obscure technologies within the Unity game engine, and while the documentation covers most of the shader language and functions it fails to provide comprehensive techniques that would result in compelling graphics. They’re core focus appears to be that of treating shaders as a black box with which materials are constructed and applied to meshes and while this allows for quick iteration and prototyping of mechanics it fails to provide an intuitive means by which a game designer can move past hobbyist levels of production value.Unity’s shader language, ShaderLab , is based on the Nvidia CG Language which is similar in structure to C making it somewhat obscure when it comes to translating graphical concepts to a visible program. As such in this blog post I’ll be detailing and dissecting several shaders I’ve developed after doing my own research into the subject.

Setup:

 

test_scene

To test Unity 5.x shaders I’ve opted to first construct a scene complete with the new physically based rendering bounce lighting effect. To set this up I’ve first constructed some level geometry consisting of four walls, each a separate color, and a floor. The bounce lighting is visible on the floor geometry due to  baked light maps Unity 5.x generates based on meshes flagged as being static in the scene. I am also utilizing the default blue skybox for global illumination.

 

Global Illumination

lightsettings

To achieve these lightmap results the following setup is used. The most important portion of these settings is the Precomputed Realtime GI which controls the amount of processing the CPU will use to construct the lightmaps.

The General GI settings contains the values that affect the lighting being simulated in your scene as they influence the lightmap. The Intensity and Boost values scales the effect of each bounced light ray on a surface, when varied can create some interesting results.

The atlas size is used to control the size of the baked image, the larger the image the more GPU memory your game will require.

That should cover the bulk of the scene setup, it should be noted that I have my player settings setup for forward rendering since none of these examples will require using the stencil shader or altering shadows.