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

Casting Shadows with a transparent surface shader

Discussion in 'Shaders' started by Studio-Raef, Jul 12, 2019.

  1. Studio-Raef

    Studio-Raef

    Joined:
    Jul 1, 2013
    Posts:
    9
    I'm racking my brain over something stupid probably.

    I'm trying to make a simple shader that allows me to load in a cloud texture and adjust the black/gray/white values so it becomes more or less transparent

    upload_2019-7-12_14-56-21.png
    And then change some values to make the clouds bigger or smaller
    upload_2019-7-12_14-59-0.png


    This works nicely and also with transparency like I want it to. But what I don't seem to find is how I get this alpha value to work with shadows. The shadows that get cast are of the entire quad instead of what's visible
    upload_2019-7-12_15-0-42.png

    But it I load in an image that does have transparency in the source, it does work. (the shader only affects the alpha and albedo, the projected shadow is from the alpha of the image)

    upload_2019-7-12_15-2-21.png

    Does anyone know how I could let the shadows take my calculated Alpha into account?

    Here's the shader:


    Code (CSharp):
    1. Shader "Custom/CloudSurface"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _LowEnd("Low End",Float) = 0.1
    8.         _HighEnd("High End",Float) = 0.9
    9.     }
    10.     SubShader
    11.     {
    12.  
    13.         Tags { "RenderType"="Transparent" "Queue" = "Transparent" }
    14.         LOD 200
    15.         Blend SrcAlpha OneMinusSrcAlpha
    16.  
    17.         CGPROGRAM
    18.         // Physically based Standard lighting model, and enable shadows on all light types
    19.         #pragma surface surf Standard fullforwardshadows alpha:fade
    20.  
    21.  
    22.         // Use shader model 3.0 target, to get nicer looking lighting
    23.         #pragma target 3.0
    24.  
    25.         sampler2D _MainTex;
    26.  
    27.         struct Input
    28.         {
    29.             float2 uv_MainTex;
    30.         };
    31.  
    32.         fixed4 _Color;
    33.         half _LowEnd;
    34.         half _HighEnd;
    35.  
    36.         UNITY_INSTANCING_BUFFER_START(Props)
    37.             // put more per-instance properties here
    38.         UNITY_INSTANCING_BUFFER_END(Props)
    39.  
    40.         void surf (Input IN, inout SurfaceOutputStandard o)
    41.         {
    42.             // Albedo comes from a texture tinted by color
    43.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) ;
    44.             half cloud = clamp((c.r - _LowEnd) / (_HighEnd - _LowEnd), 0, 1);
    45.             o.Albedo =cloud* _Color;
    46.  
    47.             o.Alpha = cloud * _Color.a;
    48.         }
    49.         ENDCG
    50.        
    51.     }
    52.     FallBack "Standard"
    53. }
    54.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    addshadow
    https://docs.unity3d.com/Manual/SL-SurfaceShaders.html

    Surface Shaders by default do not generate a shadow caster pass, so your shader is just using the one from the fallback (in this case the Standard shader's shadow caster). By adding addshadow to your #pragma surf line it'll generate a shadow caster pass that takes into account the your surf function's code.

    However, I believe the shadow it produces will have a hard cutoff edge rather than a dithered fade like what you're seeing, or might not work at all for alpha:fade, I forget which. The solution to that is to write a fully custom shadow caster pass instead of using addshadow.

    https://catlikecoding.com/unity/tutorials/rendering/part-12/
     
    Last edited: Jul 12, 2019
    Homicide likes this.
  3. JahnStar

    JahnStar

    Joined:
    Jul 17, 2018
    Posts:
    1
    Maybe it will help someone else. Try this:
    Fallback "Legacy Shaders/Transparent/Cutout/VertexLit"