Why my GL.Color doesn't work?

Hi, I wanna to draw a mini-map using the GL class, and first, i wanna to draw a semi-transparent background which is a quad and then draw some lines to represent the road in my scene.And i wanna to use a different color to draw the line, so i use GL.Color(Color.yellow), but it seems that i doesn't work at all. Could you help me find the bug? Here's my code:

using UnityEngine;
using System.Collections;

public class GLUtility : MonoBehaviour
{
    public Material _glMaterial;

    private Vector3 _startVertex = new Vector3(0, 0, 0);
    private Vector3 _mousePos;

    void Update()
    {
        _mousePos = Input.mousePosition;
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // _startVertex = new Vector3(_mousePos.x / Screen.width, _mousePos.y / Screen.height, 0);
            _startVertex = new Vector3(_mousePos.x, _mousePos.y, 0);
        }
    }
    //Note that the OpenGL's coordinate system in Unity is right-hand rule.And the left-bottom is (0,0), and right-top is (1,1) when you use GL.LoadOrtho()
    void OnPostRender()
    {
        if (_glMaterial == null)
        {
            print("Please assign a material on the inspector");
            return;
        }
        GL.PushMatrix();
        _glMaterial.SetPass(0);

        GL.LoadPixelMatrix();
        GL.Viewport(new Rect(0, 0, Screen.width, Screen.height));
        //Draw the backgroud
        GL.Color(new Color(0.5f, 0.0f, 0.0f, 0.5f));
        GL.Begin(GL.QUADS);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(0, Screen.height / 3, 0);
        GL.Vertex3(Screen.width / 3, Screen.height / 3, 0);
        GL.Vertex3(Screen.width / 3, 0, 0);
        GL.End();
        //Draw line
        GL.Color(Color.yellow);
        GL.Begin(GL.LINES);
        GL.Vertex(_startVertex);
        GL.Vertex(new Vector3(_mousePos.x, _mousePos.y, 0));
        GL.End();
        GL.PopMatrix();
    }
}

Thank you very much.

2 Answers

2

See Statement's answer. Also, you need to make sure the shader on the material you are using actually makes use of the vertex color. Most of the built-in shaders don't.

Good catch. The wiki has some colored shaders also, like this one http://unifycommunity.com/wiki/index.php?title=VertexColorUnlit

This is found in an example in the GL.Color docs.

This function can only be called between GL.Begin and GL.End functions.

So...

  // Error: Not between GL.Begin - GL.End
  GL.Color(new Color(0.5f, 0.0f, 0.0f, 0.5f)); 
GL.Begin(GL.QUADS);
  GL.Vertex3(0, 0, 0);
  GL.Vertex3(0, Screen.height / 3, 0);
  GL.Vertex3(Screen.width / 3, Screen.height / 3, 0);
  GL.Vertex3(Screen.width / 3, 0, 0);
GL.End();

should be

GL.Begin(GL.QUADS);
  GL.Color(new Color(0.5f, 0.0f, 0.0f, 0.5f));
  GL.Vertex3(0, 0, 0);
  GL.Vertex3(0, Screen.height / 3, 0);
  GL.Vertex3(Screen.width / 3, Screen.height / 3, 0);
  GL.Vertex3(Screen.width / 3, 0, 0);
GL.End();

and

  // Error: Not between GL.Begin - GL.End
  GL.Color(Color.yellow); 
GL.Begin(GL.LINES);
  GL.Vertex(_startVertex);
  GL.Vertex(new Vector3(_mousePos.x, _mousePos.y, 0));
GL.End();

should be

GL.Begin(GL.LINES);
  GL.Color(Color.yellow);
  GL.Vertex(_startVertex);
  GL.Vertex(new Vector3(_mousePos.x, _mousePos.y, 0));
GL.End();

HiI changed the shader to vertex type and it works, thank you very much.And official document actually says GL.Color() can only be called between GL.Begin and GL.End functions.But when i use GL.Color() out of GL.Beigin() and GL.End(), it works too, there seems no limit using the GL.Color()