Created
July 22, 2024 10:32
-
-
Save vojd/95c68cf9ecff4cd8362586f5dbc55dac to your computer and use it in GitHub Desktop.
Bevy read pixel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Shows the basics of reading a pixel color value from an `Image` in Bevy | |
/// Pass in a handle to indicate which `Image` you're interested in | |
/// In this example I'm passing in a `LevelHandle` to snag an image used for a level | |
pub(crate) fn read_pixel( | |
query: Query<&Handle<Image>, With<LevelHandle>>, | |
images: Res<Assets<Image>>, | |
) { | |
let handle = query.single(); | |
// the coordinates should be passed in through a Query | |
let x: usize = 100; | |
let y: usize = 100; | |
if let Some(img) = images.get(handle) { | |
let width = img.texture_descriptor.size.width as usize; | |
let index = 4 * (x + width * y); | |
let rgba = &img.data[index..index + 4]; | |
let r = rgba[0]; | |
let g = rgba[1]; | |
let b = rgba[2]; | |
let a = rgba[3]; | |
println!("RGBA: ({}, {}, {}, {})", r, g, b, a); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment