Skip to content

Commit 3d48723

Browse files
FraserLeejames7132
authored andcommitted
Added offset parameter to TextureAtlas::from_grid_with_padding (bevyengine#4836)
# Objective Increase compatibility with a fairly common format of padded spritesheets, in which half the padding value occurs before the first sprite box begins. The original behaviour falls out when `Vec2::ZERO` is used for `offset`. See below unity screenshot for an example of a spritesheet with padding ![Screen Shot 2022-05-24 at 4 11 49 PM](https://user-images.githubusercontent.com/30442265/170123682-287e5733-b69d-452b-b2e6-46d8d29293fb.png) ## Solution Tiny change to `crates/bevy_sprite/src/texture_atlas.rs` ## Migration Guide Calls to `TextureAtlas::from_grid_with_padding` should be modified to include a new parameter, which can be set to `Vec2::ZERO` to retain old behaviour. ```rust from_grid_with_padding(texture, tile_size, columns, rows, padding) | V from_grid_with_padding(texture, tile_size, columns, rows, padding, Vec2::ZERO) ``` Co-authored-by: FraserLee <30442265+FraserLee@users.noreply.github.com>
1 parent 9b0ac6b commit 3d48723

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

crates/bevy_sprite/src/texture_atlas.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,27 @@ impl TextureAtlas {
6868
}
6969

7070
/// Generate a `TextureAtlas` by splitting a texture into a grid where each
71-
/// cell of the grid of `tile_size` is one of the textures in the atlas
71+
/// `tile_size` by `tile_size` grid-cell is one of the textures in the atlas
7272
pub fn from_grid(
7373
texture: Handle<Image>,
7474
tile_size: Vec2,
7575
columns: usize,
7676
rows: usize,
7777
) -> TextureAtlas {
78-
Self::from_grid_with_padding(texture, tile_size, columns, rows, Vec2::new(0f32, 0f32))
78+
Self::from_grid_with_padding(texture, tile_size, columns, rows, Vec2::ZERO, Vec2::ZERO)
7979
}
8080

8181
/// Generate a `TextureAtlas` by splitting a texture into a grid where each
82-
/// cell of the grid of `tile_size` is one of the textures in the atlas and is separated by
83-
/// some `padding` in the texture
82+
/// `tile_size` by `tile_size` grid-cell is one of the textures in the
83+
/// atlas. Grid cells are separated by some `padding`, and the grid starts
84+
/// at `offset` pixels from the top left corner.
8485
pub fn from_grid_with_padding(
8586
texture: Handle<Image>,
8687
tile_size: Vec2,
8788
columns: usize,
8889
rows: usize,
8990
padding: Vec2,
91+
offset: Vec2,
9092
) -> TextureAtlas {
9193
let mut sprites = Vec::new();
9294
let mut x_padding = 0.0;
@@ -102,8 +104,8 @@ impl TextureAtlas {
102104
}
103105

104106
let rect_min = Vec2::new(
105-
(tile_size.x + x_padding) * x as f32,
106-
(tile_size.y + y_padding) * y as f32,
107+
(tile_size.x + x_padding) * x as f32 + offset.x,
108+
(tile_size.y + y_padding) * y as f32 + offset.y,
107109
);
108110

109111
sprites.push(Rect {

0 commit comments

Comments
 (0)