I am using Job system to run over 64k vertices of a mesh. I am trying to get a NativeHashMap array with the modified vertices. The thing is that my vertices are modifying only once, and thats not what i want. Any help would be appreciated. Some code below…
public void ThreadDeform(Vector3 point, float force, float radius, float depth)
{
var localPoint = transform.InverseTransformPoint(point);
NativeHashMap<int, float3> modifiedVerts = new NativeHashMap<int, float3>(1000, Allocator.TempJob);
MeshDeformerJob job = new MeshDeformerJob
{
point = localPoint,
radius = radius,
force = force,
vertices = nativeVertices,
dirs = nativeDirs,
deltaTime = Time.fixedDeltaTime,
needsUpdate = nativeNeedsUpdate,
modifiedVerts = modifiedVerts.AsParallelWriter()
};
JobHandle jobHandle = job.Schedule<MeshDeformerJob>(nativeVertices.Length, 64);
jobHandle.Complete();
if (job.needsUpdate[0])
{
NativeKeyValueArrays<int, float3> ar = modifiedVerts.GetKeyValueArrays(Allocator.Temp);
for (int i = 0; i < ar.Length; i++)
{
var currentKey = ar.Keys;
var currentValue = ar.Values;
vertices[currentKey] = new Vector3(currentValue.x, currentValue.y, currentValue.z);
Debug.Log($"currentKey {currentKey}, value {currentValue}");
}
ar.Dispose();
}
modifiedVerts.Clear();
modifiedVerts.Dispose();
}
And the Job method…
public struct MeshDeformerJob : IJobParallelFor
{
public float deltaTime;
public float3 point;
public float3 dirPoint;
public float radius;
public float force;
public float depth;
public float3 objPos;
[NativeDisableParallelForRestriction]
public NativeArray<bool> needsUpdate;
public NativeArray<float3> vertices;
[ReadOnly]
public NativeArray<float3> dirs;
public NativeHashMap<int, float3>.ParallelWriter modifiedVerts;
public void Execute(int index)
{
float3 vertex = vertices[index];
float3 dir = dirs[index];
float dist = math.distancesq(vertex, point);
if (dist < radius)
{
Debug.Log("Mesh Deformed");
float3 deform = vertex + force *dir * deltaTime;
modifiedVerts.TryAdd(index, deform);
needsUpdate[0] = true;
}
}
}