gpt4 book ai didi

ios - 从 URL Xcode 快速加载 3d 资源

转载 作者:行者123 更新时间:2023-11-28 11:49:06 25 4
gpt4 key购买 nike

我有一个简单的 HTTP 服务器在运行,我试图从我的本地服务器获取这个 scenekit,但它显示 NIL 错误,或错误加载场景。我不明白如何从我简单的本地主机加载这个模型。如何配置我的代码,以便我能够从远程或本地服务器获取任何 Scenekit。

提前致谢

  do {
let shipScene = try SCNScene(url: URL(fileURLWithPath: "http://localhost:8080/chair.scn") , options: nil)



// Set the scene to the view
sceneView.scene = shipScene
let shipNode = shipScene.rootNode.childNodes.first!
shipNode.position = SCNVector3Zero
shipNode.position.z = 0.15
shipNode.position.y = 0
shipNode.position.x = 0
let action = SCNAction.repeatForever(SCNAction.rotate(by: .pi, around: SCNVector3(0, 1, 0), duration: 5))
shipNode.runAction(action)
planeNode.addChildNode(shipNode)
node.addChildNode(planeNode)

} catch {
print("ERROR loading scene")
}

最佳答案

正如@Prashant 所说,您需要先实际下载模型才能使用它。

因此,您需要做的第一件事是创建一个 URLSession 来下载文件,例如:

/// Downloads An SCNFile From A Remote URL
func downloadSceneTask(){

//1. Get The URL Of The SCN File
guard let url = URL(string: "http://localhost:8080/chair.scn") else { return }

//2. Create The Download Session
let downloadSession = URLSession(configuration: URLSession.shared.configuration, delegate: self, delegateQueue: nil)

//3. Create The Download Task & Run It
let downloadTask = downloadSession.downloadTask(with: url)
downloadTask.resume()
}

}

然后我们将引用 URLSessionDownloadDelegate 例如:

class ViewController: UIViewController, URLSessionDownloadDelegate { }

现在我们已经连接了委托(delegate),我们需要使用以下回调将我们下载的文件复制到设备的Documents Directory:

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {

//1. Create The Filename
let fileURL = getDocumentsDirectory().appendingPathComponent("chair.scn")

//2. Copy It To The Documents Directory
do {
try FileManager.default.copyItem(at: location, to: fileURL)

print("Successfuly Saved File \(fileURL)")

//3. Load The Model
loadModel()

} catch {

print("Error Saving: \(error)")
}

}

请注意,在该函数中,我使用以下辅助方法来获取文档目录:

/// Returns The Documents Directory
///
/// - Returns: URL
func getDocumentsDirectory() -> URL {

let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory

}

下载并复制文件后,我们将调用 loadModel 函数 (3),如下所示:

/// Loads The SCNFile From The Documents Directory
func loadModel(){

//1. Get The Path Of The Downloaded File
let downloadedScenePath = getDocumentsDirectory().appendingPathComponent("chair.scn")

do {

//2. Load The Scene Remembering The Init Takes ONLY A Local URL
let modelScene = try SCNScene(url: downloadedScenePath, options: nil)

//3. Create A Node To Hold All The Content
let modelHolderNode = SCNNode()

//4. Get All The Nodes From The SCNFile
let nodeArray = modelScene.rootNode.childNodes

//5. Add Them To The Holder Node
for childNode in nodeArray {
modelHolderNode.addChildNode(childNode as SCNNode)
}

//6. Set The Position
modelHolderNode.position = SCNVector3(0, 0, -1.5)

//7. Add It To The Scene
self.augmentedRealityView?.scene.rootNode.addChildNode(modelHolderNode)


} catch {
print("Error Loading Scene")
}

}

希望对你有帮助

关于ios - 从 URL Xcode 快速加载 3d 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52051263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com