Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

uGUI masking blocked by background meshes

Discussion in 'UGUI & TextMesh Pro' started by ldb, Sep 23, 2014.

  1. ldb

    ldb

    Joined:
    Apr 30, 2013
    Posts:
    40
    I have a canvas, with a panel acting as a mask for a text object. All in world view. It kind of works. But when I have any mesh, an object I've imported, unity terrain etc, behind the canvas, it interferes with the masking quite severely.

    Hopefully I'm missing something obvious but at the moment it seems like quite a big problem.

    First image is with mask component disabled, second is when I enable mask. As you can see the objects behind the canvas are playing havoc with the masking.

    Any suggestions?

    Thanks in advance

    unity_46_uGUI_no_masking.jpg unity_46_uGUI_masking.jpg
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
    That looks pretty cool :p

    I'm guessing you are using deferred or light layers? You need to add a pass to the canvas at the start that cleans the stencil buffer out. Just making an image that fills the whole canvas be the first thing that renders and use this shader:

    Code (csharp):
    1.  
    2. Shader "UI/MaskClear"
    3. {
    4.     SubShader
    5.     {
    6.         Tags
    7.         {
    8.             "Queue"="Transparent"
    9.             "IgnoreProjector"="True"
    10.             "RenderType"="Transparent"
    11.             "PreviewType"="Plane"
    12.             "CanUseSpriteAtlas"="True"
    13.         }
    14.      
    15.         Stencil
    16.         {
    17.             Ref 0
    18.             Comp Always
    19.             Pass Zero
    20.             ReadMask 255
    21.             WriteMask 255
    22.         }
    23.  
    24.         Cull Off
    25.         Lighting Off
    26.         ZWrite Off
    27.         ZTest [unity_GUIZTestMode]
    28.         Fog { Mode Off }
    29.         Blend Zero One
    30.         ColorMask 0
    31.  
    32.         Pass
    33.         {
    34.         CGPROGRAM
    35.             #pragma vertex vert
    36.             #pragma fragment frag
    37.             #include "UnityCG.cginc"
    38.          
    39.             struct appdata_t
    40.             {
    41.                 float4 vertex   : POSITION;
    42.                 float4 color    : COLOR;
    43.                 float2 texcoord : TEXCOORD0;
    44.             };
    45.  
    46.             struct v2f
    47.             {
    48.                 float4 vertex   : SV_POSITION;
    49.                 fixed4 color    : COLOR;
    50.             };
    51.          
    52.             fixed4 _Color;
    53.  
    54.             v2f vert(appdata_t IN)
    55.             {
    56.                 v2f OUT;
    57.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    58.                 OUT.color = IN.color;
    59.                 return OUT;
    60.             }
    61.  
    62.             fixed4 frag(v2f IN) : SV_Target
    63.             {
    64.                 return IN.color;
    65.             }
    66.         ENDCG
    67.         }
    68.     }
    69. }
    70.  
    I just put this on an image that scales with the canvas :)
    before-bad.PNG
    Before


    after-good.PNG
    After
     
    karl_jones, DanSuperGP and ldb like this.
  3. ldb

    ldb

    Joined:
    Apr 30, 2013
    Posts:
    40
    Thank you!

    Yes, I'm using deferred Lighting, you're a legend. Works perfectly now. I love the new GUI stuff.
     
  4. Myralogue

    Myralogue

    Joined:
    Jul 5, 2015
    Posts:
    2
    Thanks @Tim C this fixed my issue!

    I was looking for so long, thought I was going to go mental o_O