Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Mesh API - SetIndexBufferData Help

Discussion in 'Graphics Experimental Previews' started by Bodyclock, Sep 9, 2020.

  1. Bodyclock

    Bodyclock

    Joined:
    May 8, 2018
    Posts:
    172
    Hi All,

    I'm currently converting a procedural mesh generation tool to the new API. I'm on 2019 LTS and using the Job System. The SetVertexBufferData is all working fine but SetIndexBufferData is just giving me a mesh with the correct number of vertices but 0 triangles. If I use SetIndices (the commented out code at the bottom) with the same data it works just fine. I thought it might have something to do with .AsArray() so I explicitly created a NativeArray with no change. I'm also using uint as the integer type.
    Am I doing something wrong or is there a bug with SetIndexBufferData?
    Any help would be appreciated.

    Code (CSharp):
    1.         public static void SetMesh(UnityEngine.Mesh mesh, MeshData meshData)
    2.         {
    3.             int vertexCount = meshData.VertexData.Length;
    4.             int indexCount = meshData.Indices.Length;
    5.  
    6.             mesh.Clear();
    7.  
    8.             mesh.SetVertexBufferParams(vertexCount, GetVertexLayout());
    9.             mesh.SetVertexBufferData(meshData.VertexData.AsArray(), 0, 0, vertexCount);
    10.  
    11.             mesh.SetIndexBufferParams(indexCount, IndexFormat.UInt32);
    12.             mesh.SetIndexBufferData(meshData.Indices.AsArray(), 0, 0, indexCount);
    13.  
    14.             //NativeArray<uint> triangles = meshData.Indices.AsArray();
    15.             //mesh.SetIndexBufferData(triangles, 0, 0, indexCount);
    16.  
    17.             //mesh.SetIndices(meshData.Indices.AsArray(), MeshTopology.Triangles, 0, false);
    18.  
    19.             mesh.RecalculateNormals();
    20.             mesh.RecalculateTangents();
    21.             mesh.RecalculateBounds();
    22.         }
     
  2. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,012
  3. Bodyclock

    Bodyclock

    Joined:
    May 8, 2018
    Posts:
    172
    Last edited: Sep 9, 2020
  4. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    397
    Careful with those calls. Recalculate normals and tangents is not done (and I recommend doing it yourself if you can because that is VERY slow). Also, you're settings the vertex buffer and index buffer without the flags so that it doesn't recalculate bounds all the time. It is recalculating them 3 times in your case, I believe.
    You should set the flag to all those methods to not recalculate, and do it at the end, when you call the last SetIndexBufferData without the flag. there's no nee to call recalculate bounds after that.