
// Uniform variables for texturing
uniform sampler2D 	aaa_tex2d[4];

// Dimension of textures : 0, 1, 2, 3 dimension, -1 if unused per Tex Unit
uniform	int			aaa_tex_dim[4];

// Size of Texture for each texture unit
uniform vec2		aaa_tex_0_size;

// 0 = time
// 1 = mouse X
// 2 = mouse Y
// 3 = InteriorSize ~ 20.0
uniform float		aaa_pu_float[6];


const float PI = 3.14159265359;
const float TAU = 2.0*PI;
const float deg30 = TAU/12.0;



float hexDist(vec2 a, vec2 b){
	vec2 p = abs(b-a);
	float s = sin(deg30);
	float c = cos(deg30);
	
	float diagDist = s*p.x + c*p.y;
	return max(diagDist, p.x)/c;
}

vec2 nearestHex(float s, vec2 st){
	float h = sin(deg30)*s;
	float r = cos(deg30)*s;
	float b = s + 2.0*h;
	float a = 2.0*r;
	float m = h/r;

	vec2 sect = st/vec2(2.0*r, h+s);
	vec2 sectPxl = mod(st, vec2(2.0*r, h+s));
	
	float aSection = mod(floor(sect.y), 2.0);
	
	vec2 coord = floor(sect);
	if(aSection > 0.0){
		if(sectPxl.y < (h-sectPxl.x*m)){
			coord -= 1.0;
		}
		else if(sectPxl.y < (-h + sectPxl.x*m)){
			coord.y -= 1.0;
		}

	}
	else{
		if(sectPxl.x > r){
			if(sectPxl.y < (2.0*h - sectPxl.x * m)){
				coord.y -= 1.0;
			}
		}
		else{
			if(sectPxl.y < (sectPxl.x*m)){
				coord.y -= 1.0;
			}
			else{
				coord.x -= 1.0;
			}
		}
	}
	
	float xoff = mod(coord.y, 2.0)*r;
	return vec2(coord.x*2.0*r-xoff, coord.y*(h+s))+vec2(r*2.0, s);
}

void main(void){
	vec2 uv = gl_FragCoord.xy/aaa_tex_0_size.xy;
	float s = aaa_tex_0_size.x/80.0;
	vec2 nearest = nearestHex(s, gl_FragCoord.xy);
	vec4 texel = texture2D(aaa_tex2d[0], nearest/aaa_tex_0_size.xy, -100.0);
	float dist = hexDist(gl_FragCoord.xy, nearest);
	
	//float luminance = (texel.r + texel.g + texel.b)/3.0;
	//float interiorSize = luminance*s;
	
	float interior = 1.0 - smoothstep(aaa_pu_float[3]-1.0, aaa_pu_float[3], dist);
	
	gl_FragColor = vec4(texel.rgb*interior, 1.0);
}