avoid-failing-on-bad-geometry.patch 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. diff --git a/thirdparty/xatlas/xatlas.cpp b/thirdparty/xatlas/xatlas.cpp
  2. index df5ef94db..eb0824a51 100644
  3. --- a/thirdparty/xatlas/xatlas.cpp
  4. +++ b/thirdparty/xatlas/xatlas.cpp
  5. @@ -1276,6 +1276,9 @@ class Vertex
  6. {
  7. public:
  8. uint32_t id;
  9. + // -- GODOT start --
  10. + uint32_t original_id;
  11. + // -- GODOT end --
  12. Edge *edge;
  13. Vertex *next;
  14. Vertex *prev;
  15. @@ -1283,7 +1286,10 @@ public:
  16. Vector3 nor;
  17. Vector2 tex;
  18. - Vertex(uint32_t id) : id(id), edge(NULL), pos(0.0f), nor(0.0f), tex(0.0f)
  19. + // -- GODOT start --
  20. + //Vertex(uint32_t id) : id(id), edge(NULL), pos(0.0f), nor(0.0f), tex(0.0f)
  21. + Vertex(uint32_t id) : id(id), original_id(id), edge(NULL), pos(0.0f), nor(0.0f), tex(0.0f)
  22. + // -- GODOT end --
  23. {
  24. next = this;
  25. prev = this;
  26. @@ -1934,6 +1940,64 @@ public:
  27. return f;
  28. }
  29. + // -- GODOT start --
  30. + Face *addUniqueFace(uint32_t v0, uint32_t v1, uint32_t v2) {
  31. +
  32. + int base_vertex = m_vertexArray.size();
  33. +
  34. + uint32_t ids[3] = { v0, v1, v2 };
  35. +
  36. + Vector3 base[3] = {
  37. + m_vertexArray[v0]->pos,
  38. + m_vertexArray[v1]->pos,
  39. + m_vertexArray[v2]->pos,
  40. + };
  41. +
  42. + //make sure its not a degenerate
  43. + bool degenerate = distanceSquared(base[0], base[1]) < NV_EPSILON || distanceSquared(base[0], base[2]) < NV_EPSILON || distanceSquared(base[1], base[2]) < NV_EPSILON;
  44. + xaDebugAssert(!degenerate);
  45. +
  46. + float min_x = 0;
  47. +
  48. + for (int i = 0; i < 3; i++) {
  49. + if (i == 0 || m_vertexArray[v0]->pos.x < min_x) {
  50. + min_x = m_vertexArray[v0]->pos.x;
  51. + }
  52. + }
  53. +
  54. + float max_x = 0;
  55. +
  56. + for (int j = 0; j < m_vertexArray.size(); j++) {
  57. + if (j == 0 || m_vertexArray[j]->pos.x > max_x) { //vertex already exists
  58. + max_x = m_vertexArray[j]->pos.x;
  59. + }
  60. + }
  61. +
  62. + //separate from everything else, in x axis
  63. + for (int i = 0; i < 3; i++) {
  64. +
  65. + base[i].x -= min_x;
  66. + base[i].x += max_x + 10.0;
  67. + }
  68. +
  69. + for (int i = 0; i < 3; i++) {
  70. + Vertex *v = new Vertex(m_vertexArray.size());
  71. + v->pos = base[i];
  72. + v->nor = m_vertexArray[ids[i]]->nor,
  73. + v->tex = m_vertexArray[ids[i]]->tex,
  74. +
  75. + v->original_id = ids[i];
  76. + m_vertexArray.push_back(v);
  77. + }
  78. +
  79. + uint32_t indexArray[3];
  80. + indexArray[0] = base_vertex + 0;
  81. + indexArray[1] = base_vertex + 1;
  82. + indexArray[2] = base_vertex + 2;
  83. + return addFace(indexArray, 3, 0, 3);
  84. + }
  85. + // -- GODOT end --
  86. +
  87. // These functions disconnect the given element from the mesh and delete it.
  88. // @@ We must always disconnect edge pairs simultaneously.
  89. @@ -2915,6 +2979,14 @@ Mesh *triangulate(const Mesh *inputMesh)
  90. Vector2 p0 = polygonPoints[i0];
  91. Vector2 p1 = polygonPoints[i1];
  92. Vector2 p2 = polygonPoints[i2];
  93. +
  94. + // -- GODOT start --
  95. + bool degenerate = distance(p0, p1) < NV_EPSILON || distance(p0, p2) < NV_EPSILON || distance(p1, p2) < NV_EPSILON;
  96. + if (degenerate) {
  97. + continue;
  98. + }
  99. + // -- GODOT end --
  100. +
  101. float d = clamp(dot(p0 - p1, p2 - p1) / (length(p0 - p1) * length(p2 - p1)), -1.0f, 1.0f);
  102. float angle = acosf(d);
  103. float area = triangleArea(p0, p1, p2);
  104. @@ -2938,6 +3010,11 @@ Mesh *triangulate(const Mesh *inputMesh)
  105. }
  106. }
  107. }
  108. + // -- GODOT start --
  109. + if (!bestIsValid)
  110. + break;
  111. + // -- GODOT end --
  112. +
  113. xaDebugAssert(minAngle <= 2 * PI);
  114. // Clip best ear:
  115. uint32_t i0 = (bestEar + size - 1) % size;
  116. @@ -5606,7 +5683,10 @@ public:
  117. }
  118. if (chartMeshIndices[vertex->id] == ~0) {
  119. chartMeshIndices[vertex->id] = m_chartMesh->vertexCount();
  120. - m_chartToOriginalMap.push_back(vertex->id);
  121. + // -- GODOT start --
  122. + //m_chartToOriginalMap.push_back(vertex->id);
  123. + m_chartToOriginalMap.push_back(vertex->original_id);
  124. + // -- GODOT end --
  125. m_chartToUnifiedMap.push_back(unifiedMeshIndices[unifiedVertex->id]);
  126. halfedge::Vertex *v = m_chartMesh->addVertex(vertex->pos);
  127. v->nor = vertex->nor;
  128. @@ -5699,7 +5779,10 @@ public:
  129. const halfedge::Vertex *vertex = it.current()->vertex;
  130. if (chartMeshIndices[vertex->id] == ~0) {
  131. chartMeshIndices[vertex->id] = m_chartMesh->vertexCount();
  132. - m_chartToOriginalMap.push_back(vertex->id);
  133. + // -- GODOT start --
  134. + //m_chartToOriginalMap.push_back(vertex->id);
  135. + m_chartToOriginalMap.push_back(vertex->original_id);
  136. + // -- GODOT end --
  137. halfedge::Vertex *v = m_chartMesh->addVertex(vertex->pos);
  138. v->nor = vertex->nor;
  139. v->tex = vertex->tex; // @@ Not necessary.
  140. @@ -7573,6 +7656,14 @@ AddMeshError AddMesh(Atlas *atlas, const InputMesh &mesh, bool useColocalVertice
  141. }
  142. }
  143. internal::halfedge::Face *face = heMesh->addFace(tri[0], tri[1], tri[2]);
  144. +
  145. + // -- GODOT start --
  146. + if (!face && heMesh->errorCode == internal::halfedge::Mesh::ErrorCode::AlreadyAddedEdge) {
  147. + //there is still hope for this, no reason to not add, at least add as separate
  148. + face = heMesh->addUniqueFace(tri[0], tri[1], tri[2]);
  149. + }
  150. + // -- GODOT end --
  151. +
  152. if (!face) {
  153. if (heMesh->errorCode == internal::halfedge::Mesh::ErrorCode::AlreadyAddedEdge)
  154. error.code = AddMeshErrorCode::AlreadyAddedEdge;