public static NNInfo GetNearest (NavMeshGraph graph, GraphNode[] nodes, Vector3 position, NNConstraint constraint, bool accurateNearestNode) {
if (nodes == null || nodes.Length == 0) {
Debug.LogError ("NavGraph hasn't been generated yet or does not contain any nodes");
return new NNInfo ();
}
if (constraint == null) constraint = NNConstraint.None;
Int3[] vertices = graph.vertices;
//Query BBTree
if (graph.bbTree == null) {
/** \todo Change method to require a navgraph */
return GetNearestForce (graph as NavGraph, graph as INavmeshHolder, position, constraint, accurateNearestNode);
//Debug.LogError ("No Bounding Box Tree has been assigned");
//return new NNInfo ();
}
//Searches in radiuses of 0.05 - 0.2 - 0.45 ... 1.28 times the average of the width and depth of the bbTree
float w = (graph.bbTree.root.rect.width + graph.bbTree.root.rect.height)*0.5F*0.02F;
NNInfo query = graph.bbTree.QueryCircle (position,w,constraint);//graph.bbTree.Query (position,constraint);
if (query.node == null) {
for (int i=1;i<=8;i++) {
query = graph.bbTree.QueryCircle (position, i*i*w, constraint);
if (query.node != null || (i-1)*(i-1)*w > AstarPath.active.maxNearestNodeDistance*2) { // *2 for a margin
break;
}
}
}
if (query.node != null) {
query.clampedPosition = ClosestPointOnNode (query.node as TriangleMeshNode,vertices,position);
}
if (query.constrainedNode != null) {
if (constraint.constrainDistance && ((Vector3)query.constrainedNode.position - position).sqrMagnitude > AstarPath.active.maxNearestNodeDistanceSqr) {
query.constrainedNode = null;
} else {
query.constClampedPosition = ClosestPointOnNode (query.constrainedNode as TriangleMeshNode, vertices, position);
}
}
return query;
}