/// <summary> /// Traces the calculated path from the end node to the start. /// This will build an array (<see cref="path)"/> of the nodes this path will pass through and also set the <see cref="vectorPath"/> array to the <see cref="path"/> arrays positions. /// Assumes the <see cref="vectorPath"/> and <see cref="path"/> are empty and not null (which will be the case for a correctly initialized path). /// </summary> protected virtual void Trace(PathNode from) { // Current node we are processing PathNode c = from; int count = 0; while (c != null) { c = c.parent; count++; if (count > 2048) { Debug.LogWarning("Infinite loop? >2048 node path. Remove this message if you really have that long paths (Path.cs, Trace method)"); break; } } // Ensure capacities for lists AstarProfiler.StartProfile("Check List Capacities"); if (path.Capacity < count) { path.Capacity = count; } if (vectorPath.Capacity < count) { vectorPath.Capacity = count; } AstarProfiler.EndProfile(); c = from; for (int i = 0; i < count; i++) { path.Add(c.node); c = c.parent; } // Reverse int half = count / 2; for (int i = 0; i < half; i++) { var tmp = path[i]; path[i] = path[count - i - 1]; path[count - i - 1] = tmp; } for (int i = 0; i < count; i++) { vectorPath.Add((Vector3)path[i].position); } }
/** Traces the calculated path from the end node to the start. * This will build an array (#path) of the nodes this path will pass through and also set the #vectorPath array to the #path arrays positions. * Assumes the #vectorPath and #path are empty and not null (which will be the case for a correctly initialized path). */ protected virtual void Trace(PathNode from) { int count = 0; PathNode c = from; while (c != null) { c = c.parent; count++; if (count > 1024) { Debug.LogWarning("Inifinity loop? >1024 node path. Remove this message if you really have that long paths (Path.cs, Trace function)"); break; } } //Ensure capacities for lists AstarProfiler.StartProfile("Check List Capacities"); if (path.Capacity < count) { path.Capacity = count; } if (vectorPath.Capacity < count) { vectorPath.Capacity = count; } AstarProfiler.EndProfile(); c = from; for (int i = 0; i < count; i++) { path.Add(c.node); c = c.parent; } int half = count / 2; for (int i = 0; i < half; i++) { GraphNode tmp = path[i]; path[i] = path[count - i - 1]; path[count - i - 1] = tmp; } for (int i = 0; i < count; i++) { vectorPath.Add((Vector3)path[i].position); } }
public override void Prepare() { AstarProfiler.StartProfile("Get Nearest"); if (startNode == null) { //Initialize the NNConstraint nnConstraint.tags = enabledTags; NNInfo startNNInfo = AstarPath.active.GetNearest(originalStartPoint, nnConstraint); startPoint = startNNInfo.clampedPosition; startNode = startNNInfo.node; } else { startPoint = (Vector3)startNode.position; } AstarProfiler.EndProfile(); #if ASTARDEBUG Debug.DrawLine((Vector3)startNode.position, startPoint, Color.blue); #endif if (startNode == null) { Error(); LogError("Couldn't find a close node to the start point"); return; } if (!startNode.Walkable) { #if ASTARDEBUG Debug.DrawRay(startPoint, Vector3.up, Color.red); Debug.DrawLine(startPoint, (Vector3)startNode.position, Color.red); #endif Error(); LogError("The node closest to the start point is not walkable"); return; } }
protected override void Prepare() { AstarProfiler.StartProfile("Get Nearest"); if (startNode == null) { //Initialize the NNConstraint nnConstraint.tags = enabledTags; var startNNInfo = AstarPath.active.GetNearest(originalStartPoint, nnConstraint); startPoint = startNNInfo.position; startNode = startNNInfo.node; } else { //Good Game //startPoint = (Vector3)startNode.position; startPoint = startNode.position; } AstarProfiler.EndProfile(); #if ASTARDEBUG Debug.DrawLine((Vector3)startNode.position, startPoint, Color.blue); #endif if (startNode == null) { FailWithError("Couldn't find a close node to the start point"); return; } if (!CanTraverse(startNode)) { FailWithError("The node closest to the start point could not be traversed"); return; } }
protected NavmeshTile BuildTileMesh(Voxelize vox, int x, int z, int threadIndex = 0) { AstarProfiler.StartProfile("Build Tile"); AstarProfiler.StartProfile("Init"); vox.borderSize = TileBorderSizeInVoxels; vox.forcedBounds = CalculateTileBoundsWithBorder(x, z); vox.width = tileSizeX + vox.borderSize * 2; vox.depth = tileSizeZ + vox.borderSize * 2; if (!useTiles && relevantGraphSurfaceMode == RelevantGraphSurfaceMode.OnlyForCompletelyInsideTile) { // This best reflects what the user would actually want vox.relevantGraphSurfaceMode = RelevantGraphSurfaceMode.RequireForAll; } else { vox.relevantGraphSurfaceMode = relevantGraphSurfaceMode; } vox.minRegionSize = Mathf.RoundToInt(minRegionSize / (cellSize * cellSize)); AstarProfiler.EndProfile("Init"); // Init voxelizer vox.Init(); vox.VoxelizeInput(transform, CalculateTileBoundsWithBorder(x, z)); AstarProfiler.StartProfile("Filter Ledges"); vox.FilterLedges(vox.voxelWalkableHeight, vox.voxelWalkableClimb, vox.cellSize, vox.cellHeight); AstarProfiler.EndProfile("Filter Ledges"); AstarProfiler.StartProfile("Filter Low Height Spans"); vox.FilterLowHeightSpans(vox.voxelWalkableHeight, vox.cellSize, vox.cellHeight); AstarProfiler.EndProfile("Filter Low Height Spans"); vox.BuildCompactField(); vox.BuildVoxelConnections(); vox.ErodeWalkableArea(CharacterRadiusInVoxels); vox.BuildDistanceField(); vox.BuildRegions(); var cset = new VoxelContourSet(); vox.BuildContours(contourMaxError, 1, cset, Voxelize.RC_CONTOUR_TESS_WALL_EDGES | Voxelize.RC_CONTOUR_TESS_TILE_EDGES); VoxelMesh mesh; vox.BuildPolyMesh(cset, 3, out mesh); AstarProfiler.StartProfile("Build Nodes"); // Position the vertices correctly in graph space (all tiles are laid out on the xz plane with the (0,0) tile at the origin) for (int i = 0; i < mesh.verts.Length; i++) { mesh.verts[i] *= Int3.Precision; } vox.transformVoxel2Graph.Transform(mesh.verts); NavmeshTile tile = CreateTile(vox, mesh, x, z, threadIndex); AstarProfiler.EndProfile("Build Nodes"); AstarProfiler.EndProfile("Build Tile"); return(tile); }
/** Calculates the path until completed or until the time has passed \a targetTick. * Usually a check is only done every 500 nodes if the time has passed \a targetTick. * Time/Ticks are got from System.DateTime.UtcNow.Ticks. * * Basic outline of what the function does for the standard path (Pathfinding.ABPath). * \code * while the end has not been found and no error has ocurred * check if we have reached the end * if so, exit and return the path * * open the current node, i.e loop through its neighbours, mark them as visited and put them on a heap * * check if there are still nodes left to process (or have we searched the whole graph) * if there are none, flag error and exit * * pop the next node of the heap and set it as current * * check if the function has exceeded the time limit * if so, return and wait for the function to get called again * \endcode */ public override void CalculateStep(long targetTick) { int counter = 0; //Continue to search while there hasn't ocurred an error and the end hasn't been found while (CompleteState == PathCompleteState.NotCalculated) { searchedNodes++; //Close the current node, if the current node is the target node then the path is finished if (currentR.node == endNode) { CompleteState = PathCompleteState.Complete; break; } if (currentR.H < partialBestTarget.H) { partialBestTarget = currentR; } AstarProfiler.StartFastProfile(4); //Debug.DrawRay ((Vector3)currentR.node.Position, Vector3.up*2,Color.red); //Loop through all walkable neighbours of the node and add them to the open list. currentR.node.Open(this, currentR, pathHandler); AstarProfiler.EndFastProfile(4); //any nodes left to search? if (pathHandler.HeapEmpty()) { Error(); LogError("No open points, whole area searched"); return; } //Select the node with the lowest F score and remove it from the open list AstarProfiler.StartFastProfile(7); currentR = pathHandler.PopNode(); AstarProfiler.EndFastProfile(7); //Check for time every 500 nodes, roughly every 0.5 ms usually if (counter > 500) { //Have we exceded the maxFrameTime, if so we should wait one frame before continuing the search since we don't want the game to lag if (System.DateTime.UtcNow.Ticks >= targetTick) { //Return instead of yield'ing, a separate function handles the yield (CalculatePaths) return; } counter = 0; if (searchedNodes > 1000000) { throw new System.Exception("Probable infinite loop. Over 1,000,000 nodes searched"); } } counter++; } AstarProfiler.StartProfile("Trace"); if (CompleteState == PathCompleteState.Complete) { Trace(currentR); } else if (calculatePartial && partialBestTarget != null) { CompleteState = PathCompleteState.Partial; Trace(partialBestTarget); } AstarProfiler.EndProfile(); }
/** Prepares the path. Searches for start and end nodes and does some simple checking if a path is at all possible */ public override void Prepare() { AstarProfiler.StartProfile("Get Nearest"); //Initialize the NNConstraint nnConstraint.tags = enabledTags; NNInfo startNNInfo = AstarPath.active.GetNearest(startPoint, nnConstraint, startHint); //Tell the NNConstraint which node was found as the start node if it is a PathNNConstraint and not a normal NNConstraint PathNNConstraint pathNNConstraint = nnConstraint as PathNNConstraint; if (pathNNConstraint != null) { pathNNConstraint.SetStart(startNNInfo.node); } startPoint = startNNInfo.clampedPosition; startIntPoint = (Int3)startPoint; startNode = startNNInfo.node; //If it is declared that this path type has an end point //Some path types might want to use most of the ABPath code, but will not have an explicit end point at this stage if (hasEndPoint) { NNInfo endNNInfo = AstarPath.active.GetNearest(endPoint, nnConstraint, endHint); endPoint = endNNInfo.clampedPosition; // Note, other methods assume hTarget is (Int3)endPoint hTarget = (Int3)endPoint; endNode = endNNInfo.node; hTargetNode = endNode; } AstarProfiler.EndProfile(); if (startNode == null && (hasEndPoint && endNode == null)) { Error(); LogError("Couldn't find close nodes to the start point or the end point"); return; } if (startNode == null) { Error(); LogError("Couldn't find a close node to the start point"); return; } if (endNode == null && hasEndPoint) { Error(); LogError("Couldn't find a close node to the end point"); return; } if (!startNode.Walkable) { Error(); LogError("The node closest to the start point is not walkable"); return; } if (hasEndPoint && !endNode.Walkable) { Error(); LogError("The node closest to the end point is not walkable"); return; } if (hasEndPoint && startNode.Area != endNode.Area) { Error(); LogError("There is no valid path to the target (start area: " + startNode.Area + ", target area: " + endNode.Area + ")"); return; } }
/** Calculates the path until completed or until the time has passed \a targetTick. * Usually a check is only done every 500 nodes if the time has passed \a targetTick. * Time/Ticks are got from System.DateTime.UtcNow.Ticks. * * Basic outline of what the function does for the standard path (Pathfinding.ABPath). * \code * while the end has not been found and no error has ocurred * check if we have reached the end * if so, exit and return the path * * open the current node, i.e loop through its neighbours, mark them as visited and put them on a heap * * check if there are still nodes left to process (or have we searched the whole graph) * if there are none, flag error and exit * * pop the next node of the heap and set it as current * * check if the function has exceeded the time limit * if so, return and wait for the function to get called again * \endcode */ public override void CalculateStep(long targetTick) { int counter = 0; //Continue to search while there hasn't ocurred an error and the end hasn't been found while (CompleteState == PathCompleteState.NotCalculated) { searchedNodes++; //Close the current node, if the current node is the target node then the path is finished if (currentR.node == endNode) { CompleteState = PathCompleteState.Complete; break; } if (currentR.h < partialBestTarget.h) { partialBestTarget = currentR; } //Loop through all walkable neighbours of the node and add them to the open list. currentR.node.Open(runData, currentR, hTarget, this); //any nodes left to search? if (runData.open.numberOfItems <= 1) { Error(); LogError("No open points, whole area searched"); return; } //Select the node with the lowest F score and remove it from the open list currentR = runData.open.Remove(); //Check for time every 500 nodes, roughly every 0.5 ms usually if (counter > 500) { //Have we exceded the maxFrameTime, if so we should wait one frame before continuing the search since we don't want the game to lag if (System.DateTime.UtcNow.Ticks >= targetTick) { //Return instead of yield'ing, a separate function handles the yield (CalculatePaths) return; } counter = 0; } counter++; } AstarProfiler.StartProfile("Trace"); if (CompleteState == PathCompleteState.Complete) { Trace(currentR); } else if (calculatePartial && partialBestTarget != null) { CompleteState = PathCompleteState.Partial; Trace(partialBestTarget); } AstarProfiler.EndProfile(); }
/** Prepares the path. Searches for start and end nodes and does some simple checking if a path is at all possible */ public override void Prepare() { AstarProfiler.StartProfile("Get Nearest"); //Initialize the NNConstraint nnConstraint.tags = enabledTags; var startNNInfo = AstarPath.active.GetNearest(startPoint, nnConstraint, startHint); //Tell the NNConstraint which node was found as the start node if it is a PathNNConstraint and not a normal NNConstraint var pathNNConstraint = nnConstraint as PathNNConstraint; if (pathNNConstraint != null) { pathNNConstraint.SetStart(startNNInfo.node); } startPoint = startNNInfo.position; startIntPoint = (Int3)startPoint; startNode = startNNInfo.node; if (startNode == null) { Error(); LogError("Couldn't find a close node to the start point"); return; } if (!startNode.Walkable) { Error(); LogError("The node closest to the start point is not walkable"); return; } // If it is declared that this path type has an end point // Some path types might want to use most of the ABPath code, but will not have an explicit end point at this stage if (hasEndPoint) { var endNNInfo = AstarPath.active.GetNearest(endPoint, nnConstraint, endHint); endPoint = endNNInfo.position; endNode = endNNInfo.node; if (startNode == null && endNode == null) { Error(); LogError("Couldn't find close nodes to the start point or the end point"); return; } if (endNode == null) { Error(); LogError("Couldn't find a close node to the end point"); return; } // This should not trigger unless the user has modified the NNConstraint if (!endNode.Walkable) { Error(); LogError("The node closest to the end point is not walkable"); return; } // This should not trigger unless the user has modified the NNConstraint if (startNode.Area != endNode.Area) { Error(); LogError("There is no valid path to the target (start area: " + startNode.Area + ", target area: " + endNode.Area + ")"); return; } #if !ASTAR_NO_GRID_GRAPH // Potentially we want to special case grid graphs a bit // to better support some kinds of games // If this returns true it will overwrite the // endNode, endPoint, hTarget and hTargetNode fields if (!EndPointGridGraphSpecialCase(endNNInfo.node)) #endif { // Note, other methods assume hTarget is (Int3)endPoint hTarget = (Int3)endPoint; hTargetNode = endNode; // Mark end node with flag1 to mark it as a target point pathHandler.GetPathNode(endNode).flag1 = true; } } AstarProfiler.EndProfile(); }
/// <summary> /// Calculates the path until completed or until the time has passed targetTick. /// Usually a check is only done every 500 nodes if the time has passed targetTick. /// Time/Ticks are got from System.DateTime.UtcNow.Ticks. /// /// Basic outline of what the function does for the standard path (Pathfinding.ABPath). /// <code> /// while the end has not been found and no error has occurred /// check if we have reached the end /// if so, exit and return the path /// /// open the current node, i.e loop through its neighbours, mark them as visited and put them on a heap /// /// check if there are still nodes left to process (or have we searched the whole graph) /// if there are none, flag error and exit /// /// pop the next node of the heap and set it as current /// /// check if the function has exceeded the time limit /// if so, return and wait for the function to get called again /// </code> /// </summary> protected override void CalculateStep(long targetTick) { int counter = 0; // Continue to search as long as we haven't encountered an error and we haven't found the target while (CompleteState == PathCompleteState.NotCalculated) { searchedNodes++; // Close the current node, if the current node is the target node then the path is finished if (currentR.flag1) { // We found a target point // Mark that node as the end point CompleteWith(currentR.node); break; } if (currentR.H < partialBestTarget.H) { partialBestTarget = currentR; } AstarProfiler.StartFastProfile(4); // Loop through all walkable neighbours of the node and add them to the open list. currentR.node.Open(this, currentR, pathHandler); AstarProfiler.EndFastProfile(4); // Any nodes left to search? if (pathHandler.heap.isEmpty) { if (calculatePartial && partialBestTarget != null) { CompleteState = PathCompleteState.Partial; Trace(partialBestTarget); } else { FailWithError("Searched whole area but could not find target"); } return; } // Select the node with the lowest F score and remove it from the open list AstarProfiler.StartFastProfile(7); currentR = pathHandler.heap.Remove(); AstarProfiler.EndFastProfile(7); // Check for time every 500 nodes, roughly every 0.5 ms usually if (counter > 500) { // Have we exceded the maxFrameTime, if so we should wait one frame before continuing the search since we don't want the game to lag if (System.DateTime.UtcNow.Ticks >= targetTick) { // Return instead of yield'ing, a separate function handles the yield (CalculatePaths) return; } counter = 0; // Mostly for development if (searchedNodes > 1000000) { throw new System.Exception("Probable infinite loop. Over 1,000,000 nodes searched"); } } counter++; } AstarProfiler.StartProfile("Trace"); if (CompleteState == PathCompleteState.Complete) { Trace(currentR); } else if (calculatePartial && partialBestTarget != null) { CompleteState = PathCompleteState.Partial; Trace(partialBestTarget); } AstarProfiler.EndProfile(); }
/// <summary> /// Main pathfinding method. /// This method will calculate the paths in the pathfinding queue. /// /// See: CalculatePathsThreaded /// See: StartPath /// </summary> IEnumerator CalculatePaths(PathHandler pathHandler) { // Max number of ticks before yielding/sleeping long maxTicks = (long)(astar.maxFrameTime * 10000); long targetTick = System.DateTime.UtcNow.Ticks + maxTicks; while (true) { // The path we are currently calculating Path p = null; AstarProfiler.StartProfile("Path Queue"); // Try to get the next path to be calculated bool blockedBefore = false; while (p == null) { try { p = queue.PopNoBlock(blockedBefore); blockedBefore |= p == null; } catch (ThreadControlQueue.QueueTerminationException) { yield break; } if (p == null) { AstarProfiler.EndProfile(); yield return(null); AstarProfiler.StartProfile("Path Queue"); } } AstarProfiler.EndProfile(); AstarProfiler.StartProfile("Path Calc"); IPathInternals ip = (IPathInternals)p; // Max number of ticks we are allowed to continue working in one run // One tick is 1/10000 of a millisecond maxTicks = (long)(astar.maxFrameTime * 10000); ip.PrepareBase(pathHandler); // Now processing the path // Will advance to Processing ip.AdvanceState(PathState.Processing); // Call some callbacks // It needs to be stored in a local variable to avoid race conditions var tmpOnPathPreSearch = OnPathPreSearch; if (tmpOnPathPreSearch != null) { tmpOnPathPreSearch(p); } // Tick for when the path started, used for calculating how long time the calculation took long startTicks = System.DateTime.UtcNow.Ticks; long totalTicks = 0; AstarProfiler.StartFastProfile(8); AstarProfiler.StartFastProfile(0); //Prepare the path AstarProfiler.StartProfile("Path Prepare"); ip.Prepare(); AstarProfiler.EndProfile("Path Prepare"); AstarProfiler.EndFastProfile(0); // Check if the Prepare call caused the path to complete // If this happens the path usually failed if (p.CompleteState == PathCompleteState.NotCalculated) { // For debug uses, we set the last computed path to p, so we can view debug info on it in the editor (scene view). astar.debugPathData = ip.PathHandler; astar.debugPathID = p.pathID; // Initialize the path, now ready to begin search AstarProfiler.StartProfile("Path Initialize"); ip.Initialize(); AstarProfiler.EndProfile(); // The error can turn up in the Init function while (p.CompleteState == PathCompleteState.NotCalculated) { // Do some work on the path calculation. // The function will return when it has taken too much time // or when it has finished calculation AstarProfiler.StartFastProfile(2); AstarProfiler.StartProfile("Path Calc Step"); ip.CalculateStep(targetTick); AstarProfiler.EndFastProfile(2); AstarProfiler.EndProfile(); // If the path has finished calculation, we can break here directly instead of sleeping // Improves latency if (p.CompleteState != PathCompleteState.NotCalculated) { break; } AstarProfiler.EndFastProfile(8); totalTicks += System.DateTime.UtcNow.Ticks - startTicks; // Yield/sleep so other threads can work AstarProfiler.EndProfile(); yield return(null); AstarProfiler.StartProfile("Path Calc"); startTicks = System.DateTime.UtcNow.Ticks; AstarProfiler.StartFastProfile(8); // Cancel function (and thus the thread) if no more paths should be accepted. // This is done when the A* object is about to be destroyed // The path is returned and then this function will be terminated (see similar IF statement higher up in the function) if (queue.IsTerminating) { p.FailWithError("AstarPath object destroyed"); } targetTick = System.DateTime.UtcNow.Ticks + maxTicks; } totalTicks += System.DateTime.UtcNow.Ticks - startTicks; p.duration = totalTicks * 0.0001F; #if ProfileAstar System.Threading.Interlocked.Increment(ref AstarPath.PathsCompleted); #endif } // Cleans up node tagging and other things ip.Cleanup(); AstarProfiler.EndFastProfile(8); // Call the immediate callback // It needs to be stored in a local variable to avoid race conditions var tmpImmediateCallback = p.immediateCallback; if (tmpImmediateCallback != null) { tmpImmediateCallback(p); } AstarProfiler.StartFastProfile(13); // It needs to be stored in a local variable to avoid race conditions var tmpOnPathPostSearch = OnPathPostSearch; if (tmpOnPathPostSearch != null) { tmpOnPathPostSearch(p); } AstarProfiler.EndFastProfile(13); // Push the path onto the return stack // It will be detected by the main Unity thread and returned as fast as possible (the next late update) returnQueue.Enqueue(p); ip.AdvanceState(PathState.ReturnQueue); AstarProfiler.EndProfile(); // Wait a bit if we have calculated a lot of paths if (System.DateTime.UtcNow.Ticks > targetTick) { yield return(null); targetTick = System.DateTime.UtcNow.Ticks + maxTicks; } } }
protected void ScanAllTiles(OnScanStatus statusCallback) { #if ASTARDEBUG System.Console.WriteLine("Recast Graph -- Collecting Meshes"); #endif #if BNICKSON_UPDATED editorTileSize = (int)(EditorVars.GridSize / cellSize); #endif //---- //Voxel grid size int gw = (int)(forcedBounds.size.x / cellSize + 0.5f); int gd = (int)(forcedBounds.size.z / cellSize + 0.5f); if (!useTiles) { tileSizeX = gw; tileSizeZ = gd; } else { tileSizeX = editorTileSize; tileSizeZ = editorTileSize; } //Number of tiles int tw = (gw + tileSizeX - 1) / tileSizeX; int td = (gd + tileSizeZ - 1) / tileSizeZ; tileXCount = tw; tileZCount = td; if (tileXCount * tileZCount > TileIndexMask + 1) { throw new System.Exception("Too many tiles (" + (tileXCount * tileZCount) + ") maximum is " + (TileIndexMask + 1) + "\nTry disabling ASTAR_RECAST_LARGER_TILES under the 'Optimizations' tab in the A* inspector."); } tiles = new NavmeshTile[tileXCount * tileZCount]; #if ASTARDEBUG System.Console.WriteLine("Recast Graph -- Creating Voxel Base"); #endif // If this is true, just fill the graph with empty tiles if (scanEmptyGraph) { for (int z = 0; z < td; z++) { for (int x = 0; x < tw; x++) { tiles[z * tileXCount + x] = NewEmptyTile(x, z); } } return; } AstarProfiler.StartProfile("Finding Meshes"); List <ExtraMesh> extraMeshes; #if !NETFX_CORE || UNITY_EDITOR System.Console.WriteLine("Collecting Meshes"); #endif CollectMeshes(out extraMeshes, forcedBounds); AstarProfiler.EndProfile("Finding Meshes"); // A walkableClimb higher than walkableHeight can cause issues when generating the navmesh since then it can in some cases // Both be valid for a character to walk under an obstacle and climb up on top of it (and that cannot be handled with navmesh without links) // The editor scripts also enforce this but we enforce it here too just to be sure walkableClimb = Mathf.Min(walkableClimb, walkableHeight); //Create the voxelizer and set all settings var vox = new Voxelize(cellHeight, cellSize, walkableClimb, walkableHeight, maxSlope); vox.inputExtraMeshes = extraMeshes; vox.maxEdgeLength = maxEdgeLength; int lastInfoCallback = -1; var watch = System.Diagnostics.Stopwatch.StartNew(); //Generate all tiles for (int z = 0; z < td; z++) { for (int x = 0; x < tw; x++) { int tileNum = z * tileXCount + x; #if !NETFX_CORE || UNITY_EDITOR System.Console.WriteLine("Generating Tile #" + (tileNum) + " of " + td * tw); #endif //Call statusCallback only 10 times since it is very slow in the editor if (statusCallback != null && (tileNum * 10 / tiles.Length > lastInfoCallback || watch.ElapsedMilliseconds > 2000)) { lastInfoCallback = tileNum * 10 / tiles.Length; watch.Reset(); watch.Start(); statusCallback(new Progress(Mathf.Lerp(0.1f, 0.9f, tileNum / (float)tiles.Length), "Building Tile " + tileNum + "/" + tiles.Length)); } BuildTileMesh(vox, x, z); } } #if !NETFX_CORE System.Console.WriteLine("Assigning Graph Indices"); #endif if (statusCallback != null) { statusCallback(new Progress(0.9f, "Connecting tiles")); } //Assign graph index to nodes uint graphIndex = (uint)AstarPath.active.astarData.GetGraphIndex(this); GraphNodeDelegateCancelable del = delegate(GraphNode n) { n.GraphIndex = graphIndex; return(true); }; GetNodes(del); #if BNICKSON_UPDATED #if DEBUG if (useCenterTileOnly && (3 != tileXCount || 3 != tileZCount)) { EB.Debug.LogError("RecastGenerator.ScanAllTiles() : Incorrect amount of tiles generated if ceneter tile is all that is required"); } #endif int centerXTile = (tileXCount / 2); int centerZTile = (tileZCount / 2); #endif for (int z = 0; z < td; z++) { for (int x = 0; x < tw; x++) { #if BNICKSON_UPDATED // if we're only using the center tile, and this is not the center tile if (useCenterTileOnly && !(centerZTile == z && centerXTile == x)) { continue; } #endif #if !NETFX_CORE System.Console.WriteLine("Connecing Tile #" + (z * tileXCount + x) + " of " + td * tw); #endif if (x < tw - 1) { ConnectTiles(tiles[x + z * tileXCount], tiles[x + 1 + z * tileXCount]); } if (z < td - 1) { ConnectTiles(tiles[x + z * tileXCount], tiles[x + (z + 1) * tileXCount]); } } } AstarProfiler.PrintResults(); }
protected override void Prepare() { AstarProfiler.StartProfile("Get Nearest"); nnConstraint.tags = enabledTags; var startNNInfo = AstarPath.active.GetNearest(startPoint, nnConstraint); //Tell the NNConstraint which node was found as the start node if it is a PathNNConstraint and not a normal NNConstraint var pathNNConstraint = nnConstraint as PathNNConstraint; if (pathNNConstraint != null) { pathNNConstraint.SetStart(startNNInfo.node); } startPoint = startNNInfo.position; startIntPoint = (Int3)startPoint; startNode = startNNInfo.node; if (startNode == null) { FailWithError("Couldn't find a node close to the start point"); return; } if (!CanTraverse(startNode)) { FailWithError("The node closest to the start point could not be traversed"); return; } // If it is declared that this path type has an end point if (hasEndPoint) { var endNNInfo = AstarPath.active.GetNearest(endPoint, nnConstraint); endPoint = endNNInfo.position; endNode = endNNInfo.node; if (endNode == null) { FailWithError("Couldn't find a node close to the end point"); return; } // This should not trigger unless the user has modified the NNConstraint if (!CanTraverse(endNode)) { FailWithError("The node closest to the end point could not be traversed"); return; } // This should not trigger unless the user has modified the NNConstraint if (startNode.Area != endNode.Area) { FailWithError("There is no valid path to the target"); return; } #if !ASTAR_NO_GRID_GRAPH if (!EndPointGridGraphSpecialCase(endNNInfo.node)) #endif { // Note, other methods assume hTarget is (Int3)endPoint hTarget = (Int3)endPoint; hTargetNode = endNode; // Mark end node with flag1 to mark it as a target point pathHandler.GetPathNode(endNode).flag1 = true; } } AstarProfiler.EndProfile(); }
void IUpdatableGraph.UpdateArea(GraphUpdateObject guo) { // Figure out which tiles are affected // Expand TileBorderSizeInWorldUnits voxels in all directions to make sure // all tiles that could be affected by the update are recalculated. var affectedTiles = GetTouchingTiles(guo.bounds, TileBorderSizeInWorldUnits); if (!guo.updatePhysics) { for (int z = affectedTiles.ymin; z <= affectedTiles.ymax; z++) { for (int x = affectedTiles.xmin; x <= affectedTiles.xmax; x++) { NavmeshTile tile = tiles[z * tileXCount + x]; NavMeshGraph.UpdateArea(guo, tile); } } return; } Voxelize vox = globalVox; if (vox == null) { throw new System.InvalidOperationException("No Voxelizer object. UpdateAreaInit should have been called before this function."); } AstarProfiler.StartProfile("Build Tiles"); var allMeshes = vox.inputMeshes; // Build the new tiles // If we are updating more than one tile it makes sense to do a more optimized pass for assigning each mesh to the buckets that it intersects. var buckets = PutMeshesIntoTileBuckets(vox.inputMeshes, affectedTiles); for (int x = affectedTiles.xmin; x <= affectedTiles.xmax; x++) { for (int z = affectedTiles.ymin; z <= affectedTiles.ymax; z++) { vox.inputMeshes = buckets[(z - affectedTiles.ymin) * affectedTiles.Width + (x - affectedTiles.xmin)]; stagingTiles.Add(BuildTileMesh(vox, x, z)); } } for (int i = 0; i < buckets.Length; i++) { ListPool <RasterizationMesh> .Release(buckets[i]); } for (int i = 0; i < allMeshes.Count; i++) { allMeshes[i].Pool(); } ListPool <RasterizationMesh> .Release(ref allMeshes); vox.inputMeshes = null; uint graphIndex = (uint)AstarPath.active.data.GetGraphIndex(this); // Set the correct graph index for (int i = 0; i < stagingTiles.Count; i++) { NavmeshTile tile = stagingTiles[i]; GraphNode[] nodes = tile.nodes; for (int j = 0; j < nodes.Length; j++) { nodes[j].GraphIndex = graphIndex; } } AstarProfiler.EndProfile("Build Tiles"); }
public override void Scan() { AstarProfiler.Reset(); //AstarProfiler.StartProfile ("Base Scan"); //base.Scan (); //AstarProfiler.EndProfile ("Base Scan"); if (useCRecast) { ScanCRecast(); } else { MeshFilter[] filters; ExtraMesh[] extraMeshes; if (!CollectMeshes(out filters, out extraMeshes)) { nodes = new Node[0]; return; } Voxelize vox = new Voxelize(cellHeight, cellSize, walkableClimb, walkableHeight, maxSlope); vox.maxEdgeLength = maxEdgeLength; vox.forcedBounds = forcedBounds; vox.includeOutOfBounds = includeOutOfBounds; //g.GetComponent<Voxelize>(); vox.VoxelizeMesh(filters, extraMeshes); /*bool[,] open = new bool[width,depth]; * int[,] visited = new int[width+1,depth+1]; * * for (int z=0;z<depth;z++) { * for (int x = 0;x < width;x++) { * open[x,z] = graphNodes[z*width+x].walkable; * } * }*/ /*for (int i=0;i<depth*width;i++) { * open[i] = graphNodes[i].walkable; * } * * * int wd = width*depth; * * List<int> boundary = new List<int>(); * * int p = 0; * * for (int i=0;i<wd;i++) { * if (!open[i]) { * boundary.Add (i); * * p = i; * * int backtrack = i-1; * * * }*/ vox.ErodeWalkableArea(Mathf.CeilToInt(2 * characterRadius / cellSize)); vox.BuildDistanceField(); vox.BuildRegions(); VoxelContourSet cset = new VoxelContourSet(); vox.BuildContours(contourMaxError, 1, cset, Voxelize.RC_CONTOUR_TESS_WALL_EDGES); VoxelMesh mesh; vox.BuildPolyMesh(cset, 3, out mesh); Vector3[] vertices = new Vector3[mesh.verts.Length]; AstarProfiler.StartProfile("Build Nodes"); for (int i = 0; i < vertices.Length; i++) { vertices[i] = (Vector3)mesh.verts[i]; } matrix = Matrix4x4.TRS(vox.voxelOffset, Quaternion.identity, Int3.Precision * Voxelize.CellScale); //Int3.Precision*Voxelize.CellScale+(Int3)vox.voxelOffset //GenerateNodes (this,vectorVertices,triangles, out originalVertices, out _vertices); NavMeshGraph.GenerateNodes(this, vertices, mesh.tris, out _vectorVertices, out _vertices); AstarProfiler.EndProfile("Build Nodes"); AstarProfiler.PrintResults(); } }