ARKit、RealityKit 使用ARSessionDelegate代理协议 获取得到相机的位置

ARKit、RealityKit 使用ARSessionDelegate代理协议 获取得到相机的位置,第1张

swiftUI中,ARKit、RealityKit
使用ARSessionDelegate代理协议
获取得到相机的位置。
第一步:使用arView.session.delegate = arView语句将ARSession代理设置为ARView;
第二步:扩展ARView,使其遵循ARSessionDelegate协议;
第三步:执行协议的相应方法。

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        //1.ARSession代理设置为ARView
        arView.session.delegate = arView

        // Load the "Box" scene from the "Experience" Reality File
        let boxAnchor = try! Experience.loadBox()
        
        // Add the box anchor to the scene
        arView.scene.anchors.append(boxAnchor)
        
        return arView
        
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {

    }

}



//2.扩展arView
extension ARView :ARSessionDelegate{
//3.执行协议didUpdate frame
    public  func session(_ session: ARSession, didUpdate frame: ARFrame) {
        let cameraPos = frame.camera.transform.columns.3
        print(cameraPos.x,cameraPos.y,cameraPos.z)
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/990649.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存