precision mediump float;

uniform sampler2D noise_sparse;
uniform sampler2D noise_fine;

uniform vec2  ires;
uniform float scroll_speed;
uniform float warp_boost;
uniform float warp_opacity;
uniform float time;

const float noise_sparse_scale = 0.0007;
const float noise_fine_scale = 0.004;
const float warp_clouds_scale = 0.0003;
const vec2 scroll_dir = vec2(1,0);
const vec2 blink_scroll = vec2(50.0, 0);
const float PI = 3.1415;

void main() {
	vec2 uv=gl_FragCoord.xy/ires.xy;
	float scroll = scroll_speed * time;
	float sparse = texture2D(noise_sparse, (gl_FragCoord.xy + scroll_dir * scroll)*noise_sparse_scale).r;
	sparse = ((sparse * sparse) - 0.2) * 1.2;
	float fine = texture2D(noise_fine, (gl_FragCoord.xy + scroll_dir * scroll)*noise_fine_scale).r;
	fine = fine - 0.62;
	if (fine > 0.) {
		fine = fine * 10.0;
	}
	if (fine > 0.3) {
		float fine_blink_fade = texture2D(noise_sparse, (gl_FragCoord.xy + blink_scroll*time)*noise_sparse_scale).r;
		fine = fine - ((fine_blink_fade * fine_blink_fade) - 0.2) * 1.2;
	}
	vec4 clouds = vec4(vec3(0.3, 0.5, 0.7) * sparse, 1.0);
	vec4 stars = vec4(fine);

	vec2 warp_scroll = scroll_dir * scroll;
	vec2 warp_uv = (gl_FragCoord.xy * sin(uv.y * PI) + warp_scroll)*warp_clouds_scale;
	vec2 warp_uv_inv = (gl_FragCoord.xy * sin((abs(uv.y-0.5)) * uv.x * PI) + warp_scroll*0.5)*warp_clouds_scale;
	float warp_clouds_sparse = texture2D(noise_sparse, vec2(0.5,1) * warp_uv).r;
	float warp_clouds_fine = texture2D(noise_sparse, vec2(2,1) * warp_uv_inv).r;
	
	vec4 warp = vec4(vec3(0), 1);
	warp += vec4(vec3(0.3,0.2,0.9)*warp_clouds_sparse, 0);
	warp += vec4(vec3(0.5,0.0,0.5)*(warp_clouds_fine*0.5+warp_clouds_sparse)*abs(uv.y-0.5), 0);
	
	gl_FragColor = mix(clouds + stars, warp*warp_boost, warp_opacity);
}