ARSceneView中将哪个图像用作参考?

时间:2019-05-02 21:12:34

标签: swift arkit

我目前正在制作一个使用图像跟踪的AR应用程序。 我希望它在参考图像文件夹中有2张图像(图像A:〜670x210,图像B:〜1123x794)。

如何检查相机检测到的图像? 例如,如果相机看到的图像A是我要制作的平面是物理对象的两倍,如果相机看到的图像B是我要制作的平面与物理对象相同的大小。

如果需要,这是我的代码段:


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Create a session configuration
        let configuration = ARImageTrackingConfiguration()

        if let trackedImage = ARReferenceImage.referenceImages(inGroupNamed: "ARpaperImage", bundle: Bundle.main) {

            configuration.trackingImages = trackedImage
            configuration.maximumNumberOfTrackedImages = 1

        }

        // Run the view's session
        sceneView.session.run(configuration)
    }


    // MARK: - ARSCNViewDelegate

    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {

        let node = SCNNode()

        if let imageAnchor = anchor as? ARImageAnchor {


            let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
            plane.firstMaterial?.diffuse.contents = UIColor(white: 1.0, alpha: 0,5)

            let planeNode = SCNNode(geometry: plane)

            planeNode.eulerAngles.x = -.pi / 2

            node.addChildNode(planeNode)

        }

        return node

    }


1 个答案:

答案 0 :(得分:0)

ARReferenceImage具有name属性,您可以使用该属性来确定已检测到哪个ReferenceImage,这很简单:

  

图片的描述性名称

这样,当您将ARReferenceImages放入资源束中时,您可以并且(可能已经拥有)为其命名,例如:

enter image description here

您可以在下面的委托回调中使用这些名称来满足您的要求。

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

因此,假设我们有两个ARReferenceImages,分别叫Brown和Choco。然后,使用这些名称,我们可以创建逻辑来显示不同的内容,例如:

  //-------------------------
  //MARK: - ARSCNViewDelegate
  //-------------------------

  extension ViewController: ARSCNViewDelegate{

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

      //1. Check We Have Detected An ARImageAnchor
      guard let validAnchor = anchor as? ARImageAnchor, let referenceImageName = validAnchor.referenceImage.name else { return }

      //2. Get The Physical Size Of The Reference Image (This Is Specificed When You Create Your Target In The ARResource Bundle)
      let physicalSizeOfReferenceImage = CGSize(width: validAnchor.referenceImage.physicalSize.width, height: validAnchor.referenceImage.physicalSize.height)

      ///3. Log The Data For Debugging Purposes
      print("""
        Detected ARAnchorID = \(validAnchor.identifier)
        Detected Reference Image Name = \(referenceImageName)
        Detected Reference Image Physical Size = (width) \(physicalSizeOfReferenceImage.width),
        Detected Reference Image Physical Size = (height) \(physicalSizeOfReferenceImage.height)
        """)


      //4. Perform Instanciation & Other Login Based On The Reference Image Detected
      if referenceImageName == "Brown"{

        node.addChildNode(planeNodeOfSize(CGSize(width: physicalSizeOfReferenceImage.width * 2, height: physicalSizeOfReferenceImage.height * 2)))

      }else if referenceImageName == "Choco"{

        node.addChildNode(planeNodeOfSize(CGSize(width: physicalSizeOfReferenceImage.width, height: physicalSizeOfReferenceImage.height)))

      }
    }

    /// Generates An SCNNode With An SCNPlaneGeometry Of A Specified Width & Height
    ///
    /// - Parameter size: CGSize
    /// - Returns: SCNNode
    func planeNodeOfSize(_ size: CGSize) -> SCNNode{

      //1. Create An SCNPlane Of Our Chosen Size
      let planeNode = SCNNode()
      let planeGeometry = SCNPlane(width: size.width, height: size.height )
      planeGeometry.firstMaterial?.diffuse.contents = UIColor.white
      planeNode.opacity = 0.25
      planeNode.geometry = planeGeometry

      //2. Rotate The PlaneNode To Horizontal
      planeNode.eulerAngles.x = -.pi/2

      //3. Return Our Node
      return planeNode
    }

  }

希望有帮助...