I am trying to load a 3d model "cube.usdz" onto the world position of the camera.
I've already looked at this solution: https://stackoverflow.com/a/45089244/13246089, but I have no idea how to implement it and where do I even call the function "session"? Sorry I am very new to this and don't know what I am doing.
Why does my code always go to "why does nothing work"? Why can't I get the position of my camera? Any assistance or suggestions would be greatly appreciated.
import UIKit
import RealityKit
import ARKit
class Load3DModelViewController: UIViewController, ARSessionDelegate {
@IBOutlet weak var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
// set up AR configuration
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal
arView.session.run(configuration)
load3DModel()
arView.session.delegate = self
}
func load3DModel() {
let modelFileName = "cube.usdz"
guard let modelEntity = try? Entity.load(named: modelFileName) else {
print("Failed to load the 3D model")
return
}
if let pos = arView.session.currentFrame?.camera.transform {
let xpos = pos.columns.3.x
let ypos = pos.columns.3.y
let zpos = pos.columns.3.z
let modelTranslation = SIMD3<Float>(xpos,ypos,zpos - 1)
modelEntity.setPosition(modelTranslation, relativeTo: nil)
arView.scene.addAnchor(modelEntity as! HasAnchoring)
} else {
print("\nwhy does nothing work\n")
}
}
}