create_atlas.script 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. local function create_texture(width, height)
  2. -- create a new rgba texture
  3. local create_texture_params = {
  4. width = width,
  5. height = height,
  6. type = resource.TEXTURE_TYPE_2D,
  7. format = resource.TEXTURE_FORMAT_RGBA,
  8. }
  9. local my_texture_id = resource.create_texture("/my_custom_texture.texturec", create_texture_params)
  10. -- create a buffer with pixel data
  11. local buf = buffer.create(width * height, { { name=hash("rgba"), type=buffer.VALUE_TYPE_UINT8, count=4 } } )
  12. local stream = buffer.get_stream(buf, hash("rgba"))
  13. local half_width = width / 2
  14. for y=1, height do
  15. for x=1, width do
  16. local index = (y-1) * width * 4 + (x-1) * 4 + 1
  17. stream[index + 0] = x > half_width and 0xFF or 0x00
  18. stream[index + 1] = x > half_width and 0x00 or 0xFF
  19. stream[index + 2] = x > half_width and 0x00 or 0x00
  20. stream[index + 3] = 0xFF
  21. end
  22. end
  23. -- set the pixels on the texture
  24. local set_texture_params = { width=width, height=height, x=0, y=0, type=resource.TEXTURE_TYPE_2D, format=resource.TEXTURE_FORMAT_RGBA, num_mip_maps=1 }
  25. resource.set_texture(my_texture_id, set_texture_params, buf)
  26. return my_texture_id
  27. end
  28. local function create_atlas(texture_id, width, height)
  29. local params = {
  30. texture = texture_id,
  31. animations = {
  32. {
  33. id = "my_animation_left",
  34. width = width / 2,
  35. height = height,
  36. frame_start = 1,
  37. frame_end = 2,
  38. },
  39. {
  40. id = "my_animation_right",
  41. width = width / 2,
  42. height = height,
  43. frame_start = 2,
  44. frame_end = 3,
  45. }
  46. },
  47. geometries = {
  48. {
  49. width = width / 2,
  50. height = height,
  51. pivot_x = 0.5,
  52. pivot_y = 0.5,
  53. vertices = {
  54. 0, height,
  55. 0, 0,
  56. width / 2, 0,
  57. width / 2, height
  58. },
  59. uvs = {
  60. 0, height,
  61. 0, 0,
  62. width / 2, 0,
  63. width / 2, height
  64. },
  65. indices = {0,1,2,0,2,3}
  66. },
  67. {
  68. width = width / 2,
  69. height = height,
  70. pivot_x = 0.5,
  71. pivot_y = 0.5,
  72. vertices = {
  73. 0, height,
  74. 0, 0,
  75. width / 2, 0,
  76. width / 2, height
  77. },
  78. uvs = {
  79. width / 2, height,
  80. width / 2, 0,
  81. width, 0,
  82. width, height
  83. },
  84. indices = {0,1,2,0,2,3}
  85. }
  86. }
  87. }
  88. local my_atlas_id = resource.create_atlas("/my_atlas.texturesetc", params)
  89. return my_atlas_id
  90. end
  91. function init(self)
  92. local width = 128
  93. local height = 128
  94. local my_texture_id = create_texture(width, height)
  95. local my_atlas_id = create_atlas(my_texture_id, width, height)
  96. -- set the new atlas on the sprite and show one image
  97. go.set("#sprite", "image", my_atlas_id)
  98. sprite.play_flipbook("#sprite", "my_animation_left")
  99. -- set the new atlas on the gui component and use on a node from the gui script
  100. go.set("gui#gui", "textures", my_atlas_id, { key = "my_atlas" })
  101. msg.post("gui#gui", "use_atlas", { texture = "my_atlas", animation = "my_animation_right" })
  102. end