/*public void Start () * { * setupInitialValues (); * writeNavigationMeshToFile (); * }*/ /* * setupInitialValue will set up the initial values for the instance variables * Parameter: none * Return: none */ void setupInitialValues() { positionToStart = -1; //goalPosition = GameObject.FindGameObjectWithTag ("Goal").transform.TransformPoint (new Vector3 (0f, 0f, 0f)); //listOfObjects = GameObject.FindGameObjectsWithTag ("Surface"); //staticObjects = GameObject.FindGameObjectsWithTag ("Obstacle"); // GameObject agent = GameObject.FindGameObjectWithTag ("Player"); //agentStart = agent.transform.position; //agentDiameter = agent.GetComponent<CapsuleCollider> ().radius * 2; // agentDiameter = agent.GetComponent<CapsuleCollider> ().radius; //objectMesh = new Mesh[listOfObjects.Length]; //staticObjectMesh = new Mesh[staticObjects.Length]; //objectsArray = new AIObjects[staticObjects.Length]; surfacePolygonHolders = new AIPolygonHolder(); //allSearchs = new string[9]; //allSearchs [0] = "A*"; //allSearchs [1] = "A* Optimal"; //allSearchs [2] = "Bi-Directional"; //allSearchs [3] = "Bi-Directional Optimal"; //allSearchs [4] = "FringeSearch"; //allSearchs [5] = "Beam Search"; //allSearchs [6] = "Dynamic Weighted A*"; //allSearchs [7] = "Dynamic Weighted BDBOP"; //allSearchs [8] = "Dynamic Weighted BDOP"; //getObjectsArray (); getSurfacePolygonHolders(); //mergeSurfacePolygonHolders (); }
/* * mergePolygonHolder will merge two AIPolygonHolder objects. This will create a new AIPolygonHolder * place the polygons from each AIPolygonHolder to the new AIPolygonHolder then merge the Polygons * in the new AIPolygonHolder * Parameter: (AIPolygonHolder)holderToMerge is the AIPolygonHolder that will be merged with this object * Return: (AIPolygonHolder) * the new AIPolygonHolder that is the two merged AIPolygonHolders */ public AIPolygonHolder mergePolygonHolder(AIPolygonHolder holderToMerge) { copyAndSrink(polygonArray); holderToMerge.copyAndSrink(holderToMerge.getPolygons()); AIPolygon[] tempArray = mergeTwoArrays(polygonArray, holderToMerge.getPolygons()); //makes a new AIPolygonHolder with the capacity of both AIPolygonHolders being Merged AIPolygonHolder newHolder = new AIPolygonHolder(tempArray.Length); //makes a new AIPolygonHolder with the capacity of both AIPolygonHolders being Merged for (int count = 0; count < tempArray.Length; count++) { newHolder.addPolygon(tempArray [count].getVertices()); } newHolder.merge(); // merged the polygons in the new AIPolygonHolders return(newHolder); }
/* mergeSurfacePolygonsHolders calls the merge function to merge all the polygons in * the surfacePolygonHolders array. * Parameters: none * Return: none */ void mergeSurfacePolygonHolders() { for (int count = 0; count < surfacePolygonHolders.Length; count++) { surfacePolygonHolders [count].merge(); } for (int count = surfacePolygonHolders.Length - 1; count >= 0; count--) { if (surfacePolygonHolders [count] != null) { for (int count2 = surfacePolygonHolders.Length - 1; count2 >= 0; count2--) { if (surfacePolygonHolders [count2] != null && count != count2) { surfacePolygonHolders [count] = surfacePolygonHolders [count].mergePolygonHolder(surfacePolygonHolders [count2]); surfacePolygonHolders [count2] = null; } } } } int counter = 0; for (int count = 0; count < surfacePolygonHolders.Length; count++) { if (surfacePolygonHolders [count] != null) { counter++; } } AIPolygonHolder[] tempArray = new AIPolygonHolder[counter]; for (int count = 0, tempCount = 0; count < surfacePolygonHolders.Length; count++) { if (surfacePolygonHolders [count] != null) { tempArray [tempCount] = surfacePolygonHolders [count]; tempCount++; } } }
/* getSurgavePolygonHolders gets all the triangle polygons that compose all objects in a Unity map, * and loads them into the triangleArray. Then creates new AIObjects from those triangles out of the walkable * polygons and loads them into surfacePolygonHolders to be used for nav mesh construction. * Parameters: none * Return: none */ void getSurfacePolygonHolders() { Vector3[] WorldPositionArray = new Vector3[3]; for (int count = 0; count < listOfObjects.Length; count++) { objectMesh [count] = listOfObjects [count].GetComponent <MeshFilter> ().mesh; vectorArray = objectMesh[count].vertices; triangleArray = objectMesh[count].GetTriangles(0); surfacePolygonHolders[count] = new AIPolygonHolder(triangleArray.Length / 3); for (int count1 = 0, count2 = 1, count3 = 2; count1 < triangleArray.Length; count1 += 3, count2 += 3, count3 += 3) { WorldPositionArray [0] = listOfObjects [count].transform.TransformPoint(vectorArray [triangleArray [count1]]); WorldPositionArray [1] = listOfObjects [count].transform.TransformPoint(vectorArray [triangleArray [count2]]); WorldPositionArray [2] = listOfObjects [count].transform.TransformPoint(vectorArray [triangleArray [count3]]); if (checkVertex(WorldPositionArray) == false) { surfacePolygonHolders[count].addPolygon(WorldPositionArray); } } } }
/* * writeAfterDataToFile method will write all the data from the navigation mesh and the search agent to the * end of the file * parameters: none * return: none */ /*void writeAfterDataToFile () * { * string filePath = "data.txt"; * StreamReader inputStream = new StreamReader(filePath); * string tempString = ""; * tempString += inputStream.ReadToEnd(); * tempString += "\n"; * inputStream.Close (); * tempString += Application.loadedLevelName + "\t"; * if (AINavigationMeshAgent.loadNavigationMesh == true) * tempString += "load Navigation Mesh\t"; * else * tempString += "build Navigation Mesh\t"; * tempString += AINavigationMeshData.timeTaken + "\t"; * tempString += AINavigationMeshData.initialPolygonCount + "\t"; * tempString += AINavigationMeshData.obstacleCount + "\t"; * tempString += AINavigationMeshData.finalPolygonCount + "\t"; * tempString += AISearchAgentData.searchType + "\t"; * tempString += AISearchAgentData.timeTaken + "\t"; * tempString += AISearchAgentData.nodesVisited + "\t"; * tempString += AISearchAgentData.maxQueueSize + "\t"; * tempString += AISearchAgentData.finalPathLength + "\t"; * tempString += AISearchAgentData.finalPathCost + "\t"; * StreamWriter writer = new StreamWriter (filePath); * writer.Write (tempString); * writer.Close (); * * * * }*/ /* * doAISearchMethods will make a search agent to start searching though the navigation mesh * Parameter: none * Return: none */ /*void doAISearchMethods() * { * * AINavigationMeshAgent.polygonLengthMin = surfacePolygonHolders [positionToStart].getMinPolygonLength (); * AINavigationMeshAgent.polygonLengthAvg = surfacePolygonHolders [positionToStart].getAveragePolygonLength (); * if (positionToStart != -1) * tempSearch = new AISearch (searchType, goalPosition, surfacePolygonHolders [positionToStart].getPolygons ()); * if (tempSearch != null) { * Vector3[] vertices = tempSearch.getWayPoints (); * if (vertices != null) * for (int count = 0; count < vertices.Length; count++) * Instantiate (sphere, vertices [count], new Quaternion (0f, 0f, 0f, 0f)); * } * }*/ /* getObjectsArray gets all the triangle polygons that compose all objects in a Unity map, * and loads them into the triangleArray. Then creates new AIObjects from those triangles and loads * them into objectsArray to be used for nav mesh construction. * Parameters: none * Return: none */ /*void getObjectsArray () * { * Vector3[] WorldPositionArray = new Vector3[3]; * for (int count = 0; count < staticObjects.Length; count++) { * staticObjectMesh [count] = staticObjects [count].GetComponent<MeshFilter> ().mesh; * vectorArray = staticObjectMesh[count].vertices; * triangleArray = staticObjectMesh[count].GetTriangles (0); * objectsArray[count] = new AIObjects(triangleArray.Length/3, staticObjects[count]); * for (int count1 = 0, count2 = 1, count3 = 2; count1 < triangleArray.Length; count1 += 3, count2 += 3, count3 += 3) { * WorldPositionArray [0] = staticObjects [count].transform.TransformPoint (vectorArray [triangleArray [count1]]); * WorldPositionArray [1] = staticObjects [count].transform.TransformPoint (vectorArray [triangleArray [count2]]); * WorldPositionArray [2] = staticObjects [count].transform.TransformPoint (vectorArray [triangleArray [count3]]); * objectsArray[count].addPolygon(WorldPositionArray); * } * } * }*/ /* getSurgavePolygonHolders gets all the triangle polygons that compose all objects in a Unity map, * and loads them into the triangleArray. Then creates new AIObjects from those triangles out of the walkable * polygons and loads them into surfacePolygonHolders to be used for nav mesh construction. * Parameters: none * Return: none */ void getSurfacePolygonHolders() { Vector3[] WorldPositionArray = new Vector3[3]; NavMeshTriangulation triangulatedNavMesh = NavMesh.CalculateTriangulation(); //Mesh mesh = new Mesh (); //mesh.vertices = triangulatedNavMesh.vertices; //mesh.triangles = triangulatedNavMesh.indices; vectorArray = triangulatedNavMesh.vertices; triangleArray = triangulatedNavMesh.indices; surfacePolygonHolders = new AIPolygonHolder(triangleArray.Length / 3); for (int count1 = 0, count2 = 1, count3 = 2; count1 < triangleArray.Length; count1 += 3, count2 += 3, count3 += 3) { WorldPositionArray [0] = vectorArray [triangleArray [count1]]; WorldPositionArray [1] = vectorArray [triangleArray [count2]]; WorldPositionArray [2] = vectorArray [triangleArray [count3]]; //if (checkVertex (WorldPositionArray) == false) { surfacePolygonHolders.addPolygon(WorldPositionArray); //} } }
/* * loadNavigationMeshFromFile method will load all the data for the navigation mesh from the file * Paramters: none * Return: none */ public void loadNavigationMeshFromFile() { startTime = Time.realtimeSinceStartup; string filePath = Application.loadedLevelName + "NavigationMesh.dat"; Stream TestFileStream = File.OpenRead(filePath); BinaryFormatter deserializer = new BinaryFormatter(); positionToStart = 0; AIPolygonHolderData dataholder = (AIPolygonHolderData)deserializer.Deserialize(TestFileStream); AIPolygon[] polygons = new AIPolygon[dataholder.polygonData.Length]; for (int count = 0; count < dataholder.polygonData.Length; count++) { Vector3[] vertices = new Vector3[dataholder.polygonData [count].vertices.Length / 3]; int id = dataholder.polygonData [count].id; int[] neighbors = dataholder.polygonData [count].neighbors; int neighborsHeld = dataholder.polygonData [count].neighborCount; bool gotGoal = dataholder.polygonData [count].gotGoal; bool gotAgent = dataholder.polygonData [count].gotAgent; int agentID = dataholder.polygonData[count].agentID; for (int count2 = 0; count2 < vertices.Length; count2++) { vertices [count2] = new Vector3(dataholder.polygonData [count].vertices [count2, 0], dataholder.polygonData [count].vertices [count2, 1], dataholder.polygonData [count].vertices [count2, 2]); } polygons [count] = new AIPolygon(); polygons [count].setData(vertices, id, neighborsHeld, neighbors, gotGoal, gotAgent, agentID); } AINavigationMeshData.initialPolygonCount = getSurfacePolygonCount(); AINavigationMeshData.obstacleCount = staticObjects.Length; surfacePolygonHolders = new AIPolygonHolder[1]; surfacePolygonHolders [0] = new AIPolygonHolder(); surfacePolygonHolders [0].setAll(polygons, polygons.Length); positionToStart = 0; AINavigationMeshData.timeTaken = Time.realtimeSinceStartup - startTime; AINavigationMeshData.finalPolygonCount = getSurfacePolygonCount(); TestFileStream.Close(); writeNavigationMeshToFile(); }