Ramaraunt1

terraingensys

Dec 28th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.35 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MainGen : MonoBehaviour
  5. {
  6. public int seed = 12345;
  7. public Material material;
  8. public Texture2D Heightmap;
  9. public Texture2D Watermap;
  10. public Texture2D Tempmap;
  11. public int width = 1024;
  12. public int height = 1024;
  13. public Texture2D outputTexture;
  14. public MapData mapData;
  15.  
  16. //BIOME GRAPH ARRAY
  17. BiomeType[,] BiomeTable = new BiomeType[6, 6] {
  18. //COLDEST //COLDER //COLD //HOT //HOTTER //HOTTEST
  19. { BiomeType.Ice, BiomeType.Tundra, BiomeType.Grassland, BiomeType.Desert, BiomeType.Desert, BiomeType.Desert }, //DRYEST
  20. { BiomeType.Ice, BiomeType.Tundra, BiomeType.Grassland, BiomeType.Desert, BiomeType.Desert, BiomeType.Desert }, //DRYER
  21. { BiomeType.Ice, BiomeType.Tundra, BiomeType.Woodland, BiomeType.Woodland, BiomeType.Savanna, BiomeType.Savanna }, //DRY
  22. { BiomeType.Ice, BiomeType.Tundra, BiomeType.BorealForest, BiomeType.Woodland, BiomeType.Savanna, BiomeType.Savanna }, //WET
  23. { BiomeType.Ice, BiomeType.Tundra, BiomeType.BorealForest, BiomeType.SeasonalForest, BiomeType.TropicalRainforest, BiomeType.TropicalRainforest }, //WETTER
  24. { BiomeType.Ice, BiomeType.Tundra, BiomeType.BorealForest, BiomeType.TemperateRainforest, BiomeType.TropicalRainforest, BiomeType.TropicalRainforest } //WETTEST
  25. };
  26.  
  27. //BIOME LOOKUP METHOD
  28. public BiomeType GetBiomeType(Tile tile)
  29. {
  30. return BiomeTable[(int)tile.MoistureType, (int)tile.HeatType];
  31. }
  32.  
  33.  
  34. void Start()
  35. {
  36.  
  37.  
  38.  
  39. outputTexture = new Texture2D(width, height, TextureFormat.ARGB32, false);
  40. mapData = new MapData(width, height);
  41. Color32[] heightArray = new Color32[width*height];
  42. Color32[] waterArray = new Color32[width * height];
  43. Color32[] tempArray = new Color32[width * height];
  44. heightArray = Heightmap.GetPixels32(0);
  45. waterArray = Watermap.GetPixels32(0);
  46. tempArray = Tempmap.GetPixels32(0);
  47.  
  48. // convert arrays of colors into arrays of brightness values.
  49. float[] heightPercentages = new float[1048576];
  50. float[] waterPercentages = new float[1048576];
  51. float[] tempPercentages = new float[1048576];
  52.  
  53. //also needed stuff
  54. int increment = 0;
  55. for (int x = 0; x < width; x++)
  56. {
  57. for (int z = 0; z < height; z++)
  58. {
  59. heightPercentages[increment] = ((Color)heightArray[increment]).grayscale;
  60. waterPercentages[increment] = ((Color)waterArray[increment]).grayscale;
  61. tempPercentages[increment] = ((Color)tempArray[increment]).grayscale;
  62. increment++;
  63. }
  64. }
  65. Debug.Log(tempArray[1].r);
  66.  
  67.  
  68. //Now its time to set up the different colors for the different biomes
  69. Color32 ocean = new Color32(0, 0, 255,100);
  70. Color32 ice = new Color32(0, 255, 255, 100);
  71. Color32 tundra = new Color32(102, 0, 0, 100);
  72. Color32 grassland = new Color32(102, 204, 0, 100);
  73. Color32 borealForest = new Color32(0, 102, 51, 100);
  74. Color32 woodland = new Color32(0, 51, 0, 100);
  75. Color32 desert = new Color32(255, 128, 0, 1);
  76. Color32 seasonalForest = new Color32(0, 204, 0, 100);
  77. Color32 temperateRainForest = new Color32(25, 51, 0, 100);
  78. Color32 savanna = new Color32(153, 153, 0, 100);
  79. Color32 tropicalRainForest = new Color32(76, 153, 0, 100);
  80. Color32 coldMountains = new Color32(24, 24, 24, 100);
  81. Color32 temperateMountains = new Color32(100, 100, 100, 100);
  82. Color32 barrenMountains = new Color32(150, 150, 150, 100);
  83. Color32 tallMountains = new Color32(200, 200, 200, 100);
  84.  
  85. Color32 errorTerrain = new Color32(255, 0, 0, 100);
  86.  
  87. //Lets make the array to store the final result.
  88. Color32[] final = new Color32[1048576];
  89.  
  90. //now its time to calculate everything and produce the final image result.
  91. increment = 0;
  92. for (int x = 0; x < width; x++)
  93. {
  94. for (int z = 0; z < height; z++)
  95. {
  96. //outer most layer is heightmap. Then its temperature, then percipitation.
  97. if (heightPercentages[increment] > .9) //if its white, its either water or ice.
  98. {
  99. if (tempPercentages[increment] < .1)//if its super cold
  100. {
  101. final[increment] = ice;
  102. }
  103. else //if its not super cold
  104. {
  105. final[increment] = ocean;
  106. }
  107. }
  108. else if (heightPercentages[increment] >= 0 ) //if its flat to hilly
  109. {
  110. int temperature;
  111. int percipitation;
  112. if (tempPercentages[increment] < .1)
  113. {
  114. temperature = 0;
  115. }
  116. else if (tempPercentages[increment] < .35)
  117. {
  118. temperature = 1;
  119. }
  120. else if (tempPercentages[increment] < .5)
  121. {
  122. temperature = 2;
  123. }
  124. else if (tempPercentages[increment] < .65)
  125. {
  126. temperature = 3;
  127. }
  128. else if (tempPercentages[increment] < .8)
  129. {
  130. temperature = 4;
  131. }
  132. else if (tempPercentages[increment] < .9)
  133. {
  134. temperature = 5;
  135. }
  136. else
  137. {
  138. temperature = 6;
  139. }
  140.  
  141. if (waterPercentages[increment] < .1)
  142. {
  143. percipitation = 0;
  144. }
  145. else if (waterPercentages[increment] < .35)
  146. {
  147. percipitation = 1;
  148. }
  149. else if (waterPercentages[increment] < .5)
  150. {
  151. percipitation = 2;
  152. }
  153. else if (waterPercentages[increment] < .65)
  154. {
  155. percipitation = 3;
  156. }
  157. else if (waterPercentages[increment] < .8)
  158. {
  159. percipitation = 4;
  160. }
  161. else if (waterPercentages[increment] < .9)
  162. {
  163. percipitation = 5;
  164. }
  165. else
  166. {
  167. percipitation = 6;
  168. }
  169. Color32 cur_color = new Color32() ;
  170. switch(temperature)
  171. {
  172. case 0:
  173. cur_color = ice;
  174. break;
  175. case 1:
  176. cur_color = tundra;
  177. break;
  178. case 2:
  179. switch(percipitation)
  180. {
  181. case 0:
  182. cur_color = grassland;
  183. break;
  184. case 1:
  185. cur_color = grassland;
  186. break;
  187. case 2:
  188. cur_color = woodland;
  189. break;
  190. default:
  191. cur_color = borealForest;
  192. break;
  193. }
  194. break;
  195. case 3:
  196. switch (percipitation)
  197. {
  198. case 0:
  199. cur_color = desert;
  200. break;
  201. case 1:
  202. cur_color = desert;
  203. break;
  204. case 2:
  205. cur_color = woodland;
  206. break;
  207. case 3:
  208. cur_color = woodland;
  209. break;
  210. case 4:
  211. cur_color = seasonalForest;
  212. break;
  213. case 5:
  214. cur_color = temperateRainForest;
  215. break;
  216. }
  217. break;
  218. case 4:
  219. switch (percipitation)
  220. {
  221. case 0:
  222. cur_color = desert;
  223. break;
  224. case 1:
  225. cur_color = desert;
  226. break;
  227. case 2:
  228. cur_color = savanna;
  229. break;
  230. case 3:
  231. cur_color = savanna;
  232. break;
  233. case 4:
  234. cur_color = temperateRainForest;
  235. break;
  236. }
  237. break;
  238. case 5:
  239. switch (percipitation)
  240. {
  241. case 0:
  242. cur_color = desert;
  243. break;
  244. case 1:
  245. cur_color = desert;
  246. break;
  247. case 2:
  248. cur_color = savanna;
  249. break;
  250. case 3:
  251. cur_color = savanna;
  252. break;
  253. case 4:
  254. cur_color = temperateRainForest;
  255. break;
  256. }
  257. break;
  258. }
  259. final[increment] = cur_color;
  260.  
  261.  
  262. }
  263. else
  264. { final[increment] = errorTerrain; }
  265. increment++;
  266. }
  267. }
  268.  
  269. //We got it! Now lets make the texture.
  270.  
  271. outputTexture.SetPixels32(final,0);
  272. outputTexture.Apply();
  273. material.mainTexture = outputTexture;
  274.  
  275. }
  276.  
  277. }
Advertisement
Add Comment
Please, Sign In to add comment