bevyengine / bevyengine/bevy

Update a TextureAtlas index based on the row and column of the TextureAtlasLayout::from_grid params

Open
#14,739 0 comments 0 reactions 0 assignees View on GitHub
A-Rendering C-Usability D-Straightforward S-Needs-Design
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## What problem does this solve or what need does it fill?
Presuming that most of people create their `TextureAtlas` using the `TextureAtlasLayout::from_grid` function, it feels weird that we cannot update its index using grid identifier (colum & row).

As of today, there is no built-in way to update the index of a `TextureAtlas` with the column and row of the `UVec` you want.

## What solution would you like?
Something like this:
```rs
// Setting up a sprite based on an sprite sheet file
let texture = asset_server.load("spritesheet.png");
let atlas_layout = texture_atlases.add(TextureAtlasLayout::from_grid(UVec2::splat(64), 6, 6, None, None));

commands.spawn((
SpriteBundle {
texture,
..default()
},
TextureAtlas::from(atlas_layout)
));

// Then inside a system, making them vary
pub fn randomize_image(
mut sprites_query: Query<(&SpriteSheet, &mut TextureAtlas)>
) {
for (sprite_sheet, mut atlas) in sprites_query.iter_mut() {
let col = rand::thread_rng().gen_range(1..6);
let row = rand::thread_rng().gen_range(1..6);
atlas.set_index_from_grid(col, row);
}
}
```

## What alternative(s) have you considered?
I built a custom `SpriteSheet` struct wrapping the `TextureAtlasLayout` to introduce what I wanted:
```rs
#[derive(Component, Default, Clone)]
pub struct SpriteSheet {
pub atlas_layout: Handle,
pub grid_width: u32
}

impl SpriteSheet {
pub fn new (atlas_layout: Handle, grid_width: u32) -> Self {
Self {
atlas_layout,
grid_width,
}
}

pub fn get_asset_index_at(&self, column: u32, row: u32) -> u32 {
(row-1) * self.grid_width + column-1
}
}
```
## Additional context
This feature is somewhat easy to introduce as it's basic math based on the grid width:
```math
index = (row-1) * self.grid width + column-1
```
and I would love to provide a PR for this. But based on the fact that `TextureAtlas` is not forcefully built on top of a grid, where to store the `grid_width`?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.