【问题标题】:Adding multiple SCNNode(s) at the same time同时添加多个 SCNNode(s)
【发布时间】: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) 而不是全部。你知道为什么吗? 提前致谢!

【问题讨论】:

    标签: swift swiftui augmented-reality scenekit arkit


    【解决方案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()
            }
        }
    }
    

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 2018-03-28
    • 1970-01-01
    • 2018-10-09
    • 2019-08-11
    • 2022-11-12
    • 1970-01-01
    • 2017-12-27
    • 2019-01-29
    • 2018-08-10
    相关资源
    最近更新 更多