繁体   English   中英

Unity3D Vuforia相机在现实世界中的位置

[英]Unity3D Vuforia Camera Position in Real World Unit

我正在使用Vuforia和Unity3D开发应用程序。 为此,我需要在使用手机跟踪图像的同时从图像目标获取现实世界中的相机位置(x,y,z)。 我想知道是否有可能在Vuforia中获得这样的位置信息。 如果是,那么将非常感谢任何示例代码。

尝试这样的事情。

public class ExampleClass : MonoBehaviour {
public Transform target;
Camera camera;

void Start() {
    camera = GetComponent<Camera>();
}

void Update() {
    Vector3 screenPos = camera.WorldToScreenPoint(target.position);
    Debug.Log("target is " + screenPos.x + " pixels from the left");
}
}

这是将位置从世界空间转换为屏幕空间。

屏幕空间以像素为单位定义。 屏幕的左下角是(0,0); 右上角是(pixelWidth,pixelHeight)。 z位置以相机的世界单位为单位。

可能不是您想要的,但这只是一个起点。

编辑:这返回世界空间而不是局部空间,所以这应该正是您想要的

public class ExampleClass : MonoBehaviour {
public GameObject someObject;
public Vector3 thePosition;

void Start() {
    // Instantiate an object to the right of the current object
    thePosition = transform.TransformPoint(Vector3.right * 2);
    Instantiate(someObject, thePosition, someObject.transform.rotation);
}
}

请注意,返回的位置受比例尺的影响。 如果要处理方向向量,请使用Transform.TransformDirection。 您可以使用Transform.InverseTransformPoint从世界到本地空间执行相反的转换。

完成Vuforia摄像机的预制,以使顶部对象不会移动,该对象是保留Vuforia AR组件的对象。 子对象是持有“相机”组件的对象,随着Vuforia根据标记检测计算其位置,该子对象正在空间中移动。

因此,您所需要做的就是获取子对象及其位置。 我不确定,但我认为您可以接受

CameraDevice.Instance.transform.position;

如果不可用,则:

Camera cam = FindObjectOfType<Camera>();
Vector3 worldPos = cam.transform.position;

localPosition返回相对于父级的位置,position返回相对于世界原点的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM