-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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 a way to specify padding/ margins between sprites in a TextureAtlas. #460
Conversation
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 think theres actually a minor bug here. If you try using this with the linked Kenny asset, we get incorrect results. (the asset has 1x1 px padding, 57 columns, and 31 rows)
You might also notice that the kenny asset already works on master with 57 columns and 31 rows. This is because texture_width
and texture_height
resolve to 16.982456 x 16.967741
and this just happens to render the same as 16x16 (the real tile size).
I think the real "fix" here is to change the api to something a bit more natural:
from_grid(tile_size: Vec2, columns: usize, rows: usize)
from_grid_with_padding(tile_size: Vec2, columns: usize, rows: usize, padding: Vec2)
That should both simplify the calculations and make it easier to reason about
I hope that I understood your comment, @cart. I was also unsure whether the TextureAtlass size should be the size of the texture or the what I currently have (tile size * columns/ rows). Thanks for the comments! |
Yeah I think i made the wrong choice when I wrote the initial I'll take a look at the changes today |
Oh no I forgot to look at this! Sorry about that. Looking now. |
First, we'll want to update the fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
let texture_handle = asset_server
.load("assets/textures/rpg/chars/gabe/gabe-idle-run.png")
.unwrap();
let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1);
let texture_atlas_handle = texture_atlases.add(texture_atlas);
commands
.spawn(Camera2dComponents::default())
.spawn(SpriteSheetComponents {
texture_atlas: texture_atlas_handle,
scale: Scale(6.0),
..Default::default()
})
.with(Timer::from_seconds(0.1, true));
} This currently doesn't render correctly because the sprites.push(Rect {
min: Vec2::new(tile_size.x() + x_padding, tile_size.y() + y_padding),
max: Vec2::new(
(x + 1) as f32 * tile_size.x(),
(y + 1) as f32 * tile_size.y(),
),
}) here |
e33b2d3
to
6f423fe
Compare
I got a little excited last time and pushed before really thinking about it. The code should now work as I was expecting it to. Sorry! |
…as. (bevyengine#460) Add a way to specify padding between sprites in a TextureAtlas
Occasionally, it's useful to be able to specify margins between sprites in a TextureAtlas, like for using Kenney.nl sprite sheets
e.g.
(https://www.kenney.nl/assets/roguelike-rpg-pack)