Convert texture to buffer

I have an image in the atlas.
Is it possible to somehow convert it to a buffer or convert all texture to buffer?
I try this:

 local res_path = go.get("#sprite", "texture0")
 local buf = resource.get_buffer(res_path)

But get this error:

It is currently not possible to read the pixel data of a texture. You can create a new buffer and set that on a sprite. We have discussed it and it’s a feature we want to add though.

Some things to note:

  • The data you get might be compressed (ETC1 on Android for instance). In such a case you must make sure to not compress a texture that you want to manipulate this way.
  • We also want to provide some data about an atlas, so that you know the UVs of the individual images and can update a specific image in the texture.

@britzl thanks for the answer. It would be really great if I could read data from the texture.
I would also like to be able to create an atlas for new images.(For example downloaded from the Internet). By creating one atlas it would be possible to reduce the number of drawcalls.

I found a small life hack how can I upload a single image to the buffer. Using the imageloader, I can load the image into the buffer. Then, using the drawpixels, you can get the desired pixel (Or you can work with the buffer yourself).

local function load_image(path)
  local data = sys.load_resource(path)
  local resource = imageloader.load({data = data, channels = 4, no_vertical_flip = true})
  local buffer_info = {
    buffer = resource.buffer,
    width = resource.header.width,
    height = resource.header.height,
    channels = resource.header.channels,
    premultiply_alpha = false
  }
  return buffer_info
end

local function get_pixel(resource, x, y)
  local r, g, b, a = drawpixels.color(resource, 0, 0)
  return {r = r, g = g, b = b, a = a}
end

Maybe someone will come in handy.

1 Like

Yep, we have this in our backlog. Me and @JCash talked about this recently.

4 Likes

does this still true till this day?

Yes