SCNNode 未显示

SCNNode is not showing up

我是 Swift 和 ARKit 的新手。由于某种原因,我尝试显示的 SCNNode 节点没有显示。我正在与 SwiftUI 合作。我在下一个代码块中定义了应该呈现节点的函数 addNode。

import Foundation
import ARKit
import SwiftUI

// MARK: - ARViewIndicator
struct ARViewIndicator: UIViewControllerRepresentable {
   typealias UIViewControllerType = ARView
   
   func makeUIViewController(context: Context) -> ARView {
      return ARView()
   }
   func updateUIViewController(_ uiViewController:
   ARViewIndicator.UIViewControllerType, context:
   UIViewControllerRepresentableContext<ARViewIndicator>) { }
}


class ARView: UIViewController, ARSCNViewDelegate {
    var arView: ARSCNView {
        return self.view as! ARSCNView
    }
    
    override func loadView() {
     self.view = ARSCNView(frame: .zero)
    }
    
    override func viewDidLoad() {
      super.viewDidLoad()
      arView.delegate = self
      arView.scene = SCNScene()
    }

    // MARK: - Functions for standard AR view handling
   override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)
   }
    
   override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
   }
    
   override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let configuration = ARWorldTrackingConfiguration()
        arView.debugOptions = [.showFeaturePoints, 
                               .showWorldOrigin]
        arView.session.run(configuration)
        arView.delegate = self
   }
    
   override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)
      arView.session.pause()
   }
    
    func addNode(){
        let node = SCNNode()
        node.geometry = SCNBox(width: 0.1, 
                              height: 0.1, 
                              length: 0.1, 
                       chamferRadius: 0)
        node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
        node.position = SCNVector3(0,0,0.3)
        arView.scene.rootNode.addChildNode(node)
        arView.delegate = self
        
        print(123)
    }
    
   // MARK: - ARSCNViewDelegate
   func sessionWasInterrupted(_ session: ARSession) {}
   
   func sessionInterruptionEnded(_ session: ARSession) {}
   func session(_ session: ARSession, didFailWithError error: Error)
   {}
   func session(_ session: ARSession, cameraDidChangeTrackingState
   camera: ARCamera) {}
    
}

...单击“HOME”按钮时将调用该函数

import SwiftUI
import ARKit

// MARK: - NavigationIndicator
struct NavigationIndicator: UIViewControllerRepresentable {
   typealias UIViewControllerType = ARView
   func makeUIViewController(context: Context) -> ARView {
      return ARView()
   }
   func updateUIViewController(_ uiViewController:
   NavigationIndicator.UIViewControllerType, context:
   UIViewControllerRepresentableContext<NavigationIndicator>) { }
}

struct ContentView: View {
   @State var page = "Home"
   
   var body: some View {
      VStack {
        ZStack {
           NavigationIndicator()
            VStack {
                Spacer()
                HStack {
                    Button("Home") {
                        let ar = ARView();
                        ar.addNode()                        
                    }.padding()
                     .background(RoundedRectangle(cornerRadius: 10)
                     .foregroundColor(Color.white).opacity(0.7))
                    Spacer()
               }
          }
        }
      }
   }
}


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

你知道为什么它没有出现吗? 提前致谢!

将此方法用于 SceneKitView

import SwiftUI
import ARKit

struct SceneKitView: UIViewRepresentable {
    
    let arView = ARSCNView(frame: .zero)
    @Binding var pressed: Bool
    @Binding var node: SCNNode
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    final class Coordinator: NSObject, ARSCNViewDelegate {
        
        var control: SceneKitView
        
        init(_ control: SceneKitView) {
            self.control = control
        }
        
        func renderer(_ renderer: SCNSceneRenderer,
               updateAtTime time: TimeInterval) {
            
            if control.pressed {
                self.control.node = self.addCube()
                self.control.arView.scene.rootNode.addChildNode(control.node)
            }
        }
        
        fileprivate func addCube() -> SCNNode {
            control.node.geometry = SCNBox(width: 0.25,
                                          height: 0.25,
                                          length: 0.25,
                                   chamferRadius: 0.01)
            control.node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
            control.node.geometry?.firstMaterial?.lightingModel = .phong
            control.node.position = SCNVector3(0, 0,-2)
            return control.node
        }
    }
        
    func makeUIView(context: Context) -> ARSCNView {
        arView.scene = SCNScene()
        arView.delegate = context.coordinator
        arView.autoenablesDefaultLighting = true
        arView.debugOptions = .showFeaturePoints
        // arView.allowsCameraControl = true
        
        let config = ARWorldTrackingConfiguration()
        arView.session.run(config)
        
        return arView
    }
    
    func updateUIView(_ uiView: ARSCNView,
                       context: Context) { }
}

然后将此代码用于 ContentView

struct ContentView: View {
    
    @State var pressed: Bool = false
    @State var node = SCNNode()
       
    var body: some View {
        ZStack {
            SceneKitView(pressed: $pressed, node: $node)
            VStack {
                Spacer()
                HStack {
                    Button("Blue Cube") {
                        pressed.toggle()
                    }.padding()
                     .foregroundColor(.red)

                    Spacer()
                }
            }
        }
    }
}

P.S.

但是,模拟器中的 ARSCNView 出现了一个奇怪的问题——按下按钮后,SCNBox 仅在点击带有 .allowsCameraControl = true 的屏幕后才会出现。