// __multiversion__
// This signals the loading code to prepend either #version 100 or #version 300 es as apropriate.

#include "fragmentVersionCentroid.h"

#if __VERSION__ >= 300

#if defined(TEXEL_AA) && defined(TEXEL_AA_FEATURE)
_centroid in highp vec2 uv;
#else
_centroid in vec2 uv;
#endif

#else

varying vec2 uv;

#endif

#include "uniformShaderConstants.h"
#include "util.h"
#include "uniformPerFrameConstants.h"

varying highp vec4 wpos;
varying highp vec3 posi;

LAYOUT_BINDING(0) uniform sampler2D TEXTURE_0;
LAYOUT_BINDING(1) uniform sampler2D TEXTURE_1;

#include "dect.h"

highp vec3 lens(highp vec2 u, highp vec2 w)
{
    highp vec2 uvd = u * length(u);
    highp float f2 = max(1.960 / (1.0 + 32.120 * pow(length(uvd + 1.464 * w), 1.696)), 0.0) * 0.458;
    highp vec2 uvx = mix(u, uvd, - 1.108);
    highp float f4 = max(0.032 - pow(length(uvx + 0.586 * w), 2.992), 0.0) * 5.464;
    uvx = mix(u, uvd, -1.760);
    highp float f5 = max(0.002 - pow(length(uvx + 1.660 * w), 5.5), 0.0) * 6.208;
    uvx = mix(u, uvd, -0.588);
    highp float f6 = max(0.058 - pow(length(uvx + 1.540 * w), 1.096), 0.000) * 9.912;
    highp vec3 c = vec3(0.0,0.0,0.0);
    highp float l = f2 + f4 + f5 + f6; 
    c += vec3(l,l,l);
    return c * 0.580;
}

highp float sun(highp vec3 po)
{
    highp float c = 1.0;
    highp float l = 1.0;
    highp vec3 uvs = po;
    highp vec3 p = po;
    l = length(p);
	uvs -= smoothstep(1.0, 0.0, p / l * - 0.474);
    uvs += smoothstep(1.0, 0.0, p / l * 0.474);
    c = 0.01 / length(abs(fract(uvs) - 0.5));
    return c * 1.704 / l;
    //return smoothstep(5.0,0.0,length(po*5.0));
}

highp float ray(highp vec3 po, highp float v)
{
    highp float c = 1.0;
    highp float l = 1.0;
    highp vec3 uvs = po;
    highp vec3 p = po;
    l = length(p);
    uvs -= smoothstep(1.0, -0.864, p / l * -0.574);
    uvs += smoothstep(1.0, -0.864, p / l * 0.574);
    uvs *= l - 4.024 + sin(l / - 1.728);
    c = 0.04 / length(abs(fract(uvs) - 0.5));
    return c * v / l;
}


void main()
{
#if !defined(TEXEL_AA) || !defined(TEXEL_AA_FEATURE)
	vec4 diffuse = texture2D( TEXTURE_0, uv );
#else
	vec4 diffuse = texture2D_AA(TEXTURE_0, uv );
#endif

    vec4 temp = texture2D(TEXTURE_1, vec2(0.0, 1.0));
    float chuva = Rain(FOG_CONTROL);
    float dia = max(0.0, min( 1.0, (temp.r - 0.48) * 2.0));
    float noite =  mix(0.0,5.0,pow(max(min(1.0-temp.r*1.5,1.0),0.0),1.2));
    float dsol = mix(mix( 0.0, 5.0, clamp((1.-dia*(1.5+chuva))*2.5, .0, 1.)*clamp((temp.r -.15)*1.0,0.,1.)),0.5,noite);
    
    highp vec3 posii = posi.xyz;
    highp vec3 c = vec3(0.0,0.0,0.0);
    highp vec2 w = -wpos.xy * 0.2;

    highp float lu = abs(mix(mix(sun(posii), ray(posii, 1.080) * dia, 0.100), smoothstep(5.0, -5.0, length(posii) * 6.0), 0.5));
    highp vec3 cs = mix(mix(vec3(0.990,0.890,0.724), vec3(0.990,0.923,1.0), noite), vec3(0.990,0.923,0.724) + FOG_COLOR.rgb, dsol);
    c = mix(vec3(0.0,0.0,0.0), cs, lu);
    c += lens(posii.xz, w) * dia;
    c = mix(c, vec3(0.0,0.0,0.0), smoothstep(0.0, 1.0, length(posii) * 1.8));    
    highp vec4 dif = vec4(c, 1.0);
              dif.a *= mix(1.0, 0.0, chuva);
#ifdef ALPHA_TEST
	if(diffuse.a < 0.5)
		discard;
#endif

	gl_FragColor = dif;
}
