Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Feature Request Improved Mesh API?

Discussion in 'Graphics Dev Blitz Day 2023 - Q&A' started by MechaWolf99, May 24, 2023.

  1. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    289
    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.
    Code (CSharp):
    1.  
    2. [StructLayout(LayoutKind.Sequential)]
    3. private struct Stream0
    4. {
    5.     public float3 position;
    6.     public float3 normal;
    7.     public float4 tangent;
    8.     public float2 texCoord0;
    9. }
    10.  
    11. [NativeDisableContainerSafetyRestriction]
    12. private NativeArray<Stream0> stream0;
    13.  
    14. [NativeDisableContainerSafetyRestriction]
    15. private NativeArray<int3> triangles;
    16.  
    17. // In a method...
    18. var meshDataArray = Mesh.AllocateWritableMeshData(1);
    19. var meshData = meshDataArray[0];
    20. var descriptor = new NativeArray<VertexAttributeDescriptor>(4, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
    21.          
    22. descriptor[0] = new VertexAttributeDescriptor(VertexAttribute.Position, dimension: 3);
    23. descriptor[1] = new VertexAttributeDescriptor(VertexAttribute.Normal, dimension: 3);
    24. descriptor[2] = new VertexAttributeDescriptor(VertexAttribute.Tangent, dimension: 4);
    25. descriptor[3] = new VertexAttributeDescriptor(VertexAttribute.TexCoord0, dimension: 2);
    26.          
    27. meshData.SetVertexBufferParams(vertexCount, descriptor);
    28. descriptor.Dispose();
    29.  
    30. meshData.SetIndexBufferParams(indexCount, IndexFormat.UInt32);
    31.          
    32. meshData.subMeshCount = 1;
    33. meshData.SetSubMesh(0, new SubMeshDescriptor(0, indexCount) { vertexCount = vertexCount }, MeshUpdateFlags.DontRecalculateBounds | MeshUpdateFlags.DontValidateIndices);
    34.  
    35. stream0 = meshData.GetVertexData<Stream0>();
    36. 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?
     
  2. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    815
    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.
     
  3. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    96
    +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<VectorX> while Set Method accept List array and nativeArray, what is the reason of that?

    And getting total indices count would be nice.
     
  4. Jebtor

    Jebtor

    Unity Technologies

    Joined:
    Apr 18, 2018
    Posts:
    111
    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.
     
  5. Jebtor

    Jebtor

    Unity Technologies

    Joined:
    Apr 18, 2018
    Posts:
    111
    @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?
     
  6. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    815
    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.
     
  7. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    289
    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.

    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. :)

    Ahh, no looks like I missed that one!
     
  8. Anne_Daney

    Anne_Daney

    Unity Technologies

    Joined:
    Aug 30, 2022
    Posts:
    19
    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.
     
    MechaWolf99 likes this.
  9. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    289
    I submitted a couple! I hope they are helpful, and looking forward to future Mesh improvements! :D

    If you do reopen the survey, I will definitely fill it out, if I see it!
     
    Jebtor and Anne_Daney like this.
  10. KamilVDono

    KamilVDono

    Joined:
    Jun 2, 2016
    Posts:
    10
    Hey,

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

    Jebtor

    Unity Technologies

    Joined:
    Apr 18, 2018
    Posts:
    111
    @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?
     
    TJHeuvel-net likes this.
  12. Anne_Daney

    Anne_Daney

    Unity Technologies

    Joined:
    Aug 30, 2022
    Posts:
    19
    Jebtor likes this.
  13. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    289
    Anne_Daney likes this.