Can RealityKit and and SceneKit be used together?

Viewed 1768

I wanted to use the object detection in RealityKit's ARView and can only find documentation to implement it in SceneKit's ARSCNView.

Is there a way around this?

1 Answers

ARKit's ARSCNView class is an offspring of SceneKit's SCNView class, so you do not even need to import SceneKit if you've already imported ARKit module. Although you can easily use ARKit and RealityKit together.

For RealityKit's object detection use the following code:

import ARKit
import RealityKit

extension ViewController: ARSessionDelegate {

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

        guard let objectAnchor = anchors.first as? ARObjectAnchor,
              let _ = objectAnchor.referenceObject.name
        else { return }

        let anchor = AnchorEntity(anchor: objectAnchor)   
        anchor.addChild(model)    
        arView.scene.anchors.append(anchor)
    }
}

And place a corresponding content into .arresourcegroup folder!

class ViewController: UIViewController {

    @IBOutlet var arView: ARView! 

    override func viewDidLoad() {
        super.viewDidLoad()    
        arView.session.delegate = self

        guard let obj = ARReferenceObject.referenceObjects(inGroupNamed: "Objs", 
                                                                 bundle: nil) 
        else { return }

        let config = ARWorldTrackingConfiguration()
        config.detectionObjects = obj
        arView.session.run(config)
    }
}
Related