Skip to content

Instantly share code, notes, and snippets.

@gamemachine
Created September 8, 2019 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamemachine/acfa5d22c770daaaed039d9efe9db23f to your computer and use it in GitHub Desktop.
Save gamemachine/acfa5d22c770daaaed039d9efe9db23f to your computer and use it in GitHub Desktop.
using System;
using Unity.Collections;
using Unity.Mathematics;
namespace AiGame.UnityPhysics
{
public class UnityPhysicsMods
{
public struct MappedVert
{
public float3 Vert;
public int Index;
}
public static NativeArray<float3> WeldVertices(NativeArray<int> indices, NativeArray<float3> vertices)
{
bool hasDups = false;
int uniqueVertIndex = 0;
NativeHashMap<uint, MappedVert> uniqueVertMap = new NativeHashMap<uint, MappedVert>(vertices.Length, Allocator.Temp);
for(int i=0;i<vertices.Length;i++)
{
float3 vert = vertices[i];
uint hash = math.hash(vert);
MappedVert mapped = new MappedVert { Vert = vert, Index = uniqueVertIndex };
if (uniqueVertMap.TryAdd(hash, mapped))
{
uniqueVertIndex++;
} else
{
hasDups = true;
}
}
if (!hasDups)
{
return vertices;
}
NativeArray<MappedVert> values = uniqueVertMap.GetValueArray(Allocator.Temp);
NativeArray<float3> uniqueVerts = new NativeArray<float3>(values.Length, Allocator.Temp);
for(int i=0;i<values.Length;i++)
{
MappedVert mapped = values[i];
uniqueVerts[mapped.Index] = mapped.Vert;
}
for (int i=0;i<indices.Length;i++)
{
int index = indices[i];
float3 vert = vertices[index];
uint hash = math.hash(vert);
if (!uniqueVertMap.TryGetValue(hash, out MappedVert mapped))
{
throw new ArgumentException("Vert not found");
}
indices[i] = mapped.Index;
}
return uniqueVerts;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment