Improved Mesh API?

Hello, I was wondering if there was any plans for improving working with the mesh API?

2019, 2020, and 2021 (iirc) had some really good updates to the API (mostly done by Aras I think). The updates allowed you to use jobs and even compute shaders to generate meshes extremely fast. Which are great improvements!

But right now actually doing it is kind of a pain. There is a lot of boilerplate required to even start, and (at least for me) a lot of code comments to keep things straight when actually generating a mesh. This is somewhat unavoidable, given its nature, but it could definitely be improved.

As an example, here is a snippet of the code to just setup for generation a mesh. (Edited version of what Catlike Coding shows in their post about mesh generation

As you can see, it is a lot. And it is just boilerplate type of stuff.

[StructLayout(LayoutKind.Sequential)]
private struct Stream0
{
    public float3 position;
    public float3 normal;
    public float4 tangent;
    public float2 texCoord0;
}

[NativeDisableContainerSafetyRestriction]
private NativeArray<Stream0> stream0;

[NativeDisableContainerSafetyRestriction]
private NativeArray<int3> triangles;

// In a method...
var meshDataArray = Mesh.AllocateWritableMeshData(1);
var meshData = meshDataArray[0];
var descriptor = new NativeArray<VertexAttributeDescriptor>(4, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

descriptor[0] = new VertexAttributeDescriptor(VertexAttribute.Position, dimension: 3);
descriptor[1] = new VertexAttributeDescriptor(VertexAttribute.Normal, dimension: 3);
descriptor[2] = new VertexAttributeDescriptor(VertexAttribute.Tangent, dimension: 4);
descriptor[3] = new VertexAttributeDescriptor(VertexAttribute.TexCoord0, dimension: 2);

meshData.SetVertexBufferParams(vertexCount, descriptor);
descriptor.Dispose();

meshData.SetIndexBufferParams(indexCount, IndexFormat.UInt32);

meshData.subMeshCount = 1;
meshData.SetSubMesh(0, new SubMeshDescriptor(0, indexCount) { vertexCount = vertexCount }, MeshUpdateFlags.DontRecalculateBounds | MeshUpdateFlags.DontValidateIndices);

stream0 = meshData.GetVertexData<Stream0>();
triangles = meshData.GetIndexData<int>().Reinterpret<int3>(4);

So setting it up is cumbersome, and then using it is as well. Some things I find difficult is getting the right index count for the vertex count, getting and setting the correct indicies and making sure not to go over. Debugging when I do something wrong with the index or vertex counts or indices. And generally just setting up the indices correct for each vertex.

Again, I know some of this is unavoidable, but are there any plans to improve this process?

4 Likes

Hope you dont mind if i add a usecase i'd like to see. I'd like to make a Mesh using my custom provided Index and Vertex buffers. That way i can copy and merge meshes on the gpu, and they dont have to be readable.

+1
I feel like, documentation expects readers write their own graphics library on the past. It would be very hard to use Mesh api for me if Catlike' s tutorials not exist.

If its main usage is jobs, then why Get() methods return Vector types instead of math types anyway?

Also on standard mesh api, Get methods can return only List while Set Method accept List array and nativeArray, what is the reason of that?

And getting total indices count would be nice.

Hey! We have ideas we’d like to explore but no concrete work has been scheduled or committed to. The updates you are referring to were done in 2019.3, 2020.1 and 2021.2. The scope of these improvements has been “what already exists in the underlying engine, but has not been exposed to C# yet”. We are aware this has resulted in having to write quite a lot of code for sometimes relatively simple scenarios. As you mention some of this is likely unavoidable but we will definitely keep our eyes open for improvements if we revisit the API here.

Please submit a new idea on our roadmap here if you'd like to see us prioritize revisiting and extending this API. Please provide as much details as possible, also link to this thread and other samples you might have. More guidelines for the roadmap page can be found here .

Finally, I wanted to check if you were able to fill in the Mesh survey when it was open. It has been closed now but would like to get an idea of how many people here have answered the survey.

@TJHeuvel-net , to make sure I understand the request fully. The supplied buffers would be present on the CPU or come in the form of GraphicsBuffers on the GPU?


Graphicsbuffers, like the ones returned by the GetIndexBuffer from the Mesh api.

Basically i want to merge meshes on the GPU and then use these in RenderMesh methods, which expects a Mesh.

[quote=“Jebtor”, post:4, topic: 918753]
Hey! We have ideas we’d like to explore but no concrete work has been scheduled or committed to. The updates you are referring to were done in 2019.3, 2020.1 and 2021.2. The scope of these improvements has been “what already exists in the underlying engine, but has not been exposed to C# yet”. We are aware this has resulted in having to write quite a lot of code for sometimes relatively simple scenarios. As you mention some of this is likely unavoidable but we will definitely keep our eyes open for improvements if we revisit the API here.
[/quote]
Hi! Thank you for the reply! Glad to hear you are aware of the API situation. Yeah I remember the changes be mostly about just exposing stuff that was already there. Great changes non the less.

[quote=“Jebtor”, post:4, topic: 918753]
Please submit a new idea on our roadmap here if you’d like to see us prioritize revisiting and extending this API. Please provide as much details as possible, also link to this thread and other samples you might have. More guidelines for the roadmap page can be found here .
[/quote]
Okay, I will submit some new ideas to the roadmap, is it better to try to separate something like this in to multiple ideas, or just one big one? Or does it matter? Just want to be as easy and helpful for you all as I can make it. :slight_smile:

[quote=“Jebtor”, post:4, topic: 918753]
Finally, I wanted to check if you were able to fill in the Mesh survey when it was open. It has been closed now but would like to get an idea of how many people here have answered the survey.
[/quote]
Ahh, no looks like I missed that one!

Hi MechaWolf99,

Thank you for your consideration :)
You can split your feedback by theme but you can also create 1 big feedback if it's easier for you.
We can easily link 1 feedback to multiple feature request.

About the survey, we are thinking of reopening it. Other people have missed it too. I'll post an update in this Thread if it re-opens.

1 Like


I submitted a couple! I hope they are helpful, and looking forward to future Mesh improvements! :smile:

If you do reopen the survey, I will definitely fill it out, if I see it!

2 Likes

Hey,

Reopen survey sounds great. Also @TJHeuvel-net idea about constructing mesh from existing buffer would be super awesome to have.

1 Like

@TJHeuvel-net thanks for the clarification! Please file a product request with as much detail as possible if you'd like us to consider this feature.

A follow up question I have is whether this feature is a means to an end. Lets say the RenderMesh API would accept GraphicsBuffer(s) plus a descriptor. Would this be more useful? Would that make the feature obsolete?

1 Like

Hey everyone!
The mesh survey has been reopened.

Here a reminder of the link to the survey:
https://unitysoftware.co1.qualtrics.com/jfe/form/SV_56JZ0M9rCLeIndA

1 Like

[quote=“Anne_Daney”, post:12, topic: 918753]
Hey everyone!
The mesh survey has been reopened.

Here a reminder of the link to the survey:
https://unitysoftware.co1.qualtrics.com/jfe/form/SV_56JZ0M9rCLeIndA
[/quote]
Thanks! Filled it out!

1 Like