mvaganov

simple arbitrary-3D-line-drawing-class for Unity3D

May 29th, 2013
83
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 LineDraw : MonoBehaviour
  6. {
  7.     static LineDraw instance = null;
  8.     float widthStart = 0.1f;
  9.     float widthFinish = 0.1f;
  10.     ShaderOptionEnum shader = ShaderOptionEnum.SelfIlluminDiffuse;
  11.     Color color = Color.red;
  12.    
  13.     string[] shaders = {"Self-Illumin/Diffuse", "Particles/Additive"};//, "VertexLit", "Diffuse"}; 
  14.     public enum ShaderOptionEnum {SelfIlluminDiffuse = 0, ParticlesAdditive};
  15.     //public class LineData{Vector3 a, b; float s, f; ShaderOptionEnum shader; Color c1, c2;}
  16.    
  17.     public void SetWidth_(float widthStart, float widthFinish)
  18.     {
  19.         this.widthStart = widthStart;
  20.         this.widthFinish = widthFinish;
  21.     }
  22.    
  23.     public void SetShaderOption_(ShaderOptionEnum shader)
  24.     {
  25.         this.shader = shader;
  26.     }
  27.    
  28.     public void SetColor_(Color color)
  29.     {
  30.         this.color = color;
  31.     }
  32.    
  33.     public static void SetShaderOption(ShaderOptionEnum shader){instance.SetShaderOption_(shader);}
  34.     public static void SetColor(Color color){instance.SetColor_(color);}
  35.     public static void SetWidth(float widthStart, float widthFinish){instance.SetWidth_(widthStart, widthFinish);}
  36.    
  37.     List<GameObject> temp = new List<GameObject>();
  38.     List<GameObject> unusedTemp = new List<GameObject>();
  39.    
  40.     void LateUpdate()
  41.     {
  42.         for(int i = 0; i < temp.Count; ++i)
  43.         {
  44.             temp[i].active = false;
  45.             unusedTemp.Add(temp[i]);
  46.         }
  47.         temp.Clear();
  48.  
  49. //      t += Time.deltaTime;
  50. //      if(t >= 360) t-=360;
  51. //      Quaternion q = Quaternion.AngleAxis(t * 360, Vector3.forward);
  52. //      Vector3 v = q * new Vector3(-1,-1,-1);
  53. //      LineDraw.AddTemporary(Vector3.zero, v);
  54.     }
  55. //  float t = 0;
  56.    
  57.     void Start()
  58.     {
  59.         if(instance == null)
  60.             instance = this;
  61. //      GameObject g = LineDraw.AddPersistent(new Vector3(-1,-1,-2), new Vector3(5,1,-2));
  62. //      Destroy(g, 3);
  63.     }
  64.    
  65.     GameObject GetLineGameObject()
  66.     {
  67.         GameObject go;
  68.         if(unusedTemp.Count > 0)
  69.         {
  70.             int lastIndex = unusedTemp.Count-1;
  71.             go = unusedTemp[lastIndex];
  72.             unusedTemp.RemoveAt(lastIndex);
  73.             go.active = true;
  74.         }else{
  75.             go = new GameObject("line");
  76.             go.AddComponent<LineRenderer>();
  77.         }
  78.         go.transform.parent = transform;
  79.         return go;
  80.     }
  81.    
  82.     public GameObject AddLine_(Vector3 a, Vector3 b, bool persistent)
  83.     {
  84.         int numPoints = 2;
  85.         GameObject lineg = GetLineGameObject();
  86.         LineRenderer lineRenderer = lineg.GetComponent<LineRenderer>();
  87.         string shaderName = shaders[(int)shader];
  88.         if(lineRenderer.material.shader.name != shaderName)
  89.         {
  90.             UnityEngine.Shader shaderObject = Shader.Find(shaderName);
  91.             lineRenderer.material = new Material(shaderObject);
  92.         }
  93.         lineRenderer.material.color = color;
  94.         lineRenderer.castShadows = false;
  95.         lineRenderer.receiveShadows = false;
  96.         lineRenderer.SetColors(color, color);
  97.         lineRenderer.SetWidth(widthStart, widthFinish);
  98.         lineRenderer.SetVertexCount(numPoints);
  99.         lineRenderer.SetPosition(0, a);
  100.         lineRenderer.SetPosition(1, b);
  101.         //lineg.transform.position = (a+b)/2;
  102.         if(!persistent)
  103.         {
  104.             temp.Add(lineg);
  105.         }
  106.         return lineg;
  107.     }
  108.     public static GameObject AddPersistent(Vector3 a, Vector3 b){return instance.AddLine_(a, b, true);}
  109.     public static void AddTemporary(Vector3 a, Vector3 b){instance.AddLine_(a, b, false);}
  110. }
RAW Paste Data