gpt4 book ai didi

swift - 在哪些情况下 ARSCNView.raycastQuery 返回 nil?

转载 作者:行者123 更新时间:2023-12-03 19:18:06 24 4
gpt4 key购买 nike

在我的渲染器委托(delegate)中,我从 View 中心创建了一个光线转换查询,以跟踪估计的平面并显示一个跟随光线转换结果的 3D 指针。

通过 view.raycastQuery(from:allowing:alignment:) 完成但返回零。

我的问题是为什么?没有文档说明为什么这个函数会返回一个 nil 值。我知道光线转换结果可能是空的,但为什么不会创建查询?

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
guard view.session.currentFrame?.camera.trackingState == .normal else {
return
}
DispatchQueue.main.async {
let center = view.center
DispatchQueue.global(qos: .userInitiated).async {
if let query = view.raycastQuery(from: center, allowing: .estimatedPlane, alignment: .any) {
let results = view.session.raycast(query)
...
}
else {
// sometimes it gets here
}
}
}
}

最佳答案

可退货ARRaycastQuery?最初是可选的
根据苹果 documentation :

raycastQuery(from:allowing:alignment:) instance method creates a ray that extends in the positive z-direction from the argument screen space point, to determine if any of the argument targets exist in the physical environment anywhere along the ray. If so, ARKit returns a 3D position where the ray intersects the target.


如果射线不与目标相交,则没有 ARRaycastQuery?目的。所以有nil .
func raycastQuery(from point: CGPoint, 
allowing target: ARRaycastQuery.Target,
alignment: ARRaycastQuery.TargetAlignment) -> ARRaycastQuery?
返回 nil 的两个可能原因是什么? ?
  • 没有检测到射线击中的平面
  • 代码逻辑错误

  • 解决方案
    您的代码可能如下所示:
    @IBOutlet var sceneView: ARSCNView!

    @IBAction func onTap(_ sender: UIGestureRecognizer) {

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

    let query: ARRaycastQuery? = sceneView.raycastQuery(from: tapLocation,
    allowing: estimatedPlane,
    alignment: alignment)

    if let nonOptQuery: ARRaycastQuery = query {

    let result: [ARRaycastResult] = sceneView.session.raycast(nonOptQuery)

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

    self.loadGeometry(rayCast)
    }
    }
    这是获取模型的方法:
    func loadGeometry(_ result: ARRaycastResult) {

    let scene = SCNScene(named: "art.scnassets/myScene.scn")!

    let node: SCNNode? = scene.rootNode.childNode(withName: "model",
    recursively: true)

    node?.position = SCNVector3(result.worldTransform.columns.3.x,
    result.worldTransform.columns.3.y,
    result.worldTransform.columns.3.z)

    self.sceneView.scene.rootNode.addChildNode(node!)
    }

    关于swift - 在哪些情况下 ARSCNView.raycastQuery 返回 nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60073349/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com