040.png
41.png
MeshFilter mesh_filter = gameObject.AddComponent<MeshFilter>();
MeshRenderer mesh_renderer = gameObject.AddComponent<MeshRenderer>();
// gameObject.GetComponent<MeshRenderer>().material
Mesh mesh = mesh_filter.mesh;
mesh.Clear();
//设置顶点 N个顶点,就会有N-1个三角形
float radius1 = 0.5f; //圆的半径
float radius2 = 1; //圆的半径
int segments = 64; //设置有64个梯形
Vector3[] vertices = new Vector3[segments * 2]; //多少个顶点
Vector3 centerCircle = new Vector3(0,0,0); //圆心点
//Mathf.Deg2Rad 度转弧度:Mathf.Deg2Rad = (PI * 2) / 360
float deltaAngle = Mathf.Deg2Rad * 360f / segments;
float currentAngle = 0;
//设置三角形顶点顺序,顺时针设置
for (int i = 0; i < vertices.Length; i= i+2)
{
float cosA = Mathf.Cos(currentAngle);
float sinA = Mathf.Sin(currentAngle);
vertices[i] = new Vector3(cosA * radius1 + centerCircle.x, sinA * radius1 + centerCircle.y, 0);
vertices[i + 1] = new Vector3(cosA * radius2 + centerCircle.x, sinA * radius2 + centerCircle.y, 0);
currentAngle += deltaAngle;
}
//顶点 1个梯形有2个三角形,每个三角形有3个顶点
int[] triangles = new int[segments * 2 * 3];
//0,3,1, 0,2,3 2,5,3 2,4,5,... 2(segments -1)次方,
for (int i = 0, j = 0; i < segments * 6; i += 6, j += 2)
{
triangles[i] = j;
triangles[i + 1] = (j + 3) % vertices.Length;
triangles[i + 2] = (j + 1) % vertices.Length;
triangles[i + 3] = j;
triangles[i + 4] = (j + 2) % vertices.Length;
triangles[i + 5] = (j + 3) % vertices.Length;
}
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
网友评论