创建平面网格

时间:2013-09-11 19:57:32

标签: c# unity3d unityscript

我正在努力创建一个统一的平面网格。我能够获得一个有两个三角形的网格,但是我无法获得连续两个以上的网格。我试图获得一个连接在一起的连续多个三角形的长网格。如果您查看我的代码,我希望您能理解。

我的代码没有错误,但我无法看到网格。我需要帮助。

private float space = 40 / 200;
    MeshFilter mf;
    Mesh mesh;
    private Vector3[] vertices = new Vector3[400];

    void Start () 
    {
        mf = GetComponent<MeshFilter>();
        mesh = mf.mesh;
        mesh.Clear();

        #region Vertices
        for (int i = 0; i < 200; i++)
        {
            vertices[i] = new Vector3(-20 + i * space, -6, 0);          //Top vertices
            vertices[i+200] = new Vector3(-20 + i * space, -10, 0);     //Bottom vertices
        }
        #endregion

        #region Normals
        Vector3[] Normals = new Vector3[400];

        for(int i = 0; i < 400; i++)
            Normals[i] = Vector3.back;
        #endregion

        #region uvs
        Vector2[] uvs = new Vector2[400];

        Vector2 _00 = new Vector2(0, 0);
        Vector2 _10 = new Vector2(1, 0);
        Vector2 _01 = new Vector2(0, 1);
        Vector2 _11 = new Vector2(1, 1);

        for(int i=0; i<200; i++)
        {
            uvs[i] = new Vector2(i/199, 1);
            uvs[i+200] = new Vector2(0, i/199);
        }

        #endregion

        #region Triangles
        int[] Triangle = new int[(200-1) * 2 * 3];
        int t = 0;

        for(int i=0; i<200 - 1; i++)
        {
            Triangle[t++] = i;
            Triangle[t++] = i + 200 + 1;
            Triangle[t++] = i + 200;

            Triangle[t++] = i;
            Triangle[t++] = i + 1;
            Triangle[t++] = i + 200 + 1;
        }
        #endregion

        mesh.vertices = vertices;
        mesh.normals = Normals;
        mesh.uv = uvs;
        mesh.triangles = Triangle;

        mesh.RecalculateBounds();
        mesh.Optimize();
    }

1 个答案:

答案 0 :(得分:1)

目前尚不清楚这是否是根本问题,但你正在做integer arithmeticfloat space = 40 / 200;总是等于0,i/199总是等于0,对于i&lt; 199.

这意味着你的顶点看起来像:

vertices[i] = new Vector3(-20 + i * 0, -6, 0);          //Top vertices
vertices[i+200] = new Vector3(-20 + i * 0, -10, 0);

紫外线看起来像:

uvs[i] = new Vector2(0, 1);
uvs[i+200] = new Vector2(0, 0);

您可以将float space = 40 / 200;更改为float space = (float)40 / 200;,将i/199更改为(float)i/199