use mln_gfx::shaders::source::{GlslEsDeclarations, GlslEsSource, GlslSource};
pub(crate) const CLIPPING_MASK: GlslSource = GlslSource {
vertex: r"
void main() {
gl_Position = projectTile(vec2(a_pos), vec2(a_pos));
}
",
fragment: r"
void main() {
fragColor = vec4(1.0);
}
",
};
pub(crate) const CLIPPING_MASK_ES: GlslEsSource = CLIPPING_MASK.es(&GlslEsDeclarations::DEFAULT);
pub(crate) const GLOBE_DEPTH: GlslSource = GlslSource {
vertex: r"
void main() {
gl_Position = projectTileFor3D(vec2(a_pos), 0.0);
}
",
fragment: r"
void main() {
fragColor = vec4(0.0);
}
",
};
pub(crate) const GLOBE_DEPTH_ES: GlslEsSource = GLOBE_DEPTH.es(&GlslEsDeclarations::DEFAULT);
pub(crate) const LAYER_COMPOSITE: GlslSource = GlslSource {
vertex: r"
void main() {
gl_Position = vec4(vec2(a_pos) * 2.0 - 1.0, 0.0, 1.0);
}
",
fragment: r"
// The blend modes that no blend function expresses, of the W3C compositing
// spec, on premultiplied colors.
float lum(vec3 c) {
return dot(c, vec3(0.3, 0.59, 0.11));
}
// Pulls a color back into gamut around its luminosity, which it keeps.
vec3 clip_color(vec3 c) {
float l = lum(c);
float n = min(min(c.r, c.g), c.b);
float x = max(max(c.r, c.g), c.b);
if (n < 0.0) c = l + (c - l) * l / max(l - n, 1e-6);
if (x > 1.0) c = l + (c - l) * (1.0 - l) / max(x - l, 1e-6);
return c;
}
vec3 set_lum(vec3 c, float l) {
return clip_color(c + (l - lum(c)));
}
float sat(vec3 c) {
return max(max(c.r, c.g), c.b) - min(min(c.r, c.g), c.b);
}
// Scales a color to a saturation, keeping its hue: a gray gives black.
vec3 set_sat(vec3 c, float s) {
float n = min(min(c.r, c.g), c.b);
float x = max(max(c.r, c.g), c.b);
return x > n ? (c - n) * s / (x - n) : vec3(0.0);
}
// The blend modes that the shader computes on straight colors: the
// source's `cs` over the backdrop's `cb`.
vec3 blend(vec3 cs, vec3 cb, int mode) {
if (mode == 3) return set_lum(cs, lum(cb));
if (mode == 4) return set_lum(cb, lum(cs));
if (mode == 10) {
vec3 d = mix(sqrt(cb), ((16.0 * cb - 12.0) * cb + 4.0) * cb, step(cb, vec3(0.25)));
vec3 darkened = cb - (1.0 - 2.0 * cs) * cb * (1.0 - cb);
vec3 lightened = cb + (2.0 * cs - 1.0) * (d - cb);
return mix(darkened, lightened, step(0.5, cs));
}
// Color dodge and burn keep their divisors above zero, which gives the
// spec's results where the source or backdrop is 0 or 1.
if (mode == 11) return min(vec3(1.0), cb / max(1.0 - cs, vec3(1e-6)));
if (mode == 12) return 1.0 - min(vec3(1.0), (1.0 - cb) / max(cs, vec3(1e-6)));
if (mode == 13) return set_lum(set_sat(cs, sat(cb)), lum(cb));
return set_lum(set_sat(cb, sat(cs)), lum(cb));
}
vec4 composite(vec4 src, vec4 dst, int mode) {
float inv_dst_a = 1.0 - dst.a;
float inv_src_a = 1.0 - src.a;
vec3 co;
if (mode == 1) {
co = inv_dst_a * src.rgb + inv_src_a * dst.rgb + src.rgb * dst.rgb;
} else if (mode == 2 || mode == 5) {
// Overlay multiplies or screens by the backdrop, and hard light by
// the source.
vec3 screen = mode == 2 ? step(dst.a, 2.0 * dst.rgb) : step(src.a, 2.0 * src.rgb);
vec3 multiplied = inv_dst_a * src.rgb + inv_src_a * dst.rgb + 2.0 * dst.rgb * src.rgb;
vec3 screened = src.rgb + dst.rgb + dst.a * src.rgb + src.a * dst.rgb - vec3(src.a * dst.a) - 2.0 * dst.rgb * src.rgb;
co = mix(multiplied, screened, screen);
} else if (mode == 6) {
co = src.rgb + dst.rgb - 2.0 * min(src.rgb * dst.a, dst.rgb * src.a);
} else if (mode == 7) {
co = src.rgb + dst.rgb - 2.0 * src.rgb * dst.rgb;
} else if (mode == 8) {
co = src.rgb + dst.rgb - max(src.rgb * dst.a, dst.rgb * src.a);
} else if (mode == 9) {
co = src.rgb + dst.rgb - min(src.rgb * dst.a, dst.rgb * src.a);
} else {
// Rounding to 8 bits can leave a premultiplied channel above its alpha.
vec3 cs = src.a > 0.0 ? clamp(src.rgb / src.a, 0.0, 1.0) : vec3(0.0);
vec3 cb = dst.a > 0.0 ? clamp(dst.rgb / dst.a, 0.0, 1.0) : vec3(0.0);
co = inv_dst_a * src.rgb + inv_src_a * dst.rgb + src.a * dst.a * blend(cs, cb, mode);
}
return vec4(co, src.a + dst.a * inv_src_a);
}
void main() {
ivec2 texel = ivec2(gl_FragCoord.xy);
vec4 color = texelFetch(u_image, texel, 0);
vec4 src = color * props.opacity;
if (props.blend == 0) {
fragColor = src;
} else {
fragColor = composite(src, texelFetch(u_backdrop, texel, 0), props.blend);
}
}
",
};
pub(crate) const LAYER_COMPOSITE_ES: GlslEsSource = LAYER_COMPOSITE.es(&GlslEsDeclarations::DEFAULT);
pub(crate) const SKY: GlslSource = GlslSource {
vertex: r"
MLN_LOC(0) out vec2 v_pos;
MLN_LOC(1) out vec3 v_view_direction;
void main() {
v_pos = vec2(a_pos);
vec4 view_position = sky.inv_view_projection * vec4(v_pos, 0.0, 1.0);
v_view_direction = view_position.xyz / view_position.w;
gl_Position = vec4(v_pos, 1.0, 1.0);
}
",
fragment: r"#ifdef GL_ES
precision highp float;
#endif
MLN_LOC(0) in vec2 v_pos;
MLN_LOC(1) in vec3 v_view_direction;
// The atmosphere's height in the planet's radii: 100 km over the Earth's.
const float ATMOSPHERE_HEIGHT = 100000.0 / 6371000.0;
void main() {
vec2 pixel = (v_pos * 0.5 + 0.5) * sky.viewport_size;
float distance_to_horizon = dot(pixel - sky.horizon, sky.horizon_normal);
float opacity = 1.0;
if (sky.sky_blend > 0.0) {
// On the globe the horizon is the planet's limb, and the sky shows
// where the ray passes through the atmosphere, from a camera within
// ten times its height.
vec3 ray = normalize(v_view_direction);
vec3 center = sky.globe_center.xyz;
float center_distance = length(center);
float angle_to_center = acos(clamp(dot(ray, center) / center_distance, -1.0, 1.0));
float horizon_angle = asin(min(1.0 / center_distance, 1.0));
float distance_to_limb = ((angle_to_center - horizon_angle) * sky.pixels_per_radian + 1.0) * sky.pixel_ratio;
distance_to_horizon = mix(distance_to_horizon, distance_to_limb, sky.sky_blend);
float closest_approach = max(dot(ray, center), 0.0);
float lowest_altitude =
sqrt(max(center_distance * center_distance - closest_approach * closest_approach, 0.0)) - 1.0;
float inside = 1.0 - smoothstep(ATMOSPHERE_HEIGHT, 10.0 * ATMOSPHERE_HEIGHT, center_distance - 1.0);
float through = 1.0 - smoothstep(0.0, ATMOSPHERE_HEIGHT, lowest_altitude);
opacity = inside * through * (1.0 - sky.atmosphere_blend);
}
vec4 color = vec4(0.0);
if (distance_to_horizon > 0.0) {
if (sky.sky_horizon_blend > 0.0 && distance_to_horizon < sky.sky_horizon_blend) {
float blend = 1.0 - distance_to_horizon / sky.sky_horizon_blend;
color = mix(sky.sky_color, sky.horizon_color, blend * blend);
} else {
color = sky.sky_color;
}
}
fragColor = color * mix(1.0, opacity, sky.sky_blend);
}
",
};
pub(crate) const SKY_ES: GlslEsSource = SKY.es(&GlslEsDeclarations::DEFAULT);
pub(crate) const ATMOSPHERE: GlslSource = GlslSource {
vertex: r"
MLN_LOC(0) out vec3 v_view_direction;
void main() {
vec2 pos = vec2(a_pos);
vec4 target = atmosphere.inv_view_projection * vec4(pos, 0.0, 1.0);
v_view_direction = target.xyz / target.w - atmosphere.camera_position.xyz;
// OpenGL maps NDC z=0 to framebuffer depth 0.5; Vulkan/D3D12 use a
// zero-to-one NDC depth range, so z=0.5 matches it.
#ifdef MLN_GL
gl_Position = vec4(pos, 0.0, 1.0);
#else
gl_Position = vec4(pos, 0.5, 1.0);
#endif
}
",
fragment: r"#ifdef GL_ES
precision highp float;
#endif
MLN_LOC(0) in vec3 v_view_direction;
const int PRIMARY_STEPS = 5;
const int LIGHT_STEPS = 3;
const float EARTH_RADIUS = 6371000.0;
const float ATMOSPHERE_RADIUS = 6471000.0;
vec2 ray_sphere_intersection(vec3 origin, vec3 direction, float radius) {
float a = dot(direction, direction);
float b = 2.0 * dot(direction, origin);
float c = dot(origin, origin) - radius * radius;
float discriminant = b * b - 4.0 * a * c;
if (discriminant < 0.0) return vec2(1e5, -1e5);
float root = sqrt(discriminant);
return vec2((-b - root) / (2.0 * a), (-b + root) / (2.0 * a));
}
vec4 scatter(vec3 direction, vec3 origin, vec3 sun) {
const vec3 rayleigh_coefficient = vec3(5.5e-6, 13.0e-6, 22.4e-6);
const float mie_coefficient = 21e-6;
const float rayleigh_height = 8e3;
const float mie_height = 1.2e3;
const float mie_direction = 0.758;
direction = normalize(direction);
sun = normalize(sun);
vec2 bounds = ray_sphere_intersection(origin, direction, ATMOSPHERE_RADIUS);
if (bounds.x > bounds.y) return vec4(0.0, 0.0, 0.0, 1.0);
bounds.x = max(bounds.x, 0.0);
vec2 ground = ray_sphere_intersection(origin, direction, EARTH_RADIUS);
if (ground.x <= ground.y && ground.x > 0.0) bounds.y = min(bounds.y, ground.x);
float primary_step = (bounds.y - bounds.x) / float(PRIMARY_STEPS);
float primary_time = bounds.x + primary_step * 0.5;
float primary_rayleigh_depth = 0.0;
float primary_mie_depth = 0.0;
vec3 total_rayleigh = vec3(0.0);
vec3 total_mie = vec3(0.0);
float mu = dot(direction, sun);
float mu_squared = mu * mu;
float g_squared = mie_direction * mie_direction;
float rayleigh_phase = 3.0 / (16.0 * PI) * (1.0 + mu_squared);
float mie_phase = 3.0 / (8.0 * PI) * ((1.0 - g_squared) * (mu_squared + 1.0)) /
(pow(1.0 + g_squared - 2.0 * mu * mie_direction, 1.5) * (2.0 + g_squared));
for (int i = 0; i < PRIMARY_STEPS; ++i) {
vec3 sample_position = origin + direction * primary_time;
float sample_height = length(sample_position) - EARTH_RADIUS;
float rayleigh_step = exp(-sample_height / rayleigh_height) * primary_step;
float mie_step = exp(-sample_height / mie_height) * primary_step;
primary_rayleigh_depth += rayleigh_step;
primary_mie_depth += mie_step;
float light_step = ray_sphere_intersection(sample_position, sun, ATMOSPHERE_RADIUS).y /
float(LIGHT_STEPS);
float light_time = light_step * 0.5;
float light_rayleigh_depth = 0.0;
float light_mie_depth = 0.0;
for (int j = 0; j < LIGHT_STEPS; ++j) {
vec3 light_position = sample_position + sun * light_time;
float light_height = length(light_position) - EARTH_RADIUS;
light_rayleigh_depth += exp(-light_height / rayleigh_height) * light_step;
light_mie_depth += exp(-light_height / mie_height) * light_step;
light_time += light_step;
}
vec3 attenuation = exp(-(mie_coefficient * (primary_mie_depth + light_mie_depth) +
rayleigh_coefficient * (primary_rayleigh_depth + light_rayleigh_depth)));
total_rayleigh += rayleigh_step * attenuation;
total_mie += mie_step * attenuation;
primary_time += primary_step;
}
float opacity = exp(-(length(rayleigh_coefficient) * length(total_rayleigh) +
mie_coefficient * length(total_mie)));
vec3 color = 22.0 * (rayleigh_phase * rayleigh_coefficient * total_rayleigh +
mie_phase * mie_coefficient * total_mie);
return vec4(color, opacity);
}
void main() {
vec3 camera = atmosphere.camera_position.xyz * EARTH_RADIUS;
vec4 color = scatter(v_view_direction, camera, atmosphere.sun_position.xyz);
color.rgb = 1.0 - exp(-color.rgb);
color = pow(color, vec4(1.0 / 2.2));
fragColor = vec4(color.rgb, 1.0 - color.a) * atmosphere.sun_position.w;
}
",
};
pub(crate) const ATMOSPHERE_ES: GlslEsSource = ATMOSPHERE.es(&GlslEsDeclarations::DEFAULT);