Advertisement
logancberrypie

thing

Aug 11th, 2021
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Controller : MonoBehaviour
  7. {
  8. [Header("Heightmap data")]
  9. [Range(0, 10)]
  10. public int octaves = 6;
  11. [Range(0, 10)]
  12. public float frequency = 1.25f;
  13. [Range(0, 300)]
  14. public float scale = 150f;
  15. [Range(-2, 2)]
  16. public float amplitudeMod = 0.5f;
  17. [Range(0, 5)]
  18. public float frequencyMod = 2f;
  19. [Range(0, 30)]
  20. public int depth = 16;
  21. public long seed;
  22.  
  23.  
  24. public MapSizes mapSize = MapSizes.ExtraLarge_512;
  25.  
  26. [Header("BiomeInfo")]
  27.  
  28. public static BiomeType[,] BiomeTable = new BiomeType[6, 6] {
  29. //COLDEST //COLDER //COLD //HOT //HOTTER //HOTTEST
  30. { BiomeType.Ice, BiomeType.Tundra, BiomeType.Grassland, BiomeType.Desert, BiomeType.Desert, BiomeType.Desert }, //DRYEST
  31. { BiomeType.Ice, BiomeType.Tundra, BiomeType.Grassland, BiomeType.Grassland, BiomeType.Desert, BiomeType.Desert }, //DRYER
  32. { BiomeType.Ice, BiomeType.Tundra, BiomeType.Woodland, BiomeType.Woodland, BiomeType.Savanna, BiomeType.Savanna }, //DRY
  33. { BiomeType.Ice, BiomeType.Tundra, BiomeType.Woodland, BiomeType.Woodland, BiomeType.Savanna, BiomeType.Savanna }, //WET
  34. { BiomeType.Ice, BiomeType.BorealForest, BiomeType.BorealForest, BiomeType.Woodland, BiomeType.Rainforest, BiomeType.Rainforest }, //WETTER
  35. { BiomeType.Ice, BiomeType.Tundra, BiomeType.BorealForest, BiomeType.Woodland, BiomeType.Rainforest, BiomeType.Rainforest } //WETTEST
  36. };
  37.  
  38. public Dictionary<BiomeType, Biome> biomeDict;
  39.  
  40.  
  41. public AnimationCurve mappingFunction;
  42. // Start is called before the first frame update
  43.  
  44. public static BiomeType getBiomeFromTable(MoistureEnums m, HeatEnums h)
  45. {
  46. return BiomeTable[(int)m,(int)h];
  47. }
  48.  
  49.  
  50.  
  51. public void Run_RandomSeed()
  52. {
  53. seed = (long)UnityEngine.Random.Range(0, 999999f);//technically not a ulong
  54. OveralController();
  55. }
  56. public void Run_InputSeed()
  57. {
  58. OveralController();
  59. }
  60. public void OveralController()
  61. {
  62. biomeDict = new Dictionary<BiomeType, Biome>()
  63. {
  64. {BiomeType.Desert,new Biome(BiomeType.Desert) },
  65. {BiomeType.Savanna,new Biome(BiomeType.Savanna) },
  66. {BiomeType.Grassland,new Biome(BiomeType.Grassland) },
  67. {BiomeType.Woodland,new Biome(BiomeType.Woodland) },
  68. {BiomeType.Rainforest,new Biome(BiomeType.Rainforest) },
  69. {BiomeType.BorealForest,new Biome(BiomeType.BorealForest) },
  70. {BiomeType.Tundra,new Biome(BiomeType.Tundra) },
  71. {BiomeType.Ice,new Biome(BiomeType.Ice) }
  72. };
  73.  
  74.  
  75.  
  76. int tempSize = MapSizeToInt(mapSize);
  77.  
  78.  
  79. float[,] baseheightMap = HeightMapGenerator.GetData(tempSize, scale, octaves, amplitudeMod, frequencyMod, seed);
  80.  
  81. TerrainData terrainData = Terrain.activeTerrain.terrainData;
  82.  
  83. terrainData.heightmapResolution = tempSize + 1;
  84. terrainData.alphamapResolution = tempSize;
  85. terrainData.size = new Vector3(tempSize, depth, tempSize);
  86. terrainData.SetDetailResolution(tempSize, 8);
  87. terrainData.SetHeights(0, 0, baseheightMap);
  88.  
  89. float[,] baseMoistureMap = HeightMapGenerator.GetMoistureMap(tempSize,seed);
  90. float[,] baseHeatMap = HeightMapGenerator.GetHeatMap(tempSize,seed);
  91. //Generate a biome map [top 3 per tile]
  92. biomeData[,] biomeMap = CalculateBiomeMap(tempSize,baseMoistureMap, baseHeatMap);
  93.  
  94. GenerateLayers(terrainData);
  95. PaintMap(tempSize,biomeMap,terrainData);
  96.  
  97.  
  98. //Generate height map for each of the biomes
  99.  
  100. //for each point -> apply interpolation
  101. }
  102.  
  103. private void PaintMap(int tempSize, biomeData[,] biomeMap,TerrainData terrainData)
  104. {
  105. float[,,] splatMap = new float[tempSize, tempSize, terrainData.alphamapLayers];
  106. for (int x=0;x<tempSize;x++)
  107. {
  108. for (int y=0;y<tempSize;y++)
  109. {
  110. var d1 = biomeMap[x, y].primaryD;
  111. var d2 = biomeMap[x, y].secondD;
  112. var d3 = biomeMap[x, y].thirdD;
  113. float totalDistance = d1 + d2 + d3;
  114. var index1 = biomeDict[biomeMap[x, y].primaryBiome].texInfo.terrainLayerID;
  115. var index2 = biomeDict[biomeMap[x, y].secondaryBiome].texInfo.terrainLayerID;
  116. var index3 = biomeDict[biomeMap[x, y].thirdBiome].texInfo.terrainLayerID;
  117.  
  118. splatMap[x, y, index1] = d1 / totalDistance;
  119. splatMap[x, y, index2] = d2 / totalDistance;
  120. splatMap[x, y, index3] = d3 / totalDistance;
  121. }
  122. }
  123. terrainData.SetAlphamaps(0, 0, splatMap);
  124. }
  125.  
  126. private void GenerateLayers(TerrainData terrainData)
  127. {
  128. List<TerrainLayer> temp = new List<TerrainLayer>();
  129. int indexCount = 0;
  130. foreach(var output in biomeDict)
  131. {
  132. TerrainLayer tempLayer = new TerrainLayer();
  133. tempLayer.diffuseTexture = output.Value.texInfo.tex;
  134. tempLayer.name = output.Value.bType.ToString();
  135. tempLayer.tileSize = new Vector2(Biome.TILE_SIZE,Biome.TILE_SIZE);
  136. tempLayer.metallic = 0;
  137. temp.Add(tempLayer);
  138.  
  139. output.Value.texInfo.terrainLayerID = indexCount;
  140. indexCount += 1;
  141. }
  142. terrainData.terrainLayers = temp.ToArray();
  143. }
  144.  
  145. private biomeData[,] CalculateBiomeMap(int tempSize, float[,] baseMoistureMap, float[,] baseHeatMap)
  146. {
  147. biomeData[,] tempArray = new biomeData[tempSize, tempSize];
  148. for (int x =0;x<tempSize;x++)
  149. {
  150. for (int y = 0;y<tempSize;y++)
  151. {
  152. float mValue = baseMoistureMap[x, y];
  153. float hValue = baseHeatMap[x, y];
  154. MoistureEnums[] closestMoistures = BiomeMapController.getClosestMoisture(mValue);
  155. HeatEnums[] closestHeat = BiomeMapController.getClosestHeat(hValue);
  156. tempArray[x,y] = BiomeMapController.getClosestBiome(mValue,hValue,closestMoistures,closestHeat);
  157. }
  158. }
  159. return tempArray;
  160. }
  161.  
  162. public static int MapSizeToInt(MapSizes size)
  163. {
  164. switch(size)
  165. {
  166. case MapSizes.Debug_16:
  167. return 16;
  168. case MapSizes.Tiny_32:
  169. return 32;
  170. case MapSizes.Small_64:
  171. return 64;
  172. case MapSizes.Medium_128:
  173. return 128;
  174. case MapSizes.Large_256:
  175. return 256;
  176. case MapSizes.ExtraLarge_512:
  177. return 512;
  178. case MapSizes.RolePlay_1024:
  179. return 1024;
  180. default:
  181. return 128;
  182. }
  183.  
  184. }
  185.  
  186. /// <summary>
  187. /// Contains the 3 closest biomes and how close each of them biomes is to being "perfect".
  188. /// primary , secondary and third could all be the same biome!
  189. /// </summary>
  190. public struct biomeData
  191. {
  192. public BiomeType primaryBiome;
  193. public float primaryD;
  194. public BiomeType secondaryBiome;
  195. public float secondD;
  196. public BiomeType thirdBiome;
  197. public float thirdD;
  198. }
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement