Ramaraunt1

Untitled

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