Deferred shader with custom lighting model

Could anyone show me an example deferred shader with a custom lighting model? Been trying to figure this out for a solid 8 hours. If anyone has one they’ve used before or just a barebones template, please let me take a look. I have a nice looking custom cel lighting model that I have with a forward shader right now and I’ld like to convert it so it can take all lights from all angles.

Every version I’ve attempted always ends up as a weird datamoshed bugged shader. Seems like there is not appropriate documentation for custom deferred shaders.

Deferred shading doesn’t have per-shader custom lighting models, because the lighting/shading is deferred until later. In your Project Settings>Graphics, you have to override the Deferred and Deferred Reflections with your own custom deferred shading/lighting model. You can find the default versions of those files if you go here: Download Archive and find the version of Unity you’re currently working with, click the Windows download dropdown button and you’ll see an option for “Built-in shaders”. In this zip file, there’s a folder called DefaultResourcesExtra which has the Internal-DeferredReflections.shader and Internal.DeferredShading.shader.

Modify those to get the kind of shading you want in your game.
If you want the ability to use either the regular shading model or this custom one, you can utilize the alpha channel of the _CameraGBufferTexture2 to do a branch in this file so that if the value is 0, it uses your custom shading model, and if 1 it uses the default Unity one that’s already in that file.

This has a varying performance impact depending on platform/hardware but it’s the only real choice short of rolling your own render pipeline (or doing something hacky like writing your custom shading to deferred emissive channel so the final result isn’t affected much by lighting, but then deferred lighting is still being processed on your buffers so you’re calculating two lighting models even though you only need one, which is pretty terrible).

8 Likes