
// completely unoptimized and has some bugs

struct fragment {
	float4 position  : POSITION;
	float4 color0    : COLOR0;	//r,g,b=lightdir a=nikovalue
	float2 texcoord0 : TEXCOORD0;
	float4 envcoord  : TEXCOORD1;
	float4 normal    : COLOR1;
	float3 camera    : TEXCOORD2;
};

struct pixel {
	float4 color0 : COLOR0;
};



pixel main(fragment IN,
           uniform sampler2D texture,
           uniform sampler2D normalMap,
           uniform float time,
           uniform float hue,		// unused
           uniform float useDepth	// unused
           )
{
	pixel OUT;

	float3 texCol = tex2D(texture, IN.texcoord0).rgb;
	float3 normalCol = tex2D(normalMap, IN.texcoord0).rgb;

	// do the blue stuff with black fake env
/*	float sinIntensity = 5.4925;
	// sin coloring shit
	float sinval1 = 0.15+0.12*sin(IN.normal.x*sinIntensity+IN.normal.y*sinIntensity+IN.normal.z*sinIntensity + time*0.0);

	float3 ambiColor = float4(sinval1*texCol.r,
	                          sinval1*texCol.g,
	                          sinval1*texCol.b);

	// apply backlight
	ambiColor += 0.6*IN.color0.a;

	OUT.color0 = float(ambiColor, 1.0);
*/

	// "parallax"
	float scale = 0.04;
	float bias = 0.00;
	//float height = (texCol.r+texCol.g+texCol.b)/3.0;
	float height = (normalCol.r+normalCol.g+normalCol.b)/3.0;
	height = height*scale-bias;
	float2 lookup = height*IN.camera.xy+IN.texcoord0.xy;

	// bump
	float3 light = 2.0*(IN.color0.rgb-0.5);
	float3 lightVector = 2.0*(light-0.5);
	float3 bumpVector = 2.0*(tex2D(normalMap, lookup).rgb-0.5);
	float diffuse = (dot(bumpVector, lightVector));

	float3 newTex = tex2D(texture, lookup).rgb;
	OUT.color0 = float4(diffuse*texCol*0.8, 1.0);

	// add original texture
	OUT.color0.rgb = (OUT.color0.rgb*0.6) + (texCol*0.5);

	return OUT;
}
