Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Texture colors inverted on WASM target #2

Open
Zentendo opened this issue Oct 30, 2021 · 2 comments
Open

Bug: Texture colors inverted on WASM target #2

Zentendo opened this issue Oct 30, 2021 · 2 comments

Comments

@Zentendo
Copy link

@caelunshun Thanks for adding WASM support btw!

Texture colors appear to be inverted on the WASM target, using the latest experimental branch commit 26be4a0:

Native Vulkan target:
image

Inverted colors with same result on Chrome, Firefox, Edge:
image

Original texture:
crab

Relevant code:

let mut texBuilder = cx.create_texture_set_builder();
texBuilder.add_texture(include_bytes!("../assets/crab.png"), "sprite1").unwrap();
let texSet = texBuilder.build(1024, 1024).unwrap();
cx.add_texture_set(texSet);

let tex = Context::texture_for_name(canvas.context(), "sprite1").unwrap();
canvas.draw_sprite(tex, vec2(20., 20.), 400.);
@Zentendo Zentendo changed the title Bug: Texture Colors Inverted on WASM target Bug: Texture colors inverted on WASM target Oct 30, 2021
@caelunshun
Copy link
Owner

caelunshun commented Oct 30, 2021

Thanks for the bug report!

Looks like wgpu doesn't implement BGRA textures on WebGL, so the BGRA texture atlas gets interpreted as RGBA—thus swapping the red and blue channels.

As a workaround, we can use an RGBA format when on the Web.

@Zentendo
Copy link
Author

Zentendo commented Nov 1, 2021

@caelunshun Works, thanks!

#[cfg(target_arch = "wasm32")]
pub fn add_web_texture(
  texBuilder: &mut TextureSetBuilder,
  data: &[u8],
  name: impl Into<String>,
) -> Result<(), image::ImageError> {
  let image = image::load_from_memory(data)?.to_rgba8();
  texBuilder.add_raw_texture(image.width(), image.height(), image.into_raw(), name);
  Ok(())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants