Swift 如何以编程方式将COLLADA(.dae)文件更改为SceneKit(.scn)文件

Swift 如何以编程方式将COLLADA(.dae)文件更改为SceneKit(.scn)文件,swift,mobile,native,scenekit,arkit,Swift,Mobile,Native,Scenekit,Arkit,我正在使用scenekit,问题是有一个管理员将.dae文件上传到服务器,我将在应用程序中获得该链接,并且通过使用代码,我必须在相机上显示该链接。在运行.scn文件url的应用程序时,它工作正常,但对于.dae文件url,它正在生成错误此平台不支持COLLADA文件所以我想在运行时将.dae文件转换为.scn文件。如果有人知道任何其他可以将.dae文件转换为.scn文件的源代码,请提及。通过此源代码,管理员将在转换后将.scn文件上载到服务器。最终使用I/O swift概念解决了堆栈溢出问题 第

我正在使用
scenekit
,问题是有一个管理员将
.dae
文件上传到服务器,我将在应用程序中获得该链接,并且通过使用代码,我必须在相机上显示该链接。在运行
.scn
文件url的应用程序时,它工作正常,但对于.dae文件url,它正在生成错误
此平台不支持COLLADA文件所以我想在运行时将.dae文件转换为.scn文件。如果有人知道任何其他可以将
.dae
文件转换为
.scn
文件的源代码,请提及。通过此源代码,管理员将在转换后将
.scn
文件上载到服务器。

最终使用I/O swift概念解决了堆栈溢出问题

第1步#

第二步#

现在我已经将url保存在本地设备内存中,现在我将单击相机中的任意位置并在场景中加载该本地url

// first need to download file to local directory
func downloadSceneTask(){
    
    //1. Get The URL Of The SCN File
    guard let url = URL(string: "Your_url") 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()
}
第三步#

我的ViewDidLoad函数如下

func addTapGestureToSceneView() {
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didReceiveTapGesture(_:)))
    sceneView.addGestureRecognizer(tapGestureRecognizer)
}

@objc func didReceiveTapGesture(_ sender: UITapGestureRecognizer) {
    let location = sender.location(in: sceneView)
    guard let hitTestResult = sceneView.hitTest(location, types: [.featurePoint, .estimatedHorizontalPlane]).first
        else { return }
    let results = self.sceneView.hitTest(location, types: .featurePoint)

    // 2
    guard let result = results.first else {
        return
    }
    // 3
    let translation = result.worldTransform.translation
    self.translation = translation
    anchor = ARAnchor(transform: hitTestResult.worldTransform)
    sceneView.session.add(anchor: anchor!)
}
第4步#

添加协议ARSCNViewDelegate

override func viewDidLoad() {
    super.viewDidLoad()
    sceneView.delegate = self
    addTapGestureToSceneView()
    downloadSceneTask()
}
override func viewDidLoad() {
    super.viewDidLoad()
    sceneView.delegate = self
    addTapGestureToSceneView()
    downloadSceneTask()
}
// Getting file from local directory and load
extension ViewController: ARSCNViewDelegate {

   func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
       guard !(anchor is ARPlaneAnchor) else { return }
       if let droneNode = loadModel() {
           DispatchQueue.main.async {
               node.addChildNode(droneNode)
           }
       }
   }

   func getDocumentsDirectory() -> URL {
       let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
       let documentsDirectory = paths[0]
       return documentsDirectory

   }

   // Loads The SCNFile From The Documents Directory
   func loadModel() -> SCNNode? {
       //1. Get The Path Of The Downloaded File
       let downloadedScenePath = getDocumentsDirectory().appendingPathComponent("table.obj")
       let asset = MDLAsset(url: downloadedScenePath)
       let object = asset.object(at: 0)
       let node = SCNNode(mdlObject: object)
       //7. Add It To The Scene
       return node

    }
}