Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. Dismiss Notice

Help Wanted Transparent occlusion mask with two behaviors

Discussion in 'Shaders' started by Hugoliv, Jan 22, 2021.

  1. Hugoliv


    Dec 8, 2017
    Hi !

    I am using URP. I have an occlusion mask that works fine. The occlusion may vary with an input gradient texture.

    Here is the occlusion mask:

    Code (CSharp):
    1. Shader "Custom/OcclusionMaskAlpha"
    2. {
    3.     Properties
    4.     {
    5.         [MainTexture] _MainTex("BaseMap", 2D) = "white" {}
    6.         _CutOff("Alpha CutOff", Range(0,1)) = 1
    7.     }
    9.     SubShader{
    10.         Tags {"Queue" = "Geometry+10" "RenderType" = "Opaque" "RenderPipeline" = "UniversalRenderPipeline"}
    11.         ColorMask 0
    12.         ZWrite On
    13.         AlphaTest GEqual [_CutOff]
    14.         Cull Back
    15.         AlphaToMask On
    17.         Pass {
    18.             SetTexture[_MainTex] {
    19.                 combine texture alpha * primary
    20.             }
    21.         }
    22.     }
    23. }
    I have a plane containing a hole in my scene where the occlusion mask is applied (this is the plane selected in the picture below). The meshes behind the plane are well occludded using the alpha channel of the texture (it's a greyscale image with alpha source from grayscale selected in the import settings). The meshes are using a simple opaque unlit ShaderGraph.

    I am looking for a way to have two different behaviors for the same occlusion mask. I would like to have some of the meshes occludded using the AlphaTest as if the alpha cutoff was set to 0 (for example the blue ones) and some of the meshes occluded without using the AlphaTest as if the alpha cutoff was set to 1 (the red one for example).


    I want to achieves this kind of result (this is a gimp work), where alpha test is used on the left, but not on the right.


    I tried many solutions (Ztest, Zwrite, stencils, post-process features in URP, etc...) without any success.

    Do you have any idea how to achieve this ? I attached a .unitypackage for convenience.

    Attached Files:

  2. Hugoliv


    Dec 8, 2017
    no idea ?