public float[,] generate() { // IF population density map must be generated randomly then we do it if (CityGenerator.popMapInput == null) { if (CityGeneratorUI.DebugMode) { Debug.Log("Generating Random Population Density Map..."); } pg = new PerlinGenerator(CityGenerator.popSeed, CityGenerator.popOctaves, CityGenerator.popPersistance, CityGenerator.popZoom, 0, 1); fPopMap = pg.getValues(CityGenerator.mapSize, CityGenerator.mapSize); if (CityGeneratorUI.DebugMode) { Debug.Log("Random Population Density Map generated!"); } } // ELSE population map should be passed as input by the user else { if (CityGeneratorUI.DebugMode) { Debug.Log("Population Density Map generated using existing map"); } //make a new reader and use it to read the texture TextureReader g = new TextureReader(); fPopMap = g.readTexture(CityGenerator.popMapInput, CityGenerator.mapSize); } findPeaks(); return(fPopMap); }
public void generate() { // Initialize Height Map float[,] heightMap = new float[CityGenerator.mapSize, CityGenerator.mapSize]; //set the size CityGenerator.terrain.terrainData.size = new Vector3(CityGenerator.terrainSize, (CityGenerator.maxHeight - CityGenerator.minHeight), CityGenerator.terrainSize); //based on the size we also set the alphamap resolution CityGenerator.terrain.terrainData.alphamapResolution = CityGenerator.mapSize; if (CityGenerator.terrainMap == null) { //generate random terrain PerlinGenerator perlin = new PerlinGenerator(CityGenerator.terrainSeed, CityGenerator.terrainOctaves, CityGenerator.terrainPersistance, CityGenerator.terrainZoom, 0, 1); heightMap = perlin.getValues(CityGenerator.mapSize, CityGenerator.mapSize); if (CityGeneratorUI.DebugMode) { Debug.Log("Random Terrain Generated"); } } else { //use the given terrain Map TextureReader g = new TextureReader(); heightMap = g.readTexture(CityGenerator.terrainMap, CityGenerator.mapSize); } int w = heightMap.GetLength(0); int h = heightMap.GetLength(1); float[,] result = new float[h, w]; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { result[j, i] = heightMap[i, j]; } } heightMap = result; CityGenerator.terrain.terrainData.SetHeights(0, 0, heightMap); CityGenerator.terrain.gameObject.transform.position = new Vector3(0, CityGenerator.minHeight, 0); if (CityGenerator.rWater) { // we must assign a new water to the terrain, so that the coordinates are updated GameObject.DestroyImmediate(GameObject.Find("Water(Clone)")); CityGenerator.water = GameObject.Instantiate((GameObject)Resources.Load("Water")); // water is at height 0 CityGenerator.water.transform.position = new Vector3(CityGenerator.terrainSize / 2f, 0, CityGenerator.terrainSize / 2f); CityGenerator.water.transform.localScale = new Vector3(Mathf.Sqrt(2) * CityGenerator.terrainSize / 2f, 1, Mathf.Sqrt(2) * CityGenerator.terrainSize / 2f); } else { GameObject.DestroyImmediate(GameObject.Find("Water(Clone)")); } }
//private static int[] colors = { red, green, blue }; public float[,] generate() { //set up array fGrowthMap = new float[CityGenerator.mapSize, CityGenerator.mapSize]; // IF growth rule must be generated randomly then we do it if (CityGenerator.growthMapInput == null) { if (CityGeneratorUI.DebugMode) { Debug.Log("Generating Random Growth Rule Map..."); } pg = new PerlinGenerator(CityGenerator.growthSeed, CityGenerator.growthOctaves, CityGenerator.growthPersistance, CityGenerator.growthZoom, 0, 1); float[,] perlinNoise = pg.getValues(CityGenerator.mapSize, CityGenerator.mapSize); float redBound = CityGenerator.growthBasic; float greenBound = redBound + CityGenerator.growthNewYork; for (int i = 0; i < CityGenerator.mapSize; i++) { for (int j = 0; j < CityGenerator.mapSize; j++) { if (perlinNoise[i, j] <= redBound) // we choose red { fGrowthMap[i, j] = red; } else if (perlinNoise[i, j] <= greenBound) // we choose green { fGrowthMap[i, j] = green; } else // we choose blue { fGrowthMap[i, j] = blue; } } } if (CityGeneratorUI.DebugMode) { Debug.Log("Random Growth Rule Map generated!"); } } // ELSE growth rule should be passed as input by the user else { if (CityGeneratorUI.DebugMode) { Debug.Log("GrowthRule Map generated using existing map"); } // IF the map given in input is of the right size, then we are happy if (CityGenerator.growthMapInput.height == CityGenerator.mapSize && CityGenerator.growthMapInput.width == CityGenerator.mapSize) { //the array that will contain all the pixels of the texture Color[] pixelArray = new Color[CityGenerator.mapSize * CityGenerator.mapSize]; pixelArray = CityGenerator.growthMapInput.GetPixels(0, 0, CityGenerator.mapSize, CityGenerator.mapSize); for (int i = 0; i < pixelArray.Length; i += CityGenerator.mapSize) { for (int j = i; j < i + CityGenerator.mapSize; j++) { if (pixelArray[j].r == 1 && pixelArray[j].g == 0 && pixelArray[j].b == 0) { fGrowthMap[(j % CityGenerator.mapSize), (i / CityGenerator.mapSize)] = red; } if (pixelArray[j].r == 0 && pixelArray[j].g == 1 && pixelArray[j].b == 0) { fGrowthMap[(j % CityGenerator.mapSize), (i / CityGenerator.mapSize)] = green; } if (pixelArray[j].r == 0 && pixelArray[j].g == 0 && pixelArray[j].b == 1) { fGrowthMap[(j % CityGenerator.mapSize), (i / CityGenerator.mapSize)] = blue; } } } } // ELSE we show an error else { Debug.LogError("Map must have the same size of the terrain!"); } } return(fGrowthMap); }