Swift 4 从 Firebase 加载 3D 模型

Swift 4 Load 3D Models from Firebase

我正在尝试将存储在 Firebase 中的 3D 模型添加到我的 iOS 应用程序中。

现在我将默认对象 (ship.scn) 存储到我的 Firebase 存储中。

如何将从 Firebase 获取的数据转换为 SCNNode?

这是我现在的代码:

 let storage = Storage.storage().reference()
    let modelPath = storage.child("models/ship.scn")
    print("ModelPath: \(modelPath)")
    modelPath.getMetadata { (metaData, error) in
        if error != nil {
            print("ERROR: ", error!)
        }else{
            print("Metadata: \(metaData!)")
        }
    }

    // this is what firebase shows for images
    // how can i get the Data as SCNNode?
    modelPath.getData(maxSize: 1 * 1024 * 1024) { (data, error) in
        if error != nil {
            print("Error getData: \(error!)")
        }else {
            print(data)
        }
    }

我通过将 3D 对象从 firebase 下载到设备文档文件夹中解决了这个问题。 因此,当我需要 3D 对象时,我会创建对下载的 3D 对象的引用

写入目录:(其中 modelPath 是 firebase 中的 storage.child('your path'))

   let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
   let tempDirectory = URL.init(fileURLWithPath: paths, isDirectory: true)
   let targetUrl = tempDirectory.appendingPathComponent("ship.scn")
   modelPath.write(toFile: targetUrl) { (url, error) in
                if error != nil {
                    print("ERROR: \(error!)")
                }else{
                    print("modelPath.write OKAY")
                }
           }

从目录加载 3D 文件:

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    let tempDirectory = URL.init(fileURLWithPath: paths, isDirectory: true)
    let targetUrl = tempDirectory.appendingPathComponent("\ship.scn")
    var sceneForNode: SCNScene? = nil
    do {
        // load the 3D-Model node from directory path
        sceneForNode = try SCNScene(url: targetUrl, options: nil)
    }catch{
        print(error)
    }
    // create node to display on scene
    let node: SCNNode? = sceneForNode?.rootNode.childNode(withName: "ship", recursively: true)