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. Dismiss Notice

Question How to use AllocateWritableMeshData with existing mesh

Discussion in 'General Graphics' started by jbooth, Apr 20, 2023.

  1. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,445
    So, what I want to do is use the new MeshData system to jobify some mesh modification such that I don't need to get the managed arrays from the regular Mesh class. Currently, the mesh class does not take a NativeArray<int> for triangles, so I end up allocating a lot of data just to set that.

    However, I can't really find an easy way to make a new mesh which matches the old mesh. With writeable mesh data's you can pass it the managed mesh and get back a readonly MeshDataArray to work with, but the writeable version doesn't have such a constructor. And copying a mesh successfully using the new API is difficult, because there is no easy way to grab it's AttributeDescriptor list or simply copy the data from the read only mesh.

    What I want:

    Code (CSharp):
    1.  
    2. // get a writeable copy of the mesh data
    3. var meshDataWrite = Mesh.AllocateWritableMeshData(mf.sharedMesh);
    4.  
    But there seems no easy way to do this, other than querrying the readable version and copying all the data to it, making sure to match all possible format combinations. Am I just missing something obvious?
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    549
    There is Mesh.MeshData.HasVertexAttribute, Mesh.MeshData.GetVertexAttributeFormat, and Mesh.MeshData.GetVertexAttributeDimension but I also don't see an easy way to do this without creating structs for all possible permutations of vertex attributes.

    Usually, you'll only need a few different vertex variations in a project but I doubt it's possible to make this generic (unless you can declare a new struct via reflection?).
     
  3. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,445
    Yeah, I saw those, but it ends up being a ton of possible combinations when being able to simply grab the vertex desc from the mesh would make this easy, much like you can get the RenderTexture descriptor from a render texture. And once you can do that, copying the data would be easy as well. This just seems like an obvious use case "I want to make a copy of a mesh to modify" that I keep feeling like their must be something I'm missing.
     
  4. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    549
    I doubt there is anything you are missing but if anybody who knows better, I'd like to know too.

    I'm wondering if you could hack it by using a bunch of structs of different sizes (multiples of sizeof(float)) and just do an opaque copy. Still a lot of work but would reduce the number of combinations to maybe 16 or so.
     
    Last edited: Apr 20, 2023
  5. kdchabuk

    kdchabuk

    Joined:
    Feb 7, 2019
    Posts:
    47
    Just for making a copy, it is not necessary to take into account all the possible combinations, but it does need to be copied over manually.

    Assuming you already have your source mesh, source meshData, and destination meshData:
    Code (CSharp):
    1. var descriptors = new NativeArray<VertexAttributeDescriptors>(srcMesh.GetVertexAttributes(), Allocator.TempJob);
    2. var maxStream = descriptors.Max(x => x.stream) + 1;
    3.  
    4. // Later in a job...
    5. dstMeshData.SetVertexBufferParams(srcMeshData.vertexCount, descriptors);
    6.  
    7. for(int stream = 0; stream < maxStream; stream++)
    8. {
    9.   var dstArray = dstMeshData.GetVertexData<byte>(stream);
    10.   var srcArray = srcMeshData.GetVertexData<byte>(stream);
    11.   NativeArray<byte>.Copy(srcArray, dstArray);
    12. }
    Then copy the index params/buffers in a similar way (SetIndexBufferParams and GetIndexData). Though it does get more complicated to modify the vertex data if don't know the layout of a particular stream.
     
    Last edited: Apr 28, 2023
    kenamis and jbooth like this.