Could you find the Error? - Procedural Mesh Vertex Looping

Hi everybody,

I created a mesh from code and now i want to loop it when i press a Button.

Here is the code how i fill my Array

 #region vertices
        for (int x = 0; x < (width * segWidth); x += segWidth)
        {
            for (int z = 0; z < (depth * segDepth); z += segDepth)
            {
                vertices.Add(new Vector3(x, 0, z));
                vertices.Add(new Vector3(x + segWidth, 0, z));
                vertices.Add(new Vector3(x, 0, z + segDepth));
                vertices.Add(new Vector3(x + segWidth, 0, z + segDepth));
            }
        }
        #endregion

It looks like this picture

It works fine on the X axis, it’s just the Z axis… and i don’t know what i’m doing wrong :S

Here’re some pictures to explain the example:

And the Error is the Z axis (see following pictures):

And here is my algorithm:

void GridHandler()
    {
        for (int v = 0; v <= m.vertices.Length - 1; v++)
        {
            if(m.vertices[v].x < 1)
            {
                #region X Negativ
                    try
                    {
                        if (m.vertices[v + 1].x < 1)
                        {
                            finalPos[v] = new Vector3(finalPos[v].x + (width * segWidth), 0, finalPos[v].z);
                            m.vertices[v] = finalPos[v];
                        }
                    }
                    catch
                    {
                        print("X Negativ Reset");
                        finalPos[v] = new Vector3(finalPos[v].x + (width * segWidth), 0, finalPos[v].z);
                        m.vertices[v] = finalPos[v];
                    }
                #endregion
            }
            else if (m.vertices[v].x > (width * segWidth - 1))
            {
                #region X Positiv
                    try
                    {
                        if (m.vertices[v - 1].x > (width * segWidth - 1))
                        {
                            finalPos[v] = new Vector3(finalPos[v].x - (width * segWidth), 0, finalPos[v].z);
                            m.vertices[v] = finalPos[v];
                        }
                    }
                    catch
                    {
                        print("X Positiv Reset");
                        finalPos[v] = new Vector3(finalPos[v].x - (width * segWidth), 0, finalPos[v].z);
                        m.vertices[v] = finalPos[v];
                    }
                #endregion
            }
            else if (m.vertices[v].z < 1)
            {
                #region Z Negativ
                    try
                    {
                        if (m.vertices[v + 2].z < 1)
                        {
                            finalPos[v] = new Vector3(finalPos[v].x, 0, finalPos[v].z + (depth * segDepth));
                            m.vertices[v] = finalPos[v];
                        }
                    }
                    catch
                    {
                        print("Z Negativ Reset");
                        finalPos[v] = new Vector3(finalPos[v].x, 0, finalPos[v].z + (depth * segDepth));
                        m.vertices[v] = finalPos[v];
                    }
                #endregion
            }
            else if (m.vertices[v].z > (depth * segDepth - 1))
            {
                #region Z Positiv
                    try
                    {
                        if (m.vertices[v - 2].z > (depth * segDepth - 1))
                        {
                            finalPos[v] = new Vector3(finalPos[v].x, 0, finalPos[v].z - (depth * segDepth));
                            m.vertices[v] = finalPos[v];
                        }
                    }
                    catch
                    {
                        print("Z Positiv Reset");
                        finalPos[v] = new Vector3(finalPos[v].x, 0, finalPos[v].z - (depth * segDepth));
                        m.vertices[v] = finalPos[v];
                    }
                #endregion
            }
            else
            {
                m.vertices[v] = finalPos[v];
            }
        }
    }

PS: Can somebody show me how to Lerp this? Thx :slight_smile:

Try and simplify your grid so that it is a 2x2 set of tiles and then you can step over the total of 16 verts and see what is happening.

You are making separate vertices for the left/right side of each quad, right? Shared verts at touching edges won’t be useful for you in this instance, if I understand what you’re trying to do.

Now it’s a bit easier, but i don’t know how to solve this problem…

It seems to indicate you are not moving the ends of a strip of verts. Take a look at your start/end limiting logic and make sure it is not off-by-one. Off-by-one bugs are a super-common type of error that would be the first type of defect I would look like in this case. If it heps you, print the X,Y,Z value of each vert as you move it, and confirm you are moving all the verts you think you are (you might only be moving the middle ones, not the end ones), and that they are correct before and after moving (in case you are moving them, but the end ones are being moved by an incorrect amount).