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

Question Receiving Shadows On a Tranparent Shader

Discussion in 'Universal Render Pipeline' started by Breinhardte, Sep 12, 2021.

  1. Breinhardte

    Breinhardte

    Joined:
    Jul 17, 2021
    Posts:
    8
    In Unity 2020.1.3f1

    I'm currently working on a small shader which takes the alpha component of a mesh's vertex colours and uses it for transparency, the result is a fade out along the edge of the shape:

    Fairly simple stuff, and works generally okay. The only noticable bug is that it doesn't receive shadows.

    Through a bit of reading, I know that it's possible to receive shadows on transparent shaders such as these and have seen working visual examples, but there's a lot of dubious information on the exact reason and resolution to this problem.

    The shader is as follows:
    Code (CSharp):
    1.  Shader "Custom/StandardVertexAlpha"
    2. {
    3.      Properties
    4.      {
    5.          _Color("Color", Color) = (1,1,1,1)
    6.          _MainTex("Texture", 2D) = "white" {}
    7.          _Emission("Emission", Color) = (0,0,0,0)
    8.      }
    9.          SubShader
    10.      {
    11.          Tags { "RenderType" = "Transparent"  "Queue" = "AlphaTest"}
    12.          Cull Off
    13.          Lighting Off
    14.          CGPROGRAM
    15.          #pragma surface surf Lambert alpha:blend
    16.          struct Input
    17.          {
    18.              float2 uv_MainTex;
    19.              half4 color : COLOR;
    20.          };
    21.          sampler2D _MainTex;
    22.          fixed4 _Color;
    23.          fixed4 _Emission;
    24.          void surf(Input IN, inout SurfaceOutput o)
    25.          {
    26.              fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    27.              o.Albedo = c.rgb * IN.color.rgb;
    28.              o.Alpha = IN.color.a;
    29.              o.Emission = _Emission;
    30.          }
    31.          float ShadowAttenuation(int index, float3 worldPos)
    32.          {
    33.              return 1.0;
    34.          }
    35.          float CascadedShadowAttenuation(float3 worldPos)
    36.          {
    37.              return 1.0;
    38.          }
    39.          ENDCG
    40.      }
    41.          Fallback "Diffuse"
    42. }
    The two methods "ShadowAttenuation" and "CascadedShadowAttenuation" are taken from an old Unity tutorial, but don't seem to function anymore.


    I've found a couple of hacks, like using the keepalpha pragma, but this is from 2008, so wondering where things are in 2021.

    This hack seems to work okay in general cases:


    But, it does have some depth testing artefacts, and issues with emission:


     
    Last edited: Sep 12, 2021
  2. Breinhardte

    Breinhardte

    Joined:
    Jul 17, 2021
    Posts:
    8
    With the workaround mentioned at the end of the post, the emission problem I have actually solved. The only remaining issue is the depth item.



    I basically have a simple custom lambert cutout plane behind my custom vertex alpha shader. it works until the camera reaches a certain height, then as you can see it stops rendering in the right order, showing the skybox under the vertex alpha shader.

    The custom cutout shader is using:
    Code (CSharp):
    1. Tags {"RenderType" = "TransparentCutout" "IgnoreProjector" = "True" "Queue" = "Transparent" }

    My custom vertex alpha shader is using:
    Code (CSharp):
    1. Tags { "RenderType" = "Opaque" "IgnoreProjector" = "True"  "Queue" = "Transparent"}
    2.  
     
    Last edited: Sep 12, 2021