Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Generates a grayscale image from Voronoi noise, simulating district zones
- /// </summary>
- private void GenerateDistrctsMap()
- {
- // private texture2d
- districtMap = new Texture2D(gridWidth, gridWidth);
- Color[] pix = new Color[gridWidth * gridWidth];
- // private int
- seed = UnityEngine.Random.Range(0, 1000);
- // From a custom Voronoi class
- VoronoiNoise Voronoi = new VoronoiNoise(seed, frequency, amp);
- // Grid width atm is 60
- float[,] arr = new float[gridWidth, gridWidth];
- //Sample the 2D noise and add it into a array.
- for (int y = 0; y < gridWidth; y++)
- {
- for (int x = 0; x < gridWidth; x++)
- {
- float fx = x / (gridWidth - 1.0f);
- float fy = y / (gridWidth - 1.0f);
- arr[x, y] = Voronoi.Sample2D(fx, fy);
- }
- }
- for (int y = 0; y < gridWidth; y++)
- {
- for (int x = 0; x < gridWidth; x++)
- {
- float n = arr[x, y];
- districtMap.SetPixel(x, y, new Color(n, n, n, 1));
- }
- }
- districtMap.Apply();
- // districtBorderMap is a RawImage UI element
- Texture2D districtMapCopy = districtMap;
- districtBorderMap.texture = districtMapCopy;
- }
- /// <summary>
- /// Floodfills district zones from districtMap, uses borderThreshold for checking greyscale values on Voronoi borders.
- /// </summary>
- private void FloodfillDistricts()
- {
- int width = districtMap.width;
- int height = districtMap.height;
- Color[] colors = new Color[] { Color.yellow, Color.blue, Color.green };
- List<Color> usedColors = new List<Color>();
- // Loop over each pixel in the districts map
- for (int y = 0; y < height; y++)
- {
- for (int x = 0; x < width; x++)
- {
- Color pixelColor = districtMap.GetPixel(x, y);
- // If the pixel is white (above threshold) and hasn't been colored yet, floodfill it with a random color
- if (pixelColor.grayscale > borderThreshold && !usedColors.Contains(pixelColor))
- {
- Color fillColor = colors[Random.Range(0, colors.Length)];
- FloodfillBlob(x, y, pixelColor, fillColor, ref usedColors);
- }
- }
- }
- districtMap.Apply();
- districtColorMap.texture = districtMap;
- }
- /// <summary>
- /// Performs a floodfill on the current Voronoi blob. Blob defined as a white-ish space surrounding by black-ish borders
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <param name="targetColor"></param>
- /// <param name="fillColor"></param>
- /// <param name="usedColors"></param>
- private void FloodfillBlob(int x, int y, Color targetColor, Color fillColor, ref List<Color> usedColors)
- {
- int width = districtMap.width;
- int height = districtMap.height;
- Queue<Vector2Int> pixelsToCheck = new Queue<Vector2Int>();
- pixelsToCheck.Enqueue(new Vector2Int(x, y));
- while (pixelsToCheck.Count > 0)
- {
- Vector2Int pixel = pixelsToCheck.Dequeue();
- int px = pixel.x;
- int py = pixel.y;
- // Check if the pixel is within bounds and has the target color
- if (px >= 0 && px < width && py >= 0 && py < height && districtMap.GetPixel(px, py) == targetColor)
- {
- // Set the pixel to the fill color and add it to the list of used colors
- districtMap.SetPixel(px, py, fillColor);
- if (!usedColors.Contains(fillColor))
- {
- usedColors.Add(fillColor);
- }
- // Add neighboring pixels to the queue to be checked
- pixelsToCheck.Enqueue(new Vector2Int(px - 1, py));
- pixelsToCheck.Enqueue(new Vector2Int(px + 1, py));
- pixelsToCheck.Enqueue(new Vector2Int(px, py - 1));
- pixelsToCheck.Enqueue(new Vector2Int(px, py + 1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment