Struct graphics::texture_packer::TexturePacker
source · pub struct TexturePacker<T> {
pub textures: Vec<T>,
pub atlas: usize,
pub skyline: Vec<[u32; 2]>,
}
Expand description
A texture packer using a skyline heuristic.
For offline texture packing, see texture_packer.
Designed for adding textures one by one to current texture atlas. Packs tiles without backtracking or knowledge about future tiles.
- Perfect at packing tiles of same size
- Good at packing tiles of some unit size
- Decent at packing tiles of similar sizes
- Can be used with pre-sorted tile sizes for better packing
Can also be used as storage for textures.
Design
A skyline is a list of non-hole atlas offsets, used to efficiently determine a good place to put the next tile.
In this texture packer, only a single skyline is kept track of, since new texture atlases are created by need.
This texture packer has runtime complexity O(N^2)
for inserting a new tile,
where N
is the number of points in the skyline.
Since N
is usually a low number, the packing is pretty fast.
The algorithm was designed by Sven Nilsen (2019) for Piston-Graphics.
Fields§
§textures: Vec<T>
Stores current texture atlas and previously created ones.
atlas: usize
The index to the current texture atlas.
skyline: Vec<[u32; 2]>
Texture atlas offsets from left to right.
When a new tile is added with same offset, it updates the atlas offsets that it overlaps. This means that “holes” get filled in over time.
Implementations§
source§impl<T: ImageSize> TexturePacker<T>
impl<T: ImageSize> TexturePacker<T>
sourcepub fn new() -> TexturePacker<T>
pub fn new() -> TexturePacker<T>
Returns a new TexturePacker
.
sourcepub fn create(&mut self, size: [u32; 2], texture: T) -> usize
pub fn create(&mut self, size: [u32; 2], texture: T) -> usize
Create a new texture atlas with an initial tile.
The new texture atlas is made the current one.