Shader "Custom/GeneralSpriteShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        _Intensity ("Intensity", Float) = 1
        _LightTexture ("Light Texture", 2D) = "white" {}
        _LightCount ("Light Count", Int) = 0
        _AmbientLight ("Ambient Light Color", Color) = (0.0, 0.0, 0.0, 1.0)
    }
    SubShader
    {
        Tags { "QUEUE"="Transparent" "IGNOREPROJECTOR"="true" "RenderType"="Transparent" "CanUseSpriteAtlas"="true" "PreviewType"="Plane" }
        ZWrite Off
        Cull Off
        Blend One OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
			#pragma multi_compile _ PIXELSNAP_ON

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float4 color : COLOR;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                fixed4 color : COLOR;
                float2 wp : TEXCOORD1;
            };

            sampler2D _MainTex;
            fixed4 _Color;
            float4 _MainTex_ST;
            float _Intensity;
            sampler2D _LightTexture;

            int _LightCount;
            float4 _LightLocations[500];
            float _LightIntensities[500];
            float _LightSizes[500];
            fixed4 _LightColors[500];

            fixed4 _AmbientLight;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.color = v.color * _Color;
                float4 worldpos = mul(unity_ObjectToWorld, v.vertex);
                o.wp = float2(worldpos.x, worldpos.y);
                return o;
            }

            fixed4 frag (v2f input) : SV_Target
            {
                fixed4 total_light_col = _AmbientLight;
                [loop]
                for (int i = 0; i < _LightCount; i++) {
                    float size = max(_LightSizes[i], 0.01);
                    float2 location = float2(_LightLocations[i].x, _LightLocations[i].y);
                    location = input.wp - location;
                    location = float2(location.x / size + 0.5, location.y / size + 0.5);

                    if (location.x < 0 || location.x > 1 || location.y < 0 || location.y > 1) {
                        continue;
                    }

                    fixed4 curr_light_col = tex2D(_LightTexture, location) * _LightIntensities[i];
                    curr_light_col = fixed4(curr_light_col.rgb * _LightColors[i].rgb * curr_light_col.a, 0);
                    total_light_col += pow(curr_light_col, 2);
                }
                total_light_col = sqrt(total_light_col);

                fixed4 sprite_col = tex2D(_MainTex, input.uv);
                sprite_col = sprite_col * input.color;

                fixed4 col = sprite_col * total_light_col;
                col = fixed4(col.rgb * col.a, sprite_col.a * _AmbientLight.a);

                col.r = min(1, col.r + _Intensity);
                col.g = min(1, col.g + _Intensity);
                col.b = min(1, col.b + _Intensity);
                col.rgb *= col.a;
                return col;
            }
            ENDCG
        }
    }
}
