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!
Record device position in 3 dimensional space over given time
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)
}
- 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)
- Export your MDLAsset to a supported file format (the format should also support keyframe animation):
try! asset.export(to: url)
Thanks for your reply! Sadly, this doesn't clear everything up for me... Could you maybe walk me through serializing transform data from the ARCamera and writing it to disk? I just want an easily usable 3d file that I can then use in a 3d application like cinema 4d or Maya. Your second method you mentioned also sounds interesting, but is there a way to export a camera object? Otherwise I'd need to parent a camera to the created cube and that just doesn't seem very elegant of a solution. Also, with this method the exported cube stays static and doesn't move, somehow the translation and rotation data isn't being written into the object (I am using a usd file), there is only a static cube in the scene. Thanks in advance for any further assistance, it is greatly appreciated!