First, an explanation on why I want to do that.
I have a bursted job that iterates through many thousands entities and computes vertices, uvs and triangles and stores them in their respective native arrays. If I use a Complete() statement on this job, it takes ~1.5ms to complete. Without Complete(), it takes almost no time but the job is still getting done! This is the only place in my code that still has a Complete() statement and I want to get rid of it. The job itself can’t be optimized any further either. However, I need to call Complete() so I can set the mesh data from the main thread because neither this
mesh.SetVertices(verticesList);
Nor this
mesh.vertices = vertices;
Works in a thread as both throw a UnityException: get_canAccess can only be called from the main thread. in the mesh class.
mesh.Clear() throws a similar error.
So what I want is to schedule my computation job then create a new job that depends on the former which simply sets the new mesh data onto the mesh, instead of calling Complete() and do so on the main thread.
I looked everywhere on the internet but I couldn’t find any example on how to set mesh data from inside a thread. I’ve tried dabbling with mesh.SetVertexBufferData but I have no idea if that would work inside a thread and I can’t figure it out myself with just the example given in the docs. Plus it looks like that works only for vertices, not triangles and uvs. I’ve also looked at Tertle’s various mesh examples passing dynamic buffers onto the mesh directly, but that has to be done from the main thread as well.
Is setting mesh data from within a job not possible? If it’s not, how can I avoid the Complete() call and somehow have a sort of callback to the job so that I can pass the data onto the mesh on the main thread once the computational job complete? And if it is possible, isn’t there some sort of pointer magic that can be used?