Yes, I think that should be possible now.
It would look something like this:
local img = go.get("#sprite", "image")
-- get info about the atlas that is used
-- it shouldn't matter if this is a tilesource or an atlas
-- needs to be tested though...
-- https://defold.com/ref/resource/#resource.get_atlas:path
local atlas = resource.get_atlas(img)
print("texture path", atlas.texture)
pprint("animations", atlas.animations)
pprint("geometries", atlas.geometries)
-- get information about the texture that is used
-- https://defold.com/ref/resource/#resource.get_texture_info:path
local texture = resource.get_texture_info(atlas.texture)
print("texture w,h", texture.width, texture.height)
-- update the texture with new pixel data
-- https://defold.com/ref/resource/#resource.set_texture:path-table-buffer
local info = {
type = texture.type,
width = texture.width,
height = texture.height,
format = resource.TEXTURE_FORMAT_RGB, -- not sure why this isn't in the texture info table...
x = 32, -- where to draw
y = 64,
compression_type = resource.COMPRESSION_TYPE_DEFAULT
}
-- create a buffer with the pixels
-- https://defold.com/ref/stable/buffer/#buffer.create:element_count-declaration
local buf = buffer.create()
resource.set_texture(atlas.texture, info, buf)
I have some questions of my own. @JCash or @jhonny.goransson should be able to answer:
- The result from resource.get_texture_info() provides a lot of useful info when you wish to later update the texture using resource.set_texture(), but it seems like we do not provide:
- Compression type of the texture
- Format of the texture
- The above flow works for updating a texture with new pixels. What if I want to read the existing pixels first? Do we have a way to get the pixel data? Preferably uncompressed if the data is compressed, but that is probably asking too much. Might be better to not compress a texture you wish to read from.
I think the above flow is a good candidate for a utility module that could be shared with the community.