About Me

Hi. I'm Josh Ols. Lead Graphics Developer for RUST LTD.

Contact:
crunchy.bytes.blog[at]gmail[dot]com

View Joshua Ols's profile on LinkedIn

Meta
« Alloy Anouncement | Main | Unity Shader Gotcha #7 - World Space & Detail Textures »
Saturday
May182013

Unity Shader Gotcha #8 - Custom Deferred Lighting

UPDATE 3

This article is now obselete, as Unity 5 provides a menu for explicitly selecting a deferred override shader to be included in the project.

 

UPDATE 2

Holy crap I wish I knew this sooner! Apparently the custom deferred lighting file doesn't get included in builds unless you put it in a folder called "Resources" somewhere in your Assets heirarchy. That explains so much. :(

http://forum.unity3d.com/threads/170532-Modified-Internal-PrePassLighting-isn-t-used-in-webplayer

 

UPDATE

Some things I discovered that may be of use to others. Number one, it is possible to assign default textures to texture parameters on shaders. So you could potentially use that system to get texture lookup tables into the deferred lighting shader.

Also, it is possible to use include files in the deferred lighting shader, which means you can share code and use configuration flags in a centralized way. 

 

ORIGINAL

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.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (17)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Great Site, Continue the very good job. Thanks a ton.
  • Response
    Response: fLpqOstg
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Jim Glover Chevrolet
  • Response
    Response: gm transmission
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Response: muscle roller
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Response: techa.bl.ee
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Response: pimpblog.nl
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting
  • Response
    Response: Dax-Futers
    Crunchy Bytes - Journal - Unity Shader Gotcha #8 - Custom Deferred Lighting

Reader Comments (2)

A thing to note is that it only works in the editor, not in a build, because the shader is not referenced from your scene. To make it work in a build, just add a reference to the shader from anywhere in the scene - either by using it in a material, or by creating a script that holds a reference to the shader.
May 30, 2013 | Unregistered Commenterpixelmager
Wouldn't adding the shader to the Resources folder (in the root of assets) solve the build problem?
September 4, 2013 | Unregistered CommenterSven

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.