Search Unity

Surface shader - Alpha: fade and self-shadowing

Discussion in 'Shaders' started by Misscelan, Aug 21, 2018.

  1. Misscelan

    Misscelan

    Joined:
    Mar 8, 2013
    Posts:
    176
    I have this shader that affects some objects for which I manipulate the alpha everytime the player is behind it (so the player is always in-sight).

    The problem is this shader does not receive self-shadowing. Is there a way to implement this on the shader?
    I've tried with alpha-cutoff, it works but I cannot progressively fade the object which makes it look weird.

    This is the shader:

    Code (CSharp):
    1. Shader "Custom/ColorVertexTransparent" {
    2.         Properties{
    3.             _Color("Color", Color) = (1,1,1,1)
    4.             _MainTex("Albedo (RGB)", 2D) = "white" {}
    5.             _Glossiness("Smoothness", Range(0,1)) = 0.5
    6.             _Metallic("Metallic", Range(0,1)) = 0.0
    7.             _EmissionColor("_EmissionColor", Color) = (1,1,1,1)
    8.             _EmissionPower("_EmissionPower", Range(0,10)) = 0.0
    9.             _FogImpact("_FogImpact", Range(0,1)) = 0.0
    10.             _FogEnd("_FogEnd", Range(0,100)) = 20.0
    11.             _FogStart("_FogStart", Range(0,10)) = 0.2
    12.  
    13.         }
    14.         SubShader{
    15.  
    16.  
    17.         //so they don't render on top of each other
    18.         Pass
    19.         {
    20.             ColorMask 0
    21.             ZWrite On
    22.         }
    23.  
    24.         Tags{ "Queue" = "Transparent" "RenderType" = "Opaque" }
    25.  
    26.         LOD 200
    27.         ZWrite Off
    28.         CGPROGRAM
    29.  
    30.         #pragma surface surf Standard fullforwardshadows vertex:vert alpha:fade addshadow
    31.         #pragma target 3.0
    32.  
    33.         sampler2D _MainTex;
    34.  
    35.         struct Input {
    36.             float2 uv_MainTex;
    37.             float4 color: COLOR;
    38.             float fog;
    39.         };
    40.  
    41.         half _Glossiness;
    42.         half _Metallic;
    43.         fixed4 _Color;
    44.         fixed4 _EmissionColor;
    45.         fixed _EmissionPower;
    46.         float _FogImpact;
    47.         float _FogStart;
    48.         float _FogEnd;
    49.  
    50.         void vert(inout appdata_full v, out Input o)
    51.         {
    52.             UNITY_INITIALIZE_OUTPUT(Input, o);
    53.             float l_pos = length(UnityObjectToClipPos(v.vertex));
    54.             float l_diff = _FogEnd - _FogStart;
    55.             float l_invDiff = 1.0f / l_diff;
    56.             o.fog = clamp((l_pos - _FogEnd) * l_invDiff, 0.0, 1.0) * _FogImpact;
    57.         }
    58.  
    59.         void surf(Input IN, inout SurfaceOutputStandard o)
    60.         {
    61.             o.Albedo = lerp(IN.color, _Color, IN.fog);
    62.             o.Emission = _EmissionColor * _EmissionPower;
    63.             o.Metallic = _Metallic;
    64.             o.Smoothness = _Glossiness;
    65.             o.Alpha = _Color.a;
    66.         }
    67.         ENDCG
    68.     }
    69.     FallBack "Diffuse"
    70. }
    71.  
    Thanks!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Nope.

    First, Unity doesn't support transparent objects receiving shadows, or more specifically objects in the transparent queue receiving shadows. That can be sort of worked around by using the AlphaTest queue, but using the "alpha" keyword in a surface shader automatically removes all shadow receiving parts of the shader.

    The closest you can get is using "Queue"="AlphaTest", Blend SrcAlpha OneMinusSrcAlpha, and adding keepalpha instead of alpha:fade on the #pragma surface line. However in doing so you'll see there are a ton of weird visual artifacts with doing so.

    Unity long ago chose to not support shadow receiving on transparent objects, and the way they do directional light shadows makes it much harder.

    If you search on the forums you'll find threads on the topic that go back to 2008 asking about this.


    The HDRP (and possibly LWRP, it keeps getting enabled and disabled in each version) does support this though.
     
    Alic, shegway and Misscelan like this.