SwiftUI - how to add a Scenekit Scene

前端 未结 3 537
萌比男神i
萌比男神i 2021-02-04 19:57

How can I add a Scenekit Scene to a SwiftUI view?

I tried the following Hello World, using the standard Ship Scene example...

import SwiftUI
import Scene         


        
3条回答
  •  长情又很酷
    2021-02-04 20:30

    You don't need use UIViewRepresentable anymore. Here's an update code for SwiftUI

    import SwiftUI
    import SceneKit
    
    struct ContentView: View {
        var scene: SCNScene? {
            SCNScene(named: "Models.scnassets/Avatar.scn")
        }
    
        var cameraNode: SCNNode? {
            let cameraNode = SCNNode()
            cameraNode.camera = SCNCamera()
            cameraNode.position = SCNVector3(x: 0, y: 0, z: 2)
            return cameraNode
        }
    
        var body: some View {
            SceneView(
                scene: scene,
                pointOfView: cameraNode,
                options: [
                    .allowsCameraControl,
                    .autoenablesDefaultLighting,
                    .temporalAntialiasingEnabled
                ]
            )
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    

提交回复
热议问题