如何在Swift UI中添加3D形状?

如何在Swift UI中添加3D形状?,swift,scenekit,swiftui,swift-playground,swift5,Swift,Scenekit,Swiftui,Swift Playground,Swift5,我想知道如何在swift UI中添加3D形状(如球体) 我尝试添加一个scnscene并在swift用户界面中使用它,但我收到一条错误消息 //SceneKit class myscene: SCNScene{ override init(){ super.init() } required init?(coder: NSCoder) { fatalError("init(coder: ) has not been implemented") } } //Swif

我想知道如何在swift UI中添加3D形状(如球体)

我尝试添加一个scnscene并在swift用户界面中使用它,但我收到一条错误消息

//SceneKit

class myscene: SCNScene{
 override init(){
     super.init()
 }
 required init?(coder: NSCoder) {
     fatalError("init(coder: ) has not been implemented")
 }
}

//Swift UI 

struct ContentView: View {
 var body: some View {

     let sphere = SCNSphere(radius: 2.0)
     sphere.firstMaterial?.diffuse.contents = UIColor.blue
     let spherenode = SCNNode(geometry: sphere)
     spherenode.position = SCNVector3(x: 0.0, y: 3.0, z: 0.0)
 }
}

错误消息位于
var body:some View{
行,内容如下:

函数声明了一个不透明的返回类型,但在其主体中没有用于推断底层类型的返回语句


请帮我解决这个问题……

下面是演示如何使用球体设置SceneKit场景的最简单代码。希望对您有所帮助

import SwiftUI
import SceneKit

struct SceneKitView: UIViewRepresentable {
    func makeUIView(context: UIViewRepresentableContext<SceneKitView>) -> SCNView {
        let sceneView = SCNView()
        sceneView.scene = SCNScene()
        sceneView.allowsCameraControl = true
        sceneView.autoenablesDefaultLighting = true
        sceneView.backgroundColor = UIColor.black

        let sphere = SCNSphere(radius: 2.0)
        sphere.firstMaterial?.diffuse.contents = UIColor.blue
        let spherenode = SCNNode(geometry: sphere)
        spherenode.position = SCNVector3(x: 0.0, y: 3.0, z: 0.0)

        sceneView.scene?.rootNode.addChildNode(spherenode)
        return sceneView
    }

    func updateUIView(_ uiView: SCNView, context: UIViewRepresentableContext<SceneKitView>) {

    }

    typealias UIViewType = SCNView
}

struct DemoSceneKit: View {
    var body: some View {
        SceneKitView()
    }
}

struct DemoSceneKit_Previews: PreviewProvider {
    static var previews: some View {
        DemoSceneKit()
    }
}
导入快捷界面
导入SceneKit
结构SceneKitView:UIViewRepresentable{
func makeUIView(上下文:UIViewRepresentableContext)->SCNView{
让sceneView=SCNView()
sceneView.scene=SCNScene()
sceneView.allowsCameraControl=true
sceneView.autoenablesDefaultLighting=true
sceneView.backgroundColor=UIColor.black
设球体=圆球(半径:2.0)
sphere.firstMaterial?.diffuse.contents=UIColor.blue
设spherenode=SCNNode(几何体:球体)
spherenode.position=SCInvector3(x:0.0,y:3.0,z:0.0)
sceneView.scene?.rootNode.addChildNode(spherenode)
返回场景视图
}
func updateUIView(uiView:SCNView,context:UIViewRepresentableContext){
}
类型别名UIViewType=SCNView
}
结构DemoSceneKit:视图{
var body:一些观点{
SceneKitView()
}
}
结构DemoSceneKit_预览:PreviewProvider{
静态var预览:一些视图{
DemoSceneKit()
}
}