Texture Atlas Packing
A texture atlas combines many separate images (UI icons, props, sprites from different characters) into one texture. Sprites that share a texture and material can be drawn in one batch, and the GPU switches textures less often. Atlases are built by a packer: an algorithm that places rectangles of different sizes into one bigger rectangle with as little wasted space as possible.
Engines include packers (Unity's Sprite Atlas, Godot's AtlasTexture and the 2D atlas import mode, Unreal's Paper2D sprite sheets) and standalone tools exist (TexturePacker, free-tex-packer). Knowing how they work helps you choose settings and understand why an atlas came out 2048 wide when you expected 1024.
The shelf algorithm
The simplest good packer is the shelf (or skyline-lite) algorithm:
- Sort the images by height, tallest first.
- Place them left to right on a "shelf" as high as its first image.
- When the next image doesn't fit the width, start a new shelf below.
- If the shelves overflow the height, double the atlas size and start again.
Real packers (MaxRects, Skyline, Guillotine) fill the gaps above shorter images too, and also try rotating images; they usually waste noticeably less space, especially when image heights vary a lot. The shelf version is short enough to read in one go:
def pack(images, width, height, padding=2):
"""Shelf packing. images: {name: (w, h)} -> {name: (x, y, w, h)} or None if it doesn't fit."""
placed = {}
x = y = shelf_h = padding
for name, (w, h) in sorted(images.items(), key=lambda kv: (-kv[1][1], -kv[1][0], kv[0])):
if x + w + padding > width: # start a new shelf
x, y = padding, y + shelf_h + padding
shelf_h = 0
if y + h + padding > height or w + 2 * padding > width:
return None
placed[name] = (x, y, w, h)
x += w + padding
shelf_h = max(shelf_h, h)
return placed
def pack_pow2(images, padding=2, start=32, limit=4096):
size = start
while size <= limit:
for w, h in [(size, size // 2), (size, size)]: # try a half-height atlas first
result = pack(images, w, h, padding)
if result:
return w, h, result
size *= 2
raise ValueError("does not fit in the maximum atlas size")
icons = {"sword": (16, 24), "shield": (24, 24), "potion": (12, 18), "coin": (10, 10),
"key": (14, 8), "gem": (12, 12), "bow": (14, 24), "map": (24, 16), "skull": (16, 16)}
w, h, rects = pack_pow2(icons)
used = sum(rw * rh for _, _, rw, rh in rects.values())
print(f"atlas {w}x{h}, {len(rects)} images, {used / (w * h):.0%} of the area used")
for name, (x, y, rw, rh) in sorted(rects.items(), key=lambda kv: (kv[1][1], kv[1][0])):
print(f" {name:<7} at ({x:2},{y:2}) size {rw}x{rh}")atlas 64x64, 9 images, 61% of the area used
shield at ( 2, 2) size 24x24
sword at (28, 2) size 16x24
bow at (46, 2) size 14x24
potion at ( 2,28) size 12x18
map at (16,28) size 24x16
skull at (42,28) size 16x16
gem at ( 2,48) size 12x12
coin at (16,48) size 10x10
key at (28,48) size 14x8
Seeing the result
A packer is easier to judge when you can see it. This version draws the same atlas as characters, one character per 2×2 pixels, each image as a letter:
def pack(images, width, height, padding=2):
placed = {}
x = y = shelf_h = padding
for name, (w, h) in sorted(images.items(), key=lambda kv: (-kv[1][1], -kv[1][0], kv[0])):
if x + w + padding > width:
x, y = padding, y + shelf_h + padding
shelf_h = 0
if y + h + padding > height or w + 2 * padding > width:
return None
placed[name] = (x, y, w, h)
x += w + padding
shelf_h = max(shelf_h, h)
return placed
icons = {"sword": (16, 24), "shield": (24, 24), "potion": (12, 18), "coin": (10, 10),
"key": (14, 8), "gem": (12, 12), "bow": (14, 24), "map": (24, 16), "skull": (16, 16)}
W, H = 64, 64
rects = pack(icons, W, H)
grid = [["." for _ in range(W // 2)] for _ in range(H // 2)]
for letter, (name, (x, y, w, h)) in zip("ABCDEFGHI", sorted(rects.items())):
for gy in range(y // 2, (y + h + 1) // 2):
for gx in range(x // 2, (x + w + 1) // 2):
grid[gy][gx] = letter
print(f"{letter} = {name}", end=" ")
print()
print("\n".join("".join(row) for row in grid))A = bow B = coin C = gem D = key E = map F = potion G = shield H = skull I = sword
................................
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
.GGGGGGGGGGGG.IIIIIIII.AAAAAAA..
................................
.FFFFFF.EEEEEEEEEEEE.HHHHHHHH...
.FFFFFF.EEEEEEEEEEEE.HHHHHHHH...
.FFFFFF.EEEEEEEEEEEE.HHHHHHHH...
.FFFFFF.EEEEEEEEEEEE.HHHHHHHH...
.FFFFFF.EEEEEEEEEEEE.HHHHHHHH...
.FFFFFF.EEEEEEEEEEEE.HHHHHHHH...
.FFFFFF.EEEEEEEEEEEE.HHHHHHHH...
.FFFFFF.EEEEEEEEEEEE.HHHHHHHH...
.FFFFFF.........................
................................
.CCCCCC.BBBBB.DDDDDDD...........
.CCCCCC.BBBBB.DDDDDDD...........
.CCCCCC.BBBBB.DDDDDDD...........
.CCCCCC.BBBBB.DDDDDDD...........
.CCCCCC.BBBBB...................
.CCCCCC.........................
................................
................................
The dots are wasted texture memory. You can see why shelf packing wastes space: each shelf is as tall as its tallest image, leaving gaps above shorter neighbours.
Settings that matter
| Setting | Effect | Typical choice |
|---|---|---|
| Max size | upper bound per atlas page; more pages if exceeded | 2048 (mobile) to 4096 |
| Power of two | some platforms and compression formats need it | on, unless the engine handles NPOT |
| Padding | prevents bleeding between neighbours under filtering | 2 px (more if mipmapped) |
| Extrude | repeats edge pixels into the padding | 1 px for tiles and tight sprites |
| Allow rotation | packs tighter; the engine must rotate UVs back | only if the engine supports it |
| Trim | removes transparent borders; stores the offset | on for characters, off for tiles |
Grouping images into atlases
Packing efficiency matters less than what goes together. Put images that appear on screen together in the same atlas (all UI of the HUD, all props of a level). An atlas that mixes level-1 and level-9 art forces both to stay in memory. Unity's Sprite Atlas, Godot's import groups and TexturePacker's per-folder projects all support this.
Test yourself
What is the main runtime benefit of a texture atlas?
- Smaller PNG files on disk
- Sprites sharing one texture can be batched into fewer draw calls
- Higher resolution
- Automatic animation
Why does shelf packing waste space?
- It never rotates
- Each shelf is as tall as its tallest image, so shorter images leave gaps above them
- It requires power-of-two images
- It sorts by name
Which images should share an atlas?
- Alphabetically adjacent ones
- Images that are on screen at the same time
- Images of the same size
- All images in the game