Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - add_texture returns index to texture #2864

Closed
wants to merge 1 commit into from

Conversation

hymm
Copy link
Contributor

@hymm hymm commented Sep 24, 2021

If you need to build a texture atlas from an already created texture that is not match a grid, you need to use new_empty and add_texture to create it. However it is not straight forward to get the index to be used with TextureAtlasSprite. add_texture should be changed to return the index to the texture.

Currently you can do something like this:

let texture = asset_server.load::<Texture>::("texture.png");
let texture_atlas = TextureAtlas::new_empty(texture, Vec2::new(40.0, 40.0));

texture_atlas.add_texture(Rect { 
  min: Vec2::new(20.0, 20.0),
  max: Vec2::new(40.0, 40.0),
});
let index = (texture_atlas.len() - 1) as u32;

let texture_atlas_sprite = TextureAtlasSprite {
  index,
  Default::default()
};

But this is more clear

let index = texture_atlas.add_texture(Rect { 
  min: Vec2::new(20.0, 20.0),
  max: Vec2::new(40.0, 40.0),
});

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Sep 24, 2021
Copy link
Contributor

@inodentry inodentry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this. :)

@inodentry inodentry added A-Assets Load files from disk to use for things like images, models, and sounds A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Needs-Review and removed S-Needs-Triage This issue needs to be labelled labels Sep 24, 2021
Copy link
Member

@NiklasEi NiklasEi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice enhancement 👍

///
/// # Arguments
///
/// * `rect` - The section of the atlas that contains the texture to be added,
/// from the top-left corner of the texture to the bottom-right corner
pub fn add_texture(&mut self, rect: Rect) {
pub fn add_texture(&mut self, rect: Rect) -> u32 {
Copy link
Member

@NiklasEi NiklasEi Sep 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking a look at get_texture_index we are returning the index as usize. I think we should do the same here.

Copy link
Contributor Author

@hymm hymm Sep 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference would actually be to change get_texture_index to return a u32 too. TextureAtlasSprite and the shader for it takes a u32.

Copy link
Member

@NiklasEi NiklasEi Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should come up with a guideline to keep Bevy consistent. My opinion would be to use usize for indices and cast them to u32 when passing them to shaders. Initial responses on Discord suggest the same. Making all this consistent should be another PR though, so I wouldn't block this one based on the inconsistency.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, this needs to be u32 because we directly use the TextureAtlasSprite type when generating shader uniform buffers. In the renderer rework, this is no longer the case. And I agree that we should favor usize in user facing types for more natural indexing.

I think we should merge this as-is. Can you create another PR for the pipelined-rendering branch and make the relevant types in bevy_sprites2 use usize?

@NiklasEi NiklasEi added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review labels Sep 27, 2021
@cart
Copy link
Member

cart commented Sep 28, 2021

bors r+

bors bot pushed a commit that referenced this pull request Sep 28, 2021
If you need to build a texture atlas from an already created texture that is not match a grid, you need to use new_empty and add_texture to create it.  However it is not straight forward to get the index to be used with TextureAtlasSprite. add_texture should be changed to return the index to the texture.

Currently you can do something like this:

```rs
let texture = asset_server.load::<Texture>::("texture.png");
let texture_atlas = TextureAtlas::new_empty(texture, Vec2::new(40.0, 40.0));

texture_atlas.add_texture(Rect { 
  min: Vec2::new(20.0, 20.0),
  max: Vec2::new(40.0, 40.0),
});
let index = (texture_atlas.len() - 1) as u32;

let texture_atlas_sprite = TextureAtlasSprite {
  index,
  Default::default()
};
```

But this is more clear
```rs
let index = texture_atlas.add_texture(Rect { 
  min: Vec2::new(20.0, 20.0),
  max: Vec2::new(40.0, 40.0),
});
```
@bors bors bot changed the title add_texture returns index to texture [Merged by Bors] - add_texture returns index to texture Sep 28, 2021
@bors bors bot closed this Sep 28, 2021
bors bot pushed a commit that referenced this pull request Nov 1, 2021
Per this comment #2864 (comment),
I have done a pass at changing all the public facing indexes for `TextureAtlas` to usize.
@hymm hymm deleted the add-texture-returns-index branch October 5, 2023 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants