TessellationUtilities.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2016 Chris Conway (Koderz). All Rights Reserved.
  2. #pragma once
  3. #include "RuntimeMeshBuilder.h"
  4. /**
  5. *
  6. */
  7. class TessellationUtilities
  8. {
  9. public:
  10. static void CalculateTessellationIndices(const IRuntimeMeshVerticesBuilder* Vertices, const FRuntimeMeshIndicesBuilder* Indices, FRuntimeMeshIndicesBuilder* TessellationIndices);
  11. private:
  12. struct Vertex
  13. {
  14. FVector Position;
  15. FVector2D TexCoord;
  16. Vertex() { }
  17. Vertex(const FVector& InPosition, const FVector2D& InTexCoord)
  18. : Position(InPosition), TexCoord(InTexCoord)
  19. { }
  20. FORCEINLINE bool operator==(const Vertex& Other) const
  21. {
  22. return Position == Other.Position;
  23. }
  24. FORCEINLINE bool operator<(const Vertex& Other) const
  25. {
  26. return Position.X < Other.Position.X
  27. || Position.Y < Other.Position.Y
  28. || Position.Z < Other.Position.Z;
  29. }
  30. };
  31. static FORCEINLINE uint32 HashValue(const FVector& Vec)
  32. {
  33. return 31337 * GetTypeHash(Vec.X) + 13 * GetTypeHash(Vec.Y) + 3 * GetTypeHash(Vec.Z);
  34. }
  35. static FORCEINLINE uint32 HashValue(const Vertex& Vert)
  36. {
  37. return HashValue(Vert.Position);
  38. }
  39. struct Edge
  40. {
  41. private:
  42. uint32 IndexFrom;
  43. uint32 IndexTo;
  44. Vertex VertexFrom;
  45. Vertex VertexTo;
  46. uint32 CachedHash;
  47. public:
  48. Edge() : CachedHash(0) { }
  49. Edge(uint32 InIndexFrom, uint32 InIndexTo, const Vertex& InVertexFrom, const Vertex& InVertexTo)
  50. : IndexFrom(InIndexFrom), IndexTo(InIndexTo), VertexFrom(InVertexFrom), VertexTo(InVertexTo)
  51. {
  52. // Hash should only consider position, not index.
  53. // We want values with different indices to compare true.
  54. CachedHash = 7 * HashValue(VertexFrom) + 2 * HashValue(VertexTo);
  55. }
  56. Vertex GetVertex(uint32 I) const
  57. {
  58. switch (I)
  59. {
  60. case 0:
  61. return VertexFrom;
  62. case 1:
  63. return VertexTo;
  64. default:
  65. checkNoEntry();
  66. return Vertex();
  67. }
  68. }
  69. uint32 GetIndex(uint32 I) const
  70. {
  71. switch (I)
  72. {
  73. case 0:
  74. return IndexFrom;
  75. case 1:
  76. return IndexTo;
  77. default:
  78. checkNoEntry();
  79. return 0;
  80. }
  81. }
  82. Edge GetReverse() const
  83. {
  84. return Edge(IndexTo, IndexFrom, VertexTo, VertexFrom);
  85. }
  86. FORCEINLINE bool operator<(const Edge& Other) const
  87. {
  88. // Quick out, otherwise we have to compare vertices
  89. if (IndexFrom == Other.IndexFrom && IndexTo == Other.IndexTo)
  90. {
  91. return false;
  92. }
  93. return VertexFrom < Other.VertexFrom || VertexTo < Other.VertexTo;
  94. }
  95. FORCEINLINE bool operator==(const Edge& Other) const
  96. {
  97. return (IndexFrom == Other.IndexFrom && IndexTo == Other.IndexTo) ||
  98. (VertexFrom == Other.VertexFrom && VertexTo == Other.VertexTo);
  99. }
  100. friend FORCEINLINE uint32 GetTypeHash(const Edge& E)
  101. {
  102. return E.CachedHash;
  103. }
  104. };
  105. struct Corner
  106. {
  107. uint32 Index;
  108. FVector2D TexCoord;
  109. Corner() : Index(0) { }
  110. Corner(uint32 InIndex, FVector2D InTexCoord)
  111. : Index(InIndex), TexCoord(InTexCoord)
  112. { }
  113. };
  114. class Triangle
  115. {
  116. Edge Edge0;
  117. Edge Edge1;
  118. Edge Edge2;
  119. public:
  120. Triangle(uint32 Index0, uint32 Index1, uint32 Index2, const Vertex& Vertex0, const Vertex& Vertex1, const Vertex& Vertex2)
  121. : Edge0(Index0, Index1, Vertex0, Vertex1)
  122. , Edge1(Index1, Index2, Vertex1, Vertex2)
  123. , Edge2(Index2, Index0, Vertex2, Vertex0)
  124. { }
  125. FORCEINLINE bool operator<(const Triangle& Other) const
  126. {
  127. return Edge0 < Other.Edge0 || Edge1 < Other.Edge1 || Edge2 < Other.Edge2;
  128. }
  129. FORCEINLINE const Edge& GetEdge(uint32 I)
  130. {
  131. return ((Edge*)&Edge0)[I];
  132. }
  133. FORCEINLINE uint32 GetIndex(uint32 I)
  134. {
  135. return GetEdge(I).GetIndex(0);
  136. }
  137. };
  138. using EdgeDictionary = TMap<Edge, Edge>;
  139. using PositionDictionary = TMap<FVector, Corner>;
  140. static void AddIfLeastUV(PositionDictionary& PosDict, const Vertex& Vert, uint32 Index);
  141. static void ReplacePlaceholderIndices(const IRuntimeMeshVerticesBuilder* Vertices, const FRuntimeMeshIndicesBuilder* Indices,
  142. EdgeDictionary& EdgeDict, PositionDictionary& PosDict, FRuntimeMeshIndicesBuilder* OutIndices);
  143. static void ExpandIB(const IRuntimeMeshVerticesBuilder* Vertices, const FRuntimeMeshIndicesBuilder* Indices,
  144. EdgeDictionary& OutEdgeDict, PositionDictionary& OutPosDict, FRuntimeMeshIndicesBuilder* OutIndices);
  145. };