bevy/crates
Félix Lescaudey de Maneville 135c7240f1
Texture Atlas rework (#5103)
# Objective

> Old MR: #5072 
> ~~Associated UI MR: #5070~~
> Adresses #1618

Unify sprite management

## Solution

- Remove the `Handle<Image>` field in `TextureAtlas` which is the main
cause for all the boilerplate
- Remove the redundant `TextureAtlasSprite` component
- Renamed `TextureAtlas` asset to `TextureAtlasLayout`
([suggestion](https://github.com/bevyengine/bevy/pull/5103#discussion_r917281844))
- Add a `TextureAtlas` component, containing the atlas layout handle and
the section index

The difference between this solution and #5072 is that instead of the
`enum` approach is that we can more easily manipulate texture sheets
without any breaking changes for classic `SpriteBundle`s (@mockersf
[comment](https://github.com/bevyengine/bevy/pull/5072#issuecomment-1165836139))

Also, this approach is more *data oriented* extracting the
`Handle<Image>` and avoiding complex texture atlas manipulations to
retrieve the texture in both applicative and engine code.
With this method, the only difference between a `SpriteBundle` and a
`SpriteSheetBundle` is an **additional** component storing the atlas
handle and the index.

~~This solution can be applied to `bevy_ui` as well (see #5070).~~

EDIT: I also applied this solution to Bevy UI

## Changelog

- (**BREAKING**) Removed `TextureAtlasSprite`
- (**BREAKING**) Renamed `TextureAtlas` to `TextureAtlasLayout`
- (**BREAKING**) `SpriteSheetBundle`:
  - Uses a  `Sprite` instead of a `TextureAtlasSprite` component
- Has a `texture` field containing a `Handle<Image>` like the
`SpriteBundle`
- Has a new `TextureAtlas` component instead of a
`Handle<TextureAtlasLayout>`
- (**BREAKING**) `DynamicTextureAtlasBuilder::add_texture` takes an
additional `&Handle<Image>` parameter
- (**BREAKING**) `TextureAtlasLayout::from_grid` no longer takes a
`Handle<Image>` parameter
- (**BREAKING**) `TextureAtlasBuilder::finish` now returns a
`Result<(TextureAtlasLayout, Handle<Image>), _>`
- `bevy_text`:
  - `GlyphAtlasInfo` stores the texture `Handle<Image>`
  - `FontAtlas` stores the texture `Handle<Image>`
- `bevy_ui`:
- (**BREAKING**) Removed `UiAtlasImage` , the atlas bundle is now
identical to the `ImageBundle` with an additional `TextureAtlas`

## Migration Guide

* Sprites

```diff
fn my_system(
  mut images: ResMut<Assets<Image>>, 
-  mut atlases: ResMut<Assets<TextureAtlas>>, 
+  mut atlases: ResMut<Assets<TextureAtlasLayout>>, 
  asset_server: Res<AssetServer>
) {
    let texture_handle: asset_server.load("my_texture.png");
-   let layout = TextureAtlas::from_grid(texture_handle, Vec2::new(25.0, 25.0), 5, 5, None, None);
+   let layout = TextureAtlasLayout::from_grid(Vec2::new(25.0, 25.0), 5, 5, None, None);
    let layout_handle = atlases.add(layout);
    commands.spawn(SpriteSheetBundle {
-      sprite: TextureAtlasSprite::new(0),
-      texture_atlas: atlas_handle,
+      atlas: TextureAtlas {
+         layout: layout_handle,
+         index: 0
+      },
+      texture: texture_handle,
       ..Default::default()
     });
}
```
* UI


```diff
fn my_system(
  mut images: ResMut<Assets<Image>>, 
-  mut atlases: ResMut<Assets<TextureAtlas>>, 
+  mut atlases: ResMut<Assets<TextureAtlasLayout>>, 
  asset_server: Res<AssetServer>
) {
    let texture_handle: asset_server.load("my_texture.png");
-   let layout = TextureAtlas::from_grid(texture_handle, Vec2::new(25.0, 25.0), 5, 5, None, None);
+   let layout = TextureAtlasLayout::from_grid(Vec2::new(25.0, 25.0), 5, 5, None, None);
    let layout_handle = atlases.add(layout);
    commands.spawn(AtlasImageBundle {
-      texture_atlas_image: UiTextureAtlasImage {
-           index: 0,
-           flip_x: false,
-           flip_y: false,
-       },
-      texture_atlas: atlas_handle,
+      atlas: TextureAtlas {
+         layout: layout_handle,
+         index: 0
+      },
+      image: UiImage {
+           texture: texture_handle,
+           flip_x: false,
+           flip_y: false,
+       },
       ..Default::default()
     });
}
```

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: François <mockersf@gmail.com>
Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
2024-01-16 13:59:08 +00:00
..
bevy_a11y resolve all internal ambiguities (#10411) 2024-01-09 19:08:15 +00:00
bevy_animation Skip alloc when updating animation path cache (#11330) 2024-01-13 19:33:11 +00:00
bevy_app resolve all internal ambiguities (#10411) 2024-01-09 19:08:15 +00:00
bevy_asset Better error message on incorrect asset label (#11254) 2024-01-08 22:45:07 +00:00
bevy_audio Remove the ability to ignore global volume (#11092) 2024-01-15 15:31:54 +00:00
bevy_core Replace or document ignored doctests (#11040) 2024-01-01 16:50:56 +00:00
bevy_core_pipeline Revert "Implement minimal reflection probes. (#10057)" (#11307) 2024-01-12 20:41:51 +00:00
bevy_derive Reexport winit::platform::android::activity::* in bevy_winit (#11011) 2023-12-19 20:15:03 +00:00
bevy_diagnostic Describe purpose of bevy_diagnostic (#11327) 2024-01-14 20:17:26 +00:00
bevy_dylib Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_dynamic_plugin Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_ecs Async channel v2 (#10692) 2024-01-15 19:23:00 +00:00
bevy_ecs_compile_fail_tests Rename WorldQueryData & WorldQueryFilter to QueryData & QueryFilter (#10779) 2023-12-12 19:45:50 +00:00
bevy_encase_derive Add [lints] table, fix adding #![allow(clippy::type_complexity)] everywhere (#10011) 2023-11-18 20:58:48 +00:00
bevy_gilrs Remove unnecessary path prefixes (#10749) 2023-11-28 23:43:40 +00:00
bevy_gizmos Warn when bevy_sprite and bevy_pbr are not enabled with bevy_gizmos (#11296) 2024-01-14 13:51:14 +00:00
bevy_gltf Use EntityHashMap whenever possible (#11353) 2024-01-15 15:51:17 +00:00
bevy_hierarchy Inline trivial methods in bevy_hierarchy (#11332) 2024-01-13 22:20:50 +00:00
bevy_input Extend Touches with clear and reset methods (#10930) 2024-01-04 23:13:29 +00:00
bevy_internal Revert "Implement minimal reflection probes. (#10057)" (#11307) 2024-01-12 20:41:51 +00:00
bevy_log Add support for updating the tracing subscriber in LogPlugin (#10822) 2024-01-15 15:26:13 +00:00
bevy_macro_utils Bump toml_edit in build-template-pages tool (#11342) 2024-01-16 05:23:18 +00:00
bevy_macros_compile_fail_tests Standardize toml format with taplo (#10594) 2023-11-21 01:04:14 +00:00
bevy_math Remove Default impl for CubicCurve (#11335) 2024-01-14 04:40:37 +00:00
bevy_mikktspace Update glam, encase and hexasphere (#11082) 2024-01-08 22:58:45 +00:00
bevy_pbr Approximate indirect specular occlusion (#11152) 2024-01-15 16:10:55 +00:00
bevy_ptr Simplify equality assertions (#10988) 2023-12-16 23:58:41 +00:00
bevy_reflect Update glam, encase and hexasphere (#11082) 2024-01-08 22:58:45 +00:00
bevy_reflect_compile_fail_tests Fix nested generics in Reflect derive (#10791) 2023-11-29 01:46:09 +00:00
bevy_render Camera-driven UI (#10559) 2024-01-16 00:39:10 +00:00
bevy_scene Change Entity::generation from u32 to NonZeroU32 for niche optimization (#9907) 2024-01-08 23:03:00 +00:00
bevy_sprite Texture Atlas rework (#5103) 2024-01-16 13:59:08 +00:00
bevy_tasks Async channel v2 (#10692) 2024-01-15 19:23:00 +00:00
bevy_text Texture Atlas rework (#5103) 2024-01-16 13:59:08 +00:00
bevy_time Add paused run condition (#11313) 2024-01-12 22:18:57 +00:00
bevy_transform Rename "AddChild" to "PushChild" (#11194) 2024-01-04 16:06:14 +00:00
bevy_ui Texture Atlas rework (#5103) 2024-01-16 13:59:08 +00:00
bevy_utils Add static assertions to bevy_utils for compile-time checks (#11182) 2024-01-02 22:08:30 +00:00
bevy_window Remove CanvasParentResizePlugin (#11057) 2023-12-21 20:01:22 +00:00
bevy_winit Change WinitPlugin defaults to limit game update rate when window is not visible (for real this time) (#11305) 2024-01-15 17:53:35 +00:00