1
\$\begingroup\$

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. enter image description here

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
        triangles.Add((i*4)+4);
        triangles.Add((i*4)+1);
        triangles.Add((i*4));

        triangles.Add((i*4)+1);
        triangles.Add((i*4)+4);
        triangles.Add((i*4)+5);

        //top face
        triangles.Add((i*4)+5);
        triangles.Add((i*4)+2);
        triangles.Add((i*4)+1);

        triangles.Add((i*4)+2);
        triangles.Add((i*4)+5);
        triangles.Add((i*4)+6);

        //right face
        triangles.Add((i*4)+2);
        triangles.Add((i*4)+6);
        triangles.Add((i*4)+7);

        triangles.Add((i*4)+7);
        triangles.Add((i*4)+3);
        triangles.Add((i*4)+2);

        //bottom face
        triangles.Add((i*4)+0);
        triangles.Add((i*4)+3);
        triangles.Add((i*4)+4);

        triangles.Add((i*4)+4);
        triangles.Add((i*4)+3);
        triangles.Add((i*4)+7);

    }
\$\endgroup\$
  • \$\begingroup\$ Seems like you know how to do it, but need help debugging your code? \$\endgroup\$ – MichaelHouse Aug 8 '17 at 3:41
  • \$\begingroup\$ 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? \$\endgroup\$ – Grigori Aug 8 '17 at 3:43
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Aug 8 '17 at 4:20
  • \$\begingroup\$ 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. \$\endgroup\$ – Grigori Aug 8 '17 at 4:40
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Aug 8 '17 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.