暫無描述
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.

EdgeDictionary.cs 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Collections;
  5. namespace UnityEngine.Rendering.Universal
  6. {
  7. internal struct EdgeDictionary : IEdgeStore
  8. {
  9. static private Dictionary<ShadowEdge, int> m_EdgeDictionary = new Dictionary<ShadowEdge, int>(new EdgeComparer()); // This is done so we don't create garbage allocating and deallocating a dictionary object
  10. private class EdgeComparer : IEqualityComparer<ShadowEdge>
  11. {
  12. public bool Equals(ShadowEdge edge0, ShadowEdge edge1)
  13. {
  14. return (edge0.v0 == edge1.v0 && edge0.v1 == edge1.v1) || (edge0.v1 == edge1.v0 && edge0.v0 == edge1.v1);
  15. }
  16. public int GetHashCode(ShadowEdge edge)
  17. {
  18. int v0 = edge.v0;
  19. int v1 = edge.v1;
  20. if (edge.v1 < edge.v0)
  21. {
  22. v0 = edge.v1;
  23. v1 = edge.v0;
  24. }
  25. int hashCode = v0 << 15 | v1;
  26. return hashCode.GetHashCode();
  27. }
  28. }
  29. public NativeArray<ShadowEdge> GetOutsideEdges(NativeArray<Vector3> vertices, NativeArray<int> indices)
  30. {
  31. m_EdgeDictionary.Clear();
  32. m_EdgeDictionary.EnsureCapacity(indices.Length);
  33. for (int i = 0; i < indices.Length; i += 3)
  34. {
  35. int v0Index = indices[i];
  36. int v1Index = indices[i + 1];
  37. int v2Index = indices[i + 2];
  38. ShadowEdge edge0 = new ShadowEdge(v0Index, v1Index);
  39. ShadowEdge edge1 = new ShadowEdge(v1Index, v2Index);
  40. ShadowEdge edge2 = new ShadowEdge(v2Index, v0Index);
  41. // When a key comparison is made edges (A, B) and (B, A) are equal (see EdgeComparer)
  42. if (m_EdgeDictionary.ContainsKey(edge0))
  43. m_EdgeDictionary[edge0] = m_EdgeDictionary[edge0] + 1;
  44. else
  45. m_EdgeDictionary.Add(edge0, 1);
  46. if (m_EdgeDictionary.ContainsKey(edge1))
  47. m_EdgeDictionary[edge1] = m_EdgeDictionary[edge1] + 1;
  48. else
  49. m_EdgeDictionary.Add(edge1, 1);
  50. if (m_EdgeDictionary.ContainsKey(edge2))
  51. m_EdgeDictionary[edge2] = m_EdgeDictionary[edge2] + 1;
  52. else
  53. m_EdgeDictionary.Add(edge2, 1);
  54. }
  55. // Count the outside edges
  56. int outsideEdges = 0;
  57. foreach (KeyValuePair<ShadowEdge, int> keyValuePair in m_EdgeDictionary)
  58. {
  59. if (keyValuePair.Value == 1)
  60. outsideEdges++;
  61. }
  62. // Create unsorted edges array
  63. int edgeIndex = 0;
  64. NativeArray<ShadowEdge> edges = new NativeArray<ShadowEdge>(outsideEdges, Allocator.Temp);
  65. foreach (KeyValuePair<ShadowEdge, int> keyValuePair in m_EdgeDictionary)
  66. {
  67. if (keyValuePair.Value == 1)
  68. {
  69. edges[edgeIndex++] = keyValuePair.Key;
  70. }
  71. }
  72. return edges;
  73. }
  74. }
  75. }