使用Unity绘画任意管道教程 VR资源

仅为年时 2017-08-18 15:43:24
三维空间画管道主要是用到空间圆参数方程来实现。

在拐角的处理方面,暂时只是前后管道预留出一个半径的长度,用贝塞尔曲线进行十均分然后画mesh连接,由于圆的空间参数方程的单位向量的a与b的值是圆的随机值,是通过坐标向量求得,所以可能会出现画圆的时候起点不统一的情况。

在前后管道重新生成的过程中,直管道部分暂时是两个重叠,尾部缺一个半径的距离,是为了避免管道有裂缝。

 
 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class drawPipe : MonoBehaviour {
    //第一个点和下一个点
    Vector3 firstPoint = Vector3.zero;
    Vector3 nextPoint = Vector3.zero;
    //管道半径
    public float circleR = 0.5f;
    //管道材质球
    public Material lineMaterial;
    //管道方向法线向量
    Vector3 n,n1,n2;
    //法向量单位向量
    Vector3 n0,n10, n20;
    //坐标向量
    Vector3 i1 = new Vector3(0, 1, 0);
    Vector3 i2 = new Vector3(1, 0, 0);
    //单位向量
    Vector3 a, al;
    Vector3 b, bl;
    //第一个与下一个圆的坐标
    Vector3 circleXYZFirst;
    Vector3 circleXYZNext;
    //123三点
    Vector3[] points = new Vector3[3];
    //拐角三点的前后两点
    Vector3 cornerPointF;
    Vector3 cornerPointB;
    //第一次画管道的最后点的预留点
    Vector3 secondPF;
    //拐点后用来重复续接的靠近尾部的点。
    Vector3 cornerPointB_B;
    //用于下次接着画的起点
    Vector3 cornerPointBStart = Vector3.zero;
    //拐点密度
    float density = 0.1f;//绘制的0-1的间隙 越小曲线越接近曲线
    //贝塞尔曲线生成拐点坐标列表
    List<Vector3> cornerPoints = new List<Vector3>();
 
    Vector3[] _Vertices = new Vector3[16];
    Vector3[] _VerticesAll = new Vector3[96];
    List<Vector3> _VerticesAllList = new List<Vector3>();
 
    GameObject pointItem;
 
    // Use this for initialization
    void Start () {
        points = new Vector3[3]
        {
            Vector3.zero,
            Vector3.zero,
            Vector3.zero
        };
    }
         
        // Update is called once per frame
        void Update () {
        DrawPipeA();
    }
    private void DrawPipeA()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if(Physics.Raycast(ray,out hit) && hit.collider.tag == "plane")
            {
                if(firstPoint == Vector3.zero)
                {
                    firstPoint = hit.point;
                    points[0] = firstPoint;
                    pointItem = (GameObject)Instantiate(Resources.Load("Prefabs/point"), points[0], transform.rotation);
                }
                else if(nextPoint == Vector3.zero)
                {
                    nextPoint = hit.point;
                    points[1] = nextPoint;
                    //画管道
                    DrawPipeB(points[0], points[1]);
                    pointItem = (GameObject)Instantiate(Resources.Load("Prefabs/point"), points[1], transform.rotation);
                }
                else
                {
                    if(points[2] == Vector3.zero)
                    {
                        nextPoint = hit.point;
                        points[2] = nextPoint;
                        //画带拐角的管道
                        DrawPipeB(points[0], points[1], points[2]);
                        pointItem = (GameObject)Instantiate(Resources.Load("Prefabs/point"), points[2], transform.rotation);
                    }
                    else
                    {
                        points[0] = points[1];
                        points[1] = points[2];
                        points[2] = hit.point;
                        //画带拐角的管道
                        DrawPipeB(points[0], points[1], points[2]);
                        pointItem = (GameObject)Instantiate(Resources.Load("Prefabs/point"), points[2], transform.rotation);
                    }
                }
            }
        }
    }
    //画管道,创建mesh
    private void DrawPipeB(Vector3 firstP,Vector3 secondP)
    {
        n = secondP - firstP;
        n0 = n / Mathf.Sqrt(n.x * n.x + n.y * n.y + n.z * n.z);
        secondPF = secondP - n0 * circleR;
 
        GameObject _Line = new GameObject("MeshLine");
        _Line.AddComponent<MeshFilter>();
        _Line.AddComponent<MeshRenderer>();
        if (lineMaterial == null) Debug.Log("The line material is empty! 线段未添加材质球!");
        else _Line.GetComponent<MeshRenderer>().material = lineMaterial;
 
        al = Vector3.Cross(n, i1);//叉乘
        if (al == Vector3.zero)
        {
            al = Vector3.Cross(n, i2);
        }
        a = Vector3.Normalize(al);//单位化向量
        bl = Vector3.Cross(n, a);
        b = Vector3.Normalize(bl);
        for (float angle = 0, x = 0; angle < (2 * Mathf.PI - Mathf.PI / 4); angle = angle + (Mathf.PI / 4), x++)
        {
            circleXYZFirst.x = firstP.x + circleR * Mathf.Cos(angle) * a.x + circleR * Mathf.Sin(angle) * b.x;//参数方程公式
            circleXYZFirst.y = firstP.y + circleR * Mathf.Cos(angle) * a.y + circleR * Mathf.Sin(angle) * b.y;
            circleXYZFirst.z = firstP.z + circleR * Mathf.Cos(angle) * a.z + circleR * Mathf.Sin(angle) * b.z;
 
            _Vertices[(int)x] = circleXYZFirst;
        }
        for (float angle = 0, y = 8; angle < (2 * Mathf.PI - Mathf.PI / 4); angle = angle + (Mathf.PI / 4), y++)
        {
            circleXYZNext.x = secondPF.x + circleR * Mathf.Cos(angle) * a.x + circleR * Mathf.Sin(angle) * b.x;//参数方程公式
            circleXYZNext.y = secondPF.y + circleR * Mathf.Cos(angle) * a.y + circleR * Mathf.Sin(angle) * b.y;
            circleXYZNext.z = secondPF.z + circleR * Mathf.Cos(angle) * a.z + circleR * Mathf.Sin(angle) * b.z;
 
            _Vertices[(int)y] = circleXYZNext;
        }
 
        //生成所有面
        int[] _Triangles = new int[48]
        {
            //顶点0,1,9按顺序生成一个面,面朝向顺时针方向
            0,1,8,
            1,9,8,
            1,2,9,
            2,10,9,
            2,3,10,
            3,11,10,
            3,4,11,
            4,12,11,
            4,5,12,
            5,13,12,
            5,6,13,
            6,14,13,
            6,7,14,
            7,15,14,
            7,0,15,
            0,8,15
        };
 
        Mesh _mesh = new Mesh();
        _mesh.Clear();
        _mesh.vertices = _Vertices;
        _mesh.triangles = _Triangles;
        _mesh.RecalculateNormals();
        _Line.GetComponent<MeshFilter>().mesh = _mesh;
    }
    //画带拐角的管道,创建mesh
    private void DrawPipeB(Vector3 firstP, Vector3 secondP,Vector3 thirdP)
    {
        cornerPoints.Clear();
        _VerticesAllList.Clear();
 
        GameObject _Line = new GameObject("MeshLine");
        _Line.AddComponent<MeshFilter>();
        _Line.AddComponent<MeshRenderer>();
        if (lineMaterial == null) Debug.Log("The line material is empty! 线段未添加材质球!");
        else _Line.GetComponent<MeshRenderer>().material = lineMaterial;
 
        n1 = secondP - firstP;
        n2 = thirdP - secondP;
 
        n10 = n1 / Mathf.Sqrt(n1.x * n1.x + n1.y * n1.y + n1.z * n1.z);
        cornerPointF = secondP - n10 * circleR;
 
        n20 = n2 / Mathf.Sqrt(n2.x * n2.x + n2.y * n2.y + n2.z * n2.z);
        cornerPointB = secondP + n20 * circleR;
        cornerPointB_B = thirdP - n20 * circleR;
 
        //处理第一个点
        if (cornerPointBStart == Vector3.zero)
        {
            cornerPoints.Add(firstP);
        }
        else
        {
            cornerPoints.Add(cornerPointBStart);
        }
        //给第一个点赋值,留作下次用
        cornerPointBStart = cornerPointB;
        for (float i = 0; i < 1; i += density)
        {
            Vector3 cornerPot = CornerPoint(i + density, cornerPointF, cornerPointB, secondP);
            cornerPoints.Add(cornerPot);
            //Instantiate(Resources.Load("Prefabs/point11"), cornerPot, transform.rotation);
        }
        cornerPoints.Add(cornerPointB_B);
 
        for (int i = 1; i < cornerPoints.Count; i++)
        {
            n = cornerPoints[i] - cornerPoints[i - 1];
 
            //GameObject _Line = new GameObject("MeshLine" + i);
            //_Line.AddComponent<MeshFilter>();
            //_Line.AddComponent<MeshRenderer>();
            //if (lineMaterial == null) Debug.Log("The line material is empty! 线段未添加材质球!");
            //else _Line.GetComponent<MeshRenderer>().material = lineMaterial;
 
            al = Vector3.Cross(n, i1);//叉乘
            if (al == Vector3.zero)
            {
                al = Vector3.Cross(n, i2);
            }
            a = Vector3.Normalize(al);//单位化向量
            bl = Vector3.Cross(n, a);
            b = Vector3.Normalize(bl);
            if(i == 1)
            {
                for (float angle = 0, x = 0; angle < (2 * Mathf.PI - Mathf.PI / 4); angle = angle + (Mathf.PI / 4), x++)
                {
                    circleXYZFirst.x = cornerPoints[i - 1].x + circleR * Mathf.Cos(angle) * a.x + circleR * Mathf.Sin(angle) * b.x;//参数方程公式
                    circleXYZFirst.y = cornerPoints[i - 1].y + circleR * Mathf.Cos(angle) * a.y + circleR * Mathf.Sin(angle) * b.y;
                    circleXYZFirst.z = cornerPoints[i - 1].z + circleR * Mathf.Cos(angle) * a.z + circleR * Mathf.Sin(angle) * b.z;
                    _Vertices[(int)x] = circleXYZFirst;
                    _VerticesAllList.Add(_Vertices[(int)x]);//第一个圈点存入list
                }
            }
            else
            {
                for (float angle = 0, x = 0; angle < (2 * Mathf.PI - Mathf.PI / 4); angle = angle + (Mathf.PI / 4), x++)
                {
                    _Vertices[(int)x] = _Vertices[(int)x + 8];
                    //_VerticesAllList.Add(_Vertices[(int)x]);//拐角点存入list
                }
            }
            for (float angle = 0, y = 8; angle < (2 * Mathf.PI - Mathf.PI / 4); angle = angle + (Mathf.PI / 4), y++)
            {
                circleXYZNext.x = cornerPoints[i].x + circleR * Mathf.Cos(angle) * a.x + circleR * Mathf.Sin(angle) * b.x;//参数方程公式
                circleXYZNext.y = cornerPoints[i].y + circleR * Mathf.Cos(angle) * a.y + circleR * Mathf.Sin(angle) * b.y;
                circleXYZNext.z = cornerPoints[i].z + circleR * Mathf.Cos(angle) * a.z + circleR * Mathf.Sin(angle) * b.z;
                _Vertices[(int)y] = circleXYZNext;
                _VerticesAllList.Add(_Vertices[(int)y]);//除了第一圈点的剩下点存入list
            }
        }
        for(int i = 0;i < _VerticesAllList.Count; i++)
        {
            _VerticesAll[i] = _VerticesAllList[i];
        }
         
        //生成所有面
        int[] _Triangles = new int[48]
        {