2021-04-10 09:14:16 +00:00
|
|
|
#version 330 core
|
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
layout(location = 0) out vec4 fragColor;
|
2021-04-10 09:14:16 +00:00
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
in vec3 ray_dir;
|
|
|
|
in vec3 ray_dir_view;
|
2021-04-10 09:14:16 +00:00
|
|
|
|
2023-04-12 20:50:03 +00:00
|
|
|
uniform bool is_envmap;
|
2023-04-07 06:17:37 +00:00
|
|
|
uniform sampler2D sky_view_tex;
|
|
|
|
uniform sampler2D transmittance_tex;
|
2021-04-10 09:14:16 +00:00
|
|
|
|
2021-08-19 11:09:44 +00:00
|
|
|
uniform vec3 fg_SunDirection;
|
|
|
|
uniform float fg_CameraDistanceToEarthCenter;
|
|
|
|
uniform float fg_EarthRadius;
|
|
|
|
uniform vec3 fg_CameraViewUp;
|
2021-07-30 19:34:57 +00:00
|
|
|
|
2021-08-19 11:09:44 +00:00
|
|
|
const float ATMOSPHERE_RADIUS = 6471e3;
|
2023-04-07 06:17:37 +00:00
|
|
|
|
2023-04-12 20:50:03 +00:00
|
|
|
const float SUN_HALF_ANGULAR_DIAMETER = 0.0047560222116845481; // radians(0.545 deg / 2)
|
|
|
|
const float SUN_COS_HALF_ANGULAR_DIAMETER = 0.9999886901476798392; // cos(radians(0.545 deg / 2))
|
|
|
|
const float SUN_SIN_HALF_ANGULAR_DIAMETER = 0.0047560042817014138; // sin(radians(0.545 deg / 2))
|
2023-02-19 15:47:55 +00:00
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
// math.glsl
|
|
|
|
float M_PI();
|
2023-04-12 20:50:03 +00:00
|
|
|
float sqr(float x);
|
|
|
|
float saturate(float x);
|
2023-04-07 06:17:37 +00:00
|
|
|
// atmos_spectral.glsl
|
|
|
|
vec4 get_sun_spectral_irradiance();
|
|
|
|
vec3 linear_srgb_from_spectral_samples(vec4 L);
|
2023-04-12 20:50:03 +00:00
|
|
|
// exposure.glsl
|
|
|
|
vec3 apply_exposure(vec3 color);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Limb darkening
|
|
|
|
* http://www.physics.hmc.edu/faculty/esin/a101/limbdarkening.pdf
|
|
|
|
*/
|
|
|
|
vec4 get_sun_darkening_factor(float cos_theta)
|
|
|
|
{
|
|
|
|
// Coefficients sampled for wavelengths 630, 560, 490, 430 nm
|
|
|
|
const vec4 u = vec4(1.0);
|
|
|
|
const vec4 alpha = vec4(0.429, 0.502, 0.575, 0.643);
|
|
|
|
float sin_theta = sqrt(1.0 - sqr(cos_theta));
|
|
|
|
float center_to_edge = saturate(sin_theta / SUN_SIN_HALF_ANGULAR_DIAMETER);
|
|
|
|
float mu = sqrt(1.0 - sqr(center_to_edge));
|
|
|
|
vec4 factor = vec4(1.0) - u * (vec4(1.0) - pow(vec4(mu), alpha));
|
|
|
|
return factor;
|
|
|
|
}
|
2021-04-10 09:14:16 +00:00
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2023-04-16 05:17:49 +00:00
|
|
|
vec3 frag_ray_dir = normalize(ray_dir);
|
|
|
|
float azimuth = atan(frag_ray_dir.y, frag_ray_dir.x) / M_PI() * 0.5 + 0.5;
|
2021-07-26 10:10:58 +00:00
|
|
|
// Undo the non-linear transformation from the sky-view LUT
|
2023-04-16 05:17:49 +00:00
|
|
|
float l = asin(frag_ray_dir.z);
|
2023-04-07 06:17:37 +00:00
|
|
|
float elev = sqrt(abs(l) / (M_PI() * 0.5)) * sign(l) * 0.5 + 0.5;
|
2021-04-10 09:14:16 +00:00
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
vec4 sky_radiance = texture(sky_view_tex, vec2(azimuth, elev));
|
2023-02-19 15:47:55 +00:00
|
|
|
// When computing the sky texture we assumed an unitary light source.
|
|
|
|
// Now multiply by the sun irradiance.
|
2023-04-07 06:17:37 +00:00
|
|
|
sky_radiance *= get_sun_spectral_irradiance();
|
2021-07-30 19:34:57 +00:00
|
|
|
|
2023-04-12 20:50:03 +00:00
|
|
|
if (is_envmap == false) {
|
2021-07-30 19:34:57 +00:00
|
|
|
// Render the Sun disk
|
2023-04-12 20:50:03 +00:00
|
|
|
vec3 vs_ray_dir = normalize(ray_dir_view);
|
|
|
|
float cos_theta = dot(vs_ray_dir, fg_SunDirection);
|
2021-07-30 19:34:57 +00:00
|
|
|
|
2023-04-12 20:50:03 +00:00
|
|
|
if (cos_theta >= SUN_COS_HALF_ANGULAR_DIAMETER) {
|
2023-02-19 15:47:55 +00:00
|
|
|
float normalized_altitude =
|
|
|
|
(fg_CameraDistanceToEarthCenter - fg_EarthRadius)
|
2021-08-19 11:09:44 +00:00
|
|
|
/ (ATMOSPHERE_RADIUS - fg_EarthRadius);
|
2021-07-30 19:34:57 +00:00
|
|
|
|
2023-04-12 20:50:03 +00:00
|
|
|
float sun_zenith_cos_theta = dot(-vs_ray_dir, fg_CameraViewUp);
|
2021-07-30 19:34:57 +00:00
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
vec2 uv = vec2(sun_zenith_cos_theta * 0.5 + 0.5,
|
|
|
|
clamp(normalized_altitude, 0.0, 1.0));
|
2023-04-07 06:17:37 +00:00
|
|
|
vec4 transmittance = texture(transmittance_tex, uv);
|
2021-07-30 19:34:57 +00:00
|
|
|
|
2023-04-12 20:50:03 +00:00
|
|
|
vec4 darkening_factor = get_sun_darkening_factor(cos_theta);
|
|
|
|
|
|
|
|
// To get the actual sun radiance we should divide the irradiance
|
|
|
|
// by the solid angle subtended by the Sun, but the resulting
|
|
|
|
// radiance is too big to store in an rgb16f buffer. Also the bloom
|
|
|
|
// gets blown out too much.
|
|
|
|
// Instead, just multiply by a fixed value that looks good enough.
|
|
|
|
vec4 sun_radiance = get_sun_spectral_irradiance() * 500.0;
|
|
|
|
|
|
|
|
sun_radiance *= transmittance * darkening_factor;
|
2021-07-30 19:34:57 +00:00
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
sky_radiance += sun_radiance;
|
2021-07-30 19:34:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-28 07:40:04 +00:00
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
vec3 sky_color = linear_srgb_from_spectral_samples(sky_radiance);
|
2023-04-12 20:50:03 +00:00
|
|
|
|
|
|
|
if (is_envmap == false) {
|
|
|
|
// Only pre-expose when not rendering to the environment map.
|
|
|
|
// We want the non-exposed radiance values for IBL.
|
|
|
|
sky_color = apply_exposure(sky_color);
|
|
|
|
}
|
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
fragColor = vec4(sky_color, 1.0);
|
2021-04-10 09:14:16 +00:00
|
|
|
}
|