Search Unity

DepthMask shader for Unity 5 problem

Discussion in 'Shaders' started by SunnySunshine, May 1, 2015.

  1. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    976
    The depth mask shader from unity wiki seems to write background color over skybox.

    Code (CSharp):
    1. Shader "Masked/Mask" {
    2.     SubShader {
    3.         // Render the mask after regular geometry, but before masked geometry and
    4.         // transparent things.
    5.         Tags {"Queue" = "Geometry+10" }
    6.         // Don't draw in the RGBA channels; just the depth buffer
    7.         ColorMask 0
    8.         ZWrite On
    9.         // Do nothing specific in the pass:
    10.         Pass {}
    11.     }
    12. }
    http://wiki.unity3d.com/index.php/DepthMask



    Is there a way to prevent this?
     
    mekartikshah and Hamisam236 like this.
  2. jgb143

    jgb143

    Joined:
    Jun 8, 2010
    Posts:
    132
    I had the same question. It seems to work ok in Scene View but not in Game view. Any solutions?
     
  3. landskippedd

    landskippedd

    Joined:
    Jan 7, 2015
    Posts:
    1
    Hey guys,

    I know basically nothing about shaders but I've found two easy-ish ways to prevent this:

    1. Set your main camera to clear depth only, then add a second camera that clears skybox but has a culling mask of nothing. This is the most straightforward solution but is maybe a little resource intensive?

    2. I managed to wrangle a single camera solution using the stencil buffer:

    By adding this code to my depth mask:

    Code (CSharp):
    1.  
    2.   Stencil
    3.      {
    4.        Ref 0
    5.        Comp always
    6.        Pass replace
    7.      }
    This to my standard material shader:

    Code (CSharp):
    1.         Stencil
    2.         {
    3.             Ref 1
    4.             Comp always
    5.             Pass replace
    6.         }
    And this to the skybox shader:

    Code (CSharp):
    1.         Stencil
    2.     {
    3.         Ref 1
    4.         Comp notequal
    5.         Pass keep
    6.     }
    As far as I can tell with my (extremely) limited knowledge - this is kind of like setting everything that uses the standard shader to one layer then telling the skybox to render over everything else?

    Hope that helps!

     
  4. Drowning-Monkeys

    Drowning-Monkeys

    Joined:
    Mar 6, 2013
    Posts:
    328
    bump, this problem with a regular depth mask (not a stencil) still exists. Is there any solution?
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    The skybox gets rendered after alpha test queues, and the game view skybox uses the depth buffer to composite into the view. The only fix is to render the mask after the skybox is rendered, which means after alpha test. If you really need this you'll likely have to render your alpha tested shaders in the early transparent queue, or render the skybox using a second camera to force it to render prior to opaque.

    Basic setup for using two cameras is have a camera that's a child of your main camera with it's "depth" set to less than the parent camera and with its culling mask set to Nothing. Then set the parent camera's clear flags to Depth Only or Don't Clear.

    Neither method is particularly great.
     
    mekartikshah likes this.
  6. Drowning-Monkeys

    Drowning-Monkeys

    Joined:
    Mar 6, 2013
    Posts:
    328
    @bgolus how would i change the shader to render after alpha test? (Just to test it out).

    fortunately, i only need the depth buffer trick for a few seconds, so i can always turn on the 2nd camera, switch up their clear flags, then reset it.
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    You need a queue of 2501 or higher. Geometry is 2000, AlphaTest is 2450 and Transparency is 3000. The skybox is rendered after 2500 and before 2501, so:

    Tags { "Queue"="Geometry+501" }
    or
    Tags { "Queue"="AlphaTest+51" }
    or
    Tags { "Queue"="Transparency-499" }
     
  8. Drowning-Monkeys

    Drowning-Monkeys

    Joined:
    Mar 6, 2013
    Posts:
    328
    boom, that did it. Thanks @bgolus
     
  9. Snipe3000

    Snipe3000

    Joined:
    Aug 31, 2013
    Posts:
    8
    When looking through the depth mask, do the objects on the other side still have their shaders? Mine will not show through the depth Mask
     
  10. DanTaylor

    DanTaylor

    Joined:
    Jul 6, 2013
    Posts:
    119
    Thanks - that solved a tricky problem for me. Nice one! :D