Build Meshes with the Job System?

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?

2 Likes

Sure but you could do it right now using compute shader

The problem with the compute shader, is my target ATM includes iOS/Android devices that don’t support the compute shader.

There’s already a thread about this topic.

1 Like

@LeonhardP That thread is about drawing meshes, this is for building a mesh.

5 Likes

Hello z000z,

This is a feature that is on our list to do, but it will not be ready for the 2018.1 release.

6 Likes

@MartinGram thanks! that’s great to hear.

iOS supports compute, only android does not :wink:

What about animations? Making AnimationClip is even slower than creating meshes. I hope this is also on your list.

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!!!

1 Like

@MartinGram any update on this? Should I be able to use GetNativeVertexBufferPtr? I tried for a while but only got editor crashes for my troubles.

1 Like

Interested in this, any update?

1 Like

Mesh building via Jobs would also be at the top of my wishlist regarding ECS API.

Any ETA?

1 Like

Yes, ETA?

3 Likes

Will we have a way to write mesh vertex data directly without copying whole array? Especially writing just some parts of vertices, not all of it

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!

the first line copies the native array to managed, which is why you need to write it back, we want to write the native copy directly

2 Likes

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),
});
1 Like

Yes please, we need this as well.

Was there any progress towards this feature in the recent Unity versions?