Record device position in 3 dimensional space over given time

I would like to integrate a function into my iOS app, that allows the user to use his or her device as a virtual camera, meaning they hit record, then the motion of the iPhone or iPad is captured and then when done, saved as an fbx or similar file format virtual camera movement. Is there a more of less easy way to do this? thanks in advance!

Replies

Hello chaert-s,

You can do this by serializing the transform data of the ARCamera every frame and writing it to disk (along with the time stamp of the frame).

You could also accomplish something like this using Model I/O (though I don't believe it will export to fbx, other common formats are supported though):

1. Create an MDLTransform object. 2. In the ARSession update method, set the translation and orientation of the transform based on the elapsed time:

func session(_ session: ARSession, didUpdate frame: ARFrame) {
     
    if firstUpdateTime == nil {
      firstUpdateTime = frame.timestamp
    }
     
    let transform = frame.camera.transform
    let rotation = frame.camera.eulerAngles
    let position = transform.columns.3
     
    let elapsedTime = frame.timestamp - firstUpdateTime!
     
    // cameraTransform is an `MDLTransform`
    cameraTransform.setTranslation(position[SIMD3(0,1,2)], forTime: elapsedTime)
    cameraTransform.setRotation(rotation, forTime: elapsedTime)
  }
  1. When you are ready to export the MDLAsset that you added the mesh objects to, you create a new MDLObject, set it’s transform, and then add it to the asset:
// A box to visually represent the camera as it animates through the scene.
    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 = cameraTransform

     let asset = MDLAsset()
     asset.add(object)
  1. Export your MDLAsset to a supported file format (the format should also support keyframe animation):
       try! asset.export(to: url)