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

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

问题描述

我有一个运行中的简单HTTP服务器,我正在尝试从本地服务器中获取此Scenekit,但它向我显示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 { }

现在,我们已将代表连接起来,我们需要使用以下callback将下载的文件复制到设备的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 function(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")
    }

}

希望有帮助...

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

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