# Moving triangles in the editor?

Discussion in 'Scripting' started by BlackArcane, Jan 18, 2015.

1. ### BlackArcane

Joined:
Jun 26, 2011
Posts:
119
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?

2. ### Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
4,245
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.

3. ### aaro4130

Joined:
Jul 15, 2010
Posts:
390
First triangle:
vertices[indices[0]]
vertices[indices[1]]
vertices[indices[2]]

And so on.

4. ### BlackArcane

Joined:
Jun 26, 2011
Posts:
119
I have the following snippets for creating the handles at the correct positions and for moving the vertices when handles are moved:
Code (csharp):
1.
2. if(moveIndices)
3. {
4.     for(inti = 2; i < inds.Length; i += 3)
5.     {
6.         Vector3vert1 = transform.TransformPoint(verts[inds[i]]);
7.         Vector3vert2 = transform.TransformPoint(verts[inds[i-1]]);
8.         Vector3vert3 = transform.TransformPoint(verts[inds[i-2]]);
9.
10.         Vector3sum = Vector3.zero;
11.         sum += vert1 + vert2 + vert3;
12.         sum = sum/3;
13.
14.         GameObjecthandle = newGameObject(TAG_HANDLE_INDEX);
15.         handle.transform.position = sum;
16.         handle.transform.parent = transform;
17.         handle.tag = TAG_HANDLE_INDEX;
18.         handle.AddComponent<IrisGizmo>()._parent = this;
19.       }
20. }
21.
Code (csharp):
1.
2. indexHandles = GameObject.FindGameObjectsWithTag (TAG_HANDLE_INDEX);
3. if(moveIndices)
4. {
5.         intj = 1;
6.         for(inti = 2; i < inds.Length; i += 3)
7.         {
8.                verts[inds[i]] = indexHandles[i-2*j].transform.localPosition;
9.                verts[inds[i-1]] = indexHandles[i-2*j].transform.localPosition;
10.                verts[inds[i-2]] = indexHandles[i-2*j].transform.localPosition;
11.                j++;
12.         }
13. }
14.
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...

Last edited: Jan 23, 2015
5. ### Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
4,245
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?

6. ### BlackArcane

Joined:
Jun 26, 2011
Posts:
119
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...