/* Procedural Stars System - Reflexa Shader
   Generates beautiful stars across the entire sky
   Note: Uses hash13 function from meteor_system.glsl
*/

// Generate stars procedurally across entire sky
vec4 generateProceduralStars(vec3 world_pos, float night_intensity) {
    // Normalize position for consistent star field
    vec3 sky_pos = normalize(world_pos);
    
    // Create star field with multiple layers for depth
    vec3 star_color = vec3(0.0);
    float star_alpha = 0.0;
    
    // Multiple star layers with different densities and sizes
    for (int layer = 0; layer < 3; layer++) {
        float scale = 30.0 + float(layer) * 20.0;
        vec3 scaled_pos = sky_pos * scale;
        vec3 cell = floor(scaled_pos);
        vec3 fract_pos = fract(scaled_pos);
        
        // Check surrounding cells for stars
        for (int x = -1; x <= 1; x++) {
            for (int y = -1; y <= 1; y++) {
                for (int z = -1; z <= 1; z++) {
                    vec3 offset = vec3(float(x), float(y), float(z));
                    vec3 cell_id = cell + offset;
                    
                    // Generate random star properties
                    float star_random = hash13(cell_id + vec3(float(layer) * 100.0));
                    
                    // Star density control - only 15% of cells have stars
                    if (star_random > 0.85) {
                        // Star position within cell
                        float star_x = hash13(cell_id * 1.1 + vec3(float(layer)));
                        float star_y = hash13(cell_id * 1.2 + vec3(float(layer)));
                        float star_z = hash13(cell_id * 1.3 + vec3(float(layer)));
                        vec3 star_pos_in_cell = vec3(star_x, star_y, star_z);
                        
                        // Calculate distance to star
                        vec3 to_star = (offset + star_pos_in_cell) - fract_pos;
                        float dist = length(to_star);
                        
                        // Star size and brightness variation
                        float star_size = mix(0.02, 0.08, hash13(cell_id * 2.0 + vec3(float(layer))));
                        float star_brightness = mix(0.6, 1.0, hash13(cell_id * 3.0 + vec3(float(layer))));
                        
                        // Create star with smooth falloff
                        if (dist < star_size) {
                            float intensity = pow(1.0 - (dist / star_size), 3.0) * star_brightness;
                            
                            // Star color variation - white to warm yellow
                            float color_variation = hash13(cell_id * 4.0 + vec3(float(layer)));
                            vec3 this_star_color = mix(
                                vec3(1.0, 1.0, 1.0),
                                vec3(1.0, 0.95, 0.8),
                                color_variation
                            );
                            
                            // Add twinkling effect
                            float twinkle = hash13(cell_id * 5.0 + vec3(float(layer)));
                            intensity *= mix(0.7, 1.0, twinkle);
                            
                            star_color += this_star_color * intensity;
                            star_alpha += intensity;
                        }
                    }
                }
            }
        }
    }
    
    // Apply night intensity
    star_color *= night_intensity;
    star_alpha *= night_intensity;
    
    // Enhance brightness
    star_color *= 2.5;
    star_alpha = clamp(star_alpha * 1.5, 0.0, 1.0);
    
    return vec4(star_color, star_alpha);
}
