uniform sampler2D materialtex;
uniform sampler2D normaltex;
uniform sampler2D colortex;

varying vec3 gposition;
varying vec3 gnormal;

varying vec3 normal;
varying vec4 color;

void main()
{


	float bump_strength=0.3; // adjust this



	vec3 ntpos=gposition*3.0;
	vec2 noise_coords=vec2(0,0);

	if(abs(gnormal.z)>0.5)
		noise_coords=ntpos.xy;
	else if(abs(gnormal.x)>0.5)
		noise_coords=ntpos.zy;
	else
		noise_coords=ntpos.xz;
	vec3 noise_vector=texture2D(normaltex, noise_coords).xyz-vec3(0.5, 0.5, 0.5);

	vec3 nnormal=normalize(normal+noise_vector*bump_strength);

	vec2 lightmap_coords=nnormal.xy*0.495+vec2(0.5, 0.5);
	lightmap_coords.y*=-1.0;
	gl_FragColor=texture2D(materialtex, lightmap_coords);
	gl_FragColor+=color;
}

