Skip to content

Commit

Permalink
Fix color blending for user textures
Browse files Browse the repository at this point in the history
  • Loading branch information
mvlabat committed Apr 28, 2022
1 parent c0de8c6 commit fc83e34
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/egui.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ var s_egui: sampler;

[[stage(fragment)]]
fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
let color = in.color * textureSample(t_egui, s_egui, in.uv);
let texture_color = textureSample(t_egui, s_egui, in.uv);
// This assumes that colors are premultiplied but texture images are not.
let color = in.color * vec4<f32>(texture_color.rgb * texture_color.a, texture_color.a);

return color;
}
11 changes: 7 additions & 4 deletions src/egui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,26 +385,29 @@ impl Node for EguiNode {
}
}

pub fn as_color_image(image: egui::ImageData) -> egui::ColorImage {
pub(crate) fn as_color_image(image: egui::ImageData) -> egui::ColorImage {
match image {
egui::ImageData::Color(image) => image,
egui::ImageData::Alpha(image) => alpha_image_as_color_image(&image),
}
}

pub fn alpha_image_as_color_image(image: &egui::AlphaImage) -> egui::ColorImage {
fn alpha_image_as_color_image(image: &egui::AlphaImage) -> egui::ColorImage {
let gamma = 1.0;
egui::ColorImage {
size: image.size,
pixels: image.srgba_pixels(gamma).collect(),
}
}

pub fn color_image_as_bevy_image(egui_image: &egui::ColorImage) -> Image {
pub(crate) fn color_image_as_bevy_image(egui_image: &egui::ColorImage) -> Image {
let pixels = egui_image
.pixels
.iter()
.flat_map(|color| color.to_array())
// We unmultiply Egui textures to premultiply them later in the fragment shader.
// As user textures loaded as Bevy assets are not premultiplied (and there seems to be no
// convenient way to convert them to premultiplied ones), we do the this with Egui ones.
.flat_map(|color| color.to_srgba_unmultiplied())
.collect();

Image::new(
Expand Down

0 comments on commit fc83e34

Please sign in to comment.