首页 文章

在 ARKit 中检测飞机

提问于
浏览
1

当应用程序在 ARKit 中检测到至少一架飞机时,如何获得通知?

我目前正在执行类似操作,但与 firing/it 无关:

extension MyViewController: ARSCNViewDelegate, ARSessionDelegate {
    internal func setupAR() {
        let scene = SCNScene()
        sceneView.scene = scene
        let configuration = ARWorldTrackingConfiguration()
        configuration.planeDetection = [.horizontal]
        sceneView.session.delegate = self
        sceneView.session.run(configuration)
    }

    public func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
        if !anchors.isEmpty {
            print("ANCHORS NOT EMPTY")
            planesDetectedState()
        } else {
            print("Anchors is empty")
        }
    }
}

2 回答

  • 3

    请先添加锚点,然后再更新那些锚点。您需要使用以下功能:

    func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
            guard let frame = session.currentFrame else { return }
            print("Just added an anchor and felt so good")
    }
    

    另外,除非您在锚点上添加一个节点,否则您将看不到任何平面,这可以通过以下方式完成:

    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
            // Add a planeNode and make it a child to the node automatically added.
    }
    

    我还建议您下载入门代码,该代码对于您正在考虑的事情很容易遵循。

    https://developer.apple.com/documentation/arkit/building_your_first_ar_experience

  • 2

    如果要检测ARAnchors,可以使用多种方法。

    **第一:**您可以使用ARSessionDelegate回调:

    public func session(_ session: ARSession, didAdd anchors: [ARAnchor]) { }
    

    哪一个:

    告诉代表该会话已添加一个或多个锚。

    因此,使用此回调的示例如下:

    extension UIViewController: ARSessionDelegate{
    
        public func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
    
            //1. If We Have At Least One ARAnchor Detected The Log The Information
            if !anchors.isEmpty {
    
                anchors.forEach { (anchor) in
    
                    print("""
                          The Type Of Anchor = \(anchor.classForCoder)
                          The Anchor Identifier = \(anchor.identifier)
                          The Anchor Translation = X: \(anchor.transform.columns.3.x), Y: \(anchor.transform.columns.3.y), Z: \(anchor.transform.columns.3.z)
                          """)
                }
            }
    
        }
    }
    

    **第二:**您可以使用ARSCNViewDelegate回调:

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

    哪一个:

    告诉委托人已将与新的 AR 锚点对应的 SceneKit 节点添加到场景中。

    因此,使用此回调的示例如下:

    extension ViewController: ARSCNViewDelegate{
    
        func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    
            //1. Check An ARPlane Anchor Has Been Detected
            guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
    
            //2. Get The Width & Height Of The Plane
            let width = CGFloat(planeAnchor.extent.x)
            let height = CGFloat(planeAnchor.extent.z)
    
            //3. Create An SCNPlane So We Can Visualize The Plane Detected
            let plane = SCNPlane(width: width, height: height)
    
            //4. Set It's Colour
            plane.materials.first?.diffuse.contents = UIColor.cyan
    
            //5. Create An SCNNode To Hold Our Plane
            let planeNode = SCNNode(geometry: plane)
    
            //6. Position & Rotate It
            let x = CGFloat(planeAnchor.center.x)
            let y = CGFloat(planeAnchor.center.y)
            let z = CGFloat(planeAnchor.center.z)
            planeNode.position = SCNVector3(x,y,z)
            planeNode.eulerAngles.x = -.pi / 2
    
            //7. Add It To The Node For Anchor
            node.addChildNode(planeNode)
    
        }
    
    }
    

相关问题