You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实时记录AR相机的位置与旋转信息并保存至文件?

问题:录制AR相机运动并导出为支持关键帧的格式(如Alembic)

我花了两天时间开发一款ARView,核心需求是实时记录相机的空间位置与旋转信息,并保存为可用于3D软件的关键帧文件。最终导出的数据需要能在Autodesk Maya、Cinema4D等工具中驱动虚拟相机,优先支持可承载相机对象并实现时间轴动画的文件格式;退一步也可以导出随时间运动的物体,后续将相机父级绑定到该物体上即可。

目前我尝试将设备的位置与旋转信息记录到MDL对象中,但导出后没有动画效果。我测试过多种文件格式,部分不支持关键帧动画,了解到Alembic格式具备该能力。以下是我编写的代码(因尝试过多种方案,代码略显杂乱):

import SwiftUI
import ARKit
import RealityKit
import ModelIO

struct ARViewContainer: UIViewRepresentable {
    let session = ARSession()
    let delegate = MySessionDelegate()
    
    func makeUIView(context: Context) -> ARView {
        // Set up the ARView with session
        let arView = ARView(frame: .zero)
        let boxAnchor = try! Experience.loadBox()
        arView.scene.anchors.append(boxAnchor)
        arView.session.delegate = delegate // assign delegate to the session
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
        // Update the ARView if needed
    }
    
    func startARSession() {
        // Start the ARSession
        let configuration = ARWorldTrackingConfiguration()
        configuration.planeDetection = [.horizontal, .vertical]
        session.run(configuration, options: [])
    }
    
    func stopARSession() {
        // Stop the ARSession
        session.pause()
    }
}

class MySessionDelegate: NSObject, ARSessionDelegate {
    var object: MDLMesh?
    let asset = MDLAsset()
    let cameraTransform = MDLTransform()
    var documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    
    func session(_ session: ARSession, didUpdate frame: ARFrame) {
            // Get the camera position and orientation for the current frame
            let transform = frame.camera.transform
            let rotation = frame.camera.eulerAngles
            let position = transform.columns.3
            let elapsedTime = frame.timestamp
            cameraTransform.setTranslation(position[SIMD3(0,1,2)], forTime: elapsedTime)
            cameraTransform.setRotation(rotation, forTime: elapsedTime)
            print("Camera Transform: \(cameraTransform.matrix)")
    }

}

struct Camera: View {
    var body: some View {
        VStack {
            ARViewContainer().onAppear(perform: ARViewContainer().startARSession)
                .onDisappear(perform: ARViewContainer().stopARSession)
            Button("Export Recording") {
                // Create an MDLAsset with a box representing the camera transform
                let object = MDLMesh(boxWithExtent: .init(0.1, 0.1, 0.1), segments: .init(10, 10, 10), inwardNormals: false, geometryType: .triangles, allocator: nil)
                object.name = "Camera Transform"
                object.transform = MySessionDelegate().cameraTransform
                
                let asset = MDLAsset()
                asset.add(object)
                
                // Export the MDLAsset to a file
                let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
                let fileURL = documentsDirectory.appendingPathComponent("recording.abc")
                try! asset.export(to: fileURL)
            }
        }
        
    }
}

如果有其他可行的实现方案,欢迎分享,感谢帮助!

内容的提问来源于stack exchange,提问作者Herbert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 10:46:18