2021-08-18 23:22:26 +00:00
|
|
|
#version 330 core
|
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
uniform sampler2D aerial_perspective_tex;
|
|
|
|
uniform sampler2D transmittance_tex;
|
2021-08-18 23:22:26 +00:00
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
uniform vec3 fg_SunDirectionWorld;
|
2021-08-18 23:22:26 +00:00
|
|
|
uniform float fg_CameraDistanceToEarthCenter;
|
2023-02-19 15:47:55 +00:00
|
|
|
uniform float fg_SunZenithCosTheta;
|
2021-08-18 23:22:26 +00:00
|
|
|
uniform float fg_EarthRadius;
|
|
|
|
|
2023-04-16 05:17:49 +00:00
|
|
|
const float AP_SLICE_COUNT = 32.0;
|
2023-02-19 15:47:55 +00:00
|
|
|
const float AP_MAX_DEPTH = 128000.0;
|
2023-04-16 05:17:49 +00:00
|
|
|
const float AP_SLICE_WIDTH_PIXELS = 32.0;
|
2023-02-19 15:47:55 +00:00
|
|
|
const float AP_SLICE_SIZE = 1.0 / AP_SLICE_COUNT;
|
|
|
|
const float AP_TEXEL_WIDTH = 1.0 / (AP_SLICE_COUNT * AP_SLICE_WIDTH_PIXELS);
|
2021-08-18 23:22:26 +00:00
|
|
|
|
|
|
|
const float ATMOSPHERE_RADIUS = 6471e3;
|
|
|
|
|
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-02-19 15:47:55 +00:00
|
|
|
|
|
|
|
vec4 sample_aerial_perspective_slice(sampler2D lut, vec2 coord, float slice)
|
2021-08-18 23:22:26 +00:00
|
|
|
{
|
|
|
|
// Sample at the pixel center
|
2023-02-19 15:47:55 +00:00
|
|
|
float offset = slice * AP_SLICE_SIZE + AP_TEXEL_WIDTH * 0.5;
|
|
|
|
float x = coord.x * (AP_SLICE_SIZE - AP_TEXEL_WIDTH) + offset;
|
|
|
|
return texture(lut, vec2(x, coord.y));
|
2021-08-18 23:22:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
vec4 sample_aerial_perspective(sampler2D lut, vec2 coord, float depth)
|
2021-08-18 23:22:26 +00:00
|
|
|
{
|
|
|
|
vec4 color;
|
2023-02-19 15:47:55 +00:00
|
|
|
float w = sqrt(clamp(depth / AP_MAX_DEPTH, 0.0, 1.0));
|
|
|
|
float x = w * AP_SLICE_COUNT;
|
|
|
|
if (x <= 1.0) {
|
2021-08-18 23:22:26 +00:00
|
|
|
// Handle special case of fragments behind the first slice
|
|
|
|
color = mix(vec4(0.0, 0.0, 0.0, 1.0),
|
2023-02-19 15:47:55 +00:00
|
|
|
sample_aerial_perspective_slice(lut, coord, 0),
|
|
|
|
x);
|
2021-08-18 23:22:26 +00:00
|
|
|
} else {
|
|
|
|
// Manually interpolate between slices
|
2023-02-19 15:47:55 +00:00
|
|
|
x -= 1.0;
|
|
|
|
color = mix(sample_aerial_perspective_slice(lut, coord, floor(x)),
|
|
|
|
sample_aerial_perspective_slice(lut, coord, ceil(x)),
|
|
|
|
fract(x));
|
2021-08-18 23:22:26 +00:00
|
|
|
}
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
vec4 get_aerial_perspective(vec2 coord, float depth)
|
|
|
|
{
|
|
|
|
return sample_aerial_perspective(aerial_perspective_tex, coord, depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 mix_aerial_perspective(vec3 color, vec4 ap)
|
2021-08-18 23:22:26 +00:00
|
|
|
{
|
2023-02-19 15:47:55 +00:00
|
|
|
return color * ap.a + ap.rgb;
|
2021-08-18 23:22:26 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
vec3 add_aerial_perspective(vec3 color, vec2 coord, float depth)
|
|
|
|
{
|
|
|
|
return mix_aerial_perspective(color, get_aerial_perspective(coord, depth));
|
|
|
|
}
|
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
/*
|
|
|
|
* Get the Sun radiance at a point 'p' in world space.
|
|
|
|
* We cannot use the Sun extraterrestial irradiance directly because it will be
|
|
|
|
* attenuated by the transmittance of the atmospheric medium.
|
2021-08-18 23:22:26 +00:00
|
|
|
*/
|
2023-02-19 15:47:55 +00:00
|
|
|
vec3 get_sun_radiance(vec3 p)
|
2021-08-18 23:22:26 +00:00
|
|
|
{
|
2023-02-19 15:47:55 +00:00
|
|
|
float distance_to_earth_center = length(p);
|
|
|
|
float normalized_altitude = (distance_to_earth_center - fg_EarthRadius)
|
2021-08-18 23:22:26 +00:00
|
|
|
/ (ATMOSPHERE_RADIUS - fg_EarthRadius);
|
|
|
|
|
2023-02-19 15:47:55 +00:00
|
|
|
vec3 zenith_dir = p / distance_to_earth_center;
|
|
|
|
float sun_cos_theta = dot(zenith_dir, fg_SunDirectionWorld);
|
|
|
|
|
|
|
|
float u = sun_cos_theta * 0.5 + 0.5;
|
|
|
|
float v = clamp(normalized_altitude, 0.0, 1.0);
|
2023-04-07 06:17:37 +00:00
|
|
|
vec4 transmittance = texture(transmittance_tex, vec2(u, v));
|
2021-08-18 23:22:26 +00:00
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
vec4 L = get_sun_spectral_irradiance() * transmittance;
|
|
|
|
return linear_srgb_from_spectral_samples(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 get_sun_radiance_sea_level()
|
|
|
|
{
|
|
|
|
vec2 uv = vec2(fg_SunZenithCosTheta * 0.5 + 0.5, 0.0);
|
|
|
|
vec4 transmittance = texture(transmittance_tex, uv);
|
|
|
|
vec4 L = get_sun_spectral_irradiance() * transmittance;
|
2023-02-19 15:47:55 +00:00
|
|
|
return linear_srgb_from_spectral_samples(L);
|
2021-08-18 23:22:26 +00:00
|
|
|
}
|