Shader "Unlit/TeleportTerrain"
{
    Properties
    {
        _Color ("Color", Color) = (1, 1, 1, 1)
        _TeleportRange ("Teleport range", Float) = 20.0
        _FalloffPow ("Teleport falloff power", Float) = 2.0
    }
    SubShader
    {
        Tags { "Queue"="Transparent" "RenderType"="Transparent" }
        LOD 100
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float4 world_pos : TEXCOORD1;
            };

            fixed4 _Color;
            float _TeleportRange;
            float _FalloffPow;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.world_pos = mul(unity_ObjectToWorld, v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = _Color;
                // Fade out at TeleportRange
                float distanceZ = max(0.0, min(1.0, length(i.world_pos.xyz - _WorldSpaceCameraPos.xyz) / _TeleportRange));
                col.a *= 1 - pow(distanceZ, _FalloffPow);
                return col;
            }
            ENDCG
        }
    }
}