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.

Question How do you generate a NavMesh at runtime with procedural meshes in DOTS?

Discussion in 'Entity Component System' started by morphex, Dec 5, 2022.

  1. morphex

    morphex

    Joined:
    Dec 18, 2012
    Posts:
    99
    Pretty much the title:


    Code (CSharp):
    1.     var navMeshData = new NavMeshData();
    2.         this.bounds = new Bounds(Vector3.zero, new Vector3(50, 20, 50));
    3.         var sources = new List<NavMeshBuildSource>();
    4.         var src = new NavMeshBuildSource();
    5.         src.transform = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, Vector3.one);
    6.         src.shape = NavMeshBuildSourceShape.Mesh;
    7.  
    8.        src.sourceObject =  // Mesh from RenderMesh ???
    9.  
    10.         sources.Add(src);
    I am experimenting with generating runtime navmeshes based on a underlying structure that generates my level. All my level is being created with DOTS entities, and the meshes being RenderMesh classes.

    When I try to assign the sourceObject to a RenderMesh it seems that nothing actually happens on the generated NavMesh. If I use a Shape(such as cube instead) Everything behaves correctly and I get a working Navmesh.

    Whats the currect way of building a NavMesh with custom MeshData?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,120
    I believe MonoBehaviour NM is baked at initialisation.
    So you may not be able to generate it at runtime. Unless someone correct me.

    In that case, you may need other, or custom solution for NM generation at runtime.
    There are at least one or two solutions floating on Unity DOTS forum, for that.
    Srr, don't know links of top of my head.
     
  3. morphex

    morphex

    Joined:
    Dec 18, 2012
    Posts:
    99
    You can generate it at runtime with the new api, and it works great. Its just the custom mesh thats giving me trouble right now.

    I already found Dotsnav which looks nice, I will search for the other one and see what is out there meanwhile! Thanks for your input :)
     
    Antypodish likes this.
  4. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    355
    Code (CSharp):
    1. // First, triangulate the mesh using NavMesh.CalculateTriangulation
    2. var mesh = new Mesh();
    3. // Assign vertices, triangles, and other mesh data to the mesh
    4. NavMeshTriangulation triangulatedMesh = NavMesh.CalculateTriangulation(mesh.vertices, mesh.triangles);
    5.  
    6. // Next, create a NavMeshData object and assign the triangulated mesh data to it
    7. var navMeshData = new NavMeshData();
    8. navMeshData.vertices = triangulatedMesh.vertices;
    9. navMeshData.triangles = triangulatedMesh.indices;
    10.  
    11. // Finally, add the NavMeshData to the NavMesh using NavMesh.AddNavMeshData
    12. NavMesh.AddNavMeshData(navMeshData);