Skip to content

Commit

Permalink
tweak checks
Browse files Browse the repository at this point in the history
  • Loading branch information
vandie committed Nov 13, 2024
1 parent e90dd50 commit ffdd559
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions backends/bevy_picking_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,34 +151,39 @@ pub fn sprite_picking(
let is_cursor_in_sprite = rect.contains(cursor_pos_sprite.truncate());

let cursor_in_valid_pixels_of_sprite = is_cursor_in_sprite
&& settings.passthrough_transparency
&& (image.is_some() || {
let texture: &Image = image.and_then(|i| images.get(i))?;
// If using a texture atlas, grab the offset of the current sprite index. (0,0) otherwise
let texture_rect = atlas
.and_then(|atlas| {
texture_atlas_layout
.get(&atlas.layout)
.map(|f| f.textures[atlas.index])
})
.or(Some(URect::new(0, 0, texture.width(), texture.height())))?;
// get mouse position on texture
let texture_position =
texture_rect.center() + cursor_pos_sprite.truncate().as_uvec2();
// grab pixel
let pixel_index = (texture_position.y * texture.width()
+ texture_position.x)
as usize;
// check transparancy
if let Some(pixel_data) =
texture.data.get(pixel_index * 4..(pixel_index * 4 + 4))
{
let transparency = pixel_data[3];
transparency > settings.transparency_cutoff
} else {
false
}
});
&& (!settings.passthrough_transparency
|| image.is_some() && {
let texture: &Image = image.and_then(|i| images.get(i))?;
// If using a texture atlas, grab the offset of the current sprite index. (0,0) otherwise
let texture_rect = atlas
.and_then(|atlas| {
texture_atlas_layout
.get(&atlas.layout)
.map(|f| f.textures[atlas.index])
})
.or(Some(URect::new(
0,
0,
texture.width(),
texture.height(),
)))?;
// get mouse position on texture
let texture_position =
texture_rect.center() + cursor_pos_sprite.truncate().as_uvec2();
// grab pixel
let pixel_index = (texture_position.y * texture.width()
+ texture_position.x)
as usize;
// check transparancy
if let Some(pixel_data) =
texture.data.get(pixel_index * 4..(pixel_index * 4 + 4))
{
let transparency = pixel_data[3];
transparency > settings.transparency_cutoff
} else {
false
}
});

blocked = cursor_in_valid_pixels_of_sprite
&& pickable.map(|p| p.should_block_lower) != Some(false);
Expand Down

0 comments on commit ffdd559

Please sign in to comment.