Procedural Mesh - Change Vertices based on there Y Axis

Hy everyone,

I want to update my mesh when i press a button i change every verts.x - 1
If the any of the vertices.x are <= 0 then i want to change the height of that vertices.

Here is my code & and some pictures… it contains some errors. I don’t know how to manipulate my vertices very fast.

void GridHandler()
    {
        int countV = triangles.Count / 4;
        for (int i = countV - 1; i >= 0; i--)
        {
            if (vertices[triangles[i * 3 + 0]].x <= 0 && vertices[triangles[i * 3 + 1]].x <= 0 && vertices[triangles[i * 3 + 2]].x <= 0 && vertices[triangles[i * 3 + 3]].x <= 0)
            {
                Vector3 v0 = new Vector3(vertices[triangles[i * 3 + 0]].x, -2, vertices[triangles[i * 3 + 0]].z);
                Vector3 v1 = new Vector3(vertices[triangles[i * 3 + 2]].x, -2, vertices[triangles[i * 3 + 2]].z);
                Vector3 v2 = new Vector3(vertices[triangles[i * 3 + 1]].x, -2, vertices[triangles[i * 3 + 1]].z);
                Vector3 v3 = new Vector3(vertices[triangles[i * 3 + 3]].x, -2, vertices[triangles[i * 3 + 3]].z);
                vertices[triangles[i * 3 + 0]] = Vector3.Lerp(vertices[triangles[i * 3 + 0]], v0, Time.deltaTime * 0.2f);
                vertices[triangles[i * 3 + 2]] = Vector3.Lerp(vertices[triangles[i * 3 + 2]], v1, Time.deltaTime * 0.2f);
                vertices[triangles[i * 3 + 1]] = Vector3.Lerp(vertices[triangles[i * 3 + 1]], v2, Time.deltaTime * 0.2f);
                vertices[triangles[i * 3 + 3]] = Vector3.Lerp(vertices[triangles[i * 3 + 3]], v3, Time.deltaTime * 0.2f);
            }
            else
            {
                Vector3 v0 = new Vector3(vertices[triangles[i * 3 + 0]].x, 0, vertices[triangles[i * 3 + 0]].z);
                Vector3 v1 = new Vector3(vertices[triangles[i * 3 + 1]].x, 0, vertices[triangles[i * 3 + 1]].z);
                Vector3 v2 = new Vector3(vertices[triangles[i * 3 + 2]].x, 0, vertices[triangles[i * 3 + 2]].z);
                Vector3 v3 = new Vector3(vertices[triangles[i * 3 + 3]].x, 0, vertices[triangles[i * 3 + 3]].z);
                vertices[triangles[i * 3 + 0]] = Vector3.Lerp(vertices[triangles[i * 3 + 0]], v0, Time.deltaTime * 1f);
                vertices[triangles[i * 3 + 1]] = Vector3.Lerp(vertices[triangles[i * 3 + 1]], v1, Time.deltaTime * 1f);
                vertices[triangles[i * 3 + 2]] = Vector3.Lerp(vertices[triangles[i * 3 + 2]], v2, Time.deltaTime * 1f);
                vertices[triangles[i * 3 + 3]] = Vector3.Lerp(vertices[triangles[i * 3 + 3]], v3, Time.deltaTime * 1f);
            }
        }
    }

Thanks to everybody how can help me with that issue :S

Why are you dividing triangles.Count by 4? That seems like a good way to make sure that an arbitrary subset of your triangles aren’t affected.

as i said it contains errors, i want to know if this is the best way and how can i make this possible? :slight_smile:

OK, well first thing’s first: the triangles array of a mesh is always, always, always groups of 3. You seem to be using it as a group of 4 (are you thinking they’re quads?). Part of that is dividing by 4; part of that is, in your loops, you are modifying four indices (+0, +1, +2, and +3) - one triangle’s “+3” is going to be stomping on the toes of the next triangle’s “+0”. Quads are usually stored as a set of two triangles.

In terms of speed: since you’re using triangles.Count, I am guessing that that is a List<>? You almost always want to directly use native arrays for almost all mesh processing - they’re much, much faster.

All that being said, I don’t see a reason in your purpose that you need to be touching the triangles array at all. All you need to do is loop through the vertices array directly, and modify those positions. Unless I’m missing something your algorithm needs to do, you don’t need to modify or access the triangles, just the vertices.

ok i changed my code and it works fine with arrays but i don’t know the algorithm for my gridhandler… can you help me a bit?

void GridHandler()
    {
        for (int v = 0; v <= verts.Length; v++)
        {
           
        }
    }

I think this should be equivalent to what you had in your first function.

if (verts[v].x <= 0f) {
               Vector3 v0 = new Vector3(verts[v].x, -2, verts[v].z);
               vertices[v] = Vector3.Lerp(verts[v], v0, Time.deltaTime * 0.2f);
}
else {
               Vector3 v0 = new Vector3(verts[v].x, 0, verts[v].z);
               verts[v] = Vector3.Lerp(verts[v], v0, Time.deltaTime * 1f);
}

it’s not working. I already was at this point just with Lists :confused:
the vertices are always at 0 on the y axis.

Are they ever less than 0 on the X axis? (Remember this would be local space, not world space)

And are you assigning the positions back to the mesh and calling Apply()?

I did it but i have another problem. Look here: http://forum.unity3d.com/threads/procedural-mesh-looping-vertices-back-to-start.353882/