Search Unity

NavMeshBuilder with Mesh as source in code

Discussion in 'Navigation' started by dannuic, Apr 24, 2017.

  1. dannuic

    dannuic

    Joined:
    Mar 30, 2017
    Posts:
    4
    This is an x-post from the scripting forum (where I was directed here).

    I am making a procedurally generated 2D map and have generated a Mesh object containing vertices and triangles that describe the floor locations. I am trying to use this data to build a NavMesh, but I am running into problems. Here is what I currently have (though I have tried quite a few other things):

    Code (CSharp):
    1.     public class NavmeshGenerator : MonoBehaviour {
    2.         NavMeshData navMesh;
    3.         NavMeshDataInstance navMeshInstance;
    4.  
    5.         void OnEnable() {
    6.             navMesh = new NavMeshData();
    7.             navMeshInstance = NavMesh.AddNavMeshData(navMesh);
    8.         }
    9.  
    10.         void OnDisable() {
    11.             navMeshInstance.Remove();
    12.         }
    13.  
    14.         public void Generate(Mesh mesh) {
    15.             Destroy(GameObject.Find("Map"));
    16.             GameObject map = new GameObject("Map");
    17.  
    18.             var filter = map.AddComponent<MeshFilter>();
    19.             filter.mesh = mesh;
    20.  
    21.             Material material = new Material(Shader.Find("Diffuse"));
    22.             material.color = Color.red;
    23.  
    24.             var renderer = map.AddComponent<MeshRenderer>();
    25.             renderer.material = material;
    26.  
    27.             // ------------------NavMesh Stuff ----------------
    28.             NavMeshBuildSource source = new NavMeshBuildSource();
    29.             source.transform = map.transform.localToWorldMatrix;
    30.             source.shape = NavMeshBuildSourceShape.Mesh;
    31.             source.sourceObject = filter.mesh;
    32.             source.area = 0;
    33.             //source.size = mesh.bounds.size;
    34.  
    35.             var sources = new List<NavMeshBuildSource>();
    36.             sources.Add(source);
    37.  
    38.             NavMeshBuilder.UpdateNavMeshData(
    39.                 navMesh,
    40.                 NavMesh.GetSettingsByIndex(0),
    41.                 sources,
    42.                 mesh.bounds
    43.             );
    44.             // ---------------End NavMesh Stuff------------------
    45.  
    46.             Destroy(GameObject.Find("Mesh"));
    47.             GameObject meshObj = new GameObject("Mesh");
    48.             meshObj.transform.position = map.transform.position;
    49.             var triangulation = NavMesh.CalculateTriangulation();
    50.             var mesh2 = new Mesh();
    51.             mesh2.vertices = triangulation.vertices;
    52.             mesh2.triangles = triangulation.indices;
    53.             meshObj.AddComponent<MeshFilter>();
    54.             meshObj.GetComponent<MeshFilter>().mesh = mesh2;
    55.             Material material2 = new Material(Shader.Find("Diffuse"));
    56.             material2.color = Color.cyan;
    57.             meshObj.AddComponent<MeshRenderer>();
    58.             meshObj.GetComponent<MeshRenderer>().material = material2;
    59.         }
    60.     }
    The NavMeshGenerator is a component of an empty game object I created in the editor, and I am using 5.6.0f3 which should support this.

    The stuff after the NavMesh stuff is just to populate a few variables in a vain attempt at investigating what actually got built. The triangulation always comes back empty, so of course the second renderer has nothing to display. How am I building the NavMesh incorrectly?

    Thanks,
    -dannuic
     
    io-games likes this.
  2. dannuic

    dannuic

    Joined:
    Mar 30, 2017
    Posts:
    4
    Some more information. The UpdateNavMeshData returns true, so it thinks it successfully built the navmesh. I've tried use `NavMesh.SamplePosition` to see if anything is found, but the closest point found is at infinity (I'm assuming that the default not found case).

    This indicates to me that an empty NavMesh is getting generated successfully?
     
    Last edited: Apr 25, 2017
  3. dannuic

    dannuic

    Joined:
    Mar 30, 2017
    Posts:
    4
    And I figured it out. The main thing was that I had to rotate the mesh into the XZ plane and then rotate it (and the mesh) back. I also had geometry that was too small for the agent settings, so I had to play with those a bit.