Swift 从 URL Xcode 加载 3d 资产 [英] Swift Load A 3d Asset from URL Xcode

查看:24
本文介绍了Swift 从 URL Xcode 加载 3d 资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple HTTP server running and Im trying to fetch this scenekit from my local server but IT shows me NIL error, or Error Loading Scene. I dont understand how to load this model from my simple local host. How to configure my code so that I will be able to fetch any Scenekit from remote or local server.

提前致谢

  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 所说,您需要先实际下载模型,然后才能使用它.

As @Prashant said you will need to actually download the model first before using it.

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

The first thing you would need to do therefore is create a URLSession to download the file e.g:

/// 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 例如:

We will then make reference to the URLSessionDownloadDelegate e.g.:

class ViewController: UIViewController, URLSessionDownloadDelegate { }

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

Now we have the delegate hooked up we need to make use of the following callback to copy our downloaded file to the Documents Directory of the device:

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)")
    }

}

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

Note that in the function I am using the following helper method to get the Documents Directory:

/// 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):

Once the file has been downloaded and copied across we then call our loadModel function (3) like so:

/// 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")
    }

}

希望能帮到你...

这篇关于Swift 从 URL Xcode 加载 3d 资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆