//
//morphlet_Fist
//

texture result_texture;
texture in_texture;

float4x4 proj_matrix;

float width,height;

float levelmin,levelmax;

float intensity;

sampler Sampler_result_texture = sampler_state
{
    Texture   = (result_texture);
    MipFilter = NONE;
    MinFilter = NONE;
    MagFilter = NONE;
    ADDRESSU = WRAP;
    ADDRESSV = WRAP;
};

sampler Sampler_in_texture = sampler_state
{
    Texture   = (in_texture);
    MipFilter = NONE;
    MinFilter = NONE;
    MagFilter = NONE;
    ADDRESSU = WRAP;
    ADDRESSV = WRAP;
};

struct VS_INPUT
{
    float3 pos : POSITION;
    float2 tex0 : TEXCOORD0;
};

struct VS_OUTPUT
{
    float4 pos   : POSITION;
    float2 tex0  : TEXCOORD0;
};

VS_OUTPUT VS(VS_INPUT vertex_in)
{
    VS_OUTPUT vertex_out = (VS_OUTPUT)0;

    vertex_in.pos.x *= width;
    vertex_in.pos.y *= height;
    
    vertex_out.pos = mul(float4(vertex_in.pos,1),proj_matrix);
    vertex_out.tex0 = (vertex_in.tex0 + 1.0)/2.;

    return vertex_out;
}

struct PS_INPUT
{
  float2 tex0 : TEXCOORD0;
};

struct PS_OUTPUT
{
  float4 color : COLOR;
};


PS_OUTPUT PS(PS_INPUT ps_in)
{
   PS_OUTPUT ps_out = (PS_OUTPUT)0;
   float v,vp;
   float2 tex;
   float invwidth,invheight;
   float2 r = ps_in.tex0;

   invwidth = 1.f/width;
   invheight = 1.f/height;

   v = tex2D(Sampler_result_texture, ps_in.tex0).x;
   v = (v - levelmin) / (levelmax - levelmin);

   tex = ps_in.tex0 - float2(invwidth,0);
   vp = tex2D(Sampler_result_texture, tex).x;
   vp = (vp - levelmin) / (levelmax - levelmin);

   r.x += (v-vp)*intensity;

   tex = ps_in.tex0 - float2(0,invheight);
   vp = tex2D(Sampler_result_texture, tex).x;
   vp = (vp - levelmin) / (levelmax - levelmin);

   r.y += (v-vp)*intensity;

   ps_out.color =  tex2D(Sampler_in_texture, r);

   return ps_out;
}


