editor_atlas.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*************************************************************************/
  2. /* editor_atlas.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "editor_atlas.h"
  30. struct _EditorAtlasWorkRect {
  31. Size2i s;
  32. Point2i p;
  33. int idx;
  34. _FORCE_INLINE_ bool operator<(const _EditorAtlasWorkRect& p_r) const { return s.width > p_r.s.width; };
  35. };
  36. struct _EditorAtlasWorkRectResult {
  37. Vector<_EditorAtlasWorkRect> result;
  38. int max_w;
  39. int max_h;
  40. };
  41. void EditorAtlas::fit(const Vector<Size2i>& p_rects,Vector<Point2i>& r_result, Size2i& r_size) {
  42. //super simple, almost brute force scanline stacking fitter
  43. //it's pretty basic for now, but it tries to make sure that the aspect ratio of the
  44. //resulting atlas is somehow square. This is necesary because video cards have limits
  45. //on texture size (usually 2048 or 4096), so the more square a texture, the more chances
  46. //it will work in every hardware.
  47. // for example, it will prioritize a 1024x1024 atlas (works everywhere) instead of a
  48. // 256x8192 atlas (won't work anywhere).
  49. ERR_FAIL_COND(p_rects.size()==0);
  50. Vector<_EditorAtlasWorkRect> wrects;
  51. wrects.resize(p_rects.size());
  52. for(int i=0;i<p_rects.size();i++) {
  53. wrects[i].s=p_rects[i];
  54. wrects[i].idx=i;
  55. }
  56. wrects.sort();
  57. int widest = wrects[0].s.width;
  58. Vector<_EditorAtlasWorkRectResult> results;
  59. for(int i=0;i<=12;i++) {
  60. int w = 1<<i;
  61. int max_h=0;
  62. int max_w=0;
  63. if ( w < widest )
  64. continue;
  65. Vector<int> hmax;
  66. hmax.resize(w);
  67. for(int j=0;j<w;j++)
  68. hmax[j]=0;
  69. //place them
  70. int ofs=0;
  71. int limit_h=0;
  72. for(int j=0;j<wrects.size();j++) {
  73. if (ofs+wrects[j].s.width > w) {
  74. ofs=0;
  75. }
  76. int from_y=0;
  77. for(int k=0;k<wrects[j].s.width;k++) {
  78. if (hmax[ofs+k] > from_y)
  79. from_y=hmax[ofs+k];
  80. }
  81. wrects[j].p.x=ofs;
  82. wrects[j].p.y=from_y;
  83. int end_h = from_y+wrects[j].s.height;
  84. int end_w = ofs+wrects[j].s.width;
  85. if (ofs==0)
  86. limit_h=end_h;
  87. for(int k=0;k<wrects[j].s.width;k++) {
  88. hmax[ofs+k]=end_h;
  89. }
  90. if (end_h > max_h)
  91. max_h=end_h;
  92. if (end_w > max_w)
  93. max_w=end_w;
  94. if (ofs==0 || end_h>limit_h ) //while h limit not reched, keep stacking
  95. ofs+=wrects[j].s.width;
  96. }
  97. _EditorAtlasWorkRectResult result;
  98. result.result=wrects;
  99. result.max_h=max_h;
  100. result.max_w=max_w;
  101. results.push_back(result);
  102. }
  103. //find the result with the best aspect ratio
  104. int best=-1;
  105. float best_aspect=1e20;
  106. for(int i=0;i<results.size();i++) {
  107. float h = nearest_power_of_2(results[i].max_h);
  108. float w = nearest_power_of_2(results[i].max_w);
  109. float aspect = h>w ? h/w : w/h;
  110. if (aspect < best_aspect) {
  111. best=i;
  112. best_aspect=aspect;
  113. }
  114. }
  115. r_result.resize(p_rects.size());
  116. for(int i=0;i<p_rects.size();i++) {
  117. r_result[ results[best].result[i].idx ]=results[best].result[i].p;
  118. }
  119. r_size=Size2(results[best].max_w,results[best].max_h );
  120. }