同时添加多个 SCNNode(s)
Posted
技术标签:
【中文标题】同时添加多个 SCNNode(s)【英文标题】:Adding multiple SCNNode(s) at the same time 【发布时间】:2021-08-10 05:36:57 【问题描述】:我有一个名为addShapes
的函数。我希望它创建 3 个形状
import ARKit
import SwiftUI
struct SceneKitView: UIViewRepresentable
let arView = ARSCNView(frame: .zero)
let config = ARWorldTrackingConfiguration()
@Binding var addBox: Int
@Binding var reset: Bool
@Binding var node: SCNNode
fileprivate func addShapes()
let letBall1Geo = SCNSphere(radius: 0.05)
letBall1Geo.firstMaterial?.diffuse.contents = UIColor.red
letBall1Geo.firstMaterial?.specular.contents = UIColor.white
let ball1 = SCNNode(geometry: letBall1Geo)
ball1.position = SCNVector3(0, 0, 0)
self.arView.scene.rootNode.addChildNode(ball1)
let letBall2Geo = SCNSphere(radius: 0.05)
letBall2Geo.firstMaterial?.diffuse.contents = UIColor.white
letBall2Geo.firstMaterial?.specular.contents = UIColor.white
let ball2 = SCNNode(geometry: letBall2Geo)
ball1.position = SCNVector3(1, 1, 1)
self.arView.scene.rootNode.addChildNode(ball2)
let stickGeo = SCNCapsule(capRadius: 0.05, height: 0.1)
stickGeo.firstMaterial?.diffuse.contents = UIColor.blue
stickGeo.firstMaterial?.specular.contents = UIColor.white
let stick = SCNNode(geometry: stickGeo)
stick.position = SCNVector3(2, 2, 2)
self.arView.scene.rootNode.addChildNode(stick)
fileprivate func removeCube()
/////
func makeUIView(context: Context) -> ARSCNView
arView.scene = SCNScene()
arView.autoenablesDefaultLighting = true
arView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]
arView.session.run(self.config)
return arView
func updateUIView(_ uiView: ARSCNView,
context: Context)
if addBox > 0
self.addShapes()
if reset
self.removeCube()
问题在于它只添加了第一个形状 (ball1) 而不是全部。你知道为什么吗? 提前致谢!
【问题讨论】:
【参考方案1】:您的代码运行良好(位置是个问题):
import SwiftUI
import ARKit
struct ContentView : View
@State var addBox: Int = 3
@State var reset: Bool = false
@State var node: SCNNode = SCNNode()
var body: some View
return SceneKitView(addBox: $addBox,
reset: $reset,
node: $node).edgesIgnoringSafeArea(.all)
struct SceneKitView: UIViewRepresentable
let arView = ARSCNView(frame: .zero)
let config = ARWorldTrackingConfiguration()
@Binding var addBox: Int
@Binding var reset: Bool
@Binding var node: SCNNode
fileprivate func addShapes()
let letBall1Geo = SCNSphere(radius: 0.05)
letBall1Geo.firstMaterial?.diffuse.contents = UIColor.red
letBall1Geo.firstMaterial?.specular.contents = UIColor.white
let ball1 = SCNNode(geometry: letBall1Geo)
ball1.position = SCNVector3(0, 0, -1)
self.arView.scene.rootNode.addChildNode(ball1)
let letBall2Geo = SCNSphere(radius: 0.05)
letBall2Geo.firstMaterial?.diffuse.contents = UIColor.green
letBall2Geo.firstMaterial?.specular.contents = UIColor.white
let ball2 = SCNNode(geometry: letBall2Geo)
ball1.position = SCNVector3(0, 0.2, -1)
self.arView.scene.rootNode.addChildNode(ball2)
let stickGeo = SCNCapsule(capRadius: 0.05, height: 0.1)
stickGeo.firstMaterial?.diffuse.contents = UIColor.blue
stickGeo.firstMaterial?.specular.contents = UIColor.white
let stick = SCNNode(geometry: stickGeo)
stick.position = SCNVector3(0, 0.4, -1)
self.arView.scene.rootNode.addChildNode(stick)
func makeUIView(context: Context) -> ARSCNView
arView.scene = SCNScene()
arView.autoenablesDefaultLighting = true
arView.debugOptions = [.showFeaturePoints, .showWorldOrigin]
arView.session.run(self.config)
return arView
func updateUIView(_ uiView: ARSCNView, context: Context)
if addBox > 0
self.addShapes()
【讨论】:
感谢您的帮助!以上是关于同时添加多个 SCNNode(s)的主要内容,如果未能解决你的问题,请参考以下文章