Skip to content
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

Proportional scaling for the sprite's texture. #17258

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

silvestrpredko
Copy link
Contributor

Objective

Bevy sprite image mode lacks proportional scaling for the underlying texture. In many cases, it's required. For example, if it is desired to support a wide variety of screens with a single texture, it's okay to cut off some portion of the original texture.

Solution

I added scaling of the texture during the preparation step. To fill the sprite with the original texture, I scaled UV coordinates accordingly to the sprite size aspect ratio and texture size aspect ratio. To fit texture in a sprite the original quad is scaled and then the additional translation is applied to place the scaled quad properly.

Testing

For testing purposes could be used 2d/sprite_scale.rs. Also, I am thinking that it would be nice to have some tests for a crates/bevy_sprite/src/render/mod.rs:sprite_scale.


Showcase

image

@silvestrpredko silvestrpredko force-pushed the silvestr/fit-sprite-to-quad branch from 68bd63f to 0e7ca6f Compare January 9, 2025 13:47
This change introduces new texture scaling options for sprites that maintain aspect ratio:

- Add `TextureScale` enum with `FillCenter`, `FillStart`, `FillEnd`, `FitCenter`, `FitStart`, and `FitEnd` modes
- Extend `SpriteImageMode` with `ScaleMode` variant to support the new scaling options
@silvestrpredko silvestrpredko force-pushed the silvestr/fit-sprite-to-quad branch from 49186a3 to 296174f Compare January 9, 2025 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant