modify_atlas.script 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. -- load image from custom resources
  2. -- read pixels and write them to a buffer
  3. local function create_buffer_from_image(filename)
  4. local png = assert(sys.load_resource(filename))
  5. local loaded_image = image.load(png)
  6. local width = loaded_image.width
  7. local height = loaded_image.height
  8. local pixels = loaded_image.buffer
  9. local buffer_declaration = {
  10. {
  11. name = hash("rgba"),
  12. type = buffer.VALUE_TYPE_UINT8,
  13. count = 4
  14. }
  15. }
  16. local pixel_buffer = buffer.create(width * height, buffer_declaration)
  17. local pixel_stream = buffer.get_stream(pixel_buffer, hash("rgba"))
  18. for y = 1, height do
  19. for x = 1, width do
  20. -- flip image
  21. local pixels_index = ((height - y) * width * 4) + ((x - 1) * 4) + 1
  22. local r = pixels:byte(pixels_index + 0)
  23. local g = pixels:byte(pixels_index + 1)
  24. local b = pixels:byte(pixels_index + 2)
  25. local a = pixels:byte(pixels_index + 3)
  26. -- write to buffer stream
  27. local stream_index = ((y - 1) * width * 4) + ((x - 1) * 4) + 1
  28. pixel_stream[stream_index + 0] = r
  29. pixel_stream[stream_index + 1] = g
  30. pixel_stream[stream_index + 2] = b
  31. pixel_stream[stream_index + 3] = a
  32. end
  33. end
  34. return pixel_buffer, width, height
  35. end
  36. local function replace_atlas_image()
  37. -- get table with information about an atlas
  38. local atlas = resource.get_atlas("/example/modify_atlas.a.texturesetc")
  39. -- get table with information about the textured used by the atlas
  40. local texture = resource.get_texture_info(atlas.texture)
  41. pprint(atlas)
  42. pprint(texture)
  43. -- load an image as a Defold buffer
  44. local pixel_buffer, width, height = create_buffer_from_image("/example/resources/shipYellow_manned.png")
  45. -- get the UV coordinates of the first image in the atlas
  46. local first_uvs = atlas.geometries[1].uvs
  47. -- this offset should not be necessary but it seems like there is an issue with the
  48. -- UVs in Defold 1.5.0
  49. local x = first_uvs[1] - 0
  50. local y = first_uvs[2] - 6
  51. print(x, y)
  52. print(width, height)
  53. -- create a table with texture update information
  54. -- we want to update only a sub region of the atlas starting at a
  55. -- certain position and with a certain size
  56. local texture_info = {
  57. type = resource.TEXTURE_TYPE_2D,
  58. width = width,
  59. height = height,
  60. format = resource.TEXTURE_FORMAT_RGBA,
  61. x = x,
  62. y = y,
  63. compression_type = resource.COMPRESSION_TYPE_DEFAULT,
  64. num_mip_maps = texture.mipmaps,
  65. }
  66. -- update the atlas texture with the pixels from the provided buffer
  67. resource.set_texture(atlas.texture, texture_info, pixel_buffer)
  68. end
  69. function init(self)
  70. msg.post(".", "acquire_input_focus")
  71. end
  72. function on_input(self, action_id, action)
  73. if action_id == hash("mouse_button_left") and action.pressed then
  74. replace_atlas_image()
  75. end
  76. end