iosarkitscnscene

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")
}

Solution

  • In order to get the node that is currently is in point of view you can do something like this:

           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")
           }
    

    If you would like to get all focused nodes, collect them in an array satisfying specified condition

    Good luck!