从Firebase加载Swift 4加载3D模型

时间:2018-12-02 23:42:17

标签: firebase swift4 firebase-storage scenekit

我正在尝试将存储在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)
        }
    }

1 个答案:

答案 0 :(得分:0)

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

写入目录:(其中modelPath是firebase中的storage.child(“您的路径”​​))

   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)