NxrTriangulator.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. public class NxrTriangulator
  4. {
  5. private List<Vector2> m_points = new List<Vector2>();
  6. public NxrTriangulator(Vector2[] points)
  7. {
  8. m_points = new List<Vector2>(points);
  9. }
  10. public int[] Triangulate()
  11. {
  12. List<int> indices = new List<int>();
  13. int n = m_points.Count;
  14. if (n < 3)
  15. return indices.ToArray();
  16. int[] V = new int[n];
  17. if (Area() > 0)
  18. {
  19. for (int v = 0; v < n; v++)
  20. V[v] = v;
  21. }
  22. else
  23. {
  24. for (int v = 0; v < n; v++)
  25. V[v] = (n - 1) - v;
  26. }
  27. int nv = n;
  28. int count = 2 * nv;
  29. for (int m = 0, v = nv - 1; nv > 2;)
  30. {
  31. if ((count--) <= 0)
  32. {
  33. if (!IsClockwise(indices[0], indices[1], indices[2]))
  34. {
  35. indices.Reverse();
  36. }
  37. return indices.ToArray();
  38. }
  39. int u = v;
  40. if (nv <= u)
  41. u = 0;
  42. v = u + 1;
  43. if (nv <= v)
  44. v = 0;
  45. int w = v + 1;
  46. if (nv <= w)
  47. w = 0;
  48. if (Snip(u, v, w, nv, V))
  49. {
  50. int a, b, c, s, t;
  51. a = V[u];
  52. b = V[v];
  53. c = V[w];
  54. indices.Add(a);
  55. indices.Add(b);
  56. indices.Add(c);
  57. m++;
  58. for (s = v, t = v + 1; t < nv; s++, t++)
  59. V[s] = V[t];
  60. nv--;
  61. count = 2 * nv;
  62. }
  63. }
  64. // 统一转换为顺时针的顺序
  65. // triangles=0,9,8,8,7,6,3,2,1,1,0,8,3,1,8,3,8,6,
  66. // triangles=0,8,7,7,6,5,5,4,3,3,2,1,1,0,7,7,5,3,3,1,7,
  67. // indices.Reverse();
  68. if(!IsClockwise(indices[0], indices[1], indices[2]))
  69. {
  70. indices.Reverse();
  71. }
  72. return indices.ToArray();
  73. }
  74. bool IsClockwise(int p0, int p1, int p2)
  75. {
  76. //
  77. Vector2 p1Vec = m_points[p1] - m_points[p0];
  78. Vector2 p2Vec = m_points[p2] - m_points[p0];
  79. //V1(x1, y1) X V2(x2, y2) = x1y2 – y1x2
  80. // 结果为正时,p0 - p1 - p2 逆时针,
  81. // 结果为负时,p0 - p1 - p2 走向为顺时针,
  82. float res = p1Vec.x * p2Vec.y - p1Vec.y * p2Vec.x;
  83. return res < 0;
  84. }
  85. private float Area()
  86. {
  87. int n = m_points.Count;
  88. float A = 0.0f;
  89. for (int p = n - 1, q = 0; q < n; p = q++)
  90. {
  91. Vector2 pval = m_points[p];
  92. Vector2 qval = m_points[q];
  93. A += pval.x * qval.y - qval.x * pval.y;
  94. }
  95. return (A * 0.5f);
  96. }
  97. private bool Snip(int u, int v, int w, int n, int[] V)
  98. {
  99. int p;
  100. Vector2 A = m_points[V[u]];
  101. Vector2 B = m_points[V[v]];
  102. Vector2 C = m_points[V[w]];
  103. if (Mathf.Epsilon > (((B.x - A.x) * (C.y - A.y)) - ((B.y - A.y) * (C.x - A.x))))
  104. return false;
  105. for (p = 0; p < n; p++)
  106. {
  107. if ((p == u) || (p == v) || (p == w))
  108. continue;
  109. Vector2 P = m_points[V[p]];
  110. if (InsideTriangle(A, B, C, P))
  111. return false;
  112. }
  113. return true;
  114. }
  115. private bool InsideTriangle(Vector2 A, Vector2 B, Vector2 C, Vector2 P)
  116. {
  117. float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
  118. float cCROSSap, bCROSScp, aCROSSbp;
  119. ax = C.x - B.x; ay = C.y - B.y;
  120. bx = A.x - C.x; by = A.y - C.y;
  121. cx = B.x - A.x; cy = B.y - A.y;
  122. apx = P.x - A.x; apy = P.y - A.y;
  123. bpx = P.x - B.x; bpy = P.y - B.y;
  124. cpx = P.x - C.x; cpy = P.y - C.y;
  125. aCROSSbp = ax * bpy - ay * bpx;
  126. cCROSSap = cx * apy - cy * apx;
  127. bCROSScp = bx * cpy - by * cpx;
  128. return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
  129. }
  130. }