Simple sky shader replacement

The idea is to replace the sky shader without any dependencies. Its just one simple shader.
This control the transition between day - night sky:

float day_factor = smoothstep(-0.2, 0.4, sun_elevation);
float night_factor = smoothstep(0.2, -0.4, sun_elevation);

shader_type sky;

uniform float sun_glow_scale : hint_range(0.01, 10.0) = 0.1;
uniform float star_density : hint_range(0.0, 0.01) = 0.003;
uniform float reduce_horizon : hint_range(0.0, 100.0) = 10.0;
uniform float reduce_sun_halo : hint_range(0.0, 1.0) = 0.9;
uniform vec3 day_sky_color : source_color = vec3(0.384, 0.482, 0.596); // Typical overcast Central European day sky
uniform vec3 day_horizon_color : source_color = vec3(0.643, 0.710, 0.769); // Hazy horizon common in temperate climates
uniform vec3 sunset_horizon_color : source_color = vec3(0.831, 0.478, 0.169); // Classic European sunset
uniform vec3 sunset_halo_color : source_color = vec3(0.996, 0.961, 0.447); // Sun corona through atmosphere
uniform vec3 night_sky_color : source_color = vec3(0.043, 0.051, 0.090); // Night sky with slight light pollution
uniform vec3 night_horizon_color : source_color = vec3(0.102, 0.114, 0.161); // Horizon glow from distant cities
uniform vec3 ground_color : source_color = vec3(0.200, 0.169, 0.133); // Central European soil/terrain
uniform vec3 sun_glow_color : source_color = vec3(1.0, 0.957, 0.898); // Natural sun color

float hash(vec3 p) {
    vec3 p3 = fract(p * 0.1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}

void sky() {
    vec3 view_dir = EYEDIR;
    vec3 sun_dir = LIGHT0_DIRECTION;
    
    float sun_elevation = sun_dir.y;
    
    float day_factor = smoothstep(0.0, 0.3, sun_elevation);
    float night_factor = smoothstep(-0.3, -0.5, sun_elevation);
    float sunset_factor = 1.0 - max(day_factor, night_factor);
    
    float horizon_blend = 1.0 - abs(view_dir.y);
    horizon_blend = pow(horizon_blend, reduce_horizon);
    
    vec3 sky_top = mix(day_sky_color, night_sky_color, night_factor);
    
    vec3 sky_horizon = mix(day_horizon_color, sunset_horizon_color, sunset_factor);
    sky_horizon = mix(sky_horizon, night_horizon_color, night_factor);
    
    vec3 final_sky = mix(sky_top, sky_horizon, horizon_blend);
    
    float sun_proximity = dot(view_dir, normalize(sun_dir));
    sun_proximity = smoothstep(reduce_sun_halo, 1.0, sun_proximity);
    
    float halo_glow = sunset_factor * sun_proximity;
    final_sky = mix(final_sky, sunset_halo_color, halo_glow * 0.5);
    
    float horizon_proximity = 1.0 - abs(view_dir.y);
    horizon_proximity = pow(horizon_proximity, 2.0);
    
    float horizon_glow = sunset_factor * horizon_proximity;
    final_sky = mix(final_sky, sunset_horizon_color, horizon_glow * 0.4);
    
    float ground_blend = smoothstep(0.0, -0.2, view_dir.y);
    final_sky = mix(final_sky, ground_color, ground_blend);
    
    float sun_glow = 0.0;
    
    if (sun_elevation > -0.1 && sun_glow_scale > 0.01) {
        float sun_intensity = dot(view_dir, normalize(sun_dir));
        float exponent = 100.0 / sun_glow_scale;
        sun_glow = pow(max(0.0, sun_intensity), exponent);
        final_sky += sun_glow_color * sun_glow * 2.0;
    }
    
    if (night_factor > 0.5) {
        vec3 star_pos = floor(view_dir * 500.0);
        float stars = hash(star_pos);
        stars = step(1.0 - star_density, stars) * night_factor;
        
        float brightness = hash(star_pos + 1.0);
        stars *= mix(0.5, 1.0, brightness);
        
        final_sky += vec3(stars);
    }
    
    COLOR = final_sky;
}
5 Likes

That’s really nice.

I forgot to add the ground, and I removed the cute pink color at sunset, this color only appear when there are clouds, realistically the horizon become orange and a yellow halo form around the sky. For reference the old version i posted earlier: