From 9c48e5cccbaebba18aad16c954c79ffc7f370d16 Mon Sep 17 00:00:00 2001 From: M <9271353+sY9sE33@users.noreply.github.com> Date: Wed, 14 Oct 2020 23:49:07 -0400 Subject: [PATCH] Add a way to specify padding/ margins between sprites in a TextureAtlas. (#460) Add a way to specify padding between sprites in a TextureAtlas --- crates/bevy_sprite/src/texture_atlas.rs | 47 +++++++++++++++++++------ examples/2d/sprite_sheet.rs | 4 +-- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/crates/bevy_sprite/src/texture_atlas.rs b/crates/bevy_sprite/src/texture_atlas.rs index e41e516887e94..2cee26d7db4fc 100644 --- a/crates/bevy_sprite/src/texture_atlas.rs +++ b/crates/bevy_sprite/src/texture_atlas.rs @@ -63,29 +63,56 @@ impl TextureAtlas { } /// Generate a `TextureAtlas` by splitting a texture into a grid where each - /// cell of the grid is one of the textures in the atlas + /// cell of the grid of `tile_size` is one of the textures in the atlas pub fn from_grid( texture: Handle, - size: Vec2, + tile_size: Vec2, columns: usize, rows: usize, ) -> TextureAtlas { - let texture_width = size.x() / columns as f32; - let texture_height = size.y() / rows as f32; + 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 mut sprites = Vec::new(); + let mut x_padding = 0.0; + let mut y_padding = 0.0; + for y in 0..rows { + if y > 0 { + y_padding = padding.y(); + } for x in 0..columns { + if x > 0 { + x_padding = padding.x(); + } + + let rect_min = Vec2::new( + (tile_size.x() + x_padding) * x as f32, + (tile_size.y() + y_padding) * y as f32, + ); + sprites.push(Rect { - min: Vec2::new(x as f32 * texture_width, y as f32 * texture_height), - max: Vec2::new( - (x + 1) as f32 * texture_width, - (y + 1) as f32 * texture_height, - ), + min: rect_min, + max: Vec2::new(rect_min.x() + tile_size.x(), rect_min.y() + tile_size.y()), }) } } + TextureAtlas { - size, + size: Vec2::new( + ((tile_size.x() + x_padding) * columns as f32) - x_padding, + ((tile_size.y() + y_padding) * rows as f32) - y_padding, + ), textures: sprites, texture, texture_handles: None, diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index bc1c0b3a7ac1b..155dd6afd598f 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -32,8 +32,8 @@ fn setup( "assets/textures/rpg/chars/gabe/gabe-idle-run.png", ) .unwrap(); - let texture = textures.get(&texture_handle).unwrap(); - let texture_atlas = TextureAtlas::from_grid(texture_handle, texture.size, 7, 1); + + 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())