void OnTerrainChunkVisibilityChanged(TerrainChunk chunk, bool isVisible)
 {
     if (isVisible)
     {
         visibleTerrainChunks.Add(chunk);
     }
     else
     {
         visibleTerrainChunks.Remove(chunk);
     }
 }
        void Awake()
        {
            textureSettings.ApplyToMaterial(mapMaterial);
            textureSettings.UpdateMeshHeights(mapMaterial, heightMapSettings.minHeight, heightMapSettings.maxHeight);

            float maxViewDst = detailLevels[detailLevels.Length - 1].visibleDstThreshold;

            meshChunksize          = meshSettings.meshWorldSize;
            chunksVisibleInViewDst = Mathf.RoundToInt(maxViewDst / meshChunksize);

            //retrieve the values needed to generate things
            _heighestTerrainPoint = HeightMapGenerator.GetHeighestPoint(GetWorldHeightMap());
            _lowestTerrainPoint   = HeightMapGenerator.GetLowestPoint(GetWorldHeightMap());

            _environmentGenerator = new EnvironmentGenerator(GetWorldHeightMap(), meshSettings, textureSettings);

            //Assign all the chunks
            int minX = -Mathf.RoundToInt(_terrainSizeInChunks.x * 0.5f);
            int maxX = Mathf.RoundToInt(_terrainSizeInChunks.x * 0.5f);
            int minY = -Mathf.RoundToInt(_terrainSizeInChunks.y * 0.5f);
            int maxY = Mathf.RoundToInt(_terrainSizeInChunks.y * 0.5f);

            for (int x = minX; x <= maxX; x++)
            {
                for (int y = minY; y <= maxY; y++)
                {
                    Vector2 chunkCoord = new Vector2(x, y);

                    TerrainChunk newChunk = new TerrainChunk(chunkCoord, heightMapSettings, meshSettings, textureSettings, detailLevels, colliderLODIndex, transform, viewer, mapMaterial);
                    terrainChunkDictionary.Add(chunkCoord, newChunk);
                    newChunk.onVisibilityChanged += OnTerrainChunkVisibilityChanged;
                    newChunk.Load();

                    Debug.Log(chunkCoord);

                    _environmentGenerator.RegisterChunk(chunkCoord, GetChunkHeightMap(chunkCoord)); //register this chunk to the EnvironmentGenerator
                }
            }

            UpdateVisibleChunks();

            GetAllChunksTransforms();

            // ----->>> SpawnMultipleTrees();
        }