bevyengine / bevyengine/bevy

Sprite picking doesn't work with image render targets

Open
#18,598 0 comments 4 reactions 0 assignees View on GitHub
A-Picking C-Bug S-Ready-For-Implementation
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## Bevy version

`0.16.0-rc.2`

## What you did

```rust
commands.spawn(Camera2d);

let size = Extent3d {
width: 500,
height: 500,
..default()
};

let mut image = Image {
texture_descriptor: TextureDescriptor {
label: None,
size,
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::bevy_default(),
usage: TextureUsages::COPY_DST
| TextureUsages::TEXTURE_BINDING
| TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
},
sampler: ImageSampler::nearest(),
..default()
};

image.resize(size);

let image_handle = assets.add(image);

commands
.spawn((
Sprite::from_image(image_handle.clone()),
Pickable::default(),
))
.observe(|_: Trigger>| {
info!("hovering!"); // <- this is never triggered
});

commands.spawn((
Camera2d,
Camera {
clear_color: ClearColorConfig::Custom(PINK.into()),
target: RenderTarget::Image(image_handle.into()),
..default()
},
));
```

Repository here: https://github.com/musjj/bevy_sprite_picking_bug

## What went wrong

Pointer events are not being triggered on images being used as a camera's render target.

## Additional information

Using `Sprite::from_color(Color::BLACK, Vec2::new(500.0, 500.0))` works, but using an `Image` constructed like the above doesn't.

If I add this config:

```rust
SpritePickingSettings {
picking_mode: SpritePickingMode::BoundingBox,
..default()
}
```

It'll work. So it seems that the sprite is being treated as completely transparent, even though a camera view is being rendered into it.

Attempts to query the pixel color always returns a transparent color:

```rust
[src/main.rs:21:17] image.get_color_at(250, 250) = Ok(
Srgba(
Srgba {
red: 0.0,
green: 0.0,
blue: 0.0,
alpha: 0.0,
},
),
)
```

Which breaks the logic here:

https://github.com/bevyengine/bevy/blob/3945a6de3ba9645851ae03ad998a77bf5899d0df/crates/bevy_sprite/src/picking_backend.rs#L209-L217

This is strange because the image is displaying the pink clear color of the camera.

Contributor guide

Open the contributing guide

Research direction

Start with crates/bevy_sprite/src/picking_backend.rs around lines 209-217, then run the minimal reproduction in the linked bevy_sprite_picking_bug repository. Compare picking behavior for an image render target with the bounding-box mode; done means pointer events trigger for the default sprite-picking path when the rendered image is visibly non-transparent.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.