load a collada (dae) file into SCNNode (Swift - SceneKit)

点点圈 提交于 2019-11-30 04:42:35

问题


This works

let scene = SCNScene(named: "house.dae")

Is there an equivalent for a node?

let node = SCNNode(geometry: SCNGeometry( ..??.. "house.dae" ..??.. ))

I have searched high and low, finding nothing that will load an entire dae file into a SCNNode. (not just one ID from it)


回答1:


// add a SCNScene as childNode to another SCNScene (in this case to scene)
func addSceneToScene() {
    let geoScene = SCNScene(named: "art.scnassets/ball.dae")
    scene.rootNode.addChildNode(geoScene.rootNode.childNodeWithName("Ball", recursively: true))
}
addSceneToScene()



回答2:


iOS 9 introduced the SCNReferenceNode class. Now you can do:

if let filePath = Bundle.main.path(forResource: "Test", ofType: "dae", inDirectory: "GameScene.scnassets") {
   // ReferenceNode path -> ReferenceNode URL
   let referenceURL = URL(fileURLWithPath: filePath)

   if #available(iOS 9.0, *) {
      // Create reference node
      let referenceNode = SCNReferenceNode(URL: referenceURL)
      referenceNode?.load()
      scene.rootNode.addChildNode(referenceNode!)
   }
}



回答3:


That's what I use in real project:

extension SCNNode {

    convenience init(named name: String) {
        self.init()

        guard let scene = SCNScene(named: name) else {
            return
        }

        for childNode in scene.rootNode.childNodes {
            addChildNode(childNode)
        }
    }

}

After that you can call:

let node = SCNNode(named: "art.scnassets/house.dae")



回答4:


The scene you get from SCNScene(named:) has a rootNode property whose children are the contents of the DAE file you loaded. You should be able to pull children off that node and add them to other nodes in an existing SCNScene.



来源:https://stackoverflow.com/questions/25387604/load-a-collada-dae-file-into-scnnode-swift-scenekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!