从 SCNScene 获取 URL

Get an URL from SCNScene

apple example code for ARKit 中,他们提供了 VirtualObjectclass 从 Models.scnassets.

加载 Scn 文件
 static let availableObjects: [VirtualObject] = {
    let modelsURL = Bundle.main.url(forResource: "Models.scnassets", withExtension: nil)!

    let fileEnumerator = FileManager().enumerator(at: modelsURL, includingPropertiesForKeys: [])!

    return fileEnumerator.compactMap { element in
        let url = element as! URL

        guard url.pathExtension == "scn" && !url.path.contains("lighting") else { return nil }

        return VirtualObject(url: url)
    }
}()

假设我有一个从服务器下载的 SCNScene,第一个问题是,这个 SCNScene 类似于 scn 文件? 如果是,我如何将此 SCNScene 添加到上述函数中,并使其 URL 将其添加到 return VirtualObject(url: url) 中?我在这部分停留了几天,如果有人能帮助我,我将不胜感激。

您可以将其存储在文件管理器中并从那里获取url

如果场景是你的SCNScene:

static let availableObjects: [VirtualObject] = {
    
    var virtualObjects: [VirtualObject] = []

    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let url = paths.appendingPathComponent("chair.scn")
    
    scene.write(to: url, options: nil, delegate: nil) { float, error, pointer in
        if let error = error {
            print(error.localizedDescription)
            return
        }
    }
    
    if let objects = VirtualObject(url: url) {
        virtualObjects.append(objects)
    }
    return virtualObjects
}