Search Unity

Question ApplyAndDisposeWritableMeshData How to ?

Discussion in 'General Graphics' started by YuriyPopov, Sep 13, 2022.

  1. YuriyPopov

    YuriyPopov

    Joined:
    Sep 5, 2017
    Posts:
    237
    I"m trying to simplify a mesh passed to this method using the new NativeArray api, because I will eventually be doing this in Jobs. I know for a fact that the data returned by the native plugin is correct. I get correct results the old methods such as Mesh.SetTriangles. This is the code I have for NativeArrays:


    Code (CSharp):
    1.   void NativeArrayWay(Mesh mesh)
    2.     {
    3.  
    4.         MeshDataArray dataArray = AcquireReadOnlyMeshData(mesh);
    5.         MeshDataArray w_dataArray = AllocateWritableMeshData(1);
    6.  
    7.  
    8.         MeshData data = w_dataArray[0];
    9.         data.SetIndexBufferParams((int)mesh.GetIndexCount(0), IndexFormat.UInt32);
    10.         data.SetVertexBufferParams(mesh.vertexCount, new VertexAttributeDescriptor(VertexAttribute.Position));
    11.      
    12.         SubMeshDescriptor smd  = mesh.GetSubMesh(0);
    13.         NativeArray<Vector3> verts = new NativeArray<Vector3>(dataArray[0].vertexCount, Allocator.TempJob);
    14.         dataArray[0].GetVertices(verts);
    15.         NativeArray<uint> indicies = dataArray[0].GetIndexData<uint>();
    16.         NativeArray<int> newIndicies = data.GetIndexData<int>();
    17.      
    18.         float threshold = 0.1f;
    19.         ulong target_index_count = (ulong)((indicies.Length) * threshold);
    20.         float target_error = 1e-2f;
    21.         float[] result_error = new float[target_index_count];
    22.         int newCount = 0;
    23.    
    24.         newCount = MeshOptimizerWrapper.SimplifyMesh(newIndicies, indicies, (UInt64)indicies.Length, verts, (UInt64)verts.Length,
    25.                                                         12, target_index_count, target_error, 0, result_error);
    26.  
    27.      
    28.         //Downsize newIndicies to the new count
    29.  
    30.         var newArray = new NativeArray<int>(newCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
    31.         if (newIndicies.IsCreated)
    32.         {
    33.             NativeArray<int>.Copy(newIndicies, newArray, newArray.Length);
    34.             newIndicies.Dispose();
    35.         }
    36.         newIndicies = newArray;
    37.         data.SetIndexBufferParams(newCount, IndexFormat.UInt32);
    38.         data.subMeshCount = 1;
    39.      
    40.         smd.indexCount = newCount;
    41.         smd.indexStart = 0;
    42.         data.SetSubMesh(0, smd, MeshUpdateFlags.DontValidateIndices );
    43.         ApplyAndDisposeWritableMeshData(w_dataArray, mesh, MeshUpdateFlags.DontValidateIndices );
    44.        // mesh.RecalculateNormals();
    45.        // mesh.RecalculateTangents();
    46.         //mesh.RecalculateBounds();
    47.  
    48.    
    49.         verts.Dispose();
    50.      
    51.  
    52.     }
    53.  
    At this point I find the docs lacking, I'm not finding good examples online and have no idea what I'm doing wrong. I want to take in the mesh in this method pass the data to the native plugin, and overwrite the mesh after the simplification has done its job.