Advertisement
ThomasPixel

Untitled

Apr 21st, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | Gaming | 0 0
  1.     /// <summary>
  2.     /// Generates a grayscale image from Voronoi noise, simulating district zones
  3.     /// </summary>
  4.     private void GenerateDistrctsMap()
  5.     {
  6.         // private texture2d
  7.         districtMap = new Texture2D(gridWidth, gridWidth);
  8.         Color[] pix = new Color[gridWidth * gridWidth];
  9.  
  10.         // private int
  11.         seed = UnityEngine.Random.Range(0, 1000);
  12.  
  13.         // From a custom Voronoi class
  14.         VoronoiNoise Voronoi = new VoronoiNoise(seed, frequency, amp);
  15.  
  16.         // Grid width atm is 60
  17.         float[,] arr = new float[gridWidth, gridWidth];
  18.  
  19.         //Sample the 2D noise and add it into a array.
  20.         for (int y = 0; y < gridWidth; y++)
  21.         {
  22.             for (int x = 0; x < gridWidth; x++)
  23.             {
  24.                 float fx = x / (gridWidth - 1.0f);
  25.                 float fy = y / (gridWidth - 1.0f);
  26.  
  27.                 arr[x, y] = Voronoi.Sample2D(fx, fy);
  28.             }
  29.         }
  30.  
  31.         for (int y = 0; y < gridWidth; y++)
  32.         {
  33.             for (int x = 0; x < gridWidth; x++)
  34.             {
  35.                 float n = arr[x, y];
  36.                 districtMap.SetPixel(x, y, new Color(n, n, n, 1));
  37.             }
  38.         }
  39.  
  40.         districtMap.Apply();
  41.  
  42.         // districtBorderMap is a RawImage UI element
  43.         districtBorderMap.texture = districtMap;
  44.     }
  45.  
  46.     /// <summary>
  47.     /// Floodfills district zones from districtBorderMap, uses borderThreshold for checking greyscale values on Voronoi borders.
  48.     /// </summary>
  49.     private void FloodfillDistricts()
  50.     {
  51.         // colorMap is a private Texture2D
  52.         colorMap = new Texture2D(gridWidth, gridWidth);
  53.  
  54.         HashSet<Color> colors = new HashSet<Color>();
  55.         Color[] pixels = districtMap.GetPixels();
  56.  
  57.         int width = colorMap.width;
  58.         int height = colorMap.height;
  59.  
  60.         Stack<Vector2Int> stack = new Stack<Vector2Int>();
  61.  
  62.         for (int i = 0; i < pixels.Length; i++)
  63.         {
  64.             if (pixels[i].grayscale > borderThreshold)
  65.             {
  66.                 Color color = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value, 1);
  67.  
  68.                 stack.Push(new Vector2Int(i % width, i / width));
  69.  
  70.                 while (stack.Count > 0)
  71.                 {
  72.                     Vector2Int pixelPos = stack.Pop();
  73.                     int index = pixelPos.y * width + pixelPos.x;
  74.  
  75.                     if (pixels[index].grayscale > borderThreshold && !colors.Contains(pixels[index]))
  76.                     {
  77.                         pixels[index] = color;
  78.  
  79.                         if (pixelPos.x > 0) stack.Push(new Vector2Int(pixelPos.x - 1, pixelPos.y));
  80.                         if (pixelPos.x < width - 1) stack.Push(new Vector2Int(pixelPos.x + 1, pixelPos.y));
  81.                         if (pixelPos.y > 0) stack.Push(new Vector2Int(pixelPos.x, pixelPos.y - 1));
  82.                         if (pixelPos.y < height - 1) stack.Push(new Vector2Int(pixelPos.x, pixelPos.y + 1));
  83.                     }
  84.                 }
  85.  
  86.                 colors.Add(color);
  87.             }
  88.         }
  89.  
  90.         colorMap.SetPixels(pixels);
  91.         colorMap.Apply();
  92.  
  93.         districtColorMap.texture = colorMap;
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement