Shorten up Mesh Generator Class I'm currently writing a mesh generator class, and I'm making a method that generates vertices, normals and texcoords for a cube. But, as you can see, it's incredibly complicated and hard to follow. Is anyone willing to help me making this shorter?

public static void GenerateCube(float size, out Vector3[] vertices, out Vector2[] texCoords, out Vector3[] normals)
{
    List&lt;Vector3&gt; vertexList = new List&lt;Vector3&gt;();
    List&lt;Vector2&gt; texCoordList = new List&lt;Vector2&gt;();
    List&lt;Vector3&gt; normalList = new List&lt;Vector3&gt;();

    for (int i = 0; i &lt;= 2; i++)
    {
        Vector3 normal = i == 0 ? Vector3.UnitZ : (i == 1 ? -Vector3.UnitY : Vector3.UnitX);

        float ninety = (float)(Math.PI / 2);
        Matrix4 matrix = i == 0 ? Matrix4.Identity : (i == 1 ? Matrix4.CreateRotationX(ninety) : Matrix4.CreateRotationY(ninety));

        Vector3 vertex1a = new Vector3(-1, -1, 1);
        Vector3 vertex2a = new Vector3(-1, 1, 1);
        Vector3 vertex3a = new Vector3(1, 1, 1);
        Vector3 vertex4a = new Vector3(1, -1, 1);

        Vector3 vertex1b = new Vector3(-1, -1, 1);
        Vector3 vertex2b = new Vector3(1, -1, 1);
        Vector3 vertex3b = new Vector3(1, 1, 1);
        Vector3 vertex4b = new Vector3(-1, 1, 1);

        vertex1a = Vector3.Transform(vertex1a, matrix);
        vertex2a = Vector3.Transform(vertex2a, matrix);
        vertex3a = Vector3.Transform(vertex3a, matrix);
        vertex4a = Vector3.Transform(vertex4a, matrix);

        vertex1b = Vector3.Transform(vertex1b, matrix);
        vertex2b = Vector3.Transform(vertex2b, matrix);
        vertex3b = Vector3.Transform(vertex3b, matrix);
        vertex4b = Vector3.Transform(vertex4b, matrix);

        for (int j = -1; j &lt;= 1; j += 2)
        {
            Vector3 vertex1, vertex2, vertex3, vertex4;
            if (j == -1)
            {
                vertex1 = vertex1a;
                vertex2 = vertex2a;
                vertex3 = vertex3a;
                vertex4 = vertex4a;
            }
            else
            {
                vertex1 = vertex1b;
                vertex2 = vertex2b;
                vertex3 = vertex3b;
                vertex4 = vertex4b;
            }

            //Triangle 1
            normalList.Add(normal * j);
            texCoordList.Add(new Vector2(0, 0));
            vertexList.Add(vertex1 * j);

            normalList.Add(normal * j);
            texCoordList.Add(new Vector2(0, 1));
            vertexList.Add(vertex2 * j);

            normalList.Add(normal * j);
            texCoordList.Add(new Vector2(1, 1));
            vertexList.Add(vertex3 * j);

            //Triangle 2
            normalList.Add(normal * j);
            texCoordList.Add(new Vector2(0, 0));
            vertexList.Add(vertex1 * j);

            normalList.Add(normal * j);
            texCoordList.Add(new Vector2(1, 1));
            vertexList.Add(vertex3 * j);

            normalList.Add(normal * j);
            texCoordList.Add(new Vector2(1, 0));
            vertexList.Add(vertex4 * j);
        }
    }

    vertices = vertexList.ToArray();
    texCoords = texCoordList.ToArray();
    normals = normalList.ToArray();

    for (int i = 0; i &lt; vertices.Length; i++)
        vertices[i] *= size;
}
Im not familiar with XNA or have any experience working with matrixes, vectors and that kind of stuff. But i've made som basic refactorings to make it abit easier to read and maintain.

public static void GenerateCube(float size, out Vector3[] vertices, out Vector2[] texCoords, out Vector3[] normals)
{
        var vertexList   = new List&lt;Vector3&gt;();
        var texCoordList = new List&lt;Vector2&gt;();
        var normalList   = new List&lt;Vector3&gt;();

        const float ninety = (float)(Math.PI / 2);

        for (var i = 0; i &lt;= 2; i++)
        {
            var normal = CreateVector(i);
            var matrix = CreateMatrix(i, ninety);

            var vectorsA = CreateVectorsA();
            var vectorsB = CreateVectorsB();

            vectorsA.ForEach(x =&gt; Vector3.Transform(x, matrix));
            vectorsB.ForEach(x =&gt; Vector3.Transform(x, matrix));

            for (var j = -1; j &lt;= 1; j += 2)
            {
                var vectors = j == -1 ? vectorsA.ToArray() : vectorsB.ToArray();

                var someCalculations = CalculateSomething(j, normal);
                normalList.AddRange(someCalculations);

                AddTextureCoordinates(texCoordList);
                AddVertexes(j, vertexList, vectors);
            }
        }

        vertices  = vertexList.ToArray();
        texCoords = texCoordList.ToArray();
        normals   = normalList.ToArray();

        SetVectorSize(vertices, size);
    }

    private static void SetVectorSize(IList&lt;Vector3&gt; vertices, float size)
    {
        for (var i = 0; i &lt; vertices.Count; i++)
        {
            vertices[i] *= size;
        }
    }

    private static Vector3 CreateVector(int i)
    {
        return i == 0 ? Vector3.UnitZ : (i == 1 ? Vector3.UnitY : Vector3.UnitX);
    }

    private static Matrix4 CreateMatrix(int i, float ninety)
    {
        return i == 0 ? Matrix4.Identity : (i == 1 ? Matrix4.CreateRotationX(ninety) : Matrix4.CreateRotationY(ninety));
    }

    private static void AddVertexes(int j, ICollection&lt;Vector3&gt; vertexList, IList&lt;Vector3&gt; vectorsArray)
    {
        //Triangle 1
        vertexList.Add(vectorsArray[0] * j);
        vertexList.Add(vectorsArray[1] * j);
        vertexList.Add(vectorsArray[2] * j);

        //Triangle 2
        vertexList.Add(vectorsArray[0] * j);
        vertexList.Add(vectorsArray[2] * j);
        vertexList.Add(vectorsArray[3] * j);
    }

    private static void AddTextureCoordinates(ICollection&lt;Vector2&gt; texCoordList)
    {
        //Triangle 1
        texCoordList.Add(new Vector2(0, 0));
        texCoordList.Add(new Vector2(0, 1));
        texCoordList.Add(new Vector2(1, 1));

        //Triangle 2
        texCoordList.Add(new Vector2(0, 0));
        texCoordList.Add(new Vector2(1, 1));
        texCoordList.Add(new Vector2(1, 0));
    }

    private static IEnumerable&lt;Vector3&gt; CalculateSomething(int j, Vector3 normal)
    {
        var someCalculation = normal * j;
        return Enumerable.Repeat(someCalculation, 6);
    }

    private static List&lt;Vector3&gt; CreateVectorsB()
    {
        return new List&lt;Vector3&gt;
                   {
                       new Vector3(-1, -1, 1),
                       new Vector3(1, -1, 1),
                       new Vector3(1, 1, 1),
                       new Vector3(-1, 1, 1)
                   };
    }

    private static List&lt;Vector3&gt; CreateVectorsA()
    {
        return new List&lt;Vector3&gt;
                   {
                       new Vector3(-1, -1, 1),
                       new Vector3(-1, 1, 1),
                       new Vector3(1, 1, 1),
                       new Vector3(1, -1, 1)
                   };
    }