如何使用 ARKit 和 RealityKit 检测二维图像

How to detect the 2D images using ARKit and RealityKit

我想使用 ARKitRealityKit 检测二维图像。我不想使用 SceneKit,因为很多实现都是基于 RealityKit 的。我在 RealityKit 上找不到任何检测图像的示例。我参考了苹果的 https://developer.apple.com/documentation/arkit/detecting_images_in_an_ar_experience 示例代码。它使用 Scenekit 和 ARSCNViewDelegate

let arConfiguration = ARWorldTrackingConfiguration()
arConfiguration.planeDetection = [.vertical, .horizontal]
arConfiguration.isLightEstimationEnabled = true
arConfiguration.environmentTexturing = .automatic

if let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "sanitzer", bundle: nil) {
    arConfiguration.maximumNumberOfTrackedImages = 1
    arConfiguration.detectionImages = referenceImages
}
self.session.run(arConfiguration, options: [.resetTracking, .removeExistingAnchors])

我已经实施 ARSessionDelegate 但无法检测图像?

func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
    //how to capture image anchor?
}   
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
    //how to capture image anchor?
}

Apple 已实现 ARSCNViewDelegate 捕获检测到的图像。 RealityKit 中 ARSCNViewDelegate 的等效委托是什么?如何检测ARImageAnchor?

ARKit/RealityKit 项目中,对 session() 实例方法使用以下代码:

import ARKit
import RealityKit


class ViewController: UIViewController, ARSessionDelegate {

    func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {

        guard let imageAnchor = anchors.first as? ARImageAnchor,
              let _ = imageAnchor.referenceImage.name
        else { return }

        let anchor = AnchorEntity(anchor: imageAnchor)

        // Add Model Entity to anchor
        anchor.addChild(model)

        arView.scene.anchors.append(anchor)
    }

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

        arView.session.delegate = self
        resetTrackingConfig()
    }

    func resetTrackingConfig() {

        guard let refImg = ARReferenceImage.referenceImages(inGroupNamed: "Sub",
                                                                  bundle: nil)
        else { return }

        let config = ARWorldTrackingConfiguration()
        config.detectionImages = refImg
        config.maximumNumberOfTrackedImages = 1

        let options = [ARSession.RunOptions.removeExistingAnchors,
                       ARSession.RunOptions.resetTracking]

        arView.session.run(config, options: ARSession.RunOptions(options))
    }
}

并考虑到 - 参考图像文件夹(.png.jpg 格式)必须具有扩展名 .arresourcegroup.