editor_atlas.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*************************************************************************/
  2. /* editor_atlas.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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.h"
  31. #include "print_string.h"
  32. #include <cfloat>
  33. struct _EditorAtlasWorkRect {
  34. Size2i s;
  35. Point2i p;
  36. int idx;
  37. _FORCE_INLINE_ bool operator<(const _EditorAtlasWorkRect &p_r) const { return s.width > p_r.s.width; };
  38. };
  39. struct _EditorAtlasWorkRectResult {
  40. Vector<_EditorAtlasWorkRect> result;
  41. int max_w;
  42. int max_h;
  43. };
  44. void EditorAtlas::fit(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size) {
  45. ERR_FAIL_COND(p_rects.size() == 0);
  46. Vector<_EditorAtlasWorkRect> wrects;
  47. wrects.resize(p_rects.size());
  48. long total_area = 0;
  49. for (int i = 0; i < p_rects.size(); i++) {
  50. wrects[i].s = p_rects[i];
  51. wrects[i].idx = i;
  52. total_area += p_rects[i].width * p_rects[i].height;
  53. }
  54. wrects.sort();
  55. int widest = wrects[0].s.width;
  56. Vector<_EditorAtlasWorkRectResult> results;
  57. for (int i = 0; i <= 12; i++) {
  58. int w = 1 << i;
  59. int max_h = 0;
  60. int max_w = 0;
  61. if (w < widest)
  62. continue;
  63. Vector<int> wmax;
  64. wmax.resize(total_area / w);
  65. for (int j = 0; j < wmax.size(); j++)
  66. wmax[j] = 0;
  67. for (int j = 0; j < wrects.size(); j++) {
  68. int new_x = 0;
  69. int new_y = 0;
  70. int piece_w = wrects[j].s.width;
  71. int piece_h = wrects[j].s.height;
  72. bool found_place;
  73. do {
  74. found_place = true;
  75. new_x = 0;
  76. if (wmax.size() <= new_y + piece_h) {
  77. int prevS = wmax.size();
  78. wmax.resize(new_y + piece_h + 128);
  79. for (int k = prevS; k < wmax.size(); k++)
  80. wmax[k] = 0;
  81. }
  82. for (int k = 0; k < piece_h; k++) {
  83. if (new_x < wmax[new_y + k]) new_x = wmax[new_y + k];
  84. if (new_x + piece_w > w) {
  85. new_y += k + 1;
  86. found_place = false;
  87. break;
  88. }
  89. }
  90. if (found_place) {
  91. // one more check is calculating lost space of atlas
  92. long lost_area = 0;
  93. for (int k = 0; k < piece_h; k++) {
  94. lost_area += new_x - wmax[new_y + k];
  95. }
  96. if (lost_area >= piece_w * piece_h / 2) {
  97. found_place = false;
  98. new_y++;
  99. }
  100. }
  101. } while (!found_place);
  102. wrects[j].p.x = new_x;
  103. wrects[j].p.y = new_y;
  104. int end_h = new_y + piece_h;
  105. int end_w = new_x + piece_w;
  106. for (int k = 0; k < piece_h; k++) {
  107. wmax[new_y + k] = end_w;
  108. }
  109. if (end_h > max_h)
  110. max_h = end_h;
  111. if (end_w > max_w)
  112. max_w = end_w;
  113. }
  114. _EditorAtlasWorkRectResult result;
  115. result.result = wrects;
  116. result.max_h = max_h;
  117. result.max_w = max_w;
  118. results.push_back(result);
  119. float perimeter = (next_power_of_2(max_w) + max_h) * 2.f;
  120. print_line("Processing atlas: width " + itos(w) + " ,height " + itos(max_h) + " ,perimeter " + rtos(perimeter));
  121. }
  122. //find the result with the most efficiency
  123. int best = -1;
  124. float min_perimeter = FLT_MAX;
  125. for (int i = 0; i < results.size(); i++) {
  126. float h = results[i].max_h;
  127. float w = results[i].max_w;
  128. float perimeter = (next_power_of_2(w) + h) * 2.f;
  129. if (perimeter < min_perimeter) {
  130. best = i;
  131. min_perimeter = perimeter;
  132. }
  133. }
  134. if (best < 0) {
  135. ERR_PRINT("Atlas processing failed!");
  136. return;
  137. }
  138. r_result.resize(p_rects.size());
  139. for (int i = 0; i < p_rects.size(); i++) {
  140. r_result[results[best].result[i].idx] = results[best].result[i].p;
  141. }
  142. r_size = Size2(results[best].max_w, results[best].max_h);
  143. }