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

Shadow Strength in ShadowCaster pass

Discussion in 'Shaders' started by kulesz, Aug 1, 2018.

  1. kulesz

    kulesz

    Joined:
    Jul 1, 2012
    Posts:
    138
    Hi,

    I've got a simple custom shader (vertex + fragment) with a ShadowCaster pass like this:

    Code (CSharp):
    1. Tags {"LightMode"="ShadowCaster"}
    2. ZWrite On
    3.  
    4. struct v2f
    5. {
    6.     float2 uv : TEXCOORD1;
    7.     V2F_SHADOW_CASTER;
    8. };
    9.  
    10. v2f vert(appdata v)
    11. {
    12.     v2f o;
    13.     o.uv = v.texcoord;
    14.  
    15.     TRANSFER_SHADOW_CASTER(o);
    16.     return o;
    17. }
    18.  
    19. float4 frag(v2f i) : SV_Target
    20. {
    21.     fixed4 color = tex2D(_MainTex, i.uv);
    22.     clip(color.a - _Cutoff);
    23.  
    24.     SHADOW_CASTER_FRAGMENT(i)
    25. }

    As you can see it's very, very simple. It is also working fine, producing nice shadow below object.
    Is there any way to control strength (opacity) of that shadow? For example to be able to fade it out?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Shadow maps are a form of depth map. A depth map is a texture that stores depth values for each pixel from a particular view. In the case of shadow maps these are from the "view" of the light.

    The type of shadow mapping Unity uses the shadow map only stores one depth per pixel. This means to do semi-transparent shadows you need to use some form of dithering, like what Unity's Standard shader uses.

    Some more advanced shadow casting techniques can support various forms of shadow transparency, but the most common technique for doing transparent shadows is to approximate it with a projector of some kind.