#version 330 compatibility

uniform sampler2D gtexture;
uniform sampler2D lightmap;


/* DRAWBUFFERS:01234 */
layout(location = 0) out vec4 color;
layout(location = 1) out vec4 outColor1;
layout(location = 2) out vec4 outColor2;
layout(location = 3) out vec4 outColor3;
layout(location = 4) out vec4 outColor4;


const vec4 colortex3ClearColor = vec4(1.0, 1.0, 1.0, 1.0);
const vec4 colortex1ClearColor = vec4(0.0, 1, 1.0, 1.0);

in vec2 texCoord;
in vec4 foliageColor;
in vec2 lightMapCoords;
in vec3 viewSpacePosition;

uniform float fogStart;
uniform float fogEnd;
uniform vec3 fogColor;
uniform vec4 entityColor;
uniform int blockEntityId;

in vec3 geoNormal;

const float ambientOcclusionLevel = 0.5f;    
const float maxOutDist = 280;
const float blendDist = 80;

void main() {


    vec3 worldGeoNormal = mat3(gl_ModelViewMatrixInverse) * geoNormal;

	vec3 lightColor = texture(lightmap, lightMapCoords).rgb;

	vec4 outputColorData = texture(gtexture, texCoord);

	vec3 outputColor = outputColorData.rgb * foliageColor.rgb * (lightColor);
	float transparency = outputColorData.a * foliageColor.a;

	if (transparency < .1)
	{
		discard;
	}

    float distanceFromCamera = distance(vec3(0), viewSpacePosition);

    float fogBlendValue = clamp((distanceFromCamera - fogStart) / (fogEnd - fogStart), 0, 1);
    outputColor = mix(outputColor, fogColor, fogBlendValue);

    float outlineBlendValue = (distanceFromCamera) / (maxOutDist); 
    float outlineBlendValueStart = clamp((distanceFromCamera - blendDist) / (maxOutDist - blendDist), 0, 1); 
    float maxClip = 1;
    if (outlineBlendValue > 0.99)
    {
        maxClip = 0;
    }

    //vec3 newGeoNormal = vec3(((worldGeoNormal.x + worldGeoNormal.z)*(worldGeoNormal.x + worldGeoNormal.y)) - (outlineBlendValue * 0.5),((worldGeoNormal.y + worldGeoNormal.z)*(worldGeoNormal.y + worldGeoNormal.x)) - (outlineBlendValue * 0.5), outlineBlendValue * 10);
    vec3 newGeoNormal = vec3(((worldGeoNormal.x + worldGeoNormal.z)*(worldGeoNormal.x + worldGeoNormal.y)),((worldGeoNormal.y + worldGeoNormal.z)*(worldGeoNormal.y + worldGeoNormal.x)), (((worldGeoNormal.z + worldGeoNormal.x)*(worldGeoNormal.z + worldGeoNormal.y)) + clamp((outlineBlendValue * 40), 0, 0.9)));


    if (newGeoNormal.b == 0)
    {
        newGeoNormal = vec3(0);
    }
    
    color = vec4(mix(outputColor, entityColor.rgb, entityColor.a), transparency);

    outColor1 = vec4(vec3(1 - (outlineBlendValue * 7.5) - ((1 - foliageColor.a) * 2),(((distanceFromCamera * 0.06)) + (distanceFromCamera * 0.006) - (distanceFromCamera * 0.02)) * (foliageColor.a * 0.3), (distanceFromCamera * 0.02)* (foliageColor.a * 1)), 1);
    outColor1 = vec4(clamp(outColor1.rgb, 0, 100) + ((1 - foliageColor.a)*1), 1);
    outColor1 = clamp(vec4((1 - outlineBlendValue) * outColor1.x, clamp(((1 - outlineBlendValue) * outColor1.y), 0, 1) + outlineBlendValue, (outlineBlendValue) * outColor1.z, 1), 0, 1);

    outColor2 = vec4(newGeoNormal - ((1 - clamp(outColor1.rgb, 0, 1)) * 0.2), 1);
    outColor2 *= maxClip;

    outColor3 = vec4(vec3(outlineBlendValueStart + clamp(1 -(((1 - fogBlendValue) + (1 - fogBlendValue))), 0, 1)), 1);

    outColor4 = vec4(vec3(clamp((1 - distanceFromCamera * 0.015), 0.00, 1)), 1);
}