No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Refinery.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using Unity.Collections;
  2. using Unity.Mathematics;
  3. namespace UnityEngine.U2D.Common.UTess
  4. {
  5. struct Refinery
  6. {
  7. // Old min and max are 0.5 and 0.05. We pretty much do the same with some relaxing on both ends.
  8. private static readonly float kMinAreaFactor = 0.0482f;
  9. private static readonly float kMaxAreaFactor = 0.4820f;
  10. // After doing more tests with a number of Sprites, this is area to which we can reduce to considering quality and CPU cost.
  11. private static readonly int kMaxSteinerCount = 4084;
  12. // Check if Triangle is Ok.
  13. static bool RequiresRefining(UTriangle tri, float maxArea)
  14. {
  15. // Add any further criteria later on.
  16. return (tri.area > maxArea);
  17. }
  18. static void FetchEncroachedSegments(NativeArray<float2> pgPoints, int pgPointCount, NativeArray<int2> pgEdges, int pgEdgeCount, ref Array<UEncroachingSegment> encroach, ref int encroachCount, UCircle c)
  19. {
  20. for (int i = 0; i < pgEdgeCount; ++i)
  21. {
  22. var edge = pgEdges[i];
  23. var edgeA = pgPoints[edge.x];
  24. var edgeB = pgPoints[edge.y];
  25. // Check if center is along the Edge.
  26. if (!math.any(c.center - edgeA) || !math.any(c.center - edgeB))
  27. continue;
  28. // Get Radius
  29. var edgeD = edgeA - edgeB;
  30. var edgeM = (edgeA + edgeB) * 0.5f;
  31. var edgeR = math.length(edgeD) * 0.5f;
  32. if (math.length(edgeM - c.center) > edgeR)
  33. continue;
  34. UEncroachingSegment es = new UEncroachingSegment();
  35. es.a = edgeA;
  36. es.b = edgeB;
  37. es.index = i;
  38. encroach[encroachCount++] = es;
  39. }
  40. }
  41. static void InsertVertex(ref NativeArray<float2> pgPoints, ref int pgPointCount, float2 newVertex, ref int nid)
  42. {
  43. nid = pgPointCount;
  44. pgPoints[nid] = newVertex;
  45. pgPointCount++;
  46. }
  47. static void SplitSegments(ref NativeArray<float2> pgPoints, ref int pgPointCount, ref NativeArray<int2> pgEdges, ref int pgEdgeCount, UEncroachingSegment es)
  48. {
  49. var sid = es.index;
  50. var edge = pgEdges[sid];
  51. var edgeA = pgPoints[edge.x];
  52. var edgeB = pgPoints[edge.y];
  53. var split = (edgeA + edgeB) * 0.5f;
  54. var neid = 0;
  55. if (math.abs(edge.x - edge.y) == 1)
  56. {
  57. neid = (edge.x > edge.y) ? edge.x : edge.y;
  58. InsertVertex(ref pgPoints, ref pgPointCount, split, ref neid);
  59. // Add the split segments.
  60. var rep = pgEdges[sid];
  61. pgEdges[sid] = new int2(rep.x, neid);
  62. for (int i = pgEdgeCount; i > (sid + 1); --i)
  63. pgEdges[i] = pgEdges[i - 1];
  64. pgEdges[sid + 1] = new int2(neid, rep.y);
  65. pgEdgeCount++;
  66. }
  67. else
  68. {
  69. neid = pgPointCount;
  70. pgPoints[pgPointCount++] = split;
  71. pgEdges[sid] = new int2(math.max(edge.x, edge.y), neid);
  72. pgEdges[pgEdgeCount++] = new int2(math.min(edge.x, edge.y), neid);
  73. }
  74. }
  75. internal static bool Condition(Allocator allocator, float factorArea, float targetArea, ref NativeArray<float2> pgPoints, ref int pgPointCount, ref NativeArray<int2> pgEdges, ref int pgEdgeCount, ref NativeArray<float2> vertices, ref int vertexCount, ref NativeArray<int> indices, ref int indexCount, ref float maxArea)
  76. {
  77. // Process Triangles.
  78. maxArea = 0.0f;
  79. var minArea = 0.0f;
  80. var avgArea = 0.0f;
  81. var refined = false;
  82. var validGraph = true;
  83. // Temporary Stuffs.
  84. int triangleCount = 0, invalidTriangle = -1, inputPointCount = pgPointCount;
  85. var encroach = new Array<UEncroachingSegment>(inputPointCount, ModuleHandle.kMaxEdgeCount, allocator, NativeArrayOptions.UninitializedMemory);
  86. var triangles = new Array<UTriangle>(inputPointCount * 4, ModuleHandle.kMaxTriangleCount, allocator, NativeArrayOptions.UninitializedMemory);
  87. ModuleHandle.BuildTriangles(vertices, vertexCount, indices, indexCount, ref triangles, ref triangleCount, ref maxArea, ref avgArea, ref minArea);
  88. factorArea = factorArea != 0 ? math.clamp(factorArea, kMinAreaFactor, kMaxAreaFactor) : factorArea;
  89. var criArea = maxArea * factorArea;
  90. criArea = math.max(criArea, targetArea);
  91. // Refine
  92. while (!refined && validGraph)
  93. {
  94. // Check if any of the Triangle is Invalid or Segment is invalid. If yes, Refine.
  95. for (int i = 0; i < triangleCount; ++i)
  96. {
  97. if (RequiresRefining(triangles[i], criArea))
  98. {
  99. invalidTriangle = i;
  100. break;
  101. }
  102. }
  103. // Find any Segment that can be Split based on the Input Length.
  104. // todo.
  105. if (invalidTriangle != -1)
  106. {
  107. // Get all Segments that are encroached.
  108. var t = triangles[invalidTriangle];
  109. var encroachCount = 0;
  110. FetchEncroachedSegments(pgPoints, pgPointCount, pgEdges, pgEdgeCount, ref encroach, ref encroachCount, t.c);
  111. // Split each Encroached Segments. If no segments are encroached. Split the Triangle.
  112. if (encroachCount != 0)
  113. {
  114. for (int i = 0; i < encroachCount; ++i)
  115. {
  116. SplitSegments(ref pgPoints, ref pgPointCount, ref pgEdges, ref pgEdgeCount, encroach[i]);
  117. }
  118. }
  119. else
  120. {
  121. // Update Triangulation.
  122. var split = t.c.center;
  123. pgPoints[pgPointCount++] = split;
  124. }
  125. // Tessellate again.
  126. indexCount = 0; vertexCount = 0;
  127. validGraph = Tessellator.Tessellate(allocator, pgPoints, pgPointCount, pgEdges, pgEdgeCount, ref vertices, ref vertexCount, ref indices, ref indexCount);
  128. // Build Internal Triangles.
  129. encroachCount = 0; triangleCount = 0; invalidTriangle = -1;
  130. if (validGraph)
  131. ModuleHandle.BuildTriangles(vertices, vertexCount, indices, indexCount, ref triangles, ref triangleCount, ref maxArea, ref avgArea, ref minArea);
  132. // More than enough Steiner points inserted. This handles all sort of weird input sprites very well (even random point cloud).
  133. if (pgPointCount - inputPointCount > kMaxSteinerCount)
  134. break;
  135. }
  136. else
  137. {
  138. refined = true;
  139. }
  140. }
  141. // Dispose off
  142. triangles.Dispose();
  143. encroach.Dispose();
  144. return refined;
  145. }
  146. }
  147. }