SwiftUI:将SwiftUI路径添加到Scenekit Scene

时间:2019-12-06 15:55:23

标签: scenekit swiftui

我试图将添加SwiftUI路径添加到Scenekit场景(作为SCNShape)。

我创建了: Scenekit视图(图像1) SwiftUI路径(图片2)

然后,我基于以下方法尝试调用SwiftUI路径: Unable to create circle using UIBezierPath and SCNShape

   let scnpath = ContentView()
   let shape = SCNShape(path: scnpath)
   let shapeNode = SCNNode(geometry: shape)

没有用,结果如下:

enter image description here

import SwiftUI
import SceneKit

struct ScenekitView : UIViewRepresentable {
    let scene = SCNScene(named: "art.scnassets/ship.scn")!


    func makeUIView(context: Context) -> SCNView {
        // create and add a camera to the scene
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        scene.rootNode.addChildNode(cameraNode)

        // add SwiftUI Path to Scenekit Scene

        let scnpath = ContentView()
        let shape = SCNShape(path: scnpath)
        let shapeNode = SCNNode(geometry: shape)

        // place the camera
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)


        // retrieve the SCNView
        let scnView = SCNView()
        return scnView
    }

    func updateUIView(_ scnView: SCNView, context: Context) {
        scnView.scene = scene


    }
}

#if DEBUG
struct ScenekitView_Previews : PreviewProvider {
    static var previews: some View {
        ScenekitView()
        .colorScheme(.dark)
        .previewDevice("iPad Pro (12.9-inch) (3rd generation)")
    }
}
#endif

enter image description here


import SwiftUI

struct ContentView: View {
    var body: some View {

                Path { path in
                   path.move(to: CGPoint(x: 63.5, y: 98.38))
                           path.addLine(to: CGPoint(x: 920.14, y: 0))
                           path.addLine(to: CGPoint(x: 1094.24, y: 584.81))
                           path.addLine(to: CGPoint(x: 920.14, y: 938.58))
                           path.addLine(to: CGPoint(x: 498.47, y: 1279.73))
                           path.addLine(to: CGPoint(x: 211.79, y: 938.58))
                           path.addLine(to: CGPoint(x: 617.65, y: 584.81))
                           path.addLine(to: CGPoint(x: 735.35, y: 295.94))
                           path.addLine(to: CGPoint(x: 332.02, y: 349.08))
                           path.addLine(to: CGPoint(x: 0, y: 672.25))
                           path.addLine(to: CGPoint(x: 63.5, y: 98.38))
                }
                .stroke(Color.blue, lineWidth: 3)
            }
        }

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

真的很感谢您的帮助!

欢呼

3 个答案:

答案 0 :(得分:1)

您的SwiftUI ContentViewView,它是结构体,但是SCNShape期望UIBezierPath是一个类。因此,如果您想加入这些不同的概念,则必须执行以下操作:

ContentView

struct ContentView: View {
    let path =  Path { path in
       path.move(to: CGPoint(x: 63.5, y: 98.38))
               path.addLine(to: CGPoint(x: 920.14, y: 0))
               path.addLine(to: CGPoint(x: 1094.24, y: 584.81))
               path.addLine(to: CGPoint(x: 920.14, y: 938.58))
               path.addLine(to: CGPoint(x: 498.47, y: 1279.73))
               path.addLine(to: CGPoint(x: 211.79, y: 938.58))
               path.addLine(to: CGPoint(x: 617.65, y: 584.81))
               path.addLine(to: CGPoint(x: 735.35, y: 295.94))
               path.addLine(to: CGPoint(x: 332.02, y: 349.08))
               path.addLine(to: CGPoint(x: 0, y: 672.25))
               path.addLine(to: CGPoint(x: 63.5, y: 98.38))
    }

    var body: some View {
        self.path
            .stroke(Color.blue, lineWidth: 3)
    }
}

ScenekitView

let scnpath = ContentView().path.cgPath
let shape = SCNShape(path: UIBezierPath(cgPath: scnpath), extrusionDepth: 1.0)

答案 1 :(得分:0)

enter image description here尝试一下:

1)您没有将shapenode添加到场景中-因此您不会看到它

2)开始时,如果您对Scenekit不太熟悉,则不要自己控制相机...让此Scenekit为您服务,所以我删除了您的相机(因为它需要正确的位置+角度这样您才能真正看到一些东西

3。)像asperi告诉您的那样将形状更新为您的形状-这是您可以使用的一种方式(我没有在正在运行的示例中放过它,因为这对您来说太容易了;))

4。)结果如图所示

5。)我不知道您是否知道:在模拟器中,如果您点击模拟器中的“播放”按钮,则只能看到scenekitviews ...

struct ScenekitView : UIViewRepresentable {

    let scene = SCNScene()


    func makeUIView(context: Context) -> SCNView {

        let uiView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
        // create and add a camera to the scene
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
  //      scene.rootNode.addChildNode(cameraNode)

        // add SwiftUI Path to Scenekit Scene

        let bezierPath = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 20, height: 20))

        let shape = SCNShape(path: bezierPath, extrusionDepth: 50)
        let shapeNode = SCNNode(geometry: shape)

        // place the camera
        cameraNode.position = SCNVector3(x: 0, y: 10, z: 25)

     //   scene.rootNode.addChildNode(cameraNode)
        scene.rootNode.addChildNode(shapeNode)

        // retrieve the SCNView
        let scnView = SCNView()
        scnView.scene = scene
        scnView.backgroundColor = UIColor.blue
        scnView.frame = CGRect(x: 0, y: 0, width: 500, height: 500)

        return scnView
    }

    func updateUIView(_ scnView: SCNView, context: Context) {
        print("test")

    }
}

答案 2 :(得分:0)

如果要使其有点“性感”,可以添加镜面反射和灯光...,您将得到:

enter image description here

此处的代码:

std::transform