Triangulator.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. /// <summary>
  4. /// This script can be used to split a 2D polygon into triangles.
  5. /// The algorithm supports concave polygons, but not polygons with holes,
  6. /// or multiple polygons at once.
  7. /// Taken from <see cref="http://wiki.unity3d.com/index.php?title=Triangulator"/>
  8. /// </summary>
  9. public class Triangulator
  10. {
  11. private readonly List<Vector2> _mPoints;
  12. public Triangulator(IEnumerable<Vector2> points)
  13. {
  14. _mPoints = new List<Vector2>(points);
  15. }
  16. public int[] Triangulate()
  17. {
  18. var indices = new List<int>();
  19. var n = _mPoints.Count;
  20. if (n < 3)
  21. return indices.ToArray();
  22. var V = new int[n];
  23. if (Area() > 0) {
  24. for (var v = 0; v < n; v++)
  25. V[v] = v;
  26. } else {
  27. for (var v = 0; v < n; v++)
  28. V[v] = n - 1 - v;
  29. }
  30. var nv = n;
  31. var count = 2 * nv;
  32. for (int m = 0, v = nv - 1; nv > 2;) {
  33. if (count-- <= 0)
  34. return indices.ToArray();
  35. var u = v;
  36. if (nv <= u)
  37. u = 0;
  38. v = u + 1;
  39. if (nv <= v)
  40. v = 0;
  41. var w = v + 1;
  42. if (nv <= w)
  43. w = 0;
  44. if (Snip(u, v, w, nv, V)) {
  45. int a, b, c, s, t;
  46. a = V[u];
  47. b = V[v];
  48. c = V[w];
  49. indices.Add(a);
  50. indices.Add(b);
  51. indices.Add(c);
  52. m++;
  53. for (s = v, t = v + 1; t < nv; s++, t++)
  54. V[s] = V[t];
  55. nv--;
  56. count = 2 * nv;
  57. }
  58. }
  59. indices.Reverse();
  60. return indices.ToArray();
  61. }
  62. private float Area()
  63. {
  64. var n = _mPoints.Count;
  65. var A = 0.0f;
  66. for (int p = n - 1, q = 0; q < n; p = q++) {
  67. var pval = _mPoints[p];
  68. var qval = _mPoints[q];
  69. A += pval.x * qval.y - qval.x * pval.y;
  70. }
  71. return A * 0.5f;
  72. }
  73. private bool Snip(int u, int v, int w, int n, int[] V)
  74. {
  75. int p;
  76. var A = _mPoints[V[u]];
  77. var B = _mPoints[V[v]];
  78. var C = _mPoints[V[w]];
  79. if (Mathf.Epsilon > (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x))
  80. return false;
  81. for (p = 0; p < n; p++) {
  82. if (p == u || p == v || p == w)
  83. continue;
  84. var P = _mPoints[V[p]];
  85. if (InsideTriangle(A, B, C, P))
  86. return false;
  87. }
  88. return true;
  89. }
  90. private static bool InsideTriangle(Vector2 A, Vector2 B, Vector2 C, Vector2 P)
  91. {
  92. float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
  93. float cCROSSap, bCROSScp, aCROSSbp;
  94. ax = C.x - B.x;
  95. ay = C.y - B.y;
  96. bx = A.x - C.x;
  97. by = A.y - C.y;
  98. cx = B.x - A.x;
  99. cy = B.y - A.y;
  100. apx = P.x - A.x;
  101. apy = P.y - A.y;
  102. bpx = P.x - B.x;
  103. bpy = P.y - B.y;
  104. cpx = P.x - C.x;
  105. cpy = P.y - C.y;
  106. aCROSSbp = ax * bpy - ay * bpx;
  107. cCROSSap = cx * apy - cy * apx;
  108. bCROSScp = bx * cpy - by * cpx;
  109. return aCROSSbp >= 0.0f && bCROSScp >= 0.0f && cCROSSap >= 0.0f;
  110. }
  111. }