Problem with making mesh at runtime. (Unity3d)

127 Views Asked by At

I'm trying to make mesh with Vector3 data from database at runtime.

Here is my code

private IEnumerator CreateShape()
{
    int count = 0;

    triangles = new int[x_size * y_size * 6];

    //index buffer
    for (int i = 0; i < x_size - 1; i++)
    {
        for (int j = 0; j < y_size - 1; j++)
        {
            triangles[((i * y_size + j) * 6) + 0] = i * y_size + j;
            triangles[((i * y_size + j) * 6) + 1] = (i + 1) * y_size + j;
            triangles[((i * y_size + j) * 6) + 2] = i * y_size + j + 1;
            triangles[((i * y_size + j) * 6) + 3] = i * y_size + j + 1;
            triangles[((i * y_size + j) * 6) + 4] = (i + 1) * y_size + j;
            triangles[((i * y_size + j) * 6) + 5] = (i + 1) * y_size + j + 1;
        }
        yield return new WaitForSeconds(0.01f);
    }
}

And check updates for updating Mesh data..

private void UpdateMesh()
{
    mesh.Clear();

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

Vertices data has no problem, cuz i checked it with different way.

private void OnDrawGizmos()
{
    if (vertices[x_size * y_size - 1] != Vector3.zero)
    {
        for (int i = 0; i < x_size - 1; i++)
        {
            for (int j = 0; j < y_size - 1; j++)
            {
                Gizmos.DrawSphere(vertices[i * y_size + j], .1f);
            }
        }
    }
}

Checked whether vertices data is wrong or not with gizmos but the data has no probs:

enter image description here

It should form a simple plane mesh shape, but it goes back to the start point and continue making in the middle of mesh-create-process..

What i expected: enter image description here

But it goes wrong: enter image description here

It seemed somehow internal index buffer calculation goes wrong..

Vertices data is about 160,000ea Vector3, and tris is also about 160,000*6 = 1,000,000ea integer.

Maybe the data is too much for making mesh while runtime at all..?

or.. the axis is changed when x value become larger than y value..?

Why this happens? How can i fix this?

1

There are 1 best solutions below

0
derHugo On BEST ANSWER

As said per default meshes in unity have an index buffer of 16-bit (= UInt16) meaning the maximum index is 65535.

If you want to go higher (like with your mesh) you will need to change the Mesh.indexFormat to 32-bit (= UInt32), allowing a maximum index of 4294967295.

Note though that not all GPU support this!