Advertisement
SebastianLague

marching squares texture generation

Mar 31st, 2016
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.93 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public static class MapTexture {
  6.  
  7.     static int size;
  8.     static Texture2D texture;
  9.     static bool[,] regionStamp;
  10.  
  11.     public static Texture2D GetMapTexture(Tile[,] map, int textureResolution) {
  12.         size = textureResolution;
  13.         regionStamp = new bool[size, size];
  14.  
  15.         int mapWidth = map.GetLength (0);
  16.         int mapHeight = map.GetLength (1);
  17.         int textureWidth = (mapWidth-1) * textureResolution;
  18.         int textureHeight = (mapHeight-1) * textureResolution;
  19.  
  20.         texture = new Texture2D (textureWidth, textureHeight);
  21.         texture.filterMode = FilterMode.Point;
  22.         //texture.wrapMode = TextureWrapMode.Clamp;
  23.  
  24.         //Debug.Log(texture.GetPixel(50,50));
  25.         for (int y = 0; y < mapHeight-1; y++) {
  26.             for (int x = 0; x < mapWidth-1; x++) {
  27.                 ProcessSquare (map, x, y);
  28.             }
  29.         }
  30.         /*
  31.         for (int y = 0; y < mapHeight; y++) {
  32.             for (int x = 0; x < mapWidth; x++) {
  33.                 Debug.Log (map [x, y].regionIndex);
  34.             }
  35.             Debug.Log ("\n");
  36.         }*/
  37.  
  38.         texture.Apply ();
  39.         //Debug.Log(texture.GetPixel(50,50));
  40.         return texture;
  41.     }
  42.  
  43.     static void ProcessSquare(Tile[,] map, int x, int y) {
  44.         //Debug.Log ("Process: " + x + "  " + y);
  45.         Tile[] corners = new Tile[4] {
  46.             map [x, y],
  47.             map [x+1, y],
  48.             map [x, y+1],
  49.             map[x + 1, y+1]
  50.         };
  51.  
  52.  
  53.         FillRegion (x * size, y * size, map [x, y].colour, false);
  54.    
  55.         HashSet<int> usedConfigs = new HashSet<int> ();
  56.         List<Config> configs = new List<Config>();
  57.  
  58.  
  59.         for (int i = 0; i < 4; i++) {
  60.             int activeRegion = corners [i].regionIndex;
  61.             int config = GetConfig (corners [0].regionIndex == activeRegion, corners [1].regionIndex == activeRegion, corners [2].regionIndex == activeRegion, corners [3].regionIndex == activeRegion);
  62.             //Debug.Log ("Active: " + activeRegion + " Config: " + config);
  63.             if (!usedConfigs.Contains (config)) {
  64.                 usedConfigs.Add (config);
  65.                 configs.Add(new Config(config, corners[i].colour));
  66.             }
  67.         }
  68.         //configs.Sort ();
  69.         for (int i = 0; i < configs.Count; i++) {
  70.         //  Debug.Log(configs[i].config);
  71.             CreateRegionStamp(configs[i].config);
  72.             FillRegion(x*size,y*size,configs[i].colour,true);
  73.         }
  74.     }
  75.  
  76.  
  77.  
  78.     static int GetConfig(bool topLeft, bool topRight, bool bottomLeft, bool bottomRight) {
  79.         return 0 | ((bottomLeft)?1:0) | ((bottomRight)?2:0) | ((topRight)?4:0) | ((topLeft)?8:0);
  80.     }
  81.  
  82.     static void FillRegion(int startX, int startY, Color colour, bool useStamp) {
  83.         for (int y = 0; y < size; y++) {
  84.             for (int x = 0; x < size; x++) {
  85.                 if (startY+y == 0 && useStamp) {
  86.                     //Debug.Log ("start: " + startX + " x: " +x + " total: "  +(startX + x));
  87.                 }
  88.                 if (regionStamp [x, y] || !useStamp) {
  89.                     texture.SetPixel (startX + x, startY + y, colour);
  90.                 }
  91.             }
  92.         }
  93.         //Debug.Log ("end");
  94.         //texture.Apply ();
  95.     }
  96.        
  97.  
  98.     static void CreateRegionStamp(int config) {
  99.         regionStamp = new bool[size,size];
  100.         switch (config) {
  101.         case 1:
  102.             SetCorner (2);
  103.             break;
  104.         case 2:
  105.             SetCorner (3);
  106.             break;
  107.         case 3:
  108.             SetQuadrant (2);
  109.             SetQuadrant (3);
  110.             break;
  111.         case 4:
  112.             SetTip (1,true);
  113.             break;
  114.         case 5:
  115.             SetAll ();
  116.             SetTip (0, false);
  117.             SetCorner (3, false);
  118.             break;
  119.         case 6:
  120.             SetQuadrant (1);
  121.             SetQuadrant (3);
  122.             break;
  123.         case 7:
  124.             SetAll ();
  125.             SetTip (0,false);
  126.             break;
  127.         case 8:
  128.             SetTip (0,true);
  129.             break;
  130.         case 9:
  131.             SetQuadrant (0);
  132.             SetQuadrant (2);
  133.             break;
  134.         case 10:
  135.             SetAll ();
  136.             SetTip (1, false);
  137.             SetCorner (2, false);
  138.             break;
  139.         case 11:
  140.             SetAll ();
  141.             SetTip (1,false);
  142.             break;
  143.         case 12:
  144.             SetQuadrant (0);
  145.             SetQuadrant (1);
  146.             break;
  147.         case 13:
  148.             SetAll ();
  149.             SetTip (3,false);
  150.             break;
  151.         case 14:
  152.             SetAll ();
  153.             SetTip (2,false);
  154.             break;
  155.         case 15:
  156.             SetAll ();
  157.             break;
  158.         }
  159.     }
  160.  
  161.     static void SetCorner(int corner, bool val = true) {
  162.         for (int y = 0; y < size/2; y++) {
  163.             int maxX = size / 2 - y;
  164.             for (int x = 0; x < maxX; x++) {
  165.                 int drawX = x;
  166.                 int drawY = y;
  167.                 if (corner == 1 || corner == 3) {
  168.                     drawX = size - x-1;
  169.                 }
  170.                 if (corner == 2 || corner == 3) {
  171.                     drawY = size - y-1;
  172.                 }
  173.                 regionStamp [drawX, drawY] = val;
  174.             }
  175.         }
  176.     }
  177.  
  178.     static void SetTip(int corner, bool val) {
  179.         for (int y = 0; y < size/2-1; y++) {
  180.             int maxX = size / 2 - y-1;
  181.             for (int x = 0; x < maxX; x++) {
  182.                 int drawX = x;
  183.                 int drawY = y;
  184.                 if (corner == 1 || corner == 3) {
  185.                     drawX = size - x-1;
  186.                 }
  187.                 if (corner == 2 || corner == 3) {
  188.                     drawY = size - y-1;
  189.                 }
  190.                 regionStamp [drawX, drawY] = val;
  191.             }
  192.         }
  193.     }
  194.  
  195.     static void SetQuadrant(int quadrant) {
  196.         for (int y = 0; y < size/2; y++) {
  197.             for (int x = 0; x < size/2; x++) {
  198.                 int drawX = x;
  199.                 int drawY = y;
  200.                 if (quadrant == 1 || quadrant == 3) {
  201.                     drawX = size - x-1;
  202.                 }
  203.                 if (quadrant == 2 || quadrant == 3) {
  204.                     drawY = size - y-1;
  205.                 }
  206.                 regionStamp [drawX, drawY] = true;
  207.             }
  208.         }
  209.     }
  210.  
  211.     static void SetAll() {
  212.         for (int y = 0; y < size; y++) {
  213.             for (int x = 0; x < size; x++) {
  214.                 regionStamp [x, y] = true;
  215.             }
  216.         }
  217.     }
  218. }
  219.  
  220. public class Config : System.IComparable<Config>
  221. {
  222.     public int config;
  223.     public Color colour;
  224.  
  225.     public Config(int _config, Color _colour) {
  226.         config = _config;
  227.         colour = _colour;
  228.     }
  229.  
  230.  
  231.  
  232.     public int CompareTo(Config a) {
  233.         //Debug.Log ("Compare : "+ config + "  " + a.config);
  234.  
  235.         if (config == a.config) {
  236.             return 0;
  237.         }
  238.  
  239.         if ((config == 11 || config == 7) && (a.config == 4 || a.config == 8)) {
  240.             return 1;
  241.         }
  242.         if ((config == 14 || config == 13) && (a.config == 1 || a.config == 2)) {
  243.             return -1;
  244.         }
  245.  
  246.         if ((a.config == 11 || a.config == 7) && (config == 4 || config == 8)) {
  247.             return -1;
  248.         }
  249.         if ((a.config == 14 || a.config == 13) && (config == 1 || config == 2)) {
  250.             return 1;
  251.         }
  252.  
  253.  
  254.  
  255.  
  256.         if (config == 5 || config == 10) {
  257.             //Debug.Log ("result: -1");
  258.             return -1;
  259.         }
  260.         if (a.config == 5 || a.config == 10) {
  261.             //Debug.Log ("result: 1");
  262.             return 1;
  263.         }
  264.  
  265.         return 0;
  266.     }
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement