使用OpenGL在Unity中画一个框

转载自:http://blog.csdn.net/xv_ly15/article/details/9047509

Unity3D使用的图形引擎是DirectX,OpenGL和自带的APi(Wii)

我们这里使用OpenGL的渲染方式



代码:

(使用过OpenGl的应该相对容易理解

另外:代码中使用Shader是因为矩形框中部的透明部分需要)

    using UnityEngine;  
    using System.Collections;  
      
    public class DrawRect : MonoBehaviour {  
      
        private Vector2 mMouseStart, mMouseEnd;  
        private bool mBDrawMouseRect;  
      
        private Material rectMat = null;//画线的材质 不设定系统会用当前材质画线 结果不可控  
      
        void Start()  
        {  
      
            mBDrawMouseRect = false;  
      
            rectMat = 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 }" +  
                "} } }");//生成画线的材质  
            rectMat.hideFlags = HideFlags.HideAndDontSave;  
            rectMat.shader.hideFlags = HideFlags.HideAndDontSave;  
        }  
      
        void Update()  
        {  
            if (Input.GetMouseButtonDown(0))  
            //按下鼠标左键  
            {  
                Vector3 mousePosition = Input.mousePosition;  
                mMouseStart = new Vector2(mousePosition.x, mousePosition.y);  
            }  
      
            if (Input.GetMouseButton(0))  
            //持续按下鼠标左键  
            {  
                mBDrawMouseRect = true;  
                Vector3 mousePosition = Input.mousePosition;  
                mMouseEnd = new Vector2(mousePosition.x, mousePosition.y);  
            }  
      
            if (Input.GetMouseButtonUp(0))  
            {  
                mBDrawMouseRect = false;  
            }  
        }  
      
        void OnGUI()  
        {  
            if (mBDrawMouseRect)  
                Draw(mMouseStart, mMouseEnd);  
        }  
      
        //渲染2D框  
        void Draw(Vector2 start, Vector2 end)  
        {  
            rectMat.SetPass(0);  
      
            GL.PushMatrix();//保存摄像机变换矩阵  
      
            Color clr = Color.green;  
            clr.a = 0.1f;  
      
            GL.LoadPixelMatrix();//设置用屏幕坐标绘图  
            //透明框  
            GL.Begin(GL.QUADS);  
            GL.Color(clr);  
            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(Color.green);  
            GL.Vertex3(start.x, start.y, 0);  
            GL.Vertex3(end.x, start.y, 0);  
            GL.End();  
      
            //下  
            GL.Begin(GL.LINES);  
            GL.Color(Color.green);  
            GL.Vertex3(start.x, end.y, 0);  
            GL.Vertex3(end.x, end.y, 0);  
            GL.End();  
      
            //左  
            GL.Begin(GL.LINES);  
            GL.Color(Color.green);  
            GL.Vertex3(start.x, start.y, 0);  
            GL.Vertex3(start.x, end.y, 0);  
            GL.End();  
      
            //右  
            GL.Begin(GL.LINES);  
            GL.Color(Color.green);  
            GL.Vertex3(end.x, start.y, 0);  
            GL.Vertex3(end.x, end.y, 0);  
            GL.End();  
      
            GL.PopMatrix();//还原  
        }  
    }  


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值