Нема описа
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.

MeshUtilities.cs 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections.Generic;
  2. using Unity.Mathematics;
  3. namespace UnityEngine.U2D.Animation
  4. {
  5. internal static class MeshUtilities
  6. {
  7. /// <summary>
  8. /// Get the outline edges from a set of indices.
  9. /// This method expects the index array to be laid out with one triangle for every 3 indices.
  10. /// E.g. triangle 0: index 0 - 2, triangle 1: index 3 - 5, etc.
  11. /// </summary>
  12. public static int2[] GetOutlineEdges(in int[] indices)
  13. {
  14. var edges = new Dictionary<int, int3>(indices.Length / 3);
  15. for (var i = 0; i < indices.Length; i += 3)
  16. {
  17. var i0 = indices[i];
  18. var i1 = indices[i + 1];
  19. var i2 = indices[i + 2];
  20. var edge0 = new int2(i0, i1);
  21. var edge1 = new int2(i1, i2);
  22. var edge2 = new int2(i2, i0);
  23. AddToEdgeMap(edge0, ref edges);
  24. AddToEdgeMap(edge1, ref edges);
  25. AddToEdgeMap(edge2, ref edges);
  26. }
  27. var outlineEdges = new List<int2>(edges.Count);
  28. foreach(var edgePair in edges)
  29. {
  30. // If an edge is only used in one triangle, it is an outline edge.
  31. if (edgePair.Value.z == 1)
  32. outlineEdges.Add(edgePair.Value.xy);
  33. }
  34. return outlineEdges.ToArray();
  35. }
  36. static void AddToEdgeMap(int2 edge, ref Dictionary<int, int3> edgeMap)
  37. {
  38. var tmpEdge = math.min(edge.x, edge.y) == edge.x ? edge.xy : edge.yx;
  39. var hashCode = tmpEdge.GetHashCode();
  40. // We store the hashCode as key, so that we can do less GetHashCode-calls.
  41. // Then we store the count the int3s z-value.
  42. if (!edgeMap.ContainsKey(hashCode))
  43. edgeMap.Add(hashCode, new int3(edge, 1));
  44. else
  45. {
  46. var val = edgeMap[hashCode];
  47. val.z++;
  48. edgeMap[hashCode] = val;
  49. }
  50. }
  51. }
  52. }