如何在 Swift UI 中添加 3D 形状? [英] How to add 3D shapes in Swift UI?

查看:76
本文介绍了如何在 Swift UI 中添加 3D 形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to know how to add 3D shapes (eg. Sphere) in swift UI

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

I have tried adding a scnscene and using that in swift UI but I get a error message

//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 { 行,内容如下:

The error message is on var body: some View { line and reads as follows :

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

请帮我解决这个问题......

Please help me with this problem......

推荐答案

这里是演示如何使用球体设置 SceneKit 场景的最简单代码.希望有帮助.

Here is simplest code to demo how to setup SceneKit scene with your sphere. Hope it helps.

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()
    }
}

这篇关于如何在 Swift UI 中添加 3D 形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆