mvaganov

Line drawing in Unity3D, now with circles!

May 30th, 2013
795
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){this.color = color;}
  29.     public Color GetColor_(){return this.color;}
  30.    
  31.     public static void SetShaderOption(ShaderOptionEnum shader){instance.SetShaderOption_(shader);}
  32.     public static Color GetColor(){return instance.GetColor_();}
  33.     public static void SetColor(Color color){instance.SetColor_(color);}
  34.     public static void SetWidth(float widthStart, float widthFinish){instance.SetWidth_(widthStart, widthFinish);}
  35.     public static void SetWidth(float width){instance.SetWidth_(width, width);}
  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.         // TEST CODE
  50. //      t += Time.deltaTime;
  51. //      if(t >= 360) t-=360;
  52. //      Quaternion q = Quaternion.AngleAxis(t * 360, Vector3.forward);
  53. //      Vector3 v = q * new Vector3(1,0,0);
  54. //      LineDraw.AddTemporary(Vector3.zero, v);
  55. //      LineDraw.AddTemporaryCircle(new Vector3(2,-2,-1), .6f, new Vector3(-1,1,1).normalized);
  56.     }
  57.     // TEST CODE
  58. //  float t = 0;
  59.    
  60.     void Start()
  61.     {
  62.         if(instance == null)
  63.             instance = this;
  64.         // TEST CODE
  65. //      GameObject g = LineDraw.AddPersistent(new Vector3(-1,-1,-2), new Vector3(5,1,-2));
  66. //      Destroy(g, 3);
  67. //      Vector3 v = new Vector3(1,1,1);
  68. //      v.Normalize();
  69. //      LineDraw.AddPersistentSpiralPlane(new Vector3(-2,2,-1), 2, v);
  70.     }
  71.    
  72.     GameObject GetLineGameObject()
  73.     {
  74.         GameObject go;
  75.         LineRenderer lineRenderer;
  76.         if(unusedTemp.Count > 0)
  77.         {
  78.             int lastIndex = unusedTemp.Count-1;
  79.             go = unusedTemp[lastIndex];
  80.             unusedTemp.RemoveAt(lastIndex);
  81.             go.active = true;
  82.             lineRenderer = go.GetComponent<LineRenderer>();
  83.         }else{
  84.             go = new GameObject("line");
  85.             lineRenderer = go.AddComponent<LineRenderer>();
  86.         }
  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.         go.transform.parent = transform;
  99.         return go;
  100.     }
  101.    
  102.     public GameObject AddLine_(Vector3 a, Vector3 b, bool persistent)
  103.     {
  104.         int numPoints = 2;
  105.         GameObject lineg = GetLineGameObject();
  106.         LineRenderer lineRenderer = lineg.GetComponent<LineRenderer>();
  107.         lineRenderer.SetVertexCount(numPoints);
  108.         lineRenderer.SetPosition(0, a);
  109.         lineRenderer.SetPosition(1, b);
  110.         if(!persistent)
  111.             temp.Add(lineg);
  112.         return lineg;
  113.     }
  114.     float tempRotation = 0;
  115.     public GameObject AddCircle_(Vector3 p, float r, Vector3 normal, bool persistent)
  116.     {
  117.         float startRotation = 0;//tempRotation * 360;
  118.         int numPoints = 24;
  119.         GameObject lineg = GetLineGameObject();
  120.         LineRenderer lineRenderer = lineg.GetComponent<LineRenderer>();
  121.         lineRenderer.SetVertexCount(numPoints+1);
  122.         tempRotation += Time.deltaTime;
  123.         if(tempRotation > 1) tempRotation -= 1;
  124.         Quaternion q1 = Quaternion.LookRotation(normal);
  125.         Vector3 right = GetRightVector(Quaternion.LookRotation(normal));
  126.         // TEST CODE
  127. //      Color c = GetColor_();
  128. //          SetColor_(Color.red);   AddLine_(p, GetRightVector(q1) + p,persistent);
  129. //          SetColor_(Color.green); AddLine_(p, GetUpVector(q1) + p,persistent);
  130. //          SetColor_(Color.blue);  AddLine_(p, GetForwardVector(q1) + p,persistent);
  131. //      SetColor_(c);
  132.         lineRenderer.SetPosition(0, p + right * r);
  133.         Quaternion q;
  134.         for(int i = 0; i < numPoints; ++i)
  135.         {
  136.             q = Quaternion.AngleAxis(i*360.0f/24 + startRotation, normal);
  137.             lineRenderer.SetPosition(i, p + q * right * r);
  138.         }
  139.         q = Quaternion.AngleAxis(startRotation, normal);
  140.         lineRenderer.SetPosition(numPoints, p + q * right * r);
  141.         if(!persistent)
  142.             temp.Add(lineg);
  143.         return lineg;
  144.     }
  145.     public GameObject AddSpiralPlane_(Vector3 p, int size, Vector3 normal, bool persistent)
  146.     {
  147.         int perCircle = 24;
  148.         int numPoints = size*perCircle;
  149.         GameObject lineg = GetLineGameObject();
  150.         LineRenderer lineRenderer = lineg.GetComponent<LineRenderer>();
  151.         Vector3 right = GetRightVector(Quaternion.LookRotation(normal));
  152.         lineRenderer.SetVertexCount(numPoints);
  153.         float rad = 0;
  154.         for(int i = 0; i < numPoints; ++i)
  155.         {
  156.             Quaternion q = Quaternion.AngleAxis(i*360.0f/perCircle, normal);
  157.             Vector3 delta = q * right * rad;
  158.             lineRenderer.SetPosition(i, p + (delta * 1));
  159.             rad += 2f/perCircle;
  160.         }          
  161.         if(!persistent)
  162.             temp.Add(lineg);
  163.         return lineg;
  164.     }
  165.     public static GameObject AddPersistent(Vector3 a, Vector3 b){return instance.AddLine_(a, b, true);}
  166.     public static void AddTemporary(Vector3 a, Vector3 b){instance.AddLine_(a, b, false);}
  167.     public static GameObject AddPersistentCircle(Vector3 center, float radius, Vector3 normal)
  168.     {return instance.AddCircle_(center, radius, normal, true);}
  169.     public static void AddTemporaryCircle(Vector3 center, float radius, Vector3 normal)
  170.     {instance.AddCircle_(center, radius, normal, false);}
  171.     public static GameObject AddPersistentSpiralPlane(Vector3 center, int size, Vector3 normal)
  172.     {return instance.AddSpiralPlane_(center, size, normal, true);}
  173.  
  174.     Vector3 GetForwardVector(Quaternion q)
  175.     {
  176.         return new Vector3( 2 * (q.x * q.z + q.w * q.y),
  177.                         2 * (q.y * q.z - q.w * q.x),
  178.                         1 - 2 * (q.x * q.x + q.y * q.y));
  179.     }
  180.     public static Vector3 GetUpVector(Quaternion q)
  181.     {
  182.         return new Vector3( 2 * (q.x * q.y - q.w * q.z),
  183.                         1 - 2 * (q.x * q.x + q.z * q.z),
  184.                         2 * (q.y * q.z + q.w * q.x));
  185.     }
  186.     public static Vector3  GetRightVector(Quaternion q)
  187.     {
  188.         return new Vector3( 1 - 2 * (q.y * q.y + q.z * q.z),
  189.                         2 * (q.x * q.y + q.w * q.z),
  190.                         2 * (q.x * q.z - q.w * q.y));
  191.     }
  192. }
RAW Paste Data