2021-08-18 23:22:26 +00:00
|
|
|
#version 330 core
|
|
|
|
|
2023-04-07 06:17:37 +00:00
|
|
|
uniform sampler2D aerial_perspective_tex;
|
2021-08-18 23:22:26 +00:00
|
|
|
|
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
|
|
|
|
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));
|
|
|
|
}
|