如何从 ARFrame 相机设置 SCNView 相机?

How to set SCNView camera from ARFrame camera?

我在ViewController中创建了一个SCNView,并且有一个ARFrame信息,请问如何设置SCNView摄像头节点使其与ARFrame摄像头相同?我不想使用ARSCNView .

ARSession 可以 运行 作为独立的实体(不一定要连接到视图)。使用以下代码获得您期望的结果:

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.

这适用于 .landscape 方向...