SwiftUI - integrating with SceneKit

Wwdc20-10041 mentions that SceneKit can be now be imported into SwiftUI.

Before, the way to do that was using UIViewRepresentable, so we can display a scene of ship for ex.

What would be the new way to show the ship on a SwiftUI view?

Accepted Reply

Howdy!

You can now import both SwiftUI and SceneKit and pass along your scenes, nodes, to a SceneView that can be configured to render your scene inside your hierarchy.

Example:
Code Block
import SwiftUI
import SceneKit
struct ContentView: View {
var scene = SCNScene(named: "MyScene")
var cameraNode: SCNNode? {
scene?.rootNode.childNode(withName: "camera", recursively: false)
}
var body: some View {
SceneView(
scene: scene,
pointOfView: cameraNode,
options: []
)
}
}

Replies

Howdy!

You can now import both SwiftUI and SceneKit and pass along your scenes, nodes, to a SceneView that can be configured to render your scene inside your hierarchy.

Example:
Code Block
import SwiftUI
import SceneKit
struct ContentView: View {
var scene = SCNScene(named: "MyScene")
var cameraNode: SCNNode? {
scene?.rootNode.childNode(withName: "camera", recursively: false)
}
var body: some View {
SceneView(
scene: scene,
pointOfView: cameraNode,
options: []
)
}
}

This is awesome! : )

No lab for SceneKit This year? How can I get more examples, on how to interact SwiftUI with SceneKit? Like for example touching and change texture of a model?
Interesting .. I was struggling with this too and the example helped. One question, though. We don't know what is contained in "MyScene" but I assumed that it contained a node called "camera" for the pointOfView (POV) parameter, else cameraNode would be nil.

So I put my scene in place of "MyScene" and ran it. Imagine my surprise when it rendered in the view, even though I have no POV node in my model. Just to be sure I changed the SceneView creation to:
Code Block
SceneView(scene: scene, pointOfView: nil, options: [])

and it still rendered nicely. So SceneView has a default POV, which makes sense given pointOfView is an optional, but its location is not documented (it looks to be somewhere outside the model on the +Z axis). In fact, scene is optional too, which leaves me wondering what:
Code Block
SceneView()

means? Are there ways to provide values for those optionals after the SceneView is created?