
#ifdef VERTEX

out vec2 vTexcoord;

void main() {
	gl_Position = ftransform();
	vTexcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
}

#endif

#ifdef FRAGMENT


#define rayTraceSteps 64
#define rayTraceBiSteps 5


uniform sampler2D colortex0;
uniform sampler2D colortex1;
uniform sampler2D colortex4; //SSNORMAL TERRAIN
uniform sampler2D colortex5;
uniform sampler2D colortex6;
uniform sampler2D colortex7;
uniform sampler2D colortex15;
uniform sampler2D colortex12;
uniform sampler2D colortex13;
uniform sampler2D colortex11;
uniform sampler2D depthtex0;
uniform sampler2D depthtex2;
uniform sampler2D depthtex1;
uniform sampler2D colortex10;
uniform sampler2D shadowtex0;
uniform sampler2D shadowtex1;
uniform sampler2D shadowcolor0;

const float sunPathRotation = 40.0;


uniform mat4 gbufferModelView;
uniform mat4 gbufferProjectionInverse,gbufferProjection;
uniform mat4 gbufferModelViewInverse;
uniform vec3 sunPosition,moonPosition;
uniform vec3 cameraPosition;
uniform mat4 shadowProjection,shadowModelView;


uniform int worldTime;
uniform float rainStrength;

//>>IN-OUT<<//
in vec2 vTexcoord;

//>>INCLUDES<<//
#include "/lib/Color/Luminance.glsl"
#include "/lib/Conf/Color_Conf.glsl"
#include "/lib/Color/Saturation.glsl"
#include "/lib/Conf/ToneMapper_Conf.glsl"
#include "/lib/Color/ToneMappers.glsl"
#include "/lib/Color/Contrast.glsl"
#include "/lib/Conf/Contrast_conf.glsl"



uniform vec3 shadowLightPosition;

uniform float viewWidth, viewHeight;
uniform float frameTimeCounter;

uniform float fogStart;
uniform float fogEnd;
uniform float far;
uniform float near;




#include "/lib/blur/KawaseBlur.glsl"
#include "/lib/Conf/Sharp_conf.glsl"

#include "/lib/Post/FilmGrain.glsl"
#include "/lib/Conf/FilmGrain_conf.glsl"


#include "/lib/Conf/Sky_conf.glsl"
#include "/lib/util/time.glsl"
#include "/lib/sky/Sky_Palet.glsl"
#include "/lib/sky/Phases.glsl"
#include "/lib/sky/SunLight.glsl"

#include "/lib/util/transformations.glsl"

#include "/lib/sky/Sky_Saturation.glsl"

#include "/lib/util/cave.glsl"
#include "/lib/util/LD.glsl"


#include "/lib/sky/SUN_MOON_UV.glsl"
#include "/lib/sky/VelpixSun.glsl"
#include "/lib/util/rainVisibility.glsl"
#include "/lib/sky/RenderSky.glsl"
#include "/lib/Conf/KuwaharaDistanceConf.glsl"
#include "/lib/Post/KuwaharaFilter.glsl"
#include "/lib/sky/Fog.glsl"

#include "/lib/Conf/Shadows_Conf.glsl"
#include "/lib/shadows/distort.glsl"
#include "/lib/shadows/ShadowsCalc.glsl"

#include "/lib/Conf/VL_conf.glsl"
#include "/lib/Lighting/VL.glsl"

#include "/lib/water/WaterH.glsl"
#include "/lib/Conf/Water_conf.glsl"

#include "/lib/Reflections/ssr.glsl"



void main() {
	
	vec2 texelSize = vec2(1.0 / viewWidth,1.0 / viewHeight);

	float aspect = viewWidth / viewHeight; 

	vec3 vColor = texture2D(colortex0, vTexcoord).rgb; // >>> Main color

	float vpLuma = vpGetLuminance(vColor);

	float depth = texture2D(depthtex0, vTexcoord).r;
	float depthWorld= texture2D(depthtex2, vTexcoord).r;

	vec3 worldPos = GetWorldPos(vTexcoord, depth);


	vec4 phases = GetAtmospherePhases();

	#ifdef SHARPNESS
	vColor = vpSharpen(colortex0, vTexcoord, texelSize, SHARPNESS_STRENGHT);
	#endif
	
	#ifdef Color_ToneMapper
	vColor = Tonemap(vColor);
	#endif

	#ifdef Contrast
	vColor = vpContrast(vColor,Contrast_Strength);
	#endif
	
	#ifdef Color_FilmGrain
	vColor = vpFilmGrain(vColor, vTexcoord,FilmGrain_Strength, aspect);
	#endif 
	
	
	#ifdef Color_Saturation
	vColor = vpSaturation(vColor);
	#endif



	vec4 waterData = texture2D(colortex5, vTexcoord);

	vec3 viewPos = ScreenToView(vTexcoord, depthWorld);

    vec3 viewDir = normalize(-viewPos);


	vec3 shadowCoord = vec3(0);

	if(depth < 1.0)
{
    vec3 ShadowworldPos = ShadowGetWorldPos(vTexcoord, depth);
    shadowCoord = vpShadowCoord(ShadowworldPos);

    float shadowBright = 0.65;

    #ifdef Shadows
	#ifdef OVERWORLD
	vColor *= mix(vec3(shadowBright), vpGetShadow(shadowCoord), 0.25);
	#endif
	#endif

	float SunVolVisiblity = 1.0 * phases.x + 0.7 * phases.y + 0.8 * phases.z + 0.0 * phases.w;
	float MoonVolVisiblity = phases.w;
	
	#ifdef OVERWORLD
	#ifdef VL

	if(SunVolVisiblity > 0.001){
	  vec3 sunColor =
      vec3(1.20, 0.55, 0.20) * phases.x + 
      vec3(1.00, 0.98, 0.92) * phases.y + 
      vec3(1.25, 0.40, 0.15) * phases.z;
	  vColor += vpVolumetricLight(worldPos,sunPosition,sunColor,  VP_SUN_VOL_INTENSITY) * SunVolVisiblity;
	}
	if(MoonVolVisiblity > 0.001){
	vColor += vpVolumetricLight(worldPos,moonPosition,vec3(0.45, 0.60, 1.0),VP_MOON_VOL_INTENSITY);
	}
	#endif
	#endif


}	
	

	float water = waterData.a;

 	vec3 normal = normalize(waterData.rgb * 2.0 - 1.0);

	if (water > 0.5)
{
	vec3 distortedNormal = normal;

	vec2 w = worldPos.xz;

	distortedNormal.x += sin(w.x * 2.5 + frameTimeCounter * 1.3) * 0.020 + sin(w.y * 4.8 - frameTimeCounter * 0.8) * 0.010;

	distortedNormal = normalize(distortedNormal);
	
	#ifdef Water_Reflections
	vColor = ScreenSpaceReflection(vTexcoord, depth, distortedNormal, vColor);
	#endif

}

	#ifdef OVERWORLD
	#ifdef ARTISTIC_FOG
	vColor = vpFog(vColor);
	#endif
	#endif

	#ifdef NETHER
	vColor = vpNetherFog(vColor);
	#endif

	#ifdef END
	vColor = vpEndFog(vColor);
	#endif

	


/* DRAWBUFFERS:0 */
	gl_FragData[0] =  vec4(vColor,1.0); //gcolor
}
#endif

