How To Create Dual Screen of AR using RealityView and SwiftUI iOS 18

I have this code to make ARVR Stereo View To Be Used in VR Box Or Google Cardboard, it uses iOS 18 New RealityView but it is not Act as an AR but rather Static VR on a Camera background so as I move the iPhone the cube move with it and that's not suppose to happen if its Anchored in a plane or to world coordinate.

import SwiftUI
import RealityKit


struct ContentView : View {
    let anchor1 = AnchorEntity(.camera)
    let anchor2 = AnchorEntity(.camera)
    
    var body: some View {
        HStack (spacing: 0){
            MainView(anchor: anchor1)
            MainView(anchor: anchor2)
        }
        .background(.black)
    }
}

struct MainView : View {
    @State var anchor = AnchorEntity()
    
    var body: some View {
        RealityView { content in
            content.camera = .spatialTracking
            
            let item = ModelEntity(mesh: .generateBox(size: 0.25), materials: [SimpleMaterial()])
            anchor.addChild(item)
            content.add(anchor)

            anchor.position.z = -1.0
            anchor.orientation = .init(angle: .pi/4, axis:[0,1,1])
        } 
    }
}

the thing is if I remove .camera like this

    let anchor1 = AnchorEntity()
    let anchor2 = AnchorEntity()

It would work as AR Anchored to world coordinates but on the other hand is does not work but on the left view only not both views

Meanwhile this was so easy before RealityView and SwiftUI by cloning the view like in ARSCNView Example :

import UIKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate, ARSessionDelegate {
    //create Any Two ARSCNView's in Story board
    // and link each to the next (dont mind dimensions)
    @IBOutlet var sceneView: ARSCNView!
    @IBOutlet var sceneView2: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        sceneView.delegate = self
        sceneView.session.delegate = self
        
        // Create SceneKit box
        let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0.01)
        let item = SCNNode(geometry: box)
        item.geometry?.materials.first?.diffuse.contents = UIColor.green
        item.position = SCNVector3(0.0, 0.0, -1.0)
        item.orientation = SCNVector4(0, 1, 1, .pi/4.0)

        // retrieve the ship node
        sceneView.scene.rootNode.addChildNode(item)

    }

    override func viewDidLayoutSubviews() // To Do Add the 4 Buttons
    {
        // Stop Screen Dimming or Closing While The App Is Running
        UIApplication.shared.isIdleTimerDisabled = true
        
        let screen: CGRect = UIScreen.main.bounds
        
        let topPadding: CGFloat = self.view.safeAreaInsets.top
        let bottomPadding: CGFloat = self.view.safeAreaInsets.bottom
        let leftPadding: CGFloat = self.view.safeAreaInsets.left
        let rightPadding: CGFloat = self.view.safeAreaInsets.right
        
        let safeArea: CGRect = CGRect(x: leftPadding, y: topPadding, width: screen.size.width - leftPadding - rightPadding, height: screen.size.height - topPadding - bottomPadding)
        
        DispatchQueue.main.async
        {
            if self.sceneView != nil
            {
                self.sceneView.frame = CGRect(x: safeArea.size.width * 0 + safeArea.origin.x, y: safeArea.size.height * 0 + safeArea.origin.y, width: safeArea.size.width * 0.5, height: safeArea.size.height * 1)
            }

            if self.sceneView2 != nil
            {
                self.sceneView2.frame = CGRect(x: safeArea.size.width * 0.5 + safeArea.origin.x, y: safeArea.size.height * 0 + safeArea.origin.y, width: safeArea.size.width * 0.5, height: safeArea.size.height * 1)
            }
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let configuration = ARWorldTrackingConfiguration()
        
        sceneView.session.run(configuration)
        sceneView2.scene = sceneView.scene
        sceneView2.session = sceneView.session
    }
}

And here is the video for it

Answered by DTS Engineer in 813132022

Hello @ostoura,

Please file a bug report for this issue using Feedback Assistant.

Best regards,

Greg

Accepted Answer

Hello @ostoura,

Please file a bug report for this issue using Feedback Assistant.

Best regards,

Greg

How To Create Dual Screen of AR using RealityView and SwiftUI iOS 18
 
 
Q