Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Controller : MonoBehaviour
- {
- [Header("Heightmap data")]
- [Range(0, 10)]
- public int octaves = 6;
- [Range(0, 10)]
- public float frequency = 1.25f;
- [Range(0, 300)]
- public float scale = 150f;
- [Range(-2, 2)]
- public float amplitudeMod = 0.5f;
- [Range(0, 5)]
- public float frequencyMod = 2f;
- [Range(0, 30)]
- public int depth = 16;
- public long seed;
- public MapSizes mapSize = MapSizes.ExtraLarge_512;
- [Header("BiomeInfo")]
- public static BiomeType[,] BiomeTable = new BiomeType[6, 6] {
- //COLDEST //COLDER //COLD //HOT //HOTTER //HOTTEST
- { BiomeType.Ice, BiomeType.Tundra, BiomeType.Grassland, BiomeType.Desert, BiomeType.Desert, BiomeType.Desert }, //DRYEST
- { BiomeType.Ice, BiomeType.Tundra, BiomeType.Grassland, BiomeType.Grassland, BiomeType.Desert, BiomeType.Desert }, //DRYER
- { BiomeType.Ice, BiomeType.Tundra, BiomeType.Woodland, BiomeType.Woodland, BiomeType.Savanna, BiomeType.Savanna }, //DRY
- { BiomeType.Ice, BiomeType.Tundra, BiomeType.Woodland, BiomeType.Woodland, BiomeType.Savanna, BiomeType.Savanna }, //WET
- { BiomeType.Ice, BiomeType.BorealForest, BiomeType.BorealForest, BiomeType.Woodland, BiomeType.Rainforest, BiomeType.Rainforest }, //WETTER
- { BiomeType.Ice, BiomeType.Tundra, BiomeType.BorealForest, BiomeType.Woodland, BiomeType.Rainforest, BiomeType.Rainforest } //WETTEST
- };
- public Dictionary<BiomeType, Biome> biomeDict;
- public AnimationCurve mappingFunction;
- // Start is called before the first frame update
- public static BiomeType getBiomeFromTable(MoistureEnums m, HeatEnums h)
- {
- return BiomeTable[(int)m,(int)h];
- }
- public void Run_RandomSeed()
- {
- seed = (long)UnityEngine.Random.Range(0, 999999f);//technically not a ulong
- OveralController();
- }
- public void Run_InputSeed()
- {
- OveralController();
- }
- public void OveralController()
- {
- biomeDict = new Dictionary<BiomeType, Biome>()
- {
- {BiomeType.Desert,new Biome(BiomeType.Desert) },
- {BiomeType.Savanna,new Biome(BiomeType.Savanna) },
- {BiomeType.Grassland,new Biome(BiomeType.Grassland) },
- {BiomeType.Woodland,new Biome(BiomeType.Woodland) },
- {BiomeType.Rainforest,new Biome(BiomeType.Rainforest) },
- {BiomeType.BorealForest,new Biome(BiomeType.BorealForest) },
- {BiomeType.Tundra,new Biome(BiomeType.Tundra) },
- {BiomeType.Ice,new Biome(BiomeType.Ice) }
- };
- int tempSize = MapSizeToInt(mapSize);
- float[,] baseheightMap = HeightMapGenerator.GetData(tempSize, scale, octaves, amplitudeMod, frequencyMod, seed);
- TerrainData terrainData = Terrain.activeTerrain.terrainData;
- terrainData.heightmapResolution = tempSize + 1;
- terrainData.alphamapResolution = tempSize;
- terrainData.size = new Vector3(tempSize, depth, tempSize);
- terrainData.SetDetailResolution(tempSize, 8);
- terrainData.SetHeights(0, 0, baseheightMap);
- float[,] baseMoistureMap = HeightMapGenerator.GetMoistureMap(tempSize,seed);
- float[,] baseHeatMap = HeightMapGenerator.GetHeatMap(tempSize,seed);
- //Generate a biome map [top 3 per tile]
- biomeData[,] biomeMap = CalculateBiomeMap(tempSize,baseMoistureMap, baseHeatMap);
- GenerateLayers(terrainData);
- PaintMap(tempSize,biomeMap,terrainData);
- //Generate height map for each of the biomes
- //for each point -> apply interpolation
- }
- private void PaintMap(int tempSize, biomeData[,] biomeMap,TerrainData terrainData)
- {
- float[,,] splatMap = new float[tempSize, tempSize, terrainData.alphamapLayers];
- for (int x=0;x<tempSize;x++)
- {
- for (int y=0;y<tempSize;y++)
- {
- var d1 = biomeMap[x, y].primaryD;
- var d2 = biomeMap[x, y].secondD;
- var d3 = biomeMap[x, y].thirdD;
- float totalDistance = d1 + d2 + d3;
- var index1 = biomeDict[biomeMap[x, y].primaryBiome].texInfo.terrainLayerID;
- var index2 = biomeDict[biomeMap[x, y].secondaryBiome].texInfo.terrainLayerID;
- var index3 = biomeDict[biomeMap[x, y].thirdBiome].texInfo.terrainLayerID;
- splatMap[x, y, index1] = d1 / totalDistance;
- splatMap[x, y, index2] = d2 / totalDistance;
- splatMap[x, y, index3] = d3 / totalDistance;
- }
- }
- terrainData.SetAlphamaps(0, 0, splatMap);
- }
- private void GenerateLayers(TerrainData terrainData)
- {
- List<TerrainLayer> temp = new List<TerrainLayer>();
- int indexCount = 0;
- foreach(var output in biomeDict)
- {
- TerrainLayer tempLayer = new TerrainLayer();
- tempLayer.diffuseTexture = output.Value.texInfo.tex;
- tempLayer.name = output.Value.bType.ToString();
- tempLayer.tileSize = new Vector2(Biome.TILE_SIZE,Biome.TILE_SIZE);
- tempLayer.metallic = 0;
- temp.Add(tempLayer);
- output.Value.texInfo.terrainLayerID = indexCount;
- indexCount += 1;
- }
- terrainData.terrainLayers = temp.ToArray();
- }
- private biomeData[,] CalculateBiomeMap(int tempSize, float[,] baseMoistureMap, float[,] baseHeatMap)
- {
- biomeData[,] tempArray = new biomeData[tempSize, tempSize];
- for (int x =0;x<tempSize;x++)
- {
- for (int y = 0;y<tempSize;y++)
- {
- float mValue = baseMoistureMap[x, y];
- float hValue = baseHeatMap[x, y];
- MoistureEnums[] closestMoistures = BiomeMapController.getClosestMoisture(mValue);
- HeatEnums[] closestHeat = BiomeMapController.getClosestHeat(hValue);
- tempArray[x,y] = BiomeMapController.getClosestBiome(mValue,hValue,closestMoistures,closestHeat);
- }
- }
- return tempArray;
- }
- public static int MapSizeToInt(MapSizes size)
- {
- switch(size)
- {
- case MapSizes.Debug_16:
- return 16;
- case MapSizes.Tiny_32:
- return 32;
- case MapSizes.Small_64:
- return 64;
- case MapSizes.Medium_128:
- return 128;
- case MapSizes.Large_256:
- return 256;
- case MapSizes.ExtraLarge_512:
- return 512;
- case MapSizes.RolePlay_1024:
- return 1024;
- default:
- return 128;
- }
- }
- /// <summary>
- /// Contains the 3 closest biomes and how close each of them biomes is to being "perfect".
- /// primary , secondary and third could all be the same biome!
- /// </summary>
- public struct biomeData
- {
- public BiomeType primaryBiome;
- public float primaryD;
- public BiomeType secondaryBiome;
- public float secondD;
- public BiomeType thirdBiome;
- public float thirdD;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement