我刚刚开始在Unity中开发,我希望开发一个简单的增强现实应用程序。该应用程序的目标是在图像上显示3D立方体,使用Vuforia通过我的网络摄像头检测到该立方体。接下来,我将使用Leap Motion允许手势选择其中一个蓝色球体并将其拖动到另一个位置,使立方体在该顶点处变形。
开放式3D立方体是导入的3D模型。我在Unity中添加的蓝色球体,并将以下脚本添加到立方体中,以使球体附着在其中一个顶点上。
void Start()
{
points = new ArrayList();
points.Add(point1);
points.Add(point2);
points.Add(point3);
points.Add(point4);
points.Add(point5);
points.Add(point6);
points.Add(point7);
points.Add(point8);
foreach (GameObject point in points)
{
point.transform.parent = myObject.transform;
}
if (transform.gameObject.GetComponent<MeshCollider>())
{
transform.gameObject.GetComponent<MeshCollider> ().convex = true;
}
}
void Update ()
{
Vector3[] Verticles = new Vector3[]
{
//6 Sides
//Back Side
point1.transform.position,point2.transform.position,point3.transform.position,point4.transform.position,
//Right Side
point2.transform.position,point6.transform.position,point4.transform.position,point8.transform.position,
//Front Side
point5.transform.position,point6.transform.position,point7.transform.position,point8.transform.position,
//Left Side
point5.transform.position,point1.transform.position,point7.transform.position,point3.transform.position,
//Up Side
point3.transform.position,point4.transform.position,point7.transform.position,point8.transform.position,
//Down Side
point1.transform.position,point2.transform.position,point5.transform.position,point6.transform.position,
};
int[] Triangles = new int[]
{
2,1,0,
1,2,3,
4,7,5,
7,4,6,
9,10,8,
10,9,11,
12,15,13,
15,12,14,
16,19,17,
19,16,18,
21,22,20,
22,21,23,
};
Vector2[] UV = new Vector2[]
{
new Vector2(0,0),
new Vector2(1,0),
new Vector2(0,1),
new Vector2(1,1),
new Vector2(0,0),
new Vector2(1,0),
new Vector2(0,1),
new Vector2(1,1),
new Vector2(0,0),
new Vector2(1,0),
new Vector2(0,1),
new Vector2(1,1),
new Vector2(0,0),
new Vector2(1,0),
new Vector2(0,1),
new Vector2(1,1),
new Vector2(0,0),
new Vector2(1,0),
new Vector2(0,1),
new Vector2(1,1),
new Vector2(0,0),
new Vector2(1,0),
new Vector2(0,1),
new Vector2(1,1),
};
Vector3[] Normals = new Vector3[]
{
Vector3.back,
Vector3.back,
Vector3.back,
Vector3.back,
Vector3.right,
Vector3.right,
Vector3.right,
Vector3.right,
Vector3.forward,
Vector3.forward,
Vector3.forward,
Vector3.forward,
Vector3.left,
Vector3.left,
Vector3.left,
Vector3.left,
Vector3.up,
Vector3.up,
Vector3.up,
Vector3.up,
Vector3.down,
Vector3.down,
Vector3.down,
Vector3.down,
};
transform.GetComponent<MeshFilter>().mesh.vertices = Verticles;
transform.GetComponent<MeshFilter>().mesh.triangles = Triangles;
}
然而,当我按下Unity中的PLAY按钮并将图像显示在我的网络摄像头前,以便Vuforia可以检测到它时,拖动其中一个球体不会使我的立方体变形,无论是在场景还是游戏模式。