Search Unity

Alpha Fade Surface Shader Example

Discussion in 'Shaders' started by sebj, May 16, 2020.

  1. sebj

    sebj

    Joined:
    Dec 4, 2013
    Posts:
    70
    Hi,

    I am trying to create a custom surface shader that generates a shadowcaster with the fade behaviour, however it is not working.

    Here is Unity's Standard shader with the rendering mode to fade:

    upload_2020-5-16_18-56-7.png

    And here is the result of the shader below:

    upload_2020-5-16_18-55-54.png

    Note that all I have done is create a new Surface Shader in the Assets dialogue, disable the fallback and instead add the shadow and alpha pragmas as per the docs.

    I have attempted to set blending modes, queues, etc but these do not make any difference. Note above that both planes are translucent - the mesh itself is rendering correctly in both images, the only difference is it's not casting a translucent shadow in the second.

    Code (csharp):
    1.  
    2. Shader "Custom/NewSurfaceShader"
    3. {
    4.     Properties
    5.     {
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    8.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    9.         _Metallic ("Metallic", Range(0,1)) = 0.0
    10.     }
    11.     SubShader
    12.     {
    13.         Tags { "RenderType"="Opaque" }
    14.         LOD 200
    15.  
    16.         CGPROGRAM
    17.         // Physically based Standard lighting model, and enable shadows on all light types
    18.         #pragma surface surf Standard fullforwardshadows addshadow alpha:fade
    19.  
    20.         // Use shader model 3.0 target, to get nicer looking lighting
    21.         #pragma target 3.0
    22.  
    23.         sampler2D _MainTex;
    24.  
    25.         struct Input
    26.         {
    27.             float2 uv_MainTex;
    28.         };
    29.  
    30.         half _Glossiness;
    31.         half _Metallic;
    32.         fixed4 _Color;
    33.  
    34.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    35.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    36.         // #pragma instancing_options assumeuniformscaling
    37.         UNITY_INSTANCING_BUFFER_START(Props)
    38.             // put more per-instance properties here
    39.         UNITY_INSTANCING_BUFFER_END(Props)
    40.  
    41.         void surf (Input IN, inout SurfaceOutputStandard o)
    42.         {
    43.             // Albedo comes from a texture tinted by color
    44.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    45.             o.Albedo = c.rgb;
    46.             // Metallic and smoothness come from slider variables
    47.             o.Metallic = _Metallic;
    48.             o.Smoothness = _Glossiness;
    49.             o.Alpha = c.a;
    50.         }
    51.         ENDCG
    52.     }
    53.     //FallBack "Diffuse"
    54. }
    55.  
    Where can I find an example for creating the fade behaviour seen in the standard shader?