使用 `named: ` 加载时 SCNScene 为 nil

SCNScene is nil when loading using `named: `

我正在使用以下代码在 scenekit 中渲染场景,当从 art.scnassests 文件夹加载 dae 文件时,它可以完美运行。

let scene = SCNScene(named: "art.scnassets/idle.dae")

但是我想下载资产并应用它,但出现错误

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let scene = SCNScene(named: documentsURL.absoluteString+"idle.dae")

文件夹中存在名为 idle.dae 的文件。

我收到错误:fatal error: unexpectedly found nil while unwrapping an Optional value

如何加载下载的资源并动态应用?我究竟做错了什么?请问有什么指点吗?我是 iOS 编程的菜鸟。

除非 iOS 11 发生了一些变化,否则您将无法在 iOS 上的运行时下载和实例化 DAE 文件。它们在构建时 compressed/compiled 使用名为 scntool.

的实用程序

您能否改为使用模型 I/O 支持的文件格式之一?请参阅 https://developer.apple.com/videos/play/wwdc2015/602/?time=320 for the original list (Alembic .abc, Polygon .ply Triangles .stl, WaveFront .obj), and https://developer.apple.com/videos/play/wwdc2017/610/ 以快速讨论 Pixar 的 USD(通用场景描述)。

如果您受困于 DAE 文件,Frederik Jacques 在 https://the-nerd.be/2014/11/07/dynamically-load-collada-files-in-scenekit-at-runtime/ 上有一篇文章概述了他对 DAE 处理管道进行逆向工程的经验。他的技术允许下载从服务器上的 DAE 文件处理的 SCN 文件。 另见 Load uncompressed collada file using iOS Scene Kit (with comments by an authoritative source) and https://forums.developer.apple.com/thread/38010