pub struct TexturePacker<T> {
    pub textures: Vec<T, Global>,
    pub atlas: usize,
    pub skyline: Vec<[u32; 2], Global>,
}
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, Global>

Stores current texture atlas and previously created ones.

§atlas: usize

The index to the current texture atlas.

§skyline: Vec<[u32; 2], Global>

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> TexturePacker<T>where T: ImageSize,

source

pub fn new() -> TexturePacker<T>

Returns a new TexturePacker.

source

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.

source

pub fn update(&mut self, ind: usize, size: [u32; 2]) -> (usize, [u32; 2])

Update current texture atlas.

  • ind: index of atlas offset in the skyline
  • size: size of new tile

Returns the index of the current texture atlas and the atlas offset of the new tile.

source

pub fn find_space(&self, size: [u32; 2]) -> Option<usize>

Returns the index of atlas offset in skyline with room for a new tile.

Returns None if no room was found in the current texture atlas.

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for TexturePacker<T>where T: RefUnwindSafe,

§

impl<T> Send for TexturePacker<T>where T: Send,

§

impl<T> Sync for TexturePacker<T>where T: Sync,

§

impl<T> Unpin for TexturePacker<T>where T: Unpin,

§

impl<T> UnwindSafe for TexturePacker<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.