//
//morphlet_Fist
//

vertexshader vshader;
pixelshader pshader;

texture result_texture;

float4x4 proj_matrix;
float width,height;


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

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 invwidth;
   float invheight;
   float3 u,v,w1,w2;
   float2 tex;
   float a,b,c;
   float4 color;

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


   color = tex2D(Sampler_result_texture, ps_in.tex0);
   a = (color.x+color.y+color.z)*50./3.;

   tex = ps_in.tex0-float2(invwidth,0);
   color = tex2D(Sampler_result_texture, tex);
   b = (color.x+color.y+color.z)*50./3.;

   tex = ps_in.tex0-float2(0,invheight);
   color = tex2D(Sampler_result_texture, tex);
   c = (color.x+color.y+color.z)*50./3.;

   u.z = a - b;
   u.x = sqrt(1.f-u.z);
   u.y = 0.f;

   v.z = a - c;
   v.x = 0.f;
   v.y = sqrt(1.f-v.z);

   w1 = (normalize(cross(u,v))+1.)/2.;
   //w1 =  normalize(cross(u,v));

   ps_out.color = float4(w1,a/60.);

   return ps_out;
}


technique T0
{

    pass P0
    {
        Lighting     = FALSE;
        CullMode     = NONE;
        ZWriteEnable = FALSE;
        AlphaBlendEnable = FALSE;
        ZENABLE = FALSE;
        CLIPPING = FALSE;
        FILLMODE = SOLID;

        VertexShader = (vshader);
        PixelShader  = (pshader);
    }
}


