editor_atlas_packer.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**************************************************************************/
  2. /* editor_atlas_packer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "editor_atlas_packer.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "core/math/vector2.h"
  33. #include "core/math/vector2i.h"
  34. void EditorAtlasPacker::chart_pack(Vector<Chart> &charts, int &r_width, int &r_height, int p_atlas_max_size, int p_cell_resolution) {
  35. int divide_by = MIN(64, p_cell_resolution);
  36. Vector<PlottedBitmap> bitmaps;
  37. int max_w = 0;
  38. for (int i = 0; i < charts.size(); i++) {
  39. const Chart &chart = charts[i];
  40. //generate aabb
  41. Rect2i aabb;
  42. int vertex_count = chart.vertices.size();
  43. const Vector2 *vertices = chart.vertices.ptr();
  44. for (int j = 0; j < vertex_count; j++) {
  45. if (j == 0) {
  46. aabb.position = vertices[j];
  47. } else {
  48. aabb.expand_to(vertices[j]);
  49. }
  50. }
  51. Ref<BitMap> src_bitmap;
  52. src_bitmap.instantiate();
  53. src_bitmap->create((aabb.size + Vector2(divide_by - 1, divide_by - 1)) / divide_by);
  54. int w = src_bitmap->get_size().width;
  55. int h = src_bitmap->get_size().height;
  56. //plot triangles, using divisor
  57. for (int j = 0; j < chart.faces.size(); j++) {
  58. Vector2i v[3];
  59. for (int k = 0; k < 3; k++) {
  60. Vector2 vtx = chart.vertices[chart.faces[j].vertex[k]];
  61. vtx -= aabb.position;
  62. vtx /= divide_by;
  63. vtx.x = MIN(vtx.x, w - 1);
  64. vtx.y = MIN(vtx.y, h - 1);
  65. v[k] = vtx;
  66. }
  67. for (int k = 0; k < 3; k++) {
  68. int l = k == 0 ? 2 : k - 1;
  69. Vector<Point2i> points = Geometry2D::bresenham_line(v[k], v[l]);
  70. for (Point2i point : points) {
  71. src_bitmap->set_bitv(point, true);
  72. }
  73. }
  74. }
  75. //src_bitmap->convert_to_image()->save_png("bitmap" + itos(i) + ".png");
  76. //grow by 1 for each side
  77. int bmw = src_bitmap->get_size().width + 2;
  78. int bmh = src_bitmap->get_size().height + 2;
  79. int heights_size = -1;
  80. bool transpose = false;
  81. if (chart.can_transpose && bmh > bmw) {
  82. heights_size = bmh;
  83. transpose = true;
  84. } else {
  85. heights_size = bmw;
  86. }
  87. max_w = MAX(max_w, heights_size);
  88. Vector<int> top_heights;
  89. Vector<int> bottom_heights;
  90. top_heights.resize(heights_size);
  91. bottom_heights.resize(heights_size);
  92. for (int x = 0; x < heights_size; x++) {
  93. top_heights.write[x] = -1;
  94. bottom_heights.write[x] = 0x7FFFFFFF;
  95. }
  96. for (int x = 0; x < bmw; x++) {
  97. for (int y = 0; y < bmh; y++) {
  98. bool found_pixel = false;
  99. for (int lx = x - 1; lx < x + 2 && !found_pixel; lx++) {
  100. for (int ly = y - 1; ly < y + 2 && !found_pixel; ly++) {
  101. int px = lx - 1;
  102. if (px < 0 || px >= w) {
  103. continue;
  104. }
  105. int py = ly - 1;
  106. if (py < 0 || py >= h) {
  107. continue;
  108. }
  109. if (src_bitmap->get_bit(px, py)) {
  110. found_pixel = true;
  111. }
  112. }
  113. }
  114. if (found_pixel) {
  115. if (transpose) {
  116. if (x > top_heights[y]) {
  117. top_heights.write[y] = x;
  118. }
  119. if (x < bottom_heights[y]) {
  120. bottom_heights.write[y] = x;
  121. }
  122. } else {
  123. if (y > top_heights[x]) {
  124. top_heights.write[x] = y;
  125. }
  126. if (y < bottom_heights[x]) {
  127. bottom_heights.write[x] = y;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. String row;
  134. for (int j = 0; j < top_heights.size(); j++) {
  135. row += "(" + itos(top_heights[j]) + "-" + itos(bottom_heights[j]) + "),";
  136. }
  137. PlottedBitmap plotted_bitmap;
  138. plotted_bitmap.offset = aabb.position;
  139. plotted_bitmap.top_heights = top_heights;
  140. plotted_bitmap.bottom_heights = bottom_heights;
  141. plotted_bitmap.chart_index = i;
  142. plotted_bitmap.transposed = transpose;
  143. plotted_bitmap.area = bmw * bmh;
  144. bitmaps.push_back(plotted_bitmap);
  145. }
  146. bitmaps.sort();
  147. int atlas_max_width = nearest_power_of_2_templated(p_atlas_max_size) / divide_by;
  148. int atlas_w = nearest_power_of_2_templated(max_w);
  149. int atlas_h;
  150. while (true) {
  151. atlas_h = 0;
  152. //do a tetris
  153. Vector<int> heights;
  154. heights.resize(atlas_w);
  155. for (int i = 0; i < atlas_w; i++) {
  156. heights.write[i] = 0;
  157. }
  158. int *atlas_ptr = heights.ptrw();
  159. for (int i = 0; i < bitmaps.size(); i++) {
  160. int best_height = 0x7FFFFFFF;
  161. int best_height_offset = -1;
  162. int w = bitmaps[i].top_heights.size();
  163. const int *top_heights = bitmaps[i].top_heights.ptr();
  164. const int *bottom_heights = bitmaps[i].bottom_heights.ptr();
  165. for (int j = 0; j <= atlas_w - w; j++) {
  166. int height = 0;
  167. for (int k = 0; k < w; k++) {
  168. int pixmap_h = bottom_heights[k];
  169. if (pixmap_h == 0x7FFFFFFF) {
  170. continue; //no pixel here, anything is fine
  171. }
  172. int h = MAX(0, atlas_ptr[j + k] - pixmap_h);
  173. if (h > height) {
  174. height = h;
  175. }
  176. }
  177. if (height < best_height) {
  178. best_height = height;
  179. best_height_offset = j;
  180. }
  181. }
  182. for (int j = 0; j < w; j++) { //add
  183. if (top_heights[j] == -1) { //unused
  184. continue;
  185. }
  186. int height = best_height + top_heights[j] + 1;
  187. atlas_ptr[j + best_height_offset] = height;
  188. atlas_h = MAX(atlas_h, height);
  189. }
  190. // set
  191. Vector2 offset = bitmaps[i].offset;
  192. if (bitmaps[i].transposed) {
  193. SWAP(offset.x, offset.y);
  194. }
  195. Vector2 final_pos = Vector2(best_height_offset * divide_by, best_height * divide_by) + Vector2(divide_by, divide_by) - offset;
  196. charts.write[bitmaps[i].chart_index].final_offset = final_pos;
  197. charts.write[bitmaps[i].chart_index].transposed = bitmaps[i].transposed;
  198. }
  199. if (atlas_h <= atlas_w * 2 || atlas_w >= atlas_max_width) {
  200. break; //ok this one is enough
  201. }
  202. //try again
  203. atlas_w *= 2;
  204. }
  205. r_width = atlas_w * divide_by;
  206. r_height = atlas_h * divide_by;
  207. }