我已经编写了跟踪车轮的代码 并在检测到车轮的SCNScene对象“ .scn”被自动放置
现在我尝试添加更多选项来替换自动放置的车轮 带有新的“ .scn”对象
但是我总是收到错误消息,旧放置的“ .scn”对象不变
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARImageTrackingConfiguration()
guard let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "Photos", bundle: Bundle.main) else {
print("No images available")
return
}
configuration.trackingImages = trackedImages
configuration.maximumNumberOfTrackedImages = 1
sceneView.session.run(configuration)
}
func renderer( _ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
if let imageAnchor = anchor as? ARImageAnchor {
let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
plane.firstMaterial?.diffuse.contents = UIColor(white: -1, alpha: 0.8)
plane.cornerRadius = 0.7
let planeNode = SCNNode(geometry: plane)
planeNode.eulerAngles.x = -.pi / 2
let jantScene = SCNScene(named: "art.scnassets/wheel.scn")
self.jantNode = jantScene?.rootNode.childNode(withName: "wheel", recursively: true)
jantNode.position = SCNVector3Zero
jantNode.position.z = 0.01
jantNode.scale = SCNVector3(x: 0.01225, y: 0.01225, z: 0.01225)
planeNode.addChildNode(jantNode)
node.addChildNode(planeNode)
}
return node
}
当我单击按钮时,添加了对象但未删除旧对象
@objc func buttonClicked(sender : UIButton){
let plane = SCNPlane(width: 0.7, height: 0.7)
plane.firstMaterial?.diffuse.contents = UIColor(white: -1, alpha: 0.8)
plane.cornerRadius = 0.7
let planeNode = SCNNode(geometry: plane)
planeNode.eulerAngles.x = -.pi / 2
jantScene = SCNScene(named: availableWheels[1])!
newjantNode = self.jantScene?.rootNode.childNode(withName: "wheel", recursively: true)!
newjantNode.position = SCNVector3Zero
newjantNode.position.z = 0.01
newjantNode.scale = SCNVector3(x: 0.01225, y: 0.01225, z: 0.01225)
planeNode.addChildNode(newjantNode)
node.addChildNode(newjantNode)
}
答案 0 :(得分:0)
您需要
使用以下命令删除包含节点的所有子节点:
node.childNodes.forEach {
$0.removeFromParentNode()
}
顺便说一句,加载.scn
文件时总是使用SCNReferenceNode。