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

New Mesh() VBO returns null???

Discussion in 'Scripting' started by garryguan99, Jul 11, 2018.

  1. garryguan99

    garryguan99

    Joined:
    Apr 4, 2017
    Posts:
    3
    What up Unity forum.

    I create new Meshes and assign it to a game object I instantiate at runtime.
    Problem is when I use glMapBuffer in my native plugin, it returns a NULL value and I can't update it.

    Code (CSharp):
    1.  int[] vertSizes = new int[activeChunks];
    2.         int[] triSizes = new int[activeChunks];
    3.         VoxelNativeInterface.GetChunkMeshSizes(vertSizes, triSizes);
    4.  
    5.         uint[] chunkVBOs = new uint[activeChunks];
    6.         uint[] chunkEBOs = new uint[activeChunks];
    7.  
    8.         int i = 0;
    9.         Debug.Log("Active chunks " + activeChunks);
    10.         foreach (GameObject chunk in chunks)
    11.         {
    12.             var filter = chunk.GetComponent<MeshFilter>();
    13.             var mesh = new Mesh();
    14.             mesh.MarkDynamic();
    15.             filter.sharedMesh = mesh;
    16.  
    17.             Debug.Log("i " + i);
    18.             Debug.Log("Vert sizes " + vertSizes[i]);
    19.             Debug.Log("tri sizes " + triSizes[i]);
    20.  
    21.             //Vector3[] newVecs = new Vector3[vertSizes[i]];
    22.             //for (int j = 0; j < vertSizes[i]; j++)
    23.             //    newVecs[j] = Vector3.zero;
    24.             mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
    25.             mesh.vertices = new Vector3[vertSizes[i]];
    26.             mesh.normals = new Vector3[vertSizes[i]];
    27.             mesh.uv = new Vector2[0];
    28.             mesh.triangles = new int[triSizes[i]];
    29.  
    30.             mesh.UploadMeshData(true);
    31.             chunkVBOs[i] = (uint)mesh.GetNativeVertexBufferPtr(0);
    32.             chunkEBOs[i] = (uint)mesh.GetNativeIndexBufferPtr();
    33.  
    34.             Debug.Log("mesh vertexBuffer count " + mesh.vertexBufferCount);
    35.             Debug.Log("mesh vertex count " + mesh.vertexCount);
    36.             Debug.Log("mesh indices count " + mesh.GetIndexCount(0));
    37.  
    38.             Debug.Log("buffer to int: " + (uint)mesh.GetNativeVertexBufferPtr(0));
    39.  
    40.             i++;
    41.         }
    42.         for (int j = 0; j < i; j++)
    43.         {
    44.             Debug.Log("ChunkVBO [" + j + "] " + (uint)chunkVBOs[j]);
    45.             Debug.Log("ChunkEBO [" + j + "] " + (uint)chunkEBOs[j]);
    46.         }
    47.         Debug.Log("Binding chunks");
    48.         //GL.IssuePluginEvent(VoxelNativeInterface.BindChunks(activeChunks, chunkVBOs, chunkEBOs), 1);
    49.         VoxelNativeInterface.BindChunks(activeChunks, chunkVBOs, chunkEBOs);
    Native C++
    Code (Boo):
    1.  
    2.     GLint bufferSize = 0;
    3.     void *vboDataPtr = BeginModifyVertexBuffer(bufferSize);
    4.  
    5.     LogToUnity("BufferSize: " + to_string(bufferSize));
    6.     LogToUnity("VBO: " + to_string(m_vbo) + ", EBO: " + to_string(m_ebo));
    7.     if (!vboDataPtr)
    8.     {
    9.         LogToUnity("VBO IS NULLL!");
    10.         return;
    11.     }
    12.  
    13.     int vertexStride = bufferSize / m_vertices.size();
    14.     char *bufferPtr = (char *)vboDataPtr;
    15.     Vertex *vertexPtr = (Vertex*)bufferPtr;
    16.  
    17.     for (int i = 0; i < m_vertices.size(); i++)
    18.     {
    19.         const VoxelVertex &src = m_vertices[i];
    20.         Vertex &dst = *(Vertex*)bufferPtr;
    21.  
    22.         dst.pos.x = src.position.x;
    23.         dst.pos.y = src.position.y;
    24.         dst.pos.z = src.position.z;
    25.         dst.normal.x = src.normal.x;
    26.         dst.normal.y = src.normal.y;
    27.         dst.normal.z = src.normal.z;
    28.         bufferPtr += vertexStride;
    29.     }
    30.  
    31.     string msg = "VBO buffer size: " + to_string(bufferSize);
    32.     LogToUnity(msg);
    33.     EndModifyVertexBuffer();
     
  2. dcecar

    dcecar

    Joined:
    Dec 7, 2016
    Posts:
    6
    did you find out what the problem was?