2

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)
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Spiff
  • 783
  • 8
  • 9

1 Answers1

3

AR solution

Read this post for details.


Non-AR solution

ARSession is an AR object, so all session's properties and methods are working only when session is running. ARSession is meaningless in .nonAR mode (i.e. VR mode), on macOS app, and in the Xcode Simulator. In your case, I suggest using the following scenario.

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)
    }
}

P. S.

In RealityKit 2.0 using .ar mode, you can't change far clipping plane value.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    Thanks, Andy. Is there an equivalent way to set world transform when in .nonAR mode? – Spiff Mar 09 '22 at 18:24
  • When working in VR, there is no scenario in which you need to move the coordinate grid. What for? `setWorldOrigin` is required in AR mode only, as the AR world origin, by default, is built at the same location where the ARCamera is. – Andy Jazz Mar 09 '22 at 18:55
  • 1
    I'm placing models within an empty space and redefining the origin down a few ticks means I don't have to keep adding an offset to coordinates. I understand that it is a small thing but if there was an easy way to move the origin why not do it? – Spiff Mar 09 '22 at 20:22
  • Move cameras, move anchors, move models. What is the point of moving the coordinate grid in VR? In AR, this is necessary, in VR it's not necessary. – Andy Jazz Mar 09 '22 at 20:59