Last active
March 11, 2020 09:18
-
-
Save stilllisisi/fdd2c9b0035b6180c95573193038806a to your computer and use it in GitHub Desktop.
【游戏-网格】使用Mesh网格生成圆柱网格。Mesh不止可以用于绘制图形,还可以用来生成圆柱
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//一个顶点,可以分配一个UV信息,也就是说如果生成一个6边的圆柱,你需要7个边的顶点,第一边和最后一边重合,来生成0-1之间的UV值,包括法线,切线,颜色等,如果你需要一个顶点有3个法线面向,那就需要在这个位置生成3个顶点。 | |
//源代码: | |
public Mesh CreateMesh(int edg_x, int edg_y, float rad, float len) | |
{ | |
edg_x = Mathf.Max(2, edg_x);//保证最低2个边 | |
edg_y = Mathf.Max(2, edg_y); | |
int _deglen = edg_x * edg_y + edg_y; | |
normals = new Vector3[_deglen]; | |
verts = new Vector3[_deglen]; | |
uvs = new Vector2[_deglen]; | |
int[] trians = new int[edg_x * (edg_y - 1) * 6]; | |
float reg = 6.28318f / edg_x; | |
float _len = len / (edg_y - 1); | |
Vector2 uvStep = new Vector2(1f / edg_x, 1f / (edg_y - 1)); | |
for (int y = 0; y < edg_y; y++) | |
for (int x = 0; x < edg_x + 1; x++)//多一个边来保存UV值 | |
{ | |
int i = x + y * (edg_x + 1); | |
verts[i] = new Vector3(Mathf.Sin((reg * (x % edg_x) + angle) % 6.28318f) * rad, Mathf.Cos((reg * (x % edg_x) + angle) % 6.28318f) * rad, y * _len);//计算顶点坐标 | |
normals[i] = -new Vector3(verts[i].x, verts[i].y, 0);//计算法线方向 | |
int id = x % (edg_x + 1) * 6 + y * edg_x * 6; | |
if (x < edg_x + 1 && y < edg_y - 1 && (id + 5) < trians.Length)//计算顶点数组 | |
{ | |
trians[id] = i; | |
trians[id + 1] = trians[id + 4] = i + edg_x + 1; | |
trians[id + 2] = trians[id + 3] = i + 1; | |
trians[id + 5] = i + edg_x + 2; | |
} | |
if (edg_x != 2)//计算UV,考虑到2个边的情况 | |
uvs[i] = new Vector2(x == edg_x ? 1f : uvStep.x * x, y == edg_y - 1 ? 1f : uvStep.y * y); | |
else | |
uvs[i] = new Vector2(x % edg_x, y == edg_y - 1 ? 1f : uvStep.y * y); | |
} | |
Mesh mesh = new Mesh(); | |
mesh.vertices = verts; | |
mesh.triangles = trians; | |
mesh.uv = uvs; | |
mesh.normals = normals; | |
return mesh; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gameinstitute.qq.com/community/detail/128614