Esempio n. 1
0
 public DijkstraSearchThread(NavigationGraphNode startNode, NodeGoalBounds bounds, GoalBoundsDijkstraMapFlooding dijkstra, GoalBoundingTable goalBoundingTable, int index)
 {
     this.startNode         = startNode;
     this.bounds            = bounds;
     this.dijkstra          = dijkstra;
     this.goalBoundingTable = goalBoundingTable;
     this.index             = index;
 }
    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());
    }
Esempio n. 4
0
    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);

        GoalBoundingTable goalBoundingTable = ScriptableObject.CreateInstance <GoalBoundingTable>();
        var nodes = GetNodesHack(navMesh);

        goalBoundingTable.table = new NodeGoalBounds[nodes.Count];

        // List of threads to wait.
        List <Thread> threads = new List <Thread>();

        //calculate goal bounds for each edge
        for (int i = 0; i < nodes.Count; i++)
        {
            if (nodes[i] is NavMeshEdge)
            {
                var            dijkstra = new GoalBoundsDijkstraMapFlooding(navMesh);
                NodeGoalBounds auxGoalBounds;

                //initialize the GoalBounds structure for the edge
                auxGoalBounds = ScriptableObject.CreateInstance <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] = ScriptableObject.CreateInstance <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
                //run in a new thread:

                /*
                 * dijkstra.Search(nodes[i], auxGoalBounds);
                 *
                 * goalBoundingTable.table[i] = auxGoalBounds;
                 */

                var    searchThread = new DijkstraSearchThread(nodes[i], auxGoalBounds, dijkstra, goalBoundingTable, i);
                Thread t            = new Thread(new ThreadStart(searchThread.Search));
                threads.Add(t);

                t.Start();
                //t.Join();

                //edgeIndex++;
            }
        }

        foreach (Thread t in threads)
        {
            t.Join();
        }

        //saving the assets, this takes forever using Unity's serialization mechanism
        goalBoundingTable.SaveToAssetDatabase();
        EditorUtility.ClearProgressBar();
    }