예제 #1
0
    void GenerateMesh()
    {
        //Create a new mesh
        Mesh mesh = new Mesh();

        mesh.name = "TubeMesh";
        //Pass the mesh to this object's meshFilter
        meshFilter.mesh = mesh;

        List <Vector3> vertices = new List <Vector3>();

        Torus = new Torus {
            MajorRadius = MajorRadius, MinorRadius = minorRadius
        };
        float curveAngle = 2f * Mathf.PI / (float)CurveSegments;
        float pipeAngle  = 2f * Mathf.PI / (float)tubeIncrement;

        for (int i = 0; i < Segments; i++)
        {
            for (int j = 0; j < tubeIncrement; j++)
            {
                vertices.Add(Torus.PointOnTorus(i * curveAngle, j * pipeAngle));
            }
        }

        mesh.vertices = vertices.ToArray();
        List <int> triangles = new List <int>();

        for (int i = 1; i < Segments; i++)
        {
            for (int j = 0; j < tubeIncrement; j++)
            {
                int row        = i * tubeIncrement;
                int lastRow    = (i - 1) * tubeIncrement;
                int column     = j;
                int nextColumn = (j + 1) % tubeIncrement;
                //Make the triangles in reverse order to invert the normals
                //Make a triangle with the vertex one row behind this one and the vertex one row behind and one column over
                triangles.Add(lastRow + column);
                triangles.Add(lastRow + nextColumn);
                triangles.Add(row + column);
                //Make a triangle with the vertex one column over and the vertex one row behind and one column over
                triangles.Add(lastRow + nextColumn);
                triangles.Add(row + nextColumn);
                triangles.Add(row + column);
            }
        }

        mesh.triangles = triangles.ToArray();
        mesh.RecalculateNormals();
    }