如何使用GL库在unity3d mesh编程游戏编程中glquads是绘制什么形状

Unity3D GL方式画线 | Unity3D教程手册
当前位置 :
>> Unity3D GL方式画线
Unity3D GL方式画线
GL方式画线如下图:
相关文章:
Unity3D GL方式画线
把如下脚本放在摄像机上:
using UnityE
using System.C
[RequireComponent(typeof (Camera))]
public class VectorLine : MonoBehaviour
public int numberOfPoints = 2;
public Color lineColor = Color.
public int lineWidth = 3;
public bool drawLines =
private Material lineM
private Vector2[] lineP
void Awake()
lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass {" +
BindChannels { Bind \"Color\",color }" +
Blend SrcAlpha OneMinusSrcAlpha" +
ZWrite Off Cull Off Fog { Mode Off }" +
lineMaterial.hideFlags = HideFlags.HideAndDontS
lineMaterial.shader.hideFlags = HideFlags.HideAndDontS
// Creates a simple two point line
void Start()
linePoints = new Vector2[2];
// Sets line endpoints to center of screen and mouse position
void Update()
linePoints[0] = new Vector2(0.5f, 0.5f);
linePoints[1] = new Vector2(Input.mousePosition.x/Screen.width, Input.mousePosition.y/Screen.height);
void OnPostRender()
if (!drawLines || linePoints == null || linePoints.Length & 2)
float nearClip = cam.nearClipPlane + 0.00001f;
int end = linePoints.Length - 1;
float thisWidth = 1f/Screen.width * lineWidth * 0.5f;
lineMaterial.SetPass(0);
GL.Color(lineColor);
if (lineWidth == 1)
GL.Begin(GL.LINES);
for (int i = 0; i & ++i)
GL.Vertex(cam.ViewportToWorldPoint(new Vector3(linePoints[i].x, linePoints[i].y, nearClip)));
GL.Vertex(cam.ViewportToWorldPoint(new Vector3(linePoints[i+1].x, linePoints[i+1].y, nearClip)));
GL.Begin(GL.QUADS);
for (int i = 0; i & ++i)
Vector3 perpendicular = (new Vector3(linePoints[i+1].y, linePoints[i].x, nearClip) -
new Vector3(linePoints[i].y, linePoints[i+1].x, nearClip)).normalized * thisW
Vector3 v1 = new Vector3(linePoints[i].x, linePoints[i].y, nearClip);
Vector3 v2 = new Vector3(linePoints[i+1].x, linePoints[i+1].y, nearClip);
GL.Vertex(cam.ViewportToWorldPoint(v1 - perpendicular));
GL.Vertex(cam.ViewportToWorldPoint(v1 + perpendicular));
GL.Vertex(cam.ViewportToWorldPoint(v2 + perpendicular));
GL.Vertex(cam.ViewportToWorldPoint(v2 - perpendicular));
void OnApplicationQuit()
DestroyImmediate(lineMaterial);
【上一篇】
【下一篇】
您可能还会对这些文章感兴趣!Unity3D 使用 GL 绘制线条 - CSDN博客
Unity3D 使用 GL 绘制线条
本文永久地址:/article/77.aspx,【文章转载请注明出处!】
在前两篇中,我们使用了 Graphics()以及 Line Renderer 方法绘制线条(),这次我们通过
GL 来绘制线条,这种方法与前两种方法相比,效率与性能上面差不多。原文链接:/blog/index.php//3-ways-to-draw-3d-lines-in-unity3d/。
新建立一个 C# 脚本,取名:DrawLinesByGL.cs 代码如下:
using&UnityE
using&System.C
public&class&DrawLinesByGL
: MonoBehaviour
&&&&public&Shader
&&&&private&static&Material
&&&&private&GameObject
&&&&private&float&speed
&&&&private&Vector3[]
&&&&private&Vector3[]
&&&&private&Vector3
&&&&private&GUIStyle
&&&&private&GUIStyle
&&&&void&Start
&&&&&&&&labelStyle
=&new&GUIStyle();
&&&&&&&&labelStyle.normal.textColor
&&&&&&&&linkStyle
=&new&GUIStyle();
&&&&&&&&linkStyle.normal.textColor
=&new&Material(shader);
=&new&GameObject(&g&);
&&&&&&&&lp
=&new&Vector3[0];
&&&&&&&&sp
=&new&Vector3[0];
&&&&void&processInput()
&&&&&&&&float&s
= speed * Time.deltaT
&&&&&&&&if(Input.GetKey(KeyCode.RightShift)
|| Input.GetKey(KeyCode.LeftShift)) s = s * 0.1f;
&&&&&&&&if(Input.GetKey(KeyCode.UpArrow))
g.transform.Rotate(-s, 0, 0);
&&&&&&&&if(Input.GetKey(KeyCode.DownArrow))
g.transform.Rotate(s, 0, 0);
&&&&&&&&if(Input.GetKey(KeyCode.LeftArrow))
g.transform.Rotate(0, -s, 0);
&&&&&&&&if(Input.GetKey(KeyCode.RightArrow))
g.transform.Rotate(0, s, 0);
&&&&&&&&if(Input.GetKeyDown(KeyCode.C))
&&&&&&&&&&&&g.transform.rotation
= Quaternion.
&&&&&&&&&&&&lp
=&new&Vector3[0];
&&&&&&&&&&&&sp
=&new&Vector3[0];
&&&&void&Update()
&&&&&&&&processInput();
&&&&&&&&if(Input.GetMouseButton(0))
&&&&&&&&&&&&&
&&&&&&&&&&&&Vector3
e = GetNewPoint();
&&&&&&&&&&&&&
&&&&&&&&&&&&if(s
!= Vector3.zero) {
&&&&&&&&&&&&&&&&for(int&i
= 0; i & lp.L i += 2) {
&&&&&&&&&&&&&&&&&&&&float&d
= Vector3.Distance(lp[i], e);
&&&&&&&&&&&&&&&&&&&&if(d
& 1 && Random.value & 0.9f) sp = AddLine(sp, lp[i], e,&false);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&lp
= AddLine(lp, s, e,&false);
&&&&&&&&&&&&}
&&&&&&&&&&&&&
&&&&&&&&&&&&s
&&&&&&&&}&else&{
&&&&&&&&&&&&s
= Vector3.
&&&&void&Update1()
&&&&&&&&processInput();
&&&&&&&&Vector3
&&&&&&&&if(Input.GetMouseButtonDown(0))
&&&&&&&&&&&&s
= GetNewPoint();
&&&&&&&&if(Input.GetMouseButton(0))
&&&&&&&&&&&&e
= GetNewPoint();
&&&&&&&&&&&&lp
= AddLine(lp, s, e,&true);
&&&&&&&&if(Input.GetMouseButtonUp(0))
&&&&&&&&&&&&e
= GetNewPoint();
&&&&&&&&&&&&lp
= AddLine(lp, s, e,&false);
&&&&Vector3[]
AddLine(Vector3[] l, Vector3 s, Vector3 e,&bool&tmp)
&&&&&&&&int&vl
&&&&&&&&if(!tmp
|| vl == 0) l = resizeVertices(l, 2);
&&&&&&&&else&vl
&&&&&&&&&&&&&
&&&&&&&&l[vl]
&&&&&&&&l[vl+1]
&&&&&&&&return&l;
&&&&Vector3[]
resizeVertices(Vector3[] ovs,&int&ns)
&&&&&&&&Vector3[]
nvs =&new&Vector3[ovs.Length
+ ns];
&&&&&&&&for(int&i
= 0; i & ovs.L i++) nvs[i] = ovs[i];
&&&&&&&&return&
&&&&Vector3
GetNewPoint() {
&&&&&&&&return&g.transform.InverseTransformPoint(
&&&&&&&&&&&&Camera.main.ScreenToWorldPoint(
&&&&&&&&&&&&&&&&new&Vector3(Input.mousePosition.x,
Input.mousePosition.y, Camera.main.transform.position.z * -1.0f)
&&&&&&&&&&&&)
&&&&&&&&);
&&&&void&OnPostRender()
&&&&&&&&m.SetPass(0);
&&&&&&&&GL.PushMatrix();
&&&&&&&&GL.MultMatrix(g.transform.transform.localToWorldMatrix);
&&&&&&&&GL.Begin(
GL.LINES );
&&&&&&&&GL.Color(&new&Color(0,0,0,0.4f)
&&&&&&&&for(int&i
= 0; i & lp.L i++) {
&&&&&&&&&&&&GL.Vertex3(lp[i].x,
lp[i].y, lp[i].z);
&&&&&&&&GL.Color(&new&Color(0,0,0,0.1f)
&&&&&&&&for(int&i
= 0; i & sp.L i++) {
&&&&&&&&&&&&GL.Vertex3(sp[i].x,
sp[i].y, sp[i].z);
&&&&&&&&GL.End();
&&&&&&&&GL.PopMatrix();
&&&&void&OnGUI()
&&&&&&&&GUI.Label
(10, 10, 300, 24),&&GL.
Cursor keys to rotate (with Shift for slow)&,
labelStyle);
&&&&&&&&int&vc
= lp.Length + sp.L
&&&&&&&&GUI.Label
(10, 26, 300, 24),&&Pushing
vc +&&
vertices. 'C' to clear&,
labelStyle);
&&&&&&&&GUI.Label
(10, Screen.height - 20, 250, 24),&&.Inspired
by a demo from &,
labelStyle);
&&&&&&&&if(GUI.Button
(150, Screen.height - 20, 300, 24),&&mrdoob&,
linkStyle)) {
&&&&&&&&&&&&Application.OpenURL(&&);
然后把 DrawLinesByGL.cs 组件挂载到 MainCamera 对象上,如图:
运行游戏,最终效果如图:
资源下载地址:,共下载&95&次。
本文已收录于以下专栏:
相关文章推荐
为了在Profiler中 检测到具体是哪个 UIRect 的Update 会申请内存,在UIRect 的Update 里面调用了 Profiler 操作,并且传入的是 UIRect 的名字。在UWA的...
1. 你们的项目组使用源代码管理工具了么?    应该用。VSS、CVS、PVCS、ClearCase、CCC/Harvest、FireFly都可以。我的选择是VSS。   2. 你们的项目组使用缺陷...
Unity中使用GL在Camera上画线using System.C
using System.Collections.G
using UnityEpu...
Vectrosity是一个很方便的画线插件,用它我们可以画出2D,3D,贝塞尔,圆,椭圆等各种线条图案。
首先导入Vectrosity,导入后是这样的
打开Sences,看第一个Demo
unity使用LineRender画线后,使线不被UI的图片遮挡
using UnityE
using System.C
public class DrawLine : MonoBehaviour ...
今天在群里看见有个小伙在问Game视图寻路时怎么画线。更多精彩请关注【狗刨学习网】
      
       正好前几天写了个寻路,而且自己也不知道具体怎么在寻路时画线,所以决定帮帮他,...
/************************************************
* 创建人:陈鹏
* 创建日期:
* Blog: http://www.liv...
不记得在哪里看到的代码,效果很简单,修改了一下,可以用,还是有一定的参考价值
using UnityE
using System.C
public class ...
Unity中的曲线绘制(一)——直线
  让我们从创建一条简单的直线开始。两点成线,我们需要一个起始点p0,以及终点p2;
  现在我们可以创建一个游戏对象...
他的最新文章
讲师:宋宝华
讲师:何宇健
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字) 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
关于unity3D的GL图像库的使用
下载积分:840
内容提示:关于unity3D的GL图像库的使用
文档格式:TXT|
浏览次数:278|
上传日期: 16:01:21|
文档星级:
全文阅读已结束,如果下载本文需要使用
 840 积分
下载此文档
该用户还上传了这些文档
关于unity3D的GL图像库的使用
关注微信公众号Unity自带GL详解 - CSDN博客
Unity自带GL详解
转载自:.cn/s/blog_gxzf.html
GL即Graphics Library。Low-Level Graphics Library。计算matrices,发出类似OpenGL的immediate模式的渲染指令,和其它低级图像任务。Graphic.DrawMesh()比GL更高效。
GL立即绘制函数,只用当前material的设置,除非你显示指定mat。否则mat可以是任何材质。并且GL可能会改变材质。
GL立即执行的,如果你在Update()里调用,它们将在相机渲染前执行,相机渲染将会清空屏幕,GL效果将无法看到。
通常GL用法是,在camera上贴脚本,并在OnPostRender()里执行。
1、GL的线等基本图元并没有UV,所以没有贴图纹理映射的,shader里仅仅做的是单色计算或者对之前的影像加以处理。
2、GL所使用的shader里必须有cull off指令,否则显示会变成如下。(如何???)
3、如果是线,颜色是GL.Color( new Color(1,1,1,0.5f)设置颜色。如果是GL.TRIANGLES或者GL.QUADS,则颜色是shader里的颜色。
方法说明:
1、GL.PushMatrix():保存Matrices至matrix stack上
GL.PopMatrix():从matrix stack上读取matrices。
2、GL.LoadPixeMatrix():改变MVP矩阵,使得transform里的xy直接对应像素,(0,0)表示屏幕ViewPort的左下角,z的范围是(-1,1),该函数改变camera的参数,所以需要GL.PushMatrix()保存和GL.PopMatrix()读取。
GL.Vertex3()的取值范围从左下角的(0,0,0)到右上角的(Screen.width,Screen.height, 0)。
使用GL.LoadPixeMatrix时,Vertex3的xy值不需要除上Screen.width和Screen.height
3、GL.LoadOrtho():设置ortho perspective,即水平视角。GL.Vertex3()取值范围从左下角的(0,0,0)到右上角的(1,1,0)即需要除上Screen.width和Screen.height。
4、OnPostRender():只有物体上有激活的相机时,才会调用该函数。当摄像机完成渲染场景后,绘制了所有物体再调用该方法。OnPostRender可以使用coroutine,加yield使用
WaitForEndOfFrame():等待至所有绘制之后,在展示frame到屏幕之前,可以做截图。可以在任务物体上使用该函数。
例子1:屏幕划线
using UnityE
using System.C
using System.Collections.G
public class Line : MonoBehaviour
private Vector3 vB
private Vector3 vE
private static IList&Vector3& _posList = new List&Vector3&();
private static bool _isReady =
public static bool isReady
set{ _isReady = }
void Start()
mat = 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 }& +
&} } }&);//生成画线的材质
mat.hideFlags = HideFlags.HideAndDontS
mat.color = Color.
mat.shader.hideFlags = HideFlags.HideAndDontS
public static void setPosList( ref IList&Vector3& posList )
if( _isReady )
_posList = posL
if( _posList.Count & 1 )
_isReady =
void OnPostRender()
if( _isReady )
vBeg = camera.WorldToScreenPoint( panelFullMap.instance.mainPlayer.position );
vEnd = camera.WorldToScreenPoint( _posList[0] );
GL.PushMatrix(); //保存当前Matirx
mat.SetPass(0); //刷新当前材质
GL.LoadOrtho();//设置pixelMatrix
GL.Color(Color.yellow);
GL.Begin(GL.LINES);
GL.Vertex3( vBeg.x/Screen.width, vBeg.y/Screen.height, z );
GL.Vertex3( vEnd.x/Screen.width, vEnd.y/Screen.height, z );
GL.PopMatrix();//读取之前的Matrix
for( int i = 0; i & _posList.Count - 1; ++i )
vBeg = camera.WorldToScreenPoint( _posList[i] );
vEnd = camera.WorldToScreenPoint( _posList[i+1] );
GL.PushMatrix();
mat.SetPass(0);
GL.LoadOrtho();
GL.LoadPixelMatrix();
GL.Begin(GL.LINES);
GL.Color(Color.green);
GL.Vertex3( vBeg.x/Screen.width, vBeg.y/Screen.height, z );
GL.Vertex3( vEnd.x/Screen.width, vEnd.y/Screen.height, z );
GL.Vertex3( vBeg.x, vBeg.y, 0 );
GL.Vertex3( vEnd.x, vEnd.y, 0 );
GL.PopMatrix();
例子2:截图
using System.IO;
using UnityE
using System.C
public class ScreenShot : MonoBehaviour
void Start()
StartCoroutine(UploadPNG() );
IEnumerator UploadPNG()
yield return new WaitForEndOfFrame();
print (&yuuuuu&);
int width = Screen.
int height = Screen.
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath+&/ss.png&,bytes);
UnityEditor.AssetDatabase.Refresh();
例子3:展示Alpha
using UnityE
using System.C
public class GLTest : MonoBehaviour
public Texture2D t2d;
void Start()
mat = new Material(shader);
mat.mainTexture = t2d;
void OnPostRender()
Debug.LogError(&Please Assign a material on the inspector&);
GL.PushMatrix();
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.QUADS);
GL.Vertex3(0, 0, 0.1F);
GL.Vertex3(1f, 0, 0.1F);
GL.Vertex3(1f, 1, 0.1F);
GL.Vertex3(0, 1, 0.1F);
GL.PopMatrix();
Shader &Custom/GLDrawLine&
Properties {
_MainTex (&Base (RGB)&, 2D) = &white& {}
Blend DstAlpha zero
Color(1,1,1,1)
一个完整的例子:
转载自:/article/65.aspx
本文已收录于以下专栏:
相关文章推荐
作者:PEPE 
出处:/ 
1、绘制2D图像的时需要使用GL.LoadOrtho()方法来将图形映射到平面中。
  2、所...
万万没想到对于3D物体应付起来易如反掌的Unity3D在面对图表的绘制是如此简直无力,需要和C++或者其它语言一样需要调用到自己底层绘图接口《【Unity3D】平面绘图——GL与GUI》(点击打开链接...
本文永久地址:/article/77.aspx,【文章转载请注明出处!】
在前两篇中,我们使用了 Graphics(查看详情)以及 Line Rend...
开发过程中遇到了一个问题,在UI上动态画弧线,花了一天的时间,终于解决了,拿来和大家分享一下(个人场景里只有UI)。
我才用的动态画线的方法是GL画线,有一定的性能开销,unity版本5.3.2.。...
最近由于闲来无事,无意中看到vectrosity这个画线插件,就想研究一下,下载下来之后发现导入到unity里之后出现错误,由于自己是蠢新所以改了一通也没改对,所以就放弃了,大家需要插件的话待会我会上...
在Unity3D中绘制一个平面图形可不容易,可别惯性思维觉得,嗯,这引擎3D的事情都能解决了,2D的不叫事情。其实除去布置UI,在Unity3D中绘制一个2D平面图形是很蛋疼的一件事情。不然的话,也不...
Untiy自定义UI组件
自定义折线图,曲线图,条形图,饼图,函数公式图等
可自定义多种别的工具比如颜色拾取器,日期拾取器,简单Excel的绘制
最近更新Unity3D,更新包推送了Standard Assets Example Project的更新,于是便下载下来研究了下,发现还是挺好用的,里面有很多示例,有各种模型,适用于学习Unity的新...
OpenGL编程入门学习  非常详细的教程,很适合初学者
本文转自:/doing5552/archive//71532.html
他的最新文章
讲师:宋宝华
讲师:何宇健
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)unity3D技术之GL.MultiTexCoord3 多重纹理坐标3_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
unity3D技术之GL.MultiTexCoord3 多重纹理坐标3
&&unity3D技术
阅读已结束,下载文档到电脑
想免费下载更多文档?
定制HR最喜欢的简历
你可能喜欢

我要回帖

更多关于 unity3d人工智能编程 的文章

 

随机推荐