// https://www.shadertoy.com/view/ldXXDj

// Created by inigo quilez - iq/2014
//   https://www.youtube.com/c/InigoQuilez
//   https://iquilezles.org/
// Creative Commons license.


// A simple and cheap 2D shader to accompany the Pirates of the Caribean music.
#pragma iChannel0 stars.jpg
#pragma iChannel1 rgba_noise.png

float fbm( vec2 p )
{
    return 0.5000*texture( iChannel1, p*1.00 ).x + 
           0.2500*texture( iChannel1, p*2.02 ).x + 
           0.1250*texture( iChannel1, p*4.03 ).x + 
           0.0625*texture( iChannel1, p*8.04 ).x;
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    float time = mod( iTime, 60.0 );
	vec2 p = (2.0*fragCoord-iResolution.xy) / iResolution.y;
    vec2 i = p;

    // camera
    p += vec2(1.0,3.0)*0.001*2.0*cos( iTime*5.0 + vec2(0.0,1.5) );    
    p += vec2(1.0,3.0)*0.001*1.0*cos( iTime*9.0 + vec2(1.0,4.5) );    
    float an = 0.3*sin( 0.1*time );
    float co = cos(an);
    float si = sin(an);
    p = mat2( co, -si, si, co )*p*0.85;
    
    // water
    vec2 q = vec2(p.x,1.0)/p.y;
    q.y -= 0.9*time;    
    vec2 off = texture( iChannel0, 0.1*q*vec2(1.0,2.0) - vec2(0.0,0.007*iTime) ).xy;
    q += 0.4*(-1.0 + 2.0*off);
    vec3 col = 0.2*sqrt(texture( iChannel0, 0.05*q *vec2(1.0,4.0) + vec2(0.0,0.01*iTime) ).zyx);
    float re = 1.0-smoothstep( 0.0, 0.7, abs(p.x-0.6) - abs(p.y)*0.5+0.2 );
    col += 1.0*vec3(1.0,0.9,0.73)*re*0.2*(0.1+0.9*off.y)*5.0*(1.0-col.x);
    float re2 = 1.0-smoothstep( 0.0, 2.0, abs(p.x-0.6) - abs(p.y)*0.85 );
    col += 0.7*re2*smoothstep(0.35,1.0,texture( iChannel1, 0.075*q *vec2(1.0,4.0) ).x);
    
    // sky
    vec3 sky = vec3(0.0,0.05,0.1)*1.4;
    // stars    
    sky += 0.5*smoothstep( 0.95,1.00,texture( iChannel1, 0.25*p ).x);
    sky += 0.5*smoothstep( 0.85,1.0,texture( iChannel1, 0.25*p ).x);
    sky += 0.2*pow(1.0-max(0.0,p.y),2.0);
    // clouds    
    float f = fbm( 0.002*vec2(p.x,1.0)/p.y );
    vec3 cloud = vec3(0.3,0.4,0.5)*0.7*(1.0-0.85*smoothstep(0.4,1.0,f));
    sky = mix( sky, cloud, 0.95*smoothstep( 0.4, 0.6, f ) );
    sky = mix( sky, vec3(0.33,0.34,0.35), pow(1.0-max(0.0,p.y),2.0) );
    col = mix( col, sky, smoothstep(0.0,0.1,p.y) );

    // horizon
    col += 0.1*pow(clamp(1.0-abs(p.y),0.0,1.0),9.0);

    // moon
    float d = length(p-vec2(0.6,0.5));
    vec3 moon = vec3(0.98,0.97,0.95)*(1.0-0.1*smoothstep(0.2,0.5,f));
    col += 0.8*moon*exp(-4.0*d)*vec3(1.1,1.0,0.8);
    col += 0.2*moon*exp(-2.0*d);
    moon *= 0.85+0.15*smoothstep(0.25,0.7,fbm(0.05*p+0.3));
    col = mix( col, moon, 1.0-smoothstep(0.2,0.22,d) );
    
    // postprocess
    col = pow( 1.4*col, vec3(1.5,1.2,1.0) );    
    col *= clamp(1.0-0.3*length(i), 0.0, 1.0 );

    // fade
    col *=       smoothstep( 3.0, 6.0,time);
    col *= 1.0 - smoothstep(44.0,50.0,time);

    fragColor = vec4( col, 1.0 );
}