
struct VsInput
{
  float2 pos       : POSITION;
  float4 scolor    : COLOR0;
  float4 ranges    : COLOR1;
  float2 tc        : TEXCOORD0;
  float4 mr        : TEXCOORD1;
  float4 mg        : TEXCOORD2;
  float4 mb        : TEXCOORD3;
  float4 mc        : TEXCOORD4;
};

struct VsOutput
{
  float4 pos       : POSITION;
  float4 scolor    : COLOR0;
  float4 ranges    : COLOR1;
  float2 tc        : TEXCOORD0;
  float4 mr        : TEXCOORD1;
  float4 mg        : TEXCOORD2;
  float4 mb        : TEXCOORD3;
  float4 mc        : TEXCOORD4;
};


float4 tm          : register(c16);


VsOutput vsh(VsInput I)
{
  VsOutput O;
  O.pos=float4(I.pos.xy*tm.xy+tm.zw, 1, 1);
  O.scolor=I.scolor;
  O.ranges.xy=(I.ranges.xy*2-1)/64+I.tc;
  O.ranges.zw=I.ranges.zw;
  O.tc=I.tc;
  O.mr=I.mr;
  O.mg=I.mg;
  O.mb=I.mb;
  O.mc=I.mc;
  return O;
}


sampler tex : register(s0);


float4 psh(VsOutput I) : COLOR
{
  float4 ts=tex2D(tex, I.tc);
  float  sd=tex2D(tex, I.ranges.xy).a;
  float d=ts.a;

  float  a=smoothstep(I.ranges.w, 1-I.ranges.w,  d);
  float sa=smoothstep(I.ranges.z, 1-I.ranges.z, sd);

  a=sqrt(a);
  sa=sqrt(sa);

  float4 sc=I.scolor*sa;
  float4 c=I.mc+ts.r*I.mr+ts.g*I.mg+ts.b*I.mb;

  float4 r=lerp(sc, c, a);

  clip(dot(r, 1)-1.0/255);

  return r;
}
