Search Unity

While updating a mesh, failure in setting triangles?

Discussion in 'Editor & General Support' started by karsnen, Feb 1, 2019.

  1. karsnen

    karsnen

    Joined:
    Feb 9, 2014
    Posts:
    65
    Scenario
    I receive mesh data from Apple's ARKit which I use to update meshes. Sometimes (not very often) I receive as follows.

    Error Message

    Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 45, VertexCount: 17
    UnityEngine.Mesh:set_triangles(Int32[])\
    Call.PlaneMesh:UpdateMesh(ARPlaneAnchor)

    Explanation with code

    The mesh has a material & a line renderer to outline the mesh (which do not cause any problems).

    ARPlaneAnchor
    The parameter that has been passed into that method. ARPlaneAnchor.planeGeometry is an extension of Apple's Arkit planeGeomtry property -> https://developer.apple.com/documentation/arkit/arplanegeometry

    PlaneMesh.cs
    It holds the mesh property & it is where it is being initialized & updated as required. From the values, I receive I create the mesh in runtime.

    Code (CSharp):
    1. internal sealed class PlaneMesh : MonoBehaviour
    2. {
    3.     [SerializeField] internal MeshFilter MyMeshFilter;
    4.     [SerializeField] internal LineRenderer MyLineRenderer;
    5.     [SerializeField] internal BoxCollider PlaneCollider;
    6.     [SerializeField] internal PlaneSettings MySettings;
    7.  
    8.     private Mesh thisMesh;
    9.     // It always initializes before updating. No doubt about it
    10.     internal void InitiliazeMesh(ARPlaneAnchor arPlaneAnchor)
    11.     {
    12.         thisMesh = new Mesh();
    13.         UpdateMesh(arPlaneAnchor);
    14.         MyMeshFilter.mesh = thisMesh;
    15.  
    16.     }
    17.     // this is the method that is called in the error message
    18.     internal void UpdateMesh(ARPlaneAnchor _arPlaneAnchor)
    19.     {
    20.         // Can only access planeGeometry if ARKit 1.5.
    21.         if (_arPlaneAnchor.planeGeometry.vertices.Length != thisMesh.vertices.Length ||
    22.             _arPlaneAnchor.planeGeometry.textureCoordinates.Length != thisMesh.uv.Length ||
    23.             _arPlaneAnchor.planeGeometry.triangleIndices.Length != thisMesh.triangles.Length)
    24.         {   thisMesh.Clear();}
    25.      
    26.         thisMesh.vertices = _arPlaneAnchor.planeGeometry.vertices;
    27.         thisMesh.uv = _arPlaneAnchor.planeGeometry.textureCoordinates;
    28.         thisMesh.triangles = _arPlaneAnchor.planeGeometry.triangleIndices;
    29.      
    30.         MyLineRenderer.positionCount = _arPlaneAnchor.planeGeometry.boundaryVertexCount;
    31.         MyLineRenderer.SetPositions(_arPlaneAnchor.planeGeometry.boundaryVertices);
    32.      
    33.         Vector3 _extent = _arPlaneAnchor.extent;
    34.         if (Mathf.Approximately(_extent.y, 0f) == true)
    35.         { _extent.y = 0.005f; } // Either vertical or horizontal alginment -> y value seems to be '0' always.
    36.      
    37.         PlaneCollider.size = _extent;
    38.         PlaneCollider.center = thisMesh.bounds.center;
    39.      
    40.         // Assign the mesh object and update it.
    41.         thisMesh.RecalculateBounds();
    42.         thisMesh.RecalculateNormals();
    43.      
    44.     }
    45. // probably not needed for the current scenario but just placed it for my ignorance.
    46.     internal void ResetMesh()
    47.     {
    48.         MyMeshFilter.mesh = null;
    49.         thisMesh.Clear();
    50.         thisMesh = null;
    51.         MyLineRenderer.SetPosition(0, Vector3.zero);
    52.         MyLineRenderer.SetPosition(1, Vector3.zero);
    53.         PlaneCollider.center = Vector3.zero;
    54.         PlaneCollider.size = Vector3.one;
    55.     }
    56. }
    57.  
    Request
    As I have limited experience in handling meshes in runtime, I would like to know the following
    1. Why am I getting that error?
    2. What should I do better to resolve it?
    3. Am I following the right way to create/update the mesh in runtime?
    I am intending this as production ready code

    Thank you,
    Karsnen
     
    Last edited: Feb 1, 2019