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

后端 未结 4 1551
暖寄归人
暖寄归人 2021-01-05 04:47

This works

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

Is there an equivalent for a node?

let node = SCNNode(geometry: SCNGe         


        
相关标签:
4条回答
  • 2021-01-05 05:22
    // 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()
    
    0 讨论(0)
  • 2021-01-05 05:30

    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.

    0 讨论(0)
  • 2021-01-05 05:40

    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")
    
    0 讨论(0)
  • 2021-01-05 05:41

    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!)
       }
    }
    
    0 讨论(0)
提交回复
热议问题