mvaganov

Graph Generation for Unity3D

Jun 4th, 2013
210
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Graph : MonoBehaviour
  6. {  
  7.     public class Node
  8.     {
  9.         public GameObject gameObject;
  10.         public List<Edge> edges = new List<Edge> ();
  11.     }
  12.  
  13.     public class Edge
  14.     {
  15.         public Node a, b;
  16.         public GameObject gameObject;
  17.         public float Distance ()
  18.         {
  19.             return Vector3.Distance (
  20.                 a.gameObject.transform.position,
  21.                 b.gameObject.transform.position);
  22.         }
  23.     }
  24. /* //prefab_graphNode must have the following script component:
  25. using UnityEngine;
  26. using System.Collections;
  27. public class NodeHolder : MonoBehaviour {
  28.     public Graph.Node node;
  29. }
  30. */
  31.     public GameObject prefab_graphNode;
  32.     public int numberOfNodes = 5;
  33.     public Vector3
  34.         areaMin = new Vector3 (-10, 0, -10),
  35.         areaMax = new Vector3 (10, 0, 10);
  36.     List<GameObject> nodes = new List<GameObject> ();  
  37.    
  38.     static float Dist (GameObject a, GameObject b)
  39.     {
  40.         return Vector3.Distance (a.transform.position,
  41.             b.transform.position);
  42.     }
  43.  
  44.     public static List<GameObject> GetClosest (
  45.         GameObject src, int count, List<GameObject> all)
  46.     {
  47.         List<GameObject> r = new List<GameObject> ();
  48.         bool addThisOne;
  49.         for (int i = 0; i < all.Count; ++i) {
  50.             float dist = 0, distOld = 0;
  51.             dist = Dist (src, all [i]);
  52.             if (r.Count >= count) {
  53.                 distOld = Dist (src, all [count - 1]);
  54.             }
  55.             addThisOne = src != all [i]
  56.                 && (r.Count <= count || dist < distOld);
  57.             if (addThisOne) {
  58.                 bool added = false;
  59.                 for (int a = 0; !added && a < r.Count; ++a) {
  60.                     distOld = Dist (src, r [a]);
  61.                     if (dist < distOld) {
  62.                         r.Insert (a, all [i]);
  63.                         added = true;
  64.                     }
  65.                 }
  66.                 if (!added) r.Add (all [i]);
  67.             }
  68.         }
  69.         if (r.Count > count)
  70.             r.RemoveRange (count, r.Count - count);
  71.         return r;
  72.     }
  73.  
  74.     // Use this for initialization
  75.     void Start ()
  76.     {
  77.         Vector3 randLoc;
  78.         for (int i = 0; i < numberOfNodes; ++i) {
  79.             randLoc = new Vector3 (
  80.                 Random.Range (areaMin.x, areaMax.x),
  81.                 Random.Range (areaMin.y, areaMax.y),
  82.                 Random.Range (areaMin.z, areaMax.z));
  83.             GameObject go = (GameObject)Instantiate (
  84.                 prefab_graphNode, randLoc,
  85.                 Quaternion.identity);              
  86.             go.transform.parent = transform;
  87.             nodes.Add (go);
  88.             Graph.Node n = new Graph.Node ();
  89.             n.gameObject = go;
  90.             go.GetComponent<NodeHolder> ().node = n;
  91.         }
  92.         for (int a = 0; a < numberOfNodes; ++a) {
  93.             List<GameObject> con = GetClosest (nodes [a], 3, nodes);
  94.             Graph.Node n = nodes [a].GetComponent<NodeHolder> ().node;
  95.             for (int e = 0; e < con.Count; ++e) {
  96.                 Edge ed = new Edge ();
  97.                 ed.a = n;
  98.                 ed.b = con [e].GetComponent<NodeHolder> ().node;
  99.                 n.edges.Add (ed);
  100.                 CreateEdgeObject (ed);
  101.             }
  102.         }
  103.     }
  104.  
  105.     LineRenderer CreateEdgeObject (Edge e)
  106.     {
  107.         GameObject lineObj = new GameObject ("edge");
  108.         e.gameObject = lineObj;
  109.         return SetupEdgeObject(e);
  110.     }
  111.     LineRenderer SetupEdgeObject(Edge e)
  112.     {
  113.         Vector3 start = e.a.gameObject.transform.position;
  114.         Vector3 end = e.b.gameObject.transform.position;
  115.         string shaderName = "Self-Illumin/Diffuse";
  116.         UnityEngine.Shader shaderObject = Shader.Find (shaderName);
  117.         LineRenderer liner = e.gameObject.GetComponent<LineRenderer>();
  118.         if(liner == null)
  119.         {
  120.             liner = e.gameObject.AddComponent<LineRenderer> ();
  121.             liner.material = new Material (shaderObject);
  122.             liner.material.color = Color.white;
  123.             liner.SetWidth (.2f, 0f);
  124.             liner.transform.parent = transform;
  125.             liner.SetVertexCount (2);
  126.         }
  127.         liner.SetPosition (0, start);
  128.         liner.SetPosition (1, end);
  129.         return liner;
  130.     }
  131.     void RefreshEdgeCoordinates()
  132.     {
  133.         Node n;
  134.         for(int i = 0; i < nodes.Count; ++i)
  135.         {
  136.             n = nodes[i].GetComponent<NodeHolder>().node;
  137.             for(int e = 0; e < n.edges.Count; ++e)
  138.             {
  139.                 SetupEdgeObject(n.edges[e]);
  140.             }
  141.         }
  142.     }
  143.     float timer = 0;
  144.     void Update ()
  145.     {
  146.         timer += Time.deltaTime;
  147.         if(timer > 1)
  148.         {
  149.             timer -= 1;
  150.             RefreshEdgeCoordinates();
  151.         }
  152.     }
  153. }
RAW Paste Data