Advertisement
SebastianLague

Marching Squares Tutorial Visualization

Mar 2nd, 2015
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.68 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System.Collections.Generic;
  5.  
  6.  
  7. public class Manager : MonoBehaviour {
  8.  
  9.     List<Vector3> vertices;
  10.     List<List<int>> triangles;
  11.     public GraphicNode[] nodes;
  12.     public Transform[] nodeP;
  13.  
  14.     public TextMesh binaryText;
  15.     public TextMesh configText;
  16.  
  17.     public void SelectNode(int index) {
  18.         nodes[index].Toggle();
  19.         int val = 0;
  20.         int n = 8;
  21.         string binary = "";
  22.         for (int i =0; i < nodes.Length; i ++) {
  23.             if (nodes[i].isControl) {
  24.                 if (nodes[i].active) {
  25.                     binary += "1";
  26.                     val += n;
  27.                 }
  28.                 else {
  29.                     binary += "0";
  30.                 }
  31.                 n = n >> 1;
  32.                 //print (n);
  33.             }
  34.         }
  35.         //print (val);
  36.         binaryText.text = binary;
  37.         configText.text ="=" +val;
  38.         Triangulate(val);
  39.  
  40.         Mesh mesh = new Mesh();
  41.         GetComponent<MeshFilter>().mesh = mesh;
  42.         GetComponent<MeshRenderer>();
  43.  
  44.         mesh.subMeshCount = triangles.Count;
  45.         mesh.vertices = vertices.ToArray();
  46.         for (int i = 0; i < triangles.Count; i ++) {
  47.             mesh.SetTriangles(triangles[i].ToArray(),i);
  48.         }
  49.  
  50.    
  51.         //mesh.triangles = triangles.ToArray();
  52.         mesh.RecalculateNormals();
  53.     }
  54.  
  55.     void Triangulate(int configuration) {
  56.         for (int i = 0; i < nodes.Length ; i ++) {
  57.             if (nodes[i].isControl == false) {
  58.                 nodes[i].Clear();
  59.             }
  60.         }
  61.         vertices = new List<Vector3>();
  62.         triangles = new List<List<int>>();
  63.  
  64.         switch (configuration) {
  65.         case 0: // 0000
  66.             return;
  67.         case 15: // 1111
  68.             MeshFromPoints(nodeP[0].position, nodeP[2].position, nodeP[4].position, nodeP[6].position);
  69.             Active(0,2,4,6);
  70.             break;
  71.  
  72.             // 1 point:
  73.         case 8: // 1000
  74.             //print ("AWE");
  75.             MeshFromPoints(nodeP[0].position, nodeP[1].position, nodeP[7].position);
  76.             Active(0,1,7);
  77.             break;
  78.         case 4: // 0100
  79.             MeshFromPoints(nodeP[1].position,nodeP[2].position, nodeP[3].position);
  80.             Active(1,2,3);
  81.             break;
  82.         case 2: // 0010
  83.             MeshFromPoints(nodeP[3].position,nodeP[4].position, nodeP[5].position);
  84.             Active(3,4,5);
  85.             break;
  86.         case 1: // 0001
  87.             MeshFromPoints( nodeP[5].position,nodeP[6].position, nodeP[7].position);
  88.             Active(5,6,7);
  89.             break;
  90.            
  91.             // 2 points:
  92.         case 12: // 1100
  93.             MeshFromPoints(nodeP[0].position, nodeP[2].position, nodeP[3].position, nodeP[7].position);
  94.             Active(0,2,3,7);
  95.             break;
  96.         case 9: // 1001
  97.             Active(0,1,5,6);
  98.             MeshFromPoints(nodeP[0].position, nodeP[1].position, nodeP[5].position, nodeP[6].position);
  99.             break;
  100.         case 6: // 0110
  101.             Active(1,2,4,5);
  102.             MeshFromPoints( nodeP[1].position,nodeP[2].position, nodeP[4].position, nodeP[5].position);
  103.             break;
  104.         case 3: // 0011
  105.             Active(3,4,6,7);
  106.             MeshFromPoints(nodeP[3].position,nodeP[4].position, nodeP[6].position, nodeP[7].position );
  107.             break;
  108.         case 10: // 1010
  109.             Active(0,1,3,4,5,7);
  110.             MeshFromPoints(nodeP[0].position, nodeP[1].position, nodeP[3].position, nodeP[4].position, nodeP[5].position, nodeP[7].position);
  111.             break;
  112.         case 5: // 0101
  113.             Active(1,2,3,5,6,7);
  114.             MeshFromPoints(nodeP[1].position, nodeP[2].position, nodeP[3].position, nodeP[5].position, nodeP[6].position, nodeP[7].position);
  115.             break;
  116.            
  117.             // 3 points:
  118.         case 14: // 1110
  119.             Active(0,2,4,5,7);
  120.             MeshFromPoints(nodeP[0].position, nodeP[2].position, nodeP[4].position, nodeP[5].position, nodeP[7].position);
  121.             break;
  122.         case 11: // 1011
  123.             Active(0,1,3,4,6);
  124.             MeshFromPoints(nodeP[0].position, nodeP[1].position, nodeP[3].position, nodeP[4].position, nodeP[6].position);
  125.             break;
  126.         case 7: // 0111
  127.             Active(1,2,4,6,7);
  128.             MeshFromPoints(nodeP[1].position, nodeP[2].position, nodeP[4].position, nodeP[6].position, nodeP[7].position);
  129.             break;
  130.         case 13: // 1101
  131.             Active(0,2,3,5,6);
  132.             MeshFromPoints(nodeP[0].position, nodeP[2].position, nodeP[3].position, nodeP[5].position, nodeP[6].position);
  133.             break;
  134.         }
  135.     }
  136.  
  137.     void Active(params int[] a) {
  138.         string letters = "ABCDEFGHIJKLMOP";
  139.         for (int i = 0; i < a.Length ; i ++) {
  140.             nodes[a[i]].SetActive();
  141.             nodes[a[i]].text.text = letters[i] + "";
  142.         }
  143.     }
  144.  
  145.     void MeshFromPoints(params Vector3[] nodes) {
  146.         for (int i = 0; i < nodes.Length; i ++) {
  147.             //print (nodes[i]);
  148.         }
  149.         if (nodes.Length >= 3)
  150.             CreateTriangle(nodes[0],nodes[1],nodes[2]);
  151.         if (nodes.Length >= 4)
  152.             CreateTriangle(nodes[0],nodes[2],nodes[3]);
  153.         if (nodes.Length >= 5)
  154.             CreateTriangle(nodes[0],nodes[3],nodes[4]);
  155.         if (nodes.Length >= 6)
  156.             CreateTriangle(nodes[0],nodes[4],nodes[5]);
  157.     }
  158.  
  159.    
  160.     void CreateTriangle(Vector3 a, Vector3 b, Vector3 c) {
  161.         vertices.Add(a);
  162.         vertices.Add(b);
  163.         vertices.Add(c);
  164.  
  165.         List<int> t = new List<int>();
  166.  
  167.         t.Add(vertices.Count-3);
  168.         t.Add(vertices.Count-2);
  169.         t.Add(vertices.Count-1);
  170.         triangles.Add(t);
  171.     }
  172.  
  173. }
  174.  
  175. using UnityEngine;
  176. using System.Collections;
  177. using UnityEngine.UI;
  178.  
  179. public class GraphicNode : MonoBehaviour {
  180.  
  181.     public int index;
  182.     public bool isControl;
  183.  
  184.     [HideInInspector]
  185.     public bool active;
  186.  
  187.     public TextMesh text;
  188.  
  189.     Color controlSel;
  190.     Color controlDes;
  191.  
  192.     Color sel;
  193.     Color des;
  194.  
  195.     void Awake() {
  196.         controlSel = Col (161,62,62);
  197.         controlDes = Col (255,255,255);
  198.  
  199.         sel = Col (225,225,225);
  200.         des = Col (105,105,105);
  201.         text.text = "";
  202.         Image i = GetComponent<Image>();
  203.         if (!isControl) {
  204.             i.color = (active)?sel:des;
  205.         }
  206.         else {
  207.             i.color = (active)?controlSel:controlDes;
  208.         }
  209.     }
  210.  
  211.     Color Col(float r, float g, float b) {
  212.         return new Color(r/255f,g/255f, b/255f);
  213.     }
  214.  
  215.     public void Toggle() {
  216.         active = !active;
  217.         Image i = GetComponent<Image>();
  218.  
  219.         if (!isControl) {
  220.             i.color = (active)?sel:des;
  221.         }
  222.         else {
  223.             i.color = (active)?controlSel:controlDes;
  224.         }
  225.  
  226.         if (!active)
  227.             text.text = "";
  228.     }
  229.  
  230.     public void SetActive() {
  231.         active = false;
  232.         Toggle();
  233.     }
  234.  
  235.     public void Clear() {
  236.         active = true;
  237.         Toggle();
  238.     }
  239.    
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement