Unity GL画折线

新建一个脚本,这个物体得挂在有摄像机组件的物体上才能生效

OnPostRender() 这个函数才会被自动调用(类似生命周期自动调用)

然后就可以代码画线了,原理是openGL的画线

1using UnityEngine; 2using System.Collections; 3using System.Collections.Generic; 4 5/// <summary> 6/// GL画图 7/// </summary> 8public class GLDraw : UnityNormalSingleton<GLDraw> { 9 10 public Transform p1; 11 public Transform p2; 12 private List<Vector2> pointList; 13 private bool isOpen; 14 15 private Material mat; 16 private Shader shader; 17 18 private void Start() 19 { 20 pointList = new List<Vector2>(); 21 shader = Shader.Find("Unlit/Color"); 22 mat = new Material(shader); 23 mat.SetColor("Main Color", Color.black); 24 } 25 26 public void DrawLine(List<object> list) 27 { 28 pointList.Clear(); 29 for (int i = 0; i < list.Count; i++) 30 { 31 Vector2 screenPos = (Vector2)list[i]; 32 pointList.Add(new Vector2(screenPos.x, Screen.height - screenPos.y)); 33 } 34 } 35 36 public void ShowLine(bool b) 37 { 38 isOpen = b; 39 } 40 41 void OnPostRender() 42 { 43 if (!isOpen) 44 { 45 return; 46 } 47 48 //mat = new Material(Shader.Find("Unlit/Color")); 49 //Debug.Log("调用"); 50 //if (!mat) 51 //{ 52 // Debug.LogError("Please Assign a material on the inspector"); 53 // return; 54 //} 55 GL.PushMatrix(); //保存当前Matirx 56 mat.SetPass(0); //刷新当前材质 57 GL.LoadPixelMatrix();//设置pixelMatrix 58 GL.Color(Color.yellow); 59 GL.Begin(GL.LINES); 60 61 //画2次,奇偶数画连续折线法 62 for (int i = 0; i < pointList.Count; i++) 63 { 64 GL.Vertex3(pointList[i].x, pointList[i].y, 0); 65 } 66 for (int i = 1; i < pointList.Count; i++) 67 { 68 GL.Vertex3(pointList[i].x, pointList[i].y, 0); 69 } 70     //单次画线发 71 //GL.Vertex3(0, 0, 0);//GL.Vertex3(Screen.width, Screen.height, 0); 72     //固定2点画 73 //GL.Vertex3(p1.position.x, p1.position.y, 0); 74 //GL.Vertex3(p2.position.x, p2.position.y, 0); 75 GL.End(); 76 GL.PopMatrix();//读取之前的Matrix 77 } 78}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Unity GL画折线 - HelloWorld