在 ARKIT 中获取聚焦相机的所有 ARAnchors

Get All ARAnchors of focused Camera in ARKIT

When application launched first a vertical surface is detected on one wall than camera focus to the second wall, in the second wall another surface is detected. The first wall is now no more visible to the ARCamera but this code is providing me the anchors of the first wall. but I need anchors of the second wall which is right now Visible/focused in camera.

if let anchor = sceneView.session.currentFrame?.anchors.first {
    let node = sceneView.node(for: anchor)
    addNode(position: SCNVector3Zero, anchorNode: node)
} else {
    debugPrint("anchor node is nil")
}

答案的线索在您 if let 语句的开头。

让我们分解一下:

当您说 let anchor = sceneView.session.currentFrame?.anchors.first 时,您引用的是 ARAnchor 的可选数组,它自然可以包含多个元素。

因为你总是打电话给 first 例如索引 [0],您将始终获得添加到数组中的第一个 ARAnchor

既然你现在有 2 个锚点,你自然需要最后一个(最新的)元素。因此,您可以先试试这个:

if let anchor = sceneView.session.currentFrame?.anchors.last {

    let node = sceneView.node(for: anchor)
    addNode(position: SCNVector3Zero, anchorNode: node)

     } else {

        debugPrint("anchor node is nil")
}

更新: 由于另一位发帖者对这个问题的解释不同,因为他们认为问题是 我如何检测 ARPlaneAnchor 是否在视野中? 让我们换一种方式来处理它。

首先我们需要考虑到 ARCamera 有一个显示我们内容的讲台:

因此,我们需要确定 ARPlaneAnchor 是否为 inViewOfFrostrum

首先我们将创建 2 个变量:

var planesDetected = [ARPlaneAnchor: SCNNode]()
var planeID: Int = 0

第一个存储ARPlaneAnchor及其关联的SCNNode,第二个是为了为每个平面提供唯一的ID。

ARSCNViewDelegate 中,我们可以可视化一个 ARPlaneAnchor 然后存储它的信息,例如:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

    //1. Get The Current ARPlaneAnchor
    guard let anchor = anchor as? ARPlaneAnchor else { return }

    //2. Create An SCNode & Geometry To Visualize The Plane
    let planeNode = SCNNode()
    let planeGeometry = SCNPlane(width: CGFloat(anchor.extent.x), height: CGFloat(anchor.extent.z))
    planeGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
    planeNode.geometry = planeGeometry

    //3. Set The Position Based On The Anchors Extent & Rotate It
    planeNode.position = SCNVector3(anchor.center.x, anchor.center.y, anchor.center.z)
    planeNode.eulerAngles.x = -.pi / 2

    //4. Add The PlaneNode To The Node & Give It  A Unique ID
    node.addChildNode(planeNode)
    planeNode.name = String(planeID)

    //5. Store The Anchor & Node
    planesDetected[anchor] = planeNode

    //6. Increment The Plane ID
    planeID += 1

}

现在我们已经存储了检测到的平面,然后我们当然需要确定是否有任何平面在 ARCamera 的视野中,例如:

/// Detects If An Object Is In View Of The Camera Frostrum
func detectPlaneInFrostrumOfCamera(){

    //1. Get The Current Point Of View
    if let currentPointOfView = augmentedRealityView.pointOfView{

        //2. Loop Through All The Detected Planes
        for anchorKey in planesDetected{

            let anchor = anchorKey.value

            if augmentedRealityView.isNode(anchor, insideFrustumOf: currentPointOfView){

               print("ARPlaneAnchor With ID \(anchor.name!) Is In View")

            }else{

                 print("ARPlaneAnchor With ID \(anchor.name!) Is Not In View")
            }

        }

    }
}

最后我们需要访问这个函数,我们可以在下面的 delegate 方法中完成,例如 renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval):

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

        detectPlaneInFrostrumOfCamera()
}

希望这两个都指向正确的方向...

为了获取当前在视点中的节点,您可以这样做:

       var targettedAnchorNode: SCNNode?
       if let anchors = sceneView.session.currentFrame?.anchors {
           for anchor in anchors {

               if let anchorNode = sceneView.node(for: anchor), let pointOfView = sceneView.pointOfView, sceneView.isNode(anchorNode, insideFrustumOf: pointOfView) {
                   targettedAnchorNode = anchorNode
                   break
               }
           }

           if let targettedAnchorNode = targettedAnchorNode {
               addNode(position: SCNVector3Zero, anchorNode: targettedAnchorNode)
           } else {
               debugPrint("Targetted node not found")
           }

       } else {
           debugPrint("Anchors not found")
       }

如果想获取所有被关注的节点,将它们收集到一个满足指定条件的数组中

祝你好运!