如何 "convert" 一个 ARHitTestResult 到一个 ARRaycastResult?

How to "convert" an ARHitTestResult to an ARRaycastResult?

我目前正在使用 Swift 构建一个 AR 应用程序,并且想转换这种类型的东西:让 arHitTestResults : [ARHitTestResult] = sceneView.hitTest(screenCentre, types: [.featurePoint] )(在 iOS 14 中不再可用)为此:let arResults: [ARRaycastResult] = sceneView.raycastQuery(from: tapLocation, allowing: existingPln, alignment: alignment)

但系统地收到此消息:无法将类型 'ARRaycastQuery?' 的值转换为指定类型“[ARRaycastResult]”

我能做什么?

感谢您的回答!

@objc func handleTap(gestureRecognize: UITapGestureRecognizer) {

    let screenCentre: CGPoint = CGPoint(x: self.sceneView.bounds.midX, 
                                        y: self.sceneView.bounds.midY)
    let tapLocation: CGPoint = screenCentre
    let existingPln: ARRaycastQuery.Target = .existingPlaneGeometry
    let alignment: ARRaycastQuery.TargetAlignment = .vertical
    
    let arResults: [ARRaycastResult] = sceneView.raycastQuery(from: tapLocation, 
                                                          allowing: existingPln, 
                                                         alignment: alignment)
}

您无需将已弃用的 ARHitTestResult class 转换为 ARRaycastResult,因为这些 class 不可互换。在场景中使用光线投射时,请改用以下方法:

@IBAction func tapped(_ sender: UITapGestureRecognizer) {

    let tapLocation: CGPoint = sender.location(in: arView)
    let estimatedPlane: ARRaycastQuery.Target = .estimatedPlane
    let alignment: ARRaycastQuery.TargetAlignment = .any

    let result = arView.raycast(from: tapLocation,
                            allowing: estimatedPlane,
                           alignment: alignment)

    guard let raycast: ARRaycastResult = result.first
    else { return }

    let anchor = AnchorEntity(world: raycast.worldTransform)
    anchor.addChild(model)
    arView.scene.anchors.append(anchor)
 
    print(raycast.worldTransform.columns.3)
}

如果您需要更多光线投射示例,请查看