BsTexAtlasGenerator.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include "BsTexAtlasGenerator.h"
  2. #include "BsDebug.h"
  3. namespace BansheeEngine
  4. {
  5. class TexAtlasNode
  6. {
  7. public:
  8. TexAtlasNode()
  9. :x(0), y(0), width(0), height(0), children(nullptr), nodeFull(false)
  10. { }
  11. TexAtlasNode(UINT32 _x, UINT32 _y, UINT32 _width, UINT32 _height)
  12. :x(_x), y(_y), width(_width), height(_height), children(nullptr), nodeFull(false)
  13. { }
  14. ~TexAtlasNode()
  15. {
  16. if(children != nullptr)
  17. bs_deleteN(children, 2);
  18. }
  19. UINT32 x, y, width, height;
  20. TexAtlasNode* children;
  21. bool nodeFull;
  22. bool insert(TexAtlasElementDesc& element)
  23. {
  24. float aspect = width / (float)height;
  25. return insert(element, aspect);
  26. }
  27. bool insert(TexAtlasElementDesc& element, float aspect)
  28. {
  29. if (children != nullptr)
  30. {
  31. if (children[0].insert(element))
  32. return true;
  33. return children[1].insert(element);
  34. }
  35. else
  36. {
  37. if(nodeFull)
  38. return false;
  39. if (element.input.width > width || element.input.height > height)
  40. return false;
  41. if (element.input.width == width && element.input.height == height)
  42. {
  43. element.output.x = x;
  44. element.output.y = y;
  45. nodeFull = true;
  46. return true;
  47. }
  48. float dw = (float)(width - element.input.width);
  49. float dh = (height - element.input.height) * aspect;
  50. children = bs_newN<TexAtlasNode>(2);
  51. if (dw > dh)
  52. {
  53. children[0].x = x;
  54. children[0].y = y;
  55. children[0].width = element.input.width;
  56. children[0].height = height;
  57. children[1].x = x + element.input.width;
  58. children[1].y = y;
  59. children[1].width = width - element.input.width;
  60. children[1].height = height;
  61. }
  62. else
  63. {
  64. children[0].x = x;
  65. children[0].y = y;
  66. children[0].width = width;
  67. children[0].height = element.input.height;
  68. children[1].x = x;
  69. children[1].y = y + element.input.height;
  70. children[1].width = width;
  71. children[1].height = height - element.input.height;
  72. }
  73. return children[0].insert(element);
  74. }
  75. }
  76. };
  77. TexAtlasGenerator::TexAtlasGenerator(bool square, UINT32 maxTexWidth, UINT32 maxTexHeight, bool fixedSize)
  78. :mSquare(square), mMaxTexWidth(maxTexWidth), mMaxTexHeight(maxTexHeight), mFixedSize(fixedSize)
  79. {
  80. if(square)
  81. {
  82. if(maxTexWidth > maxTexHeight)
  83. maxTexWidth = maxTexHeight;
  84. if(maxTexHeight > maxTexWidth)
  85. maxTexHeight = maxTexWidth;
  86. }
  87. }
  88. Vector<TexAtlasPageDesc> TexAtlasGenerator::createAtlasLayout(Vector<TexAtlasElementDesc>& elements) const
  89. {
  90. for(size_t i = 0; i < elements.size(); i++)
  91. elements[i].output.page = -1;
  92. //sortBySize(elements);
  93. int numPages = generatePagesForSize(elements, mMaxTexWidth, mMaxTexHeight);
  94. if(numPages == -1)
  95. {
  96. LOGWRN("Some of the provided elements don't fit in an atlas of provided size. Returning empty array of pages.");
  97. return Vector<TexAtlasPageDesc>();
  98. }
  99. if(numPages == 0)
  100. return Vector<TexAtlasPageDesc>();
  101. UINT32 lastPageWidth = mMaxTexWidth;
  102. UINT32 lastPageHeight = mMaxTexHeight;
  103. INT32 lastPageIdx = numPages - 1;
  104. // If size isn't fixed, try to reduce the size of the last page
  105. if(!mFixedSize)
  106. {
  107. while (true)
  108. {
  109. if (lastPageWidth <= 1 || lastPageHeight <= 1)
  110. break;
  111. int newLastPageWidth = lastPageWidth;
  112. int newLastPageHeight = lastPageHeight;
  113. if(mSquare)
  114. {
  115. if (newLastPageWidth > newLastPageHeight)
  116. newLastPageWidth /= 2;
  117. else
  118. newLastPageHeight /= 2;
  119. }
  120. else
  121. {
  122. if (newLastPageWidth > newLastPageHeight)
  123. newLastPageWidth /= 2;
  124. else
  125. newLastPageHeight /= 2;
  126. }
  127. // Clear page indexes so we know which pages to process
  128. for(size_t i = 0; i < elements.size(); i++)
  129. {
  130. if(elements[i].output.page >= lastPageIdx)
  131. elements[i].output.page = -1;
  132. }
  133. if(generatePagesForSize(elements, newLastPageWidth, newLastPageHeight, lastPageIdx) == 1)
  134. {
  135. lastPageWidth = newLastPageWidth;
  136. lastPageHeight = newLastPageHeight;
  137. }
  138. else
  139. {
  140. // We're done but we need to re-do all the pages with the last valid size
  141. for(size_t i = 0; i < elements.size(); i++)
  142. {
  143. if(elements[i].output.page >= lastPageIdx)
  144. elements[i].output.page = -1;
  145. }
  146. generatePagesForSize(elements, lastPageWidth, lastPageHeight, lastPageIdx);
  147. break;
  148. }
  149. }
  150. }
  151. // Handle degenerate case
  152. for(size_t i = 0; i < elements.size(); i++)
  153. {
  154. if(elements[i].output.page == -1 && elements[i].input.width == 0 && elements[i].input.height == 0)
  155. {
  156. elements[i].output.x = 0;
  157. elements[i].output.y = 0;
  158. }
  159. }
  160. // Create page descriptors and return
  161. Vector<TexAtlasPageDesc> pages;
  162. for(int i = 0; i < numPages - 1; i++)
  163. {
  164. TexAtlasPageDesc pageDesc;
  165. pageDesc.width = mMaxTexWidth;
  166. pageDesc.height = mMaxTexHeight;
  167. pages.push_back(pageDesc);
  168. }
  169. TexAtlasPageDesc lastPageDesc;
  170. lastPageDesc.width = lastPageWidth;
  171. lastPageDesc.height = lastPageHeight;
  172. pages.push_back(lastPageDesc);
  173. return pages;
  174. }
  175. int TexAtlasGenerator::generatePagesForSize(Vector<TexAtlasElementDesc>& elements, UINT32 width, UINT32 height, UINT32 startPage) const
  176. {
  177. if(elements.size() == 0)
  178. return 0;
  179. int currentPage = startPage;
  180. int numPages = 0;
  181. while (true)
  182. {
  183. int largestTexId = findLargestTextureWithoutPage(elements); // Start with the largest available texture
  184. if (largestTexId == -1) // No more textures, we're done
  185. return numPages;
  186. TexAtlasElementDesc& currentElem = elements[largestTexId];
  187. // If the texture is larger than the atlas size then it can never fit
  188. while (width < currentElem.input.width || height < currentElem.input.height)
  189. return -1;
  190. TexAtlasNode atlasNode(0, 0, width, height);
  191. atlasNode.insert(elements[largestTexId]);
  192. elements[largestTexId].output.page = currentPage;
  193. // Now that the first (largest) texture has been added, do the same for every other texture until atlas is full
  194. while (true)
  195. {
  196. int addedTextureId = addLargestTextureWithoutPageThatFits(elements, atlasNode); // Try to add next largest texture
  197. if (addedTextureId == -1)
  198. break;
  199. elements[addedTextureId].output.page = currentPage;
  200. }
  201. currentPage++;
  202. numPages++;
  203. }
  204. }
  205. int TexAtlasGenerator::addLargestTextureWithoutPageThatFits(Vector<TexAtlasElementDesc>& elements, TexAtlasNode& node) const
  206. {
  207. UINT32 sizeLimit = std::numeric_limits<UINT32>::max();
  208. while (true)
  209. {
  210. UINT32 largestSize = 0;
  211. INT32 largestId = -1;
  212. for (size_t i = 0; i < elements.size(); i++)
  213. {
  214. if (elements[i].output.page == -1) // Signifies that we haven't processed this node yet
  215. {
  216. UINT32 size = elements[i].input.width * elements[i].input.height;
  217. if (size > largestSize && size < sizeLimit)
  218. {
  219. largestSize = size;
  220. largestId = (INT32)i;
  221. }
  222. }
  223. }
  224. if (largestId == -1)
  225. return -1;
  226. if (node.insert(elements[largestId]))
  227. return largestId;
  228. else
  229. sizeLimit = largestSize;
  230. }
  231. }
  232. int TexAtlasGenerator::findLargestTextureWithoutPage(const Vector<TexAtlasElementDesc>& elements) const
  233. {
  234. INT32 largestId = -1;
  235. UINT32 largestSize = 0;
  236. for (size_t i = 0; i < elements.size(); i++)
  237. {
  238. if (elements[i].output.page == -1) // Signifies that we haven't processed this node yet
  239. {
  240. UINT32 size = elements[i].input.width * elements[i].input.height;
  241. if (size > largestSize)
  242. {
  243. largestSize = size;
  244. largestId = (INT32)i;
  245. }
  246. }
  247. }
  248. return largestId;
  249. }
  250. void TexAtlasGenerator::sortBySize(Vector<TexAtlasElementDesc>& elements) const
  251. {
  252. for (size_t i = 0; i < elements.size(); i++)
  253. {
  254. INT32 largestId = -1;
  255. UINT32 largestSize = 0;
  256. for (size_t j = i; j < elements.size(); j++)
  257. {
  258. UINT32 size = elements[j].input.width * elements[j].input.height;
  259. if (size > largestSize)
  260. {
  261. largestSize = size;
  262. largestId = (INT32)j;
  263. }
  264. }
  265. if(largestId != -1)
  266. {
  267. TexAtlasElementDesc temp = elements[i];
  268. elements[i] = elements[largestId];
  269. elements[largestId] = temp;
  270. }
  271. }
  272. }
  273. }