SceneView ( SceneKit View ) in SwiftUI - iOS 14 / MacOS BigSur

Using the new SceneKit SceneView in SwiftUI how do I turn on the SceneKit debug parameters? The current options parameter only takes a limited number of Options?

https://developer.apple.com/documentation/scenekit/sceneview/options

Code Block swift
struct ContentView : View {
    @State var theScene = SCNScene(named: "ship.scn")!
    @State private var pointOfView = "distantCamera"
    public var body: some View {
        ZStack {
            SceneView(
                scene: theScene,
                pointOfView: theScene.rootNode.childNode(withName: pointOfView, recursively: true),
                options: [ .allowsCameraControl, SCNDebugOptions.showBoundingBoxes ]  - [ERROR]
            )
            Button(action: {
                // Can I turn on debug for SceneView here?
                // show SCNView statistics such as fps and timing information
                scnView.showsStatistics = true  - [ERROR]
                scnView.debugOptions = [.renderAsWireframe, .showBoundingBoxes]  - [ERROR]
            }) {
                Text("Debug").padding(.top, 300)
            }
        }
    }
}


Answered by OOPer in 631996022
I'm not sure this is the right way to handle this situation, but you can set showsStatistics or debugOptions inside SCNSceneRendererDelegate.

A sample code. (Taken and modified from the template project of iOS/Game/SceneKit. You may need to modify some parts.)
Code Block
import SwiftUI
import SceneKit
struct ContentView: View {
@StateObject var coordinator = SceneCoordinator()
var body: some View {
ZStack {
SceneView(
scene: coordinator.theScene,
options: [.allowsCameraControl],
delegate: coordinator
)
Button(action: {
coordinator.showsStatistics = true
coordinator.debugOptions = [.renderAsWireframe, .showBoundingBoxes]
}) {
Text("Debug").padding(.top, 300)
}
}
}
}
class SceneCoordinator: NSObject, SCNSceneRendererDelegate, ObservableObject {
var showsStatistics: Bool = false
var debugOptions: SCNDebugOptions = []
lazy var theScene: SCNScene = {
// create a new scene
let scene = SCNScene(named: "art.scnassets/ship.scn")!
//...
return scene
}()
    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
renderer.showsStatistics = self.showsStatistics
renderer.debugOptions = self.debugOptions
}
}


(To make this code run on simulators, I needed to disable Metal API Validation with Edit Scheme...>Run>Diagnostics.)
Accepted Answer
I'm not sure this is the right way to handle this situation, but you can set showsStatistics or debugOptions inside SCNSceneRendererDelegate.

A sample code. (Taken and modified from the template project of iOS/Game/SceneKit. You may need to modify some parts.)
Code Block
import SwiftUI
import SceneKit
struct ContentView: View {
@StateObject var coordinator = SceneCoordinator()
var body: some View {
ZStack {
SceneView(
scene: coordinator.theScene,
options: [.allowsCameraControl],
delegate: coordinator
)
Button(action: {
coordinator.showsStatistics = true
coordinator.debugOptions = [.renderAsWireframe, .showBoundingBoxes]
}) {
Text("Debug").padding(.top, 300)
}
}
}
}
class SceneCoordinator: NSObject, SCNSceneRendererDelegate, ObservableObject {
var showsStatistics: Bool = false
var debugOptions: SCNDebugOptions = []
lazy var theScene: SCNScene = {
// create a new scene
let scene = SCNScene(named: "art.scnassets/ship.scn")!
//...
return scene
}()
    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
renderer.showsStatistics = self.showsStatistics
renderer.debugOptions = self.debugOptions
}
}


(To make this code run on simulators, I needed to disable Metal API Validation with Edit Scheme...>Run>Diagnostics.)
@OOPer's solution is an interesting workaround, but as noted it may not be the best solution since you will be setting the debug options every frame in updateAtTime.

A better solution may be to create your own representable container for SCNView using UIViewRepresentable, and expose whatever SCNDebugOptions you'd like in your initializer.

You should also file an enhancement request for additional options to be added to SceneView.Options using Feedback Assistant.

Thanks!

@OOPer, thanks for this work around! I can use it while I file an enhancement request with Apple for additional options to be added to SceneView.Options using Feedback Assistant..

Thanks @gchiste, I am going to file an enhancement request! 😀

I guess showsStatistics is still in the works? Any update on that ticket?

SceneView ( SceneKit View ) in SwiftUI - iOS 14 / MacOS BigSur
 
 
Q