editor_atlas_packer.cpp 8.2 KB

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