Home > Enterprise >  How to use setWorldOrigin with ARView?
How to use setWorldOrigin with ARView?

Time:03-10

In the RealityKit code below I expect the box to be positioned lower given the world translation I've applied to y. I think I'm misunderstanding what setWorldOrigin does. I want to redefine the coordinate mapping so that zero is in a different location. What am I doing/expecting incorrectly? Thanks.

let arView = ARView(frame: .zero, cameraMode: .nonAR)
arView.environment.background = .color(.white)

var relativeTransform = matrix_identity_float4x4
relativeTransform.columns.3.y = -1
arView.session.setWorldOrigin(relativeTransform: relativeTransform)
        
let material = SimpleMaterial(color: .gray, isMetallic: false)
let entity = ModelEntity(mesh: .generateBox(size: 0.3), materials: [material])
        
let anchor = AnchorEntity(world: .zero)
anchor.addChild(entity)
arView.scene.addAnchor(anchor)

CodePudding user response:

ARSession is an augmented reality object, so all session's properties and methods are working only when session is running. ARSession is meaningless in .nonAR mode, VR mode, and in the Xcode Simulator. Use your code in AR mode only.


Notwithstanding, a solution for VR scenario may look like this:

import UIKit
import RealityKit

class Camera: Entity, HasPerspectiveCamera, HasAnchoring {
    required init() {
        super.init()
   
        self.camera = PerspectiveCameraComponent(near: 0.01, far: 200.00, 
                                 fieldOfViewInDegrees: 50.0)
        self.transform.translation.y = 0.5
        self.transform.translation.z = 2.0
    }
}

class ViewController: UIViewController {
        
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        arView.environment.background = .color(.black)
        let camera = Camera()
        arView.scene.addAnchor(camera)

        let entity = ModelEntity(mesh: .generateBox(size: 0.1))      
        let anchor = AnchorEntity(world: .zero)
        anchor.addChild(entity)
        arView.scene.addAnchor(anchor)
    }
}
  • Related