2021-04-10 09:14:16 +00:00
|
|
|
#version 330 core
|
|
|
|
|
|
|
|
out vec4 fragColor;
|
|
|
|
|
2021-07-28 07:40:04 +00:00
|
|
|
in vec3 vRayDir;
|
|
|
|
in vec3 vRayDirView;
|
2021-04-10 09:14:16 +00:00
|
|
|
|
2021-07-30 19:34:57 +00:00
|
|
|
uniform bool sun_disk;
|
2021-07-26 10:10:58 +00:00
|
|
|
uniform sampler2D sky_view_lut;
|
2021-07-28 07:40:04 +00:00
|
|
|
uniform sampler2D transmittance_lut;
|
|
|
|
uniform vec3 fg_SunDirection;
|
2021-04-10 09:14:16 +00:00
|
|
|
|
2021-07-30 19:34:57 +00:00
|
|
|
uniform mat4 osg_ModelViewMatrix;
|
|
|
|
|
2021-07-26 10:10:58 +00:00
|
|
|
const float PI = 3.141592653;
|
2021-07-30 19:34:57 +00:00
|
|
|
const vec3 EXTRATERRESTRIAL_SOLAR_ILLUMINANCE = vec3(128.0);
|
2021-07-28 07:40:04 +00:00
|
|
|
|
2021-07-30 19:34:57 +00:00
|
|
|
const float sun_solid_angle = 0.545*PI/180.0; // ~half a degree
|
2021-07-28 07:40:04 +00:00
|
|
|
const float sun_cos_solid_angle = cos(sun_solid_angle);
|
2021-04-10 09:14:16 +00:00
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2021-07-28 07:40:04 +00:00
|
|
|
vec3 rayDir = normalize(vRayDir);
|
2021-07-26 10:10:58 +00:00
|
|
|
float azimuth = atan(rayDir.y, rayDir.x) / PI * 0.5 + 0.5;
|
|
|
|
// Undo the non-linear transformation from the sky-view LUT
|
|
|
|
float l = asin(rayDir.z);
|
|
|
|
float elev = sqrt(abs(l) / (PI * 0.5)) * sign(l) * 0.5 + 0.5;
|
2021-04-10 09:14:16 +00:00
|
|
|
|
2021-07-26 10:10:58 +00:00
|
|
|
vec3 color = texture(sky_view_lut, vec2(azimuth, elev)).rgb;
|
2021-07-30 19:34:57 +00:00
|
|
|
color *= EXTRATERRESTRIAL_SOLAR_ILLUMINANCE;
|
|
|
|
|
|
|
|
if (sun_disk) {
|
|
|
|
// Render the Sun disk
|
|
|
|
vec3 rayDirView = normalize(vRayDirView);
|
|
|
|
float cosTheta = dot(rayDirView, fg_SunDirection);
|
|
|
|
|
|
|
|
if (cosTheta >= sun_cos_solid_angle) {
|
|
|
|
vec4 groundPoint = osg_ModelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0);
|
|
|
|
float altitude = length(groundPoint);
|
|
|
|
float scaledAltitude = altitude / 100000.0;
|
|
|
|
|
|
|
|
vec3 up = normalize(vec4(0.0, 0.0, 0.0, 1.0) - groundPoint).xyz;
|
|
|
|
float sunZenithCosTheta = dot(rayDirView, up);
|
|
|
|
|
|
|
|
vec2 coords = vec2(sunZenithCosTheta * 0.5 + 0.5,
|
|
|
|
clamp(scaledAltitude, 0.0, 1.0));
|
|
|
|
vec3 transmittance = texture(transmittance_lut, coords).rgb;
|
|
|
|
|
|
|
|
// Limb darkening
|
|
|
|
// http://www.physics.hmc.edu/faculty/esin/a101/limbdarkening.pdf
|
|
|
|
vec3 u = vec3(1.0);
|
|
|
|
vec3 a = vec3(0.397, 0.503, 0.652);
|
|
|
|
float centerToEdge = 1.0 - (cosTheta - sun_cos_solid_angle)
|
|
|
|
/ (1.0 - sun_cos_solid_angle);
|
|
|
|
float mu = sqrt(max(1.0 - centerToEdge * centerToEdge, 0.0));
|
|
|
|
vec3 factor = vec3(1.0) - u * (vec3(1.0) - pow(vec3(mu), a));
|
|
|
|
|
|
|
|
color += EXTRATERRESTRIAL_SOLAR_ILLUMINANCE * transmittance * factor;
|
|
|
|
}
|
|
|
|
}
|
2021-07-28 07:40:04 +00:00
|
|
|
|
2021-04-10 09:14:16 +00:00
|
|
|
fragColor = vec4(color, 1.0);
|
|
|
|
}
|