varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
uniform sampler2D u_texture;
uniform float time;

uniform vec2 SIZE;

uniform int num_of_planets;

uniform float[10] planet_colors_r;
uniform float[10] planet_colors_g;
uniform float[10] planet_colors_b;

uniform float[10] planet_pos_x;
uniform float[10] planet_pos_y;

uniform float[10] planet_radiuses;
uniform float[10] planet_glow_radiuses;

uniform float[10] planet_glow_noise;

void main(void)
{
    //vec4 c = texture2D(u_texture, v_texCoord);
    vec2 pix_pos = v_texCoord * SIZE;
    float dist_ratio = -1;
    float current_noise = 0.0;
    vec3 current_color = vec3(0);
    for(int i = 0; i < num_of_planets; i++)
    {
        float x = planet_pos_x[i];
        float y = planet_pos_y[i];
        float r = planet_radiuses[i];
        float glow_r = planet_glow_radiuses[i];

        //float size_ratio = SIZE.x/SIZE

        float dist = distance(vec2(x,y), pix_pos);

        //Right 
        if(dist >= r && dist <= glow_r)
        {
            //Fade plant glow from both directions!
            float dist_r = dist-r;
            float dif = glow_r - r;
            float start_fade = 1.0;

/*            if(dist-r < dif*0.05)
            {
                start_fade = (dist - r)/(dif*0.05);
            }
            */
            dist = dist - r;
            dist_ratio = dist/dif;
            //dist_ratio = //dist_ratio * dist_ratio;
            dist_ratio = 1 - dist_ratio;//*dist_ratio*dist_ratio;
            dist_ratio = dist_ratio*dist_ratio*dist_ratio;//sin(3.141 * 0.5 + 3.141*0.5 * dist_ratio);
            dist_ratio *= start_fade; 

            current_color = vec3(planet_colors_r[i], planet_colors_g[i], planet_colors_b[i]);
            current_noise = planet_glow_noise[i];
            break;
        }
    }

    vec4 final_color = vec4(0);

    if(dist_ratio > -1)
    {
        final_color = vec4(current_color, 0.1) * dist_ratio * (0.8 + 0.2 * current_noise);
    }

    gl_FragColor = final_color;
}