RealityKit – ARView with '.ar' mode + ARView with '. nonAR' mode

Viewed 94

I want to create an AR experience with a full screen video with AR elements, and in addition a small view at the bottom of the screen to show some relevant 3D objects. In the past, I used to implement such features with an ARSCNView for the AR part, and a SCNView (SceneKit view) for the little preview part.

Is there a way I can do that with RealityKit for those 2 views?

I understand that RealityKit needs an ARView to render, which means I would need 2 ARViews on my screen, with different cameraMode settings:

let arView = ARView(frame: self.view.frame,
                    cameraMode: .ar,
                    automaticallyConfigureSession: true)
view.addSubview(arView)
        
let newAnchor = AnchorEntity(world: [0, 0, -1])
let newBox = ModelEntity(mesh: .generateBox(size: 0.5))
newAnchor.addChild(newBox)
arView.scene.anchors.append(newAnchor)
        
        
        
let arView2 = ARView(frame: CGRect(x:50,y:300,width: 200,height:200),
                     cameraMode: .nonAR,
                     automaticallyConfigureSession: true)
view.addSubview(arView2)
        
let newAnchor2 = AnchorEntity(world: [0, 0,-1])
let newBox2 = ModelEntity(mesh: .generateBox(size: 0.5))
newAnchor2.addChild(newBox2)
arView2.scene.anchors.append(newAnchor2)

arView shows the video feed with the newBox as expected.

My problem is that arView2 shows the camera feed although I expected a black view with only the newBox2. In addition this camera feed is distorted (it seems to be the contents of arView resized to fit arView2)

What am I missing?

1 Answers

Unlike ARKit, in RealityKit 2.0 you cannot simultaneously run two ARSessions (I mean a face config as primary setting, and a world config as a secondary setting), just a single one. Therefore, the running session of the first ARView must be used by both views.

arView2.session = arView.session

arView2.environment.background = .color(.systemIndigo)
Related