Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Transparent Surface Shader Invisible In front Of UI

Discussion in 'Shaders' started by xeetsh, Apr 12, 2021.

  1. xeetsh

    xeetsh

    Joined:
    Jun 3, 2015
    Posts:
    6
    I wan't to produce a semi transparent overlay over my UI for which I'm using this simple variant of the standard surface shader in with added transparency:

    Code (CSharp):
    1. Shader "Custom/FakeUIMaskShader"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "Queue" = "Overlay+1" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
    13.         LOD 100
    14.  
    15.         Stencil {
    16.             Ref 2
    17.             Comp Equal
    18.         }
    19.  
    20.         CGPROGRAM
    21.         // Physically based Standard lighting model, and enable shadows on all light types
    22.         #pragma surface surf Standard fullforwardshadows alpha
    23.  
    24.         // Use shader model 3.0 target, to get nicer looking lighting
    25.         #pragma target 3.0
    26.  
    27.         sampler2D _MainTex;
    28.  
    29.         struct Input
    30.         {
    31.             float2 uv_MainTex;
    32.         };
    33.  
    34.         half _Glossiness;
    35.         half _Metallic;
    36.         fixed4 _Color;
    37.  
    38.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    39.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    40.         // #pragma instancing_options assumeuniformscaling
    41.         UNITY_INSTANCING_BUFFER_START(Props)
    42.             // put more per-instance properties here
    43.         UNITY_INSTANCING_BUFFER_END(Props)
    44.  
    45.         void surf (Input IN, inout SurfaceOutputStandard o)
    46.         {
    47.             // Albedo comes from a texture tinted by color
    48.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    49.             o.Albedo = c.rgb;
    50.             // Metallic and smoothness come from slider variables
    51.             o.Metallic = _Metallic;
    52.             o.Smoothness = _Glossiness;
    53.             o.Alpha = c.a;
    54.         }
    55.         ENDCG
    56.     }
    57.     FallBack "Diffuse"
    58. }
    59.  
    This works fine as long as the object is't in front of my Screen Space Camera UI (UI elements all are using the default unity UI shader). If the object is in front of the UI the UI get's rendered over the object like this:

    See the red part is hidden behind the UI inspite of the object (selected) is in front of the UI.

    If I remove the alpha pragma, everything works fine (except the transparency missing of course):


    The fact that this uses a stencil should be irrelevant, as this also happens when removing the stencil part.

    Is there anything I'm missing? Thanks in advance!
     

    Attached Files:

  2. kirin178

    kirin178

    Joined:
    May 17, 2020
    Posts:
    1
    Hi,
    I just ran into a similar problem and solved with adding the two line in Pass function
    Code (CSharp):
    1.   SubShader {
    2.     Pass {
    3.       ZWrite On
    4.       ColorMask 0
    5.     }
    6.   }
     
unityunity