ARKit中节点功能的渲染器didUpdate未执行

时间:2019-03-06 14:19:50

标签: ios swift arkit

我试图在XCode中运行以下代码:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    // Checking if the planeAnchor exists
    guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

    // Declaring some variables for future use
    let width = CGFloat(planeAnchor.extent.x)
    let height = CGFloat(planeAnchor.extent.z)
    //The plane(geometry) for the node
    let plane = SCNPlane(width: width, height: height)

    // The planeNode for the plane anchor
    let planeNode = SCNNode(geometry: plane)
    //Setting up the plane node
    planeNode.geometry?.firstMaterial?.diffuse.contents = UIColor.init(red: 0, green: 0, blue: 1, alpha: 0.4)
    planeNode.geometry?.firstMaterial?.isDoubleSided = true

    // Some variables for future use
    let x = CGFloat(planeAnchor.center.x)
    let y = CGFloat(planeAnchor.center.y)
    let z = CGFloat(planeAnchor.center.z)
    // The plane node's position
    planeNode.position = SCNVector3(x,y,z)
    // This is done so that the plane node is horizontal
    planeNode.eulerAngles.x = -.pi / 2

    // Adding the planeNode to the rootNode??
    node.addChildNode(planeNode)

    // Informing the user that a plane anchor has been found
    mainLabel.text = "Found a plane anchor!"

}

这是viewDidLoad函数的代码:

override public func viewDidLoad() {
    sceneView.session.run(configuration)
    sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]
    view.addSubview(sceneView)
    sceneView.delegate = self
    sceneView.session.delegate = self
    configuration.planeDetection = .horizontal
    //Setup constraints
    setupConstraints()
}

由于某些原因,渲染器的didUpdate节点的forAnchor函数未执行。 mainLabel的文本完全不变,并且视图中从未添加平面。该课程是公开的。可能是什么原因呢?我该如何解决? 请帮忙。

谢谢。

1 个答案:

答案 0 :(得分:1)

这里的问题是您在完全设置配置之前运行了会话。在运行会话之前,应完成任何会话配置代码。试试这个:

override public func viewDidLoad() {

    sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]

    view.addSubview(sceneView)

    sceneView.delegate = self
    sceneView.session.delegate = self

    // setup the plane detection FIRST
    configuration.planeDetection = .horizontal

    // then run the session
    sceneView.session.run(configuration)

    //Setup constraints
    setupConstraints()
}