Do matting (or color filter) to texture

I try to write an old game with the old game data.
The game has a lot of image sprite, but it use black [0.0,0.0,0.0,0.0] as transparent color. So when loading texture and sprite, I need to find a way to filter the color black to transparent color.
I search for a long time but there is no similar solution in example or other demo project.

There’s probably a straightforward way of achieving this using a simple shader.

But why not simply converting black to transparent in the sources themselves?

Thank your answer. I just solved it by myself yesterday.
Your question that why not converting the source. The answer is more like to respect the original game. I try to remake the game while not modifying the original material.
Also the images are all stored in bmp files, I will have to find a way to convert them to png with transparent color setting.

About my solution to this problem, here is the code:
I use image crate to read raw data of the texture image, and fill the texture by raw data. Maybe there is a better way to iterate pixel and modify them, I am not quite sure right now.

18     let loader = world.read_resource::<Loader>();
19     let texture_storage = world.read_resource::<AssetStorage<Texture>>();
21     let mut img = ImageReader::open(image_path).unwrap().decode().unwrap().into_rgba();
22     let (image_w, image_h) = img.dimensions();
23     for (_x, _y, pixel) in img.enumerate_pixels_mut() {
24         if pixel.to_rgb() == *image::Rgb::from_slice(&[0u8,0,0]) {
25             *pixel = *image::Rgba::from_slice(&[0,0,0,0]);
26         }
27     }
28     let texture_builder = TextureBuilder::new()
29         .with_kind(Kind::D2(image_w, image_h, 1, 1))
30         .with_view_kind(ViewKind::D2)
31         .with_data_width(image_w)
32         .with_data_height(image_h)
33         .with_sampler_info(SamplerInfo::new(Filter::Linear, WrapMode::Clamp))
34         .with_raw_data(img.as_raw().clone(), Format::Rgba8Unorm);
35     let texture_data = texture_builder.into();
37     loader.load_from_data(
38         texture_data,
39         (),
40         &texture_storage)
1 Like