Reputation: 21
I am trying to use that the ARKit imageDetection functionality in a swiftUI project and have troubles to implement the renderer. This is what happened so far: In Xcode 11.2 one can start a new ARKit project using swiftUI. The UIViewRepresentable protocol is used in the ARViewContainer struct that returns an ARView. An ARView object/var is created inside that struct and this "arView" apparently does have a "session" vobject. I think I could set up this (AR)-session object like it used to work with SceneKit:
struct ScanARViewContainer: UIViewRepresentable {
makeUIView(context: Context) -> ARView {
//let arView = MyARView(frame: .zero)
// changed this line to the following to have an own renderer
let arView = ARView(frame: .zero, cameraMode: ARView.CameraMode.ar, automaticallyConfigureSession: false)
guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else {
fatalError("Missing expected asset catalog resources.")
}
let configuration = ARWorldTrackingConfiguration()
configuration.detectionImages = referenceImages
arView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
return arView
}
The code is compiled with no complains and when sending this to the phone the AR session is started and seems to do something. The next step would be to change the renderer to show detected images. In SceneKit one needed to make use of the ARSCNViewDelegate (Image detection results). This is where I got stuck. I tried to create an own myARView class first to get access to the ARSessionDelegate hoping for being able to access didadd anchor functions.
class MyARView : ARView, ARSessionDelegate {
required init(frame: CGRect) {
super.init(frame: frame, cameraMode: ARView.CameraMode.ar, automaticallyConfigureSession: false)
self.session.delegate = self
}
}
Then I wanted to uses this "new" class in the ARViewContainer struct:
let arView = MyARView(frame: .zero, cameraMode: ARView.CameraMode.ar, automaticallyConfigureSession: false)
//old : let arView = ARView(frame: .zero, cameraMode: ARView.CameraMode.ar, automaticallyConfigureSession: false)
But the compiler complains about "Type 'ARViewContainer' does not conform to protocol 'UIViewRepresentable'. Or I get this complain "Type of expression is ambiguous without more context" when declaring the
let arView = MyARView(...'
Does anybody know how to do this correctly?
Upvotes: 1
Views: 966
Reputation: 21
I think I found good inspiration here about how to "catch" the delegate callbacks. ARKit & Reality composer - how to Anchor scene using image coordinates
Thanks to Mark D
Upvotes: 1