mirror of
https://github.com/bevyengine/bevy
synced 2024-12-22 19:13:08 +00:00
135c7240f1
# 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>
114 lines
3.3 KiB
Rust
114 lines
3.3 KiB
Rust
use ab_glyph::{GlyphId, Point};
|
|
use bevy_asset::{Assets, Handle};
|
|
use bevy_math::Vec2;
|
|
use bevy_render::{
|
|
render_asset::RenderAssetPersistencePolicy,
|
|
render_resource::{Extent3d, TextureDimension, TextureFormat},
|
|
texture::Image,
|
|
};
|
|
use bevy_sprite::{DynamicTextureAtlasBuilder, TextureAtlasLayout};
|
|
use bevy_utils::HashMap;
|
|
|
|
#[cfg(feature = "subpixel_glyph_atlas")]
|
|
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
|
|
pub struct SubpixelOffset {
|
|
x: u16,
|
|
y: u16,
|
|
}
|
|
|
|
#[cfg(feature = "subpixel_glyph_atlas")]
|
|
impl From<Point> for SubpixelOffset {
|
|
fn from(p: Point) -> Self {
|
|
fn f(v: f32) -> u16 {
|
|
((v % 1.) * (u16::MAX as f32)) as u16
|
|
}
|
|
Self {
|
|
x: f(p.x),
|
|
y: f(p.y),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(not(feature = "subpixel_glyph_atlas"))]
|
|
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
|
|
pub struct SubpixelOffset;
|
|
|
|
#[cfg(not(feature = "subpixel_glyph_atlas"))]
|
|
impl From<Point> for SubpixelOffset {
|
|
fn from(_: Point) -> Self {
|
|
Self
|
|
}
|
|
}
|
|
|
|
pub struct FontAtlas {
|
|
pub dynamic_texture_atlas_builder: DynamicTextureAtlasBuilder,
|
|
pub glyph_to_atlas_index: HashMap<(GlyphId, SubpixelOffset), usize>,
|
|
pub texture_atlas: Handle<TextureAtlasLayout>,
|
|
pub texture: Handle<Image>,
|
|
}
|
|
|
|
impl FontAtlas {
|
|
pub fn new(
|
|
textures: &mut Assets<Image>,
|
|
texture_atlases: &mut Assets<TextureAtlasLayout>,
|
|
size: Vec2,
|
|
) -> FontAtlas {
|
|
let texture = textures.add(Image::new_fill(
|
|
Extent3d {
|
|
width: size.x as u32,
|
|
height: size.y as u32,
|
|
depth_or_array_layers: 1,
|
|
},
|
|
TextureDimension::D2,
|
|
&[0, 0, 0, 0],
|
|
TextureFormat::Rgba8UnormSrgb,
|
|
// Need to keep this image CPU persistent in order to add additional glyphs later on
|
|
RenderAssetPersistencePolicy::Keep,
|
|
));
|
|
let texture_atlas = TextureAtlasLayout::new_empty(size);
|
|
Self {
|
|
texture_atlas: texture_atlases.add(texture_atlas),
|
|
glyph_to_atlas_index: HashMap::default(),
|
|
dynamic_texture_atlas_builder: DynamicTextureAtlasBuilder::new(size, 0),
|
|
texture,
|
|
}
|
|
}
|
|
|
|
pub fn get_glyph_index(
|
|
&self,
|
|
glyph_id: GlyphId,
|
|
subpixel_offset: SubpixelOffset,
|
|
) -> Option<usize> {
|
|
self.glyph_to_atlas_index
|
|
.get(&(glyph_id, subpixel_offset))
|
|
.copied()
|
|
}
|
|
|
|
pub fn has_glyph(&self, glyph_id: GlyphId, subpixel_offset: SubpixelOffset) -> bool {
|
|
self.glyph_to_atlas_index
|
|
.contains_key(&(glyph_id, subpixel_offset))
|
|
}
|
|
|
|
pub fn add_glyph(
|
|
&mut self,
|
|
textures: &mut Assets<Image>,
|
|
texture_atlases: &mut Assets<TextureAtlasLayout>,
|
|
glyph_id: GlyphId,
|
|
subpixel_offset: SubpixelOffset,
|
|
texture: &Image,
|
|
) -> bool {
|
|
let texture_atlas = texture_atlases.get_mut(&self.texture_atlas).unwrap();
|
|
if let Some(index) = self.dynamic_texture_atlas_builder.add_texture(
|
|
texture_atlas,
|
|
textures,
|
|
texture,
|
|
&self.texture,
|
|
) {
|
|
self.glyph_to_atlas_index
|
|
.insert((glyph_id, subpixel_offset), index);
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|