xatlas.h 4.0 KB

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