// Always provided by OBS
uniform float4x4 ViewProj<
	bool automatic = true;
	string name = "View Projection Matrix";
>;

// Provided by Stream Effects
uniform float4 Time<
	bool automatic = true;
	string name = "Time Array";
	string description = "A float4 value containing the total time, rendering time and the time since the last tick. The last value is a random number between 0 and 1.";
>;
uniform float4x4 Random<
	bool automatic = true;
	string name = "Random Array";
	string description = "A float4x4 value containing random values between 0 and 1";
>;
uniform float4 ViewSize<
	bool automatic = true;
>;
uniform texture2d InputA<
	bool automatic = true;
>;

uniform float HexagonScale<
	string name = "Hexagon Scale";
	string field_type = "slider";
	float minimum = 1.0;
	float maximum = 1024.0;
	float step = 1.0;
> = 120.0;
uniform float HexagonWallScale<
	string name = "Hexagon Wall Scale";
	string field_type = "slider";
	float minimum = 0.0;
	float maximum = 100.0;
	float step = 0.01;
	float scale = 0.01;
> = 100.0;

// ---------- Shader Code
sampler_state def_sampler {
	AddressU  = Clamp;
	AddressV  = Clamp;
	Filter    = Linear;
};

struct VertData {
	float4 pos : POSITION;
	float2 uv  : TEXCOORD0;
};

VertData VSDefault(VertData v_in) {
	VertData vert_out;
	vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
	vert_out.uv  = v_in.uv;
	return vert_out;
}

#define PI		3.1415926f
#define TAU		6.2831853f
#define deg30	0.20943951

float hexDist(float2 a, float2 b){
	float2 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;
}

float2 nearestHex(float s, float2 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;

	float2 sect = st/float2(2.0*r, h+s);
	float2 sectPxl = fmod(st, float2(2.0*r, h+s));
	
	float aSection = fmod(floor(sect.y), 2.0);
	
	float2 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 = fmod(coord.y, 2.0)*r;
	return float2(coord.x*2.0*r-xoff, coord.y*(h+s))+float2(r*2.0, s);
}

float4 PSDefault(VertData v_in) : TARGET {
	float2 coord = v_in.uv * ViewSize.xy;
	float s = ViewSize.x/HexagonScale;
	float2 nearest = nearestHex(s, coord);
	float4 texel = InputA.Sample(def_sampler, nearest / ViewSize.xy);//, -100.0);
	float dist = hexDist(coord, nearest);
	
	float luminance = (texel.r + texel.g + texel.b)/3.0;
	//float interiorSize = luminance*s;
	float interiorSize = s * HexagonWallScale;
	float interior = 1.0 - smoothstep(interiorSize-1.0, interiorSize, dist);
	//fragColor = float4(dist);
	return float4(texel.rgb*interior, 1.0);
	//fragColor = float4(nearest, 0.0, 1.0);
}

technique Draw
{
	pass
	{
		vertex_shader = VSDefault(v_in);
		pixel_shader  = PSDefault(v_in); 
	}
}
