#ifdef GL_ES
precision mediump float;
#endif

uniform vec2      iResolution;           // viewport resolution (in pixels)
uniform float     iGlobalTime;           // shader playback time (in seconds)

varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
varying vec2 v_position;

uniform vec2 dir;
uniform float speed;
uniform float stageTime;


#define CLOUDS_SCALE 2.0
#define CLOUDS_TIME_SCALE 0.2
#define ADDNOISE(n) k = pow(2.0, n); gray += noise(vec3((uv.x * CLOUDS_SCALE) * k, uv.y * CLOUDS_SCALE * k, 0.0)) / k; total += 1.0/k;

// Noise function adapted from a shader found on ShaderToy.com
float hash(float n) { return fract(sin(n)*753.5453123); }
float noise(in vec3 x)
{
    // Perlin noise
    vec3 p = floor(x);
    vec3 f = fract(x);
    f = f*f*(3.0 - 2.0*f);

    float n = p.x + p.y*157.0 + 113.0*p.z;
    return mix(mix(mix(hash(n + 0.0),   hash(n + 1.0),   f.x),
                   mix(hash(n + 157.0), hash(n + 158.0), f.x), f.y),
               mix(mix(hash(n + 113.0), hash(n + 114.0), f.x),
                   mix(hash(n + 270.0), hash(n + 271.0), f.x), f.y), f.z);
}

vec2 hash22(vec2 p)
{
    p = vec2( dot(p,vec2(127.1,311.7)),
              dot(p,vec2(269.5,183.3)));

    return vec2(-1,-1) + 2.0 * vec2(fract(sin(p.x)*43758.5453123),fract(sin(p.y)*43758.5453123));
}
float hash21(vec2 p)
{
    p = vec2( dot(p,vec2(127.1,311.7)),
              dot(p,vec2(269.5,183.3)));

    return -1.0 + 2.0 * fract(sin(p.x)*43758.5453123);
}
float perlin_noise(vec2 p)
{
    vec2 pi = floor(p);
    vec2 pf = p - pi;

    vec2 w = pf * pf * (3.0 - 2.0 * pf);

    return mix(mix(dot(hash22(pi + vec2(0.0, 0.0)), pf - vec2(0.0, 0.0)), 
                   dot(hash22(pi + vec2(1.0, 0.0)), pf - vec2(1.0, 0.0)), w.x), 
               mix(dot(hash22(pi + vec2(0.0, 1.0)), pf - vec2(0.0, 1.0)), 
                   dot(hash22(pi + vec2(1.0, 1.0)), pf - vec2(1.0, 1.0)), w.x),
               w.y);
}
float value_noise(vec2 p)
{
    vec2 pi = floor(p);
    vec2 pf = p - pi;

    vec2 w = pf * pf * (3.0 - 2.0 * pf);

    return mix(mix(hash21(pi + vec2(0.0, 0.0)), hash21(pi + vec2(1.0, 0.0)), w.x),
               mix(hash21(pi + vec2(0.0, 1.0)), hash21(pi + vec2(1.0, 1.0)), w.x),
               w.y);
}
float noise_sum(vec2 p)
{
    float f = 0.0;
    p = p * 5.0;
    for(int i=0;i<5;i++)
    {
        f+=perlin_noise(p)/pow(2,i); 
        p = (2) * p;
    }
    return f;
}
float clouds(vec2 p)
{
    float f=0;
    float total = 0;
    float k=0;
    int max=5;
    for(int i=2;i<max;i++)
    {
        k = pow(2.0, i); 
        float shift = iGlobalTime * 0.02;
        f += perlin_noise(vec2(p.x*2*k,p.y*2*k)+dir*shift*(i+1.0)*0.2*k)/k; 
        total += 1.0/k;
    }
    f/=4;

   return f;
}



void main()
{
    //获得坐标
    vec2 pos = vec2(v_texCoord.x*iResolution.x,v_texCoord.y*iResolution.y);
    //底层云雾坐标
    vec2 pos2=pos;
    pos2+=dir*speed*iGlobalTime;
    pos2=pos2/(iResolution.x*1+10);//aaa
    
    vec4 color=vec4(0,0,0,0);
    float f=0;

    //底层云雾--------------------------
    f=noise_sum(pos2)*0.7+0.4;//bbb
    color+=vec4(vec3(f*0.3+0.6),f+2)*0.35;
    color=clamp(color,0,1);
    //---------------------------------------------

    //底色加成--------------------------
    //顶0*0.6+0.4-------------1*0.6+0.4底
    //顶0.4---------------------------1底
    f=clamp(abs((v_texCoord.y-stageTime)*0.6+0.4),0,1);
    color-=vec4(vec3((1-f)*0.2),0);                    
    //----------------------------------------------

	gl_FragColor=color;
}
