来自服务器 URL 的 .obj 文件不起作用
.obj file from server URL doesn't work
我需要从服务器 URL 导入 3D 模型,但无法正常工作。
这是我的代码:
guard let path = modelPath, !path.isEmpty else {
fatalError("Failed to find model file path.")
}
guard let modelURL = URL(string: path) else {
fatalError("Failed to find model URL.")
}
let asset = MDLAsset(url:modelURL)
guard let object = asset.object(at: 0) as? MDLMesh else {
fatalError("Failed to get mesh from asset.")
}
...在 object
处崩溃。
我认为 .obj 对象至少还需要一个 .mlt 文件,可能还需要一个 .jpg 文件作为纹理,检查是否有错误,因为这些文件丢失
MDLAsset(url:)
不处理从服务器下载模型,它仅适用于指向本地存储的 URL
s。
您必须自己下载(使用 URLSession
或类似 Alamofire 的框架)。
示例使用 URLSession
:
下载任务将 return 回调关闭后将被删除的文件的临时位置 return 因此,如果您需要重新使用该文件,则必须将其重新保存在某个地方。
tempLocation
文件的扩展名为 .tmp
,MDLAsset
将无法处理。即使您不需要保留文件,我也没有想出比使用所需的扩展名重新保存它更好的方法(.obj
)。
let fileManager = FileManager.default
let localModelName = "model.obj"
let serverModelURL = URL(...)
let localModelURL = fileManager
.urls(for: .documentDirectory, in: .userDomainMask[0]
.appendingPathComponent(localModelName)
let session = URLSession(configuration: .default)
let task = session.downloadTask(with: modelURL) { tempLocation, response, error in
guard let tempLocation = tempLocation else {
// handle error
return
}
do {
// FileManager's copyItem throws an error if the file exist
// so we check and remove previously downloaded file
// That's just for testing purposes, you probably wouldn't want to download
// the same model multiple times instead of just persisting it
if fileManager.fileExists(atPath: localModelURL.path) {
try fileManager.removeItem(at: localModelURL)
}
try fileManager.copyItem(at: tempLocation, to: localModelURL)
} catch {
// handle error
}
let asset = MDLAsset(url: localURL)
guard let object = asset.object(at: 0) as? MDLMesh else {
fatalError("Failed to get mesh from asset.")
}
}
task.resume() // don't forget to call resume to start downloading
我需要从服务器 URL 导入 3D 模型,但无法正常工作。 这是我的代码:
guard let path = modelPath, !path.isEmpty else {
fatalError("Failed to find model file path.")
}
guard let modelURL = URL(string: path) else {
fatalError("Failed to find model URL.")
}
let asset = MDLAsset(url:modelURL)
guard let object = asset.object(at: 0) as? MDLMesh else {
fatalError("Failed to get mesh from asset.")
}
...在 object
处崩溃。
我认为 .obj 对象至少还需要一个 .mlt 文件,可能还需要一个 .jpg 文件作为纹理,检查是否有错误,因为这些文件丢失
MDLAsset(url:)
不处理从服务器下载模型,它仅适用于指向本地存储的 URL
s。
您必须自己下载(使用 URLSession
或类似 Alamofire 的框架)。
示例使用 URLSession
:
下载任务将 return 回调关闭后将被删除的文件的临时位置 return 因此,如果您需要重新使用该文件,则必须将其重新保存在某个地方。
tempLocation
文件的扩展名为 .tmp
,MDLAsset
将无法处理。即使您不需要保留文件,我也没有想出比使用所需的扩展名重新保存它更好的方法(.obj
)。
let fileManager = FileManager.default
let localModelName = "model.obj"
let serverModelURL = URL(...)
let localModelURL = fileManager
.urls(for: .documentDirectory, in: .userDomainMask[0]
.appendingPathComponent(localModelName)
let session = URLSession(configuration: .default)
let task = session.downloadTask(with: modelURL) { tempLocation, response, error in
guard let tempLocation = tempLocation else {
// handle error
return
}
do {
// FileManager's copyItem throws an error if the file exist
// so we check and remove previously downloaded file
// That's just for testing purposes, you probably wouldn't want to download
// the same model multiple times instead of just persisting it
if fileManager.fileExists(atPath: localModelURL.path) {
try fileManager.removeItem(at: localModelURL)
}
try fileManager.copyItem(at: tempLocation, to: localModelURL)
} catch {
// handle error
}
let asset = MDLAsset(url: localURL)
guard let object = asset.object(at: 0) as? MDLMesh else {
fatalError("Failed to get mesh from asset.")
}
}
task.resume() // don't forget to call resume to start downloading