【问题标题】:How to change COLLADA(.dae) file to SceneKit(.scn) file programmatically Swift如何以编程方式将 COLLADA(.dae) 文件更改为 SceneKit(.scn) 文件 Swift
【发布时间】:2021-03-25 03:02:17
【问题描述】:
COLLADA(.dae) , SceneKit(.scn)

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

【问题讨论】:

    标签: swift mobile native scenekit arkit


    【解决方案1】:

    感谢 stackoverflow 使用 Model I/O swift 概念解决了

    第一步#

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

    第二步#

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

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

    第三步#

    我的 ViewDidLoad 函数是这样的

    override func viewDidLoad() {
        super.viewDidLoad()
        sceneView.delegate = self
        addTapGestureToSceneView()
        downloadSceneTask()
    }
    

    第四步#

    添加协议 ARSCNViewDelegate

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

    【讨论】:

      猜你喜欢
      • 2014-10-12
      • 2015-10-16
      • 1970-01-01
      • 2018-09-18
      • 2015-01-12
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      相关资源
      最近更新 更多