diff --git a/crates/bevy_sprite/src/texture_atlas.rs b/crates/bevy_sprite/src/texture_atlas.rs index 49d366fa64ff1..88c5dba6fef41 100644 --- a/crates/bevy_sprite/src/texture_atlas.rs +++ b/crates/bevy_sprite/src/texture_atlas.rs @@ -66,27 +66,22 @@ impl TextureAtlas { } } - /// Generate a `TextureAtlas` by splitting a texture into a grid where each - /// cell of the grid of `tile_size` is one of the textures in the atlas + /// Generate a `TextureAtlas` by splitting a texture into a grid. + /// + /// Each cell in the grid of `tile_size` corresponds to one texture in the atlas. + /// + /// * Cells can have horizontal and/or vertical separation specified by `padding` + /// * The grid can start at a point `offset` relative to the top-left corner of the texture pub fn from_grid( texture: Handle, tile_size: Vec2, columns: usize, rows: usize, + padding: Option, + offset: Option, ) -> TextureAtlas { - Self::from_grid_with_padding(texture, tile_size, columns, rows, Vec2::new(0f32, 0f32)) - } - - /// Generate a `TextureAtlas` by splitting a texture into a grid where each - /// cell of the grid of `tile_size` is one of the textures in the atlas and is separated by - /// some `padding` in the texture - pub fn from_grid_with_padding( - texture: Handle, - tile_size: Vec2, - columns: usize, - rows: usize, - padding: Vec2, - ) -> TextureAtlas { + let padding = padding.unwrap_or_default(); + let offset = offset.unwrap_or_default(); let mut sprites = Vec::new(); let mut x_padding = 0.0; let mut y_padding = 0.0; @@ -101,8 +96,8 @@ impl TextureAtlas { } let rect_min = Vec2::new( - (tile_size.x + x_padding) * x as f32, - (tile_size.y + y_padding) * y as f32, + offset.x + (tile_size.x + x_padding) * x as f32, + offset.y + (tile_size.y + y_padding) * y as f32, ); sprites.push(Rect { diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index acf025a02ab76..4e594f2dedd18 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -28,7 +28,8 @@ fn setup( mut texture_atlases: ResMut>, ) { let texture_handle = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png"); - let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1); + let texture_atlas = + TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, None, None); let texture_atlas_handle = texture_atlases.add(texture_atlas); commands.spawn_bundle(OrthographicCameraBundle::new_2d()); commands