BsTexAtlasGenerator.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsVector2.h"
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup Image
  7. * @{
  8. */
  9. /**
  10. * Represents a single element used as in input to TexAtlasGenerator. Usually represents a single texture.
  11. *
  12. * @note input is required to be filled in before passing it to TexAtlasGenerator.
  13. * @note output will be filled in by TexAtlasGenerator after a call to TexAtlasGenerator::createAtlasLayout().
  14. */
  15. struct TexAtlasElementDesc
  16. {
  17. struct
  18. {
  19. UINT32 width, height;
  20. } input;
  21. struct
  22. {
  23. UINT32 x, y;
  24. INT32 page;
  25. } output;
  26. };
  27. /** A single page of the texture atlas. */
  28. struct TexAtlasPageDesc
  29. {
  30. UINT32 width, height;
  31. };
  32. class TexAtlasNode;
  33. /** Organizes a set of textures into a single larger texture (an atlas) by minimizing empty space. */
  34. class BS_UTILITY_EXPORT TexAtlasGenerator
  35. {
  36. public:
  37. /**
  38. * Constructs a new texture atlas generator with the provided parameters.
  39. *
  40. * @param[in] square (optional) Should the returned texture always be square. (width == height)
  41. * This option is only used if @p fixedSize parameter is set to false.
  42. * @param[in] maxTexWidth (optional) Maximum width of the texture.
  43. * @param[in] maxTexHeight (optional) Maximum height of the texture.
  44. * @param[in] fixedSize (optional) If this field is false, algorithm will try to reduce the size of the texture
  45. * if possible. If it is true, the algorithm will always produce textures of the specified
  46. * @p maxTexWidth, @p maxTexHeight size.
  47. */
  48. TexAtlasGenerator(bool square = false, UINT32 maxTexWidth = 2048, UINT32 maxTexHeight = 2048, bool fixedSize = false);
  49. /**
  50. * Creates an optimal texture layout by packing texture elements in order to end up with as little empty space
  51. * as possible.
  52. *
  53. * @param[in] elements Elements to process. They need to have their input structures filled in,
  54. * and this method will fill output when it returns.
  55. * @return One or more descriptors that determine the size of the final atlas textures.
  56. * Texture elements will reference these pages with their output.page parameter.
  57. *
  58. * @note
  59. * Algorithm will split elements over multiple textures if they don't fit in a single texture (Determined by
  60. * maximum texture size).
  61. */
  62. Vector<TexAtlasPageDesc> createAtlasLayout(Vector<TexAtlasElementDesc>& elements) const;
  63. private:
  64. bool mSquare;
  65. bool mFixedSize;
  66. UINT32 mMaxTexWidth;
  67. UINT32 mMaxTexHeight;
  68. /**
  69. * Organize all of the provide elements and place them into minimum number of pages with the specified width and height.
  70. *
  71. * Caller must ensure @p elements array has the page indexes reset to -1 before calling, otherwise it will be assumed
  72. * those elements already have assigned pages.
  73. *
  74. * Using @p startPage parameter you may add an offset to the generated page indexes.
  75. *
  76. * @return Number of pages generated.
  77. */
  78. int generatePagesForSize(Vector<TexAtlasElementDesc>& elements, UINT32 width, UINT32 height, UINT32 startPage = 0) const;
  79. /**
  80. * Finds the largest element without a page that fits within the provided node.
  81. *
  82. * @return Array index of the found page, or -1 if all textures have a page.
  83. */
  84. int addLargestTextureWithoutPageThatFits(Vector<TexAtlasElementDesc>& elements, TexAtlasNode& node) const;
  85. /**
  86. * Scan all of the provided elements and find the largest one that still doesn't have a page assigned.
  87. *
  88. * @return Array index of the found page, or -1 if all textures have a page.
  89. */
  90. int findLargestTextureWithoutPage(const Vector<TexAtlasElementDesc>& elements) const;
  91. /** Sorts all the texture elements so that larget elements come first. */
  92. void sortBySize(Vector<TexAtlasElementDesc>& elements) const;
  93. };
  94. /** @endcond */
  95. }