Exemple #1
0
    public override void updateMesh()
    {
        mesh         = new Mesh();
        subTriangles = new List <Vector3>();
        List <Vector3> verts   = new List <Vector3>();
        List <Vector3> normals = new List <Vector3>();
        List <int>     tris    = new List <int>();

        Vector3[,] controlVec = new Vector3[control.GetLength(0), control.GetLength(1)];

        for (int x = 0; x < control.GetLength(0); ++x)
        {
            for (int y = 0; y < control.GetLength(1); ++y)
            {
                controlVec[x, y] = control[x, y].position;
            }
        }

        //generate vertices
        for (int x = 0; x <= resolution; ++x)
        {
            for (int y = 0; y <= resolution; ++y)
            {
                verts.Add(GeometryUtil.BezerpQuad(controlVec, (float)x / (resolution), (float)y / (resolution)));
            }
        }

        //generate triangles
        for (int x = 0; x < resolution; ++x)
        {
            for (int y = 0; y < resolution; ++y)
            {
                int c0 = x + y * (resolution + 1);
                int c1 = c0 + 1;
                int c2 = c1 + resolution;
                int c3 = c2 + 1;

                tris.Add(c0); tris.Add(c1); tris.Add(c2);
                tris.Add(c3); tris.Add(c2); tris.Add(c1);
            }
        }

        int vertCount = verts.Count;

        mesh.SetVertices(verts);
        mesh.SetTriangles(tris, 0);
        mesh.RecalculateNormals();
        normals = new List <Vector3>(mesh.normals);

        //generate backface
        for (int i = 0; i < 6 * resolution * resolution; i += 3)
        {
            tris.Add(tris[i] + vertCount);
            tris.Add(tris[i + 2] + vertCount);
            tris.Add(tris[i + 1] + vertCount);
            verts.Add(verts[i / 3]);
            normals.Add(-normals[i / 3]);
        }


        mesh.SetVertices(verts);
        mesh.SetTriangles(tris, 0);
        mesh.SetNormals(normals);
        mesh.RecalculateBounds();
    }