// __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 vec3 pos;

LAYOUT_BINDING(0) uniform sampler2D TEXTURE_0;

highp vec2 rotate(in highp vec2 p, in highp float t)
{
	return p * cos(-t) + vec2(p.y, -p.x) * sin(-t);
}

highp float sun(highp vec2 po)
{
	highp float c = 0.0;
	highp float l = 0.0;
    highp float z = 0.0;
    highp vec2 uv = po;
    highp vec2 p = po;
    //po -= 0.5;
    p = mix(rotate(po, 0.0),rotate(po, 0.0),smoothstep( 0.0, 1.0,length(po*1.272)));
    highp float r = 0.390;
    l = length(p);
    uv -= smoothstep(0.376, -0.152, p / l * - r) * abs(sin(l * 9.0 - z * 2.0));
    uv += smoothstep(-0.256, 0.064, p / l * r) * abs(sin( l * 9.0 - z * 2.0));
    c += 0.018 / length(abs(fract(uv * - 0.864) - 0.5));

    return c/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

#ifdef ALPHA_TEST
	if(diffuse.a < 0.5)
		discard;
#endif

    highp float v = 2.0;
    highp vec3 N = vec3(0.0,0.0,0.0);
    highp vec3 posi = pos;
    //normalize
    highp vec3 Np = normalize(cross(dFdx(posi), dFdy(posi)));
    N = normalize(vec3(posi.x,posi.y*v,posi.z)) * v;

diffuse.rgb = mix(mix(vec3(0.0,0.0,0.0),vec3(0.519,0.367,1.000),sun(N.xz)),FOG_COLOR.rgb,mix(smoothstep(0.0, 0.1, length(N.xz*0.05)),1.0,N.y*0.5));

	gl_FragColor = diffuse;
}
