首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SCNNode未显示

SCNNode未显示
EN

Stack Overflow用户
提问于 2021-05-18 09:24:26
回答 1查看 75关注 0票数 1

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

代码语言:javascript
复制
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“时调用该函数。

代码语言:javascript
复制
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()
    }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-18 17:53:28

SceneKitView使用此方法

代码语言:javascript
复制
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

代码语言:javascript
复制
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()
                }
            }
        }
    }
}

附注:

但是,模拟器中的ARSCNView出现了一个奇怪的问题-按下按钮后,只有在使用.allowsCameraControl = true轻击屏幕后才会出现SCNBox。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67578800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档