OStack程序员社区-中国程序员成长平台

标题: ios - 在 ARKIT 中获取所有聚焦相机的 ARAnchors [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:55
标题: ios - 在 ARKIT 中获取所有聚焦相机的 ARAnchors

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



Best Answer-推荐答案


答案的线索在你的 if let 语句的开始行。

让我们分解一下:

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

因为你总是调用 first 例如index [0],您将始终获得添加到数组中的第一个 ARAnchor

由于您现在有 2 个 anchor ,因此您自然需要最后一个(最新的)元素。因此,您可以尝试以下方法:

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 有一个 Frostrum 用于显示我们的内容:

enter image description here

因此,我们需要确定 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()
}

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

关于ios - 在 ARKIT 中获取所有聚焦相机的 ARAnchors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49363187/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4