using SharpNav;
using SharpNav.Geometry;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vector3 = UnityEngine.Vector3;
public class SharpNavTest : MonoBehaviour
{
// assign your mesh here, try the example mesh provided in https://github.com/Robmaister/SharpNav/blob/96ddd939f292a4f4e76b460c1f783cdf1e9bcf41/Source/SharpNav.Examples/nav_test.obj
public MeshFilter meshFilter;
void Start()
{
// get mesh data
var vertices = meshFilter.mesh.vertices;
var indices = meshFilter.mesh.triangles;
var triangleCount = meshFilter.mesh.triangles.Length;
// convert into sharpnav Vector3 array
SharpNav.Geometry.Vector3[] navVerts = new SharpNav.Geometry.Vector3[vertices.Length];
for (int i = 0, length = vertices.Length; i < length; i++)
{
navVerts[i] = ToSharpVector(vertices[i]);
}
//prepare the geometry from your mesh data
var tris = TriangleEnumerable.FromVector3(navVerts, 0, 1, vertices.Length / 3);
// check bounds
var bounds = tris.GetBoundingBox();
Debug.DrawLine(ToUnityVector(bounds.Min.Xzy), ToUnityVector(bounds.Max.Xzy), Color.red, 99);
//use the default generation settings
var settings = NavMeshGenerationSettings.Default;
settings.AgentHeight = 1.7f;
settings.AgentRadius = 0.6f;
//generate the mesh
var navMesh = NavMesh.Generate(tris, settings);
// TODO: now what???
}
// converts sharpnav vector3 to unity vector3
Vector3 ToUnityVector(SharpNav.Geometry.Vector3 vector)
{
return new Vector3(vector.X, vector.Y, vector.Z);
}
// converts unity vector3 to sharpnav vector3
SharpNav.Geometry.Vector3 ToSharpVector(Vector3 vector)
{
return new SharpNav.Geometry.Vector3(vector.x, vector.y, vector.z);
}
}