# What is the point of Geometry2D.make_atlas()?

I'm completely bewildered by the Geometry2D.make_atlas() method. I believe it has to do with texture atlases based on its name and comment on its source code but am unable to parse exactly how it's supposed to work and what it's supposed to do.

For example, this:

func _ready():
print(Geometry2D.make_atlas([
Vector2(1.0, 1.0),
Vector2(20.0, 1.0),
Vector2(1.0, 20.0),
Vector2(20.0, 20.0)
]))


Produces this:

{ "points": [(20, 0), (0, 0), (20, 1), (0, 1)], "size": (21, 21) }


And it's not clear to me how the points given in my initial array are translated into the ones in the resulting "points" portion of the atlas nor how this would be useful.

• Commented Apr 21, 2023 at 21:03

It provides the layout for a texture atlas. You give it an ordered list of tiles sizes and the algorithm tries to rearrange those tiles into the smallest square that it can. It gives you the size of the atlas and the locations of the tiles (based on the original index order) within the atlas.

In your example, you're giving it four tiles. The resulting atlas that it finds to holds them all is of size 21 x 21.

• The first tile you provided (1x1) is packed such that its starting corner is at position (20,0) within that 21x21 atlas.
• The second tile you provided (20x1) is packed such that its starting corner is at position (0,0) within that 21x21 atlas.
• The third tile you provided (1x20) is packed such that its starting corner is at position (20,1) within that 21x21 atlas.
• The fourth tile you provided (20x20) is packed such that its starting corner is at position (0,1) within that 21x21 atlas.

If we assume a layout where 0,0 is the upper left hand corner, then the layout would look like this:

Note: if you prefer to assume a different coordinate plane layout, just flip the result as necessary to fit that mental model.

As mentioned in the implementation comments, the function attempts to pack the tiles into a square to increase the likelihood that the result will comply with video card memory constraints.

The use case for this function is that it saves you the trouble of determining your own texture atlas layout. You could do this on your own, either by hand or with your own algorithm, but the framework is giving you a prebuilt solution so that you don't have to. The one provided by Godot is brute force - there's probably a faster implementation. But it would be one more thing to code, test, debug, and maintain. And relative to the overall performance of your game, optimizing texture atlas construction might not be worth the tradeoff.