Advertisement
Guest User

RecastGenerator.cs ScanInternal

a guest
May 12th, 2016
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.38 KB | None | 0 0
  1. //THE SCAN INTERNAL FOR  RECASTGENERATOR.cs    
  2.  
  3. public override IEnumerable<Progress> ScanInternal()
  4.         {
  5.  
  6.         //public override void ScanInternal (OnScanStatus statusCallback) {
  7.             AstarProfiler.Reset ();
  8.             AstarProfiler.StartProfile ("Base Scan");
  9.             //AstarProfiler.InitializeFastProfile (new string[] {"Rasterize", "Rasterize Inner 1", "Rasterize Inner 2", "Rasterize Inner 3"});
  10.  
  11.             TriangleMeshNode.SetNavmeshHolder (AstarPath.active.astarData.GetGraphIndex (this), this);
  12.  
  13.  
  14.             //THIS WAS ScanTiledNavmesh //////////////////////////////////////////////////////////////
  15. #if ASTARDEBUG
  16.             System.Console.WriteLine ("Recast Graph -- Collecting Meshes");
  17. #endif
  18.  
  19.             //----
  20.  
  21.             //Voxel grid size
  22.             int gw = (int)(forcedBounds.size.x / cellSize + 0.5f);
  23.             int gd = (int)(forcedBounds.size.z / cellSize + 0.5f);
  24.  
  25.             if (!useTiles)
  26.             {
  27.                 tileSizeX = gw;
  28.                 tileSizeZ = gd;
  29.             }
  30.             else
  31.             {
  32.                 tileSizeX = editorTileSize;
  33.                 tileSizeZ = editorTileSize;
  34.             }
  35.  
  36.             //Number of tiles
  37.             int tw = (gw + tileSizeX - 1) / tileSizeX;
  38.             int td = (gd + tileSizeZ - 1) / tileSizeZ;
  39.  
  40.             tileXCount = tw;
  41.             tileZCount = td;
  42.  
  43.             if (tileXCount * tileZCount > TileIndexMask + 1)
  44.             {
  45.                 throw new System.Exception("Too many tiles (" + (tileXCount * tileZCount) + ") maximum is " + (TileIndexMask + 1) +
  46.                     "\nTry disabling ASTAR_RECAST_LARGER_TILES under the 'Optimizations' tab in the A* inspector.");
  47.             }
  48.  
  49.             tiles = new NavmeshTile[tileXCount * tileZCount];
  50.  
  51. #if ASTARDEBUG
  52.             System.Console.WriteLine ("Recast Graph -- Creating Voxel Base");
  53. #endif
  54.  
  55.             // If this is true, just fill the graph with empty tiles
  56.             if (scanEmptyGraph)
  57.             {
  58.                 for (int z = 0; z < td; z++)
  59.                 {
  60.                     for (int x = 0; x < tw; x++)
  61.                     {
  62.                         tiles[z * tileXCount + x] = NewEmptyTile(x, z);
  63.                     }
  64.                 }
  65.                 yield break;
  66.             }
  67.  
  68.             AstarProfiler.StartProfile("Finding Meshes");
  69.             List<ExtraMesh> extraMeshes;
  70.  
  71. #if !NETFX_CORE || UNITY_EDITOR
  72.             System.Console.WriteLine("Collecting Meshes");
  73. #endif
  74.             CollectMeshes(out extraMeshes, forcedBounds);
  75.  
  76.             AstarProfiler.EndProfile("Finding Meshes");
  77.  
  78.             // A walkableClimb higher than walkableHeight can cause issues when generating the navmesh since then it can in some cases
  79.             // 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)
  80.             // The editor scripts also enforce this but we enforce it here too just to be sure
  81.             walkableClimb = Mathf.Min(walkableClimb, walkableHeight);
  82.  
  83.             //Create the voxelizer and set all settings
  84.             var vox = new Voxelize(cellHeight, cellSize, walkableClimb, walkableHeight, maxSlope);
  85.             vox.inputExtraMeshes = extraMeshes;
  86.  
  87.             vox.maxEdgeLength = maxEdgeLength;
  88.  
  89.             int lastInfoCallback = -1;
  90.             var watch = System.Diagnostics.Stopwatch.StartNew();
  91.  
  92.             //Generate all tiles
  93.             for (int z = 0; z < td; z++)
  94.             {
  95.                 for (int x = 0; x < tw; x++)
  96.                 {
  97.  
  98.                     int tileNum = z * tileXCount + x;
  99. #if !NETFX_CORE || UNITY_EDITOR
  100.                     System.Console.WriteLine("Generating Tile #" + (tileNum) + " of " + td * tw);
  101. #endif
  102.  
  103.                     //Call statusCallback only 10 times since it is very slow in the editor
  104.                     if (/*statusCallback != null&&*/ (tileNum * 10 / tiles.Length > lastInfoCallback || watch.ElapsedMilliseconds > 20)) //that was milliseconds 2000
  105.                     {
  106.                         lastInfoCallback = tileNum * 10 / tiles.Length;
  107.                         watch.Reset();
  108.                         watch.Start();
  109.  
  110.                         //statusCallback(new Progress(AstarMath.MapToRange(0.1f, 0.9f, tileNum / (float)tiles.Length), "Building Tile " + tileNum + "/" + tiles.Length));
  111.                         yield return new Progress(AstarMath.MapToRange(0.1f, 0.9f, tileNum / (float)tiles.Length), "Building Tile " + tileNum + "/" + tiles.Length);
  112.                     }
  113.  
  114.                     BuildTileMesh(vox, x, z);
  115.                 }
  116.             }
  117.  
  118. #if !NETFX_CORE
  119.             System.Console.WriteLine("Assigning Graph Indices");
  120. #endif
  121.  
  122.             //if (statusCallback != null) statusCallback(new Progress(0.9f, "Connecting tiles"));
  123.             yield return new Progress(0.9f, "Getting Nodes");
  124.  
  125.             //Assign graph index to nodes
  126.             uint graphIndex = (uint)AstarPath.active.astarData.GetGraphIndex(this);
  127.  
  128.             GraphNodeDelegateCancelable del = delegate(GraphNode n)
  129.             {
  130.                 n.GraphIndex = graphIndex;
  131.                 return true; //this is an inline delegate so the return true is for the method 'del'
  132.             };
  133.             GetNodes(del);
  134.  
  135.             for (int z = 0; z < td; z++)
  136.             {
  137.                 if (z % 5 == 0) yield return new Progress(Mathf.Lerp(0.91f, 0.99f, z / (float)td), "Connecting tiles");
  138.                 for (int x = 0; x < tw; x++)
  139.                 {
  140. #if !NETFX_CORE
  141.                     System.Console.WriteLine("Connecing Tile #" + (z * tileXCount + x) + " of " + td * tw);
  142. #endif
  143.                     if (x < tw - 1) ConnectTiles(tiles[x + z * tileXCount], tiles[x + 1 + z * tileXCount]);
  144.                     if (z < td - 1) ConnectTiles(tiles[x + z * tileXCount], tiles[x + (z + 1) * tileXCount]);
  145.                 }
  146.             }
  147.  
  148.             AstarProfiler.PrintResults();
  149. #if !NETFX_CORE
  150.             System.Console.WriteLine("Finished AStar Connecting");
  151. #endif
  152.  
  153.  
  154. #if DEBUG_REPLAY
  155.             DebugReplay.WriteToFile ();
  156. #endif
  157.             AstarProfiler.PrintFastResults();
  158.             //END THIS WAS ScanTiledNavmesh //////////////////////////////////////////////////////////////
  159.             yield break;
  160.         }
  161.  
  162.         //protected void ScanTiledNavmesh(OnScanStatus statusCallback)
  163.         //{
  164.         //  ScanAllTiles(statusCallback);
  165.  
  166.         //}
  167.  
  168.         //protected void ScanAllTiles(OnScanStatus statusCallback)
  169.         //{
  170.         //this has been moved to  ScanInternal()
  171.         //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement