//角度为0-360正值,0起X轴逆时针
public static GameObject CreatePentagonMesh(float _radius, float _innerradius, float _startangle, float _angledegree, float _height,GameObject obj = null)
{
//Debug.Log(string.Format("起始角度 = {0},总角度 = {1}.", _startangle, _angledegree));
GameObject tempGo = obj;
if (tempGo == null)
{
tempGo = new GameObject("Pentagon");
}
MeshFilter meshFilter = tempGo.GetOrAddComponent<MeshFilter>();
MeshRenderer meshRenderer = tempGo.GetOrAddComponent<MeshRenderer>();
Shader Shader = Shader.Find("Unlit/Color");
Vector3[] vertices = new Vector3[30];
float totalAngle = _angledegree - _startangle;
float tempDis = _innerradius / Mathf.Cos(totalAngle * 0.5f * Mathf.Deg2Rad);
Vector3 leftDir = Quaternion.AngleAxis(_angledegree, Vector3.up) * Vector3.right;
Vector3 middleDir = Quaternion.AngleAxis(_startangle + totalAngle * 0.5f, Vector3.up) * Vector3.right;
Vector3 rightDir = Quaternion.AngleAxis(_startangle, Vector3.up) * Vector3.right;
//bottom
vertices[0] = tempDis * leftDir;
vertices[1] = _radius * leftDir;
vertices[2] = _radius * middleDir;
vertices[3] = _radius * rightDir;
vertices[4] = tempDis * rightDir;
if (vertices[0].magnitude > vertices[1].magnitude)
{
Debug.Log("角度或半径差不太合适。");
}
//top
for (int i = 0; i < 5; i++)
{
vertices[i + 5] = vertices[i] + _height * Vector3.up;
}
vertices[10] = vertices[0];
vertices[11] = vertices[5];
vertices[12] = vertices[9];
vertices[13] = vertices[4];
vertices[14] = vertices[4];
vertices[15] = vertices[9];
vertices[16] = vertices[8];
vertices[17] = vertices[3];
vertices[18] = vertices[3];
vertices[19] = vertices[8];
vertices[20] = vertices[7];
vertices[21] = vertices[2];
vertices[22] = vertices[2];
vertices[23] = vertices[7];
vertices[24] = vertices[6];
vertices[25] = vertices[1];
vertices[26] = vertices[1];
vertices[27] = vertices[6];
vertices[28] = vertices[5];
vertices[29] = vertices[0];
int[] triangles = new int[]
{
0,1,2,
0,2,4,
4,2,3,
5,7,6,
5,9,7,
9,8,7,
0,9,5,
0,4,9,
4,8,9,
4,3,8,
3,7,8,
3,2,7,
2,6,7,
2,1,6,
1,5,6,
1,0,5,
};
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
meshFilter.mesh = mesh;
meshRenderer.material.shader = Shader;
meshRenderer.material.color = Color.gray;
mesh.RecalculateNormals();
MeshCollider mc = tempGo.GetOrAddComponent<MeshCollider>();
mc.sharedMesh = mesh;
mc.convex = true;
mc.isTrigger = true;
return tempGo;
}
|