How do you load a .dae file into an SCNNode in iOS SceneKit?

你离开我真会死。 提交于 2019-12-03 03:47:29

问题


I am having trouble understanding how geometry from .dae files should be loaded into an iOS SceneKit scene graph.

I know that you can load .dae files directly into an SCNScene:

// create a new scene
let scene = SCNScene("test.scnassets/test.dae")

I also know that you can create an empty SCNScene and add built-in 3D objects into it as child nodes:

let scene = SCNScene()
var sphereNode = SCNNode()
sphereNode.geometry = SCNSphere(radius: 1.0)
scene.rootNode.addChildNode(sphereNode)

In the second case, can you load a .dae file into SCNNode instead of using built in objects? Or is it expected that all .dae files need to be loaded into the SCNScene first?

If the latter is true, how then do you place the SCNNode objects into the right place in the scene graph hierarchy under SCNScene?EDIT #1:

This is working for me now, which is modified version of what @FlippinFun suggested.

let url = NSBundle.mainBundle().URLForResource("test.scnassets/test", withExtension: "dae")
let source = SCNSceneSource(URL: url, options: nil)
let block = source.entryWithIdentifier("ID10", withClass: SCNGeometry.self) as SCNGeometry
scene.rootNode.addChildNode(SCNNode(geometry: block))

But perhaps there's a better way? The above involves manually loading each identifier from the .dae tree. In my file, there's a node "SketchUp" with child identifiers "ID2" and "ID10". This method will not let you load the root "SketchUp". (I get a runtime error.)


回答1:


The usual pattern is to load your scene and retrieve the node you are interested in with its name using

[scene.rootNode childNodeWithName:aName recursively:YES];

Then you can reparent this node to your main scene.




回答2:


I had a similar problem where I had one .dae file and I wanted to load multiple nodes from that file into a node. This worked for me:

var node = SCNNode()
let scene = SCNScene(named: "helloScene.dae")
var nodeArray = scene.rootNode.childNodes

for childNode in nodeArray {
  node.addChildNode(childNode as SCNNode)
}

First, I set a node that will hold it all together so it looks like it does in the original file. Next we import the .dae file we want and copy all the nodes inside of it. Lastly, for each node in the array I added it to the node.




回答3:


Thanks for your help!

Here is a simple collada2Node class.

//collada2SCNNode
class func collada2SCNNode(filepath:String) -> SCNNode {
    var node = SCNNode()
    let scene = SCNScene(named: filepath)
    var nodeArray = scene!.rootNode.childNodes

    for childNode in nodeArray {
        node.addChildNode(childNode as SCNNode)
    }
    return node
}



回答4:


func nodeWithFile(path: String) -> SCNNode {

    if let scene = SCNScene(named: path) {

        let node = scene.rootNode.childNodes[0] as SCNNode
        return node

    } else {
        println("Invalid path supplied")
        return SCNNode()
    }

}



回答5:


The following answers didn't work for me.

Here is what worked for me in Xcode 7.0 beta 5 , iOS 9.0 beta / Swift 2.0:

3D model type: Collada 3D model name: "model.dae"

  1. Load the scene:

    let lampScene = SCNScene(named: "art.scnassets/model.dae")
    
  2. Access the child SCNode which is in the SCNScene:

    let lampRootNode = lampScene?.rootNode.childNodes[0]
    
  3. Add the lamp node from the loaded scene into the current loaded and viewed scene:

    self.scene.rootNode.addChildNode(lampRootNode!)
    

NOTE: The self.scene exists due to:

private var scene:SCNScene!

Prior of adding the object to the scene I have made some scene setup according to the scene kit example. If you need the code mention it in the comments.

Hope it helps.




回答6:


guard let shipScene = SCNScene(named: "ship.dae") else { return }
let shipNode = SCNNode()
let shipSceneChildNodes = shipScene.rootNode.childNodes
for childNode in shipSceneChildNodes {
    shipNode.addChildNode(childNode)
}


来源:https://stackoverflow.com/questions/25356301/how-do-you-load-a-dae-file-into-an-scnnode-in-ios-scenekit

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