I created a SCNView in ViewController, and I have an ARFrame information, how do I set the SCNView camera node so that it is the same as the ARFrame camera ? I don't want to use ARSCNView.
I created a SCNView in ViewController, and I have an ARFrame information, how do I set the SCNView camera node so that it is the same as the ARFrame camera ? I don't want to use ARSCNView.
ARSession can be run as standalone entity (it must not necessarily be connected to a view). Use the following code to get what you expect:
import SceneKit
import ARKit
class ViewController: UIViewController {
@IBOutlet var sceneKitView: SCNView!
let session = ARSession()
var arCameraTransform: simd_float4x4?
let scnCamera = SCNNode()
override func viewDidLoad() {
super.viewDidLoad()
sceneKitView.scene = SCNScene()
sceneKitView.backgroundColor = .black
let cylinder = SCNCylinder(radius: 0.1, height: 1)
cylinder.firstMaterial?.diffuse.contents = UIColor.red
let node = SCNNode(geometry: cylinder)
node.position.z = -0.75
sceneKitView.scene?.rootNode.addChildNode(node)
scnCamera.camera = SCNCamera()
sceneKitView.scene?.rootNode.addChildNode(scnCamera)
self.session.delegate = self
self.session.run(ARWorldTrackingConfiguration())
}
}
...
extension ViewController: ARSessionDelegate {
func session(_ session: ARSession, didUpdate frame: ARFrame) {
self.arCameraTransform = frame.camera.transform
scnCamera.simdTransform = self.arCameraTransform!
print(sceneKitView.pointOfView?.simdTransform.columns.3 as Any)
}
}
P.S.
This works for .landscape orientation...