/// <summary> /// Start generating the mesh. /// </summary> public void GenerateNewMesh(Vector2[] terrainNoiseOffset, Vector2 biomeNoiseOffset) { // Disable renderer so the old mesh can't be seen in the new place. meshRenderer.enabled = false; // Check the terrain tile's 9(3x3 grid) closest voronoi tiles if some tiles are missing create them. Otherwise do nothing. VoronoiTiling.GenerateMissingCentroids(transform.position); // Get the closest centroids to the terrain tile to have less centroids for the tiles vertices to go through. // "amount" is the centroid amount that every vertex in a tile goes through to find the closest one(to get the biome it belongs to). // Too small => Artifacts, Too large => Lag spike closestCentroids = VoronoiTiling.GetClosestCentroids(transform.position, 5); // Stop possible previous coroutines from affecting the latest coroutine StopAllCoroutines(); StartCoroutine(GenerateDraft(terrainNoiseOffset, biomeNoiseOffset)); }
/// <summary> /// Get biome that given vertex belongs to. /// </summary> private int GetBiome(int x, int z, Vector3 terrainSize, int biomeAmount, Vector2 noiseOffset) { float noiseX, noiseZ, value; if (TerrainController.UsePerlinNoiseForBiomes) { noiseX = TerrainController.BiomeFrequency * 0.1f * x / xSegments + noiseOffset.x; noiseZ = TerrainController.BiomeFrequency * 0.1f * z / zSegments + noiseOffset.y; value = Mathf.PerlinNoise(noiseX, noiseZ); } else { noiseX = x * xStep + transform.position.x - terrainSize.x / 2; noiseZ = z * zStep + transform.position.z - terrainSize.z / 2; value = VoronoiTiling.GetClosestCentroidValue(new Vector2Int((int)noiseX, (int)noiseZ), closestCentroids); } return(Mathf.FloorToInt(value / (1.00f / biomeAmount))); }