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

TextureAtlasBuilder now respects insertion order #11474

Merged

Conversation

KirmesBude
Copy link
Contributor

@KirmesBude KirmesBude commented Jan 22, 2024

Objective

TextureAtlases are commonly used to drive animations described as a consecutive range of indices. The current TextureAtlasBuilder uses the AssetId of the image to determine the index of the texture in the TextureAtlas. The AssetId of an Image Asset can change between runs.
The TextureAtlas exposes get_texture_index to get the index from a given AssetId, but this needlessly complicates the process of creating a simple TextureAtlas animation.
Fixes #2459

Solution

  • Use the (ordered) image_ids of the 'texture to place' vector to retrieve the packed locations and compose the textures of the TextureAtlas.

@KirmesBude KirmesBude force-pushed the feature/texture_atlas_builder_order branch from 6846b84 to 05c5478 Compare January 22, 2024 13:04
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen A-Animation Make things move and change over time labels Jan 22, 2024
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jan 22, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 22, 2024
Merged via the queue into bevyengine:main with commit b2e2f8d Jan 22, 2024
26 checks passed
@KirmesBude
Copy link
Contributor Author

@alice-i-cecile There is actually one subtle change that I did not think about. If you add the same image multiple times to the TextureAtlasBuilder it would only be added once to the TextureAtlas. Now it will appear multiple times (the atlas texture itself will still contain the image data only once, but there are additional indices that will point to the same part of the atlas texture).

@alice-i-cecile
Copy link
Member

Hmm. I'm really not sure if that's desired behavior or not.

github-merge-queue bot pushed a commit that referenced this pull request Jan 27, 2024
# Objective

Allow TextureAtlasBuilder in AssetLoader.
Fixes #2987

## Solution

- TextureAtlasBuilder no longer hold just AssetIds that are used to
retrieve the actual image data in `finish`, but &Image instead.
- TextureAtlasBuilder now required AssetId only optionally (and it is
only used to retrieve the index from the AssetId in TextureAtlasLayout),

## Issues

- The issue mentioned here
#11474 (comment)
now also extends to the actual atlas texture. In short: Calling
add_texture multiple times for the same texture will lead to duplicate
image data in the atlas texture and additional indices.
If you provide an AssetId we can probably do something to de-duplicate
the entries while keeping insertion order (suggestions welcome on how
exactly). But if you don't then we are out of luck (unless we can and
want to hash the image, which I do not think we want).

---

## Changelog

### Changed
- TextureAtlasBuilder `add_texture` can be called without providing an
AssetId
- TextureAtlasBuilder `finish` no longer takes Assets<Image> and no
longer returns a Handle<Image>

## Migration Guide

- For `add_texture` you need to wrap your AssetId in Some
- `finish` now returns the atlas texture image directly instead of a
handle. Provide the atlas texture to `add` on Assets<Texture> to get a
Handle<Image>
tjamaan pushed a commit to tjamaan/bevy that referenced this pull request Feb 6, 2024
# Objective

Allow TextureAtlasBuilder in AssetLoader.
Fixes bevyengine#2987

## Solution

- TextureAtlasBuilder no longer hold just AssetIds that are used to
retrieve the actual image data in `finish`, but &Image instead.
- TextureAtlasBuilder now required AssetId only optionally (and it is
only used to retrieve the index from the AssetId in TextureAtlasLayout),

## Issues

- The issue mentioned here
bevyengine#11474 (comment)
now also extends to the actual atlas texture. In short: Calling
add_texture multiple times for the same texture will lead to duplicate
image data in the atlas texture and additional indices.
If you provide an AssetId we can probably do something to de-duplicate
the entries while keeping insertion order (suggestions welcome on how
exactly). But if you don't then we are out of luck (unless we can and
want to hash the image, which I do not think we want).

---

## Changelog

### Changed
- TextureAtlasBuilder `add_texture` can be called without providing an
AssetId
- TextureAtlasBuilder `finish` no longer takes Assets<Image> and no
longer returns a Handle<Image>

## Migration Guide

- For `add_texture` you need to wrap your AssetId in Some
- `finish` now returns the atlas texture image directly instead of a
handle. Provide the atlas texture to `add` on Assets<Texture> to get a
Handle<Image>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Animation Make things move and change over time A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior 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.

TextureAtlasBuilder does not preserve order of inserted textures leading to incorrect animations
3 participants