#version 150 compatibility

// fragment shader for map rendering

// https://learnopengl.com/Lighting/Basic-Lighting

// TODO: light parameters.

uniform vec2 u_resolution;  // Canvas size (width,height)
uniform vec2 u_mouse;       // mouse position in screen pixels
uniform float u_time;       // Time in seconds since load

//uniform mat4 view_matrix;

uniform sampler2D ShadowMap;
uniform sampler2D Texture1; // need
uniform vec3 PlayerPosition;
uniform vec3 CameraVector;
varying vec3 Playercheckcoords; // need

varying vec3 normcoords;
varying vec3 vPos;

varying vec4 ShadowCoord;

// Passed in from the vertex shader.
varying vec2 v_texcoord;
varying vec4 VertexColor;
varying vec3 vertexcoords; // need

uniform int UndergroundMode;

void main() {
	
	vec4  color;
	
	// Get fragment color
	color = texture2D(Texture1, gl_TexCoord[0].st);
	
	//float Z = gl_ProjectionMatrix[3].z/(gl_FragCoord.z * -2.0 + 1.0 - gl_ProjectionMatrix[2].z);
	//gl_FragCoord.z
	/*
	if (Z < 0.99) {
		
		color.r = 0;
		color.g = 0;
		color.b = 0;
		
	}
	*/
	
	// produce cone shaped holes in ground, to see thru ceilings
	if (UndergroundMode > 0) {
		
		vec3 comparison = vertexcoords.xyz;
		float maxdist = 3.0;
		vec3 checkpcoords = Playercheckcoords;
		checkpcoords.z += maxdist;
		
		float Dist2D = distance(checkpcoords.xy, comparison.xy);
		float dist3d = distance(checkpcoords.xyz, comparison.xyz);
		
		// discard based on sort of cone shape, 0.4 is minimum height at which the effect starts.
		
		float offset = 0.0;
		if (UndergroundMode > 1) // /* + 0.4 USE THIS WHEN STANDING ON angled surfaces. */
			offset = 0.4;
		
		if ( (Dist2D < maxdist) && (comparison.z > (Playercheckcoords.z + offset) + (Dist2D * 0.25)) ) {
			
			maxdist = exp2(dist3d);
			
			color.a = color.a * (Dist2D * 0.35 /*(1.0 /maxdist)*/);
			
		}
		
	}
	
	gl_FragColor = color; // vec4(color.r, color.g, color.b, color.a) /** VertexColor*/;  // todo: lighting

}
