Packing an atlas at runtime

In 1.4.2 we got a new functionality with resource.create_atlas using that in conjunction with resource.create_texture and resource.set_texture we can now create atlases at runtime.

It is a powerful feature that enables you to use files on disk (such as downloaded images) and set them into an atlas and then use these images in your collections (but not GUI, more on that later). This is very useful if you download 100s of images and want to show them at the same time. As if you did that you would most likely run out of resources.

Over the last couple of days I have been trying this out and the outcome of this is this repository. It serves as both a example and a naive runtime texture packing solution.

Usage

It’s fairly straightforward to use the packing of the library. You only need to supply a name of the runtime atlas as well as the images you want to pack, and of course width and height.

local atlas_entries = {
	"example/assets/images/image0.png", "example/assets/images/image1.png"
}

local atlas_id = rtatlas.pack("example", atlas_entries, 256, 256)

go.set("#sprite", "image", atlas_id)
sprite.play_flipbook("#sprite", "image0.png")

I have to admit that I struggled with figuring this feature out as there was a lot of things about packing atlases I wasn’t aware of, I would recommend to you to look into the code of runtime-atlas/runtime-atlas.lua if you struggle with it too, I have added a lot of comments to the code to help you understand it more.

17 Likes

Thank you for this! It’s definately helping the community to fill some knowledge gaps :slight_smile:

4 Likes

Just as an fyi I think I’ve found a solution to the gui problem, so it will probably be included in the next beta

5 Likes

There’s a PR for a fix up now. Here’s an example with creating an atlas in a script and then using it in a GUI:

6 Likes

That’s so awesome! Thank you for the super fix :grin:

2 Likes