由于项目需求,自己做了一个小demo,把识别图上的虚拟按钮映射到屏幕上。按钮的位置和虚拟按钮保持一致,大小根据相机距离进行调节。
void GetVertex(Transform[] Vbtns,Button[] Ubtns) { for (int i = 0; i < Vbtns.Length; i++) { Vector3[] vertexs = new Vector3[4]; RectTransform UIBtnRect = Ubtns[i].GetComponent<RectTransform>(); //根据虚拟按钮的缩放信息,求出宽高 float vbLength = Vbtns[i].localScale.x; float vbWidth = Vbtns[i].localScale.z; //根据宽高和位置信息求出四个顶点的坐标 vertexs[0] = new Vector3(Vbtns[i].position.x - (vbLength / 2), Vbtns[i].position.y, Vbtns[i].position.z + (vbWidth / 2)); vertexs[1] = new Vector3(Vbtns[i].position.x - (vbLength / 2), Vbtns[i].position.y, Vbtns[i].position.z - (vbWidth / 2)); vertexs[2] = new Vector3(Vbtns[i].position.x + (vbLength / 2), Vbtns[i].position.y, Vbtns[i].position.z - (vbWidth / 2)); vertexs[3] = new Vector3(Vbtns[i].position.x + (vbLength / 2), Vbtns[i].position.y, Vbtns[i].position.z + (vbWidth / 2)); //转化为屏幕坐标以后,求出屏幕UI的宽和高 ,加上位置信息来确定 float UIleghth = arCamera.WorldToScreenPoint(vertexs[0]).x - arCamera.WorldToScreenPoint(vertexs[3]).x; float UIwidth = arCamera.WorldToScreenPoint(vertexs[0]).y - arCamera.WorldToScreenPoint(vertexs[1]).y; UIBtnRect.position = arCamera.WorldToScreenPoint(Vbtns[i].position); UIBtnRect.rotation = Quaternion.Euler(new Vector3(Vbtns[i].eulerAngles.x, -Vbtns[i].eulerAngles.z, -Vbtns[i].eulerAngles.y)); UIBtnRect.sizeDelta = new Vector2(Mathf.Abs(UIleghth), Mathf.Abs(UIwidth)); } }
