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?
You can use ARKit and RealityKit together, but you cannot use RealityKit with SceneKit due to the fact that they are totally different. However, object detection can be easily implemented in RealityKit's ARView.
For that, 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)
}
}
Then 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)
}
}