private void OnTerrainChunkVisibilityChanged(TerrainChunk chunk, bool isVisible) { if (isVisible) { visibleTerrainChunks.Add(chunk); } else { visibleTerrainChunks.Remove(chunk); } }
private void UpdateVisibleChunks(bool initialLoad = false) { HashSet <Vector2> alreadyUpdatedChunkCoords = new HashSet <Vector2>(); for (int i = visibleTerrainChunks.Count - 1; i >= 0; i--) { alreadyUpdatedChunkCoords.Add(visibleTerrainChunks[i].coord); visibleTerrainChunks[i].UpdateTerrainChunk(); } int currentChunkCoordX = Mathf.RoundToInt(viewerPos.x / meshSizeInWorld); int currentChunkCoordY = Mathf.RoundToInt(viewerPos.y / meshSizeInWorld); for (int yOffset = -chunksVisibleInViewDst; yOffset <= chunksVisibleInViewDst; yOffset++) { for (int xOffset = -chunksVisibleInViewDst; xOffset <= chunksVisibleInViewDst; xOffset++) { Vector2 viewedChunkCoord = new Vector2(currentChunkCoordX + xOffset, currentChunkCoordY + yOffset); if (alreadyUpdatedChunkCoords.Contains(viewedChunkCoord)) { continue; } if (terrainChunkDictionary.ContainsKey(viewedChunkCoord)) { terrainChunkDictionary[viewedChunkCoord].UpdateTerrainChunk(); } else { TerrainChunk newChunk = new TerrainChunk(viewedChunkCoord, worldSettings, detailLevels, colliderLODIndex, chunkParent.transform, viewer); terrainChunkDictionary.Add(viewedChunkCoord, newChunk); newChunk.OnVisibilityChanged += OnTerrainChunkVisibilityChanged; if (initialLoad) { initialChunkLoadCount++; newChunk.OnLodMeshUpdated += OnInitialMeshUpdated; } newChunk.Load(); } } } }
private void OnInitialMeshUpdated(TerrainChunk chunk) { chunk.OnLodMeshUpdated -= OnInitialMeshUpdated; initialChunkLoadCount--; CheckIfInitialLoadComplete(); }