private static void CalculateGoalBounds() { //get the NavMeshGraph from the current scene NavMeshPathGraph navMesh = GameObject.Find("Navigation Mesh").GetComponent <NavMeshRig>().NavMesh.Graph; //this is needed because RAIN AI does some initialization the first time the QuantizeToNode method is called //if this method is not called, the connections in the navigationgraph are not properly initialized navMesh.QuantizeToNode(new Vector3(0, 0, 0), 1.0f); var dijkstra = new GoalBoundsDijkstraMapFlooding(navMesh); GoalBoundingTable goalBoundingTable = new GoalBoundingTable(); var nodes = GetNodesHack(navMesh); goalBoundingTable.table = new NodeGoalBounds[nodes.Count]; NodeGoalBounds auxGoalBounds; //calculate goal bounds for each edge for (int i = 0; i < nodes.Count; i++) { if (nodes[i] is NavMeshEdge) { //initialize the GoalBounds structure for the edge auxGoalBounds = new NodeGoalBounds { connectionBounds = new Assets.Scripts.IAJ.Unity.Pathfinding.DataStructures.GoalBounding.Bounds[nodes[i].OutEdgeCount] }; for (int j = 0; j < nodes[i].OutEdgeCount; j++) { auxGoalBounds.connectionBounds[j] = new Assets.Scripts.IAJ.Unity.Pathfinding.DataStructures.GoalBounding.Bounds(); auxGoalBounds.connectionBounds[j].InitializeBounds(nodes[i].LocalPosition); } if (i % 10 == 0) { float percentage = (float)i / (float)nodes.Count; EditorUtility.DisplayProgressBar("GoalBounding precomputation progress", "Calculating goal bounds for each edge", percentage); } //run a Dijkstra mapflooding for each node dijkstra.Search(nodes[i], auxGoalBounds); goalBoundingTable.table[i] = auxGoalBounds; } } //saving the table to a binary file EditorUtility.DisplayProgressBar("GoalBounding precomputation progress", "Saving GoalBoundsTable to an Asset file", 100); goalBoundingTable.Save(Application.dataPath + "/Resources/", "GoalBoundingTable.dat"); //saving the assets, this takes forever using Unity's serialization mechanism EditorUtility.ClearProgressBar(); }
private static void CalculateGoalBounds() { Debug.Log("Creating goalbounds table"); DateTime startTime = DateTime.Now; //get the NavMeshGraph from the current scene NavMeshPathGraph navMesh = GameObject.Find("Navigation Mesh").GetComponent <NavMeshRig>().NavMesh.Graph; //this is needed because RAIN AI does some initialization the first time the QuantizeToNode method is called //if this method is not called, the connections in the navigationgraph are not properly initialized navMesh.QuantizeToNode(new Vector3(0, 0, 0), 1.0f); GoalBoundingTable goalBoundingTable = new GoalBoundingTable(); var nodes = GetNodesHack(navMesh); goalBoundingTable.table = new NodeGoalBounds[nodes.Count]; var dijkstra = new GoalBoundsDijkstraMapFlooding(nodes); NodeGoalBounds auxGoalBounds; //calculate goal bounds for each edge for (int i = 0; i < nodes.Count; i++) { if (nodes[i] is NavMeshEdge) { //initialize the GoalBounds structure for the edge auxGoalBounds = new NodeGoalBounds(); auxGoalBounds.connectionBounds = new Assets.Scripts.IAJ.Unity.Pathfinding.DataStructures.GoalBounding.Bounds[nodes[i].OutEdgeCount]; for (int j = 0; j < nodes[i].OutEdgeCount; j++) { auxGoalBounds.connectionBounds[j] = new Assets.Scripts.IAJ.Unity.Pathfinding.DataStructures.GoalBounding.Bounds(); auxGoalBounds.connectionBounds[j].InitializeBounds(nodes[i].Position); } if (i % 10 == 0) { float percentage = (float)i / (float)nodes.Count; EditorUtility.DisplayProgressBar("GoalBounding precomputation progress", "Calculating goal bounds for each edge", percentage); } //run a Dijkstra mapflooding for each node dijkstra.Search(nodes[i], auxGoalBounds); goalBoundingTable.table[i] = auxGoalBounds; //edgeIndex++; } } BinaryFormatter bf = new BinaryFormatter(); if (File.Exists("Assets/GoalBoundingData/GoalBounding.dat")) { File.Delete("Assets/GoalBoundingData/GoalBounding.dat"); } FileStream file = File.Create("Assets/GoalBoundingData/GoalBounding.dat"); bf.Serialize(file, goalBoundingTable); file.Close(); EditorUtility.ClearProgressBar(); TimeSpan time = DateTime.Now - startTime; Debug.Log("Duration creating goalboundtable : " + time.ToString()); }