#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
int BorderSize;
float R;
float G;
float B;
float2 ImageSize;

sampler2D SpriteTextureSampler = sampler_state
{
  Texture = <SpriteTexture>;
};

struct VertexShaderOutput
{
  float4 Color : COLOR0;
  float2 TextureCoordinates : TEXCOORD0;
};

float4 MainPS(VertexShaderOutput input) : COLOR
{
    float4 color = tex2Dlod(SpriteTextureSampler, float4(input.TextureCoordinates.x, input.TextureCoordinates.y, 0, 0));

    if(color.a == 1 && color.r ==1 && color.g ==1 && color.b ==1) 
    {
        return color;
    }

    if (color.r==0 && color.a==1)
    {
        color = float4(0,0,0,0);
    }

    float2 pixel = (int2)input.TextureCoordinates * ImageSize;
    
    float2 offsets[8] = {float2(-1,0), float2(1,0), float2(0,1), float2(0,-1), float2(-1,1), float2(1,1), float2(1,-1), float2(-1,-1)};
    
    float2 checkp1 = pixel + (float)BorderSize * offsets[0];
    float2 curUV1 = input.TextureCoordinates + checkp1/(float2)ImageSize;
    float4 alpha1 = tex2Dlod(SpriteTextureSampler, float4(curUV1.x, curUV1.y,0,0));
    if (alpha1.a==1 && alpha1.r==0) {alpha1=float4(0,0,0,0);}

    float2 checkp2 = pixel + (float)BorderSize * offsets[1];
    float2 curUV2 = input.TextureCoordinates + checkp2/(float2)ImageSize;
    float4 alpha2 = tex2Dlod(SpriteTextureSampler, float4(curUV2.x, curUV2.y,0,0));
    if (alpha2.a==1 && alpha2.r==0) {alpha2=float4(0,0,0,0);}

    float2 checkp3 = pixel + (float)BorderSize * offsets[2];
    float2 curUV3 = input.TextureCoordinates + checkp3/(float2)ImageSize;
    float4 alpha3 = tex2Dlod(SpriteTextureSampler, float4(curUV3.x, curUV3.y,0,0));
    if (alpha3.a==1 && alpha3.r==0) {alpha3=float4(0,0,0,0);}

    float2 checkp4 = pixel + (float)BorderSize * offsets[3];
    float2 curUV4 = input.TextureCoordinates + checkp4/(float2)ImageSize;
    float4 alpha4 = tex2Dlod(SpriteTextureSampler, float4(curUV4.x, curUV4.y,0,0));
    if (alpha4.a==1 && alpha4.r==0) {alpha4=float4(0,0,0,0);}

    float2 checkp5 = pixel + (float)BorderSize * offsets[4];
    float2 curUV5 = input.TextureCoordinates + checkp5/(float2)ImageSize;
    float4 alpha5 = tex2Dlod(SpriteTextureSampler, float4(curUV5.x, curUV5.y,0,0));
    if (alpha5.a==1 && alpha5.r==0) {alpha5=float4(0,0,0,0);}

    float2 checkp6 = pixel + (float)BorderSize * offsets[5];
    float2 curUV6 = input.TextureCoordinates + checkp6/(float2)ImageSize;
    float4 alpha6 = tex2Dlod(SpriteTextureSampler, float4(curUV6.x, curUV6.y,0,0));
    if (alpha6.a==1 && alpha6.r==0) {alpha6=float4(0,0,0,0);}

    float2 checkp7 = pixel + (float)BorderSize * offsets[6];
    float2 curUV7 = input.TextureCoordinates + checkp7/(float2)ImageSize;
    float4 alpha7 = tex2Dlod(SpriteTextureSampler, float4(curUV7.x, curUV7.y,0,0));
    if (alpha7.a==1 && alpha7.r==0) {alpha7=float4(0,0,0,0);}

    float2 checkp8 = pixel + (float)BorderSize * offsets[7];
    float2 curUV8 = input.TextureCoordinates + checkp8/(float2)ImageSize;
    float4 alpha8 = tex2Dlod(SpriteTextureSampler, float4(curUV8.x, curUV8.y,0,0));
    if (alpha8.a==1 && alpha8.r==0) {alpha8=float4(0,0,0,0);}

    float alpha = alpha1.a + alpha2.a + alpha3.a + alpha4.a + alpha5.a + alpha6.a + alpha7.a + alpha8.a;

    if (alpha>0)
    {
        return  float4(R,G,B,1);
    }

return color;

};

technique SpriteDrawing
{
  pass P0
  {
      PixelShader = compile PS_SHADERMODEL MainPS();
  }
};