-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add optional transparency passthrough for sprite backend with bevy_picking #16388
base: main
Are you sure you want to change the base?
Add optional transparency passthrough for sprite backend with bevy_picking #16388
Conversation
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m excited for this change. I did a quick visual code review but haven’t tested it yet.
/// Off by default for backwards compatibility. This setting is provided to give you fine-grained | ||
/// control over if transparency on sprites is ignored. | ||
pub passthrough_transparency: bool, | ||
/// How Opaque does part of a sprite need to be in order count as none-transparent (defaults to 10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate in the docs what the units are? 10 what? What is a valid range I could use, as a user?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these two options be combined into a single enum? It doesn't make sense to have an alpha cutoff setting when alpha passthrough is disabled. e.g.
SpritePickingAlphaTest {
/// Don't test alpha, only consider the rect of sprites.
Ignore,
/// Test the pixel alpha when running hit tests. Alpha values below this will not be considered for picking.
Threshold(u8)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah an enum is probably a better idea for those settings. Will rework to include.
Seems everyone agrees that this should be enabled by default so I've updated that and introduced a new |
It looks like your PR is a breaking change, but you didn't provide a migration guide. Could you add some context on what users should update when this change get released in a new version of Bevy? |
Can you add a small migration guide explaining that this is now on by default? Definitely the better default, but could break existing users. |
Added. Hope that's ok and clear? |
The migration guide was clear and effective, but the tone / target audience was a bit weird. The goal is to communicate "why is my stuff broken and how do I fix it", but your initial draft was closer to a change log :) I've quickly edited the PR description incorporating the information you've added. These get automatically scraped / compiled, so it's good to clean them up here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the previous two comments not being part of this review, I've been noticing while trying to adapt this picking backend into my project.
Testing the changes got this error after a crash:
I'm using 0.15 release branch with the changes applied on top. Running the sprite_picking example |
I verified this on Windows and on main as well. Looks like there's something wrong with |
edit: Oh wait, irritating, this looks like a different panic... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested the sprite_picking example, looks like the black squares are offset by half its width and height. Color sprites are actually a 1x1 image, so it looks like we're offset by half a pixel somewhere. Edit: nevermind I completely misunderstood what was happening, looks like the color sprites are off by half width/height which in this case is 64x64 Edit 2: DOUBLE NEVERMIND I WAS CONFUSED, we pass a custom size to the color sprite, which makes it 64x64, but the image size is 1x1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not considering the custom_size of sprites, or the rect
of sprites inside the pixel checking code.
I also think we should consider moving all the pixel picking logic into the Sprite
struct as a method. It seems reasonable to want to reuse "given this relative position, where am I in the texture for this sprite?" (but like with the float part remaining).
Edit: It looks like we're also not considering Sprite::flip_x
, Sprite::flip_y
, and Sprite::image_mode
. I'm tinkering to see if I can get an implementation that works.
}) | ||
.or(Some(URect::new(0, 0, texture.width(), texture.height())))?; | ||
// get mouse position on texture | ||
let texture_position = (texture_rect.center().as_vec2() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're getting the center and then casting to a float. Odd image sizes will mean the center won't be at 0.5, 0.5 like we'd expect. I'm pretty sure we need to cast the rect to f32 and then get the center, so texture_rect.as_rect().center()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, let me check my commits because I think I had some correction for this. Wonder if I accidentally removed it with all the reworking we've done.
Objective
Solution
Testing
Showcase
Migration Guide
Sprite picking now ignores transparent regions (with an alpha value less than or equal to 0.1). To configure this, modify the
SpriteBackendSettings
resource.