swiftswiftuivirtual-realityrealitykitrealityview

Stereo view using RealityView in iOS 18


Im trying to make a VR Stereo 3d view using RealityView And SwiftUI in iOS 18

Issue: Both sides are not connected together meaning when the 3d item on the left view moves I do not see the one on the right moves same way or moves at all

Expected: Both Sids Should Move together in Sync with a single user move

here is my code so far,

import SwiftUI
import RealityKit

struct ContentView: View {
  @State var box = AnchorEntity()
  @State var box2 = AnchorEntity()

  @State private var rotateTime: Timer!

  @State var rotateCounter: Float = 0.0

  var body: some View {
    
    HStack {
        RealityView{ content in
            
            let item = ModelEntity(mesh: .generateBox(size: .init(0.5, 0.5, 0.5)), materials: [SimpleMaterial(color: .white, isMetallic: false)])
            box.addChild(item)
            // rotate box on x and y 45 degrees
            box.transform.rotation = simd_quatf(angle: .pi / 4, axis:[0,1,1])

            content.add(box)
        }

        RealityView{ content in
            content.add(box.clone(recursive: true))
        }

    }
    .background(.black)
    .onAppear(){
        
        // After 5 Sec the animation starts
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5.0)
        {
            //-- Run Rotation Loop ----
            rotateTime?.invalidate() // to make sure no instance for it before
            
            rotateTime = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: { _ in
                rotateMe()
            })
        }
    }
}

func rotateMe(){
    
    rotateCounter += 1
    
    if rotateCounter > 360 {
        rotateCounter = 0.0
    }

    box.transform.rotation = simd_quatf(angle: (rotateCounter * (.pi / 180)), axis: [0,1,0])
  }
}

When the code starts it will wait for 5 sec then start to try animation for the left view box enter image description here

enter image description here


Solution

  • Everything works fine if each anchor entity is unique.

    Also, iOS 18/iPadOS 18 Simulator doesn't work properly.

    Use a device!

    import SwiftUI
    import RealityKit
    
    struct ContentView : View {
        @State var anchor1 = AnchorEntity()
        @State var anchor2 = AnchorEntity()
        @State var rotateTime: Timer!
        @State var rotateCounter: Float = 0.0
    
        var body: some View {
            HStack {
                RealityView { rvc in
                    let item = ModelEntity(mesh: .generateBox(size: 0.5), 
                                      materials: [SimpleMaterial()])
                    anchor1.addChild(item)
                    anchor1.orientation = .init(angle: .pi/4, axis:[0,1,1])
                    rvc.add(anchor1)
                }
                RealityView { rvc in
                    let item = ModelEntity(mesh: .generateBox(size: 0.5), 
                                      materials: [SimpleMaterial()])
                    anchor2.addChild(item)
                    anchor2.orientation = .init(angle: .pi/4, axis:[0,1,1])
                    rvc.add(anchor2)
                }
            }
            .onAppear {
                DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                    rotateTime?.invalidate()
                    rotateTime = Timer.scheduledTimer(
                        withTimeInterval: 0.01, repeats: true, block: { _ in
                            rotate(anchor1)
                            rotate(anchor2)
                        }
                    )
                }
            }
            .background(.black)
        }
        func rotate(_ anchor: AnchorEntity) {
            rotateCounter += 1
            
            if rotateCounter > 360 { rotateCounter = 0.0 }
            
            anchor.orientation = .init(angle: (rotateCounter * (.pi/180)), axis: [0,1,0])
        }
    }