Unity Shader Gotcha #8 - Custom Deferred Lighting
Saturday, May 18, 2013 at 9:05PM
1 Comment Background
This is not well-known, but Unity provides a way for you to specify a custom deferred lighting shader. All you have to do is download Unity's internal shaders collection, grab "Internal-PrePassLighting.shader", import it into your Unity project, then close and restart the editor. After this, Unity will now be using the version referenced in your project.
From here, you can modify the lighting equation inside the CalculateLight() function to change how it calculates the lighting contributions.
Problems
You are still limited to having the same input and output limitations as Unity's built-in deferred lighting shader. So that means you only have (normal XYZ, specular) in the range [0,1] as inputs, and (diffuse RGB, specular) as outputs. More importantly, this shader's output is shared by all material shaders. So changing it will make all of their outputs wrong, forcing you to have to write a complete set of replacement shaders that can work with your new custom deferred lighting results.
So this is not something for the faint of heart.
Benefits
Obviously the biggest benefit is being able to use a custom lighting function for deferred lighting. You can also change how the specular power input is interpreted, possibly packing more data into it or simply changing the mapping. Finally, you can change how the specular lighting is calculated so you can potentially use a better specular light color approximation in the material shaders. At the very least, you will probably want to fix the inadequacies of Unity's default lighting equation.
So if you are in any way serious about getting good lighting and materials out of Unity, this is the only way to go.
Unity3D