gpt4 book ai didi

scenekit - SwiftUI:将 SwiftUI 路径添加到 Scenekit 场景

转载 作者:行者123 更新时间:2023-12-04 01:36:46 34 4
gpt4 key购买 nike

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

我创建了:Scenekit View (图片 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()
}
}

非常感谢任何帮助!

干杯

最佳答案

你的 SwiftUI ContentView 是一个 View,它是一个结构体,但是 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)

关于scenekit - SwiftUI:将 SwiftUI 路径添加到 Scenekit 场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59216411/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com