首页 > 解决方案 > 如何在 ARKit 中停止实时跟踪图像并将 SCNNode 保留在场景中?

问题描述

我可以跟踪一个对象并在其上方放置一个文本,它工作得非常好。我使用这个函数来创建我的 SCNNode:

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        let node = SCNNode()
        if let _ = anchor as? ARImageAnchor {
            let virtualNode = VirtualObjectNode(text: "ho ho ho")
            node.addChildNode(virtualNode)
            textNode = node
            let scene = SCNScene()
            scene.rootNode.addChildNode(node)
            sceneView.scene = scene
        }
        return node
    }

现在我想将我的文本保持在它出现的第一个位置并停止跟踪对象......但我无法做到这一点。我创建了这个全局变量:

var textNode: SCNNode?

如您所见,我在创建文本后填充它,并在“didAdd node”函数中使用此代码将其添加到新场景中:

guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
guard let planeGeoemtry = ARSCNPlaneGeometry(device: sceneView.device!) else { fatalError() }
planeAnchor.addPlaneNode(on: node, geometry: planeGeoemtry, contents: UIColor.yellow.withAlphaComponent(0.1))
DispatchQueue.main.async(execute: {
    node.addChildNode(self.textNode!)
})

但它只是行不通!

标签: swiftarkitscenescnnode

解决方案


推荐阅读