zoukankan      html  css  js  c++  java
  • Unity之屏幕画线

    using UnityEngine;
    using System.Collections;
    
    public class DrawRectangle : MonoBehaviour {
    
    public Color rectColor = Color.green;
    
    
    private Material rectMat = null;//画线的材质 不设定系统会用当前材质画线 结果不可控
    
    // Use this for initialization
    
    void Start () {
    
    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 () {
    }
    
    
    
    void OnPostRender() {//画线这种操作推荐在OnPostRender()里进行 而不是直接放在Update,所以需要标志来开启
    
    Rect rect0 = new Rect(0,0,0,0);
    
    
    drawRect(rect0);
    
    }
    
     
    
    void drawRect(Rect rect0){
    if (! rectMat) 
    return;
    GL.PushMatrix();//保存摄像机变换矩阵
    rectMat.SetPass( 0 );
    
    GL.LoadPixelMatrix();//设置用屏幕坐标绘图
    
    // GL.Begin(GL.QUADS);
    
    // GL.Color( new Color(rectColor.r,rectColor.g,rectColor.b,0.1f) );//设置颜色和透明度,方框内部透明
    
    // GL.Vertex3( 0,0,0);
    
    // GL.Vertex3( Screen.width/2,0,0);
    
    // GL.Vertex3( Screen.width/2,Screen.height/2,0 );
    
    // GL.Vertex3( 0,Screen.height/2,0 );
    
    // GL.End();
    float startX = rect0.x;
    float startY = rect0.y;
    float endX = rect0.xMax;
    float endY = rect0.yMax;
    
    GL.Begin(GL.LINES);
    
    GL.Color(rectColor);//设置方框的边框颜色 边框不透明
    
    GL.Vertex3( startX,startY,0);
    GL.Vertex3( endX,startY,0);
    
    GL.Vertex3( endX,startY,0);
    GL.Vertex3( endX,endY,0 );
    
    GL.Vertex3( endX,endY,0 );
    GL.Vertex3( startX,endY,0 );
    
    GL.Vertex3( startX,endY,0 );
    GL.Vertex3( startX,startY,0);
    
    GL.End();
    // GL.Begin(GL.LINES);
    // GL.Vertex3(0, 0, 0);
    // GL.Vertex3(Screen.width, Screen.height, 0);
    // GL.End(); 
    GL.PopMatrix();//恢复摄像机投影矩阵
    }
    
    
    void drawRects(Rect[] rects){
    if (! rectMat) 
    return;
    GL.PushMatrix();//保存摄像机变换矩阵
    rectMat.SetPass( 0 );
    
    GL.LoadPixelMatrix();//设置用屏幕坐标绘图
    
    // GL.Begin(GL.QUADS);
    
    // GL.Color( new Color(rectColor.r,rectColor.g,rectColor.b,0.1f) );//设置颜色和透明度,方框内部透明
    
    // GL.Vertex3( 0,0,0);
    
    // GL.Vertex3( Screen.width/2,0,0);
    
    // GL.Vertex3( Screen.width/2,Screen.height/2,0 );
    
    // GL.Vertex3( 0,Screen.height/2,0 );
    
    // GL.End();
    GL.Begin(GL.LINES);
    for(int i = 0 ; i < rects.Length ; i++){
    
    Rect rect0 = rects[i];
    //Debug.Log(rect0);
    float startX = rect0.x;
    float startY = rect0.y;
    float endX = rect0.xMax;
    float endY = rect0.yMax;
    
    
    
    GL.Color(rectColor);//设置方框的边框颜色 边框不透明
    
    GL.Vertex3( startX,startY,0);
    GL.Vertex3( endX,startY,0);
    
    GL.Vertex3( endX,startY,0);
    GL.Vertex3( endX,endY,0 );
    
    GL.Vertex3( endX,endY,0 );
    GL.Vertex3( startX,endY,0 );
    
    GL.Vertex3( startX,endY,0 );
    GL.Vertex3( startX,startY,0);
    
    
    }
    GL.End();
    // GL.Begin(GL.LINES);
    // GL.Vertex3(0, 0, 0);
    // GL.Vertex3(Screen.width, Screen.height, 0);
    // GL.End(); 
    GL.PopMatrix();//恢复摄像机投影矩阵
    
    }
    }
  • 相关阅读:
    使用MSXML2::IXMLDOMDocument2Ptr每次都要CreateInstance和load(xmlfile)吗?
    .Net程序安装打包的一些经验贡献
    感慨SQL2005中的数据挖掘算法
    COM客户端没法激活托管代码生成的COM Server的原因
    预感~=命中注定
    创业经理10大必备素质
    全局缓存管理工具
    XML DOM的结构概念图解哪里是Element,哪里是Attribute,哪里是Text
    用GetVolumeInformation得到的不是硬盘的序列号,不要再抄这样的错误好吗?
    站在生活的背后
  • 原文地址:https://www.cnblogs.com/huangshiyu13/p/5634698.html
Copyright © 2011-2022 走看看