#version 300 es

in highp vec3 worldPos;
out vec4 outColor;

void main()
{
	vec3 xPositive = vec3(1.0, 0.0, 0.0);
	vec3 yPositive = vec3(0.0, 1.0, 0.0);
	vec3 zPositive = vec3(0.0, 0.0, 1.0);

	vec3 xNegative = vec3(0.0, 1.0, 1.0);
	vec3 yNegative = vec3(1.0, 0.0, 1.0);
	vec3 zNegative = vec3(1.0, 1.0, 0.0);

	float pi = 3.141592653589793;
	float piDiv2 = pi/2.0;

	highp vec3 worldPosUnitLength = normalize(worldPos);
	float xzTheta = atan(worldPosUnitLength.x, worldPosUnitLength.z);
	vec3 outColor3;

	//render "color spots" to illustrate the world axes
	//first, render the "horizontal circle"
	float elevationRadians = asin(worldPosUnitLength.y);
	if(xzTheta <= pi && xzTheta > piDiv2)
	{
		outColor3 = mix(zNegative,xPositive,(pi-xzTheta)/piDiv2);
	}
	else if(xzTheta <= piDiv2 && xzTheta > 0.0)
	{
		outColor3 = mix(xPositive,zPositive,(piDiv2-xzTheta)/piDiv2);
	}
	else if(xzTheta <= 0.0 && xzTheta > -piDiv2)
	{
		outColor3 = mix(zPositive,xNegative,(-xzTheta)/piDiv2);
	}
	else//(xzTheta <= -piDiv2 && xzTheta >= -pi)
	{
		outColor3 = mix(xNegative,zNegative,(-xzTheta-piDiv2)/piDiv2);
	}

	//blend in the "vertical color"
	if(worldPosUnitLength.y > 0.0)
	{
		outColor3 = mix(outColor3, yPositive, elevationRadians/piDiv2);
	}
	else//(worldPosUnitLength.y <= 0.0)
	{
		outColor3 = mix(outColor3, yNegative, -elevationRadians/piDiv2);
	}

	//most of the world is grayscale, except for the color spots, which are exponentially faded in according to world axes
	float worldPosLengthMax = max(max(abs(worldPosUnitLength.x), abs(worldPosUnitLength.y)), abs(worldPosUnitLength.z));
	float tolerance = 0.3;
	float minThreshold = 1.0 - tolerance;

	float delta = (elevationRadians + piDiv2)/pi;
	vec3 deltaGray = vec3(delta, delta, delta);
	if(worldPosLengthMax <= minThreshold)
	{
		outColor3 = deltaGray;
	}
	else
	{
		float scalar = (worldPosLengthMax - minThreshold)/(1.0 - minThreshold);
		float smoothedScalar = pow(pow(2.0,scalar) - 1.0, 2.0);
		outColor3 = mix(deltaGray, outColor3, smoothedScalar);
	}

	outColor3 *= 0.8;//wash out to differentiate from the (otherwise identical) floor disc shader
	outColor = vec4(outColor3, 1.0);
}
