【问题标题】:Detect planes in ARKit在 ARKit 中检测平面
【发布时间】:2018-05-16 07:54:51
【问题描述】:

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

我目前正在做这样的事情,但它没有触发/它不相关:

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

【问题讨论】:

  • 如果它解决了您的问题,请接受其中一个答案作为正确答案。这样,检查此问题的其他人可以确定该解决方案有效。如果答案不起作用,如果您能告诉我们,那就太好了。发送。
  • @Manganese 我仍然需要对它们进行测试。同时感谢您的帮助
  • 当然,希望它有效。

标签: ios swift arkit


【解决方案1】:

请先添加锚以更新这些。您需要使用以下功能:

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,有几种方法可以做到。

    1st:你可以使用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)
    

    其中:

    告诉代理一个 SceneKit 节点对应一个新的 AR 锚点已添加到场景中。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 2017-11-09
      • 2019-09-11
      • 1970-01-01
      • 2020-10-28
      • 2018-03-20
      相关资源
      最近更新 更多