Search Unity

VR, Post-processing, vertex/frag shader, single pass stereo, instancing, not writing to depth.

Discussion in 'Image Effects' started by Alex_May, Jun 10, 2017.

  1. Alex_May

    Alex_May

    Joined:
    Dec 29, 2013
    Posts:
    198
    Well, it's just like the title says.

    What do you mean it doesn't make sense?

    OK here's what I'm doing. I've got a post-processing effect that draws fog. We're rendering in forward path, using single-pass stereo. This shader is for drawing instanced meshes, and I recently converted it from a surface shader to a vert/frag shader.

    Trouble is, now the instanced meshes don't seem to be writing to the depth buffer, even though ZWrite is on. Objects that are near to the camera are visible but only if they are in front of some nearby geometry. If they're in front of something far away, they disappear into the fog. Says to me that they're not writing to depth.

    Surface shaders with the exact same vertex program still work fine in the same situation. I note that the manual says that depth is written in the shadowcaster pass, but my vert/frag shader falls back to diffuse so that should be ok?

    Anyone experienced anything like this?
     
  2. equalsequals

    equalsequals

    Joined:
    Sep 27, 2010
    Posts:
    154
    Have you attempted to write a Pass to handle the ShadowCaster pass explicitly?

    Code (csharp):
    1.  
    2.         Pass
    3.         {
    4.             Name "ShadowCaster"
    5.             Tags{ "LightMode" = "ShadowCaster" }
    6.  
    7.             CGPROGRAM
    8.             #pragma vertex vert
    9.             #pragma fragment frag
    10.             #pragma target 2.0
    11.             #pragma multi_compile_shadowcaster
    12.             #include "UnityCG.cginc"
    13.  
    14.             struct a2v_shadow
    15.             {
    16.                 float4 vertex : POSITION;
    17.                 float3 normal : NORMAL;
    18.             };
    19.             struct v2f_shadow
    20.             {
    21.                 V2F_SHADOW_CASTER;
    22.             };
    23.  
    24.             v2f_shadow vert(a2v_shadow v)
    25.             {
    26.                 v2f_shadow o;
    27.                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    28.                 return o;
    29.             }
    30.  
    31.             float4 frag(v2f_shadow i) : SV_Target
    32.             {
    33.                 SHADOW_CASTER_FRAGMENT(i)
    34.             }
    35.             ENDCG
    36.         }
    37.