////////////////////////////////////////////////////////
//	GTU-famicom version 0.50
//	Author: aliaspider - aliaspider@gmail.com
//	License: GPLv3      
////////////////////////////////////////////////////////

struct input
{
   float2 video_size;
   float2 texture_size;
   float2 output_size;
   float  frame_count;
   float  frame_direction;
   float frame_rotation;
};


void main_vertex
(
   uniform input IN,
   float4 position : POSITION,
   out float4 oPosition : POSITION,
   uniform float4x4 modelViewProj,
   float2 tex : TEXCOORD,
   out float2 oTex : TEXCOORD,
   out float colorPhase
)
{
   oPosition = mul(modelViewProj, position);
   oTex = tex;

   float2 pos = (tex.xy*IN.output_size.xy*IN.texture_size.xy/IN.video_size.xy)-0.5;
   colorPhase = 8.0 + pos.x + pos.y * 4.0 + IN.frame_count * 4.0;
}

#define GET_LEVEL(X) ((X)*(255.0f / (128.0f*(1.962f-.518f)))-(.518f / (1.962f-.518f)))

float4 main_fragment (uniform sampler2D s0 : TEXUNIT0, uniform sampler2D nestable,float2 tex : TEXCOORD, in float colorPhase) : COLOR
{
	float4 c=tex2D(s0, tex.xy);

	float2 pixmapCoord;
	pixmapCoord.x= c.x*(15.0/(16.0*4.0))+c.y*(3.0/4.0)+(0.5/(16.0*4.0));
	pixmapCoord.y= 1.0-(floor(fmod(colorPhase+0.5,12.0))/(12.0*8.0)+c.z*(7.0/8.0)+(0.5/(12.0*8.0)));

	return float4(GET_LEVEL(tex2D(nestable, pixmapCoord.xy).r));
}
