Search Unity

  1. The 2021.2 beta is now available. To find out what's new, have a look at our 2021.2 beta blog post.
    Dismiss Notice
  2. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  3. Dismiss Notice

Mesh Built using Native Buffers Dissappearing at Close Distance

Discussion in '5.5 Beta' started by gapMindful, Oct 22, 2016.

  1. gapMindful

    gapMindful

    Joined:
    Jun 26, 2013
    Posts:
    17
    Using the new "GetNativeVertexBufferPtr" and "GetNativeIndexBufferPtr" on mesh. I'm building meshes entirely in native code. However, when I view the meshes at a "close distance" , they disappear depending on the camera angle. Any help on this would be really appreciated, using the native buffers, rebuilding the example below has gone down to ~.81 ms from about ~6.33ms using set vert/tri/normal/uv.

    In the example below, there are actually eight sets of meshes, each one is a block of cubes.

    Up Close GIF:
    https://thumbs.gfycat.com/ThriftyImaginaryGiantschnauzer-size_restricted.gif

    At a Distance:
    https://i.imgur.com/NUnyVIz.png

    Initially I build the mesh with allocated VERT/UV/TRI/NORMAL Buffers
    Code (csharp):
    1.  
    2. builtMesh = new Mesh();
    3. builtMesh.MarkDynamic();
    4. meshFilter.sharedMesh = builtMesh;
    5. currentVertsArray = new Vector3[vertCount];
    6. currentNormalsArray = new Vector3[vertCount];
    7. currentUvArray = new Vector2[vertCount];
    8. currentTrianglesArray = new int[triangleCount];
    9. builtMesh.vertices = currentVertsArray;
    10. builtMesh.uv = currentUvArray;
    11. builtMesh.normals = currentNormalsArray;
    12. builtMesh.triangles = currentTrianglesArray;
    13. VoxelNativeInterface.setMeshBuffer(NativeId, builtMesh.GetNativeVertexBufferPtr(0), builtMesh.GetNativeIndexBufferPtr());
    14.  
    Then later I populate the mesh buffers using lists built in my native plugin
    Code (csharp):
    1.  
    2. auto vertexDataPtr = s_CurrentAPI->BeginModifyVertexBuffer(vertHandle, &vertexSize);
    3. auto indexDataPtr = s_CurrentAPI->BeginModifyVertexBuffer(indexHandle, &indexSize);
    4. auto vertexStride = int(vertexSize / maxVertCount);
    5. auto vertexPtr = static_cast<char*>(vertexDataPtr);
    6. auto indexPtr = static_cast<UINT16*>(indexDataPtr);
    7. for (auto i = 0; i < maxVertCount; ++i) {
    8.     auto& dst = *reinterpret_cast<MeshVertex*>(vertexPtr);
    9.     dst.pos[0] = (*currentVertList)[i].x;
    10.     dst.pos[1] = (*currentVertList)[i].y;
    11.     dst.pos[2] = (*currentVertList)[i].z;
    12.     dst.normal[0] = (*currentNormalList)[i].x;
    13.     dst.normal[1] = (*currentNormalList)[i].y;
    14.     dst.normal[2] = (*currentNormalList)[i].z;
    15.     dst.uv[0] = (*currentUvList)[i].x;
    16.     dst.uv[1] = (*currentUvList)[i].y;
    17.     vertexPtr += vertexStride;
    18. }
    19. for(auto i = 0; i < maxTriCount; ++i){
    20.     indexPtr[i] = static_cast<UINT16>((*currentTriangleList)[i]);
    21. }
    22. s_CurrentAPI->EndModifyVertexBuffer(vertHandle);
    23. s_CurrentAPI->EndModifyVertexBuffer(indexHandle);
    24.  
    Much thanks to @Aras for helping me get this far with the example link from here:
    https://forum.unity3d.com/threads/m...tnativeindexbufferptr-layout-question.430301/
     
  2. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    291
    Are the mesh bounds set correctly?
     
  3. gapMindful

    gapMindful

    Joined:
    Jun 26, 2013
    Posts:
    17
  4. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    291
    That does seem like a culling problem to me, but if you're certain the bounds are set correctly, I don't really know what could be wrong. Sorry!
     
  5. Hyp-X

    Hyp-X

    Joined:
    Jun 24, 2015
    Posts:
    393
    Do you manually calculate the bounds and set it into Mesh.bounds?
    Don't expect Mesh.RecalculateBounds() to work... It doesn't read vertex data from the video memory copy that you updated. (Vertex buffers should be write only in the first place for performance reasons)
     
    mh114 likes this.
  6. gapMindful

    gapMindful

    Joined:
    Jun 26, 2013
    Posts:
    17
    @Hyp-X I'm calculating the bounds in my native code then passing that back off to the script which is setting the Mesh.bounds property. I'm actually using the bounds to create colliders later on, which is whats showing up in the gif linked above. I think that ends up giving a pretty good visual that the bounds are set as expected. Maybe there could possible be a conflict with setting the bounds before the native buffer is technically populated? The call to "requestRender" below actually just schedules the native mesh to be populated later when the plug-in's event handler is invoked later after a call to GL.IssuePluginEvent. But, I am calling all of this in an OnPostRender script on the main camera, so I think it should be happening in the same frame...

    Code (CSharp):
    1. if (builtMesh.vertexBufferCount > 0) {
    2.      VoxelNativeInterface.setMeshBuffer(NativeId, builtMesh.GetNativeVertexBufferPtr(0), builtMesh.GetNativeIndexBufferPtr());
    3.      VoxelNativeInterface.requestRender(NativeId);
    4.      builtMesh.bounds = bounds;
    5. } else if (vertCount == 0) {
    6.      builtMesh.bounds = new Bounds(Vector3.zero, Vector3.zero);
    7. }
     
    pesch2 likes this.
unityunity