xatlas.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // This code is in the public domain -- [email protected]
  2. #pragma once
  3. #ifndef XATLAS_H
  4. #define XATLAS_H
  5. #include <float.h> // FLT_MAX
  6. // -- GODOT start --
  7. #include <limits.h> // INT_MAX, UINT_MAX
  8. // -- GODOT end --
  9. namespace xatlas {
  10. typedef void (*PrintFunc)(const char *, ...);
  11. struct Atlas;
  12. struct CharterOptions
  13. {
  14. float proxyFitMetricWeight;
  15. float roundnessMetricWeight;
  16. float straightnessMetricWeight;
  17. float normalSeamMetricWeight;
  18. float textureSeamMetricWeight;
  19. float maxChartArea;
  20. float maxBoundaryLength;
  21. CharterOptions()
  22. {
  23. // These are the default values we use on The Witness.
  24. proxyFitMetricWeight = 2.0f;
  25. roundnessMetricWeight = 0.01f;
  26. straightnessMetricWeight = 6.0f;
  27. normalSeamMetricWeight = 4.0f;
  28. textureSeamMetricWeight = 0.5f;
  29. /*
  30. proxyFitMetricWeight = 1.0f;
  31. roundnessMetricWeight = 0.1f;
  32. straightnessMetricWeight = 0.25f;
  33. normalSeamMetricWeight = 1.0f;
  34. textureSeamMetricWeight = 0.1f;
  35. */
  36. maxChartArea = FLT_MAX;
  37. maxBoundaryLength = FLT_MAX;
  38. }
  39. };
  40. struct PackMethod
  41. {
  42. enum Enum
  43. {
  44. TexelArea, // texel_area determines resolution
  45. ApproximateResolution, // guess texel_area to approximately match desired resolution
  46. ExactResolution // run the packer multiple times to exactly match the desired resolution (slow)
  47. };
  48. };
  49. struct PackerOptions
  50. {
  51. PackMethod::Enum method;
  52. // 0 - brute force
  53. // 1 - 4096 attempts
  54. // 2 - 2048
  55. // 3 - 1024
  56. // 4 - 512
  57. // other - 256
  58. // Avoid brute force packing, since it can be unusably slow in some situations.
  59. int quality;
  60. float texelArea; // This is not really texel area, but 1 / texel width?
  61. uint32_t resolution;
  62. bool blockAlign; // Align charts to 4x4 blocks.
  63. bool conservative; // Pack charts with extra padding.
  64. int padding;
  65. PackerOptions()
  66. {
  67. method = PackMethod::ApproximateResolution;
  68. quality = 1;
  69. texelArea = 8;
  70. resolution = 512;
  71. blockAlign = false;
  72. conservative = false;
  73. padding = 0;
  74. }
  75. };
  76. struct AddMeshErrorCode
  77. {
  78. enum Enum
  79. {
  80. Success,
  81. AlreadyAddedEdge, // index0 and index1 are the edge indices
  82. DegenerateColocalEdge, // index0 and index1 are the edge indices
  83. DegenerateEdge, // index0 and index1 are the edge indices
  84. DuplicateEdge, // index0 and index1 are the edge indices
  85. IndexOutOfRange, // index0 is the index
  86. InvalidIndexCount, // not evenly divisible by 3 - expecting triangles
  87. ZeroAreaFace,
  88. ZeroLengthEdge // index0 and index1 are the edge indices
  89. };
  90. };
  91. struct AddMeshError
  92. {
  93. AddMeshErrorCode::Enum code;
  94. uint32_t face;
  95. uint32_t index0, index1;
  96. };
  97. struct IndexFormat
  98. {
  99. enum Enum
  100. {
  101. HalfFloat,
  102. Float
  103. };
  104. };
  105. struct InputMesh
  106. {
  107. uint32_t vertexCount;
  108. const void *vertexPositionData;
  109. uint32_t vertexPositionStride;
  110. const void *vertexNormalData; // optional
  111. uint32_t vertexNormalStride; // optional
  112. // optional
  113. // The input UVs are provided as a hint to the chart generator.
  114. const void *vertexUvData;
  115. uint32_t vertexUvStride;
  116. uint32_t indexCount;
  117. const void *indexData;
  118. IndexFormat::Enum indexFormat;
  119. // optional. indexCount / 3 in length.
  120. // Charter also uses material boundaries as a hint to cut charts.
  121. const uint16_t *faceMaterialData;
  122. };
  123. struct OutputChart
  124. {
  125. uint32_t *indexArray;
  126. uint32_t indexCount;
  127. };
  128. struct OutputVertex
  129. {
  130. float uv[2];
  131. uint32_t xref; // Index of input vertex from which this output vertex originated.
  132. };
  133. struct OutputMesh
  134. {
  135. OutputChart *chartArray;
  136. uint32_t chartCount;
  137. uint32_t *indexArray;
  138. uint32_t indexCount;
  139. OutputVertex *vertexArray;
  140. uint32_t vertexCount;
  141. };
  142. void SetPrint(PrintFunc print);
  143. Atlas *Create();
  144. void Destroy(Atlas *atlas);
  145. // useColocalVertices - generates fewer charts (good), but is more sensitive to bad geometry.
  146. AddMeshError AddMesh(Atlas *atlas, const InputMesh &mesh, bool useColocalVertices = true);
  147. void Generate(Atlas *atlas, CharterOptions charterOptions = CharterOptions(), PackerOptions packerOptions = PackerOptions());
  148. uint32_t GetWidth(const Atlas *atlas);
  149. uint32_t GetHeight(const Atlas *atlas);
  150. uint32_t GetNumCharts(const Atlas *atlas);
  151. const OutputMesh * const *GetOutputMeshes(const Atlas *atlas);
  152. const char *StringForEnum(AddMeshErrorCode::Enum error);
  153. } // namespace xatlas
  154. #endif // XATLAS_H