如何使用ARKit和RealityKit检测2D图像 [英] How to detect the 2D images using ARKit and RealityKit

查看:311
本文介绍了如何使用ARKit和RealityKit检测2D图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ARKit RealityKit 检测2D图像.我不想使用SceneKit,因为许多基于RealityKit的实现.我找不到在RealityKit上检测图像的任何示例.我引用了 https://developer.apple.com/documentation/arkit/detecting_images_in_an_ar_experience 示例苹果提供的代码.它使用Scenekit和ARSCNViewDelegate

I want to detect the 2D images using ARKit and RealityKit. I don't want to use SceneKit because many implementations based on RealityKit. I couldn't find any examples detecting images on RealityKit. I referred https://developer.apple.com/documentation/arkit/detecting_images_in_an_ar_experience sample code from apple. It uses Scenekit and 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,但无法检测到图像吗?

I have implemented ARSessionDelegate but not able to detect image?

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?

Apple has implemented ARSCNViewDelegate capture the detected images. What is the equivalent delegate for ARSCNViewDelegate in RealityKit? How to detect ARImageAnchor?

推荐答案

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

In ARKit/RealityKit project use the following code for session() instance methods:

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.

And take into consideration – a folder for reference images (in .png or .jpg format) must have an extension .arresourcegroup.

这篇关于如何使用ARKit和RealityKit检测2D图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆