123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- using UnityEngine;
- using System.Collections.Generic;
- namespace Pathfinding {
- using Pathfinding.Util;
-
-
-
-
-
-
-
-
- public static class GraphUtilities {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static List<Vector3> GetContours (NavGraph graph) {
- List<Vector3> result = ListPool<Vector3>.Claim();
- if (graph is INavmesh) {
- GetContours(graph as INavmesh, (vertices, cycle) => {
- for (int j = cycle ? vertices.Count - 1 : 0, i = 0; i < vertices.Count; j = i, i++) {
- result.Add((Vector3)vertices[j]);
- result.Add((Vector3)vertices[i]);
- }
- });
- #if !ASTAR_NO_GRID_GRAPH
- } else if (graph is GridGraph) {
- GetContours(graph as GridGraph, vertices => {
- for (int j = vertices.Length - 1, i = 0; i < vertices.Length; j = i, i++) {
- result.Add((Vector3)vertices[j]);
- result.Add((Vector3)vertices[i]);
- }
- }, 0);
- #endif
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static void GetContours (INavmesh navmesh, System.Action<List<Int3>, bool> results) {
-
- var uses = new bool[3];
- var outline = new Dictionary<int, int>();
- var vertexPositions = new Dictionary<int, Int3>();
- var hasInEdge = new HashSet<int>();
- navmesh.GetNodes(_node => {
- var node = _node as TriangleMeshNode;
- uses[0] = uses[1] = uses[2] = false;
- if (node != null) {
-
- for (int j = 0; j < node.connections.Length; j++) {
- var conn = node.connections[j];
- if (conn.shapeEdge != Connection.NoSharedEdge) uses[conn.shapeEdge] = true;
- }
-
- for (int j = 0; j < 3; j++) {
-
-
- if (!uses[j]) {
- var i1 = j;
- var i2 = (j+1) % node.GetVertexCount();
- outline[node.GetVertexIndex(i1)] = node.GetVertexIndex(i2);
- hasInEdge.Add(node.GetVertexIndex(i2));
- vertexPositions[node.GetVertexIndex(i1)] = node.GetVertex(i1);
- vertexPositions[node.GetVertexIndex(i2)] = node.GetVertex(i2);
- }
- }
- }
- });
- Polygon.TraceContours(outline, hasInEdge, (chain, cycle) => {
- List<Int3> vertices = ListPool<Int3>.Claim();
- for (int i = 0; i < chain.Count; i++) vertices.Add(vertexPositions[chain[i]]);
- results(vertices, cycle);
- });
- }
- #if !ASTAR_NO_GRID_GRAPH
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static void GetContours (GridGraph grid, System.Action<Vector3[]> callback, float yMergeThreshold, GridNodeBase[] nodes = null) {
-
- HashSet<GridNodeBase> nodeSet = nodes != null ? new HashSet<GridNodeBase>(nodes) : null;
-
- if (grid is LayerGridGraph lgraph) nodes = nodes ?? lgraph.nodes;
- nodes = nodes ?? grid.nodes;
- int[] neighbourXOffsets = grid.neighbourXOffsets;
- int[] neighbourZOffsets = grid.neighbourZOffsets;
- var neighbourIndices = grid.neighbours == NumNeighbours.Six ? GridGraph.hexagonNeighbourIndices : new [] { 0, 1, 2, 3 };
- var offsetMultiplier = grid.neighbours == NumNeighbours.Six ? 1/3f : 0.5f;
- if (nodes != null) {
- var trace = ListPool<Vector3>.Claim();
- var seenStates = new HashSet<int>();
- for (int i = 0; i < nodes.Length; i++) {
- var startNode = nodes[i];
-
- if (startNode != null && startNode.Walkable && (!startNode.HasConnectionsToAllEightNeighbours || nodeSet != null)) {
- for (int startDir = 0; startDir < neighbourIndices.Length; startDir++) {
- int startState = (startNode.NodeIndex << 4) | startDir;
-
- var startNeighbour = startNode.GetNeighbourAlongDirection(neighbourIndices[startDir]);
- if ((startNeighbour == null || (nodeSet != null && !nodeSet.Contains(startNeighbour))) && !seenStates.Contains(startState)) {
-
- trace.ClearFast();
- int dir = startDir;
- GridNodeBase node = startNode;
- while (true) {
- int state = (node.NodeIndex << 4) | dir;
- if (state == startState && trace.Count > 0) {
- break;
- }
- seenStates.Add(state);
- var neighbour = node.GetNeighbourAlongDirection(neighbourIndices[dir]);
- if (neighbour == null || (nodeSet != null && !nodeSet.Contains(neighbour))) {
-
- var d0 = neighbourIndices[dir];
- dir = (dir + 1) % neighbourIndices.Length;
- var d1 = neighbourIndices[dir];
-
- Vector3 graphSpacePos = new Vector3(node.XCoordinateInGrid + 0.5f, 0, node.ZCoordinateInGrid + 0.5f);
-
- graphSpacePos.x += (neighbourXOffsets[d0] + neighbourXOffsets[d1]) * offsetMultiplier;
- graphSpacePos.z += (neighbourZOffsets[d0] + neighbourZOffsets[d1]) * offsetMultiplier;
- graphSpacePos.y = grid.transform.InverseTransform((Vector3)node.position).y;
- if (trace.Count >= 2) {
- var v0 = trace[trace.Count-2];
- var v1 = trace[trace.Count-1];
- var v1d = v1 - v0;
- var v2d = graphSpacePos - v0;
-
- if (((Mathf.Abs(v1d.x) > 0.01f || Mathf.Abs(v2d.x) > 0.01f) && (Mathf.Abs(v1d.z) > 0.01f || Mathf.Abs(v2d.z) > 0.01f)) || (Mathf.Abs(v1d.y) > yMergeThreshold || Mathf.Abs(v2d.y) > yMergeThreshold)) {
- trace.Add(graphSpacePos);
- } else {
- trace[trace.Count-1] = graphSpacePos;
- }
- } else {
- trace.Add(graphSpacePos);
- }
- } else {
-
- node = neighbour;
- dir = (dir + neighbourIndices.Length/2 + 1) % neighbourIndices.Length;
- }
- }
-
-
-
- if (trace.Count >= 3) {
- var v0 = trace[trace.Count-2];
- var v1 = trace[trace.Count-1];
- var v1d = v1 - v0;
- var v2d = trace[0] - v0;
-
- if (!(((Mathf.Abs(v1d.x) > 0.01f || Mathf.Abs(v2d.x) > 0.01f) && (Mathf.Abs(v1d.z) > 0.01f || Mathf.Abs(v2d.z) > 0.01f)) || (Mathf.Abs(v1d.y) > yMergeThreshold || Mathf.Abs(v2d.y) > yMergeThreshold))) {
- trace.RemoveAt(trace.Count - 1);
- }
- }
- if (trace.Count >= 3) {
- var v0 = trace[trace.Count-1];
- var v1 = trace[0];
- var v1d = v1 - v0;
- var v2d = trace[1] - v0;
-
- if (!(((Mathf.Abs(v1d.x) > 0.01f || Mathf.Abs(v2d.x) > 0.01f) && (Mathf.Abs(v1d.z) > 0.01f || Mathf.Abs(v2d.z) > 0.01f)) || (Mathf.Abs(v1d.y) > yMergeThreshold || Mathf.Abs(v2d.y) > yMergeThreshold))) {
- trace.RemoveAt(0);
- }
- }
- var result = trace.ToArray();
- grid.transform.Transform(result);
- callback(result);
- }
- }
- }
- }
- ListPool<Vector3>.Release(ref trace);
- }
- }
- #endif
- }
- }
|