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

Resolved NativeArray<>.Copy corrupts Data

Discussion in 'Entity Component System' started by HellGate94, Jan 28, 2021.

  1. HellGate94

    HellGate94

    Joined:
    Sep 21, 2017
    Posts:
    132
    I tried using the 2020.1 Mesh Api to create meshes and was wondering why the output is corrupt. after some digging i found that NativeArray Copy is the reason for this.

    Code (CSharp):
    1.  
    2.                 meshdata.SetVertexBufferParams(vertexCount, MemoryLayout);
    3.                 meshdata.SetIndexBufferParams(vertexCount, IndexFormat.UInt16);
    4.  
    5.                 var outputVertices = meshdata.GetVertexData<VertexData>();
    6.                 var outputIndices = meshdata.GetIndexData<ushort>();
    7.  
    8.                 // Working Data
    9.                 for (int i = 0; i < vertexCount; i++) {
    10.                     outputVertices[i] = VertexDatas[i];
    11.                     outputIndices[i] = Triangles[i];
    12.                 }
    13.  
    14.                 // Corrupt Data
    15.                 NativeArray<VertexData>.Copy(VertexDatas, outputVertices, vertexCount);
    16.                 NativeArray<ushort>.Copy(Triangles, outputIndices, vertexCount);
    using the Copy i get errors like

    ArgumentException: Index buffer element #835 (value 16518) is out of bounds; mesh only has 8259 vertices.
    UnityEngine.Mesh+MeshDataArray.ApplyToMeshAndDispose (UnityEngine.Mesh mesh, UnityEngine.Rendering.MeshUpdateFlags flags)
     
    Last edited: Jan 28, 2021
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    558
    you are passing outputIndices twice instead of Triangles?
     
  3. HellGate94

    HellGate94

    Joined:
    Sep 21, 2017
    Posts:
    132
    the fact i checked it multiple times and tried alternative without noticing this simple issue is more than just embarrassing... need to work on my focus

    anyway thanks for the help