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

#include "fragmentVersionSimple.h"
#include "uniformPerFrameConstants.h"
varying vec4 color;
varying highp vec3 pos;

LAYOUT_BINDING(1) uniform sampler2D TEXTURE_1;

#include "dect.h"

highp vec2 hash( highp vec2 x)
{
    const highp vec2 k = vec2(0.540,0.690);
    x = x * k + k.yx;
    return 1.0 + 2.0 * fract(19.080 * k * fract(x.x * x.y * (x.x + x.y)));
}

highp float gnoise( in highp vec2 pos)
{
    pos *= vec2(-0.470,-0.700);
    highp vec2 i = floor(pos);
    highp vec2 f = fract(pos);
    highp vec2 u = f * f * (3.0 - 2.0 * f);
    return (mix(mix(dot(hash(i + vec2(0.0,0.0)), f - vec2(0.0,0.0)), dot( hash(i + vec2(1.0,0.0)), f - vec2(1.0,0.0)), u.x), mix( dot( hash(i + vec2(0.0,1.0)), f - vec2(0.0,1.0)), dot( hash(i + vec2(1.0,1.0)), f - vec2(1.0,1.0)), u.x), u.y))*0.652;
}

//noise value 2D
highp float random(in highp vec2 pos)
{
    return fract(sin(dot(pos.xy, vec2(-0.600,0.430))) * 43759.057);
}
highp float noise(in highp vec2 st)
{
    highp vec2 i = floor(st);
    highp vec2 f = fract(st);
    highp float a = random(i);
    highp float b = random(i + vec2(1.0, 0.0));
    highp float c = random(i + vec2(0.0, 1.0));
    highp float d = random(i + vec2(1.0, 1.0));
    highp vec2 u = f * f * (3.0 - 2.0 * f);
    return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y;
}

highp float fbm (in highp vec2 pos, highp float ch)
{
    highp float v = mix(0.09, -0.3, ch);
    highp float la = 2.396;
    highp float ga = 0.4;
    highp float am = 0.332;
    highp float fr = 1.0;
    for (int i = 0; i < 5; i++) 
    {
        v += am * noise(abs(fr * pos + TIME * 0.05));
        fr *= la;
        am *= ga;
    }
    return v;
}

highp vec3 Cc(highp vec2 pos, highp vec3 ct, highp vec4 fc, highp float dia, highp float noite, highp float dsol, highp float ch)
{
    highp float clouds = fbm(pos * 2.980, ch);
    highp float Fimcloud = smoothstep(0.5, 0.0, clouds);
    highp vec3 tempo = mix(mix(mix(mix(vec3(0.6,0.6,0.6), vec3(1.0,1.0,1.0), dia), vec3(0.3,0.3,1.0), noite), vec3(fc.r, 0.2 * fc.r, 0.0), dsol),vec3(0.335,0.335,0.335),ch);
    return mix(ct, tempo, Fimcloud);
}

highp float hash13(highp vec3 p3)
{
	p3  = fract(p3 * vec3(0.067,0.074,0.090));
    p3 += dot(p3, p3.yzx + 19.294);
    return fract((p3.x + p3.y) * p3.z);
}

highp float stars(highp vec3 ray)
{
    highp vec3 p = ray * 150.0;
    highp float brigtness = smoothstep(1.0 - 0.05, 1.000, hash13(floor(p)));
    return smoothstep(0.5, 0.0, length(fract(p) )) * brigtness * mix(1.0, 0.1, 0.592 * length(fract(p * sin(TIME * 0.02))));
}

highp vec3 es(highp vec3 p){
    highp float s = stars(p);
    return mix(vec3(0.001,0.018,0.105), vec3(0.994,1.000,0.888), s);
}

void main() {

    highp float posc = length(pos);
    highp vec4 temp = FOG_COLOR;
    highp vec4 tp = texture2D(TEXTURE_1, vec2(0.0,1.0));
    highp float chuva = Rain(FOG_CONTROL);
    
    highp float dia = mix(pow(max(min(temp.r * 1.5 + 0.3, 1.0), 0.0), 1.2), 1.0, chuva);
    highp float noite = mix(0.0, 1.0, pow(max(min(1.0 - dia, 1.0), 0.0), 1.2));
    highp float dsol = mix(0.0, 1.1, 0.5 - abs(0.5 - (1.0 - max(0.0, min(1.0, (tp.r - 0.46) * 2.0)))));
    highp float dss = mix(mix(mix( 0.0, 2.5, clamp((1.0 - max(0.0, min(1.0, (tp.r - 0.48) * 2.0)) * (1.5 + chuva)) * 2.5, 0.0, 1.) * clamp((tp.r - 0.15) * 1.0, 0.0, 1.0)), 0.5, mix(0.0, 5.0, pow(max(min(1.0 - tp.r * 1.5, 1.0), 0.0), 1.2))), 0.0, noite);
    highp vec4 fc = FOG_COLOR;
    highp vec3 fc3 = FOG_COLOR.rgb;
    highp vec3 ctemp =  mix(mix(mix(color.rgb * 0.5, color.rgb * 0.6, dia), es(pos * 5.0), noite), FOG_COLOR.rgb, dsol);
    highp vec3 cou = vec3(0.0,0.0,0.0);
 
 if(FOG_CONTROL.x < 0.001){
    cou = mix(ctemp, fc3, chuva);
}else{
    cou = Cc(pos.xz * 9.0 + TIME * 0.002, ctemp, fc, dia, noite, dsol, chuva);
    cou = mix(cou, FOG_COLOR.rgb, mix(smoothstep(0.3, 1.0, posc * 1.7), smoothstep(0.0, 0.5, pow(posc * 1.7, 0.65)), chuva));
}
    gl_FragColor = vec4(cou,0.5);
}