Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been using this script and shader from the wiki, and they work wonders; my only problem is using them in deferred rendering.

Now, the way this shader works is by writing to the depth buffer to hide anything behind an object with this shader applied, but for some reason this wasn't working when I tried to use deferred rendering for shadows and better lighting. The problem is addressed by Kuba in the final post of this topic.

The final pass of prepass rendering (the part that renders geometry the second time and applies the result of the lighting pass to it) is rendered in to a texture, then blit-copied into the back buffer, which overrides everything that was rendered there before.

My question is if there is any way to render after that final pass, so I can still use my depth mask to hide objects behind it. If there is no way to directly write after the final pass, is there another solution I can try? Perhaps blending the two cameras on my own and some how blitting that on to the screen?

share|improve this question
up vote 0 down vote accepted

I actually got it working with this shader, this works on everything with the scene, no need for setting any render queues.

I made this in Strumpy Shader Editor, seeing as I don't have too much shader writing experience. All I had to do was turn off the RGBA on the color mask, set render queue to Geometry -10, and check the advanced shadows pass box (which adds in the bit below that renders after the final pass.)

 Shader "DepthMask/Mask"




Cull Back
ZWrite On
ZTest LEqual
ColorMask 0

#pragma surface surf BlinnPhongEditor  addshadow vertex:vert
#pragma target 2.0

            struct EditorSurfaceOutput {
                half3 Albedo;
                half3 Normal;
                half3 Emission;
                half3 Gloss;
                half Specular;
                half Alpha;
                half4 Custom;

            inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
half3 spec = light.a * s.Gloss;
half4 c;
c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
c.a = s.Alpha;
return c;


            inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
                half3 h = normalize (lightDir + viewDir);

                half diff = max (0, dot ( lightDir, s.Normal ));

                float nh = max (0, dot (s.Normal, h));
                float spec = pow (nh, s.Specular*128.0);

                half4 res;
                res.rgb = _LightColor0.rgb * diff;
                res.w = spec * Luminance (_LightColor0.rgb);
                res *= atten * 2.0;

                return LightingBlinnPhongEditor_PrePass( s, res );

            struct Input {
                float4 color : COLOR;


            void vert (inout appdata_full v, out Input o) {
float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);


            void surf (Input IN, inout EditorSurfaceOutput o) {
                o.Normal = float3(0.0,0.0,1.0);
                o.Alpha = 1.0;
                o.Albedo = 0.0;
                o.Emission = 0.0;
                o.Gloss = 0.0;
                o.Specular = 0.0;
                o.Custom = 0.0;

float4 Master0_0_NoInput = float4(0,0,0,0);
float4 Master0_1_NoInput = float4(0,0,1,1);
float4 Master0_2_NoInput = float4(0,0,0,0);
float4 Master0_3_NoInput = float4(0,0,0,0);
float4 Master0_4_NoInput = float4(0,0,0,0);
float4 Master0_5_NoInput = float4(1,1,1,1);
float4 Master0_7_NoInput = float4(0,0,0,0);
float4 Master0_6_NoInput = float4(1,1,1,1);

                o.Normal = normalize(o.Normal);
    Fallback "Diffuse"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.