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 LineDraw : MonoBehaviour
- {
- static LineDraw instance = null;
- float widthStart = 0.1f;
- float widthFinish = 0.1f;
- ShaderOptionEnum shader = ShaderOptionEnum.SelfIlluminDiffuse;
- Color color = Color.red;
- string[] shaders = {"Self-Illumin/Diffuse", "Particles/Additive"};//, "VertexLit", "Diffuse"};
- public enum ShaderOptionEnum {SelfIlluminDiffuse = 0, ParticlesAdditive};
- //public class LineData{Vector3 a, b; float s, f; ShaderOptionEnum shader; Color c1, c2;}
- public void SetWidth_(float widthStart, float widthFinish)
- {
- this.widthStart = widthStart;
- this.widthFinish = widthFinish;
- }
- public void SetShaderOption_(ShaderOptionEnum shader)
- {
- this.shader = shader;
- }
- public void SetColor_(Color color){this.color = color;}
- public Color GetColor_(){return this.color;}
- public static void SetShaderOption(ShaderOptionEnum shader){instance.SetShaderOption_(shader);}
- public static Color GetColor(){return instance.GetColor_();}
- public static void SetColor(Color color){instance.SetColor_(color);}
- public static void SetWidth(float widthStart, float widthFinish){instance.SetWidth_(widthStart, widthFinish);}
- public static void SetWidth(float width){instance.SetWidth_(width, width);}
- List<GameObject> temp = new List<GameObject>();
- List<GameObject> unusedTemp = new List<GameObject>();
- void LateUpdate()
- {
- for(int i = 0; i < temp.Count; ++i)
- {
- temp[i].active = false;
- unusedTemp.Add(temp[i]);
- }
- temp.Clear();
- // TEST CODE
- // t += Time.deltaTime;
- // if(t >= 360) t-=360;
- // Quaternion q = Quaternion.AngleAxis(t * 360, Vector3.forward);
- // Vector3 v = q * new Vector3(1,0,0);
- // LineDraw.AddTemporary(Vector3.zero, v);
- // LineDraw.AddTemporaryCircle(new Vector3(2,-2,-1), .6f, new Vector3(-1,1,1).normalized);
- }
- // TEST CODE
- // float t = 0;
- void Start()
- {
- if(instance == null)
- instance = this;
- // TEST CODE
- // GameObject g = LineDraw.AddPersistent(new Vector3(-1,-1,-2), new Vector3(5,1,-2));
- // Destroy(g, 3);
- // Vector3 v = new Vector3(1,1,1);
- // v.Normalize();
- // LineDraw.AddPersistentSpiralPlane(new Vector3(-2,2,-1), 2, v);
- }
- GameObject GetLineGameObject()
- {
- GameObject go;
- LineRenderer lineRenderer;
- if(unusedTemp.Count > 0)
- {
- int lastIndex = unusedTemp.Count-1;
- go = unusedTemp[lastIndex];
- unusedTemp.RemoveAt(lastIndex);
- go.active = true;
- lineRenderer = go.GetComponent<LineRenderer>();
- }else{
- go = new GameObject("line");
- lineRenderer = go.AddComponent<LineRenderer>();
- }
- string shaderName = shaders[(int)shader];
- if(lineRenderer.material.shader.name != shaderName)
- {
- UnityEngine.Shader shaderObject = Shader.Find(shaderName);
- lineRenderer.material = new Material(shaderObject);
- }
- lineRenderer.material.color = color;
- lineRenderer.castShadows = false;
- lineRenderer.receiveShadows = false;
- lineRenderer.SetColors(color, color);
- lineRenderer.SetWidth(widthStart, widthFinish);
- go.transform.parent = transform;
- return go;
- }
- public GameObject AddLine_(Vector3 a, Vector3 b, bool persistent)
- {
- int numPoints = 2;
- GameObject lineg = GetLineGameObject();
- LineRenderer lineRenderer = lineg.GetComponent<LineRenderer>();
- lineRenderer.SetVertexCount(numPoints);
- lineRenderer.SetPosition(0, a);
- lineRenderer.SetPosition(1, b);
- if(!persistent)
- temp.Add(lineg);
- return lineg;
- }
- float tempRotation = 0;
- public GameObject AddCircle_(Vector3 p, float r, Vector3 normal, bool persistent)
- {
- float startRotation = 0;//tempRotation * 360;
- int numPoints = 24;
- GameObject lineg = GetLineGameObject();
- LineRenderer lineRenderer = lineg.GetComponent<LineRenderer>();
- lineRenderer.SetVertexCount(numPoints+1);
- tempRotation += Time.deltaTime;
- if(tempRotation > 1) tempRotation -= 1;
- Quaternion q1 = Quaternion.LookRotation(normal);
- Vector3 right = GetRightVector(Quaternion.LookRotation(normal));
- // TEST CODE
- // Color c = GetColor_();
- // SetColor_(Color.red); AddLine_(p, GetRightVector(q1) + p,persistent);
- // SetColor_(Color.green); AddLine_(p, GetUpVector(q1) + p,persistent);
- // SetColor_(Color.blue); AddLine_(p, GetForwardVector(q1) + p,persistent);
- // SetColor_(c);
- lineRenderer.SetPosition(0, p + right * r);
- Quaternion q;
- for(int i = 0; i < numPoints; ++i)
- {
- q = Quaternion.AngleAxis(i*360.0f/24 + startRotation, normal);
- lineRenderer.SetPosition(i, p + q * right * r);
- }
- q = Quaternion.AngleAxis(startRotation, normal);
- lineRenderer.SetPosition(numPoints, p + q * right * r);
- if(!persistent)
- temp.Add(lineg);
- return lineg;
- }
- public GameObject AddSpiralPlane_(Vector3 p, int size, Vector3 normal, bool persistent)
- {
- int perCircle = 24;
- int numPoints = size*perCircle;
- GameObject lineg = GetLineGameObject();
- LineRenderer lineRenderer = lineg.GetComponent<LineRenderer>();
- Vector3 right = GetRightVector(Quaternion.LookRotation(normal));
- lineRenderer.SetVertexCount(numPoints);
- float rad = 0;
- for(int i = 0; i < numPoints; ++i)
- {
- Quaternion q = Quaternion.AngleAxis(i*360.0f/perCircle, normal);
- Vector3 delta = q * right * rad;
- lineRenderer.SetPosition(i, p + (delta * 1));
- rad += 2f/perCircle;
- }
- if(!persistent)
- temp.Add(lineg);
- return lineg;
- }
- public static GameObject AddPersistent(Vector3 a, Vector3 b){return instance.AddLine_(a, b, true);}
- public static void AddTemporary(Vector3 a, Vector3 b){instance.AddLine_(a, b, false);}
- public static GameObject AddPersistentCircle(Vector3 center, float radius, Vector3 normal)
- {return instance.AddCircle_(center, radius, normal, true);}
- public static void AddTemporaryCircle(Vector3 center, float radius, Vector3 normal)
- {instance.AddCircle_(center, radius, normal, false);}
- public static GameObject AddPersistentSpiralPlane(Vector3 center, int size, Vector3 normal)
- {return instance.AddSpiralPlane_(center, size, normal, true);}
- Vector3 GetForwardVector(Quaternion q)
- {
- return new Vector3( 2 * (q.x * q.z + q.w * q.y),
- 2 * (q.y * q.z - q.w * q.x),
- 1 - 2 * (q.x * q.x + q.y * q.y));
- }
- public static Vector3 GetUpVector(Quaternion q)
- {
- return new Vector3( 2 * (q.x * q.y - q.w * q.z),
- 1 - 2 * (q.x * q.x + q.z * q.z),
- 2 * (q.y * q.z + q.w * q.x));
- }
- public static Vector3 GetRightVector(Quaternion q)
- {
- return new Vector3( 1 - 2 * (q.y * q.y + q.z * q.z),
- 2 * (q.x * q.y + q.w * q.z),
- 2 * (q.x * q.z - q.w * q.y));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement