Is it on the radar possibly to use the job system for mesh building? Currently one of the biggest slowdowns I have at the moment is creating meshes, I’ve already got the actual vertices/normals/etc being created in background threads but just simply assigning the vertices to a mesh is a pretty intensive act and currently has to happen on the main thread.
Any chance we might see some way of doing this in jobs?
What about the 2019 release? Please make a list of currently available job types.
It’s really hard to find them scattered around forums, github and the official api.
anyways so much thanks for the year 2018 with unity!!!
Huh, I was experimenting with this a while ago. I don’t really know if this approach is valid, but you can certainly write to a managed array in a bursted job if you’re willing to give up all the safety restrictions.
/* system */
Vector3[] vertices = mesh.vertices;
void* pVertices = UnsafeUtility.AddressOf(ref vertices[0]);
int length = vertices.Length;
new SomeJob { pVertices = pVertices }.Schedule(length, 1, inputDeps);
/* job */
[BurstCompile]
unsafe struct SomeJob {
[NativeDisableUnsafePtrRestriction] public void* pVertices;
public void Execute(int index) {
UnsafeUtility.WriteArrayElement(pVertices, index, new float3(1, 1, 1));
}
}
The only weakness of this is that after the job completes you need to reassign the vertices array you wrote to to the mesh. I can’t figure out a way to write directly. This appears to duck the copy, but it weirds me out that this approach requires the reassignment, cause that gives me the impression there are two instances of the underlying array… I am posting with very incomplete knowledge, so discussion and corrections are welcome!
There would be no point to use unsafe pointer if you use mesh.vertices
There was mesh.GetNativeVertexBufferPtr that could return IntPtr to vertex stream. But it was not safe and we hardly know the layout of that struct
What I wish is that unity should have a function that could partially update the vertex buffer. Maybe like this
mesh.SetVertexPositionAt(0,new Vector3(1,0,0));
mesh.SetVertexPositionAt(1,new Vector3(0,1,0));
mesh.SetVertexPositionAt(2,new Vector3(0,0,1));
Or maybe
// IEnumerable<ValueTuple<int,Vector3>>
mesh.SetVertexPositions(new (int,Vector3)[]{
(0,new Vector3(1,0,0)),
(1,new Vector3(0,1,0)),
(2,new Vector3(0,0,1)),
});
Or maybe
mesh.SetVertexPositions(1,new Vector3[]{ // apply from offset 1 of buffer with all 3 elements of array
new Vector3(1,0,0),
new Vector3(0,1,0),
new Vector3(0,0,1),
});
and maybe
mesh.SetVertexPositions(1,0,2,new Vector3[]{ // apply from offset 1 of buffer since index 0 of array by 2 element
new Vector3(1,0,0),
new Vector3(0,1,0),
new Vector3(0,0,1),
});