# How to procedurally create an extruded cube mesh's indicies?

I'm working on a tool that allows the user to create rails(basically an extruded cube) by placing 3d points where the vertices of rail would be. The picture below shows what I'm talking about. The orange points being the user placed points and the black points being the vertices created around it.

I can create the vertices just fine the problem comes from creating the indices for the rail.

I tried using a for loop that goes through the user place points and creating the triangle indices by using i as an offset for each indices, but it using vertices that didn't exist for indices. Here's the code for the vertex and indices creation.

    List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
for (int i = 0; i < points.Count - 1; i++)
{
Vector3 forward = -(points[i] - points[i + 1]).normalized;
Quaternion rotation = Quaternion.LookRotation(forward);

//bottom left
vertices.Add(transform.InverseTransformPoint(points[i] + rotation * (-transform.up - transform.right)));
//top left
vertices.Add(transform.InverseTransformPoint(points[i] + rotation * (transform.up - transform.right)));
//top right
vertices.Add(transform.InverseTransformPoint(points[i] + rotation * (transform.up + transform.right)));
//bottom right

vertices.Add(transform.InverseTransformPoint(points[i] + rotation * (-transform.up + transform.right)));
}

for(int i=0; i<points.Count-1; i++){
//left face

//top face

//right face

//bottom face

}

• Seems like you know how to do it, but need help debugging your code?
– House
Aug 8, 2017 at 3:41
• Well I'm not sure if I know how, my method didn't work so i guess I don't. But I did have a method so maybe I do? Aug 8, 2017 at 3:43
• Seems like you have the right idea. Just going out of bounds at some point. Check your end conditions and attach the debugger to see where things are going wrong.
– House
Aug 8, 2017 at 4:20
• I know where its going wrong but I don't know how to fix it. When creating indices it it uses vertex elements that aren't in the list. Let's say there's a 3 point rail, that means there's 12 vertices total. When creating the indices for point 3 and it would do (2*4)+7 which would be 15 a vertex that doesn't exist and I don't know how where to go from there. Aug 8, 2017 at 4:40
• Each rail has 4 * (2 * (points + 1)) triangles. Try focusing on just the top triangles first, then add the left, right and bottom. Draw it out on paper, to get it right in your head.
– House
Aug 8, 2017 at 4:50