在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)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)