0

我正在尝试统一创建平面网格。我能够获得具有两个三角形的网格,但无法获得连续两个以上的网格。我正在尝试获得一个长网格,其中有许多连续连接在一起的三角形。如果你看看我的代码,我希望你能理解。

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

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();
    }
4

1 回答 1

1

目前尚不清楚这是否是根本问题,但您正在做整数运算float space = 40 / 200;总是等于 0,i/199对于 i < 199 总是等于 0。

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

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

UV 看起来都像:

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

于 2013-09-12T13:37:51.253 回答