I have a small IJobParallelFor that I have written that just basically loops through a bunch of triangles, computes it neighbors, then writes that data back to the triangle.
As far as I can tell everything is executing fine, and the correct results are being computed. But the results are not writing back to the triangle.
Here is my code for the job. I don’t think the code for Triangle is relevant but if it is let me know and I can post it or anything additional as requested.
public struct CalculateNeighborsJob : IJobParallelFor
{
public NativeArray<Triangle> triangles;
[Unity.Collections.ReadOnly]
public NativeParallelHashMap<int, UnsafeList<Triangle>> triangleCache;
public void Execute(int index)
{
var poly = triangles[index];
if (poly.HasCheckedNeighbors)
{
return;
}
UnsafeList<Triangle> neighbors = new UnsafeList<Triangle>(12, Allocator.TempJob);
foreach (var vertex in poly.VertexIndices)
{
var sharedTriangles = triangleCache[vertex];
foreach (var otherPoly in sharedTriangles)
{
if (poly.Equals(otherPoly))
continue;
if (poly.IsNeighborOf(otherPoly))
{
neighbors.Add(otherPoly);
}
}
}
poly.neighbors = new UnsafeList<Triangle>(neighbors.Length, Allocator.Persistent);
poly.neighbors.CopyFrom(neighbors);
poly.HasCheckedNeighbors = true;
triangles[index] = poly;
neighbors.Dispose();
}
}
And here is the code that runs the job
private void CalculateNeighborsJob()
{
// setup the data structures needed
var jobTriangles = new NativeList<Triangle>(Polygons.Count, Allocator.TempJob);
foreach (var polygon in Polygons)
{
jobTriangles.Add(polygon);
}
var jobTriangleCache = new NativeParallelHashMap<int, UnsafeList<Triangle>>(triangleCache.Count, Allocator.TempJob);
foreach (var item in triangleCache)
{
var cacheList = new UnsafeList<Triangle>(item.Value.Count, Allocator.TempJob);
foreach (var triangle in item.Value)
{
cacheList.Add(triangle);
}
jobTriangleCache.Add(item.Key, cacheList);
}
// create the job
var job = new CalculateNeighborsJob
{
triangles = jobTriangles,
triangleCache = jobTriangleCache
};
// Schedule the job
var jobHandle = job.Schedule(job.triangles.Length, 1);
// Run the job
jobHandle.Complete();
// Cleanup collections
jobTriangles.Dispose(jobHandle);
foreach (var item in jobTriangleCache)
{
item.Value.Dispose(jobHandle);
}
jobTriangleCache.Dispose(jobHandle);
}