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

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