I have to cube mesh and each face is a separate submesh. I would like to be able to drag a submesh with the mouse. This is to change the size of the cube. I have the index of the submesh.
I have the following code, but the delta calculation is not working. Currently the submesh gets distorted and does not move evenly. What would be the correct way to calculate the delta movement?
private Vector3 worldPosition;
void OnMouseDown()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = Camera.main.nearClipPlane;
worldPosition = Camera.main.ScreenToWorldPoint(mousePos);
}
void OnMouseDrag()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = Camera.main.nearClipPlane;
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(mousePos);
Vector3 delta = currentPosition - worldPosition;
int[] subMeshTris = mf.mesh.GetTriangles(submeshIndex);
Mesh mesh = mf.mesh;
Vector3[] vertices = mesh.vertices;
for (int i = 0; i < subMeshTris.Length; i++)
{
vertices[subMeshTris[i]] += delta;
}
mesh.vertices = vertices;
mf.mesh = mesh;
}
I have figured it out. The submeshTris array had two vertices which repeated and the delta was thus added to these vertices twice in each loop. I added a List which I populated with unique vertex indices.