I am trying to make a dynamic atlas from images on disk but failing horribly, I have been asking about this on the Discord but I think it’s time to make a forum post about it as I am clearly not smart enough to figure this out!
I start out with collecting the width
and height
of my images when loading it and saving that in a table also name and the actual buffer.
I then iterate over that and do my thing.
-- this returns our table with all our needed data.
local pack_data = get_package_data()
local atlas_creation_params = {
width = 1024, -- desired width and height of our atlas
height = 1024,
type = resource.TEXTURE_TYPE_2D,
format = resource.TEXTURE_FORMAT_RGBA,
}
local texture_id = resource.create_texture("my_atlas_name.texturec", atlas_creation_params)
-- Here we will store our data that we need when setting up the atlas data
local atlas_params = {texture="my_atlas_name.texturec", animations={}, geometries={}}
for i in pairs(pack_data) do
local img = pack_data[i]
local set_params = {width=img.w, height=img.h, x=img.w, y= img.y, type=resource.TEXTURE_TYPE_2D, format=resource.TEXTURE_FORMAT_RGBA}
resource.set_texture(texture_id, set_params, img.buffer)
-- We add this image data to our atlas parameters
atlas_params = get_atlas_parameters(img, atlas_params)
end
-- Finally we create the atlas set
resource.create_atlas("my_atlas_name.texturesetc", atlas_params)
I have tried a lot o things for the atlas parameters
local function get_atlas_parameters(image, atlas_params)
-- Do we need to increment the indices depending on how many images we got?
local indices = {0,1,2,0,2,3}
table.insert(atlas_params.animations, {
id = image.name,
width = image.w,
height = image.h,
frame_start = 1,
frame_end = 2,
})
table.insert(atlas_params.geometries, {
-- Have also tried to have these be "local" in all combinations
vertices = {
0, 0,
0, image.h,
image.w, image.h,
image.w, 0
},
uvs = {
image.x, image.y,
image.x, image.y + image.h,
image.x + image.w, image.y + image.h,
image.x + image.w, image.y
},
indices = indices
})
return atlas_params
end
I calculate the x and y in a different location but it’s just putting them on a row.
This doesn’t work for me, I know that the textures are loaded fine because if I only load one image and set the atlas_creation_params
width
and height
to be the same as the image it shows. But if i increase them to a bigger value the image will eventually disappear, making me believe that the atlas lookup and position on the atlas are not correct but I can’t figure out why.
Can anyone see something obvious that’s wrong here?