Unity3D生成一段隧道网格的方法
本文实例为大家分享了Unity3D生成一段隧道网格的具体代码,供大家参考,具体内容如下
一、需求
最近有一个需求,生成段隧道的骨架网格。目前想到的方法就是,获取隧道网格,通过一个算法分离内框和外框的点:
然后通过外框和内框上的点画线,可以通过深度优先搜索得到内外两个环的序列,从而实现骨架网格
二、生成隧道算法
隧道由段圆弧和一条直线组成,所以算法如下:
1、取圆心为0,0,0,和圆上半径的向量\underset{OB}{\rightarrow} ,绕z轴,旋转向量\underset{OB}{\rightarrow},取到圆上的点,外框上的点同理可得
2、平移内外框上的点,z轴加上偏离,得到隧道另一端的点
3、取相邻外框上的点和平移后的四个点,生成两个三角形。内框同理可得。
三、效果如下:
四、实现:
usingSystem.Collections; usingSystem.Collections.Generic; usingUnityEngine; publicclassMeshCreater:MonoBehaviour { Meshmesh; publicMaterialmat;//mesh材质 publicGameObjectgame; //Startiscalledbeforethefirstframeupdate voidStart() { mesh=newMesh(); mesh.Clear(); SetVertivesUV(); SetTriangles(); mesh.vertices=vertices.ToArray(); mesh.triangles=triangles; GameObjectobj_cell=newGameObject(); obj_cell.name="cell"; mesh.RecalculateNormals();//重置法线 mesh.RecalculateBounds();//重置范围 obj_cell.AddComponent().mesh=mesh; obj_cell.AddComponent (); obj_cell.GetComponent ().material=mat; MeshCaluatemesh_caluate=newMeshCaluate(); mesh_caluate.CalculateMesh(mesh); } //Updateiscalledonceperframe voidUpdate() { } publicList vertices=newList (); privatefloatangle=10; privatefloatmax_angle=120; //设置顶点信息 voidSetVertivesUV() { Vector3dir1=newVector3(Mathf.Sqrt(3f),-1,0); Vector3dir2=dir1*0.8f; List points1=newList (); List points2=newList (); intcount=(int)((360-max_angle)/angle); for(inti=0;i 五、缺陷
UV未计算,所以使用贴图时有问题
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。