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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Depth mask with alpha

Discussion in 'Shaders' started by Impersivevr, Feb 19, 2020.

  1. Impersivevr

    Impersivevr

    Joined:
    Nov 16, 2018
    Posts:
    3
    Hello,
    I'm trying to achieve a fade out of multiple objects by using a plane in front of them that renders the skybox.
    The cutout to skybox is the easy part – just making the shader render before the geometry (geometry-10) works perfectly.
    The problem is adding alpha to the rendered pixels during the first pass, so to achieve the fade. If I try to add another pass, it has no knowledge of the currently rendered pixels and adding alpha will just fade to black.
    I'm having problems in understanding how to make a grab pass here because all examples and tuts I can find blend with textures, which is really not what I want here.
    Does anybody have a good idea on how to do this or some good resources I can read about this?

    My shader currently looks like this.

    Code (CSharp):
    1. Shader "Custom/NewMask"
    2. {
    3.    Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.  
    7.     }
    8.     SubShader
    9.     {
    10.         Tags {"Queue" = "Geometry-10" "RenderType"="Transparent" }
    11.            ZWrite On
    12.         AlphaToMask On
    13.         ColorMask A
    14.  
    15.         Pass {
    16.         }
    17.  
    18.         CGPROGRAM
    19.        
    20.         #pragma surface surf Standard fullforwardshadows alpha:fade
    21.         #pragma target 3.0
    22.        
    23.         sampler2D _MainTex;
    24.        
    25.  
    26.         struct Input {
    27.             float2 uv_MainTex;
    28.         };
    29.  
    30.         fixed4 _Color;
    31.         void surf (Input IN, inout SurfaceOutputStandard o)
    32.         {
    33.             fixed4 c = _Color;
    34.             //o.Albedo = c.rgb;
    35.  
    36.             o.Alpha = c.a;
    37.         }
    38.         ENDCG
    39.     }
    40.     FallBack "Standard"
    41. }
    42.  
     
  2. Impersivevr

    Impersivevr

    Joined:
    Nov 16, 2018
    Posts:
    3
    Bump!
    The more I look around, the more I think this shader could be a neat solution to a problem: fading out multiple elements is quite a pain to do with normal shaders and complex geometries/hierarchies and sometimes you could just have a plane in front of the object that fades to the underlying scene. It could be useful in a number of (very controlled) situations.

    I'm pretty sure that I'm missing out something on how to further elaborate a depth mask shader. No matter what I try, I'm back at the starting point – I'm only able to depth mask but not to apply alpha to the masking object.

    Any suggestions?
     
  3. Impersivevr

    Impersivevr

    Joined:
    Nov 16, 2018
    Posts:
    3
    Still struggling with this ) :
    I've implemented the grabpass, renders above what I want to fade and I'm to edit the color of the fragments like I want so it's pretty much there, but when it comes to alpha, it just fades to black. Am I missing something? Does anybody know what I'm doing wrong?
    Thanks!


    Code (CSharp):
    1. Shader "Custom/Maskv5"
    2. {
    3.     Properties
    4.     { _Color ("Color", Color) = (0.5, 0.5, 0.5, 0.5) }
    5.     SubShader
    6.     {
    7.  
    8.         Tags { "Queue" = "Transparent-1"}
    9.  
    10.         // Grab the screen behind the object into _BackgroundTexture
    11.         GrabPass
    12.         {
    13.             "_BackgroundTexture"
    14.         }
    15.         // Render the object with the texture generated above and apply alpha
    16.  
    17.         ZWrite On
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag alpha
    23.  
    24.             #include "UnityCG.cginc"
    25.             fixed4 _Color;
    26.             struct v2f
    27.             {
    28.                 float4 grabPos : TEXCOORD0;
    29.                 float4 pos : SV_POSITION;
    30.             };
    31.  
    32.             v2f vert(appdata_base v) {
    33.                 v2f o;
    34.                 // use UnityObjectToClipPos from UnityCG.cginc to calculate
    35.                 // the clip-space of the vertex
    36.                 o.pos = UnityObjectToClipPos(v.vertex);
    37.                 // use ComputeGrabScreenPos function from UnityCG.cginc
    38.                 // to get the correct texture coordinate
    39.                 o.grabPos = ComputeGrabScreenPos(o.pos);
    40.                 return o;
    41.             }
    42.  
    43.             sampler2D _BackgroundTexture;
    44.             half4 frag(v2f i) : SV_Target
    45.             {
    46.  
    47.                 half4 bgcolor = tex2Dproj(_BackgroundTexture, i.grabPos)-(0,0,0,_Color.a);
    48.                 return bgcolor;
    49.             }
    50.             ENDCG
    51.         }
    52.  
    53.     }
    54. }  
     
    Last edited: Mar 4, 2020