Some context
I am working on a voxel game (like minecraft). I can not use a prebuilt texture atlas as I need to allow players to add custom blocks to the game. That's why I must create texture atlases at the runtime based on the textures provided. These textures need not be same in size (one can be 16 by 16 while other can be 64 by 64) so I can't just paste the images at a regular distance.
Actual question
How can I combine multiple images which can be different sizes into one while cramming as much images I can, i.e., images should be as densely packed as possible and if an image can be placed at a smaller place on the final image, it should be placed there as to not use the space that may be used by other images. The final image must be a square and its size must be the smallest power of 2. For example if there are 4 16 by 16 images, only a 32 by 32 image should be created as its enough to contain those images. But say that there were 5 images, it should create a 64 by 64 image as its the smallest size that can contain those images. I would also need the UV values of the textures that I can map to blockfaces.
Game engine I use
I use godot game engine.
Inputs
Let us say you have a series of
Image
s you want to put into your texture atlas. Let us say it is an array:And the values of that array can be either:
Image
.get_data
onTexture
s.Image
s created at runtime by other means.Compiling the sizes
Now, we can get their sizes. We are going to need them in a
PoolVector2Array
(we can actually get them in a regularArray
and convert it, but there is no need to do that):Computing the atlas
Once we have the sizes, we can call
Geometry.make_atlas
:The algorithm of
Geometry.make_atlas
will try to make the atlas square.We should query what size we got:
Sadly Godot does not expose a "next power of 2" function. Given the context, I believe a simple loop will do well on this case:
Building the atlas
Create an atlas
Image
Now that we have decided the size, we can create a new
Image
for the atlas:As you might suspect the first two parameters are
width
andheight
. The third parameter is whether or not you want to generate mipmaps. And the last is the pixel format, the one I'm picking here has four channels (Red, Green, Blue, Alpha) of 8 bits (color depth) each.Copy the input
Image
s to the atlasImage
Now copy your original images to the atlas. We get the position for each one on the result we got from
make_atlas
:Here the
Rect2
is the area of theImage
we want to draw (all of it).Note depending on the format of the
Image
you want to draw, you may have to convert it first:Also, usually, the images are imported in a compressed format that cannot be converted directly, and thus you would have to decompress first:
If the
blit_rect
line is giving you problems, try that.UV Coordinates
By the way, you say you want UV coordinates, we should get them from the same loop:
Converting to
Texture
And since you want UV coordinates, I presume this is going to be a
Texture
for some shader. So, let us make aTexture
:Then you should be able to set it where you need it. I leave that to you.