2017-06-17 53 views
0

我正在使用Unity製作AR應用程序。與真實相機Unity的ScreenToWorldPoint

使用「真實相機」,它使用特徵匹配來匹配圖片已經在數據庫中的特定對象。

特徵匹配後,我存儲的關鍵點在下面的實例中的一個值:

KeyPoint key_point; 

然後我想有使用key_point現實世界中的地位。

//let's assume that 
    //'x' is x-coordinate of key_point 
    //'y' is y-coordinate of key_point 

    Vector3 p = camera.ScreenToWorldPoint (Vector3 (x, y, camera.nearClipPlane)); 

我不知道是否有可能,因爲誰使用「camer.nearClipPlane」其他人在使用它的虛擬3D世界。

如果不可能,有沒有其他的方法可以在現實世界中改變2D位置到3D位置(我的意思是真實的相機世界)?

回答

1

我認爲你的解決方案有沒有考慮到攝像頭視角的問題。所以你的屏幕點將投影到近夾平面垂直。但是這隻對屏幕中心是正確的。

一個簡單的解決方案是使用射線對象:

Ray ray = Camera.main.ScreenPointToRay(x,y); 

https://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

根據文檔:

得到的射線是在世界空間中,開始對近平面攝像機並在屏幕上查看位置(x,y)像素座標(position.z被忽略)。「

所以你的觀點是:

Vector3 point = ray.origin;