2022-09-02 12:35:23 +00:00
|
|
|
use crate::TextureAtlas;
|
2020-07-30 21:35:07 +00:00
|
|
|
use bevy_asset::Assets;
|
2022-09-02 12:35:23 +00:00
|
|
|
use bevy_math::{IVec2, Rect, Vec2};
|
2024-01-03 03:31:04 +00:00
|
|
|
use bevy_render::{
|
|
|
|
render_asset::RenderAssetPersistencePolicy,
|
|
|
|
texture::{Image, TextureFormatPixelInfo},
|
|
|
|
};
|
2020-07-30 21:35:07 +00:00
|
|
|
use guillotiere::{size2, Allocation, AtlasAllocator};
|
2020-06-07 18:30:04 +00:00
|
|
|
|
2023-03-03 18:14:40 +00:00
|
|
|
/// Helper utility to update [`TextureAtlas`] on the fly.
|
|
|
|
///
|
|
|
|
/// Helpful in cases when texture is created procedurally,
|
|
|
|
/// e.g: in a font glyph [`TextureAtlas`], only add the [`Image`] texture for letters to be rendered.
|
2020-06-07 18:30:04 +00:00
|
|
|
pub struct DynamicTextureAtlasBuilder {
|
2023-03-03 18:14:40 +00:00
|
|
|
atlas_allocator: AtlasAllocator,
|
|
|
|
padding: i32,
|
2020-06-07 18:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DynamicTextureAtlasBuilder {
|
2023-03-03 18:14:40 +00:00
|
|
|
/// Create a new [`DynamicTextureAtlasBuilder`]
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `size` - total size for the atlas
|
|
|
|
/// * `padding` - gap added between textures in the atlas, both in x axis and y axis
|
2020-07-30 21:35:07 +00:00
|
|
|
pub fn new(size: Vec2, padding: i32) -> Self {
|
2020-06-07 18:30:04 +00:00
|
|
|
Self {
|
2020-06-14 01:53:31 +00:00
|
|
|
atlas_allocator: AtlasAllocator::new(to_size2(size)),
|
2020-07-30 21:35:07 +00:00
|
|
|
padding,
|
2020-06-07 18:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 18:14:40 +00:00
|
|
|
/// Add a new texture to [`TextureAtlas`].
|
2024-01-03 03:31:04 +00:00
|
|
|
/// It is user's responsibility to pass in the correct [`TextureAtlas`],
|
|
|
|
/// and that [`TextureAtlas::texture`] has [`Image::cpu_persistent_access`]
|
|
|
|
/// set to [`RenderAssetPersistencePolicy::Keep`]
|
2020-06-14 01:53:31 +00:00
|
|
|
pub fn add_texture(
|
|
|
|
&mut self,
|
|
|
|
texture_atlas: &mut TextureAtlas,
|
2021-12-14 03:58:23 +00:00
|
|
|
textures: &mut Assets<Image>,
|
|
|
|
texture: &Image,
|
|
|
|
) -> Option<usize> {
|
2020-07-30 21:35:07 +00:00
|
|
|
let allocation = self.atlas_allocator.allocate(size2(
|
2023-10-23 20:49:02 +00:00
|
|
|
texture.width() as i32 + self.padding,
|
|
|
|
texture.height() as i32 + self.padding,
|
2020-07-30 21:35:07 +00:00
|
|
|
));
|
2020-06-14 01:53:31 +00:00
|
|
|
if let Some(allocation) = allocation {
|
|
|
|
let atlas_texture = textures.get_mut(&texture_atlas.texture).unwrap();
|
2024-01-03 03:31:04 +00:00
|
|
|
assert_eq!(
|
|
|
|
atlas_texture.cpu_persistent_access,
|
|
|
|
RenderAssetPersistencePolicy::Keep
|
|
|
|
);
|
|
|
|
|
2020-06-14 01:53:31 +00:00
|
|
|
self.place_texture(atlas_texture, allocation, texture);
|
2022-09-02 12:35:23 +00:00
|
|
|
let mut rect: Rect = to_rect(allocation.rectangle);
|
|
|
|
rect.max -= self.padding as f32;
|
2021-12-14 03:58:23 +00:00
|
|
|
Some(texture_atlas.add_texture(rect))
|
2020-06-14 01:53:31 +00:00
|
|
|
} else {
|
|
|
|
None
|
2020-06-07 18:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 01:53:31 +00:00
|
|
|
fn place_texture(
|
|
|
|
&mut self,
|
2021-12-14 03:58:23 +00:00
|
|
|
atlas_texture: &mut Image,
|
2020-06-14 01:53:31 +00:00
|
|
|
allocation: Allocation,
|
2021-12-14 03:58:23 +00:00
|
|
|
texture: &Image,
|
2020-06-14 01:53:31 +00:00
|
|
|
) {
|
2020-07-30 21:35:07 +00:00
|
|
|
let mut rect = allocation.rectangle;
|
|
|
|
rect.max.x -= self.padding;
|
|
|
|
rect.max.y -= self.padding;
|
2023-10-23 20:49:02 +00:00
|
|
|
let atlas_width = atlas_texture.width() as usize;
|
2020-06-07 18:30:04 +00:00
|
|
|
let rect_width = rect.width() as usize;
|
2021-12-14 03:58:23 +00:00
|
|
|
let format_size = atlas_texture.texture_descriptor.format.pixel_size();
|
2020-06-07 18:30:04 +00:00
|
|
|
|
|
|
|
for (texture_y, bound_y) in (rect.min.y..rect.max.y).map(|i| i as usize).enumerate() {
|
2020-07-26 19:08:41 +00:00
|
|
|
let begin = (bound_y * atlas_width + rect.min.x as usize) * format_size;
|
|
|
|
let end = begin + rect_width * format_size;
|
|
|
|
let texture_begin = texture_y * rect_width * format_size;
|
|
|
|
let texture_end = texture_begin + rect_width * format_size;
|
2020-06-14 01:53:31 +00:00
|
|
|
atlas_texture.data[begin..end]
|
2020-06-07 18:30:04 +00:00
|
|
|
.copy_from_slice(&texture.data[texture_begin..texture_end]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 12:35:23 +00:00
|
|
|
fn to_rect(rectangle: guillotiere::Rectangle) -> Rect {
|
|
|
|
Rect {
|
|
|
|
min: IVec2::new(rectangle.min.x, rectangle.min.y).as_vec2(),
|
|
|
|
max: IVec2::new(rectangle.max.x, rectangle.max.y).as_vec2(),
|
2020-06-07 18:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_size2(vec2: Vec2) -> guillotiere::Size {
|
2020-11-17 21:40:18 +00:00
|
|
|
guillotiere::Size::new(vec2.x as i32, vec2.y as i32)
|
2020-06-07 18:30:04 +00:00
|
|
|
}
|