SceneKit – Add multiple models to the scene of the SceneView struct

Viewed 151

I want to implement adding models to the SceneKit, but I can't figure out how to add new elements. I'm using this code and I get at most one model. How do I add multiple models to the screen?

var body: some View {
    VStack {
        SceneView(
            scene: SCNScene(named: "example.usdz"),
            options: [.autoenablesDefaultLighting, 
                      .allowsCameraControl]
        )
    }
}
1 Answers

Use the following approach to add as many models to the SceneView as you wish:

import SwiftUI
import SceneKit

struct ContentView: View {
    
    var sceneOne = SCNScene(named: "model.usdz")
    var sceneTwo = SCNScene(named: "teapot.usdz")
    
    var options: SceneView.Options = [.autoenablesDefaultLighting,
                                      .allowsCameraControl]

    var body: some View {
        ZStack {

            if let node = sceneTwo?.rootNode.childNode(withName: "teapot", 
                                                    recursively: true) {

                let _ = node.position.y = 50                    // meters
                let _ = sceneOne?.rootNode.addChildNode(node)
            }

            SceneView(scene: sceneOne, options: options)
               .colorInvert()
        }
    }
}

enter image description here

Related