Search code examples
c#unity-game-engineshaderfragment-shader

Unity distance fade shader


I'm working on a URP shader in unity that is supposed to make an object transparent the further away it is from the camera.

The shader I wrote does not work and I can't find a solution.

Shader "Custom/DistanceFade" {

    Properties{

        _Color("Color", Color) = (1,1,1,1)
        _Threshold("Threshold", float) = 5
    }


        SubShader{
       Tags { "Queue" = "Transparent" }
       LOD 200

       Pass {
           CGPROGRAM
           #pragma vertex vert
           #pragma fragment frag
           #include "UnityCG.cginc"

           struct appdata {
               float4 vertex : POSITION;
               float2 texcoord : TEXCOORD0;
           };

           struct v2f {
               float4 vertex : SV_POSITION;
               float2 texcoord : TEXCOORD0;
           };

           sampler2D _MainTex;
           float4 _Color;
           float _Threshold;

           v2f vert(appdata v) {
               v2f o;
               o.vertex = UnityObjectToClipPos(v.vertex);
               o.texcoord = v.texcoord;
               return o;
           }

           fixed4 frag(v2f i) : SV_Target {
               fixed4 col = tex2D(_MainTex, i.texcoord);
               float _distance = distance(_WorldSpaceCameraPos, i.vertex);
               col.a = saturate((_Threshold - _distance) / _Threshold);
               return col * _Color;
           }
           ENDCG
       }
    }
        FallBack "Diffuse"

}

Does anyone have an idea?


Solution

  • Basically you just need to enable blending assuming you have meaningful distance values. Here is a variant which does not depend on a depth texture for the distance and gives some control over the fading interval:

    Shader "Custom/DistanceFade" {
    
        Properties{
            _Color("Color", Color) = (1,1,1,1)
            _Threshold("Threshold", float) = 5
        }
    
        SubShader{
            Tags { "Queue" = "Transparent" }
            Blend SrcAlpha OneMinusSrcAlpha    // <-- enable blending
            LOD 200
    
           Pass {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"
    
                struct appdata {
                    float4 vertex : POSITION;
                    float2 texcoord : TEXCOORD0;
                };
    
                struct v2f {
                    float4 vertex : SV_POSITION;
                    // make float3 for an extra value (can be done in other ways)
                    float3 texcoord : TEXCOORD0;
                };
    
                sampler2D _MainTex;
                float4 _Color;
                float _Threshold;
    
                v2f vert(appdata v) {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.texcoord.xy = v.texcoord.xy;
    
                    // get the distance from the camera to the vertex
                    // (we do it in world space)
                    float dist = distance(_WorldSpaceCameraPos, mul(unity_ObjectToWorld, v.vertex));
    
                    // map the distance to an fade interval
                    float beginfade = 500;
                    float endfade = 600;
                    float alpha = min(max(dist, beginfade), endfade) - beginfade;
                    alpha = 1 - alpha / (endfade - beginfade);
    
                    // put alpha somewhere unused to deliver it to the fragment shader
                    o.texcoord.z = alpha ;
                    return o;
                }
    
                fixed4 frag(v2f i) : SV_Target {
                    fixed4 col = tex2D(_MainTex, i.texcoord);
    
                    // use our fade value 
                    col.a = i.texcoord.z;
                    return col * _Color;
                }
                ENDCG
           }
        }
        FallBack "Diffuse"
    }