- float rf1 = fabs((hor_rotation - SGD_PI) / SGD_PI); // 0.0 .. 1.0
- float rf2 = avf * pow(rf1*rf1, 1/sif) * 1.0639 * _saturation;
+ float rf1 = fabs(fmod(hor_rotation, SGD_2PI) - SGD_PI) / SGD_PI;
+ float rf2 = avf * pow(rf1*rf1, 1/sif) * 1.0639 * _saturation * _scattering;
+
+ // HACK: clamp rf2 to 1.0.
+ // Somehow, rf2 is huge at certain sun angles (around midnight), which results in
+ // rf3 being negative and hence negative fog colors and weird display issues...
+ // Something more fundamental may be wrong with the formulas here...
+ if (rf2>1.0) rf2 = 1.0;
+