class TreeTextureAtlasLayout

Organizes a set of textures into a single larger texture (an atlas) by minimizing empty space.

Uses a tree structure to minimize wasted space. Elements can be dynamically added and removed from the layout.

Public

Constructors

TreeTextureAtlasLayout

TreeTextureAtlasLayout(const TreeTextureAtlasLayoutSettings &settings = TreeTextureAtlasLayoutSettings())

Methods

AddElement

TOptional<Allocation> AddElement(const Size2UI &size)

Attempts to add a new element in the layout.

size
Size of the element to add, in pixels.

Returns: Location at which the element was stored, or null if no free space was located.

RemoveElement

void RemoveElement(u32 pageId, u32 nodeId)

Removes an element from the provided node.

Grow

void Grow(const Size2UI &newSize)

Grows the atlas without changing existing allocations.

New size cannot be smaller than existing size.

Clear

void Clear()

Removes all entries from the layout.

IsEmpty

bool IsEmpty() const

Checks have any elements been added to the layout.

IsPageEmpty

bool IsPageEmpty(u32 pageId) const

Checks is a specific page empty.

GetSize

const Size2UI &GetSize() const

Returns the size of the atlas texture, in pixels.

Private

Methods

GetFreeNodeBucketForSize

u32 GetFreeNodeBucketForSize(Page &page, const Size2UI &size) const

Finds a bucket that stores nodes of the provided size.

AlignSize

Size2UI AlignSize(const Size2UI &size) const

Aligns the provided size to the alignment requested in layout settings.

PadSize

Size2UI PadSize(const Size2UI &size) const

Applies padding to the size as requested in layout settings.

Split

NodeSplitResult Split(const Node &nodeToSplit, const Size2UI &requiredSize) const

Splits a node and returns leftover rectangles, if there are any.

FindBestFreeNode

u32 FindBestFreeNode(Page &page, const Size2UI &size)

Finds the index of the best fitting node that can be the provided area.

Returns ~0u if no such node can be found.

RegisterFreeNode

void RegisterFreeNode(Page &page, u32 nodeId, const Size2UI &size)

Registers the node in the appropriate free node bucket.

AllocateNode

u32 AllocateNode()

Allocates a new mode in the nodes array and returns the allocated index.

This method will attempt to recycle unused nodes before allocating new nodes.

FreeNode

void FreeNode(u32 nodeId)

Marks the node as unused and frees it for re-allocation.

AllocatePage

Page AllocatePage()

Allocates a new atlas page.

FreePage

void FreePage(u32 pageId)

Frees a page.

Assumes the page contains only the root node.

MergeWithNextSibling

void MergeWithNextSibling(u32 nodeId)

Attempts to merge the next sibling of the provided node into the node, if the sibling exists and is free.

Fields

mSettings

mNodes

Vector<Node> mNodes

mPages

TInlineArray<Page, 2> mPages

mUnusedNodeListHead

u32 mUnusedNodeListHead

Index into mNodex of the first unused node, stored as a linked list. ~0u if no unused nodes.