float fogify(float x, float w) {
    return w / (x * x + w);
}

vec3 round_sun(float Dist) {
    Dist = Dist * 0.5 + 0.5;

    const vec3 SUN_COLOR  = vec3(5.0, 3.5, 0.8);
    const vec3 MOON_COLOR = vec3(0.55, 0.75, 1.4);

    float What = sunriseStrength + sunsetStrength;

    // Sun body
    vec3 Color = SUN_COLOR *
        (1.0 - smoothstep(0.0, 0.0015, 1.0 - Dist)) *
        (dayStrength + What);

    // Moon body
    Color += MOON_COLOR *
        smoothstep(0.9992, 1.0, 1.0 - Dist) *
        (nightStrength + What);

    // Cinematic moon glow
    float MoonGlow =
        smoothstep(0.96, 1.0, 1.0 - Dist) *
        0.35 *
        nightStrength;

    Color += vec3(0.25, 0.4, 0.9) * MoonGlow;

    // Outer soft halo
    float Halo =
        smoothstep(0.90, 1.0, 1.0 - Dist) *
        0.12 *
        nightStrength;

    Color += vec3(0.15, 0.25, 0.6) * Halo;

    return Color;
}

vec3 get_sun_glare(float Dist) {
    float Visibility = sunsetStrength + sunriseStrength;

// Fake cinematic godrays
float Rays =
    pow(max(Dist * 0.5 + 0.5, 0.0), 6.0);

Rays *= 0.35;

    const vec3 SUN_GLARE = to_linear(vec3(f_SUN_GLARE_R, f_SUN_GLARE_G, f_SUN_GLARE_B));

    vec3 FinalGlare =
    SUN_GLARE *
    Glow *
    Visibility *
    WarmTint;

FinalGlare +=
    vec3(1.0, 0.85, 0.6) *
    Rays *
    Visibility;

return FinalGlare;
}

vec3 get_clouds(vec3 PlayerPos, vec3 PlayerPosN, vec3 SunGlare, vec3 SkyColor) {
    vec2 CloudPos = PlayerPos.xz / (PlayerPos.y + length(PlayerPos.xz) / 6);

    const float ACTUAL_CLOUD_SPEED = CLOUD_SPEED / 100.;
    float Animation = float(frameTimeCounter) * ACTUAL_CLOUD_SPEED;
    CloudPos += cameraPosition.xz / 512;
    CloudPos = (CloudPos + Animation) * 98;

    float Noise =
    fbm_clouds(CloudPos * 1.8, CLOUD_QUALITY);

float Noise2 =
    fbm_clouds(
        CloudPos * 0.9 +
        vec2(frameTimeCounter * 0.002),
        CLOUD_QUALITY
    );

Noise = mix(Noise, Noise2, 0.35);

    float CloudAmount = CLOUD_AMOUNT / 100.0 + rainStrength / 10;
    Noise *= smoothstep(
    0.0,
    0.32 - CLOUD_OPACITY,
    Noise - 0.64 + CloudAmount
);
    Noise *= smoothstep(-0.1, 0.35, PlayerPosN.y);

    // Ultimate RTX raytraced ptgi 2000 cloud lighting
    const float DENSITY = 0.6;
    float Transmittance = exp(-Noise * DENSITY);
    float Absorbtion = fbm_clouds(CloudPos + to_player_pos(sunOrMoonPosN).xz * 8, 2);
    Absorbtion = pow4(Absorbtion * 2);

    float LHeight = sin(sunAngleAtHome * PI * 2);
    vec3 CloudColorRaw = (SKY_GROUND * 1.2 + SunGlare) * smoothstep(0, 0.1, abs(LHeight));
    vec3 CloudColor = (CloudColorRaw + vec3(0.1, 0.15, 0.25)) * exp(-Absorbtion * DENSITY);
    CloudColor += SKY_GROUND * Transmittance;

    return SkyColor * Transmittance + CloudColor * Noise;
}

vec3 get_sky(vec3 ViewPosN, vec3 SunGlare) {
    float upDot = dot(ViewPosN, gbufferModelView[1].xyz) + 0.1; //not much, what's up with you?

    vec3 MixedColor = mix(SKY_TOP, SKY_GROUND + SunGlare, fogify(max(upDot, 0.0), 0.03));
    return MixedColor;
}

float get_stars(vec3 PlayerPos) {

    vec3 StarCoord =
        PlayerPos /
        (PlayerPos.y + length(PlayerPos.xz));

    // Slow star movement
    StarCoord.x += frameTimeCounter * 0.00035;

    const float ACTUAL_STAR_SIZE =
        STAR_SIZE * 768.0;

    StarCoord =
        floor(StarCoord * ACTUAL_STAR_SIZE)
        / ACTUAL_STAR_SIZE;

    // Smooth horizon fade
    float Visibility =
        smoothstep(0.0, 0.18, StarCoord.y);

    #ifdef DIMENSION_OVERWORLD
    Visibility *= nightStrength;
    #endif

    // Random stars
    float Stars =
    max(0, random(StarCoord.xz) - 0.992);

float Twinkle =
    sin(frameTimeCounter * 2.0 +
    StarCoord.x * 40.0 +
    StarCoord.z * 40.0) * 0.15 + 0.85;

return Stars * 42.0 * Visibility * STAR_STRENGTH * Twinkle;

    // Soft twinkling
    Stars *=
        0.8 +
        sin(frameTimeCounter * 1.5 + StarCoord.x * 50.0) * 0.2;

    // Brightness
    Stars *= 90.0;

    return Stars * Visibility * STAR_STRENGTH;
}

vec3 get_end_sky(vec3 pos, vec3 PlayerPos) {
    const vec3 SkyT = to_linear(vec3(f_END_SKY_T_R, f_END_SKY_T_G, f_END_SKY_T_B));
    const vec3 SkyG1