shun_iwasawa 36e06f
#ifdef GL_ES
shun_iwasawa 36e06f
precision mediump float;
shun_iwasawa 36e06f
#endif
shun_iwasawa 36e06f
shun_iwasawa 36e06f
// Tweaked from  http://glsl.heroku.com/e#6015.0
shun_iwasawa 36e06f
shun_iwasawa 36e06f
shun_iwasawa 36e06f
// Posted by Trisomie21
shun_iwasawa 36e06f
shun_iwasawa 36e06f
shun_iwasawa 36e06f
uniform mat3 outputToWorld;
shun_iwasawa 36e06f
shun_iwasawa 36e06f
uniform vec4  color;
shun_iwasawa 36e06f
uniform float time;
shun_iwasawa 36e06f
uniform float brightness;
shun_iwasawa 36e06f
shun_iwasawa 36e06f
// Tweaked from  http://glsl.heroku.com/e#4982.0
shun_iwasawa 36e06f
float hash( float n ) { return fract(sin(n)*43758.5453); }
shun_iwasawa 36e06f
float rand(vec2 co){ return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); }
shun_iwasawa 36e06f
shun_iwasawa 36e06f
float noise( in vec2 x )
shun_iwasawa 36e06f
{
shun_iwasawa 36e06f
	vec2 p = floor(x);
shun_iwasawa 36e06f
	vec2 f = fract(x);
shun_iwasawa 36e06f
    	f = f*f*(3.0-2.0*f);
shun_iwasawa 36e06f
    	float n = p.x + p.y*57.0;
shun_iwasawa 36e06f
    	float res = mix(mix(hash(n+0.0), hash(n+1.0),f.x), mix(hash(n+57.0), hash(n+58.0),f.x),f.y);
shun_iwasawa 36e06f
    	return res;
shun_iwasawa 36e06f
}
shun_iwasawa 36e06f
shun_iwasawa 36e06f
vec3 cloud(vec2 p) {
shun_iwasawa 36e06f
	float f = 0.0;
shun_iwasawa 36e06f
    	f += 0.50000*noise(p*1.0*10.0); 
shun_iwasawa 36e06f
    	f += 0.25000*noise(p*2.0*10.0); 
shun_iwasawa 36e06f
    	f += 0.12500*noise(p*4.0*10.0); 
shun_iwasawa 36e06f
    	f += 0.06250*noise(p*8.0*10.0);	
shun_iwasawa 36e06f
	f *= f;
shun_iwasawa 36e06f
  
shun_iwasawa 36e06f
	return color.rgb * color.a * f * .6;
shun_iwasawa 36e06f
}
shun_iwasawa 36e06f
shun_iwasawa 36e06f
const float SPEED       = 0.01;
shun_iwasawa 36e06f
const float DENSITY	    = 1.5;
shun_iwasawa 36e06f
shun_iwasawa 36e06f
void main( void )
shun_iwasawa 36e06f
{	
shun_iwasawa 36e06f
	vec2 pos = .01 * (outputToWorld * vec3(gl_FragCoord.xy, 1.0)).xy;
shun_iwasawa 36e06f
  
shun_iwasawa 36e06f
	// Nebulous cloud - It's intended as background, ie it doesn't block stars visibility.
shun_iwasawa 36e06f
  // Stars ADD to this. 
shun_iwasawa 36e06f
	vec3 color = cloud(pos);
shun_iwasawa 36e06f
	
shun_iwasawa 36e06f
	// Stars Field - this is the idea: each star is drawn in a 'star cell' which results from
shun_iwasawa 36e06f
  // FLOORING a point function p(x,y) of the pixel coordinates. A cell's edges correspond to
shun_iwasawa 36e06f
  // coordinated lines of the form: p_x(x,y) = int, p_y(x, y) = int.
shun_iwasawa 36e06f
  
shun_iwasawa 36e06f
  // The problem lies in finding a function p which is suitable, ie p_x's and p_y's gradients should
shun_iwasawa 36e06f
  // be as orthogonal and with finite strictly positive norm as possible.
shun_iwasawa 36e06f
  
shun_iwasawa 36e06f
  // Changing to polar coordinates is simplest - when the radius (distance from origin)
shun_iwasawa 36e06f
  // is high, moving in radius and arc distance is almost orthogonal. Plus, star 'discs' are harder
shun_iwasawa 36e06f
  // to spot than star 'rows', since they are curved.
shun_iwasawa 36e06f
  
shun_iwasawa 36e06f
  // I think that a suitable deformation of the identity grid based on sin and cos exists,
shun_iwasawa 36e06f
  // but couldn't find it...  ^.^'
shun_iwasawa 36e06f
  
shun_iwasawa 36e06f
  float dist = length(pos);
shun_iwasawa 36e06f
	vec2 coord = vec2(dist, atan(pos.y, pos.x)/* / (3.1415926*2.0)*/);    // Pseudo-polar coordinates
shun_iwasawa 36e06f
  
shun_iwasawa 36e06f
	vec2  p = 40.0 * vec2(coord.x,                      // radius
shun_iwasawa 36e06f
    floor(coord.x + 1.0) * coord.y +                  // arc distance (floor helps stabilizing cell shapes, and 1.0 to avoid flooring to 0)
shun_iwasawa 36e06f
    hash(floor(40.0 * coord.x)));                     // shifts the star 'discs' along the arc, by a pseudo-random value (helps avoiding 'star rows', at least along the radial direction)
shun_iwasawa 36e06f
  
shun_iwasawa 36e06f
	vec2 uv = 2.0 * fract(p) - 1.0;                     // Pixel position in the cell, in [-1,1]^2 coordinates
shun_iwasawa 36e06f
  
shun_iwasawa 36e06f
  float cellValue      = abs(2.0 * fract(rand(floor(p)) + SPEED * time) - 1.0);
shun_iwasawa 36e06f
	float cellBrightness = clamp((cellValue - 0.9) * brightness * 10.0, 0.0, 1.0);
shun_iwasawa 36e06f
shun_iwasawa 36e06f
	color +=  clamp(
shun_iwasawa 36e06f
    (1.0 - 2.0 * length(uv)) *                                            // Comment this line to see the star cells
shun_iwasawa 36e06f
    cellBrightness, 0.0, 1.0);
shun_iwasawa 36e06f
shun_iwasawa 36e06f
shun_iwasawa 36e06f
  gl_FragColor = vec4(color, 1.0);
shun_iwasawa 36e06f
}