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();//恢复摄像机投影矩阵
}
}
Unity之屏幕画线的更多相关文章
- Unity使用GL画线
脚本需挂在相机上,如果你的脚本,编辑器报错了,Matrix stack full depth reached,加上这个方法试试GL.LoadPixelMatrix(); using System.Co ...
- unity3d之在屏幕上画线
如何在屏幕上画线,简单的代码如下: using UnityEngine; public class Test : MonoBehaviour { void OnGUI() { GL.LoadOrtho ...
- android 屏幕上面画线
作业如下:在android屏幕上面任意画线 package feng.f121.drawline;//本人创建的包名,每人有每人的不同的包 import java.security.PublicKey ...
- Android中Path类的lineTo方法和quadTo方法画线的区别
转载:http://blog.csdn.net/stevenhu_223/article/details/9229337 当我们需要在屏幕上形成画线时,Path类的应用是必不可少的,而Path类的li ...
- Unity3D 画线插件 Vectrosity_Simple2DLine
Vectrosity是一个很方便的画线插件,用它我们可以画出2D,3D,贝塞尔,圆,椭圆等各种线条图案. :链接: http://pan.baidu.com/s/1pJjTFjt 密码: uesn 首 ...
- VC动态轨迹画线
分类: 2.4 线程/图形学2010-04-30 22:14 1878人阅读 评论(0) 收藏 举报 文档null 这是一个绘制直线的简单绘图程序,能过实现动态轨迹画线,在拖动时产生临时线来表示可能画 ...
- android中实现在ImageView上随意画线涂鸦
我实现的思路: 1.继承ImageView类 2.重写onTouchEvent方法,在ACTION_MOVE(即移动时),记录下所经过的点坐标,在ACTION_UP时(即手指离开时,这时一条线已经画完 ...
- Delphi下OpenGL2d绘图(03)-画线
一.前言 画线与画点基本上代码是相同.区别在于glBegin()的参数.绘制的框架代码可以使用 Delphi下OpenGL2d绘图(01)-初始化 中的代码.修改的部份为 Draw 函数的内容. 二. ...
- OpenGL进阶演示样例1——动态画线(虚线、实线、颜色、速度等)
用OpenGL动态绘制线段.事实上非常easy,但到如今为止.网上可參考资料并不多. 于是亲自己主动手写一个函数,方便动态绘制线段.代码例如以下: #include<GL/glu ...
随机推荐
- HttpContext及HttpContext.current
慎用System.Web.HttpContext.Current http://www.cnblogs.com/david1989/p/3879201.html 线程编程中用到HttpContext. ...
- VS 2010下单元测试
1.创建单元测试项目 2.创建完成后,新建项目会自动添加“Microsoft.VisualStudio.QualityTools.UnitTestFramework”的引用,该引用用于单元 ...
- mac 开发必备软件(不断update ing...)
整理下mac环境下, 开发必备的一些软件吧, 由于不断要更新ing, 用到啥就写啥~球轻拍 1.host 绑定切换神器 a.gas mask : 只能切换单个自定义的host文件 b.ihosts(推 ...
- c#获取或修改配置文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- Flex利用titleIcon属性给Panel容器标题部添加一个ICON图标
Flex利用titleIcon属性,给Panel容器标题部添加一个ICON图标. 让我们先来看一下Demo(可以右键View Source或点击这里察看源代码): 下面是完整代码(或点击这里察看): ...
- Win7中使用Eclipse连接虚拟机中的Ubuntu中的Hadoop2.4<3>
经过前几天的学习,基本上能够小试牛刀编写一些小程序玩一玩了,在此之前做几项准备工作 明白我要用hadoop干什么 大体学习一下mapreduce ubuntu重新启动后,再启动hadoop会报连接异常 ...
- BZOJ 1084: [SCOI2005]最大子矩阵 DP
1084: [SCOI2005]最大子矩阵 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1084 Description 这里有一个n* ...
- C#操作XML的完整例子——XmlDocument篇
这是一个用c#控制台程序下, 用XmlDocument 进行XML操作的的例子,包含了查询.增加.修改.删除.保存的基本操作.较完整的描述了一个XML的整个操作流程.适合刚入门.net XML操作的 ...
- 【JavsScript】当 JavaScript 从入门到提高前需要注意的细节:变量部分
在javaScript中变量使用var声明的变量是当前作用域的变量,不使用var声明的则肯定是全局变量. http://msdn.microsoft.com/zh-cn/library/dn64545 ...
- 2012 East Central Regional Contest 解题报告
昨晚各种莫名其妙卡题. 不过细看这套题还挺简单的.全是各种暴力. 除了最后一道题计算几何看起来很麻烦的样子,其他题都是很好写的吧. A. Babs' Box Boutique 题目大意是给出不超过10 ...