Skip to content

Commit f75af10

Browse files
committed
Fix the compositing algo.
1 parent 7b7966d commit f75af10

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

shaders/src/shaders/loading_repeating_circles.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -825,16 +825,27 @@ fn draw_time_bar_discrete(
825825
/// Alpha compositing using "over" operator.
826826
/// https://en.wikipedia.org/wiki/Alpha_compositing
827827
fn composite_layers<const N: usize>(overlay_colors: &[Vec4; N]) -> Vec4 {
828-
let mut result = Vec4::ZERO;
829-
for i in (0..overlay_colors.len()).rev() {
830-
let color = overlay_colors[i];
831-
let alpha = color.w;
832-
if alpha > 0.0 {
833-
result = result + (color * alpha * (1.0 - result.w));
834-
result.w += alpha * (1.0 - result.w);
828+
// Start with a fully opaque black background.
829+
let mut color_bg = Vec4::new(0.0, 0.0, 0.0, 1.0);
830+
for i in 0..overlay_colors.len() {
831+
let color_fg = overlay_colors[i];
832+
let color_fg_rgb = color_fg.xyz();
833+
let alpha_fg = color_fg.w;
834+
if alpha_fg <= 0.0 {
835+
continue; // Skip fully transparent layers.
835836
}
837+
838+
let color_bg_rgb = color_bg.xyz();
839+
let alpha_bg = color_bg.w;
840+
841+
let alpha_final = alpha_bg + alpha_fg - alpha_bg * alpha_fg;
842+
843+
// Composite using the "over" operator.
844+
color_bg = ((color_fg_rgb * alpha_fg + color_bg_rgb * alpha_bg * (1.0 - alpha_fg))
845+
/ alpha_final)
846+
.extend(alpha_final);
836847
}
837-
return result;
848+
return color_bg;
838849
}
839850

840851
const SHOW_TIME_BAR: bool = true;
@@ -1003,7 +1014,7 @@ impl Inputs {
10031014
// * (m.y + 6.0 / 256.0));
10041015

10051016
let m = sdf_circle_filled(uv, middle_circle_position, middle_circle_radius)
1006-
.to_outline(outer_circle_outer_radius * 2.0);
1017+
.to_outline(outer_circle_outer_radius);
10071018
black_alpha = black_alpha.max(m.to_alpha() * t_assist_circle);
10081019
}
10091020

0 commit comments

Comments
 (0)