在编辑器中点选一个模型的时候,是选中整个模型的Mesh,如果我们想选中其中一个三角面的话(比如做面的吸附、或者获取选中的顶点坐标)可以按下面这样,基于原Mesh生成对应的三角面。然后就可以点选三角面了。
private void SpliteMesh(Mesh M){
Vector3[] verts = M.vertices;
Vector3[] normals = M.normals;
int[] indices = M.triangles;
for (int i = 0; i < indices.Length; i += 3) {
Vector3[] newVerts = new Vector3[3];
Vector3[] newNormals = new Vector3[3];
//Vector2[] newUvs = new Vector2[3];
for (int n = 0; n < 3; n++) {
int index = indices[i + n];
newVerts[n] = verts[index];
//newUvs[n] = uvs[index];
newNormals[n] = normals[index];
}
Mesh mesh = new Mesh();
mesh.vertices = newVerts;
mesh.normals = newNormals;
mesh.uv = newUvs;
mesh.triangles = new int[] { 0, 1, 2 ,2,1,0};
GameObject GO = new GameObject("Triangle " + (i / 3));
GO.AddComponent<FaceHandler> ();
GO.transform.position = transform.position;
GO.transform.rotation = transform.rotation;
GO.AddComponent<MeshRenderer>().material = mat;
GO.AddComponent<MeshFilter>().mesh = mesh;
GO.transform.SetParent (this.transform);
}
}
网友评论