Camera GL drawing line

/***********************************************
Copyright (C) 2018 The Company Name
File Name:           CameraGLRender.cs
Author:              拾忆丶夜
UnityVersion:       #UnityVersion#
CreateTime:          #CreateTime#
Description:         摄像机GL画线
***********************************************/

using UnityEngine;

public class CameraGLRender : MonoBehaviour
{
    public Color rectColor = Color.white;

    public Material rectMat = null; //使用Sprite下的default材质!!!

    [HideInInspector]
    public bool drawRectangle = false;

    [HideInInspector]
    public Vector3 start;

    //画线操作
    private void OnPostRender()
    {
        if (drawRectangle)
        {
            Vector3 end = Input.mousePosition;//鼠标当前位置
            GL.PushMatrix();//保存摄像机变换矩阵,把投影视图矩阵和模型视图矩阵压入堆栈保存

            if (!rectMat)
                return;

            rectMat.SetPass(0);//为渲染激活给定的pass。
            GL.LoadPixelMatrix();//设置用屏幕坐标绘图
            GL.Begin(GL.QUADS);//开始绘制矩形

            GL.Color(new Color(rectColor.r, rectColor.g, rectColor.b, 0.1f));//设置颜色和透明度,方框内部透明
            //绘制顶点
            GL.Vertex3(start.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.End();

            GL.Begin(GL.LINES);//开始绘制线
            GL.Color(rectColor);//设置方框的边框颜色 边框不透明

            GL.Vertex3(start.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, start.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(end.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.Vertex3(start.x, end.y, 0);
            GL.Vertex3(start.x, start.y, 0);
            GL.End();

            GL.PopMatrix();//恢复摄像机投影矩阵
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_33325776/article/details/129788844