// Use this for initialization void Start() { unityPath = new NavMeshPath(); MeshFilter meshFilter = this.gameObject.AddComponent <MeshFilter>(); NavMeshTriangulation triangulatedNavMesh = NavMesh.CalculateTriangulation(); debugNavMeshVertices.AddRange(triangulatedNavMesh.vertices); debugNavMeshIndices.AddRange(triangulatedNavMesh.indices); //weld vertices for (int i = 0; i < triangulatedNavMesh.indices.Length; i += 3) { for (int j = 0; j < 3; j++) { int vertexIndex = triangulatedNavMesh.indices[i + j]; Vector3 v = triangulatedNavMesh.vertices[vertexIndex]; int index = FindIndex(v); if (index == -1) { vertices.Add(v); index = vertices.Count - 1; } triangles.Add(index); } } this.ExportNavMesh(); TNavMesh.Init("G:/navmesh.txt"); }
private void Update() { Vector3 startPos = source.transform.position; Vector3 targetPos = target.transform.position; if (startPos == lastStartPos && targetPos == lastTargetPos) { return; } this.path.Clear(); float time = Time.realtimeSinceStartup; if (TNavMesh.CalculatePath(startPos, targetPos, this.path)) { } Debug.Log("TNavMesh: " + (Time.realtimeSinceStartup - time)); time = Time.realtimeSinceStartup; if (NavMesh.CalculatePath(startPos, targetPos, NavMesh.AllAreas, unityPath)) { } Debug.Log("UnityNavMesh: " + (Time.realtimeSinceStartup - time)); lastStartPos = startPos; lastTargetPos = targetPos; }
public static bool CalculatePath(Vector3 sourcePosition, Vector3 targetPosition, List <Vector3> outPath) { outPath.Clear(); TNavNode sourceNode = GetNode(sourcePosition); TNavNode targetNode = GetNode(targetPosition); if (sourceNode != null && targetNode != null) { if (sourceNode == targetNode) { outPath.Add(sourcePosition); outPath.Add(targetPosition); } else { TNavMesh.Reset(); sourceNode.position = sourcePosition; targetNode.position = targetPosition; if (TNavMesh.finder.FindPath(sourceNode, targetNode, TNavMesh.pathNodes)) { var portals = CalculatePortals(targetPosition); Funnel.StringPull(sourcePosition, targetPosition, portals, outPath); } } } return(false); }
public void GetProtal(int neighborIndex, out Vector3 left, out Vector3 right) { int edgeIndex = this.neighborEdges[neighborIndex]; int leftIndex = this.vertexIndex[edgeIndex]; int rightIndex = this.vertexIndex[(edgeIndex + 1) % 3]; left = TNavMesh.GetVertex(leftIndex); right = TNavMesh.GetVertex(rightIndex); }
public TNavNode(int index0, int index1, int index2) { vertexIndex[0] = index0; vertexIndex[1] = index1; vertexIndex[2] = index2; Vector3 v0 = TNavMesh.GetVertex(index0); Vector3 v1 = TNavMesh.GetVertex(index1); Vector3 v2 = TNavMesh.GetVertex(index2); this._centroid = (v0 + v1 + v2) / 3; }
public static List <Vector3> GetDebugNeighbor(Vector3 pos) { List <Vector3> lines = new List <Vector3>(); TNavNode node = TNavMesh.GetNode(pos); if (node != null) { for (int i = 0; i < node.GetNeighborCount(); i++) { TNavNode neightbor = node.GetNeighbor(i); lines.Add(vertices[neightbor.index0]); lines.Add(vertices[neightbor.index1]); lines.Add(vertices[neightbor.index1]); lines.Add(vertices[neightbor.index2]); lines.Add(vertices[neightbor.index2]); lines.Add(vertices[neightbor.index0]); } } return(lines); }
private void OnDrawGizmos() { //foreach (var triangle in this.pathTriangle) //{ // Gizmos.color = Color.red; // Vector3 v0 = this.vertices[triangle.index0]; // Vector3 v1 = this.vertices[triangle.index1]; // Vector3 v2 = this.vertices[triangle.index2]; // Gizmos.DrawLine(v0, v1); // Gizmos.DrawLine(v1, v2); // Gizmos.DrawLine(v2, v0); //} //for (int i = 0; i < this.portals.Count; i += 2) //{ // Gizmos.color = Color.blue; // Gizmos.DrawLine(this.portals[i], this.portals[i + 1]); //} for (int i = 0; i < TNavMesh.GetDebugPathNode().Count; i += 2) { Gizmos.color = Color.black; Gizmos.DrawLine(TNavMesh.GetDebugPathNode()[i], TNavMesh.GetDebugPathNode()[i + 1]); } for (int i = 0; i < TNavMesh.GetDebugPathNodePosition().Count - 1; i++) { Gizmos.color = Color.blue; Gizmos.DrawLine(TNavMesh.GetDebugPathNodePosition()[i], TNavMesh.GetDebugPathNodePosition()[i + 1]); } //List<Vector3> neighbor = TNavMesh.GetDebugNeighbor(this.target.transform.position); //for (int i = 0; i < neighbor.Count; i+=2) //{ // Gizmos.color = Color.yellow; // Gizmos.DrawLine(neighbor[i], neighbor[i+1]); //} //for (int i = 0; i < TNavMesh.GetDebugPortal().Count; i += 2) //{ // Gizmos.color = Color.green; // Gizmos.DrawLine(TNavMesh.GetDebugPortal()[i], TNavMesh.GetDebugPortal()[i + 1]); //} for (int i = 0; i < path.Count - 1; i++) { Gizmos.color = Color.white; Gizmos.DrawLine(path[i], path[i + 1]); } if (unityPath != null) { for (int i = 0; i < unityPath.corners.Length - 1; i++) { Gizmos.color = Color.red; Gizmos.DrawLine(unityPath.corners[i], unityPath.corners[i + 1]); } } //draw navmesh //for(int i = 0; i < debugNavMeshIndices.Count; i+=3) //{ // Vector3 v0 = debugNavMeshVertices[debugNavMeshIndices[i]]; // Vector3 v1 = debugNavMeshVertices[debugNavMeshIndices[i+1]]; // Vector3 v2 = debugNavMeshVertices[debugNavMeshIndices[i+2]]; // Gizmos.color = Color.red; // Gizmos.DrawLine(v0, v1); // Gizmos.DrawLine(v1, v2); // Gizmos.DrawLine(v2, v0); //} }