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

Question [Physics.PhysX] TriangleMesh::loadFromDesc: desc.isValid() failed!

Discussion in 'Scripting' started by robbiemaldrich, Sep 4, 2020.

  1. robbiemaldrich

    robbiemaldrich

    Joined:
    Jun 18, 2020
    Posts:
    2
    Hello! I'm currently working on a building system for my game that will create a mesh based on an array of scriptable objects. However, when I run it this error appears:

    [Physics.PhysX] TriangleMesh::loadFromDesc: desc.isValid() failed!

    I'm fairly new to unity, and so I am hoping somebody could help me understand it!
    Here is my code:

    Code (CSharp):
    1.  
    2.         Destroy(this.gameObject.GetComponent<MeshCollider>());
    3.         Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
    4.         Mesh newMesh = new Mesh();
    5.  
    6.         //loop to create mesh
    7.         for (int y = 0; y < buildArray.GetLength(0); y++)
    8.         {
    9.             for (int z = 0; z < buildArray.GetLength(1); z++)
    10.             {
    11.                 for (int x = 0; x < buildArray.GetLength(2); x++)
    12.                 {
    13.                     if (buildArray[x, y, z] != null)
    14.                     {
    15.                         Mesh BlockMesh = buildArray[x, y, z].mesh;
    16.                         CombineInstance[] combine = new CombineInstance[1];
    17.                         combine[0].mesh = BlockMesh;
    18.                         newMesh.CombineMeshes(combine, true);
    19.                      
    20.                     }
    21.                 }
    22.             }
    23.         }
    24.         mesh.Clear();
    25.  
    26.         mesh.vertices = newMesh.vertices;
    27.         mesh.triangles = newMesh.triangles;
    28.         mesh.Optimize();
    29.         mesh.RecalculateNormals();
    30.  
    31.         //and the error happens here:
    32.         this.gameObject.AddComponent<MeshCollider>();
    -Thanks!
    -This is also my first time on the forums, so I apologize if my question isn't clear.
     
    Last edited: Sep 4, 2020
    ArshakKroyan likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Find out precisely what line this is happening on by either attaching the debugger or by injecting return statements from the end going up until it stops.

    If it is bad data, steps to debug:

    1. Get this error to fire with the simplest possible geometry.
    2. when it occurs, write the mesh verts and triangles out to a file so you can analyze them

    Keep in mind there are upper limits to mesh size, depending on version of Unity. They are detailed in the documentation somewhere. It used to be 64k verts but I've not paid attention in a while.
     
  3. robbiemaldrich

    robbiemaldrich

    Joined:
    Jun 18, 2020
    Posts:
    2
    Thanks for your suggestions! I replaced the mesh I was using with a cube, and have also figured out that this error only happens if lines 16-18 are run. However it actually triggers on line 32 when the collider is created. I'm just confused about what the error means.
     
    Last edited: Sep 4, 2020
  4. Spemble

    Spemble

    Joined:
    Mar 20, 2016
    Posts:
    7
    @robbiemaldrich Did you figure it out? I have an algorithm that generates meshes - would be nice to have some pointers as to what causes it before I go digging myself.

    Edit: Should have looked around more first.
    https://github.com/atteneder/glTFast/issues/137#issuecomment-816651277

    This was my mesh though:
    verts: (8.0, 0.0, 8.0) (8.0, 0.0, 8.0) (8.0, 0.0, 8.0) (8.0, 0.0, 8.0) (8.0, 0.0, 8.0) (8.0, 0.0, 8.0)
    tris: 0 1 2 3 4 5
    (obviously something went wrong as they are all the same point XD)
     
    Last edited: Aug 27, 2021
  5. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    577
    Hey folks, I wanted to provide some additional context here.

    What you're seeing here is PhysX not being able to create a mesh object from the data we took from the Unity Mesh, somehow.

    Here are links:
    PxTriangleMeshDesc.IsValid implementation
    PxSimpleTriangleMeshDesc.isValid implementation

    Unity does very lightweight processing before passing data over from the mesh to PhysX, namely we mostly remove duplicated vertices. That's a clear path to have an empty mesh in some degenerate cases.

    Hope that helps.
     
    Razmot and DG-Unity-DRM like this.