Moving triangles in the editor?

I have been working on a small mesh editor inside the Unity editor for minor modifications on meshes and rapid prototyping. I have a working gizmo system to draw handles and I can move vertices just fine. (After all, they are just Vector3s, I just set a new position) My problem is that I have no way of moving triangles (indices). I think that the correct way to do it is to just move the vertices that “make” the triangle and that would be easy if I had a way to actually get the corresponding vertices from the int[ ] unity’s mesh contains. Any ideas?
Thanks in advance.

You use the integer values in the triangles array to look into the .vertices[ ] table and pull up the vertices from there. So if you click on the triangle with vertex indices i,j,k, you would adjust the coordinates of the vertices at .vertices and .vertices[j] and .vertices[k].
Pretty sure you need to copy the array down from the mesh, modify it, then put it back as an entire array. I don’t think you can modify individual vertices while they are actually in the mesh, but I could be wrong.

First triangle:
vertices[indices[0]]
vertices[indices[1]]
vertices[indices[2]]

And so on. :slight_smile:

I have the following snippets for creating the handles at the correct positions and for moving the vertices when handles are moved:

if(moveIndices)
{
    for(inti = 2; i < inds.Length; i += 3)
    {
        Vector3vert1 = transform.TransformPoint(verts[inds[i]]);
        Vector3vert2 = transform.TransformPoint(verts[inds[i-1]]);
        Vector3vert3 = transform.TransformPoint(verts[inds[i-2]]);

        Vector3sum = Vector3.zero;
        sum += vert1 + vert2 + vert3;
        sum = sum/3;

        GameObjecthandle = newGameObject(TAG_HANDLE_INDEX);
        handle.transform.position = sum;
        handle.transform.parent = transform;
        handle.tag = TAG_HANDLE_INDEX;
        handle.AddComponent<IrisGizmo>()._parent = this;
      }
}
indexHandles = GameObject.FindGameObjectsWithTag (TAG_HANDLE_INDEX);
if(moveIndices)
{
        intj = 1;
        for(inti = 2; i < inds.Length; i += 3)
        {
               verts[inds[i]] = indexHandles[i-2*j].transform.localPosition;
               verts[inds[i-1]] = indexHandles[i-2*j].transform.localPosition;
               verts[inds[i-2]] = indexHandles[i-2*j].transform.localPosition;
               j++;
        }
}

However, my triangles appear messed up. Some index handles appear in the position of vertices and only move one vertex while others appear correctly but do not affect vertices at all. Any ideas?

EDIT: I figured out the problem. When setting the vectors’ positions I try to set them to the index handle position, which is not correct but I still can’t find the correct solution…

In your second script block above, the 8th, 9th and 10th line all copy the same index handle location [ i - 2 * j]… I think you want three different ones perhaps?

I want the position of that handle so the three indexes that are the same are ok. I updated the code to recalculate the center and subtract the two vectors (old and new center) and I add this to the position of the vertices but for some reason it does not work…