Quantcast
Channel: Latest Questions by Yanger_xy
Viewing all articles
Browse latest Browse all 61

Why my GL.Color doesn't work?

$
0
0

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.


Viewing all articles
Browse latest Browse all 61

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>