editor_atlas_packer.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include "editor_atlas_packer.h"
  2. void EditorAtlasPacker::_plot_triangle(Ref<BitMap> p_bitmap, Vector2i *vertices) {
  3. int width = p_bitmap->get_size().width;
  4. int height = p_bitmap->get_size().height;
  5. int x[3];
  6. int y[3];
  7. for (int j = 0; j < 3; j++) {
  8. x[j] = vertices[j].x;
  9. y[j] = vertices[j].y;
  10. }
  11. // sort the points vertically
  12. if (y[1] > y[2]) {
  13. SWAP(x[1], x[2]);
  14. SWAP(y[1], y[2]);
  15. }
  16. if (y[0] > y[1]) {
  17. SWAP(x[0], x[1]);
  18. SWAP(y[0], y[1]);
  19. }
  20. if (y[1] > y[2]) {
  21. SWAP(x[1], x[2]);
  22. SWAP(y[1], y[2]);
  23. }
  24. double dx_far = double(x[2] - x[0]) / (y[2] - y[0] + 1);
  25. double dx_upper = double(x[1] - x[0]) / (y[1] - y[0] + 1);
  26. double dx_low = double(x[2] - x[1]) / (y[2] - y[1] + 1);
  27. double xf = x[0];
  28. double xt = x[0] + dx_upper; // if y[0] == y[1], special case
  29. for (int yi = y[0]; yi <= (y[2] > height - 1 ? height - 1 : y[2]); yi++) {
  30. if (yi >= 0) {
  31. for (int xi = (xf > 0 ? int(xf) : 0); xi <= (xt < width ? xt : width - 1); xi++) {
  32. //pixels[int(x + y * width)] = color;
  33. p_bitmap->set_bit(Point2(xi, yi), true);
  34. }
  35. for (int xi = (xf < width ? int(xf) : width - 1); xi >= (xt > 0 ? xt : 0); xi--) {
  36. p_bitmap->set_bit(Point2(xi, yi), true);
  37. }
  38. }
  39. xf += dx_far;
  40. if (yi < y[1])
  41. xt += dx_upper;
  42. else
  43. xt += dx_low;
  44. }
  45. }
  46. void EditorAtlasPacker::chart_pack(Vector<Chart> &charts, int &r_width, int &r_height, int p_atlas_max_size, int p_cell_resolution) {
  47. int divide_by = MIN(64, p_cell_resolution);
  48. Vector<PlottedBitmap> bitmaps;
  49. int max_w = 0;
  50. for (int i = 0; i < charts.size(); i++) {
  51. const Chart &chart = charts[i];
  52. //generate aabb
  53. Rect2i aabb;
  54. int vertex_count = chart.vertices.size();
  55. const Vector2 *vertices = chart.vertices.ptr();
  56. for (int j = 0; j < vertex_count; j++) {
  57. if (j == 0) {
  58. aabb.position = vertices[j];
  59. } else {
  60. aabb.expand_to(vertices[j]);
  61. }
  62. }
  63. Ref<BitMap> src_bitmap;
  64. src_bitmap.instance();
  65. src_bitmap->create(aabb.size / divide_by);
  66. int w = src_bitmap->get_size().width;
  67. int h = src_bitmap->get_size().height;
  68. //plot triangles, using divisor
  69. for (int j = 0; j < chart.faces.size(); j++) {
  70. Vector2i v[3];
  71. for (int k = 0; k < 3; k++) {
  72. Vector2 vtx = chart.vertices[chart.faces[j].vertex[k]];
  73. vtx -= aabb.position;
  74. vtx /= divide_by;
  75. v[k] = vtx;
  76. }
  77. _plot_triangle(src_bitmap, v);
  78. }
  79. //src_bitmap->convert_to_image()->save_png("bitmap" + itos(i) + ".png");
  80. //grow by 1 for each side
  81. int bmw = src_bitmap->get_size().width + 2;
  82. int bmh = src_bitmap->get_size().height + 2;
  83. int heights_size = -1;
  84. bool transpose = false;
  85. if (chart.can_transpose && bmh > bmw) {
  86. heights_size = bmh;
  87. transpose = true;
  88. } else {
  89. heights_size = bmw;
  90. }
  91. max_w = MAX(max_w, heights_size);
  92. Vector<int> top_heights;
  93. Vector<int> bottom_heights;
  94. top_heights.resize(heights_size);
  95. bottom_heights.resize(heights_size);
  96. for (int x = 0; x < heights_size; x++) {
  97. top_heights.write[x] = -1;
  98. bottom_heights.write[x] = 0x7FFFFFFF;
  99. }
  100. for (int x = 0; x < bmw; x++) {
  101. for (int y = 0; y < bmh; y++) {
  102. bool found_pixel = false;
  103. for (int lx = x - 1; lx < x + 2 && !found_pixel; lx++) {
  104. for (int ly = y - 1; ly < y + 2 && !found_pixel; ly++) {
  105. int px = lx - 1;
  106. if (px < 0 || px >= w)
  107. continue;
  108. int py = ly - 1;
  109. if (py < 0 || py >= h)
  110. continue;
  111. if (src_bitmap->get_bit(Vector2(px, py))) {
  112. found_pixel = true;
  113. }
  114. }
  115. }
  116. if (found_pixel) {
  117. if (transpose) {
  118. if (x > top_heights[y]) {
  119. top_heights.write[y] = x;
  120. }
  121. if (x < bottom_heights[y]) {
  122. bottom_heights.write[y] = x;
  123. }
  124. } else {
  125. if (y > top_heights[x]) {
  126. top_heights.write[x] = y;
  127. }
  128. if (y < bottom_heights[x]) {
  129. bottom_heights.write[x] = y;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. String row;
  136. for (int j = 0; j < top_heights.size(); j++) {
  137. row += "(" + itos(top_heights[j]) + "-" + itos(bottom_heights[j]) + "),";
  138. }
  139. PlottedBitmap plotted_bitmap;
  140. plotted_bitmap.offset = aabb.position;
  141. plotted_bitmap.top_heights = top_heights;
  142. plotted_bitmap.bottom_heights = bottom_heights;
  143. plotted_bitmap.chart_index = i;
  144. plotted_bitmap.transposed = transpose;
  145. plotted_bitmap.area = bmw * bmh;
  146. bitmaps.push_back(plotted_bitmap);
  147. }
  148. bitmaps.sort();
  149. int atlas_max_width = nearest_power_of_2_templated(p_atlas_max_size) / divide_by;
  150. int atlas_w = nearest_power_of_2_templated(max_w);
  151. int atlas_h;
  152. while (true) {
  153. atlas_h = 0;
  154. //do a tetris
  155. Vector<int> heights;
  156. heights.resize(atlas_w);
  157. for (int i = 0; i < atlas_w; i++) {
  158. heights.write[i] = 0;
  159. }
  160. int *atlas_ptr = heights.ptrw();
  161. for (int i = 0; i < bitmaps.size(); i++) {
  162. int best_height = 0x7FFFFFFF;
  163. int best_height_offset = -1;
  164. int w = bitmaps[i].top_heights.size();
  165. const int *top_heights = bitmaps[i].top_heights.ptr();
  166. const int *bottom_heights = bitmaps[i].bottom_heights.ptr();
  167. for (int j = 0; j < atlas_w - w; j++) {
  168. int height = 0;
  169. for (int k = 0; k < w; k++) {
  170. int pixmap_h = bottom_heights[k];
  171. if (pixmap_h == -1) {
  172. continue; //no pixel here, anything is fine
  173. }
  174. int h = MAX(0, atlas_ptr[j + k] - pixmap_h);
  175. if (h > height) {
  176. height = h;
  177. }
  178. }
  179. if (height < best_height) {
  180. best_height = height;
  181. best_height_offset = j;
  182. }
  183. }
  184. for (int j = 0; j < w; j++) { //add
  185. if (top_heights[j] == -1) { //unused
  186. continue;
  187. }
  188. int height = best_height + top_heights[j] + 1;
  189. atlas_ptr[j + best_height_offset] = height;
  190. atlas_h = MAX(atlas_h, height);
  191. }
  192. // set
  193. Vector2 offset = bitmaps[i].offset;
  194. if (bitmaps[i].transposed) {
  195. SWAP(offset.x, offset.y);
  196. }
  197. Vector2 final_pos = Vector2(best_height_offset * divide_by, best_height * divide_by) + Vector2(divide_by, divide_by) - offset;
  198. charts.write[bitmaps[i].chart_index].final_offset = final_pos;
  199. charts.write[bitmaps[i].chart_index].transposed = bitmaps[i].transposed;
  200. }
  201. if (atlas_h <= atlas_w * 2 || atlas_w >= atlas_max_width) {
  202. break; //ok this one is enough
  203. }
  204. //try again
  205. atlas_w *= 2;
  206. }
  207. r_width = atlas_w * divide_by;
  208. r_height = atlas_h * divide_by;
  209. }