Beispiel #1
0
 void OnTerrainChunkVisibilityChanged(TerrainChunk chunk, bool isVisible)
 {
     if (isVisible)
     {
         VisibleTerrainChunks.Add(chunk);
     }
     else
     {
         VisibleTerrainChunks.Remove(chunk);
     }
 }
Beispiel #2
0
        void UpdateVisibleChunks()
        {
            var alreadyUpdatedChunkCoords = new HashSet <Vector2>();

            for (int i = VisibleTerrainChunks.Count - 1; i > -1; i--)
            {
                alreadyUpdatedChunkCoords.Add(VisibleTerrainChunks[i].Coord);
                VisibleTerrainChunks[i].UpdateTerrainChunk();
            }

            int currentChunkCoordX = Mathf.RoundToInt(viewerPosition.x / _worldSize);
            int currentChunkCoordY = Mathf.RoundToInt(viewerPosition.y / _worldSize);

            for (int yOffset = -_chunksVisibleInViewDistance; yOffset <= _chunksVisibleInViewDistance; yOffset++)
            {
                for (int xOffset = -_chunksVisibleInViewDistance; xOffset <= _chunksVisibleInViewDistance; xOffset++)
                {
                    var viewedChunkCoord = new Vector2(currentChunkCoordX + xOffset, currentChunkCoordY + yOffset);
                    if (!alreadyUpdatedChunkCoords.Contains(viewedChunkCoord))
                    {
                        if (_terrainChunkDictionary.ContainsKey(viewedChunkCoord))
                        {
                            _terrainChunkDictionary[viewedChunkCoord].UpdateTerrainChunk();
                        }
                        else
                        {
                            var terrainChunk = new TerrainChunk(
                                viewedChunkCoord,
                                heightMapSettings,
                                meshSettings,
                                detailLevels,
                                colliderLODIndex,
                                transform,
                                viewer,
                                mapMaterial);
                            _terrainChunkDictionary.Add(viewedChunkCoord,
                                                        terrainChunk);

                            terrainChunk.OnVisibilityChanged += OnTerrainChunkVisibilityChanged;
                            terrainChunk.Load();
                        }
                    }
                }
            }
        }