0
\$\begingroup\$

When Unity builds a NavMesh, you can see it in the scene view if you have the Navigation window open and select "Show NavMesh" in the Navmesh Display box. I'd like to use the exact same overlay in a camera view. Is this accessible in any way? Can it be rebuilt during runtime? Since the navMesh isn't an actual mesh, I can't seem to find a way to make this happen.

enter image description here

EDIT: This is what I'm trying to work with right now:

public class NavMeshToCamera : MonoBehaviour {

    NavMeshTriangulation triangles;
    Mesh mesh;

    void Awake () {
        triangles = NavMesh.CalculateTriangulation();
        mesh = new Mesh();

        mesh.vertices = triangles.vertices;
        mesh.triangles = triangles.indices;
    }
}

I have this script attached to a secondary camera that will be enabled while holding down a button to see an overlay of a minimap of the current dungeon. I'm not quite sure what to do with the mesh data to provide a mesh visible to the camera. Since no mesh exists initially, I suppose I'd have to take the new mesh, apply a layer to it, and have only that layer visible to the camera. But I'm having some difficulties with this.

\$\endgroup\$
1
\$\begingroup\$

This should be possible by optaining the triangles from the generated NavMesh. So first you need to bake the NavMesh and then call CalculateTriangulation. This gives you a NavMeshTriangulation object which contain a list of vertices and indices. From there it should be fairly easy to transform them into a Unity Mesh. The only problem is, that the generated size of the triangulation doesn't match the size of your objects, so you have to scale the generated mesh. Strangely the scale is not uniform for all axis so for me a scalation for the x axis of 1 / 30 and for the z axis of 1 / 40 worked best. So your code should look something like this:

  var triangles = NavMesh.CalculateTriangulation ();
  Vector3[] vertices = triangles.vertices;
  for (int i = 0; i < vertices.Length; ++i) {
    vertices [i].x *= 1.0f / 30.0f;
    vertices [i].z *= 1.0f / 40.0f;
  }

  Mesh mesh = new Mesh ();
  mesh.vertices = vertices;
  mesh.triangles = triangles.indices;
  GetComponent<MeshFilter>().sharedMesh = mesh;
\$\endgroup\$
  • \$\begingroup\$ I've edited my question to show what I'm working with. I'm actually on that path right now, but can't quite get visualization of the mesh to work. \$\endgroup\$ – Jesse Williams Aug 26 '17 at 16:13
  • \$\begingroup\$ The problem could be the scale of your mesh, it may be to large to actually see it. You might want to try something like this Vector3[] vertices = triangles.vertices; for (int i = 0; i < vertices.Length; ++i) { vertices [i].x *= 1.0f / 30.0f; vertices [i].z *= 1.0f / 40.0f; } \$\endgroup\$ – Tim Rolff Aug 26 '17 at 16:20
  • \$\begingroup\$ I ended up going a completely different route, but this was invaluable information - thank you again! \$\endgroup\$ – Jesse Williams Aug 29 '17 at 23:31
  • \$\begingroup\$ @TimRolff When I do this, I cannot apply any transparent textures, it always comes out as the solid color. \$\endgroup\$ – kingcobra1986 Dec 20 '17 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.