Shader "Projector/Additive"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Color", color) = (1, 1, 1, 1)
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent" }
        LOD 100

        BlendOp Add
        Blend SrcAlpha One

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 uv_proj : TEXCOORD0;
                float depth : TEXCOORD1;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _Color;

            float4x4 unity_Projector;
            float4x4 unity_ProjectorClip;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv_proj = mul(unity_Projector, v.vertex);
                o.depth = mul(unity_ProjectorClip, v.vertex).x;
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                clip(1 - abs(i.depth - 0.5) * 2);

                float4 uv_proj = UNITY_PROJ_COORD(i.uv_proj);
                clip(1 - abs(uv_proj.xy / uv_proj.w - 0.5) * 2);

                fixed4 col = tex2Dproj (_MainTex, uv_proj) * _Color;

                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}
