public static GameObject CreateTerrain(Vector3 Position, Terrain PreviousTerrain = null, Direction direction = Direction.UP, Dictionary <Direction, GameObject> Neighbors = null, TerrainBiome.Biomes Biome = TerrainBiome.Biomes.Planes ) { TerrainSettingContainer BiomeSettings = GameMaster.gameMaster.terrainBiome.ReturnTerrainSettings(Biome); TerrainData NewTerrainData = new TerrainData(); // TerrainData settings NewTerrainData.heightmapResolution = GameMaster.gameMaster.terrainSettings.HeightMapResolution; NewTerrainData.size = new Vector3(GameMaster.gameMaster.terrainSettings.MapSize.x, BiomeSettings.MaxHeight, GameMaster.gameMaster.terrainSettings.MapSize.z); NewTerrainData.TerrainTextures(BiomeSettings.Textures); NewTerrainData.GenerateHeights(Position, direction, BiomeSettings.NoiseMin, BiomeSettings.NoiseMax); NewTerrainData.ModifyHeightsForBiome(Biome); NewTerrainData.GenerateAlphaMap(); if (Biome != TerrainBiome.Biomes.Mountain) { NewTerrainData.GenerateFoliage(); NewTerrainData.GenerateGrass(); } // Texture Settings GameObject NewTerrain = Terrain.CreateTerrainGameObject(NewTerrainData); // GameObject Settings Vector3 NewTerrainPosition = new Vector3(Position.x, Position.y, Position.z); NewTerrain.transform.position = NewTerrainPosition; NewTerrain.AddComponent <TerrainScript>(); // Terrain Settings Terrain ThisTerrain = NewTerrain.GetComponent <Terrain>(); ThisTerrain.detailObjectDistance = 250; GameMaster.gameMaster.terrainManager.TerrainPositions.Add(Position, NewTerrain); return(NewTerrain); }
public static void ModifyHeightsForBiome(this TerrainData terrainData, TerrainBiome.Biomes biome) { int Res = terrainData.heightmapResolution; float[,] Heights = terrainData.GetHeights(0, 0, Res, Res); float RandomNoise = Random.Range(200, 500); switch (biome) { case TerrainBiome.Biomes.Planes: for (int x = 1; x < Res - 1; x++) { for (int z = 1; z < Res - 1; z++) { float CurrentHeight = Heights[x, z]; if (CurrentHeight <= 0.1f) { Heights[x, z] -= Mathf.PerlinNoise(Heights[x, z] + x / RandomNoise, Heights[x, z] + z / RandomNoise) / 40; } else if (CurrentHeight >= 0.2f && CurrentHeight <= 0.4f) { Heights[x, z] += Mathf.PerlinNoise(Heights[x, z] + x / RandomNoise, Heights[x, z] + z / RandomNoise) / 100; } } } break; case TerrainBiome.Biomes.Hills: break; case TerrainBiome.Biomes.Mountain: for (int x = 1; x < Res - 1; x++) { for (int z = 1; z < Res - 1; z++) { float PreviousHeights = Heights[x - 1, z - 1]; if ((x > Res / 8 && x < Res - (Res / 8)) && (z > Res / 8 && z < Res - (Res / 8))) { Heights[x, z] = Mathf.Lerp(Heights[x, z], PreviousHeights + Mathf.PerlinNoise(x / RandomNoise, z / RandomNoise) / 40f, PreviousHeights); if (Heights[x, z] <= 0.2f) { Heights[x, z] += Mathf.PerlinNoise(x / RandomNoise, z / RandomNoise) / 30; } else if (Heights[x, z] >= 0.4f && Heights[x, z] <= 0.6f) { Heights[x, z] += Mathf.PerlinNoise(x / RandomNoise, z / RandomNoise) / 20; } else { Heights[x, z] += Mathf.PerlinNoise(x / RandomNoise, z / RandomNoise) / 10; } } } } break; default: break; } terrainData.SetHeights(0, 0, Heights); }