i have write code to track car wheel and when wheel detected SCNScene object ".scn" are placed automatically

now i tried to add some more options to replace automatically placed wheel with new wheel ".scn" object

but i always get error old placed ".scn" object dont change

 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
    }

when i click in button object added but old object not removed


  @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)
         }

1 Answers

0
MasDennis On

You need to either

  • call removeFromParentNode() on the node you want to remove, or

  • remove all the child nodes of the containing node using:

node.childNodes.forEach {
     $0.removeFromParentNode()
}

As an aside, its always good practice to use SCNReferenceNode when loading .scn files.