public static Mesh createCone(float radius, float height, int approx)
{
Mesh cone = new Mesh();
//create vertices
int numVertices = approx + 1;
Vector3[] vertices = new Vector3[numVertices];
vertices[0] = new Vector3(0, 0, height);
float angleIncrement = 2.0f * (float) Math.PI / approx;
for(uint i=0; i<approx; ++i){
float angle = angleIncrement * i; //angle from 0 to 2PI
vertices[i+1] = new Vector3( (float) Math.Cos(angle) * radius,
(float) Math.Sin(angle) * radius,
0);
}
//create triangles (three indices per face)
int[] triangleVertexIndices = new int[approx * 3 + (approx-1) * 3];
//mantle
for(int i=0; i < approx; ++i){
triangleVertexIndices[3 * i + 0] = 0; //first corner is always the top
triangleVertexIndices[3 * i + 1] = i + 1;
triangleVertexIndices[3 * i + 2] = (i + 2) % numVertices;
//i=0 => 0, 1, 2
//i=1 => 0, 2, 3
}
int indexOffset = 3 * approx;
//base
for(int i=0; i < approx - 1; ++i){
triangleVertexIndices[indexOffset + 3 * i + 0] = 1; //first corner is always the top
triangleVertexIndices[indexOffset + 3 * i + 2] = i + 2;
triangleVertexIndices[indexOffset + 3 * i + 1] = (i + 3) % numVertices;
//i=0 => 1, 2, 3
//i=1 => 1, 3, 4
}
//texture coordinates
//put it all together
cone.vertices = vertices;
cone.triangles = triangleVertexIndices;
//Auto-Normals
cone.RecalculateNormals();
return cone;
}