forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawGLLine.cs
54 lines (45 loc) · 1.82 KB
/
DrawGLLine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using UnityEngine;
// Draws lines with GL : https://docs.unity3d.com/ScriptReference/GL.html
// Usage: Attach this script to gameobject in scene
namespace UnityLibrary
{
public class DrawGLLine : MonoBehaviour
{
public Color lineColor = Color.red;
Material lineMaterial;
void Awake()
{
// must be called before trying to draw lines..
CreateLineMaterial();
}
void CreateLineMaterial()
{
// Unity has a built-in shader that is useful for drawing simple colored things
var shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// Turn on alpha blending
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// Turn backface culling off
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// Turn off depth writes
lineMaterial.SetInt("_ZWrite", 0);
}
// cannot call this on update, line wont be visible then.. and if used OnPostRender() thats works when attached to camera only
void OnRenderObject()
{
lineMaterial.SetPass(0);
GL.PushMatrix();
GL.MultMatrix(transform.localToWorldMatrix);
GL.Begin(GL.LINES);
GL.Color(lineColor);
// start line from transform position
GL.Vertex(transform.position);
// end line 100 units forward from transform position
GL.Vertex(transform.position + transform.forward * 100);
GL.End();
GL.PopMatrix();
}
}
}