Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Standard Shader with stencil mask.

Discussion in 'Shaders' started by The-Oddler, Mar 8, 2015.

  1. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    So I was trying out stencil masks with the Standard shader. I made a slightly altered version of the Standard shader, just adding

    Code (CSharp):
    1.  
    2.     Stencil {
    3.             Ref [_StencilMask]
    4.             Comp Equal
    5.             Pass Keep
    6.             Fail Keep
    7.     }
    8.  
    to both sub-shaders (with '_StencilMask' a new Int-porperty).

    This works, however shadows aren't being rendered properly. The object using this stencil mask shows the shadows from objects behind it.

    The weird shadows:


    (The cube in the center has my altered standard shader with the stencil mask. The see-through side of the outer box has a shader that sets the stencil mask.)

    The shadows I would expect:

    (Here I just changed the shader back to the normal standard shader.)

    Why is this happening? Is it something I can fix?

    I added the altered standard shader, as well as the shader that sets the stencil mask.

    Thanks!
     

    Attached Files:

  2. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    Bumb. Anyone from Unity that could take a look at this?
     
  3. Kevin-VFX

    Kevin-VFX

    Joined:
    Apr 17, 2016
    Posts:
    54
    Did you ever figure this out?
     
  4. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    Sadly no. Though thanks for the bump! Hopefully someone from Unity or so sees it now.
     
  5. a_p_u_r_o

    a_p_u_r_o

    Joined:
    May 27, 2015
    Posts:
    20
    I gave it a try.
    It seems ShadowCaster pass does not have proper access to the stencil buffer.
    You may try adding another Stencil section with "Comp Always" inside the ShadowCaster pass and see what happens.
    This will fix the shadow on the faces of the center cube where it is drawn, but gives weird shadow where it is not drawn.
    StencilAndShadowPC.jpg

    You may also try changing the Platform to Android or iOS in the Build Settings window.
    On these two platforms, it seems Unity does not use ShadowCaster pass, thus no weird shadow.
    StencilAndShadowAndroid.jpg
     
  6. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    Hmm, those are interesting results. What does the ShadowCaster pass do? Since it seems there are still shadows on iOS and Android...
     
  7. a_p_u_r_o

    a_p_u_r_o

    Joined:
    May 27, 2015
    Posts:
    20
    OK. I got it right.
    It is wrong where I said "Unity does not use ShadowCaster pass (for Android and iOS)".
    Unity is using ShadowCaster pass in two different ways for PC (and maybe for Mac and Linux aswell).

    What matters is that the see-through side also has to be involved in the separate pass which creates DepthTexture.
    Hence it also needs ShadowCaster pass.
    And with tricky Stencil operation.
    Code (ShaderLab):
    1.         Stencil
    2.         {
    3.             Ref [_StencilMask]
    4.             Comp Never
    5.             Fail Replace
    6.         }
    7.  
    To fully understand whats going on you should take a look inside Unity's FrameDebugger.
    Following captures are taken after I fixed the shader (shader_StencilMask.shader).
    FrameDebugger.png DepthPass.png

    Note: Unity version 5.3.3p1
     

    Attached Files:

  8. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    I tried your updates shaders, but something is still wrong. I made a little project to show it. Just look around the cube a little from different angles, shadows are ok sometimes, but from other angles it's still broken.
     

    Attached Files:

  9. TomMLe

    TomMLe

    Joined:
    Jul 20, 2016
    Posts:
    1
    anything new to add here? I'm looking to get masked postpro running on my project, but i have to rely on the standard shaders to maintain the look we got so far. Thus i figured, expanding the standard shader would be a good idea. It seems to still give the described errors though? Or are they gone by now?
     
  10. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    I haven't played with this since then anymore, so as far as I know the bug is still there. This was several versions ago though, so it might have been fixed, but I never heard anything from Unity about this. (I send them a bug report through the editor)
     
    a_p_u_r_o likes this.
  11. trunghieu974

    trunghieu974

    Joined:
    Jun 19, 2015
    Posts:
    25
    i have tried your shader and it work properly now :D
     
    The-Oddler likes this.
  12. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    Cool! Thanks for letting me know
     
    trunghieu974 likes this.