CollectMeshes() private method

private CollectMeshes ( List &extraMeshes, Bounds bounds ) : bool
extraMeshes List
bounds UnityEngine.Bounds
return bool
Beispiel #1
0
        /** Changes the bounds of the graph to precisely encapsulate all objects in the scene that can be included in the scanning process based on the settings.
         * Which objects are used depends on the settings. If an object would have affected the graph with the current settings if it would have
         * been inside the bounds of the graph, it will be detected and the bounds will be expanded to contain that object.
         *
         * This method corresponds to the 'Snap bounds to scene' button in the inspector.
         *
         * \see rasterizeMeshes
         * \see rasterizeTerrain
         * \see rasterizeColliders
         * \see mask
         * \see tagMask
         *
         * \see forcedBoundsCenter
         * \see forcedBoundsSize
         */
        public static void SnapForceBoundsToScene(this RecastGraph self)
        {
            var meshes = self.CollectMeshes(new Bounds(Vector3.zero, new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity)));

            if (meshes.Count == 0)
            {
                return;
            }

            var bounds = meshes[0].bounds;

            for (int i = 1; i < meshes.Count; i++)
            {
                bounds.Encapsulate(meshes[i].bounds);
                meshes[i].Pool();
            }

            self.forcedBoundsCenter = bounds.center;
            self.forcedBoundsSize   = bounds.size;
        }
Beispiel #2
0
        public static IEnumerable <Progress> ScanAllTiles(this RecastGraph self)
        {
            self.transform = self.CalculateTransform();
            self.InitializeTileInfo();

            // If this is true, just fill the graph with empty tiles
            if (self.scanEmptyGraph)
            {
                self.FillWithEmptyTiles();
                yield break;
            }

            // 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
            self.walkableClimb = Mathf.Min(self.walkableClimb, self.walkableHeight);

            yield return(new Progress(0, "Finding Meshes"));

            var bounds  = self.transform.Transform(new Bounds(self.forcedBoundsSize * 0.5f, self.forcedBoundsSize));
            var meshes  = self.CollectMeshes(bounds);
            var buckets = self.PutMeshesIntoTileBuckets(meshes);

            Queue <Int2> tileQueue = new Queue <Int2>();

            // Put all tiles in the queue
            for (int z = 0; z < self.tileZCount; z++)
            {
                for (int x = 0; x < self.tileXCount; x++)
                {
                    tileQueue.Enqueue(new Int2(x, z));
                }
            }

            var workQueue = new ParallelWorkQueue <Int2>(tileQueue);
            // Create the voxelizers and set all settings (one for each thread)
            var voxelizers = new Voxelize[workQueue.threadCount];

            for (int i = 0; i < voxelizers.Length; i++)
            {
                voxelizers[i] = new Voxelize(self.CellHeight, self.cellSize, self.walkableClimb, self.walkableHeight, self.maxSlope, self.maxEdgeLength);
            }
            workQueue.action = (tile, threadIndex) => {
                voxelizers[threadIndex].inputMeshes           = buckets[tile.x + tile.y * self.tileXCount];
                self.tiles[tile.x + tile.y * self.tileXCount] = self.BuildTileMesh(voxelizers[threadIndex], tile.x, tile.y, threadIndex);
            };

            // Prioritize responsiveness while playing
            // but when not playing prioritize throughput
            // (the Unity progress bar is also pretty slow to update)
            int timeoutMillis = Application.isPlaying ? 1 : 200;

            // Scan all tiles in parallel
            foreach (var done in workQueue.Run(timeoutMillis))
            {
                yield return(new Progress(Mathf.Lerp(0.1f, 0.9f, done / (float)self.tiles.Length), "Calculated Tiles: " + done + "/" + self.tiles.Length));
            }

            yield return(new Progress(0.9f, "Assigning Graph Indices"));

            // Assign graph index to nodes
            uint graphIndex = (uint)AstarPath.active.data.GetGraphIndex(self);

            self.GetNodes(node => node.GraphIndex = graphIndex);

            // First connect all tiles with an EVEN coordinate sum
            // This would be the white squares on a chess board.
            // Then connect all tiles with an ODD coordinate sum (which would be all black squares on a chess board).
            // This will prevent the different threads that do all
            // this in parallel from conflicting with each other.
            // The directions are also done separately
            // first they are connected along the X direction and then along the Z direction.
            // Looping over 0 and then 1
            for (int coordinateSum = 0; coordinateSum <= 1; coordinateSum++)
            {
                for (int direction = 0; direction <= 1; direction++)
                {
                    for (int i = 0; i < self.tiles.Length; i++)
                    {
                        if ((self.tiles[i].x + self.tiles[i].z) % 2 == coordinateSum)
                        {
                            tileQueue.Enqueue(new Int2(self.tiles[i].x, self.tiles[i].z));
                        }
                    }

                    workQueue        = new ParallelWorkQueue <Int2>(tileQueue);
                    workQueue.action = (tile, threadIndex) => {
                        // Connect with tile at (x+1,z) and (x,z+1)
                        if (direction == 0 && tile.x < self.tileXCount - 1)
                        {
                            self.ConnectTiles(self.tiles[tile.x + tile.y * self.tileXCount], self.tiles[tile.x + 1 + tile.y * self.tileXCount]);
                        }
                        if (direction == 1 && tile.y < self.tileZCount - 1)
                        {
                            self.ConnectTiles(self.tiles[tile.x + tile.y * self.tileXCount], self.tiles[tile.x + (tile.y + 1) * self.tileXCount]);
                        }
                    };

                    var numTilesInQueue = tileQueue.Count;
                    // Connect all tiles in parallel
                    foreach (var done in workQueue.Run(timeoutMillis))
                    {
                        yield return(new Progress(0.95f, "Connected Tiles " + (numTilesInQueue - done) + "/" + numTilesInQueue + " (Phase " + (direction + 1 + 2 * coordinateSum) + " of 4)"));
                    }
                }
            }

            for (int i = 0; i < meshes.Count; i++)
            {
                meshes[i].Pool();
            }
            ListPool <RasterizationMesh> .Release(ref meshes);

            // This may be used by the TileHandlerHelper script to update the tiles
            // while taking NavmeshCuts into account after the graph has been completely recalculated.
            if (self.OnRecalculatedTiles != null)
            {
                self.OnRecalculatedTiles(self.tiles.Clone() as NavmeshTile[]);
            }
        }