【问题标题】:ARSCNView unprojectPointARSCN查看取消项目点
【发布时间】:2018-09-09 10:59:17
【问题描述】:

我需要将我的ARSCNView 的 2d 坐标空间中的一个点转换为 3d 空间中的一个坐标。基本上是一条从视点到触摸位置的光线(直到设定的距离)。

我想为此使用arView.unprojectPoint(vec2d),但返回的点似乎总是位于视图的中心

vec2d 是从这样的二维坐标创建的SCNVector3

SCNVector3(x, y, 0) // 0 specifies camera near plane

我做错了什么?如何获得想要的结果?

【问题讨论】:

标签: swift scenekit augmented-reality arkit


【解决方案1】:

我认为您至少有两种可能的解决方案:

首先

使用hitTest(_:types:)实例方法:

此方法在与 SceneKit 视图中的点对应的捕获的相机图像中搜索真实世界的对象或 AR 锚点。

let sceneView = ARSCNView()

func calculateVector(point: CGPoint) -> SCNVector3? {

    let hitTestResults = sceneView.hitTest(point,
                                           types: [.existingPlane])

    if let result = hitTestResults.first {

        return SCNVector3.init(SIMD3(result.worldTransform.columns.3.x,
                                     result.worldTransform.columns.3.y,
                                     result.worldTransform.columns.3.z))
    }
    return nil
}

calculateVector(point: yourPoint)

第二

使用unprojectPoint(_:ontoPlane:)实例方法:

此方法返回点从 2D 视图到 ARKit 检测到的 3D 世界空间中的平面上的投影。

@nonobjc func unprojectPoint(_ point: CGPoint, 
            ontoPlane planeTransform: simd_float4x4) -> simd_float3?

或:

let point = CGPoint()
var planeTransform = simd_float4x4()

sceneView.unprojectPoint(point,
                     ontoPlane: planeTransform)

【讨论】:

    【解决方案2】:

    在相机前面'x'厘米偏移处添加一个空节点,并使其成为相机的子节点。

        //Add a node in front of camera just after creating scene
        hitNode = SCNNode()
        hitNode!.position = SCNVector3Make(0, 0, -0.25)  //25 cm offset
        sceneView.pointOfView?.addChildNode(hitNode!)
    
    
    func unprojectedPosition(touch: CGPoint) -> SCNVector3 {
        guard let hitNode = self.hitNode else {
            return SCNVector3Zero
        }
    
        let projectedOrigin = sceneView.projectPoint(hitNode.worldPosition)
        let offset = sceneView.unprojectPoint(SCNVector3Make(Float(touch.x), Float(touch.y), projectedOrigin.z))
    
        return offset
    }
    

    See the Justaline GitHub implementation of the code here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多