下面是转载别人的,缺点是只能在xz平面上画线,可以添加一个地板来测试,鼠标点击地板进行画线

 

当再次看这篇文章时,还是觉得非常吃力,因为距离上一次转载的时间有点长了,上次是看懂了,但没有记录下来,所以这次看时还是需要费点脑子才再次看懂了,所以这次我在后面详细的记录了下来,最好自己能够看懂原来作者的代码,不能完全看懂也没有关系,在最后我会解释一下核心的代码和核心的算法。            (2019年7月13日新增)

 

 

 

这两天在研究画线的东西。直到昨天才小有成效。拿出来和大家分享一下。先上图:

 

unity 编辑器Handles 画多边形 填色 unity画线_List

 

以前曾经尝试过用LineRender写画线的功能,但是在拐弯的时候会出现变形和扭曲。所以才决定用绘制Mesh的方法写一个画线的功能。

东西其实很简单,稍微有一点数学的知识,用到了三角函数。还有一些关于构造Mesh的相关代码。下面有草图一张:

unity 编辑器Handles 画多边形 填色 unity画线_unity画线_02

 

黑色的线框代表画出来的模型线,PointA和PointB代表获取的起始点和终结点,A,B,C,D为Mesh的四个顶点。

已知起始点和终结点和线宽,就可以求出四个顶点的坐标。并根据这四个顶点绘制出Mesh.

至于具体的实现细节,大家看代码吧。

 

DrawMeshLine类:

 


1. using UnityEngine;  
2. using System.Collections;  
3. using System.Collections.Generic;  
4.   
5. public class DrawMeshLine : MonoBehaviour   
6. {  
7. //线容器
8.     List<Vector3> LinePointContainer;  
9. //线宽
10.     public float LineWidth = 0.5f;  
11. //地形层
12.     public int Layer = 8;  
13. //线物体父物体
14.     GameObject LineObj;  
15.       
16.     void Start ()   
17.     {  
18.         LinePointContainer = new List<Vector3>();  
19.         LineObj = new GameObject("Lines");  
20.     }  
21.       
22.     void Update ()   
23.     {  
24.         if(Input.GetMouseButtonDown(0))  
25.         {  
26.             Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);  
27.             RaycastHit hit;  
28.             if(Physics.Raycast(ray,out hit))  
29.             {  
30.                 if(hit.transform.gameObject.layer != Layer)return;  
31.                 LinePointContainer.Add(hit.point);  
32.             }  
33.             if(LinePointContainer.Count == 1)  
34.             {  
35. //二维初始面片圆
36. //              GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
37. //              sphere.transform.parent = LineObj.transform;
38. //              sphere.transform.position = hit.point;
39. //              sphere.transform.localScale = new Vector3(LineWidth * 2,0.01f,LineWidth * 2);
40. //              sphere.renderer.material.shader = Shader.Find("GUI/Text Shader");
41. //              sphere.renderer.material.SetColor("_Color",new Color(255,0,0,255) / 255);
42.                   
43. //三维初始球
44.                 GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);  
45.                 sphere.transform.parent = LineObj.transform;  
46.                 sphere.transform.position = new Vector3(hit.point.x,hit.point.y + LineWidth,hit.point.z);  
47.                 sphere.transform.localScale = new Vector3(LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f));  
48.             }  
49.             if(LinePointContainer.Count > 1)  
50.             {  
51. //画二维面片线
52. //DrawLine2D(LinePointContainer[LinePointContainer.Count - 2],LinePointContainer[LinePointContainer.Count - 1]);
53. //画三维立体线
54.                 DrawLine3D(LinePointContainer[LinePointContainer.Count - 2],LinePointContainer[LinePointContainer.Count - 1]);                   //第一个参数是起点,第二个参数是终点
55.             }  
56.         }  
57.         if(Input.GetMouseButtonDown(1))  
58.         {  
59. //清空线容器
60.             if(LinePointContainer.Count < 3)return;  
61.             DrawLine3D(LinePointContainer[LinePointContainer.Count - 1],LinePointContainer[0]);  
62.             LinePointContainer.Clear();  
63.         }  
64.         if(Input.GetKeyDown(KeyCode.Escape))  
65.         {  
66. //清除所有线物体
67.             LinePointContainer.Clear();  
68.             ClearLineObjects();  
69.         }  
70.     }  
71.       
72. /// <summary>
73. /// 二维线
74. /// </summary>
75. /// <param name='PointA'>
76. /// 初始点
77. /// </param>
78. /// <param name='PointB'>
79. /// 结束点
80. /// </param>
81.     void DrawLine2D(Vector3 PointA,Vector3 PointB)  
82.     {  
83.         float HorDisABx = PointB.x - PointA.x;  
84.         float HorDisABz = PointB.z - PointA.z;  
85.         float HorDisAB = Mathf.Sqrt(Mathf.Pow(HorDisABx,2) + Mathf.Pow(HorDisABz,2));  
86.           
87.         float offsetX = HorDisABz * LineWidth / HorDisAB;  
88.         float offsetZ = HorDisABx * LineWidth / HorDisAB;  
89.           
90.         Vector3 Point1 = new Vector3(PointA.x - offsetX,PointA.y,PointA.z + offsetZ);  
91.         Vector3 Point2 = new Vector3(PointA.x + offsetX,PointA.y,PointA.z - offsetZ);  
92.         Vector3 Point3 = new Vector3(PointB.x + offsetX,PointB.y,PointB.z - offsetZ);  
93.         Vector3 Point4 = new Vector3(PointB.x - offsetX,PointB.y,PointB.z + offsetZ);  
94.           
95.  
96.  
97.           
98.         GameObject go = new GameObject((LinePointContainer.Count - 1).ToString());  
99.         go.transform.parent = LineObj.transform;  
100.         Mesh mesh = go.AddComponent<MeshFilter>().mesh;  
101.         go.AddComponent<MeshRenderer>();  
102.         mesh.vertices = new Vector3[]{Point1,Point2,Point3,Point4};  
103.         mesh.triangles = new int[]{2,1,0,0,3,2};  
104.           
105.         GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);  
106.         sphere.transform.parent = LineObj.transform;  
107.         sphere.transform.position = PointB;  
108.         sphere.transform.localScale = new Vector3(LineWidth * 2,0.01f,LineWidth * 2);  
109.     }  
110.       
111. /// <summary>
112. /// 三维线
113. /// </summary>
114. /// <param name='PointA'>
115. /// 初始点
116. /// </param>
117. /// <param name='PointB'>
118. /// 结束点
119. /// </param>
120.     void DrawLine3D(Vector3 PointA,Vector3 PointB)  
121.     {  
122.         float HorDisABx = PointB.x - PointA.x;  
123.         float HorDisABz = PointB.z - PointA.z;  
124.         float HorDisAB = Mathf.Sqrt(Mathf.Pow(HorDisABx,2) + Mathf.Pow(HorDisABz,2));  //求起点和终点的模长
125.           
126.         float offsetX = HorDisABz * LineWidth / HorDisAB;  
127.         float offsetZ = HorDisABx * LineWidth / HorDisAB;  
128.           
129.         Vector3 Point1 = new Vector3(PointA.x - offsetX,PointA.y,PointA.z + offsetZ);  
130.         Vector3 Point2 = new Vector3(PointA.x + offsetX,PointA.y,PointA.z - offsetZ);  
131.         Vector3 Point3 = new Vector3(PointB.x + offsetX,PointB.y,PointB.z - offsetZ);  
132.         Vector3 Point4 = new Vector3(PointB.x - offsetX,PointB.y,PointB.z + offsetZ);  
133.           
134.         GameObject go1 = new GameObject((LinePointContainer.Count - 1).ToString() + "_1");  
135.         go1.transform.parent = LineObj.transform;  
136.         Mesh mesh1 = go1.AddComponent<MeshFilter>().mesh;  
137.         go1.AddComponent<MeshRenderer>();  
138.         mesh1.vertices = new Vector3[]{Point1,Point2,Point3,Point4};  
139.         mesh1.triangles = new int[]{2,1,0,0,3,2};  
140.           
141.         Vector3 Point5 = new Vector3(PointA.x - offsetX,PointA.y + 2 * LineWidth,PointA.z + offsetZ);  
142.         Vector3 Point6 = new Vector3(PointA.x + offsetX,PointA.y + 2 * LineWidth,PointA.z - offsetZ);  
143.         Vector3 Point7 = new Vector3(PointB.x + offsetX,PointB.y + 2 * LineWidth,PointB.z - offsetZ);  
144.         Vector3 Point8 = new Vector3(PointB.x - offsetX,PointB.y + 2 * LineWidth,PointB.z + offsetZ);  
145.           
146.         GameObject go2 = new GameObject((LinePointContainer.Count - 1).ToString() + "_2");  
147.         go2.transform.parent = LineObj.transform;  
148.         Mesh mesh2 = go2.AddComponent<MeshFilter>().mesh;  
149.         go2.AddComponent<MeshRenderer>();  
150.         mesh2.vertices = new Vector3[]{Point5,Point6,Point7,Point8};  
151.         mesh2.triangles = new int[]{2,1,0,0,3,2};  
152.           
153.         GameObject go3 = new GameObject((LinePointContainer.Count - 1).ToString() + "_3");  
154.         go3.transform.parent = LineObj.transform;  
155.         Mesh mesh3 = go3.AddComponent<MeshFilter>().mesh;  
156.         go3.AddComponent<MeshRenderer>();  
157.         mesh3.vertices = new Vector3[]{Point6,Point2,Point3,Point7};  
158.         mesh3.triangles = new int[]{2,1,0,0,3,2};  
159.           
160.         GameObject go4 = new GameObject((LinePointContainer.Count - 1).ToString() + "_4");  
161.         go4.transform.parent = LineObj.transform;  
162.         Mesh mesh4 = go4.AddComponent<MeshFilter>().mesh;  
163.         go4.AddComponent<MeshRenderer>();  
164.         mesh4.vertices = new Vector3[]{Point1,Point5,Point8,Point4};  
165.         mesh4.triangles = new int[]{2,1,0,0,3,2};  
166.           
167.         GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);  
168.         sphere.transform.parent = LineObj.transform;  
169.         sphere.transform.position = new Vector3(PointB.x,PointB.y + LineWidth,PointB.z);  
170.         sphere.transform.localScale = new Vector3(LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f),LineWidth * Mathf.Sqrt(8.0f));  
171.     }  
172.       
173.     void ClearLineObjects ()  
174.     {  
175.         for(int i = 0 ; i < LineObj.transform.childCount;i ++)  
176.         {  
177.             GameObject go = LineObj.transform.GetChild(i).gameObject;  
178.             Destroy(go);  
179.         }  
180.     }  
181. }

个人理解:

想象成一个几何体,以ABCD为地面,把地面抬高2 * LineWidth米作为顶面EFGH

1 GameObject go1 = new GameObject((LinePointContainer.Count - 1).ToString() + "_1"); 这个是画面ABCD
2 GameObject go2 = new GameObject((LinePointContainer.Count - 1).ToString() + "_2");  这个是画面EFGH
3 GameObject go3 = new GameObject((LinePointContainer.Count - 1).ToString() + "_3");  这个是画面BCGF
4  GameObject go4 = new GameObject((LinePointContainer.Count - 1).ToString() + "_4");  这个是画面ADHE

其中面ABFE和面DCGH不用画,画了也没有意义

 

 

 

unity 编辑器Handles 画多边形 填色 unity画线_二维_03

 

 

(2019年7月13日新增)

其中PointA为线的起点,PointB为线的终点

上面的代码:

             

Vector3 Point1 = new Vector3(PointA.x - offsetX,PointA.y,PointA.z + offsetZ); 
                Vector3 Point2 = new Vector3(PointA.x + offsetX,PointA.y,PointA.z - offsetZ);  
                Vector3 Point3 = new Vector3(PointB.x + offsetX,PointB.y,PointB.z - offsetZ);  
               Vector3 Point4 = new Vector3(PointB.x - offsetX,PointB.y,PointB.z + offsetZ);

Point1对应于上图中的A点,Point2对应上图的B点,Point3对应上图中的C点,Point4对应上图的D点

 

想要绘制出面 ABCD,在Unity3d中只能绘制出三角形,这时我们可以把面ABCD分成两个三角面 :   面ABC和面ADC

把点Point3、Point2、Point1两两相连,再渲染(至于如何把三角形渲染成面,不需要你管,Unity3D做了),就成面ABC了

把点Point1、Point4、Point3两两相连,再渲染,就成面ADC了

 

 

当绘制出面ABC和面ADC后,把这两个面一组合,就形成了面ABCD,这时就有了下面的代码:

   mesh1.vertices = new Vector3[]{Point1,Point2,Point3,Point4};  

  mesh1.triangles = new int[]{2,1,0,0,3,2};  


其中代码  mesh1.triangles = new int[]{2,1,0,0,3,2};  总共有6个数,连续三个数组成一组,这样可以分成两组  (2,1,0)和(0,3,2)

至于为什么是三个数为一组,我猜你还缺乏Mesh基础,请参考:

 

 

再看  mesh1.vertices[2]=Point3,mesh1.vertices[1]=Point2,mesh.vertices[0]=Point1,   数组下标一组合为  (2,1,0), 把数组下标对应的点Point3、Point2、Point1 两两相连,不就成了三角形ABC了吗,Unity3D自动把三角形ABC渲染成面ABC

同理mesh1.vertices[0]=Pont1,mesh1.vertices[3]=Point4,mesh1.vertices[2]=Point3,数组下标一组合为  (0,3,2),把数组下标对应的点Point1、Point4、Point3 两两相连,不就成了三角形ADC了吗,Unity3D自动把三角形ADC渲染成面ADC

把面ABC和面ADC一组合就完成了面ABCD的绘制

 

同理,再把 面EFGH、面BCGF、面ADHE绘制,最后把四个面一组合,就组成了空心的三维模型线(算是线吧),如下图:

unity 编辑器Handles 画多边形 填色 unity画线_结点_04