Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Graph : MonoBehaviour
- {
- public class Node
- {
- public GameObject gameObject;
- public List<Edge> edges = new List<Edge> ();
- }
- public class Edge
- {
- public Node a, b;
- public GameObject gameObject;
- public float Distance ()
- {
- return Vector3.Distance (
- a.gameObject.transform.position,
- b.gameObject.transform.position);
- }
- }
- /* //prefab_graphNode must have the following script component:
- using UnityEngine;
- using System.Collections;
- public class NodeHolder : MonoBehaviour {
- public Graph.Node node;
- }
- */
- public GameObject prefab_graphNode;
- public int numberOfNodes = 5;
- public Vector3
- areaMin = new Vector3 (-10, 0, -10),
- areaMax = new Vector3 (10, 0, 10);
- List<GameObject> nodes = new List<GameObject> ();
- static float Dist (GameObject a, GameObject b)
- {
- return Vector3.Distance (a.transform.position,
- b.transform.position);
- }
- public static List<GameObject> GetClosest (
- GameObject src, int count, List<GameObject> all)
- {
- List<GameObject> r = new List<GameObject> ();
- bool addThisOne;
- for (int i = 0; i < all.Count; ++i) {
- float dist = 0, distOld = 0;
- dist = Dist (src, all [i]);
- if (r.Count >= count) {
- distOld = Dist (src, all [count - 1]);
- }
- addThisOne = src != all [i]
- && (r.Count <= count || dist < distOld);
- if (addThisOne) {
- bool added = false;
- for (int a = 0; !added && a < r.Count; ++a) {
- distOld = Dist (src, r [a]);
- if (dist < distOld) {
- r.Insert (a, all [i]);
- added = true;
- }
- }
- if (!added) r.Add (all [i]);
- }
- }
- if (r.Count > count)
- r.RemoveRange (count, r.Count - count);
- return r;
- }
- // Use this for initialization
- void Start ()
- {
- Vector3 randLoc;
- for (int i = 0; i < numberOfNodes; ++i) {
- randLoc = new Vector3 (
- Random.Range (areaMin.x, areaMax.x),
- Random.Range (areaMin.y, areaMax.y),
- Random.Range (areaMin.z, areaMax.z));
- GameObject go = (GameObject)Instantiate (
- prefab_graphNode, randLoc,
- Quaternion.identity);
- go.transform.parent = transform;
- nodes.Add (go);
- Graph.Node n = new Graph.Node ();
- n.gameObject = go;
- go.GetComponent<NodeHolder> ().node = n;
- }
- for (int a = 0; a < numberOfNodes; ++a) {
- List<GameObject> con = GetClosest (nodes [a], 3, nodes);
- Graph.Node n = nodes [a].GetComponent<NodeHolder> ().node;
- for (int e = 0; e < con.Count; ++e) {
- Edge ed = new Edge ();
- ed.a = n;
- ed.b = con [e].GetComponent<NodeHolder> ().node;
- n.edges.Add (ed);
- CreateEdgeObject (ed);
- }
- }
- }
- LineRenderer CreateEdgeObject (Edge e)
- {
- GameObject lineObj = new GameObject ("edge");
- e.gameObject = lineObj;
- return SetupEdgeObject(e);
- }
- LineRenderer SetupEdgeObject(Edge e)
- {
- Vector3 start = e.a.gameObject.transform.position;
- Vector3 end = e.b.gameObject.transform.position;
- string shaderName = "Self-Illumin/Diffuse";
- UnityEngine.Shader shaderObject = Shader.Find (shaderName);
- LineRenderer liner = e.gameObject.GetComponent<LineRenderer>();
- if(liner == null)
- {
- liner = e.gameObject.AddComponent<LineRenderer> ();
- liner.material = new Material (shaderObject);
- liner.material.color = Color.white;
- liner.SetWidth (.2f, 0f);
- liner.transform.parent = transform;
- liner.SetVertexCount (2);
- }
- liner.SetPosition (0, start);
- liner.SetPosition (1, end);
- return liner;
- }
- void RefreshEdgeCoordinates()
- {
- Node n;
- for(int i = 0; i < nodes.Count; ++i)
- {
- n = nodes[i].GetComponent<NodeHolder>().node;
- for(int e = 0; e < n.edges.Count; ++e)
- {
- SetupEdgeObject(n.edges[e]);
- }
- }
- }
- float timer = 0;
- void Update ()
- {
- timer += Time.deltaTime;
- if(timer > 1)
- {
- timer -= 1;
- RefreshEdgeCoordinates();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement