Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. using System;
  2. using Unity.Collections;
  3. using Unity.Mathematics;
  4.  
  5. namespace AiGame.UnityPhysics
  6. {
  7.  
  8. public class UnityPhysicsMods
  9. {
  10.  
  11. public struct MappedVert
  12. {
  13. public float3 Vert;
  14. public int Index;
  15. }
  16.  
  17. public static NativeArray<float3> WeldVertices(NativeArray<int> indices, NativeArray<float3> vertices)
  18. {
  19. bool hasDups = false;
  20. int uniqueVertIndex = 0;
  21. NativeHashMap<uint, MappedVert> uniqueVertMap = new NativeHashMap<uint, MappedVert>(vertices.Length, Allocator.Temp);
  22. for(int i=0;i<vertices.Length;i++)
  23. {
  24. float3 vert = vertices[i];
  25. uint hash = math.hash(vert);
  26. MappedVert mapped = new MappedVert { Vert = vert, Index = uniqueVertIndex };
  27. if (uniqueVertMap.TryAdd(hash, mapped))
  28. {
  29. uniqueVertIndex++;
  30. } else
  31. {
  32. hasDups = true;
  33. }
  34. }
  35.  
  36. if (!hasDups)
  37. {
  38. return vertices;
  39. }
  40.  
  41. NativeArray<MappedVert> values = uniqueVertMap.GetValueArray(Allocator.Temp);
  42. NativeArray<float3> uniqueVerts = new NativeArray<float3>(values.Length, Allocator.Temp);
  43.  
  44.  
  45. for(int i=0;i<values.Length;i++)
  46. {
  47. MappedVert mapped = values[i];
  48. uniqueVerts[mapped.Index] = mapped.Vert;
  49. }
  50.  
  51. for (int i=0;i<indices.Length;i++)
  52. {
  53. int index = indices[i];
  54. float3 vert = vertices[index];
  55. uint hash = math.hash(vert);
  56. if (!uniqueVertMap.TryGetValue(hash, out MappedVert mapped))
  57. {
  58. throw new ArgumentException("Vert not found");
  59. }
  60.  
  61. indices[i] = mapped.Index;
  62. }
  63.  
  64. return uniqueVerts;
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement