 uniform vec4 cPosition_World;
 uniform float uTime;
 varying vec3 vDirection;
-const   vec3 unit = normalize( vec3( 1 ) );   // radius of unit sphere, representing the sun
+
+const   float sun_size = 1.0;  // radius of sun sphere
+const   float sun_flare_size = 0.5;
+
+const   float sky_saturation = 0.5;       // how blue should the sky be (if we look straight up)
+const   float sky_horizon_offset = -0.3;  // between -1 and 1, moves horizon down if negative, moves horizon up if positive 
+
 const   float cloudiness = 0.2;
-const   float horizon_blueness = 0.02;        // how blue should the sky be at the horizon
-const   float sky_bluestep = 1.0;             // how quickly should the sky turn blue when we look up (default = 1.0/1x)
-const   float sky_blueness = 0.5;             // how blue should the sky be at the top (if we look straight up)
-const   float sky_horizon_offset = -0.2;      // between -1 and 1, if negative, moves horizon down, if positive moves horizon up
-const   float sun_flare_size = 0.5;
 
 // noise functions by Inigo Quilez
 float hash(float n) { return fract(sin(n) * 1e4); }

    acc.rgb /= acc.w + 0.0001;
    float alpha = smoothstep( 0.7, 1.0, acc.w );

    acc.rgb -= 0.6 * vec3( 0.8, 0.75, 0.7 ) * alpha * pow( unit, vec3( 13.0 ) );
    acc.rgb += 0.2 * vec3( 1.3, 1.2, 1.0 ) * ( 1.0 - alpha ) * pow( unit, vec3( 5.0 ) );

    return acc;
}

void main() {
    // procedural sky
    vec3 eye = normalize( vDirection );

    // instead of clamping, we want to extend the range
    float eye_for_sky = clamp( eye.y, sky_horizon_offset, 1.0 );

    // now transform to [0, 1.0]
    eye_for_sky = ( eye_for_sky - sky_horizon_offset ) / ( 1.0 + sky_horizon_offset );

    float eye_h = clamp( eye_for_sky, horizon_blueness, 1.0 );
    float blueness = min( eye_h * sky_blueness, 1.0 );

    vec3 sky = vec3( pow( 1.0 - blueness, 2.0 )       // less red as we move up, quadratic
                   , 1.0 - blueness                   // less green as we move up, linear
                   , 0.6 + ( 1.0 - blueness ) * 0.4 );            // blue depends on how far up we are

    eye.y = clamp( eye.y, 0.0, 1.0 );

    sky += sun( eye );

}

vec3 sun( vec3 v ) {
    float sun_body = clamp( dot( v, vec3( sun_size ) ), 0.0, 1.0 );
    return vec3( 1.6, 1.4, 1.0 ) * 0.47 * pow( sun_body, 350.0 )
         + vec3( 0.8, 0.9, 1.0 ) * 0.40 * pow( sun_body, ( 1.0 - sun_flare_size ) * 100.0 );
}
