我必须立方体网格,每个面都是一个单独的子网格。我希望能够用鼠标拖动子网格。这是改变立方体的大小。我有子网格的索引。
我有以下代码,但增量计算不起作用。目前,子网格体扭曲并且移动不均匀。计算增量运动的正确方法是什么?
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;
}
我已经弄清楚了。 submeshTris 数组有两个重复的顶点,因此在每个循环中将增量添加到这些顶点两次。我添加了一个列表,其中填充了唯一的顶点索引。
List<int> uniqueIndices = new List<int>();
for (int i = 0; i < subMeshTris.Length; i++)
{
if (!uniqueIndices.Contains(subMeshTris[i]))
{
uniqueIndices.Add(subMeshTris[i]);
}
}
for (int i = 0; i < uniqueIndices.Count; i++)
{
vertices[uniqueIndices[i]] += delta;
}