mirror of
https://github.com/bevyengine/bevy
synced 2024-11-25 14:10:19 +00:00
Add a way to specify padding/ margins between sprites in a TextureAtlas. (#460)
Add a way to specify padding between sprites in a TextureAtlas
This commit is contained in:
parent
1f7fe77f32
commit
9c48e5cccb
2 changed files with 39 additions and 12 deletions
|
@ -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<Texture>,
|
||||
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<Texture>,
|
||||
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,
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Reference in a new issue