Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class MainGen : MonoBehaviour
- {
- public int seed = 12345;
- public Material material;
- public Texture2D Heightmap;
- public Texture2D Watermap;
- public Texture2D Tempmap;
- public int width = 1024;
- public int height = 1024;
- public Texture2D outputTexture;
- public MapData mapData;
- //BIOME GRAPH ARRAY
- 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.Desert, BiomeType.Desert, BiomeType.Desert }, //DRYER
- { BiomeType.Ice, BiomeType.Tundra, BiomeType.Woodland, BiomeType.Woodland, BiomeType.Savanna, BiomeType.Savanna }, //DRY
- { BiomeType.Ice, BiomeType.Tundra, BiomeType.BorealForest, BiomeType.Woodland, BiomeType.Savanna, BiomeType.Savanna }, //WET
- { BiomeType.Ice, BiomeType.Tundra, BiomeType.BorealForest, BiomeType.SeasonalForest, BiomeType.TropicalRainforest, BiomeType.TropicalRainforest }, //WETTER
- { BiomeType.Ice, BiomeType.Tundra, BiomeType.BorealForest, BiomeType.TemperateRainforest, BiomeType.TropicalRainforest, BiomeType.TropicalRainforest } //WETTEST
- };
- //BIOME LOOKUP METHOD
- public BiomeType GetBiomeType(Tile tile)
- {
- return BiomeTable[(int)tile.MoistureType, (int)tile.HeatType];
- }
- void Start()
- {
- outputTexture = new Texture2D(width, height, TextureFormat.ARGB32, false);
- mapData = new MapData(width, height);
- Color32[] heightArray = new Color32[width*height];
- Color32[] waterArray = new Color32[width * height];
- Color32[] tempArray = new Color32[width * height];
- heightArray = Heightmap.GetPixels32(0);
- waterArray = Watermap.GetPixels32(0);
- tempArray = Tempmap.GetPixels32(0);
- // convert arrays of colors into arrays of brightness values.
- float[] heightPercentages = new float[1048576];
- float[] waterPercentages = new float[1048576];
- float[] tempPercentages = new float[1048576];
- //also needed stuff
- int increment = 0;
- for (int x = 0; x < width; x++)
- {
- for (int z = 0; z < height; z++)
- {
- heightPercentages[increment] = ((Color)heightArray[increment]).grayscale;
- waterPercentages[increment] = ((Color)waterArray[increment]).grayscale;
- tempPercentages[increment] = ((Color)tempArray[increment]).grayscale;
- increment++;
- }
- }
- Debug.Log(tempArray[1].r);
- //Now its time to set up the different colors for the different biomes
- Color32 ocean = new Color32(0, 0, 255,100);
- Color32 ice = new Color32(0, 255, 255, 100);
- Color32 tundra = new Color32(102, 0, 0, 100);
- Color32 grassland = new Color32(102, 204, 0, 100);
- Color32 borealForest = new Color32(0, 102, 51, 100);
- Color32 woodland = new Color32(0, 51, 0, 100);
- Color32 desert = new Color32(255, 128, 0, 1);
- Color32 seasonalForest = new Color32(0, 204, 0, 100);
- Color32 temperateRainForest = new Color32(25, 51, 0, 100);
- Color32 savanna = new Color32(153, 153, 0, 100);
- Color32 tropicalRainForest = new Color32(76, 153, 0, 100);
- Color32 coldMountains = new Color32(24, 24, 24, 100);
- Color32 temperateMountains = new Color32(100, 100, 100, 100);
- Color32 barrenMountains = new Color32(150, 150, 150, 100);
- Color32 tallMountains = new Color32(200, 200, 200, 100);
- Color32 errorTerrain = new Color32(255, 0, 0, 100);
- //Lets make the array to store the final result.
- Color32[] final = new Color32[1048576];
- //now its time to calculate everything and produce the final image result.
- increment = 0;
- for (int x = 0; x < width; x++)
- {
- for (int z = 0; z < height; z++)
- {
- //outer most layer is heightmap. Then its temperature, then percipitation.
- if (heightPercentages[increment] > .9) //if its white, its either water or ice.
- {
- if (tempPercentages[increment] < .1)//if its super cold
- {
- final[increment] = ice;
- }
- else //if its not super cold
- {
- final[increment] = ocean;
- }
- }
- else if (heightPercentages[increment] >= 0 ) //if its flat to hilly
- {
- int temperature;
- int percipitation;
- if (tempPercentages[increment] < .1)
- {
- temperature = 0;
- }
- else if (tempPercentages[increment] < .35)
- {
- temperature = 1;
- }
- else if (tempPercentages[increment] < .5)
- {
- temperature = 2;
- }
- else if (tempPercentages[increment] < .65)
- {
- temperature = 3;
- }
- else if (tempPercentages[increment] < .8)
- {
- temperature = 4;
- }
- else if (tempPercentages[increment] < .9)
- {
- temperature = 5;
- }
- else
- {
- temperature = 6;
- }
- if (waterPercentages[increment] < .1)
- {
- percipitation = 0;
- }
- else if (waterPercentages[increment] < .35)
- {
- percipitation = 1;
- }
- else if (waterPercentages[increment] < .5)
- {
- percipitation = 2;
- }
- else if (waterPercentages[increment] < .65)
- {
- percipitation = 3;
- }
- else if (waterPercentages[increment] < .8)
- {
- percipitation = 4;
- }
- else if (waterPercentages[increment] < .9)
- {
- percipitation = 5;
- }
- else
- {
- percipitation = 6;
- }
- Color32 cur_color = new Color32() ;
- switch(temperature)
- {
- case 0:
- cur_color = ice;
- break;
- case 1:
- cur_color = tundra;
- break;
- case 2:
- switch(percipitation)
- {
- case 0:
- cur_color = grassland;
- break;
- case 1:
- cur_color = grassland;
- break;
- case 2:
- cur_color = woodland;
- break;
- default:
- cur_color = borealForest;
- break;
- }
- break;
- case 3:
- switch (percipitation)
- {
- case 0:
- cur_color = desert;
- break;
- case 1:
- cur_color = desert;
- break;
- case 2:
- cur_color = woodland;
- break;
- case 3:
- cur_color = woodland;
- break;
- case 4:
- cur_color = seasonalForest;
- break;
- case 5:
- cur_color = temperateRainForest;
- break;
- }
- break;
- case 4:
- switch (percipitation)
- {
- case 0:
- cur_color = desert;
- break;
- case 1:
- cur_color = desert;
- break;
- case 2:
- cur_color = savanna;
- break;
- case 3:
- cur_color = savanna;
- break;
- case 4:
- cur_color = temperateRainForest;
- break;
- }
- break;
- case 5:
- switch (percipitation)
- {
- case 0:
- cur_color = desert;
- break;
- case 1:
- cur_color = desert;
- break;
- case 2:
- cur_color = savanna;
- break;
- case 3:
- cur_color = savanna;
- break;
- case 4:
- cur_color = temperateRainForest;
- break;
- }
- break;
- }
- final[increment] = cur_color;
- }
- else
- { final[increment] = errorTerrain; }
- increment++;
- }
- }
- //We got it! Now lets make the texture.
- outputTexture.SetPixels32(final,0);
- outputTexture.Apply();
- material.mainTexture = outputTexture;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment