#version 300 es

// Based on "A Practical Analytic Model for Daylight" - Preetham et al. (1999)
// https://www.researchgate.net/publication/220720443_A_Practical_Analytic_Model_for_Daylight

#define M_PI 3.14159

precision mediump float;

uniform vec2 u_resolution;
uniform vec2 u_mouse;

out vec4 FragColor;

float perez(float A, float B, float C, float D, float E, float theta, float gamma) {
    float cosGamma = cos(gamma);
    float cosTheta = max(0.001, cos(theta)); // prevent division by 0 near the horizon

    return (1.0 + A * exp(B / cosTheta)) * (1.0 + C * exp(D * gamma) + E * pow(cosGamma, 2.0));
}

float distribution(float zenith, float A, float B, float C, float D, float E, float theta, float theta_s, float gamma) {
    return zenith * perez(A, B, C, D, E, theta, gamma) / perez(A, B, C, D, E, 0.0, theta_s);
}

vec2 chromacity(float turbidity, float theta, float theta_s, float gamma) {
    vec3 turbidity_pow2 = vec3(pow(turbidity, 2.0), turbidity, 1.0);
    vec4 theta_s_pow3 = vec4(pow(theta_s, 3.0), pow(theta_s, 2.0), theta_s, 1.0);

    float zenith_x = dot(turbidity_pow2 * mat4x3(
         0.0017, -0.0290,  0.1169,
        -0.0037,  0.0638, -0.2120,
         0.0021, -0.0320,  0.0605,
         0.0000,  0.0039,  0.2589
    ), theta_s_pow3);

    float zenith_y = dot(turbidity_pow2 * mat4x3(
         0.0028, -0.0421,  0.1535,
        -0.0061,  0.0897, -0.2676,
         0.0032, -0.0415,  0.0667,
         0.0000,  0.0052,  0.2669
    ), theta_s_pow3);

    float A_x = -0.0193 * turbidity - 0.2592;
    float B_x = -0.0665 * turbidity + 0.0008;
    float C_x = -0.0004 * turbidity + 0.2125;
    float D_x = -0.0641 * turbidity - 0.8989;
    float E_x = -0.0033 * turbidity + 0.0452;

    float A_y = -0.0167 * turbidity - 0.2608;
    float B_y = -0.0950 * turbidity + 0.0092;
    float C_y = -0.0079 * turbidity + 0.2102;
    float D_y = -0.0441 * turbidity - 1.6537;
    float E_y = -0.0109 * turbidity + 0.0529;

    return vec2(
        distribution(zenith_x, A_x, B_x, C_x, D_x, E_x, theta, theta_s, gamma),
        distribution(zenith_y, A_y, B_y, C_y, D_y, E_y, theta, theta_s, gamma)
    );
}

float luminance(float turbidity, float theta, float theta_s, float gamma) {
    float chi = (4.0 / 9.0 - turbidity / 120.0) * (M_PI - 2.0 * theta_s);
    float zenith = (4.0453 * turbidity - 4.9710) * tan(chi) - 0.2115 * turbidity + 2.4192;

    float A =  0.1787 * turbidity - 1.4630;
    float B = -0.3554 * turbidity + 0.4275;
    float C = -0.0227 * turbidity + 5.3251;
    float D =  0.1206 * turbidity - 2.5771;
    float E = -0.0670 * turbidity + 0.3703;

    return distribution(zenith, A, B, C, D, E, theta, theta_s, gamma);
}

vec3 CIE_xyY_to_CIE_XYZ(vec2 chromacity, float luminance) {
    float X = chromacity.x / chromacity.y * luminance;
    float Z = (1.0 - chromacity.x - chromacity.y) / chromacity.y * luminance;

    return vec3(X, luminance, Z);
}

vec3 CIE_XYZ_to_sRGB(vec3 XYZ) {
    return mat3(
         3.2406, -0.9689,  0.0557,
        -1.5372,  1.8758, -0.2040,
        -0.4986,  0.0415,  1.0570
    ) * XYZ;
}

// Ad hoc tonemapping with brightness scaled by the sun angle.
vec3 tonemap(vec3 sRGB, float theta_s) {
    return tanh(0.05 * max(0.01, cos(theta_s)) * sRGB);
}

vec4 preetham(float turbidity, float theta, float theta_s, float gamma) {
    vec2 chromacity = chromacity(turbidity, theta, theta_s, gamma);
    float luminance = luminance(turbidity, theta, theta_s, gamma);

    vec3 XYZ = CIE_xyY_to_CIE_XYZ(chromacity, luminance);
    vec3 sRGB = CIE_XYZ_to_sRGB(XYZ);

    return vec4(tonemap(sRGB, theta_s), 1.0);
}

float angle(vec2 zeniths, vec2 azimuths) {
    float cosDelta = sin(azimuths.x) * sin(azimuths.y) + cos(azimuths.x) * cos(azimuths.y);
    float cosGamma = sin(zeniths.x) * sin(zeniths.y) * cosDelta + cos(zeniths.x) * cos(zeniths.y);

    return acos(clamp(cosGamma, -1.0, 1.0)); // prevent NaNs from numerical instability
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution) / min(u_resolution.x, u_resolution.y);

    float theta = length(uv) * M_PI;
    float view_azimuth = atan(uv.y, uv.x);

    if (theta > M_PI / 2.0) {
        FragColor = vec4(vec3(0.0), 1.0);
        return;
    }

    vec2 mouse = (u_mouse - 0.5 * u_resolution) / min(u_resolution.x, u_resolution.y);

    float theta_s = min(length(mouse) * M_PI, M_PI / 2.0);
    float sun_azimuth = atan(mouse.y, mouse.x);

    float gamma = angle(
        vec2(theta, theta_s),
        vec2(view_azimuth, sun_azimuth)
    );

    FragColor = preetham(2.0, theta, theta_s, gamma);
    FragColor.rgb = pow(FragColor.rgb, vec3(1.0 / 2.2)); // gamma correction
}