- Home /
How do I change GL LINES color?
I want to draw each line with a different color. I created a material with the Diffuse shader and use GL.Color like the script reference says but pass 0 is just black, pass 1 is nothing, pass 2 is grey, and pass 3 is white no matter what color I use.
Material mat;
public Color[] colors = new Color[6] {
Color.blue,
Color.cyan,
Color.green,
Color.magenta,
Color.red,
Color.yellow
};
void Awake ()
{
mat = new Material (Shader.Find ("Diffuse"));
}
void OnPostRender ()
{
GL.PushMatrix ();
mat.SetPass (0);
GL.LoadOrtho ();
GL.Begin (GL.LINES);
for (int i = 0; i < 12; i++) {
GL.Color (colors [i % 6]);
GL.Vertex3 (0, i / 12f, 0);
GL.Vertex3 (1, i / 12f, 0);
}
GL.End ();
GL.PopMatrix ();
}
What am I doing wrong?
Answer by zharik86 · Mar 24, 2014 at 08:28 PM
Create the new material for GL, see below:
private static Material mat;
void Awake() {
mat = new Material( "Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass { " +
" Blend SrcAlpha OneMinusSrcAlpha " +
" ZWrite Off Cull Off Fog { Mode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }" );
mat.hideFlags = HideFlags.HideAndDontSave;
mat.shader.hideFlags = HideFlags.HideAndDontSave;
}
void OnPostRender() {
...
}
void OnApplicationQuit() {
DestroyImmediate(mat);
}
Thanks. This works great. Is there a reason for mat to be static? And why do you explicitly destroy mat on quit?
@slek120 The static material becomes in order that it could be used in other scripts without creating again. Therefore that it also should be destroyed when closing application, as it static. Certainly, you can use not static variable. Then everything remains the same, but thus to you, most likely, not to be necessary the OnAplicationQuit function. Therefore it is possible to delete it.
Help! Why I only got Pink lines according your method? This question has puzzled for long time ...........
thank you in advance~
color_line code:
using UnityEngine; using System.Collections;
public class color_line : $$anonymous$$onoBehaviour { private static $$anonymous$$aterial mat; public Color[] colors = new Color[6] { Color.blue, Color.cyan, Color.green, Color.magenta, Color.red, Color.yellow };
void Awake()
{
mat = new $$anonymous$$aterial("Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass { " +
" Blend SrcAlpha One$$anonymous$$inusSrcAlpha " +
" ZWrite Off Cull Off Fog { $$anonymous$$ode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }");
mat.hideFlags = HideFlags.HideAndDontSave;
mat.shader.hideFlags = HideFlags.HideAndDontSave;
}
void OnPostRender()
{
GL.Push$$anonymous$$atrix();
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
for (int i = 0; i < 12; i++)
{
GL.Color(colors[i % 6]);
GL.Vertex3(0, i / 12f, 0);
GL.Vertex3(1, i / 12f, 0);
}
GL.End();
GL.Pop$$anonymous$$atrix();
}
void OnApplicationQuit()
{
DestroyImmediate(mat);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Try just choose existing material with shader named TextShader ( GUI/TextShader) :)