Как определить 2D-изображения с помощью ARKit и RealityKit - PullRequest
1 голос
/ 16 марта 2020

Я хочу обнаружить 2D-изображения, используя ARKit и RealityKit . Я не хочу использовать SceneKit, потому что многие реализации основаны на RealityKit. Я не смог найти примеров обнаружения изображений на RealityKit. Я сослался https://developer.apple.com/documentation/arkit/detecting_images_in_an_ar_experience образец кода от Apple. Он использует 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 захват обнаруженных изображений. Каков эквивалентный делегат для ARSCNViewDelegate в RealityKit? Как обнаружить ARImageAnchor?

1 Ответ

1 голос
/ 16 марта 2020

В 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.

...