本文永久地址:http://www.omuying.com/article/77.aspx,【文章转载请注明出处!】

在前两篇中,我们使用了 Graphics(查看详情)以及 Line Renderer 方法绘制线条(查看详情),这次我们通过 GL 来绘制线条,这种方法与前两种方法相比,效率与性能上面差不多。原文链接:http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/。

新建立一个 C# 脚本,取名:DrawLinesByGL.cs 代码如下:

001 using UnityEngine;
002 using System.Collections;
003  
004 public class DrawLinesByGL : MonoBehaviour
005 {
006     public Shader shader;
007  
008     private static Material m;
009     private GameObject g;
010     private float speed = 100.0f;
011     private Vector3[] lp;
012     private Vector3[] sp;
013     private Vector3 s;
014  
015     private GUIStyle labelStyle;
016     private GUIStyle linkStyle;
017      
018     void Start () {
019         labelStyle = new GUIStyle();
020         labelStyle.normal.textColor = Color.black;
021          
022         linkStyle = new GUIStyle();
023         linkStyle.normal.textColor = Color.blue;
024          
025         m = new Material(shader);
026         g = new GameObject("g");
027         lp = new Vector3[0];
028         sp = new Vector3[0];
029     }
030      
031     void processInput() {
032         float s = speed * Time.deltaTime;
033         if(Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift)) s = s * 0.1f;
034         if(Input.GetKey(KeyCode.UpArrow)) g.transform.Rotate(-s, 0, 0);
035         if(Input.GetKey(KeyCode.DownArrow)) g.transform.Rotate(s, 0, 0);
036         if(Input.GetKey(KeyCode.LeftArrow)) g.transform.Rotate(0, -s, 0);
037         if(Input.GetKey(KeyCode.RightArrow)) g.transform.Rotate(0, s, 0);
038          
039         if(Input.GetKeyDown(KeyCode.C)) {
040             g.transform.rotation = Quaternion.identity;
041             lp = new Vector3[0];
042             sp = new Vector3[0];
043         }
044     }
045      
046     void Update() {
047         processInput();
048          
049         if(Input.GetMouseButton(0)) {
050              
051             Vector3 e = GetNewPoint();
052              
053             if(s != Vector3.zero) {
054                 for(int i = 0; i < lp.Length; i += 2) {
055                     float d = Vector3.Distance(lp[i], e);
056                     if(d < 1 && Random.value > 0.9f) sp = AddLine(sp, lp[i], e, false);
057                 }
058                  
059                 lp = AddLine(lp, s, e, false);
060             }
061              
062             s = e;
063         else {
064             s = Vector3.zero;
065         }
066     }
067  
068     void Update1() {
069         processInput();
070          
071         Vector3 e;
072          
073         if(Input.GetMouseButtonDown(0)) {
074             s = GetNewPoint();
075         }
076          
077         if(Input.GetMouseButton(0)) {
078             e = GetNewPoint();
079             lp = AddLine(lp, s, e, true);
080         }
081  
082         if(Input.GetMouseButtonUp(0)) {
083             e = GetNewPoint();
084             lp = AddLine(lp, s, e, false);
085         }
086     }
087      
088     Vector3[] AddLine(Vector3[] l, Vector3 s, Vector3 e, bool tmp) {
089         int vl = l.Length;
090         if(!tmp || vl == 0) l = resizeVertices(l, 2);
091         else vl -= 2;
092              
093         l[vl] = s;
094         l[vl+1] = e;
095         return l;
096     }
097      
098     Vector3[] resizeVertices(Vector3[] ovs, int ns) {
099         Vector3[] nvs = new Vector3[ovs.Length + ns];
100         for(int i = 0; i < ovs.Length; i++) nvs[i] = ovs[i];
101         return nvs;
102     }
103      
104     Vector3 GetNewPoint() {
105         return g.transform.InverseTransformPoint(
106             Camera.main.ScreenToWorldPoint(
107                 new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z * -1.0f)
108             )
109         );
110     }
111  
112     void OnPostRender() {
113         m.SetPass(0);
114         GL.PushMatrix();
115         GL.MultMatrix(g.transform.transform.localToWorldMatrix);
116         GL.Begin( GL.LINES );
117         GL.Color( new Color(0,0,0,0.4f) );
118          
119         for(int i = 0; i < lp.Length; i++) {
120             GL.Vertex3(lp[i].x, lp[i].y, lp[i].z);
121         }
122          
123         GL.Color( new Color(0,0,0,0.1f) );
124          
125         for(int i = 0; i < sp.Length; i++) {
126             GL.Vertex3(sp[i].x, sp[i].y, sp[i].z);
127         }
128          
129         GL.End();
130         GL.PopMatrix();
131     }
132      
133     void OnGUI() {
134         GUI.Label (new Rect (10, 10, 300, 24), "GL. Cursor keys to rotate (with Shift for slow)", labelStyle);
135         int vc = lp.Length + sp.Length;
136         GUI.Label (new Rect (10, 26, 300, 24), "Pushing " + vc + " vertices. 'C' to clear", labelStyle);
137          
138         GUI.Label (new Rect (10, Screen.height - 20, 250, 24), ".Inspired by a demo from ", labelStyle);
139         if(GUI.Button (new Rect (150, Screen.height - 20, 300, 24), "mrdoob", linkStyle)) {
140             Application.OpenURL("http://mrdoob.com/lab/javascript/harmony/");
141         }
142     }
143 }

然后把 DrawLinesByGL.cs 组件挂载到 MainCamera 对象上,如图:

运行游戏,最终效果如图:

资源下载地址:点击下载,共下载 95 次。

Unity3D 使用 GL 绘制线条相关推荐

  1. 运行时绘制Gizmo——关于unity3D的GL图像库的使用(非常详细)

    转载来源:https://www.cnblogs.com/JLZT1223/p/6094404.html ps: 想了解一下GL库,但是都没有什么好的资料. 好不容易找到一个详细的博客,格式还全乱,阅 ...

  2. Cocos2d-x之绘制线条

    Cocos2d-x之绘制线条 自定义的方法 Line.h 1 // 2 // Line.h 3 // L01DrawingAPI 4 // 5 // Created by Mac OS 10.9.3 ...

  3. python PyQt5中文教程☞【第十节】PyQt5绘图(绘制文本drawText()、画点drawPoints()、设置颜色、QPen(画笔)绘制线条、QBrush(笔刷)绘制纹理

    引用文章:http://code.py40.com/pyqt5/32.html 文章目录 绘制文本 画点 PyQt5颜色 QPen(画笔) QBrush(笔刷) 总结:一发现有事件触发就会更新QWid ...

  4. 用html5做一条线,使用HTML5 canvas绘制线条的方法

    使用HTML5 canvas绘制线条的方法 发布时间:2020-08-29 11:24:23 来源:亿速云 阅读:96 作者:小新 这篇文章主要介绍了使用HTML5 canvas绘制线条的方法,具有一 ...

  5. java 在底图上绘制线条_使用底图和geonamescache绘制k表示聚类

    java 在底图上绘制线条 This is the third of four stories that aim to address the issue of identifying disease ...

  6. iOS 动画绘制线条颜色渐变的折线图

    效果图 .................... 概述 现状 折线图的应用比较广泛,为了增强用户体验,很多应用中都嵌入了折线图.折线图可以更加直观的表示数据的变化.网络上有很多绘制折线图的demo,有 ...

  7. canvas 边界模糊_Canvas绘制线条模糊的解决方案

    标签: 前段时间,做一个跨平台app项目,需要绘制分时图和K线图.找了很多开源的js的图表库,包括echarts.highcharts等等,都不是很满意,原因有2: 1.太臃肿,我实际上只要一个分时和 ...

  8. 在jupyter中使用python绘制线条椭球

    在jupyter中使用python绘制线条椭球 本文中我们使用了numpy matplotlib,请自行安装 import matplotlib as mpl import numpy as np i ...

  9. VC++ 绘制线条 OnLButtonDown函数(DrawView.cpp) 利用SDK全局函数实现画线功能 利用MFC的CDC类实现画线功能 利用MFC的CClientDC类实现画线功能

    目录 绘制线条 OnLButtonDown函数(DrawView.cpp) 利用SDK全局函数实现画线功能 利用MFC的CDC类实现画线功能 利用MFC的CClientDC类实现画线功能 接上:VC+ ...

最新文章

  1. how to create view (windows)
  2. android 安装sqlite3,Android真机安装sqlite3的方法
  3. JAVA构造方法,继承关系和SUPER关键字
  4. 进程间的通信IPC(无名管道和命名管道)
  5. 贪心只能过样例 loj515
  6. 高并发 数据库操作比如插入,修改等解决办法
  7. iOS 14代码泄露iPhone 12系列细节:有且仅有两款配备ToF 3D镜头
  8. Netty源码解读(一)概述
  9. linux命令 bind,Linux bind命令
  10. 如何在Spring MVC工程中进行单元测试
  11. sqlserver替换特殊字符
  12. RFID天线接头分类
  13. 2013 Esri全球用户大会之元数据支持
  14. 基于单片机的心率监测系统设计(#0495)
  15. 视频无损裁剪、转换、合并、加水印、加特效?一切搞定!
  16. 从浏览器中获取headers
  17. 《程序员》9月刊推荐:移动应用产业链大势图
  18. Java Vue uni-app 三端实现,滑动拼图验证码
  19. 从志愿军“断刀”再论敏捷之道(上篇)
  20. 细细品味C#——抽象、接口、委托、反射

热门文章

  1. python中again函数怎么用_《“笨方法”学python 》 once again 20170729
  2. git拉取tag代码_10年经验17张图带你进入gitflow企业项目代码版本管理的最佳实践...
  3. 苹果6s最大屏幕尺寸_iPhone12来了,我决定给老苹果升级一下电池_电池
  4. 3像素尺寸是多少_纸张的尺寸
  5. android微信条码支付接口,Android中调用微信支付接口
  6. JAVA写的多线程下载程序,并具有断点续传功能
  7. Java案例:Swing常用组件演示
  8. Spring Boot学习笔记:Spring Boot的Web功能
  9. Python案例:打印杨辉三角形
  10. Spring Boot基础学习笔记09:Thymeleaf模板引擎