- Home /
SetTriangles does not accept a NativeArray. Best way around this?
Using unity 2020.3, Burst Compiler and Jobs I'm trying to create a fast mesh generator. Everything works with the following code:
JobHandle createMeshJobHandle = createMeshJob.Schedule();
createMeshJobHandle.Complete();
// Assign
mesh.SetVertices( createMeshJob.vertices );
mesh.triangles = createMeshJob.triangles.ToArray();
mesh.SetColors( createMeshJob.colors );
I don't like that I am converting the createMeshJob.triangles to an array instead o fusing the mesh.triangles setter. How can I use SetTriangles instead without doing the .ToArray()?
Answer by andrew-lukasik · May 29, 2021 at 07:48 PM
It is mesh.SetIndexBufferData you're looking for
( implementation example from Keijiro )
OR
MeshDataArray (similar API).
Main advantage over mesh.SetIndexBufferData
:
So it won't block the main thread when implemented properly (big win in performance departament).Can be
IJob
-ified
Note that when you use the native array methods and in turn SetVertexBufferData, you actually need to manually specify the vertex format. Usually the Mesh class splits the different vertex attributes into seperate arrays while on the GPU the vertex array contains "vertices" interleaved. So the different vertex attributes of a single vertex are grouped together in memory. So if you generate vertices with the job system, you should directly generate the actual vertices array with all the attributes you need (position, normals, colors, ....). See SetVertexBufferData for more information.
Your answer
Follow this Question
Related Questions
How do I make multiple meshes in the same script? 1 Answer
Mesh creation by code not working? 0 Answers
Mesh generated from bezier curve loop going outside loop 0 Answers
How can I generate a up facing quad mesh with variable resolution? 0 Answers
How can I generate a up facing quad mesh with adjustable res? 0 Answers