editor_atlas_packer.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. 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. }
  76. void EditorAtlasPacker::chart_pack(Vector<Chart> &charts, int &r_width, int &r_height, int p_atlas_max_size, int p_cell_resolution) {
  77. int divide_by = MIN(64, p_cell_resolution);
  78. Vector<PlottedBitmap> bitmaps;
  79. int max_w = 0;
  80. for (int i = 0; i < charts.size(); i++) {
  81. const Chart &chart = charts[i];
  82. //generate aabb
  83. Rect2i aabb;
  84. int vertex_count = chart.vertices.size();
  85. const Vector2 *vertices = chart.vertices.ptr();
  86. for (int j = 0; j < vertex_count; j++) {
  87. if (j == 0) {
  88. aabb.position = vertices[j];
  89. } else {
  90. aabb.expand_to(vertices[j]);
  91. }
  92. }
  93. Ref<BitMap> src_bitmap;
  94. src_bitmap.instance();
  95. src_bitmap->create(Size2(Math::ceil(aabb.size.x / (real_t)divide_by), Math::ceil(aabb.size.y / (real_t)divide_by)));
  96. int w = src_bitmap->get_size().width;
  97. int h = src_bitmap->get_size().height;
  98. //plot triangles, using divisor
  99. for (int j = 0; j < chart.faces.size(); j++) {
  100. Vector2i v[3];
  101. for (int k = 0; k < 3; k++) {
  102. Vector2 vtx = chart.vertices[chart.faces[j].vertex[k]];
  103. vtx -= aabb.position;
  104. vtx /= divide_by;
  105. v[k] = vtx;
  106. }
  107. _plot_triangle(src_bitmap, v);
  108. }
  109. //src_bitmap->convert_to_image()->save_png("bitmap" + itos(i) + ".png");
  110. //grow by 1 for each side
  111. int bmw = src_bitmap->get_size().width + 2;
  112. int bmh = src_bitmap->get_size().height + 2;
  113. int heights_size = -1;
  114. bool transpose = false;
  115. if (chart.can_transpose && bmh > bmw) {
  116. heights_size = bmh;
  117. transpose = true;
  118. } else {
  119. heights_size = bmw;
  120. }
  121. max_w = MAX(max_w, heights_size);
  122. Vector<int> top_heights;
  123. Vector<int> bottom_heights;
  124. top_heights.resize(heights_size);
  125. bottom_heights.resize(heights_size);
  126. for (int x = 0; x < heights_size; x++) {
  127. top_heights.write[x] = -1;
  128. bottom_heights.write[x] = 0x7FFFFFFF;
  129. }
  130. for (int x = 0; x < bmw; x++) {
  131. for (int y = 0; y < bmh; y++) {
  132. bool found_pixel = false;
  133. for (int lx = x - 1; lx < x + 2 && !found_pixel; lx++) {
  134. for (int ly = y - 1; ly < y + 2 && !found_pixel; ly++) {
  135. int px = lx - 1;
  136. if (px < 0 || px >= w) {
  137. continue;
  138. }
  139. int py = ly - 1;
  140. if (py < 0 || py >= h) {
  141. continue;
  142. }
  143. if (src_bitmap->get_bit(Vector2(px, py))) {
  144. found_pixel = true;
  145. }
  146. }
  147. }
  148. if (found_pixel) {
  149. if (transpose) {
  150. if (x > top_heights[y]) {
  151. top_heights.write[y] = x;
  152. }
  153. if (x < bottom_heights[y]) {
  154. bottom_heights.write[y] = x;
  155. }
  156. } else {
  157. if (y > top_heights[x]) {
  158. top_heights.write[x] = y;
  159. }
  160. if (y < bottom_heights[x]) {
  161. bottom_heights.write[x] = y;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. String row;
  168. for (int j = 0; j < top_heights.size(); j++) {
  169. row += "(" + itos(top_heights[j]) + "-" + itos(bottom_heights[j]) + "),";
  170. }
  171. PlottedBitmap plotted_bitmap;
  172. plotted_bitmap.offset = aabb.position;
  173. plotted_bitmap.top_heights = top_heights;
  174. plotted_bitmap.bottom_heights = bottom_heights;
  175. plotted_bitmap.chart_index = i;
  176. plotted_bitmap.transposed = transpose;
  177. plotted_bitmap.area = bmw * bmh;
  178. bitmaps.push_back(plotted_bitmap);
  179. }
  180. bitmaps.sort();
  181. int atlas_max_width = nearest_power_of_2_templated(p_atlas_max_size) / divide_by;
  182. int atlas_w = nearest_power_of_2_templated(max_w);
  183. int atlas_h;
  184. while (true) {
  185. atlas_h = 0;
  186. //do a tetris
  187. Vector<int> heights;
  188. heights.resize(atlas_w);
  189. for (int i = 0; i < atlas_w; i++) {
  190. heights.write[i] = 0;
  191. }
  192. int *atlas_ptr = heights.ptrw();
  193. for (int i = 0; i < bitmaps.size(); i++) {
  194. int best_height = 0x7FFFFFFF;
  195. int best_height_offset = -1;
  196. int w = bitmaps[i].top_heights.size();
  197. const int *top_heights = bitmaps[i].top_heights.ptr();
  198. const int *bottom_heights = bitmaps[i].bottom_heights.ptr();
  199. for (int j = 0; j <= atlas_w - w; j++) {
  200. int height = 0;
  201. for (int k = 0; k < w; k++) {
  202. int pixmap_h = bottom_heights[k];
  203. if (pixmap_h == 0x7FFFFFFF) {
  204. continue; //no pixel here, anything is fine
  205. }
  206. int h = MAX(0, atlas_ptr[j + k] - pixmap_h);
  207. if (h > height) {
  208. height = h;
  209. }
  210. }
  211. if (height < best_height) {
  212. best_height = height;
  213. best_height_offset = j;
  214. }
  215. }
  216. for (int j = 0; j < w; j++) { //add
  217. if (top_heights[j] == -1) { //unused
  218. continue;
  219. }
  220. int height = best_height + top_heights[j] + 1;
  221. atlas_ptr[j + best_height_offset] = height;
  222. atlas_h = MAX(atlas_h, height);
  223. }
  224. // set
  225. Vector2 offset = bitmaps[i].offset;
  226. if (bitmaps[i].transposed) {
  227. SWAP(offset.x, offset.y);
  228. }
  229. Vector2 final_pos = Vector2(best_height_offset * divide_by, best_height * divide_by) + Vector2(divide_by, divide_by) - offset;
  230. charts.write[bitmaps[i].chart_index].final_offset = final_pos;
  231. charts.write[bitmaps[i].chart_index].transposed = bitmaps[i].transposed;
  232. }
  233. if (atlas_h <= atlas_w * 2 || atlas_w >= atlas_max_width) {
  234. break; //ok this one is enough
  235. }
  236. //try again
  237. atlas_w *= 2;
  238. }
  239. r_width = atlas_w * divide_by;
  240. r_height = atlas_h * divide_by;
  241. }