Unity3D生成一段隧道网格的方法

这篇文章主要为大家详细介绍了Unity3D生成一段隧道网格的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Unity3D生成一段隧道网格的具体代码,供大家参考,具体内容如下

一、需求

最近有一个需求,生成段隧道的骨架网格。目前想到的方法就是,获取隧道网格,通过一个算法分离内框和外框的点:

然后通过外框和内框上的点画线,可以通过深度优先搜索得到内外两个环的序列, 从而实现骨架网格

二、生成隧道算法

隧道由段圆弧和一条直线组成,所以算法如下:

1、取圆心为0,0,0,和圆上半径的向量 \underset{OB}{\rightarrow}  ,绕z轴,旋转向量\underset{OB}{\rightarrow},取到圆上的点,外框上的点同理可得

2、平移内外框上的点,z轴加上偏离,得到隧道另一端的点

3、取相邻外框上的点和平移后的四个点,生成两个三角形。内框同理可得。

三、效果如下:

四、实现:

 using System.Collections; using System.Collections.Generic; using UnityEngine; public class MeshCreater : MonoBehaviour { Mesh mesh; public Material mat;//mesh材质 public GameObject game; // Start is called before the first frame update void Start() { mesh = new Mesh(); mesh.Clear(); SetVertivesUV(); SetTriangles(); mesh.vertices = vertices.ToArray(); mesh.triangles = triangles; GameObject obj_cell = new GameObject(); obj_cell.name = "cell"; mesh.RecalculateNormals();//重置法线 mesh.RecalculateBounds(); //重置范围 obj_cell.AddComponent().mesh = mesh; obj_cell.AddComponent(); obj_cell.GetComponent().material = mat; MeshCaluate mesh_caluate = new MeshCaluate(); mesh_caluate.CalculateMesh(mesh); } // Update is called once per frame void Update() { } public List vertices = new List(); private float angle = 10; private float max_angle = 120; // 设置顶点信息 void SetVertivesUV() { Vector3 dir1 = new Vector3(Mathf.Sqrt(3f), -1, 0); Vector3 dir2 = dir1 * 0.8f; List points1 = new List(); List points2 = new List(); int count = (int)((360 - max_angle) / angle); for (int i = 0; i 

五、缺陷

UV未计算,所以使用贴图时有问题

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是Unity3D生成一段隧道网格的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程