我正在尝试使用 ARKit 将屏幕上的某个点取消投影到世界空间中 ARPlaneAnchor 上的坐标。基本上我想根据我的相机指向的位置在地平面上绘制一个标记。ARKit 提供了一些方法来执行此操作,但是它们附加到 ARCamera 类,不幸的是,在我的场景中,我只能访问相机的投影、视图和变换矩阵,而无法引用 ARFrame 或 ARCamera 对象本身。我写了一些与 gluUnproject() 类似的东西(据我所知)。
func screenToWorld(screenPoint: CGPoint, depth: Float, screenSize: CGSize, projectionMatrix: simd_float4x4, viewMatrix:simd_float4x4) -> float3{
let viewProj = viewMatrix * projectionMatrix
let inverse = simd_inverse(viewProj)
let x = (2.0 * screenPoint.x ) / screenSize.width - 1.0
let y = (2.0 * screenPoint.y) / screenSize.height - 1.0
let inPoint = float4(Float(x), Float(y), depth * 2.0 - 1.0, 1.0)
var position = inPoint * inverse
position.w = 1.0 / position.w
position.x …Run Code Online (Sandbox Code Playgroud)