editor_atlas_packer.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 = vtx.min(Vector2(w - 1, h - 1));
  64. v[k] = vtx;
  65. }
  66. for (int k = 0; k < 3; k++) {
  67. int l = k == 0 ? 2 : k - 1;
  68. Vector<Point2i> points = Geometry2D::bresenham_line(v[k], v[l]);
  69. for (Point2i point : points) {
  70. src_bitmap->set_bitv(point, true);
  71. }
  72. }
  73. }
  74. //src_bitmap->convert_to_image()->save_png("bitmap" + itos(i) + ".png");
  75. //grow by 1 for each side
  76. int bmw = src_bitmap->get_size().width + 2;
  77. int bmh = src_bitmap->get_size().height + 2;
  78. int heights_size = -1;
  79. bool transpose = false;
  80. if (chart.can_transpose && bmh > bmw) {
  81. heights_size = bmh;
  82. transpose = true;
  83. } else {
  84. heights_size = bmw;
  85. }
  86. max_w = MAX(max_w, heights_size);
  87. Vector<int> top_heights;
  88. Vector<int> bottom_heights;
  89. top_heights.resize(heights_size);
  90. bottom_heights.resize(heights_size);
  91. for (int x = 0; x < heights_size; x++) {
  92. top_heights.write[x] = -1;
  93. bottom_heights.write[x] = 0x7FFFFFFF;
  94. }
  95. for (int x = 0; x < bmw; x++) {
  96. for (int y = 0; y < bmh; y++) {
  97. bool found_pixel = false;
  98. for (int lx = x - 1; lx < x + 2 && !found_pixel; lx++) {
  99. for (int ly = y - 1; ly < y + 2 && !found_pixel; ly++) {
  100. int px = lx - 1;
  101. if (px < 0 || px >= w) {
  102. continue;
  103. }
  104. int py = ly - 1;
  105. if (py < 0 || py >= h) {
  106. continue;
  107. }
  108. if (src_bitmap->get_bit(px, py)) {
  109. found_pixel = true;
  110. }
  111. }
  112. }
  113. if (found_pixel) {
  114. if (transpose) {
  115. if (x > top_heights[y]) {
  116. top_heights.write[y] = x;
  117. }
  118. if (x < bottom_heights[y]) {
  119. bottom_heights.write[y] = x;
  120. }
  121. } else {
  122. if (y > top_heights[x]) {
  123. top_heights.write[x] = y;
  124. }
  125. if (y < bottom_heights[x]) {
  126. bottom_heights.write[x] = y;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. String row;
  133. for (int j = 0; j < top_heights.size(); j++) {
  134. row += "(" + itos(top_heights[j]) + "-" + itos(bottom_heights[j]) + "),";
  135. }
  136. PlottedBitmap plotted_bitmap;
  137. plotted_bitmap.offset = aabb.position;
  138. plotted_bitmap.top_heights = top_heights;
  139. plotted_bitmap.bottom_heights = bottom_heights;
  140. plotted_bitmap.chart_index = i;
  141. plotted_bitmap.transposed = transpose;
  142. plotted_bitmap.area = bmw * bmh;
  143. bitmaps.push_back(plotted_bitmap);
  144. }
  145. bitmaps.sort();
  146. int atlas_max_width = nearest_power_of_2_templated(p_atlas_max_size) / divide_by;
  147. int atlas_w = nearest_power_of_2_templated(max_w);
  148. int atlas_h;
  149. while (true) {
  150. atlas_h = 0;
  151. //do a tetris
  152. Vector<int> heights;
  153. heights.resize(atlas_w);
  154. for (int i = 0; i < atlas_w; i++) {
  155. heights.write[i] = 0;
  156. }
  157. int *atlas_ptr = heights.ptrw();
  158. for (int i = 0; i < bitmaps.size(); i++) {
  159. int best_height = 0x7FFFFFFF;
  160. int best_height_offset = -1;
  161. int w = bitmaps[i].top_heights.size();
  162. const int *top_heights = bitmaps[i].top_heights.ptr();
  163. const int *bottom_heights = bitmaps[i].bottom_heights.ptr();
  164. for (int j = 0; j <= atlas_w - w; j++) {
  165. int height = 0;
  166. for (int k = 0; k < w; k++) {
  167. int pixmap_h = bottom_heights[k];
  168. if (pixmap_h == 0x7FFFFFFF) {
  169. continue; //no pixel here, anything is fine
  170. }
  171. int h = MAX(0, atlas_ptr[j + k] - pixmap_h);
  172. if (h > height) {
  173. height = h;
  174. }
  175. }
  176. if (height < best_height) {
  177. best_height = height;
  178. best_height_offset = j;
  179. }
  180. }
  181. for (int j = 0; j < w; j++) { //add
  182. if (top_heights[j] == -1) { //unused
  183. continue;
  184. }
  185. int height = best_height + top_heights[j] + 1;
  186. atlas_ptr[j + best_height_offset] = height;
  187. atlas_h = MAX(atlas_h, height);
  188. }
  189. // set
  190. Vector2 offset = bitmaps[i].offset;
  191. if (bitmaps[i].transposed) {
  192. SWAP(offset.x, offset.y);
  193. }
  194. Vector2 final_pos = Vector2(best_height_offset * divide_by, best_height * divide_by) + Vector2(divide_by, divide_by) - offset;
  195. charts.write[bitmaps[i].chart_index].final_offset = final_pos;
  196. charts.write[bitmaps[i].chart_index].transposed = bitmaps[i].transposed;
  197. }
  198. if (atlas_h <= atlas_w * 2 || atlas_w >= atlas_max_width) {
  199. break; //ok this one is enough
  200. }
  201. //try again
  202. atlas_w *= 2;
  203. }
  204. r_width = atlas_w * divide_by;
  205. r_height = atlas_h * divide_by;
  206. }